2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkGpuDevice.h"
10 #include "effects/GrBicubicEffect.h"
11 #include "effects/GrDashingEffect.h"
12 #include "effects/GrTextureDomain.h"
13 #include "effects/GrSimpleTextureEffect.h"
15 #include "GrContext.h"
16 #include "GrBitmapTextContext.h"
17 #include "GrDistanceFieldTextContext.h"
18 #include "GrLayerCache.h"
19 #include "GrLayerHoister.h"
20 #include "GrPictureUtils.h"
21 #include "GrRecordReplaceDraw.h"
22 #include "GrStrokeInfo.h"
23 #include "GrTracing.h"
25 #include "SkGrTexturePixelRef.h"
27 #include "SkDeviceImageFilterProxy.h"
28 #include "SkDrawProcs.h"
29 #include "SkGlyphCache.h"
30 #include "SkImageFilter.h"
31 #include "SkMaskFilter.h"
32 #include "SkPathEffect.h"
33 #include "SkPicture.h"
34 #include "SkPictureData.h"
38 #include "SkSurface.h"
41 #include "SkVertState.h"
42 #include "SkXfermode.h"
43 #include "SkErrorInternals.h"
45 enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
47 #define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
50 extern bool (*gShouldDrawProc)();
51 #define CHECK_SHOULD_DRAW(draw, forceI) \
53 if (gShouldDrawProc && !gShouldDrawProc()) return; \
54 this->prepareDraw(draw, forceI); \
57 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
60 // This constant represents the screen alignment criterion in texels for
61 // requiring texture domain clamping to prevent color bleeding when drawing
62 // a sub region of a larger source image.
63 #define COLOR_BLEED_TOLERANCE 0.001f
65 #define DO_DEFERRED_CLEAR() \
68 this->clear(SK_ColorTRANSPARENT); \
72 ///////////////////////////////////////////////////////////////////////////////
74 #define CHECK_FOR_ANNOTATION(paint) \
75 do { if (paint.getAnnotation()) { return; } } while (0)
77 ///////////////////////////////////////////////////////////////////////////////
80 class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
87 SkAutoCachedTexture(SkGpuDevice* device,
88 const SkBitmap& bitmap,
89 const GrTextureParams* params,
94 *texture = this->set(device, bitmap, params);
97 ~SkAutoCachedTexture() {
99 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
103 GrTexture* set(SkGpuDevice* device,
104 const SkBitmap& bitmap,
105 const GrTextureParams* params) {
107 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
111 GrTexture* result = (GrTexture*)bitmap.getTexture();
112 if (NULL == result) {
113 // Cannot return the native texture so look it up in our cache
114 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
121 SkGpuDevice* fDevice;
125 ///////////////////////////////////////////////////////////////////////////////
127 struct GrSkDrawProcs : public SkDrawProcs {
130 GrTextContext* fTextContext;
131 GrFontScaler* fFontScaler; // cached in the skia glyphcache
134 ///////////////////////////////////////////////////////////////////////////////
136 SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, const SkSurfaceProps& props, unsigned flags) {
138 if (NULL == surface->asRenderTarget() || surface->wasDestroyed()) {
141 return SkNEW_ARGS(SkGpuDevice, (surface, props, flags));
144 SkGpuDevice::SkGpuDevice(GrSurface* surface, const SkSurfaceProps& props, unsigned flags) {
148 fContext = SkRef(surface->getContext());
150 fNeedClear = flags & kNeedClear_Flag;
152 fRenderTarget = SkRef(surface->asRenderTarget());
154 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef,
155 (surface->info(), surface, SkToBool(flags & kCached_Flag)));
156 fLegacyBitmap.setInfo(surface->info());
157 fLegacyBitmap.setPixelRef(pr)->unref();
159 this->setPixelGeometry(props.pixelGeometry());
161 bool useDFFonts = !!(flags & kDFFonts_Flag);
162 fMainTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFFonts);
163 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, this->getLeakyProperties()));
166 SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
167 const SkSurfaceProps& props, int sampleCount) {
168 if (kUnknown_SkColorType == origInfo.colorType() ||
169 origInfo.width() < 0 || origInfo.height() < 0) {
173 SkColorType ct = origInfo.colorType();
174 SkAlphaType at = origInfo.alphaType();
175 // TODO: perhaps we can loosen this check now that colortype is more detailed
176 // e.g. can we support both RGBA and BGRA here?
177 if (kRGB_565_SkColorType == ct) {
178 at = kOpaque_SkAlphaType; // force this setting
180 ct = kN32_SkColorType;
181 if (kOpaque_SkAlphaType != at) {
182 at = kPremul_SkAlphaType; // force this setting
185 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
188 desc.fFlags = kRenderTarget_GrTextureFlagBit;
189 desc.fWidth = info.width();
190 desc.fHeight = info.height();
191 desc.fConfig = SkImageInfo2GrPixelConfig(info);
192 desc.fSampleCnt = sampleCount;
194 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
195 if (!texture.get()) {
199 return SkNEW_ARGS(SkGpuDevice, (texture.get(), props));
202 SkGpuDevice::~SkGpuDevice() {
207 delete fMainTextContext;
208 delete fFallbackTextContext;
210 // The GrContext takes a ref on the target. We don't want to cause the render
211 // target to be unnecessarily kept alive.
212 if (fContext->getRenderTarget() == fRenderTarget) {
213 fContext->setRenderTarget(NULL);
216 if (fContext->getClip() == &fClipData) {
217 fContext->setClip(NULL);
220 fRenderTarget->unref();
224 ///////////////////////////////////////////////////////////////////////////////
226 bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
230 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
231 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
232 if (kUnknown_GrPixelConfig == config) {
237 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
238 flags = GrContext::kUnpremul_PixelOpsFlag;
240 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
241 config, dstPixels, dstRowBytes, flags);
244 bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
246 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
247 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
248 if (kUnknown_GrPixelConfig == config) {
252 if (kUnpremul_SkAlphaType == info.alphaType()) {
253 flags = GrContext::kUnpremul_PixelOpsFlag;
255 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
257 // need to bump our genID for compatibility with clients that "know" we have a bitmap
258 fLegacyBitmap.notifyPixelsChanged();
263 const SkBitmap& SkGpuDevice::onAccessBitmap() {
265 return fLegacyBitmap;
268 void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
269 INHERITED::onAttachToCanvas(canvas);
271 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
272 fClipData.fClipStack = canvas->getClipStack();
275 void SkGpuDevice::onDetachFromCanvas() {
276 INHERITED::onDetachFromCanvas();
277 fClipData.fClipStack = NULL;
280 // call this every draw call, to ensure that the context reflects our state,
281 // and not the state from some other canvas/device
282 void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
283 SkASSERT(fClipData.fClipStack);
285 fContext->setRenderTarget(fRenderTarget);
287 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
290 fContext->setIdentityMatrix();
292 fContext->setMatrix(*draw.fMatrix);
294 fClipData.fOrigin = this->getOrigin();
296 fContext->setClip(&fClipData);
301 GrRenderTarget* SkGpuDevice::accessRenderTarget() {
303 return fRenderTarget;
306 ///////////////////////////////////////////////////////////////////////////////
308 SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
309 SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
310 SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
311 SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
312 SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
313 shader_type_mismatch);
314 SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
315 shader_type_mismatch);
316 SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
317 SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
319 ///////////////////////////////////////////////////////////////////////////////
321 void SkGpuDevice::clear(SkColor color) {
322 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clear", fContext);
323 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
324 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
328 void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
329 CHECK_SHOULD_DRAW(draw, false);
330 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
333 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
335 fContext->drawPaint(grPaint);
338 // must be in SkCanvas::PointMode order
339 static const GrPrimitiveType gPointMode2PrimtiveType[] = {
340 kPoints_GrPrimitiveType,
341 kLines_GrPrimitiveType,
342 kLineStrip_GrPrimitiveType
345 void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
346 size_t count, const SkPoint pts[], const SkPaint& paint) {
347 CHECK_FOR_ANNOTATION(paint);
348 CHECK_SHOULD_DRAW(draw, false);
350 SkScalar width = paint.getStrokeWidth();
355 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
356 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
358 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
362 fContext->drawPath(grPaint, path, strokeInfo);
366 // we only handle hairlines and paints without path effects or mask filters,
367 // else we let the SkDraw call our drawPath()
368 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
369 draw.drawPoints(mode, count, pts, paint, true);
374 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
376 fContext->drawVertices(grPaint,
377 gPointMode2PrimtiveType[mode],
386 ///////////////////////////////////////////////////////////////////////////////
388 void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
389 const SkPaint& paint) {
390 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
392 CHECK_FOR_ANNOTATION(paint);
393 CHECK_SHOULD_DRAW(draw, false);
395 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
396 SkScalar width = paint.getStrokeWidth();
399 We have special code for hairline strokes, miter-strokes, bevel-stroke
400 and fills. Anything else we just call our path code.
402 bool usePath = doStroke && width > 0 &&
403 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
404 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
405 // another two reasons we might need to call drawPath...
407 if (paint.getMaskFilter()) {
411 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
412 #if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
416 #if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
418 usePath = !fContext->getMatrix().preservesRightAngles();
422 // until we can both stroke and fill rectangles
423 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
427 GrStrokeInfo strokeInfo(paint);
429 const SkPathEffect* pe = paint.getPathEffect();
430 if (!usePath && pe && !strokeInfo.isDashed()) {
437 this->drawPath(draw, path, paint, NULL, true);
442 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
444 fContext->drawRect(grPaint, rect, &strokeInfo);
447 ///////////////////////////////////////////////////////////////////////////////
449 void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
450 const SkPaint& paint) {
451 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
452 CHECK_FOR_ANNOTATION(paint);
453 CHECK_SHOULD_DRAW(draw, false);
456 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
458 GrStrokeInfo strokeInfo(paint);
459 if (paint.getMaskFilter()) {
460 // try to hit the fast path for drawing filtered round rects
463 if (rect.transform(fContext->getMatrix(), &devRRect)) {
464 if (devRRect.allCornersCircular()) {
466 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
467 draw.fClip->getBounds(),
468 fContext->getMatrix(),
471 maskRect.roundOut(&finalIRect);
472 if (draw.fClip->quickReject(finalIRect)) {
476 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
477 strokeInfo.getStrokeRec(),
488 bool usePath = false;
490 if (paint.getMaskFilter()) {
493 const SkPathEffect* pe = paint.getPathEffect();
494 if (pe && !strokeInfo.isDashed()) {
503 this->drawPath(draw, path, paint, NULL, true);
507 fContext->drawRRect(grPaint, rect, strokeInfo);
510 void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
511 const SkRRect& inner, const SkPaint& paint) {
512 SkStrokeRec stroke(paint);
513 if (stroke.isFillStyle()) {
515 CHECK_FOR_ANNOTATION(paint);
516 CHECK_SHOULD_DRAW(draw, false);
519 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
521 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
522 fContext->drawDRRect(grPaint, outer, inner);
528 path.addRRect(outer);
529 path.addRRect(inner);
530 path.setFillType(SkPath::kEvenOdd_FillType);
532 this->drawPath(draw, path, paint, NULL, true);
536 /////////////////////////////////////////////////////////////////////////////
538 void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
539 const SkPaint& paint) {
540 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
541 CHECK_FOR_ANNOTATION(paint);
542 CHECK_SHOULD_DRAW(draw, false);
544 GrStrokeInfo strokeInfo(paint);
546 bool usePath = false;
547 // some basic reasons we might need to call drawPath...
548 if (paint.getMaskFilter()) {
551 const SkPathEffect* pe = paint.getPathEffect();
552 if (pe && !strokeInfo.isDashed()) {
560 this->drawPath(draw, path, paint, NULL, true);
565 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
567 fContext->drawOval(grPaint, oval, strokeInfo);
570 #include "SkMaskFilter.h"
572 ///////////////////////////////////////////////////////////////////////////////
574 // helpers for applying mask filters
577 // Draw a mask using the supplied paint. Since the coverage/geometry
578 // is already burnt into the mask this boils down to a rect draw.
579 // Return true if the mask was successfully drawn.
580 bool draw_mask(GrContext* context, const SkRect& maskRect,
581 GrPaint* grp, GrTexture* mask) {
582 GrContext::AutoMatrix am;
583 if (!am.setIdentity(context, grp)) {
588 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
589 matrix.postIDiv(mask->width(), mask->height());
591 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix))->unref();
592 context->drawRect(*grp, maskRect);
596 bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
597 SkMaskFilter* filter, const SkRegion& clip,
598 GrPaint* grp, SkPaint::Style style) {
601 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
602 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
605 SkAutoMaskFreeImage autoSrc(srcM.fImage);
607 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
610 // this will free-up dstM when we're done (allocated in filterMask())
611 SkAutoMaskFreeImage autoDst(dstM.fImage);
613 if (clip.quickReject(dstM.fBounds)) {
617 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
618 // the current clip (and identity matrix) and GrPaint settings
620 desc.fWidth = dstM.fBounds.width();
621 desc.fHeight = dstM.fBounds.height();
622 desc.fConfig = kAlpha_8_GrPixelConfig;
624 GrAutoScratchTexture ast(context, desc);
625 GrTexture* texture = ast.texture();
627 if (NULL == texture) {
630 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
631 dstM.fImage, dstM.fRowBytes);
633 SkRect maskRect = SkRect::Make(dstM.fBounds);
635 return draw_mask(context, maskRect, grp, texture);
638 // Create a mask of 'devPath' and place the result in 'mask'. Return true on
639 // success; false otherwise.
640 bool create_mask_GPU(GrContext* context,
641 const SkRect& maskRect,
642 const SkPath& devPath,
643 const GrStrokeInfo& strokeInfo,
645 GrAutoScratchTexture* mask) {
647 desc.fFlags = kRenderTarget_GrTextureFlagBit;
648 desc.fWidth = SkScalarCeilToInt(maskRect.width());
649 desc.fHeight = SkScalarCeilToInt(maskRect.height());
650 // We actually only need A8, but it often isn't supported as a
651 // render target so default to RGBA_8888
652 desc.fConfig = kRGBA_8888_GrPixelConfig;
653 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
654 desc.fConfig = kAlpha_8_GrPixelConfig;
657 mask->set(context, desc);
658 if (NULL == mask->texture()) {
662 GrTexture* maskTexture = mask->texture();
663 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
665 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
666 GrContext::AutoClip ac(context, clipRect);
668 context->clear(NULL, 0x0, true);
672 tempPaint.setAntiAlias(true);
673 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
674 // blend coeff of zero requires dual source blending support in order
675 // to properly blend partially covered pixels. This means the AA
676 // code path may not be taken. So we use a dst blend coeff of ISA. We
677 // could special case AA draws to a dst surface with known alpha=0 to
678 // use a zero dst coeff when dual source blending isn't available.
679 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
682 GrContext::AutoMatrix am;
684 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
686 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
687 am.set(context, translate);
688 context->drawPath(tempPaint, devPath, strokeInfo);
692 SkBitmap wrap_texture(GrTexture* texture) {
694 result.setInfo(texture->info());
695 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
701 void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
702 const SkPaint& paint, const SkMatrix* prePathMatrix,
703 bool pathIsMutable) {
704 CHECK_FOR_ANNOTATION(paint);
705 CHECK_SHOULD_DRAW(draw, false);
706 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
709 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
711 // If we have a prematrix, apply it to the path, optimizing for the case
712 // where the original path can in fact be modified in place (even though
713 // its parameter type is const).
714 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
715 SkTLazy<SkPath> tmpPath;
716 SkTLazy<SkPath> effectPath;
719 SkPath* result = pathPtr;
721 if (!pathIsMutable) {
722 result = tmpPath.init();
723 pathIsMutable = true;
725 // should I push prePathMatrix on our MV stack temporarily, instead
726 // of applying it here? See SkDraw.cpp
727 pathPtr->transform(*prePathMatrix, result);
730 // at this point we're done with prePathMatrix
731 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
733 GrStrokeInfo strokeInfo(paint);
734 SkPathEffect* pathEffect = paint.getPathEffect();
735 const SkRect* cullRect = NULL; // TODO: what is our bounds?
736 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
737 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
739 pathPtr = effectPath.get();
740 pathIsMutable = true;
741 strokeInfo.removeDash();
744 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
745 if (paint.getMaskFilter()) {
746 if (!stroke.isHairlineStyle()) {
747 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
748 if (stroke.applyToPath(strokedPath, *pathPtr)) {
749 pathPtr = strokedPath;
750 pathIsMutable = true;
751 strokeInfo.setFillStyle();
755 // avoid possibly allocating a new path in transform if we can
756 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
758 // transform the path into device space
759 pathPtr->transform(fContext->getMatrix(), devPathPtr);
762 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
763 draw.fClip->getBounds(),
764 fContext->getMatrix(),
766 // The context's matrix may change while creating the mask, so save the CTM here to
767 // pass to filterMaskGPU.
768 const SkMatrix ctm = fContext->getMatrix();
771 maskRect.roundOut(&finalIRect);
772 if (draw.fClip->quickReject(finalIRect)) {
777 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
778 stroke, *devPathPtr)) {
779 // the mask filter was able to draw itself directly, so there's nothing
784 GrAutoScratchTexture mask;
786 if (create_mask_GPU(fContext, maskRect, *devPathPtr, strokeInfo,
787 grPaint.isAntiAlias(), &mask)) {
790 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
791 ctm, maskRect, &filtered, true)) {
792 // filterMaskGPU gives us ownership of a ref to the result
793 SkAutoTUnref<GrTexture> atu(filtered);
795 // If the scratch texture that we used as the filter src also holds the filter
796 // result then we must detach so that this texture isn't recycled for a later
798 if (filtered == mask.texture()) {
800 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
803 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
804 // This path is completely drawn
811 // draw the mask on the CPU - this is a fallthrough path in case the
813 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
814 SkPaint::kFill_Style;
815 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
816 *draw.fClip, &grPaint, style);
820 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
823 static const int kBmpSmallTileSize = 1 << 10;
825 static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
826 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
827 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
828 return tilesX * tilesY;
831 static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
832 if (maxTileSize <= kBmpSmallTileSize) {
836 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
837 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
839 maxTileTotalTileSize *= maxTileSize * maxTileSize;
840 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
842 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
843 return kBmpSmallTileSize;
849 // Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
850 // pixels from the bitmap are necessary.
851 static void determine_clipped_src_rect(const GrContext* context,
852 const SkBitmap& bitmap,
853 const SkRect* srcRectPtr,
854 SkIRect* clippedSrcIRect) {
855 const GrClipData* clip = context->getClip();
856 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
858 if (!context->getMatrix().invert(&inv)) {
859 clippedSrcIRect->setEmpty();
862 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
863 inv.mapRect(&clippedSrcRect);
865 // we've setup src space 0,0 to map to the top left of the src rect.
866 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
867 if (!clippedSrcRect.intersect(*srcRectPtr)) {
868 clippedSrcIRect->setEmpty();
872 clippedSrcRect.roundOut(clippedSrcIRect);
873 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
874 if (!clippedSrcIRect->intersect(bmpBounds)) {
875 clippedSrcIRect->setEmpty();
879 bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
880 const GrTextureParams& params,
881 const SkRect* srcRectPtr,
884 SkIRect* clippedSrcRect) const {
885 // if bitmap is explictly texture backed then just use the texture
886 if (bitmap.getTexture()) {
890 // if it's larger than the max tile size, then we have no choice but tiling.
891 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
892 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
893 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
897 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
901 // if the entire texture is already in our cache then no reason to tile it
902 if (GrIsBitmapInCache(fContext, bitmap, ¶ms)) {
906 // At this point we know we could do the draw by uploading the entire bitmap
907 // as a texture. However, if the texture would be large compared to the
908 // cache size and we don't require most of it for this draw then tile to
909 // reduce the amount of upload and cache spill.
911 // assumption here is that sw bitmap size is a good proxy for its size as
913 size_t bmpSize = bitmap.getSize();
915 fContext->getResourceCacheLimits(NULL, &cacheSize);
916 if (bmpSize < cacheSize / 2) {
920 // Figure out how much of the src we will need based on the src rect and clipping.
921 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
922 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
923 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
924 kBmpSmallTileSize * kBmpSmallTileSize;
926 return usedTileBytes < 2 * bmpSize;
929 void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
930 const SkBitmap& bitmap,
932 const SkPaint& paint) {
934 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
935 if (!m.isIdentity()) {
936 concat.setConcat(*draw->fMatrix, m);
937 draw.writable()->fMatrix = &concat;
939 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
942 // This method outsets 'iRect' by 'outset' all around and then clamps its extents to
943 // 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
944 // of 'iRect' for all possible outsets/clamps.
945 static inline void clamped_outset_with_offset(SkIRect* iRect,
948 const SkIRect& clamp) {
949 iRect->outset(outset, outset);
951 int leftClampDelta = clamp.fLeft - iRect->fLeft;
952 if (leftClampDelta > 0) {
953 offset->fX -= outset - leftClampDelta;
954 iRect->fLeft = clamp.fLeft;
956 offset->fX -= outset;
959 int topClampDelta = clamp.fTop - iRect->fTop;
960 if (topClampDelta > 0) {
961 offset->fY -= outset - topClampDelta;
962 iRect->fTop = clamp.fTop;
964 offset->fY -= outset;
967 if (iRect->fRight > clamp.fRight) {
968 iRect->fRight = clamp.fRight;
970 if (iRect->fBottom > clamp.fBottom) {
971 iRect->fBottom = clamp.fBottom;
975 static bool has_aligned_samples(const SkRect& srcRect,
976 const SkRect& transformedRect) {
977 // detect pixel disalignment
978 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
979 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
980 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
981 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
982 SkScalarAbs(transformedRect.width() - srcRect.width()) <
983 COLOR_BLEED_TOLERANCE &&
984 SkScalarAbs(transformedRect.height() - srcRect.height()) <
985 COLOR_BLEED_TOLERANCE) {
991 static bool may_color_bleed(const SkRect& srcRect,
992 const SkRect& transformedRect,
994 // Only gets called if has_aligned_samples returned false.
995 // So we can assume that sampling is axis aligned but not texel aligned.
996 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
997 SkRect innerSrcRect(srcRect), innerTransformedRect,
998 outerTransformedRect(transformedRect);
999 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1000 m.mapRect(&innerTransformedRect, innerSrcRect);
1002 // The gap between outerTransformedRect and innerTransformedRect
1003 // represents the projection of the source border area, which is
1004 // problematic for color bleeding. We must check whether any
1005 // destination pixels sample the border area.
1006 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1007 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1008 SkIRect outer, inner;
1009 outerTransformedRect.round(&outer);
1010 innerTransformedRect.round(&inner);
1011 // If the inner and outer rects round to the same result, it means the
1012 // border does not overlap any pixel centers. Yay!
1013 return inner != outer;
1016 static bool needs_texture_domain(const SkBitmap& bitmap,
1017 const SkRect& srcRect,
1018 GrTextureParams ¶ms,
1019 const SkMatrix& contextMatrix,
1021 bool needsTextureDomain = false;
1023 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1024 // Need texture domain if drawing a sub rect
1025 needsTextureDomain = srcRect.width() < bitmap.width() ||
1026 srcRect.height() < bitmap.height();
1027 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1028 // sampling is axis-aligned
1029 SkRect transformedRect;
1030 contextMatrix.mapRect(&transformedRect, srcRect);
1032 if (has_aligned_samples(srcRect, transformedRect)) {
1033 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1034 needsTextureDomain = false;
1036 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1040 return needsTextureDomain;
1043 void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1044 const SkBitmap& bitmap,
1045 const SkRect* srcRectPtr,
1046 const SkSize* dstSizePtr,
1047 const SkPaint& paint,
1048 SkCanvas::DrawBitmapRectFlags flags) {
1049 CHECK_SHOULD_DRAW(draw, false);
1053 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1054 // in the (easier) bleed case, so update flags.
1055 if (NULL == srcRectPtr) {
1056 SkScalar w = SkIntToScalar(bitmap.width());
1057 SkScalar h = SkIntToScalar(bitmap.height());
1059 dstSize.fHeight = h;
1060 srcRect.set(0, 0, w, h);
1061 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1063 SkASSERT(dstSizePtr);
1064 srcRect = *srcRectPtr;
1065 dstSize = *dstSizePtr;
1066 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1067 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1068 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1072 if (paint.getMaskFilter()){
1073 // Convert the bitmap to a shader so that the rect can be drawn
1074 // through drawRect, which supports mask filters.
1075 SkBitmap tmp; // subset of bitmap, if necessary
1076 const SkBitmap* bitmapPtr = &bitmap;
1079 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1080 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1081 dstSize.fHeight / srcRectPtr->height());
1082 // In bleed mode we position and trim the bitmap based on the src rect which is
1083 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1084 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1086 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1088 srcRect.roundOut(&iSrc);
1090 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1091 SkIntToScalar(iSrc.fTop));
1093 if (!bitmap.extractSubset(&tmp, iSrc)) {
1094 return; // extraction failed
1097 srcRect.offset(-offset.fX, -offset.fY);
1099 // The source rect has changed so update the matrix
1100 localM.preTranslate(offset.fX, offset.fY);
1106 SkPaint paintWithShader(paint);
1107 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
1108 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
1109 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1110 this->drawRect(draw, dstRect, paintWithShader);
1115 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1116 // the view matrix rather than a local matrix.
1118 m.setScale(dstSize.fWidth / srcRect.width(),
1119 dstSize.fHeight / srcRect.height());
1120 fContext->concatMatrix(m);
1122 GrTextureParams params;
1123 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1124 GrTextureParams::FilterMode textureFilterMode;
1126 bool doBicubic = false;
1128 switch(paintFilterLevel) {
1129 case SkPaint::kNone_FilterLevel:
1130 textureFilterMode = GrTextureParams::kNone_FilterMode;
1132 case SkPaint::kLow_FilterLevel:
1133 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1135 case SkPaint::kMedium_FilterLevel:
1136 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
1137 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1139 // Don't trigger MIP level generation unnecessarily.
1140 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1143 case SkPaint::kHigh_FilterLevel:
1144 // Minification can look bad with the bicubic effect.
1146 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
1149 SkErrorInternals::SetError( kInvalidPaint_SkError,
1150 "Sorry, I don't understand the filtering "
1151 "mode you asked for. Falling back to "
1153 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1159 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1160 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1165 params.setFilterMode(textureFilterMode);
1167 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
1170 SkIRect clippedSrcRect;
1171 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1173 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1176 // take the simple case
1177 bool needsTextureDomain = needs_texture_domain(bitmap,
1180 fContext->getMatrix(),
1182 this->internalDrawBitmap(bitmap,
1188 needsTextureDomain);
1192 // Break 'bitmap' into several tiles to draw it since it has already
1193 // been determined to be too large to fit in VRAM
1194 void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1195 const SkRect& srcRect,
1196 const SkIRect& clippedSrcIRect,
1197 const GrTextureParams& params,
1198 const SkPaint& paint,
1199 SkCanvas::DrawBitmapRectFlags flags,
1202 // The following pixel lock is technically redundant, but it is desirable
1203 // to lock outside of the tile loop to prevent redecoding the whole image
1204 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1205 // is larger than the limit of the discardable memory pool.
1206 SkAutoLockPixels alp(bitmap);
1207 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1209 int nx = bitmap.width() / tileSize;
1210 int ny = bitmap.height() / tileSize;
1211 for (int x = 0; x <= nx; x++) {
1212 for (int y = 0; y <= ny; y++) {
1214 tileR.set(SkIntToScalar(x * tileSize),
1215 SkIntToScalar(y * tileSize),
1216 SkIntToScalar((x + 1) * tileSize),
1217 SkIntToScalar((y + 1) * tileSize));
1219 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1223 if (!tileR.intersect(srcRect)) {
1229 tileR.roundOut(&iTileR);
1230 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1231 SkIntToScalar(iTileR.fTop));
1233 // Adjust the context matrix to draw at the right x,y in device space
1235 GrContext::AutoMatrix am;
1236 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1237 am.setPreConcat(fContext, tmpM);
1239 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
1242 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1243 // In bleed mode we want to always expand the tile on all edges
1244 // but stay within the bitmap bounds
1245 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1247 // In texture-domain/clamp mode we only want to expand the
1248 // tile on edges interior to "srcRect" (i.e., we want to
1249 // not bleed across the original clamped edges)
1250 srcRect.roundOut(&iClampRect);
1252 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1253 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
1256 if (bitmap.extractSubset(&tmpB, iTileR)) {
1257 // now offset it to make it "local" to our tmp bitmap
1258 tileR.offset(-offset.fX, -offset.fY);
1259 GrTextureParams paramsTemp = params;
1260 bool needsTextureDomain = needs_texture_domain(bitmap,
1263 fContext->getMatrix(),
1265 this->internalDrawBitmap(tmpB,
1271 needsTextureDomain);
1279 * This is called by drawBitmap(), which has to handle images that may be too
1280 * large to be represented by a single texture.
1282 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1283 * and that non-texture portion of the GrPaint has already been setup.
1285 void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1286 const SkRect& srcRect,
1287 const GrTextureParams& params,
1288 const SkPaint& paint,
1289 SkCanvas::DrawBitmapRectFlags flags,
1291 bool needsTextureDomain) {
1292 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1293 bitmap.height() <= fContext->getMaxTextureSize());
1296 SkAutoCachedTexture act(this, bitmap, ¶ms, &texture);
1297 if (NULL == texture) {
1301 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
1303 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1304 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1305 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1306 SkScalarMul(srcRect.fTop, hInv),
1307 SkScalarMul(srcRect.fRight, wInv),
1308 SkScalarMul(srcRect.fBottom, hInv));
1310 SkRect textureDomain = SkRect::MakeEmpty();
1311 SkAutoTUnref<GrFragmentProcessor> fp;
1312 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
1313 // Use a constrained texture domain to avoid color bleeding
1314 SkScalar left, top, right, bottom;
1315 if (srcRect.width() > SK_Scalar1) {
1316 SkScalar border = SK_ScalarHalf / texture->width();
1317 left = paintRect.left() + border;
1318 right = paintRect.right() - border;
1320 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1322 if (srcRect.height() > SK_Scalar1) {
1323 SkScalar border = SK_ScalarHalf / texture->height();
1324 top = paintRect.top() + border;
1325 bottom = paintRect.bottom() - border;
1327 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1329 textureDomain.setLTRB(left, top, right, bottom);
1331 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1333 fp.reset(GrTextureDomainEffect::Create(texture,
1336 GrTextureDomain::kClamp_Mode,
1337 params.filterMode()));
1339 } else if (bicubic) {
1340 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1341 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1342 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
1344 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1347 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1348 // the rest from the SkPaint.
1350 grPaint.addColorProcessor(fp);
1351 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
1352 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1353 SkColor2GrColor(paint.getColor());
1354 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
1356 fContext->drawRectToRect(grPaint, dstRect, paintRect);
1359 static bool filter_texture(SkBaseDevice* device, GrContext* context,
1360 GrTexture* texture, const SkImageFilter* filter,
1361 int w, int h, const SkImageFilter::Context& ctx,
1362 SkBitmap* result, SkIPoint* offset) {
1364 SkDeviceImageFilterProxy proxy(device);
1366 if (filter->canFilterImageGPU()) {
1367 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1368 // filter. Also set the clip wide open and the matrix to identity.
1369 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
1370 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
1376 void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1377 int left, int top, const SkPaint& paint) {
1378 // drawSprite is defined to be in device coords.
1379 CHECK_SHOULD_DRAW(draw, true);
1381 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1382 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1386 int w = bitmap.width();
1387 int h = bitmap.height();
1390 // draw sprite uses the default texture params
1391 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1393 SkImageFilter* filter = paint.getImageFilter();
1394 // This bitmap will own the filtered result as a texture.
1395 SkBitmap filteredBitmap;
1398 SkIPoint offset = SkIPoint::Make(0, 0);
1399 SkMatrix matrix(*draw.fMatrix);
1400 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
1401 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1402 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
1403 // This cache is transient, and is freed (along with all its contained
1404 // textures) when it goes out of scope.
1405 SkImageFilter::Context ctx(matrix, clipBounds, cache);
1406 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
1408 texture = (GrTexture*) filteredBitmap.getTexture();
1409 w = filteredBitmap.width();
1410 h = filteredBitmap.height();
1419 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
1421 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1424 fContext->drawRectToRect(grPaint,
1425 SkRect::MakeXYWH(SkIntToScalar(left),
1431 SK_Scalar1 * w / texture->width(),
1432 SK_Scalar1 * h / texture->height()));
1435 void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
1436 const SkRect* src, const SkRect& dst,
1437 const SkPaint& paint,
1438 SkCanvas::DrawBitmapRectFlags flags) {
1440 SkRect bitmapBounds, tmpSrc;
1442 bitmapBounds.set(0, 0,
1443 SkIntToScalar(bitmap.width()),
1444 SkIntToScalar(bitmap.height()));
1446 // Compute matrix from the two rectangles
1450 tmpSrc = bitmapBounds;
1453 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1455 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1457 if (!bitmapBounds.contains(tmpSrc)) {
1458 if (!tmpSrc.intersect(bitmapBounds)) {
1459 return; // nothing to draw
1465 matrix.mapRect(&tmpDst, tmpSrc);
1467 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1468 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1469 // Translate so that tempDst's top left is at the origin.
1470 matrix = *origDraw.fMatrix;
1471 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1472 draw.writable()->fMatrix = &matrix;
1475 dstSize.fWidth = tmpDst.width();
1476 dstSize.fHeight = tmpDst.height();
1478 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
1481 void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1482 int x, int y, const SkPaint& paint) {
1483 // clear of the source device must occur before CHECK_SHOULD_DRAW
1484 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
1485 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1486 if (dev->fNeedClear) {
1487 // TODO: could check here whether we really need to draw at all
1491 // drawDevice is defined to be in device coords.
1492 CHECK_SHOULD_DRAW(draw, true);
1494 GrRenderTarget* devRT = dev->accessRenderTarget();
1496 if (NULL == (devTex = devRT->asTexture())) {
1500 const SkBitmap& bm = dev->accessBitmap(false);
1502 int h = bm.height();
1504 SkImageFilter* filter = paint.getImageFilter();
1505 // This bitmap will own the filtered result as a texture.
1506 SkBitmap filteredBitmap;
1509 SkIPoint offset = SkIPoint::Make(0, 0);
1510 SkMatrix matrix(*draw.fMatrix);
1511 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
1512 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
1513 // This cache is transient, and is freed (along with all its contained
1514 // textures) when it goes out of scope.
1515 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
1516 SkImageFilter::Context ctx(matrix, clipBounds, cache);
1517 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
1519 devTex = filteredBitmap.getTexture();
1520 w = filteredBitmap.width();
1521 h = filteredBitmap.height();
1530 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
1532 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1535 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1540 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1541 // scratch texture).
1542 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1543 SK_Scalar1 * h / devTex->height());
1545 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1548 bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
1549 return filter->canFilterImageGPU();
1552 bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
1553 const SkImageFilter::Context& ctx,
1554 SkBitmap* result, SkIPoint* offset) {
1555 // want explicitly our impl, so guard against a subclass of us overriding it
1556 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1560 SkAutoLockPixels alp(src, !src.getTexture());
1561 if (!src.getTexture() && !src.readyToDraw()) {
1566 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1567 // must be pushed upstack.
1568 SkAutoCachedTexture act(this, src, NULL, &texture);
1570 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1574 ///////////////////////////////////////////////////////////////////////////////
1576 // must be in SkCanvas::VertexMode order
1577 static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1578 kTriangles_GrPrimitiveType,
1579 kTriangleStrip_GrPrimitiveType,
1580 kTriangleFan_GrPrimitiveType,
1583 void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1584 int vertexCount, const SkPoint vertices[],
1585 const SkPoint texs[], const SkColor colors[],
1587 const uint16_t indices[], int indexCount,
1588 const SkPaint& paint) {
1589 CHECK_SHOULD_DRAW(draw, false);
1591 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
1593 const uint16_t* outIndices;
1594 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1595 GrPrimitiveType primType;
1598 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1599 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
1603 SkPaint copy(paint);
1604 copy.setStyle(SkPaint::kStroke_Style);
1605 copy.setStrokeWidth(0);
1607 // we ignore the shader if texs is null.
1608 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1609 NULL == colors, &grPaint);
1611 primType = kLines_GrPrimitiveType;
1612 int triangleCount = 0;
1613 int n = (NULL == indices) ? vertexCount : indexCount;
1615 case SkCanvas::kTriangles_VertexMode:
1616 triangleCount = n / 3;
1618 case SkCanvas::kTriangleStrip_VertexMode:
1619 case SkCanvas::kTriangleFan_VertexMode:
1620 triangleCount = n - 2;
1624 VertState state(vertexCount, indices, indexCount);
1625 VertState::Proc vertProc = state.chooseProc(vmode);
1627 //number of indices for lines per triangle with kLines
1628 indexCount = triangleCount * 6;
1630 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1631 outIndices = outAlloc.get();
1632 uint16_t* auxIndices = outAlloc.get();
1634 while (vertProc(&state)) {
1635 auxIndices[i] = state.f0;
1636 auxIndices[i + 1] = state.f1;
1637 auxIndices[i + 2] = state.f1;
1638 auxIndices[i + 3] = state.f2;
1639 auxIndices[i + 4] = state.f2;
1640 auxIndices[i + 5] = state.f0;
1644 outIndices = indices;
1645 primType = gVertexMode2PrimitiveType[vmode];
1647 if (NULL == texs || NULL == paint.getShader()) {
1648 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1649 NULL == colors, &grPaint);
1651 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
1656 if (xmode && texs && colors) {
1657 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1658 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1664 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1666 // need to convert byte order and from non-PM to PM
1667 convertedColors.reset(vertexCount);
1669 for (int i = 0; i < vertexCount; ++i) {
1671 if (paint.getAlpha() != 255) {
1672 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1674 convertedColors[i] = SkColor2GrColor(color);
1676 colors = convertedColors.get();
1678 fContext->drawVertices(grPaint,
1688 ///////////////////////////////////////////////////////////////////////////////
1690 void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1691 size_t byteLength, SkScalar x, SkScalar y,
1692 const SkPaint& paint) {
1693 CHECK_SHOULD_DRAW(draw, false);
1694 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
1696 if (fMainTextContext->canDraw(paint)) {
1698 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
1700 SkDEBUGCODE(this->validate();)
1702 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1703 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
1705 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
1707 SkDEBUGCODE(this->validate();)
1709 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1711 // this guy will just call our drawPath()
1712 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
1716 void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1717 size_t byteLength, const SkScalar pos[],
1718 SkScalar constY, int scalarsPerPos,
1719 const SkPaint& paint) {
1720 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
1721 CHECK_SHOULD_DRAW(draw, false);
1723 if (fMainTextContext->canDraw(paint)) {
1725 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
1727 SkDEBUGCODE(this->validate();)
1729 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
1730 constY, scalarsPerPos);
1731 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
1733 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
1735 SkDEBUGCODE(this->validate();)
1737 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
1738 constY, scalarsPerPos);
1740 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1741 scalarsPerPos, paint);
1745 void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1746 size_t len, const SkPath& path,
1747 const SkMatrix* m, const SkPaint& paint) {
1748 CHECK_SHOULD_DRAW(draw, false);
1750 SkASSERT(draw.fDevice == this);
1751 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1754 ///////////////////////////////////////////////////////////////////////////////
1756 bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1757 if (!paint.isLCDRenderText()) {
1758 // we're cool with the paint as is
1762 if (paint.getShader() ||
1763 paint.getXfermode() || // unless its srcover
1764 paint.getMaskFilter() ||
1765 paint.getRasterizer() ||
1766 paint.getColorFilter() ||
1767 paint.getPathEffect() ||
1768 paint.isFakeBoldText() ||
1769 paint.getStyle() != SkPaint::kFill_Style) {
1770 // turn off lcd, but turn on kGenA8
1771 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1772 flags->fFlags |= SkPaint::kGenA8FromLCD_Flag;
1775 // we're cool with the paint as is
1779 void SkGpuDevice::flush() {
1780 DO_DEFERRED_CLEAR();
1781 fContext->resolveRenderTarget(fRenderTarget);
1784 ///////////////////////////////////////////////////////////////////////////////
1786 SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
1788 desc.fConfig = fRenderTarget->config();
1789 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1790 desc.fWidth = info.width();
1791 desc.fHeight = info.height();
1792 desc.fSampleCnt = fRenderTarget->numSamples();
1794 SkAutoTUnref<GrTexture> texture;
1795 // Skia's convention is to only clear a device if it is non-opaque.
1796 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
1798 #if CACHE_COMPATIBLE_DEVICE_TEXTURES
1799 // layers are never draw in repeat modes, so we can request an approx
1800 // match and ignore any padding.
1801 flags |= kCached_Flag;
1802 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1803 GrContext::kApprox_ScratchTexMatch :
1804 GrContext::kExact_ScratchTexMatch;
1805 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1807 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1809 if (texture.get()) {
1810 return SkGpuDevice::Create(texture, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType), flags);
1812 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1813 info.width(), info.height());
1818 SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1819 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples(), &props);
1822 void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
1823 fContext->getLayerCache()->processDeletedPictures();
1825 if (picture->fData.get() && !picture->fData->suitableForLayerOptimization()) {
1829 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
1831 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1836 GPUOptimize(picture);
1838 fContext->getLayerCache()->trackPicture(picture);
1841 bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
1842 const SkMatrix* matrix, const SkPaint* paint) {
1843 // todo: should handle these natively
1844 if (matrix || paint) {
1848 fContext->getLayerCache()->processDeletedPictures();
1851 if (!mainCanvas->getClipBounds(&clipBounds)) {
1855 SkTDArray<GrLayerHoister::HoistedLayer> atlased, nonAtlased;
1857 if (!GrLayerHoister::FindLayersToHoist(mainPicture, clipBounds, &atlased, &nonAtlased,
1858 fContext->getLayerCache())) {
1862 GrReplacements replacements;
1864 GrLayerHoister::DrawLayers(atlased, nonAtlased, &replacements);
1866 // Render the entire picture using new layers
1867 GrRecordReplaceDraw(*mainPicture->fRecord, mainCanvas, mainPicture->fBBH.get(),
1868 &replacements, NULL);
1870 GrLayerHoister::UnlockLayers(fContext->getLayerCache(), atlased, nonAtlased);
1875 SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
1876 // We always return a transient cache, so it is freed after each
1877 // filter traversal.
1878 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);