void BasicThebesLayerBuffer::DrawTo(ThebesLayer* aLayer, gfxContext* aTarget, float aOpacity, Layer* aMaskLayer) { aTarget->Save(); // If the entire buffer is valid, we can just draw the whole thing, // no need to clip. But we'll still clip if clipping is cheap --- // that might let us copy a smaller region of the buffer. // Also clip to the visible region if we're told to. if (!aLayer->GetValidRegion().Contains(BufferRect()) || (ToData(aLayer)->GetClipToVisibleRegion() && !aLayer->GetVisibleRegion().Contains(BufferRect())) || IsClippingCheap(aTarget, aLayer->GetEffectiveVisibleRegion())) { // We don't want to draw invalid stuff, so we need to clip. Might as // well clip to the smallest area possible --- the visible region. // Bug 599189 if there is a non-integer-translation transform in aTarget, // we might sample pixels outside GetEffectiveVisibleRegion(), which is wrong // and may cause gray lines. gfxUtils::ClipToRegionSnapped(aTarget, aLayer->GetEffectiveVisibleRegion()); } // Pull out the mask surface and transform here, because the mask // is internal to basic layers AutoMaskData mask; if (GetMaskData(aMaskLayer, &mask)) { DrawBufferWithRotation(aTarget, aOpacity, mask.GetSurface(), &mask.GetTransform()); } else { DrawBufferWithRotation(aTarget, aOpacity); } aTarget->Restore(); }
void DecorationMap::GenVBuffers(int32_t patchId) { const TerrainGob* terrain = this->GetParent(); ImageData* imgdata = GetMaskData(); int32_t patchCell = terrain->GetPatchDim() - 1; int32_t numPatchX = (terrain->GetNumCols()-1) / patchCell; int32_t numPatchY = (terrain->GetNumRows()-1) / patchCell; float dimx = (float)imgdata->GetWidth() / numPatchX; float dimy = (float)imgdata->GetHeight() / numPatchY; Bound2di box; int32_t px = patchId % numPatchX; int32_t py = patchId / numPatchX; box.x1 = (int32_t) (px * dimx); box.x2 = box.x1 + (int32_t)dimx; box.y1 = (int32_t) (py * dimy); box.y2 = box.y1 + (int32_t)dimy; float ycol = (float)imgdata->GetHeight(); float xcol = (float)imgdata->GetWidth(); float halfu = 1.0f / ( 2.0f * xcol); float halfv = 1.0f / (2.0f * ycol); auto& list = m_listOfVBList[patchId]; list.clear(); srand(7353 + patchId * m_instId); for(int32_t y = box.y1; y < box.y2; y++) { float v = y / ycol; for(int32_t x = box.x1; x < box.x2; x++) { float u = x / xcol; uint8_t r = *((uint8_t*)imgdata->PixelAt(x,y)); if(r > 0) { for(int32_t k = 0; k < m_numOfDecoratorsPerTexel; k++) { float nu = GetRandomFloat(u-halfu, u+halfu); float nv = GetRandomFloat(v-halfv,v+halfv); float2 posn(nu,nv); list.push_back(posn); } } } }//// for(int32_t y = box.y1; y < box.y2; y++) }
void DecorationMap::GenVBuffers() { if(m_genVB == false) return; ReleaseResources(); ImageData* imgdata = GetMaskData(); const TerrainGob* terrain = this->GetParent(); const auto& patchlist = terrain->Patches(); if(!imgdata || patchlist.size() == 0) return; int32_t patchCell = terrain->GetPatchDim() - 1; int32_t numPatchX = (terrain->GetNumCols()-1) / patchCell; int32_t numPatchY = (terrain->GetNumRows()-1) / patchCell; int32_t numPatches = numPatchX * numPatchY; int32_t dimx = imgdata->GetWidth() / numPatchX; int32_t dimy = imgdata->GetHeight() / numPatchY; if(numPatches != (int32_t) patchlist.size()) { Logger::Log(OutputMessageType::Error,"DecoMap: wrong number of terrain patches"); assert(false); return; } if(dimx < 2 || dimy < 2 ) // remX != 0 || remY != 0 { Logger::Log(OutputMessageType::Error,L"%s decoration map mask have wrong dimention", this->GetName()); assert(false); return; } m_vertexcount = 0; m_listOfVBList.resize(numPatches); for(int32_t patchId = 0; patchId < numPatches; patchId++) { GenVBuffers(patchId); } m_vertexcount = ComputeTotalVertexCount(); m_genVB = false; }
void DecorationMap::Invoke(wchar_t* fn, const void* arg, void** retVal) { TerrainMap::Invoke(fn,arg,retVal); if(StrUtils::Equal(fn,L"ApplyDirtyRegion")) { bool valid = arg != NULL; assert(valid); if(!valid) return; Bound2di box = *(Bound2di*)arg; ImageData* imgdata = GetMaskData(); m_tmpPatchSet.clear(); const TerrainGob* terrain = this->GetParent(); const TerrainPatchList& patches = terrain->Patches(); if(m_listOfVBList.size() == 0 || patches.size() == 0 || imgdata == NULL) return; int32_t patchCell = terrain->GetPatchDim() - 1; int32_t numPatchX = (terrain->GetNumCols()-1) / patchCell; int32_t numPatchY = (terrain->GetNumRows()-1) / patchCell; float dimx = (float)imgdata->GetWidth() / numPatchX; float dimy = (float)imgdata->GetHeight() / numPatchY; m_tmpPatchSet.clear(); for(int32_t y = box.y1; y < box.y2; y++) { for(int32_t x = box.x1; x < box.x2; x++) { int32_t patchIndex = (int32_t)(y / dimy) * numPatchX + (int32_t)( x / dimx); m_tmpPatchSet.insert(patchIndex); } } for(auto it = m_tmpPatchSet.begin(); it != m_tmpPatchSet.end(); it++) { GenVBuffers(*it); } m_vertexcount = ComputeTotalVertexCount(); } }
void BasicThebesLayer::PaintThebes(gfxContext* aContext, Layer* aMaskLayer, LayerManager::DrawThebesLayerCallback aCallback, void* aCallbackData, ReadbackProcessor* aReadback) { PROFILER_LABEL("BasicThebesLayer", "PaintThebes"); NS_ASSERTION(BasicManager()->InDrawing(), "Can only draw in drawing phase"); nsRefPtr<gfxASurface> targetSurface = aContext->CurrentSurface(); if (!mContentClient) { // we pass a null pointer for the Forwarder argument, which means // this will not have a ContentHost on the other side. mContentClient = new ContentClientBasic(nullptr, BasicManager()); } nsTArray<ReadbackProcessor::Update> readbackUpdates; if (aReadback && UsedForReadback()) { aReadback->GetThebesLayerUpdates(this, &readbackUpdates); } //TODO: This is going to copy back pixels that we might end up // drawing over anyway. It would be nice if we could avoid // this duplication. mContentClient->SyncFrontBufferToBackBuffer(); bool canUseOpaqueSurface = CanUseOpaqueSurface(); ContentType contentType = canUseOpaqueSurface ? gfxASurface::CONTENT_COLOR : gfxASurface::CONTENT_COLOR_ALPHA; float opacity = GetEffectiveOpacity(); if (!BasicManager()->IsRetained()) { NS_ASSERTION(readbackUpdates.IsEmpty(), "Can't do readback for non-retained layer"); mValidRegion.SetEmpty(); mContentClient->Clear(); nsIntRegion toDraw = IntersectWithClip(GetEffectiveVisibleRegion(), aContext); RenderTraceInvalidateStart(this, "FFFF00", toDraw.GetBounds()); if (!toDraw.IsEmpty() && !IsHidden()) { if (!aCallback) { BasicManager()->SetTransactionIncomplete(); return; } aContext->Save(); bool needsClipToVisibleRegion = GetClipToVisibleRegion(); bool needsGroup = opacity != 1.0 || GetOperator() != gfxContext::OPERATOR_OVER || aMaskLayer; nsRefPtr<gfxContext> groupContext; if (needsGroup) { groupContext = BasicManager()->PushGroupForLayer(aContext, this, toDraw, &needsClipToVisibleRegion); if (GetOperator() != gfxContext::OPERATOR_OVER) { needsClipToVisibleRegion = true; } } else { groupContext = aContext; } SetAntialiasingFlags(this, groupContext); aCallback(this, groupContext, toDraw, CLIP_NONE, nsIntRegion(), aCallbackData); if (needsGroup) { BasicManager()->PopGroupToSourceWithCachedSurface(aContext, groupContext); if (needsClipToVisibleRegion) { gfxUtils::ClipToRegion(aContext, toDraw); } AutoSetOperator setOperator(aContext, GetOperator()); PaintWithMask(aContext, opacity, aMaskLayer); } aContext->Restore(); } RenderTraceInvalidateEnd(this, "FFFF00"); return; } { uint32_t flags = 0; #ifndef MOZ_WIDGET_ANDROID if (BasicManager()->CompositorMightResample()) { flags |= ThebesLayerBuffer::PAINT_WILL_RESAMPLE; } if (!(flags & ThebesLayerBuffer::PAINT_WILL_RESAMPLE)) { if (MayResample()) { flags |= ThebesLayerBuffer::PAINT_WILL_RESAMPLE; } } #endif if (mDrawAtomically) { flags |= ThebesLayerBuffer::PAINT_NO_ROTATION; } PaintState state = mContentClient->BeginPaintBuffer(this, contentType, flags); mValidRegion.Sub(mValidRegion, state.mRegionToInvalidate); if (state.mContext) { // The area that became invalid and is visible needs to be repainted // (this could be the whole visible area if our buffer switched // from RGB to RGBA, because we might need to repaint with // subpixel AA) state.mRegionToInvalidate.And(state.mRegionToInvalidate, GetEffectiveVisibleRegion()); nsIntRegion extendedDrawRegion = state.mRegionToDraw; SetAntialiasingFlags(this, state.mContext); RenderTraceInvalidateStart(this, "FFFF00", state.mRegionToDraw.GetBounds()); PaintBuffer(state.mContext, state.mRegionToDraw, extendedDrawRegion, state.mRegionToInvalidate, state.mDidSelfCopy, state.mClip, aCallback, aCallbackData); MOZ_LAYERS_LOG_IF_SHADOWABLE(this, ("Layer::Mutated(%p) PaintThebes", this)); Mutated(); RenderTraceInvalidateEnd(this, "FFFF00"); } else { // It's possible that state.mRegionToInvalidate is nonempty here, // if we are shrinking the valid region to nothing. So use mRegionToDraw // instead. NS_WARN_IF_FALSE(state.mRegionToDraw.IsEmpty(), "No context when we have something to draw, resource exhaustion?"); } } if (BasicManager()->IsTransactionIncomplete()) return; gfxRect clipExtents; clipExtents = aContext->GetClipExtents(); // Pull out the mask surface and transform here, because the mask // is internal to basic layers AutoMaskData mask; gfxASurface* maskSurface = nullptr; const gfxMatrix* maskTransform = nullptr; if (GetMaskData(aMaskLayer, &mask)) { maskSurface = mask.GetSurface(); maskTransform = &mask.GetTransform(); } if (!IsHidden() && !clipExtents.IsEmpty()) { AutoSetOperator setOperator(aContext, GetOperator()); mContentClient->DrawTo(this, aContext, opacity, maskSurface, maskTransform); } for (uint32_t i = 0; i < readbackUpdates.Length(); ++i) { ReadbackProcessor::Update& update = readbackUpdates[i]; nsIntPoint offset = update.mLayer->GetBackgroundLayerOffset(); nsRefPtr<gfxContext> ctx = update.mLayer->GetSink()->BeginUpdate(update.mUpdateRect + offset, update.mSequenceCounter); if (ctx) { NS_ASSERTION(opacity == 1.0, "Should only read back opaque layers"); ctx->Translate(gfxPoint(offset.x, offset.y)); mContentClient->DrawTo(this, ctx, 1.0, maskSurface, maskTransform); update.mLayer->GetSink()->EndUpdate(ctx, update.mUpdateRect + offset); } } }
void BasicThebesLayer::PaintThebes(gfxContext* aContext, Layer* aMaskLayer, LayerManager::DrawThebesLayerCallback aCallback, void* aCallbackData, ReadbackProcessor* aReadback) { PROFILER_LABEL("BasicThebesLayer", "PaintThebes"); NS_ASSERTION(BasicManager()->InDrawing(), "Can only draw in drawing phase"); nsTArray<ReadbackProcessor::Update> readbackUpdates; if (aReadback && UsedForReadback()) { aReadback->GetThebesLayerUpdates(this, &readbackUpdates); } float opacity = GetEffectiveOpacity(); CompositionOp effectiveOperator = GetEffectiveOperator(this); if (!BasicManager()->IsRetained()) { NS_ASSERTION(readbackUpdates.IsEmpty(), "Can't do readback for non-retained layer"); mValidRegion.SetEmpty(); mContentClient->Clear(); nsIntRegion toDraw = IntersectWithClip(GetEffectiveVisibleRegion(), aContext); RenderTraceInvalidateStart(this, "FFFF00", toDraw.GetBounds()); if (!toDraw.IsEmpty() && !IsHidden()) { if (!aCallback) { BasicManager()->SetTransactionIncomplete(); return; } aContext->Save(); bool needsClipToVisibleRegion = GetClipToVisibleRegion(); bool needsGroup = opacity != 1.0 || effectiveOperator != CompositionOp::OP_OVER || aMaskLayer; nsRefPtr<gfxContext> groupContext; if (needsGroup) { groupContext = BasicManager()->PushGroupForLayer(aContext, this, toDraw, &needsClipToVisibleRegion); if (effectiveOperator != CompositionOp::OP_OVER) { needsClipToVisibleRegion = true; } } else { groupContext = aContext; } SetAntialiasingFlags(this, groupContext); aCallback(this, groupContext, toDraw, DrawRegionClip::CLIP_NONE, nsIntRegion(), aCallbackData); if (needsGroup) { BasicManager()->PopGroupToSourceWithCachedSurface(aContext, groupContext); if (needsClipToVisibleRegion) { gfxUtils::ClipToRegion(aContext, toDraw); } AutoSetOperator setOptimizedOperator(aContext, ThebesOp(effectiveOperator)); PaintWithMask(aContext, opacity, aMaskLayer); } aContext->Restore(); } RenderTraceInvalidateEnd(this, "FFFF00"); return; } if (BasicManager()->IsTransactionIncomplete()) return; gfxRect clipExtents; clipExtents = aContext->GetClipExtents(); // Pull out the mask surface and transform here, because the mask // is internal to basic layers AutoMoz2DMaskData mask; SourceSurface* maskSurface = nullptr; Matrix maskTransform; if (GetMaskData(aMaskLayer, &mask)) { maskSurface = mask.GetSurface(); maskTransform = mask.GetTransform(); } if (!IsHidden() && !clipExtents.IsEmpty()) { mContentClient->DrawTo(this, aContext->GetDrawTarget(), opacity, GetOperator(), maskSurface, &maskTransform); } for (uint32_t i = 0; i < readbackUpdates.Length(); ++i) { ReadbackProcessor::Update& update = readbackUpdates[i]; nsIntPoint offset = update.mLayer->GetBackgroundLayerOffset(); nsRefPtr<gfxContext> ctx = update.mLayer->GetSink()->BeginUpdate(update.mUpdateRect + offset, update.mSequenceCounter); if (ctx) { NS_ASSERTION(opacity == 1.0, "Should only read back opaque layers"); ctx->Translate(gfxPoint(offset.x, offset.y)); mContentClient->DrawTo(this, ctx->GetDrawTarget(), 1.0, CompositionOpForOp(ctx->CurrentOperator()), maskSurface, &maskTransform); update.mLayer->GetSink()->EndUpdate(ctx, update.mUpdateRect + offset); } } }