bool intersects(SkRect tRect, float tRot, TextDrawInfo* s) { float sRot = s->pathRotate; if (abs(tRot) < M_PI / 15 && abs(sRot) < M_PI / 15) { return SkRect::Intersects(tRect, s->bounds); } float dist = sqrt(sqr(tRect.centerX() - s->bounds.centerX()) + sqr(tRect.centerY() - s->bounds.centerY())); if(dist < 3) { return true; } SkRect sRect = s->bounds; // difference close to 90/270 degrees if(abs(cos(tRot-sRot)) < 0.3 ){ // rotate one rectangle to 90 degrees tRot += M_PI_2; tRect = SkRect::MakeXYWH(tRect.centerX() - tRect.height() / 2, tRect.centerY() - tRect.width() / 2, tRect.height(), tRect.width()); } // determine difference close to 180/0 degrees if(abs(sin(tRot-sRot)) < 0.3){ // rotate t box // (calculate offset for t center suppose we rotate around s center) float diff = atan2(tRect.centerY() - sRect.centerY(), tRect.centerX() - sRect.centerX()); diff -= sRot; float left = sRect.centerX() + dist* cos(diff) - tRect.width()/2; float top = sRect.centerY() - dist* sin(diff) - tRect.height()/2; SkRect nRect = SkRect::MakeXYWH(left, top, tRect.width(), tRect.height()); return SkRect::Intersects(nRect, sRect); } // TODO other cases not covered return SkRect::Intersects(tRect, sRect); }
GrTexture* GaussianBlur(GrContext* context, GrTexture* srcTexture, bool canClobberSrc, const SkRect& rect, bool cropToRect, float sigmaX, float sigmaY) { SkASSERT(context); SkIRect clearRect; int scaleFactorX, radiusX; int scaleFactorY, radiusY; int maxTextureSize = context->getMaxTextureSize(); sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY); SkRect srcRect(rect); scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); srcRect.roundOut(&srcRect); scale_rect(&srcRect, static_cast<float>(scaleFactorX), static_cast<float>(scaleFactorY)); // setup new clip GrClip clip(SkRect::MakeWH(srcRect.width(), srcRect.height())); SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() || kRGBA_8888_GrPixelConfig == srcTexture->config() || kAlpha_8_GrPixelConfig == srcTexture->config()); GrSurfaceDesc desc; desc.fFlags = kRenderTarget_GrSurfaceFlag; desc.fWidth = SkScalarFloorToInt(srcRect.width()); desc.fHeight = SkScalarFloorToInt(srcRect.height()); desc.fConfig = srcTexture->config(); GrTexture* dstTexture; GrTexture* tempTexture; SkAutoTUnref<GrTexture> temp1, temp2; temp1.reset(context->textureProvider()->refScratchTexture( desc, GrTextureProvider::kApprox_ScratchTexMatch)); dstTexture = temp1.get(); if (canClobberSrc) { tempTexture = srcTexture; } else { temp2.reset(context->textureProvider()->refScratchTexture( desc, GrTextureProvider::kApprox_ScratchTexMatch)); tempTexture = temp2.get(); } if (NULL == dstTexture || NULL == tempTexture) { return NULL; } GrDrawContext* drawContext = context->drawContext(); if (!drawContext) { return NULL; } for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { GrPaint paint; SkMatrix matrix; matrix.setIDiv(srcTexture->width(), srcTexture->height()); SkRect dstRect(srcRect); if (cropToRect && i == 1) { dstRect.offset(-dstRect.fLeft, -dstRect.fTop); SkRect domain; matrix.mapRect(&domain, rect); domain.inset(i < scaleFactorX ? SK_ScalarHalf / srcTexture->width() : 0.0f, i < scaleFactorY ? SK_ScalarHalf / srcTexture->height() : 0.0f); SkAutoTUnref<GrFragmentProcessor> fp( GrTextureDomainEffect::Create( srcTexture, matrix, domain, GrTextureDomain::kDecal_Mode, GrTextureParams::kBilerp_FilterMode)); paint.addColorProcessor(fp); } else { GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); paint.addColorTextureProcessor(srcTexture, matrix, params); } scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f, i < scaleFactorY ? 0.5f : 1.0f); drawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint, SkMatrix::I(), dstRect, srcRect); srcRect = dstRect; srcTexture = dstTexture; SkTSwap(dstTexture, tempTexture); } const SkIRect srcIRect = srcRect.roundOut(); // For really small blurs(Certainly no wider than 5x5 on desktop gpus) it is faster to just // launch a single non separable kernel vs two launches if (sigmaX > 0.0f && sigmaY > 0 && (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { // We shouldn't be scaling because this is a small size blur SkASSERT((scaleFactorX == scaleFactorY) == 1); SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); convolve_gaussian_2d(drawContext, dstTexture->asRenderTarget(), clip, srcRect, dstRect, srcTexture, radiusX, radiusY, sigmaX, sigmaY, cropToRect, srcIRect); srcTexture = dstTexture; srcRect = dstRect; SkTSwap(dstTexture, tempTexture); } else { if (sigmaX > 0.0f) { if (scaleFactorX > 1) { // Clear out a radius to the right of the srcRect to prevent the // X convolution from reading garbage. clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, radiusX, srcIRect.height()); drawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false); } SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); convolve_gaussian(drawContext, dstTexture->asRenderTarget(), clip, srcRect, dstRect, srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX, cropToRect); srcTexture = dstTexture; srcRect = dstRect; SkTSwap(dstTexture, tempTexture); } if (sigmaY > 0.0f) { if (scaleFactorY > 1 || sigmaX > 0.0f) { // Clear out a radius below the srcRect to prevent the Y // convolution from reading garbage. clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, srcIRect.width(), radiusY); drawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false); } SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); convolve_gaussian(drawContext, dstTexture->asRenderTarget(), clip, srcRect, dstRect, srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY, cropToRect); srcTexture = dstTexture; srcRect = dstRect; SkTSwap(dstTexture, tempTexture); } } if (scaleFactorX > 1 || scaleFactorY > 1) { // Clear one pixel to the right and below, to accommodate bilinear // upsampling. clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, srcIRect.width() + 1, 1); drawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false); clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, 1, srcIRect.height()); drawContext->clear(srcTexture->asRenderTarget(), &clearRect, 0x0, false); SkMatrix matrix; matrix.setIDiv(srcTexture->width(), srcTexture->height()); GrPaint paint; // FIXME: this should be mitchell, not bilinear. GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); paint.addColorTextureProcessor(srcTexture, matrix, params); SkRect dstRect(srcRect); scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY); drawContext->drawNonAARectToRect(dstTexture->asRenderTarget(), clip, paint, SkMatrix::I(), dstRect, srcRect); srcRect = dstRect; srcTexture = dstTexture; SkTSwap(dstTexture, tempTexture); } return SkRef(srcTexture); }
void onOnceBeforeDraw() override { fPaint.setAntiAlias(true); fPaint.setLCDRenderText(fLCD); SkISize size = this->getISize(); SkScalar w = SkIntToScalar(size.fWidth); SkScalar h = SkIntToScalar(size.fHeight); SK_COMPILE_ASSERT(4 == SK_ARRAY_COUNT(fTypefacesToUnref), typeface_cnt); fTypefacesToUnref[0] = sk_tool_utils::create_portable_typeface("sans-serif", SkTypeface::kNormal); fTypefacesToUnref[1] = sk_tool_utils::create_portable_typeface("sans-serif", SkTypeface::kBold); fTypefacesToUnref[2] = sk_tool_utils::create_portable_typeface("serif", SkTypeface::kNormal); fTypefacesToUnref[3] = sk_tool_utils::create_portable_typeface("serif", SkTypeface::kBold); SkRandom random; for (int i = 0; i < kCnt; ++i) { int length = random.nextRangeU(kMinLength, kMaxLength); char text[kMaxLength]; for (int j = 0; j < length; ++j) { text[j] = (char)random.nextRangeU('!', 'z'); } fStrings[i].set(text, length); fColors[i] = random.nextU(); fColors[i] |= 0xFF000000; fColors[i] = sk_tool_utils::color_to_565(fColors[i]); static const SkScalar kMinPtSize = 8.f; static const SkScalar kMaxPtSize = 32.f; fPtSizes[i] = random.nextRangeScalar(kMinPtSize, kMaxPtSize); fTypefaces[i] = fTypefacesToUnref[ random.nextULessThan(SK_ARRAY_COUNT(fTypefacesToUnref))]; SkRect r; fPaint.setColor(fColors[i]); fPaint.setTypeface(fTypefaces[i]); fPaint.setTextSize(fPtSizes[i]); fPaint.measureText(fStrings[i].c_str(), fStrings[i].size(), &r); // safeRect is set of x,y positions where we can draw the string without hitting // the GM's border. SkRect safeRect = SkRect::MakeLTRB(-r.fLeft, -r.fTop, w - r.fRight, h - r.fBottom); if (safeRect.isEmpty()) { // If we don't fit then just don't worry about how we get cliped to the device // border. safeRect = SkRect::MakeWH(w, h); } fPositions[i].fX = random.nextRangeScalar(safeRect.fLeft, safeRect.fRight); fPositions[i].fY = random.nextRangeScalar(safeRect.fTop, safeRect.fBottom); fClipRects[i] = r; fClipRects[i].offset(fPositions[i].fX, fPositions[i].fY); fClipRects[i].outset(2.f, 2.f); if (fEffectiveClip) { fClipRects[i].fRight -= 0.25f * fClipRects[i].width(); } } }
virtual void onDraw(SkCanvas* canvas) { struct FillAndName { SkPath::FillType fFill; const char* fName; }; static const FillAndName gFills[] = { {SkPath::kWinding_FillType, "Winding"}, {SkPath::kEvenOdd_FillType, "Even / Odd"}, {SkPath::kInverseWinding_FillType, "Inverse Winding"}, {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"}, }; struct StyleAndName { SkPaint::Style fStyle; const char* fName; }; static const StyleAndName gStyles[] = { {SkPaint::kFill_Style, "Fill"}, {SkPaint::kStroke_Style, "Stroke"}, {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"}, }; struct CapAndName { SkPaint::Cap fCap; SkPaint::Join fJoin; const char* fName; }; static const CapAndName gCaps[] = { {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"}, {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"}, {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"} }; struct PathAndName { SkPath fPath; const char* fName; }; PathAndName path; path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1); path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1); path.fName = "moveTo-line"; SkPaint titlePaint; titlePaint.setColor(SK_ColorBLACK); titlePaint.setAntiAlias(true); titlePaint.setLCDRenderText(true); titlePaint.setTextSize(15 * SK_Scalar1); const char title[] = "Line Drawn Into Rectangle Clips With " "Indicated Style, Fill and Linecaps, with stroke width 10"; canvas->drawText(title, strlen(title), 20 * SK_Scalar1, 20 * SK_Scalar1, titlePaint); SkLCGRandom rand; SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1); canvas->save(); canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1); canvas->save(); for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) { if (0 < cap) { canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0); } canvas->save(); for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) { if (0 < fill) { canvas->translate(0, rect.height() + 40 * SK_Scalar1); } canvas->save(); for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) { if (0 < style) { canvas->translate(rect.width() + 40 * SK_Scalar1, 0); } SkColor color = 0xff007000; this->drawPath(path.fPath, canvas, color, rect, gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle, gFills[fill].fFill, SK_Scalar1*10); SkPaint rectPaint; rectPaint.setColor(SK_ColorBLACK); rectPaint.setStyle(SkPaint::kStroke_Style); rectPaint.setStrokeWidth(-1); rectPaint.setAntiAlias(true); canvas->drawRect(rect, rectPaint); SkPaint labelPaint; labelPaint.setColor(color); labelPaint.setAntiAlias(true); labelPaint.setLCDRenderText(true); labelPaint.setTextSize(10 * SK_Scalar1); canvas->drawText(gStyles[style].fName, strlen(gStyles[style].fName), 0, rect.height() + 12 * SK_Scalar1, labelPaint); canvas->drawText(gFills[fill].fName, strlen(gFills[fill].fName), 0, rect.height() + 24 * SK_Scalar1, labelPaint); canvas->drawText(gCaps[cap].fName, strlen(gCaps[cap].fName), 0, rect.height() + 36 * SK_Scalar1, labelPaint); } canvas->restore(); } canvas->restore(); } canvas->restore(); canvas->restore(); }
void GLCircularRRectEffect::onSetData(const GrGLProgramDataManager& pdman, const GrProcessor& processor) { const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>(); const SkRRect& rrect = crre.getRRect(); if (rrect != fPrevRRect) { SkRect rect = rrect.getBounds(); SkScalar radius = 0; switch (crre.getCircularCornerFlags()) { case CircularRRectEffect::kAll_CornerFlags: SkASSERT(rrect.isSimpleCircular()); radius = rrect.getSimpleRadii().fX; SkASSERT(radius >= kRadiusMin); rect.inset(radius, radius); break; case CircularRRectEffect::kTopLeft_CornerFlag: radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; rect.fLeft += radius; rect.fTop += radius; rect.fRight += 0.5f; rect.fBottom += 0.5f; break; case CircularRRectEffect::kTopRight_CornerFlag: radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; rect.fLeft -= 0.5f; rect.fTop += radius; rect.fRight -= radius; rect.fBottom += 0.5f; break; case CircularRRectEffect::kBottomRight_CornerFlag: radius = rrect.radii(SkRRect::kLowerRight_Corner).fX; rect.fLeft -= 0.5f; rect.fTop -= 0.5f; rect.fRight -= radius; rect.fBottom -= radius; break; case CircularRRectEffect::kBottomLeft_CornerFlag: radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; rect.fLeft += radius; rect.fTop -= 0.5f; rect.fRight += 0.5f; rect.fBottom -= radius; break; case CircularRRectEffect::kLeft_CornerFlags: radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; rect.fLeft += radius; rect.fTop += radius; rect.fRight += 0.5f; rect.fBottom -= radius; break; case CircularRRectEffect::kTop_CornerFlags: radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; rect.fLeft += radius; rect.fTop += radius; rect.fRight -= radius; rect.fBottom += 0.5f; break; case CircularRRectEffect::kRight_CornerFlags: radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; rect.fLeft -= 0.5f; rect.fTop += radius; rect.fRight -= radius; rect.fBottom -= radius; break; case CircularRRectEffect::kBottom_CornerFlags: radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; rect.fLeft += radius; rect.fTop -= 0.5f; rect.fRight -= radius; rect.fBottom -= radius; break; default: SkFAIL("Should have been one of the above cases."); } pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); pdman.set1f(fRadiusPlusHalfUniform, radius + 0.5f); fPrevRRect = rrect; } }
virtual void onDrawContent(SkCanvas* canvas) { struct FillAndName { SkPath::FillType fFill; const char* fName; }; static const FillAndName gFills[] = { {SkPath::kWinding_FillType, "Winding"}, {SkPath::kEvenOdd_FillType, "Even / Odd"}, {SkPath::kInverseWinding_FillType, "Inverse Winding"}, {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"}, }; struct StyleAndName { SkPaint::Style fStyle; const char* fName; }; static const StyleAndName gStyles[] = { {SkPaint::kFill_Style, "Fill"}, {SkPaint::kStroke_Style, "Stroke"}, {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"}, }; SkPaint titlePaint; titlePaint.setColor(SK_ColorBLACK); titlePaint.setAntiAlias(true); titlePaint.setLCDRenderText(true); titlePaint.setTextSize(24 * SK_Scalar1); const char title[] = "Empty Paths Drawn Into Rectangle Clips With Indicated Style and Fill"; canvas->drawText(title, strlen(title), 40 * SK_Scalar1, 100*SK_Scalar1, titlePaint); SkRandom rand; SkRect rect = SkRect::MakeWH(125*SK_Scalar1, 100*SK_Scalar1); int i = 0; canvas->save(); canvas->translate(80 * SK_Scalar1, 0); canvas->save(); for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) { for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) { if (0 == i % 4) { canvas->restore(); canvas->translate(0, rect.height() + 50 * SK_Scalar1); canvas->save(); } else { canvas->translate(rect.width() + 100 * SK_Scalar1, 0); } ++i; SkColor color = rand.nextU(); color = 0xff000000| color; // force solid this->drawEmpty(canvas, color, rect, gStyles[style].fStyle, gFills[fill].fFill); SkPaint rectPaint; rectPaint.setColor(SK_ColorBLACK); rectPaint.setStyle(SkPaint::kStroke_Style); rectPaint.setStrokeWidth(-1); rectPaint.setAntiAlias(true); canvas->drawRect(rect, rectPaint); char label[1024]; sprintf(label, "%s, %s", gStyles[style].fName, gFills[fill].fName); SkPaint labelPaint; labelPaint.setColor(color); labelPaint.setAntiAlias(true); labelPaint.setLCDRenderText(true); canvas->drawText(label, strlen(label), 0, rect.height() + 15 * SK_Scalar1, labelPaint); } } canvas->restore(); canvas->restore(); }
bool GrStencilAndCoverPathRenderer::onDrawPath(const DrawPathArgs& args) { SkASSERT(!args.fStroke->isHairlineStyle()); const SkPath& path = *args.fPath; GrPipelineBuilder* pipelineBuilder = args.fPipelineBuilder; const SkMatrix& viewMatrix = *args.fViewMatrix; SkASSERT(pipelineBuilder->getStencil().isDisabled()); if (args.fAntiAlias) { SkASSERT(pipelineBuilder->getRenderTarget()->isStencilBufferMultisampled()); pipelineBuilder->enableState(GrPipelineBuilder::kHWAntialias_Flag); } SkAutoTUnref<GrPath> p(get_gr_path(fResourceProvider, path, *args.fStroke)); if (path.isInverseFillType()) { GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass, kKeep_StencilOp, kZero_StencilOp, // We know our rect will hit pixels outside the clip and the user bits will be 0 // outside the clip. So we can't just fill where the user bits are 0. We also need to // check that the clip bit is set. kEqualIfInClip_StencilFunc, 0xffff, 0x0000, 0xffff); pipelineBuilder->setStencil(kInvertedStencilPass); // fake inverse with a stencil and cover args.fTarget->stencilPath(*pipelineBuilder, viewMatrix, p, p->getFillType()); SkMatrix invert = SkMatrix::I(); SkRect bounds = SkRect::MakeLTRB(0, 0, SkIntToScalar(pipelineBuilder->getRenderTarget()->width()), SkIntToScalar(pipelineBuilder->getRenderTarget()->height())); SkMatrix vmi; // mapRect through persp matrix may not be correct if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) { vmi.mapRect(&bounds); // theoretically could set bloat = 0, instead leave it because of matrix inversion // precision. SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf; bounds.outset(bloat, bloat); } else { if (!viewMatrix.invert(&invert)) { return false; } } const SkMatrix& viewM = viewMatrix.hasPerspective() ? SkMatrix::I() : viewMatrix; if (pipelineBuilder->getRenderTarget()->hasMixedSamples()) { pipelineBuilder->disableState(GrPipelineBuilder::kHWAntialias_Flag); } args.fTarget->drawNonAARect(*pipelineBuilder, args.fColor, viewM, bounds, invert); } else { GR_STATIC_CONST_SAME_STENCIL(kStencilPass, kZero_StencilOp, kKeep_StencilOp, kNotEqual_StencilFunc, 0xffff, 0x0000, 0xffff); pipelineBuilder->setStencil(kStencilPass); SkAutoTUnref<GrDrawPathBatchBase> batch( GrDrawPathBatch::Create(viewMatrix, args.fColor, p->getFillType(), p)); args.fTarget->drawPathBatch(*pipelineBuilder, batch); } pipelineBuilder->stencil()->setDisabled(); return true; }
void GrAARectRenderer::StrokeAARect(GrDrawTarget* target, GrPipelineBuilder* pipelineBuilder, GrColor color, const SkMatrix& viewMatrix, const SkRect& rect, const SkRect& devRect, const SkStrokeRec& stroke) { SkVector devStrokeSize; SkScalar width = stroke.getWidth(); if (width > 0) { devStrokeSize.set(width, width); viewMatrix.mapVectors(&devStrokeSize, 1); devStrokeSize.setAbs(devStrokeSize); } else { devStrokeSize.set(SK_Scalar1, SK_Scalar1); } const SkScalar dx = devStrokeSize.fX; const SkScalar dy = devStrokeSize.fY; const SkScalar rx = SkScalarMul(dx, SK_ScalarHalf); const SkScalar ry = SkScalarMul(dy, SK_ScalarHalf); SkScalar spare; { SkScalar w = devRect.width() - dx; SkScalar h = devRect.height() - dy; spare = SkTMin(w, h); } SkRect devOutside(devRect); devOutside.outset(rx, ry); bool miterStroke = true; // For hairlines, make bevel and round joins appear the same as mitered ones. // small miter limit means right angles show bevel... if ((width > 0) && (stroke.getJoin() != SkPaint::kMiter_Join || stroke.getMiter() < SK_ScalarSqrt2)) { miterStroke = false; } if (spare <= 0 && miterStroke) { FillAARect(target, pipelineBuilder, color, viewMatrix, devOutside, devOutside); return; } SkRect devInside(devRect); devInside.inset(rx, ry); SkRect devOutsideAssist(devRect); // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist) // to draw the outer of the rect. Because there are 8 vertices on the outer // edge, while vertex number of inner edge is 4, the same as miter-stroke. if (!miterStroke) { devOutside.inset(0, ry); devOutsideAssist.outset(0, ry); } GeometryStrokeAARect(target, pipelineBuilder, color, viewMatrix, devOutside, devOutsideAssist, devInside, miterStroke); }
void SkBBoxHierarchyRecord::handleBBox(const SkRect& bounds) { SkIRect r; bounds.roundOut(&r); SkPictureStateTree::Draw* draw = fStateTree->appendDraw(this->writeStream().bytesWritten()); fBoundingHierarchy->insert(draw, r, true); }
void SkBitmapDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap, const SkRect* src, const SkRect& dst, const SkPaint& paint, SkCanvas::DrawBitmapRectFlags flags) { SkMatrix matrix; SkRect bitmapBounds, tmpSrc, tmpDst; SkBitmap tmpBitmap; bitmapBounds.isetWH(bitmap.width(), bitmap.height()); // Compute matrix from the two rectangles if (src) { tmpSrc = *src; } else { tmpSrc = bitmapBounds; } matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit); const SkRect* dstPtr = &dst; const SkBitmap* bitmapPtr = &bitmap; // clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if // needed (if the src was clipped). No check needed if src==null. if (src) { if (!bitmapBounds.contains(*src)) { if (!tmpSrc.intersect(bitmapBounds)) { return; // nothing to draw } // recompute dst, based on the smaller tmpSrc matrix.mapRect(&tmpDst, tmpSrc); dstPtr = &tmpDst; } // since we may need to clamp to the borders of the src rect within // the bitmap, we extract a subset. SkIRect srcIR; tmpSrc.roundOut(&srcIR); if(bitmap.pixelRef()->getTexture()) { // Accelerated source canvas, don't use extractSubset but readPixels to get the subset. // This way, the pixels are copied in CPU memory instead of GPU memory. bitmap.pixelRef()->readPixels(&tmpBitmap, &srcIR); } else { if (!bitmap.extractSubset(&tmpBitmap, srcIR)) { return; } } bitmapPtr = &tmpBitmap; // Since we did an extract, we need to adjust the matrix accordingly SkScalar dx = 0, dy = 0; if (srcIR.fLeft > 0) { dx = SkIntToScalar(srcIR.fLeft); } if (srcIR.fTop > 0) { dy = SkIntToScalar(srcIR.fTop); } if (dx || dy) { matrix.preTranslate(dx, dy); } SkRect extractedBitmapBounds; extractedBitmapBounds.isetWH(bitmapPtr->width(), bitmapPtr->height()); if (extractedBitmapBounds == tmpSrc) { // no fractional part in src, we can just call drawBitmap goto USE_DRAWBITMAP; } } else { USE_DRAWBITMAP: // We can go faster by just calling drawBitmap, which will concat the // matrix with the CTM, and try to call drawSprite if it can. If not, // it will make a shader and call drawRect, as we do below. this->drawBitmap(draw, *bitmapPtr, matrix, paint); return; } // construct a shader, so we can call drawRect with the dst SkShader* s = SkShader::CreateBitmapShader(*bitmapPtr, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &matrix); if (NULL == s) { return; } SkPaint paintWithShader(paint); paintWithShader.setStyle(SkPaint::kFill_Style); paintWithShader.setShader(s)->unref(); // Call ourself, in case the subclass wanted to share this setup code // but handle the drawRect code themselves. this->drawRect(draw, *dstPtr, paintWithShader); }
void generateAAFillRectGeometry(void* vertices, size_t offset, size_t vertexStride, GrColor color, const SkMatrix& viewMatrix, const SkRect& rect, const SkRect& devRect, bool tweakAlphaForCoverage) const { intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset; SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); SkScalar inset = SkMinScalar(devRect.width(), SK_Scalar1); inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); if (viewMatrix.rectStaysRect()) { set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_ScalarHalf); set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset); } else { // compute transformed (1, 0) and (0, 1) vectors SkVector vec[2] = { { viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY] }, { viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY] } }; vec[0].normalize(); vec[0].scale(SK_ScalarHalf); vec[1].normalize(); vec[1].scale(SK_ScalarHalf); // create the rotated rect fan0Pos->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vertexStride); viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4); // Now create the inset points and then outset the original // rotated points // TL *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) = *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + vec[1]; *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[1]; // BL *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) = *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - vec[1]; *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[1]; // BR *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) = *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - vec[1]; *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[1]; // TR *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) = *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + vec[1]; *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[1]; } // Make verts point to vertex color and then set all the color and coverage vertex attrs // values. verts += sizeof(SkPoint); for (int i = 0; i < 4; ++i) { if (tweakAlphaForCoverage) { *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; } else { *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0; } } int scale; if (inset < SK_ScalarHalf) { scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); SkASSERT(scale >= 0 && scale <= 255); } else { scale = 0xff; } verts += 4 * vertexStride; float innerCoverage = GrNormalizeByteToFloat(scale); GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); for (int i = 0; i < 4; ++i) { if (tweakAlphaForCoverage) { *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; } else { *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage; } } }
void SkDeferredCanvas::flush_translate(SkScalar* x, SkScalar* y, const SkPaint& paint) { SkRect tmp = SkRect::MakeXYWH(*x, *y, 1, 1); this->flush_check(&tmp, &paint, kNoClip_Flag | kNoCull_Flag | kNoScale_Flag); *x = tmp.x(); *y = tmp.y(); }
GrTexture* GaussianBlur(GrContext* context, GrTexture* srcTexture, bool canClobberSrc, const SkRect& rect, bool cropToRect, float sigmaX, float sigmaY) { SkASSERT(NULL != context); GrContext::AutoRenderTarget art(context); GrContext::AutoMatrix am; am.setIdentity(context); SkIRect clearRect; int scaleFactorX, radiusX; int scaleFactorY, radiusY; sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX); sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY); SkRect srcRect(rect); scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); srcRect.roundOut(); scale_rect(&srcRect, static_cast<float>(scaleFactorX), static_cast<float>(scaleFactorY)); GrContext::AutoClip acs(context, SkRect::MakeWH(srcRect.width(), srcRect.height())); SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() || kRGBA_8888_GrPixelConfig == srcTexture->config() || kAlpha_8_GrPixelConfig == srcTexture->config()); GrTextureDesc desc; desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; desc.fWidth = SkScalarFloorToInt(srcRect.width()); desc.fHeight = SkScalarFloorToInt(srcRect.height()); desc.fConfig = srcTexture->config(); GrAutoScratchTexture temp1, temp2; GrTexture* dstTexture = temp1.set(context, desc); GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(context, desc); if (NULL == dstTexture || NULL == tempTexture) { return NULL; } for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { GrPaint paint; SkMatrix matrix; matrix.setIDiv(srcTexture->width(), srcTexture->height()); context->setRenderTarget(dstTexture->asRenderTarget()); SkRect dstRect(srcRect); if (cropToRect && i == 1) { dstRect.offset(-dstRect.fLeft, -dstRect.fTop); SkRect domain; matrix.mapRect(&domain, rect); domain.inset(i < scaleFactorX ? SK_ScalarHalf / srcTexture->width() : 0.0f, i < scaleFactorY ? SK_ScalarHalf / srcTexture->height() : 0.0f); SkAutoTUnref<GrEffectRef> effect(GrTextureDomainEffect::Create( srcTexture, matrix, domain, GrTextureDomain::kDecal_Mode, GrTextureParams::kBilerp_FilterMode)); paint.addColorEffect(effect); } else { GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); paint.addColorTextureEffect(srcTexture, matrix, params); } scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f, i < scaleFactorY ? 0.5f : 1.0f); context->drawRectToRect(paint, dstRect, srcRect); srcRect = dstRect; srcTexture = dstTexture; SkTSwap(dstTexture, tempTexture); } SkIRect srcIRect; srcRect.roundOut(&srcIRect); if (sigmaX > 0.0f) { if (scaleFactorX > 1) { // Clear out a radius to the right of the srcRect to prevent the // X convolution from reading garbage. clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, radiusX, srcIRect.height()); context->clear(&clearRect, 0x0, false); } context->setRenderTarget(dstTexture->asRenderTarget()); SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); convolve_gaussian(context, srcRect, dstRect, srcTexture, Gr1DKernelEffect::kX_Direction, radiusX, sigmaX, cropToRect); srcTexture = dstTexture; srcRect = dstRect; SkTSwap(dstTexture, tempTexture); } if (sigmaY > 0.0f) { if (scaleFactorY > 1 || sigmaX > 0.0f) { // Clear out a radius below the srcRect to prevent the Y // convolution from reading garbage. clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, srcIRect.width(), radiusY); context->clear(&clearRect, 0x0, false); } context->setRenderTarget(dstTexture->asRenderTarget()); SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); convolve_gaussian(context, srcRect, dstRect, srcTexture, Gr1DKernelEffect::kY_Direction, radiusY, sigmaY, cropToRect); srcTexture = dstTexture; srcRect = dstRect; SkTSwap(dstTexture, tempTexture); } if (scaleFactorX > 1 || scaleFactorY > 1) { // Clear one pixel to the right and below, to accommodate bilinear // upsampling. clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom, srcIRect.width() + 1, 1); context->clear(&clearRect, 0x0, false); clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop, 1, srcIRect.height()); context->clear(&clearRect, 0x0, false); SkMatrix matrix; matrix.setIDiv(srcTexture->width(), srcTexture->height()); context->setRenderTarget(dstTexture->asRenderTarget()); GrPaint paint; // FIXME: this should be mitchell, not bilinear. GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); paint.addColorTextureEffect(srcTexture, matrix, params); SkRect dstRect(srcRect); scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY); context->drawRectToRect(paint, dstRect, srcRect); srcRect = dstRect; srcTexture = dstTexture; SkTSwap(dstTexture, tempTexture); } if (srcTexture == temp1.texture()) { return temp1.detach(); } else if (srcTexture == temp2.texture()) { return temp2.detach(); } else { srcTexture->ref(); return srcTexture; } }
void drawTextOverCanvas(RenderingContext* rc, SkCanvas* cv) { SkRect r = SkRect::MakeLTRB(0, 0, rc->width, rc->height); r.inset(-100, -100); quad_tree<TextDrawInfo*> boundsIntersect(r, 4, 0.6); SkPaint paintIcon; paintIcon.setStyle(SkPaint::kStroke_Style); paintIcon.setStrokeWidth(1); paintIcon.setColor(0xff000000); SkPaint paintText; paintText.setStyle(SkPaint::kFill_Style); paintText.setStrokeWidth(1); paintText.setColor(0xff000000); paintText.setTextAlign(SkPaint::kCenter_Align); paintText.setTypeface(serif); paintText.setAntiAlias(true); SkPaint::FontMetrics fm; // 1. Sort text using text order std::sort(rc->textToDraw.begin(), rc->textToDraw.end(), textOrder); uint size = rc->textToDraw.size(); for (uint i = 0; i < size; i++) { TextDrawInfo* text = rc->textToDraw.at(i); if (text->text.length() > 0) { size_t d = text->text.find(DELIM_CHAR); // not used now functionality // possibly it will be used specifying english names after that character if (d > 0) { text->text = text->text.substr(0, d); } // sest text size before finding intersection (it is used there) float textSize = getDensityValue(rc, text->textSize); paintText.setTextSize(textSize); paintText.setFakeBoldText(text->bold); paintText.setColor(text->textColor); // align center y paintText.getFontMetrics(&fm); text->centerY += (-fm.fAscent); // calculate if there is intersection bool intersects = findTextIntersection(cv, rc, boundsIntersect, text, &paintText, &paintIcon); if (!intersects) { if(rc->interrupted()){ return; } if (text->drawOnPath && text->path != NULL) { if (text->textShadow > 0) { paintText.setColor(0xFFFFFFFF); paintText.setStyle(SkPaint::kStroke_Style); paintText.setStrokeWidth(2 + text->textShadow); rc->nativeOperations.pause(); cv->drawTextOnPathHV(text->text.c_str(), text->text.length(), *text->path, text->hOffset, text->vOffset, paintText); rc->nativeOperations.start(); // reset paintText.setStyle(SkPaint::kFill_Style); paintText.setStrokeWidth(2); paintText.setColor(text->textColor); } rc->nativeOperations.pause(); cv->drawTextOnPathHV(text->text.c_str(), text->text.length(), *text->path, text->hOffset, text->vOffset, paintText); rc->nativeOperations.start(); } else { if (text->shieldRes.length() > 0) { SkBitmap* ico = getCachedBitmap(rc, text->shieldRes); if (ico != NULL) { rc->nativeOperations.pause(); cv->drawBitmap(*ico, text->centerX - ico->width() / 2 - 0.5f, text->centerY - ico->height() / 2 - getDensityValue(rc, 4.5f), &paintIcon); rc->nativeOperations.start(); } } drawWrappedText(rc, cv, text, textSize, paintText); } } } } }
static SkRect inset(const SkRect& r) { SkRect rect = r; rect.inset(r.width() / 8, r.height() / 8); return rect; }
void NativeImageSkia::drawPattern( GraphicsContext* context, const FloatRect& floatSrcRect, const FloatSize& scale, const FloatPoint& phase, CompositeOperator compositeOp, const FloatRect& destRect, WebBlendMode blendMode, const IntSize& repeatSpacing) const { FloatRect normSrcRect = floatSrcRect; normSrcRect.intersect(FloatRect(0, 0, bitmap().width(), bitmap().height())); if (destRect.isEmpty() || normSrcRect.isEmpty()) return; // nothing to draw SkMatrix totalMatrix = context->getTotalMatrix(); AffineTransform ctm = context->getCTM(); SkScalar ctmScaleX = ctm.xScale(); SkScalar ctmScaleY = ctm.yScale(); totalMatrix.preScale(scale.width(), scale.height()); // Figure out what size the bitmap will be in the destination. The // destination rect is the bounds of the pattern, we need to use the // matrix to see how big it will be. SkRect destRectTarget; totalMatrix.mapRect(&destRectTarget, normSrcRect); float destBitmapWidth = SkScalarToFloat(destRectTarget.width()); float destBitmapHeight = SkScalarToFloat(destRectTarget.height()); bool isLazyDecoded = DeferredImageDecoder::isLazyDecoded(bitmap()); // Compute the resampling mode. InterpolationQuality resampling; if (context->isAccelerated()) resampling = InterpolationLow; else if (isLazyDecoded) resampling = InterpolationHigh; else resampling = computeInterpolationQuality(totalMatrix, normSrcRect.width(), normSrcRect.height(), destBitmapWidth, destBitmapHeight, isDataComplete()); resampling = limitInterpolationQuality(context, resampling); SkMatrix localMatrix; // We also need to translate it such that the origin of the pattern is the // origin of the destination rect, which is what WebKit expects. Skia uses // the coordinate system origin as the base for the pattern. If WebKit wants // a shifted image, it will shift it from there using the localMatrix. const float adjustedX = phase.x() + normSrcRect.x() * scale.width(); const float adjustedY = phase.y() + normSrcRect.y() * scale.height(); localMatrix.setTranslate(SkFloatToScalar(adjustedX), SkFloatToScalar(adjustedY)); sk_sp<SkShader> shader; SkFilterQuality filterLevel = static_cast<SkFilterQuality>(resampling); // Bicubic filter is only applied to defer-decoded images, see // NativeImageSkia::draw for details. if (resampling == InterpolationHigh && !isLazyDecoded) { // Do nice resampling. filterLevel = kNone_SkFilterQuality; float scaleX = destBitmapWidth / normSrcRect.width(); float scaleY = destBitmapHeight / normSrcRect.height(); SkRect scaledSrcRect; // Since we are resizing the bitmap, we need to remove the scale // applied to the pixels in the bitmap shader. This means we need // CTM * localMatrix to have identity scale. Since we // can't modify CTM (or the rectangle will be drawn in the wrong // place), we must set localMatrix's scale to the inverse of // CTM scale. localMatrix.preScale(ctmScaleX ? 1 / ctmScaleX : 1, ctmScaleY ? 1 / ctmScaleY : 1); // The image fragment generated here is not exactly what is // requested. The scale factor used is approximated and image // fragment is slightly larger to align to integer // boundaries. SkBitmap resampled = extractScaledImageFragment(normSrcRect, scaleX, scaleY, &scaledSrcRect); if (repeatSpacing.isZero()) { shader = SkShader::MakeBitmapShader(resampled, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, &localMatrix); } else { shader = SkShader::MakeBitmapShader( createBitmapWithSpace(resampled, repeatSpacing.width() * ctmScaleX, repeatSpacing.height() * ctmScaleY), SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, &localMatrix); } } else { // Because no resizing occurred, the shader transform should be // set to the pattern's transform, which just includes scale. localMatrix.preScale(scale.width(), scale.height()); // No need to resample before drawing. SkBitmap srcSubset; bitmap().extractSubset(&srcSubset, enclosingIntRect(normSrcRect)); if (repeatSpacing.isZero()) { shader = SkShader::MakeBitmapShader(srcSubset, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, &localMatrix); } else { shader = SkShader::MakeBitmapShader( createBitmapWithSpace(srcSubset, repeatSpacing.width() * ctmScaleX, repeatSpacing.height() * ctmScaleY), SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, &localMatrix); } } SkPaint paint; paint.setShader(shader); paint.setXfermodeMode(WebCoreCompositeToSkiaComposite(compositeOp, blendMode)); paint.setColorFilter(sk_ref_sp(context->colorFilter())); paint.setFilterQuality(filterLevel); context->drawRect(destRect, paint); }
IntRect::operator SkRect() const { SkRect rect; rect.set(SkIntToScalar(x()), SkIntToScalar(y()), SkIntToScalar(maxX()), SkIntToScalar(maxY())); return rect; }
bool VideoLayerAndroid::drawGL(bool layerTilesDisabled) { // Lazily allocated the textures. TilesManager* tilesManager = TilesManager::instance(); VideoLayerManager* manager = tilesManager->videoLayerManager(); manager->initGLResourcesIfNeeded(); ShaderProgram* shader = tilesManager->shader(); SkRect rect = SkRect::MakeSize(getSize()); GLfloat surfaceMatrix[16]; // Calculate the video rect based on the aspect ratio and the element rect. SkRect videoRect = calVideoRect(rect); PureColorQuadData pureColorQuadData(Color(0, 0, 0, 255), LayerQuad, &m_drawTransform, &rect); if (videoRect != rect) { // Paint the whole video element with black color when video content // can't cover the whole area. shader->drawQuad(&pureColorQuadData); } // Inner rect is for the progressing / play / pause animation. SkRect innerRect = SkRect::MakeWH(manager->getButtonSize(), manager->getButtonSize()); if (innerRect.contains(videoRect)) innerRect = videoRect; double buttonSize = manager->getButtonSize(); innerRect.offset(videoRect.fLeft + (videoRect.width() - buttonSize) / 2, videoRect.fTop + (videoRect.height() - buttonSize) / 2); // When we are drawing the animation of the play/pause button in the // middle of the video, we need to ask for redraw. bool needRedraw = false; TextureQuadData iconQuadData(0, GL_TEXTURE_2D, GL_LINEAR, LayerQuad, &m_drawTransform, &innerRect); // Draw the poster image, the progressing image or the Video depending // on the player's state. if (m_playerState == PREPARING) { // Show the progressing animation, with two rotating circles showPreparingAnimation(videoRect, innerRect); needRedraw = true; } else if (m_playerState == PLAYING && m_surfaceTexture.get()) { // Show the real video. m_surfaceTexture->updateTexImage(); m_surfaceTexture->getTransformMatrix(surfaceMatrix); GLuint textureId = manager->getTextureId(uniqueId()); shader->drawVideoLayerQuad(m_drawTransform, surfaceMatrix, videoRect, textureId); manager->updateMatrix(uniqueId(), surfaceMatrix); // Use the scale to control the fading the sizing during animation double scale = manager->drawIcon(uniqueId(), PlayIcon); if (scale) { innerRect.inset(manager->getButtonSize() / 4 * scale, manager->getButtonSize() / 4 * scale); iconQuadData.updateTextureId(manager->getPlayTextureId()); iconQuadData.updateOpacity(scale); shader->drawQuad(&iconQuadData); needRedraw = true; } } else { GLuint textureId = manager->getTextureId(uniqueId()); GLfloat* matrix = manager->getMatrix(uniqueId()); if (textureId && matrix) { // Show the screen shot for each video. shader->drawVideoLayerQuad(m_drawTransform, matrix, videoRect, textureId); } else { // Show the static poster b/c there is no screen shot available. pureColorQuadData.updateColor(Color(128, 128, 128, 255)); shader->drawQuad(&pureColorQuadData); iconQuadData.updateTextureId(manager->getPosterTextureId()); iconQuadData.updateOpacity(1.0); shader->drawQuad(&iconQuadData); } // Use the scale to control the fading and the sizing during animation. double scale = manager->drawIcon(uniqueId(), PauseIcon); if (scale) { innerRect.inset(manager->getButtonSize() / 4 * scale, manager->getButtonSize() / 4 * scale); iconQuadData.updateTextureId(manager->getPauseTextureId()); iconQuadData.updateOpacity(scale); shader->drawQuad(&iconQuadData); needRedraw = true; } } return needRedraw; }
void RenderNode::pushLayerUpdate(TreeInfo& info) { LayerType layerType = properties().effectiveLayerType(); // If we are not a layer OR we cannot be rendered (eg, view was detached) // we need to destroy any Layers we may have had previously if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable())) { if (CC_UNLIKELY(mLayer)) { destroyLayer(mLayer); mLayer = nullptr; } return; } bool transformUpdateNeeded = false; if (!mLayer) { mLayer = createLayer(info.canvasContext.getRenderState(), getWidth(), getHeight()); #if !HWUI_NEW_OPS applyLayerPropertiesToLayer(info); #endif damageSelf(info); transformUpdateNeeded = true; } else if (!layerMatchesWidthAndHeight(mLayer, getWidth(), getHeight())) { #if HWUI_NEW_OPS // TODO: remove now irrelevant, currently enqueued damage (respecting damage ordering) // Or, ideally, maintain damage between frames on node/layer so ordering is always correct RenderState& renderState = mLayer->renderState; if (properties().fitsOnLayer()) { mLayer = renderState.layerPool().resize(mLayer, getWidth(), getHeight()); } else { #else if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) { #endif destroyLayer(mLayer); mLayer = nullptr; } damageSelf(info); transformUpdateNeeded = true; } SkRect dirty; info.damageAccumulator->peekAtDirty(&dirty); if (!mLayer) { Caches::getInstance().dumpMemoryUsage(); if (info.errorHandler) { std::ostringstream err; err << "Unable to create layer for " << getName(); const int maxTextureSize = Caches::getInstance().maxTextureSize; if (getWidth() > maxTextureSize || getHeight() > maxTextureSize) { err << ", size " << getWidth() << "x" << getHeight() << " exceeds max size " << maxTextureSize; } else { err << ", see logcat for more info"; } info.errorHandler->onError(err.str()); } return; } if (transformUpdateNeeded && mLayer) { // update the transform in window of the layer to reset its origin wrt light source position Matrix4 windowTransform; info.damageAccumulator->computeCurrentTransform(&windowTransform); mLayer->setWindowTransform(windowTransform); } #if HWUI_NEW_OPS info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty); #else if (dirty.intersect(0, 0, getWidth(), getHeight())) { dirty.roundOut(&dirty); mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom); } // This is not inside the above if because we may have called // updateDeferred on a previous prepare pass that didn't have a renderer if (info.renderer && mLayer->deferredUpdateScheduled) { info.renderer->pushLayerUpdate(mLayer); } #endif // There might be prefetched layers that need to be accounted for. // That might be us, so tell CanvasContext that this layer is in the // tree and should not be destroyed. info.canvasContext.markLayerInUse(this); } /** * Traverse down the the draw tree to prepare for a frame. * * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven * * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer. */ void RenderNode::prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer) { info.damageAccumulator->pushTransform(this); if (info.mode == TreeInfo::MODE_FULL) { pushStagingPropertiesChanges(info); } uint32_t animatorDirtyMask = 0; if (CC_LIKELY(info.runAnimations)) { animatorDirtyMask = mAnimatorManager.animate(info); } bool willHaveFunctor = false; if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) { willHaveFunctor = !mStagingDisplayList->getFunctors().empty(); } else if (mDisplayList) { willHaveFunctor = !mDisplayList->getFunctors().empty(); } bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence( willHaveFunctor, functorsNeedLayer); if (CC_UNLIKELY(mPositionListener.get())) { mPositionListener->onPositionUpdated(*this, info); } prepareLayer(info, animatorDirtyMask); if (info.mode == TreeInfo::MODE_FULL) { pushStagingDisplayListChanges(info); } prepareSubTree(info, childFunctorsNeedLayer, mDisplayList); pushLayerUpdate(info); info.damageAccumulator->popTransform(); }
static void paintSkBitmap(PlatformContextSkia* platformContext, const NativeImageSkia& bitmap, const SkRect& srcRect, const SkRect& destRect, const SkXfermode::Mode& compOp) { TRACE_EVENT0("skia", "paintSkBitmap"); SkPaint paint; paint.setXfermodeMode(compOp); paint.setAlpha(platformContext->getNormalizedAlpha()); paint.setLooper(platformContext->getDrawLooper()); // only antialias if we're rotated or skewed paint.setAntiAlias(hasNon90rotation(platformContext)); ResamplingMode resampling; if (platformContext->isAccelerated()) resampling = RESAMPLE_LINEAR; else if (platformContext->printing()) resampling = RESAMPLE_NONE; else { // Take into account scale applied to the canvas when computing sampling mode (e.g. CSS scale or page scale). SkRect destRectTarget = destRect; if (!(platformContext->getTotalMatrix().getType() & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask))) platformContext->getTotalMatrix().mapRect(&destRectTarget, destRect); resampling = computeResamplingMode(platformContext->getTotalMatrix(), bitmap, SkScalarToFloat(srcRect.width()), SkScalarToFloat(srcRect.height()), SkScalarToFloat(destRectTarget.width()), SkScalarToFloat(destRectTarget.height())); } if (resampling == RESAMPLE_NONE) { // FIXME: This is to not break tests (it results in the filter bitmap flag // being set to true). We need to decide if we respect RESAMPLE_NONE // being returned from computeResamplingMode. resampling = RESAMPLE_LINEAR; } resampling = limitResamplingMode(platformContext, resampling); paint.setFilterBitmap(resampling == RESAMPLE_LINEAR); if (resampling == RESAMPLE_AWESOME) drawResampledBitmap(platformContext, paint, bitmap, srcRect, destRect); else { // No resampling necessary, we can just draw the bitmap. We want to // filter it if we decided to do linear interpolation above, or if there // is something interesting going on with the matrix (like a rotation). // Note: for serialization, we will want to subset the bitmap first so // we don't send extra pixels. SkIRect enclosingSrcRect; SkRect enclosingDestRect; SkISize bitmapSize = SkISize::Make(bitmap.bitmap().width(), bitmap.bitmap().height()); bool needsClipping = computeBitmapDrawRects(bitmapSize, srcRect, destRect, &enclosingSrcRect, &enclosingDestRect); if (enclosingSrcRect.isEmpty() || enclosingDestRect.isEmpty()) return; // If destination is enlarged because source rectangle didn't align to // integer boundaries then we draw a slightly larger rectangle and clip // to the original destination rectangle. // See http://crbug.com/145540. if (needsClipping) { platformContext->save(); platformContext->clipRect(destRect); } platformContext->drawBitmapRect(bitmap.bitmap(), &enclosingSrcRect, enclosingDestRect, &paint); if (needsClipping) platformContext->restore(); } platformContext->didDrawRect(destRect, paint, &bitmap.bitmap()); }
SkRect create_rect(const SkPoint& offset) { SkRect r = SkRect::MakeLTRB(kMin, kMin, kMax, kMax); r.offset(offset); return r; }
void Image::drawPattern(GraphicsContext* context, const FloatRect& floatSrcRect, const AffineTransform& patternTransform, const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator compositeOp, const FloatRect& destRect, BlendMode) { TRACE_EVENT0("skia", "Image::drawPattern"); RefPtr<NativeImageSkia> bitmap = nativeImageForCurrentFrame(); if (!bitmap) return; FloatRect normSrcRect = normalizeRect(floatSrcRect); normSrcRect.intersect(FloatRect(0, 0, bitmap->bitmap().width(), bitmap->bitmap().height())); if (destRect.isEmpty() || normSrcRect.isEmpty()) return; // nothing to draw SkMatrix ctm = context->platformContext()->getTotalMatrix(); SkMatrix totalMatrix; totalMatrix.setConcat(ctm, patternTransform); // Figure out what size the bitmap will be in the destination. The // destination rect is the bounds of the pattern, we need to use the // matrix to see how big it will be. SkRect destRectTarget; totalMatrix.mapRect(&destRectTarget, normSrcRect); float destBitmapWidth = SkScalarToFloat(destRectTarget.width()); float destBitmapHeight = SkScalarToFloat(destRectTarget.height()); // Compute the resampling mode. ResamplingMode resampling; if (context->platformContext()->isAccelerated() || context->platformContext()->printing()) resampling = RESAMPLE_LINEAR; else resampling = computeResamplingMode(totalMatrix, *bitmap, normSrcRect.width(), normSrcRect.height(), destBitmapWidth, destBitmapHeight); resampling = limitResamplingMode(context->platformContext(), resampling); // Load the transform WebKit requested. SkMatrix matrix(patternTransform); SkShader* shader; if (resampling == RESAMPLE_AWESOME) { // Do nice resampling. float scaleX = destBitmapWidth / normSrcRect.width(); float scaleY = destBitmapHeight / normSrcRect.height(); SkRect scaledSrcRect; SkIRect enclosingScaledSrcRect; // The image fragment generated here is not exactly what is // requested. The scale factor used is approximated and image // fragment is slightly larger to align to integer // boundaries. SkBitmap resampled = extractScaledImageFragment(*bitmap, normSrcRect, scaleX, scaleY, &scaledSrcRect, &enclosingScaledSrcRect); shader = SkShader::CreateBitmapShader(resampled, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); // Since we just resized the bitmap, we need to remove the scale // applied to the pixels in the bitmap shader. This means we need // CTM * patternTransform to have identity scale. Since we // can't modify CTM (or the rectangle will be drawn in the wrong // place), we must set patternTransform's scale to the inverse of // CTM scale. matrix.setScaleX(ctm.getScaleX() ? 1 / ctm.getScaleX() : 1); matrix.setScaleY(ctm.getScaleY() ? 1 / ctm.getScaleY() : 1); } else { // No need to do nice resampling. SkBitmap srcSubset; bitmap->bitmap().extractSubset(&srcSubset, enclosingIntRect(normSrcRect)); shader = SkShader::CreateBitmapShader(srcSubset, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); } // We also need to translate it such that the origin of the pattern is the // origin of the destination rect, which is what WebKit expects. Skia uses // the coordinate system origin as the base for the patter. If WebKit wants // a shifted image, it will shift it from there using the patternTransform. float adjustedX = phase.x() + normSrcRect.x() * narrowPrecisionToFloat(patternTransform.a()); float adjustedY = phase.y() + normSrcRect.y() * narrowPrecisionToFloat(patternTransform.d()); matrix.postTranslate(SkFloatToScalar(adjustedX), SkFloatToScalar(adjustedY)); shader->setLocalMatrix(matrix); SkPaint paint; paint.setShader(shader)->unref(); paint.setXfermodeMode(WebCoreCompositeToSkiaComposite(compositeOp)); paint.setFilterBitmap(resampling == RESAMPLE_LINEAR); context->platformContext()->drawRect(destRect, paint); }
bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder, const GrProcOptInfo& colorPOI, const GrProcOptInfo& coveragePOI, GrXferProcessor::DstTexture* dstTexture, const SkRect& batchBounds) { SkRect bounds = batchBounds; bounds.outset(0.5f, 0.5f); if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), colorPOI, coveragePOI)) { return true; } GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); if (this->caps()->textureBarrierSupport()) { if (GrTexture* rtTex = rt->asTexture()) { // The render target is a texture, so we can read from it directly in the shader. The XP // will be responsible to detect this situation and request a texture barrier. dstTexture->setTexture(rtTex); dstTexture->setOffset(0, 0); return true; } } SkIRect copyRect; pipelineBuilder.clip().getConservativeBounds(rt, ©Rect); SkIRect drawIBounds; bounds.roundOut(&drawIBounds); if (!copyRect.intersect(drawIBounds)) { #ifdef SK_DEBUG GrCapsDebugf(this->caps(), "Missed an early reject. " "Bailing on draw from setupDstReadIfNecessary.\n"); #endif return false; } // MSAA consideration: When there is support for reading MSAA samples in the shader we could // have per-sample dst values by making the copy multisampled. GrSurfaceDesc desc; if (!fGpu->initCopySurfaceDstDesc(rt, &desc)) { desc.fOrigin = kDefault_GrSurfaceOrigin; desc.fFlags = kRenderTarget_GrSurfaceFlag; desc.fConfig = rt->config(); } desc.fWidth = copyRect.width(); desc.fHeight = copyRect.height(); static const uint32_t kFlags = 0; SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags)); if (!copy) { SkDebugf("Failed to create temporary copy of destination texture.\n"); return false; } SkIPoint dstPoint = {0, 0}; this->copySurface(copy, rt, copyRect, dstPoint); dstTexture->setTexture(copy); dstTexture->setOffset(copyRect.fLeft, copyRect.fTop); return true; }
static void paintSkBitmap(PlatformContextSkia* platformContext, const NativeImageSkia& bitmap, const SkIRect& srcRect, const SkRect& destRect, const SkXfermode::Mode& compOp) { #if PLATFORM(CHROMIUM) TRACE_EVENT("paintSkBitmap", platformContext, 0); #endif SkPaint paint; paint.setXfermodeMode(compOp); paint.setFilterBitmap(true); paint.setAlpha(platformContext->getNormalizedAlpha()); paint.setLooper(platformContext->getDrawLooper()); // only antialias if we're rotated or skewed paint.setAntiAlias(hasNon90rotation(platformContext)); SkCanvas* canvas = platformContext->canvas(); ResamplingMode resampling; if (platformContext->isAccelerated()) resampling = RESAMPLE_LINEAR; else resampling = platformContext->printing() ? RESAMPLE_NONE : computeResamplingMode(platformContext, bitmap, srcRect.width(), srcRect.height(), SkScalarToFloat(destRect.width()), SkScalarToFloat(destRect.height())); if (resampling == RESAMPLE_AWESOME) { drawResampledBitmap(*canvas, paint, bitmap, srcRect, destRect); } else { // No resampling necessary, we can just draw the bitmap. We want to // filter it if we decided to do linear interpolation above, or if there // is something interesting going on with the matrix (like a rotation). // Note: for serialization, we will want to subset the bitmap first so // we don't send extra pixels. canvas->drawBitmapRect(bitmap.bitmap(), &srcRect, destRect, &paint); } platformContext->didDrawRect(destRect, paint, &bitmap.bitmap()); }
virtual void onDraw(SkCanvas* canvas) { static const int kBmpSize = 2048; if (fLargeBitmap.isNull()) { makebm(&fLargeBitmap, kBmpSize, kBmpSize); } SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; static const int kMaxSrcRectSize = 1 << (SkNextLog2(kBmpSize) + 2); static const int kPadX = 30; static const int kPadY = 40; SkPaint paint; paint.setAlpha(0x20); canvas->drawBitmapRect(fLargeBitmap, NULL, SkRect::MakeWH(gSize * SK_Scalar1, gSize * SK_Scalar1), &paint); canvas->translate(SK_Scalar1 * kPadX / 2, SK_Scalar1 * kPadY / 2); SkPaint blackPaint; SkScalar titleHeight = SK_Scalar1 * 24; blackPaint.setColor(SK_ColorBLACK); blackPaint.setTextSize(titleHeight); blackPaint.setAntiAlias(true); SkString title; title.printf("Bitmap size: %d x %d", kBmpSize, kBmpSize); canvas->drawText(title.c_str(), title.size(), 0, titleHeight, blackPaint); canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight); int rowCount = 0; canvas->save(); for (int w = 1; w <= kMaxSrcRectSize; w *= 4) { for (int h = 1; h <= kMaxSrcRectSize; h *= 4) { SkIRect srcRect = SkIRect::MakeXYWH((kBmpSize - w) / 2, (kBmpSize - h) / 2, w, h); canvas->drawBitmapRect(fLargeBitmap, &srcRect, dstRect); SkString label; label.appendf("%d x %d", w, h); blackPaint.setAntiAlias(true); blackPaint.setStyle(SkPaint::kFill_Style); blackPaint.setTextSize(SK_Scalar1 * 10); SkScalar baseline = dstRect.height() + blackPaint.getTextSize() + SK_Scalar1 * 3; canvas->drawText(label.c_str(), label.size(), 0, baseline, blackPaint); blackPaint.setStyle(SkPaint::kStroke_Style); blackPaint.setStrokeWidth(SK_Scalar1); blackPaint.setAntiAlias(false); canvas->drawRect(dstRect, blackPaint); canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0); ++rowCount; if ((dstRect.width() + kPadX) * rowCount > gSize) { canvas->restore(); canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY); canvas->save(); rowCount = 0; } } } { // test the following code path: // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter() SkIRect srcRect; SkPaint paint; SkBitmap bm; bm = make_chessbm(5, 5); paint.setFilterLevel(SkPaint::kLow_FilterLevel); srcRect.setXYWH(1, 1, 3, 3); SkMaskFilter* mf = SkBlurMaskFilter::Create( kNormal_SkBlurStyle, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), SkBlurMaskFilter::kHighQuality_BlurFlag | SkBlurMaskFilter::kIgnoreTransform_BlurFlag); paint.setMaskFilter(mf)->unref(); canvas->drawBitmapRect(bm, &srcRect, dstRect, &paint); } }
PathClipView() { fOval.set(0, 0, SkIntToScalar(200), SkIntToScalar(50)); fCenter.set(SkIntToScalar(250), SkIntToScalar(250)); // test_ats(); }
static void convolve_gaussian(GrDrawContext* drawContext, GrRenderTarget* rt, const GrClip& clip, const SkRect& srcRect, const SkRect& dstRect, GrTexture* texture, Gr1DKernelEffect::Direction direction, int radius, float sigma, bool cropToSrcRect) { float bounds[2] = { 0.0f, 1.0f }; if (!cropToSrcRect) { convolve_gaussian_1d(drawContext, rt, clip, srcRect, dstRect, texture, direction, radius, sigma, false, bounds); return; } SkRect lowerSrcRect = srcRect, lowerDstRect = dstRect; SkRect middleSrcRect = srcRect, middleDstRect = dstRect; SkRect upperSrcRect = srcRect, upperDstRect = dstRect; SkScalar size; SkScalar rad = SkIntToScalar(radius); if (direction == Gr1DKernelEffect::kX_Direction) { bounds[0] = SkScalarToFloat(srcRect.left()) / texture->width(); bounds[1] = SkScalarToFloat(srcRect.right()) / texture->width(); size = srcRect.width(); lowerSrcRect.fRight = srcRect.left() + rad; lowerDstRect.fRight = dstRect.left() + rad; upperSrcRect.fLeft = srcRect.right() - rad; upperDstRect.fLeft = dstRect.right() - rad; middleSrcRect.inset(rad, 0); middleDstRect.inset(rad, 0); } else { bounds[0] = SkScalarToFloat(srcRect.top()) / texture->height(); bounds[1] = SkScalarToFloat(srcRect.bottom()) / texture->height(); size = srcRect.height(); lowerSrcRect.fBottom = srcRect.top() + rad; lowerDstRect.fBottom = dstRect.top() + rad; upperSrcRect.fTop = srcRect.bottom() - rad; upperDstRect.fTop = dstRect.bottom() - rad; middleSrcRect.inset(0, rad); middleDstRect.inset(0, rad); } if (radius >= size * SK_ScalarHalf) { // Blur radius covers srcRect; use bounds over entire draw convolve_gaussian_1d(drawContext, rt, clip, srcRect, dstRect, texture, direction, radius, sigma, true, bounds); } else { // Draw upper and lower margins with bounds; middle without. convolve_gaussian_1d(drawContext, rt, clip, lowerSrcRect, lowerDstRect, texture, direction, radius, sigma, true, bounds); convolve_gaussian_1d(drawContext, rt, clip, upperSrcRect, upperDstRect, texture, direction, radius, sigma, true, bounds); convolve_gaussian_1d(drawContext, rt, clip, middleSrcRect, middleDstRect, texture, direction, radius, sigma, false, bounds); } }
static void make_path_line(SkPath* path, const SkRect& bounds) { path->moveTo(bounds.left(), bounds.top()); path->lineTo(bounds.right(), bounds.bottom()); }
static void TestDeferredCanvasFreshFrame(skiatest::Reporter* reporter) { SkBitmap store; SkRect fullRect; fullRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(gWidth), SkIntToScalar(gHeight)); SkRect partialRect; partialRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(1), SkIntToScalar(1)); create(&store, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); SkDevice device(store); SkDeferredCanvas canvas(&device); // verify that frame is intially fresh REPORTER_ASSERT(reporter, canvas.getDeferredDevice()->isFreshFrame()); // no clearing op since last call to isFreshFrame -> not fresh REPORTER_ASSERT(reporter, !canvas.getDeferredDevice()->isFreshFrame()); // Verify that clear triggers a fresh frame canvas.clear(0x00000000); REPORTER_ASSERT(reporter, canvas.getDeferredDevice()->isFreshFrame()); // Verify that clear with saved state triggers a fresh frame canvas.save(SkCanvas::kMatrixClip_SaveFlag); canvas.clear(0x00000000); canvas.restore(); REPORTER_ASSERT(reporter, canvas.getDeferredDevice()->isFreshFrame()); // Verify that clear within a layer does NOT trigger a fresh frame canvas.saveLayer(NULL, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag); canvas.clear(0x00000000); canvas.restore(); REPORTER_ASSERT(reporter, !canvas.getDeferredDevice()->isFreshFrame()); // Verify that a clear with clipping triggers a fresh frame // (clear is not affected by clipping) canvas.save(SkCanvas::kMatrixClip_SaveFlag); canvas.clipRect(partialRect, SkRegion::kIntersect_Op, false); canvas.clear(0x00000000); canvas.restore(); REPORTER_ASSERT(reporter, canvas.getDeferredDevice()->isFreshFrame()); // Verify that full frame rects with different forms of opaque paint // trigger frames to be marked as fresh { SkPaint paint; paint.setStyle( SkPaint::kFill_Style ); paint.setAlpha( 255 ); canvas.drawRect(fullRect, paint); REPORTER_ASSERT(reporter, canvas.getDeferredDevice()->isFreshFrame()); } { SkPaint paint; paint.setStyle( SkPaint::kFill_Style ); SkBitmap bmp; create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); bmp.setIsOpaque(true); SkShader* shader = SkShader::CreateBitmapShader(bmp, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); paint.setShader(shader)->unref(); canvas.drawRect(fullRect, paint); REPORTER_ASSERT(reporter, canvas.getDeferredDevice()->isFreshFrame()); } // Verify that full frame rects with different forms of non-opaque paint // do not trigger frames to be marked as fresh { SkPaint paint; paint.setStyle( SkPaint::kFill_Style ); paint.setAlpha( 254 ); canvas.drawRect(fullRect, paint); REPORTER_ASSERT(reporter, !canvas.getDeferredDevice()->isFreshFrame()); } { SkPaint paint; paint.setStyle( SkPaint::kFill_Style ); SkBitmap bmp; create(&bmp, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF); bmp.setIsOpaque(false); SkShader* shader = SkShader::CreateBitmapShader(bmp, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); paint.setShader(shader)->unref(); canvas.drawRect(fullRect, paint); REPORTER_ASSERT(reporter, !canvas.getDeferredDevice()->isFreshFrame()); } // Verify that incomplete coverage does not trigger a fresh frame { SkPaint paint; paint.setStyle(SkPaint::kFill_Style); paint.setAlpha(255); canvas.drawRect(partialRect, paint); REPORTER_ASSERT(reporter, !canvas.getDeferredDevice()->isFreshFrame()); } // Verify that incomplete coverage due to clipping does not trigger a fresh // frame { canvas.save(SkCanvas::kMatrixClip_SaveFlag); canvas.clipRect(partialRect, SkRegion::kIntersect_Op, false); SkPaint paint; paint.setStyle(SkPaint::kFill_Style); paint.setAlpha(255); canvas.drawRect(fullRect, paint); REPORTER_ASSERT(reporter, !canvas.getDeferredDevice()->isFreshFrame()); } // Verify that stroked rect does not trigger a fresh frame { SkPaint paint; paint.setStyle( SkPaint::kStroke_Style ); paint.setAlpha( 255 ); canvas.drawRect(fullRect, paint); REPORTER_ASSERT(reporter, !canvas.getDeferredDevice()->isFreshFrame()); } // Verify kSrcMode triggers a fresh frame even with transparent color { SkPaint paint; paint.setStyle( SkPaint::kFill_Style ); paint.setAlpha( 100 ); paint.setXfermodeMode(SkXfermode::kSrc_Mode); canvas.drawRect(fullRect, paint); REPORTER_ASSERT(reporter, !canvas.getDeferredDevice()->isFreshFrame()); } }
GrDrawAtlasBatch::GrDrawAtlasBatch(const Geometry& geometry, const SkMatrix& viewMatrix, int spriteCount, const SkRSXform* xforms, const SkRect* rects, const SkColor* colors) : INHERITED(ClassID()) { SkASSERT(xforms); SkASSERT(rects); fViewMatrix = viewMatrix; Geometry& installedGeo = fGeoData.push_back(geometry); // Figure out stride and offsets // Order within the vertex is: position [color] texCoord size_t texOffset = sizeof(SkPoint); size_t vertexStride = 2*sizeof(SkPoint); fHasColors = SkToBool(colors); if (colors) { texOffset += sizeof(GrColor); vertexStride += sizeof(GrColor); } // Compute buffer size and alloc buffer fQuadCount = spriteCount; int allocSize = static_cast<int>(4*vertexStride*spriteCount); installedGeo.fVerts.reset(allocSize); uint8_t* currVertex = installedGeo.fVerts.begin(); SkRect bounds; bounds.setLargestInverted(); int paintAlpha = GrColorUnpackA(installedGeo.fColor); for (int spriteIndex = 0; spriteIndex < spriteCount; ++spriteIndex) { // Transform rect SkPoint quad[4]; const SkRect& currRect = rects[spriteIndex]; xforms[spriteIndex].toQuad(currRect.width(), currRect.height(), quad); // Copy colors if necessary if (colors) { // convert to GrColor SkColor color = colors[spriteIndex]; if (paintAlpha != 255) { color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paintAlpha)); } GrColor grColor = SkColorToPremulGrColor(color); *(reinterpret_cast<GrColor*>(currVertex+sizeof(SkPoint))) = grColor; *(reinterpret_cast<GrColor*>(currVertex+vertexStride+sizeof(SkPoint))) = grColor; *(reinterpret_cast<GrColor*>(currVertex+2*vertexStride+sizeof(SkPoint))) = grColor; *(reinterpret_cast<GrColor*>(currVertex+3*vertexStride+sizeof(SkPoint))) = grColor; } // Copy position and uv to verts *(reinterpret_cast<SkPoint*>(currVertex)) = quad[0]; *(reinterpret_cast<SkPoint*>(currVertex+texOffset)) = SkPoint::Make(currRect.fLeft, currRect.fTop); bounds.growToInclude(quad[0].fX, quad[0].fY); currVertex += vertexStride; *(reinterpret_cast<SkPoint*>(currVertex)) = quad[1]; *(reinterpret_cast<SkPoint*>(currVertex+texOffset)) = SkPoint::Make(currRect.fRight, currRect.fTop); bounds.growToInclude(quad[1].fX, quad[1].fY); currVertex += vertexStride; *(reinterpret_cast<SkPoint*>(currVertex)) = quad[2]; *(reinterpret_cast<SkPoint*>(currVertex+texOffset)) = SkPoint::Make(currRect.fRight, currRect.fBottom); bounds.growToInclude(quad[2].fX, quad[2].fY); currVertex += vertexStride; *(reinterpret_cast<SkPoint*>(currVertex)) = quad[3]; *(reinterpret_cast<SkPoint*>(currVertex+texOffset)) = SkPoint::Make(currRect.fLeft, currRect.fBottom); bounds.growToInclude(quad[3].fX, quad[3].fY); currVertex += vertexStride; } viewMatrix.mapRect(&bounds); // Outset for a half pixel in each direction to account for snapping in non-AA case bounds.outset(0.5f, 0.5f); this->setBounds(bounds); }