static void add_rect_to_clip(const GrClip& clip, const SkRect& devRect, GrClip* out) { switch (clip.clipType()) { case GrClip::kClipStack_ClipType: { SkClipStack* stack = new SkClipStack; *stack = *clip.clipStack(); // The stack is actually in clip space not device space. SkRect clipRect = devRect; SkPoint origin = { SkIntToScalar(clip.origin().fX), SkIntToScalar(clip.origin().fY) }; clipRect.offset(origin); SkIRect iclipRect; clipRect.roundOut(&iclipRect); clipRect = SkRect::Make(iclipRect); stack->clipDevRect(clipRect, SkRegion::kIntersect_Op, false); out->setClipStack(stack, &clip.origin()); break; } case GrClip::kWideOpen_ClipType: *out = GrClip(devRect); break; case GrClip::kIRect_ClipType: { SkIRect intersect; devRect.roundOut(&intersect); if (intersect.intersect(clip.irect())) { *out = GrClip(intersect); } else { *out = clip; } break; } } }
// Ensure that the 'getConservativeBounds' calls are returning bounds clamped // to the render target static void test_clip_bounds(skiatest::Reporter* reporter, GrContext* context) { static const int kXSize = 100; static const int kYSize = 100; GrTextureDesc desc; desc.fFlags = kRenderTarget_GrTextureFlagBit; desc.fConfig = kAlpha_8_GrPixelConfig; desc.fWidth = kXSize; desc.fHeight = kYSize; GrTexture* texture = context->createUncachedTexture(desc, NULL, 0); if (!texture) { return; } SkAutoUnref au(texture); SkIRect intScreen = SkIRect::MakeWH(kXSize, kYSize); SkRect screen; screen = SkRect::MakeWH(SkIntToScalar(kXSize), SkIntToScalar(kYSize)); SkRect clipRect(screen); clipRect.outset(10, 10); // create a clip stack that will (trivially) reduce to a single rect that // is larger than the screen SkClipStack stack; stack.clipDevRect(clipRect, SkRegion::kReplace_Op, false); bool isIntersectionOfRects = true; SkRect devStackBounds; stack.getConservativeBounds(0, 0, kXSize, kYSize, &devStackBounds, &isIntersectionOfRects); // make sure that the SkClipStack is behaving itself REPORTER_ASSERT(reporter, screen == devStackBounds); REPORTER_ASSERT(reporter, isIntersectionOfRects); // wrap the SkClipStack in a GrClipData GrClipData clipData; clipData.fClipStack = &stack; SkIRect devGrClipDataBound; clipData.getConservativeBounds(texture, &devGrClipDataBound, &isIntersectionOfRects); // make sure that GrClipData is behaving itself REPORTER_ASSERT(reporter, intScreen == devGrClipDataBound); REPORTER_ASSERT(reporter, isIntersectionOfRects); }
void SkSVGDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bm, const SkRect* srcOrNull, const SkRect& dst, const SkPaint& paint, SkCanvas::SrcRectConstraint) { SkMatrix adjustedMatrix; adjustedMatrix.setRectToRect(srcOrNull ? *srcOrNull : SkRect::Make(bm.bounds()), dst, SkMatrix::kFill_ScaleToFit); adjustedMatrix.postConcat(*draw.fMatrix); SkDraw adjustedDraw(draw); adjustedDraw.fMatrix = &adjustedMatrix; SkClipStack adjustedClipStack; if (srcOrNull && *srcOrNull != SkRect::Make(bm.bounds())) { adjustedClipStack = *draw.fClipStack; adjustedClipStack.clipRect(dst, *draw.fMatrix, SkCanvas::kIntersect_Op, paint.isAntiAlias()); adjustedDraw.fClipStack = &adjustedClipStack; } drawBitmapCommon(adjustedDraw, bm, paint); }
//////////////////////////////////////////////////////////////////////////////// // verify that the top state of the stack matches the passed in state static void check_state(skiatest::Reporter* reporter, const GrClipMaskCache& cache, const SkClipStack& clip, GrTexture* mask, const SkIRect& bound) { REPORTER_ASSERT(reporter, clip.getTopmostGenID() == cache.getLastClipGenID()); REPORTER_ASSERT(reporter, mask == cache.getLastMask()); SkIRect cacheBound; cache.getLastBound(&cacheBound); REPORTER_ASSERT(reporter, bound == cacheBound); }
// Ensure that the 'getConservativeBounds' calls are returning bounds clamped // to the render target DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrClipBounds, reporter, ctxInfo) { static const int kXSize = 100; static const int kYSize = 100; const SkIRect intScreen = SkIRect::MakeWH(kXSize, kYSize); const SkRect screen = SkRect::Make(intScreen); SkRect clipRect(screen); clipRect.outset(10, 10); // create a clip stack that will (trivially) reduce to a single rect that // is larger than the screen SkClipStack stack; stack.clipRect(clipRect, SkMatrix::I(), kReplace_SkClipOp, false); bool isIntersectionOfRects = true; SkRect devStackBounds; stack.getConservativeBounds(0, 0, kXSize, kYSize, &devStackBounds, &isIntersectionOfRects); // make sure that the SkClipStack is behaving itself REPORTER_ASSERT(reporter, screen == devStackBounds); REPORTER_ASSERT(reporter, isIntersectionOfRects); // wrap the SkClipStack in a GrClip GrClipStackClip clipData(&stack); SkIRect devGrClipBound; clipData.getConservativeBounds(kXSize, kYSize, &devGrClipBound, &isIntersectionOfRects); // make sure that GrClip is behaving itself REPORTER_ASSERT(reporter, intScreen == devGrClipBound); REPORTER_ASSERT(reporter, isIntersectionOfRects); }
void SkSVGDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bm, const SkRect* srcOrNull, const SkRect& dst, const SkPaint& paint, SK_VIRTUAL_CONSTRAINT_TYPE) { SkMatrix adjustedMatrix; adjustedMatrix.setRectToRect(srcOrNull ? *srcOrNull : SkRect::Make(bm.bounds()), dst, SkMatrix::kFill_ScaleToFit); adjustedMatrix.postConcat(*draw.fMatrix); SkDraw adjustedDraw(draw); adjustedDraw.fMatrix = &adjustedMatrix; SkClipStack adjustedClipStack; if (srcOrNull && *srcOrNull != SkRect::Make(bm.bounds())) { SkRect devClipRect; draw.fMatrix->mapRect(&devClipRect, dst); adjustedClipStack = *draw.fClipStack; adjustedClipStack.clipDevRect(devClipRect, SkRegion::kIntersect_Op, paint.isAntiAlias()); adjustedDraw.fClipStack = &adjustedClipStack; } drawBitmapCommon(adjustedDraw, bm, paint); }
bool SkClipStack::operator==(const SkClipStack& b) const { if (this->getTopmostGenID() == b.getTopmostGenID()) { return true; } if (fSaveCount != b.fSaveCount || fDeque.count() != b.fDeque.count()) { return false; } SkDeque::F2BIter myIter(fDeque); SkDeque::F2BIter bIter(b.fDeque); const Element* myElement = (const Element*)myIter.next(); const Element* bElement = (const Element*)bIter.next(); while (myElement != nullptr && bElement != nullptr) { if (*myElement != *bElement) { return false; } myElement = (const Element*)myIter.next(); bElement = (const Element*)bIter.next(); } return myElement == nullptr && bElement == nullptr; }
//////////////////////////////////////////////////////////////////////////////// // basic test of the cache's base functionality: // push, pop, set, canReuse & getters static void test_cache(skiatest::Reporter* reporter, GrContext* context) { if (false) { // avoid bit rot, suppress warning createTexture(context); } GrClipMaskCache cache; cache.setContext(context); SkClipStack emptyClip; emptyClip.reset(); GrIRect emptyBound; emptyBound.setEmpty(); // check initial state check_state(reporter, cache, emptyClip, NULL, emptyBound); // set the current state GrIRect bound1; bound1.set(0, 0, 100, 100); SkClipStack clip1(bound1); GrTextureDesc desc; desc.fFlags = kRenderTarget_GrTextureFlagBit; desc.fWidth = X_SIZE; desc.fHeight = Y_SIZE; desc.fConfig = kSkia8888_PM_GrPixelConfig; cache.acquireMask(clip1, desc, bound1); GrTexture* texture1 = cache.getLastMask(); REPORTER_ASSERT(reporter, texture1); if (NULL == texture1) { return; } // check that the set took check_state(reporter, cache, clip1, texture1, bound1); REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); // push the state cache.push(); // verify that the pushed state is initially empty check_state(reporter, cache, emptyClip, NULL, emptyBound); REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); // modify the new state GrIRect bound2; bound2.set(-10, -10, 10, 10); SkClipStack clip2(bound2); cache.acquireMask(clip2, desc, bound2); GrTexture* texture2 = cache.getLastMask(); REPORTER_ASSERT(reporter, texture2); if (NULL == texture2) { return; } // check that the changes took check_state(reporter, cache, clip2, texture2, bound2); REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); // check to make sure canReuse works REPORTER_ASSERT(reporter, cache.canReuse(clip2, bound2)); REPORTER_ASSERT(reporter, !cache.canReuse(clip1, bound1)); // pop the state cache.pop(); // verify that the old state is restored check_state(reporter, cache, clip1, texture1, bound1); REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); // manually clear the state cache.reset(); // verify it is now empty check_state(reporter, cache, emptyClip, NULL, emptyBound); REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); // pop again - so there is no state cache.pop(); #if !defined(SK_DEBUG) // verify that the getters don't crash // only do in release since it generates asserts in debug check_state(reporter, cache, emptyClip, NULL, emptyBound); #endif REPORTER_ASSERT(reporter, 1 == texture1->getRefCnt()); REPORTER_ASSERT(reporter, 1 == texture2->getRefCnt()); }