blob: 3045782895a5b8c35fc7fb0168ca62e1177dba4e [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
9
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000010#include "effects/GrBicubicEffect.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000011#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000012#include "effects/GrSimpleTextureEffect.h"
13
14#include "GrContext.h"
15#include "GrBitmapTextContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000016#include "GrDistanceFieldTextContext.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000017#include "GrLayerCache.h"
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000018#include "GrPictureUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000019
20#include "SkGrTexturePixelRef.h"
21
commit-bot@chromium.org82139702014-03-10 22:53:20 +000022#include "SkBounder.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000023#include "SkColorFilter.h"
24#include "SkDeviceImageFilterProxy.h"
25#include "SkDrawProcs.h"
26#include "SkGlyphCache.h"
27#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000028#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000029#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000030#include "SkPicture.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000031#include "SkRRect.h"
32#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000033#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000034#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkUtils.h"
36#include "SkErrorInternals.h"
37
38#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
39
40#if 0
41 extern bool (*gShouldDrawProc)();
42 #define CHECK_SHOULD_DRAW(draw, forceI) \
43 do { \
44 if (gShouldDrawProc && !gShouldDrawProc()) return; \
45 this->prepareDraw(draw, forceI); \
46 } while (0)
47#else
48 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
49#endif
50
51// This constant represents the screen alignment criterion in texels for
52// requiring texture domain clamping to prevent color bleeding when drawing
53// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000054#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000055
56#define DO_DEFERRED_CLEAR() \
57 do { \
58 if (fNeedClear) { \
59 this->clear(SK_ColorTRANSPARENT); \
60 } \
61 } while (false) \
62
63///////////////////////////////////////////////////////////////////////////////
64
65#define CHECK_FOR_ANNOTATION(paint) \
66 do { if (paint.getAnnotation()) { return; } } while (0)
67
68///////////////////////////////////////////////////////////////////////////////
69
70
71class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
72public:
73 SkAutoCachedTexture()
74 : fDevice(NULL)
75 , fTexture(NULL) {
76 }
77
78 SkAutoCachedTexture(SkGpuDevice* device,
79 const SkBitmap& bitmap,
80 const GrTextureParams* params,
81 GrTexture** texture)
82 : fDevice(NULL)
83 , fTexture(NULL) {
84 SkASSERT(NULL != texture);
85 *texture = this->set(device, bitmap, params);
86 }
87
88 ~SkAutoCachedTexture() {
89 if (NULL != fTexture) {
90 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
91 }
92 }
93
94 GrTexture* set(SkGpuDevice* device,
95 const SkBitmap& bitmap,
96 const GrTextureParams* params) {
97 if (NULL != fTexture) {
98 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
99 fTexture = NULL;
100 }
101 fDevice = device;
102 GrTexture* result = (GrTexture*)bitmap.getTexture();
103 if (NULL == result) {
104 // Cannot return the native texture so look it up in our cache
105 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
106 result = fTexture;
107 }
108 return result;
109 }
110
111private:
112 SkGpuDevice* fDevice;
113 GrTexture* fTexture;
114};
115
116///////////////////////////////////////////////////////////////////////////////
117
118struct GrSkDrawProcs : public SkDrawProcs {
119public:
120 GrContext* fContext;
121 GrTextContext* fTextContext;
122 GrFontScaler* fFontScaler; // cached in the skia glyphcache
123};
124
125///////////////////////////////////////////////////////////////////////////////
126
127static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
128 switch (config) {
129 case kAlpha_8_GrPixelConfig:
130 *isOpaque = false;
131 return SkBitmap::kA8_Config;
132 case kRGB_565_GrPixelConfig:
133 *isOpaque = true;
134 return SkBitmap::kRGB_565_Config;
135 case kRGBA_4444_GrPixelConfig:
136 *isOpaque = false;
137 return SkBitmap::kARGB_4444_Config;
138 case kSkia8888_GrPixelConfig:
139 // we don't currently have a way of knowing whether
140 // a 8888 is opaque based on the config.
141 *isOpaque = false;
142 return SkBitmap::kARGB_8888_Config;
143 default:
144 *isOpaque = false;
145 return SkBitmap::kNo_Config;
146 }
147}
148
149/*
150 * GrRenderTarget does not know its opaqueness, only its config, so we have
151 * to make conservative guesses when we return an "equivalent" bitmap.
152 */
153static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
154 bool isOpaque;
155 SkBitmap::Config config = grConfig2skConfig(renderTarget->config(), &isOpaque);
156
157 SkBitmap bitmap;
158 bitmap.setConfig(config, renderTarget->width(), renderTarget->height(), 0,
159 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
160 return bitmap;
161}
162
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000163SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000164 SkASSERT(NULL != surface);
165 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
166 return NULL;
167 }
168 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000169 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000170 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000171 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000172 }
173}
174
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000175SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000176 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000177 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000178}
179
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000180SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000181 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000182 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000183}
184
185void SkGpuDevice::initFromRenderTarget(GrContext* context,
186 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000187 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000188 fDrawProcs = NULL;
189
190 fContext = context;
191 fContext->ref();
192
commit-bot@chromium.org47841822014-03-27 14:19:17 +0000193 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties));
194 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
195
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000196 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000197 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000198
199 SkASSERT(NULL != renderTarget);
200 fRenderTarget = renderTarget;
201 fRenderTarget->ref();
202
203 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
204 // on the RT but not vice-versa.
205 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
206 // busting chrome (for a currently unknown reason).
207 GrSurface* surface = fRenderTarget->asTexture();
208 if (NULL == surface) {
209 surface = fRenderTarget;
210 }
reed@google.combf790232013-12-13 19:45:58 +0000211
212 SkImageInfo info;
213 surface->asImageInfo(&info);
bungeman@google.coma95a0662014-03-19 23:26:50 +0000214 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, SkToBool(flags & kCached_Flag)));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000215
reed@google.com672588b2014-01-08 15:42:01 +0000216 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000217}
218
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000219SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
220 int sampleCount) {
221 if (kUnknown_SkColorType == origInfo.colorType() ||
222 origInfo.width() < 0 || origInfo.height() < 0) {
223 return NULL;
224 }
225
226 SkImageInfo info = origInfo;
227 // TODO: perhas we can loosen this check now that colortype is more detailed
228 // e.g. can we support both RGBA and BGRA here?
229 if (kRGB_565_SkColorType == info.colorType()) {
230 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
231 } else {
commit-bot@chromium.org149e9a12014-04-09 20:45:29 +0000232 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000233 if (kOpaque_SkAlphaType != info.alphaType()) {
234 info.fAlphaType = kPremul_SkAlphaType; // force this setting
235 }
236 }
237
238 GrTextureDesc desc;
239 desc.fFlags = kRenderTarget_GrTextureFlagBit;
240 desc.fWidth = info.width();
241 desc.fHeight = info.height();
242 desc.fConfig = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType());
243 desc.fSampleCnt = sampleCount;
244
245 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
246 if (!texture.get()) {
247 return NULL;
248 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000249
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000250 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
251}
252
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000253SkGpuDevice::~SkGpuDevice() {
254 if (fDrawProcs) {
255 delete fDrawProcs;
256 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000257
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000258 delete fMainTextContext;
259 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000260
261 // The GrContext takes a ref on the target. We don't want to cause the render
262 // target to be unnecessarily kept alive.
263 if (fContext->getRenderTarget() == fRenderTarget) {
264 fContext->setRenderTarget(NULL);
265 }
266
267 if (fContext->getClip() == &fClipData) {
268 fContext->setClip(NULL);
269 }
270
271 SkSafeUnref(fRenderTarget);
272 fContext->unref();
273}
274
275///////////////////////////////////////////////////////////////////////////////
276
277void SkGpuDevice::makeRenderTargetCurrent() {
278 DO_DEFERRED_CLEAR();
279 fContext->setRenderTarget(fRenderTarget);
280}
281
282///////////////////////////////////////////////////////////////////////////////
283
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000284bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
285 int x, int y) {
286 DO_DEFERRED_CLEAR();
287
288 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
289 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo.colorType(), dstInfo.alphaType());
290 if (kUnknown_GrPixelConfig == config) {
291 return false;
292 }
293
294 uint32_t flags = 0;
295 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
296 flags = GrContext::kUnpremul_PixelOpsFlag;
297 }
298 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
299 config, dstPixels, dstRowBytes, flags);
300}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000301
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000302bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
303 int x, int y) {
304 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
305 GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType());
306 if (kUnknown_GrPixelConfig == config) {
307 return false;
308 }
309 uint32_t flags = 0;
310 if (kUnpremul_SkAlphaType == info.alphaType()) {
311 flags = GrContext::kUnpremul_PixelOpsFlag;
312 }
313 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
314
315 // need to bump our genID for compatibility with clients that "know" we have a bitmap
316 this->onAccessBitmap().notifyPixelsChanged();
317
318 return true;
319}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000320
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000321const SkBitmap& SkGpuDevice::onAccessBitmap() {
322 DO_DEFERRED_CLEAR();
323 return INHERITED::onAccessBitmap();
324}
325
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000326void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
327 INHERITED::onAttachToCanvas(canvas);
328
329 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
330 fClipData.fClipStack = canvas->getClipStack();
331}
332
333void SkGpuDevice::onDetachFromCanvas() {
334 INHERITED::onDetachFromCanvas();
335 fClipData.fClipStack = NULL;
336}
337
338// call this every draw call, to ensure that the context reflects our state,
339// and not the state from some other canvas/device
340void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
341 SkASSERT(NULL != fClipData.fClipStack);
342
343 fContext->setRenderTarget(fRenderTarget);
344
345 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
346
347 if (forceIdentity) {
348 fContext->setIdentityMatrix();
349 } else {
350 fContext->setMatrix(*draw.fMatrix);
351 }
352 fClipData.fOrigin = this->getOrigin();
353
354 fContext->setClip(&fClipData);
355
356 DO_DEFERRED_CLEAR();
357}
358
359GrRenderTarget* SkGpuDevice::accessRenderTarget() {
360 DO_DEFERRED_CLEAR();
361 return fRenderTarget;
362}
363
364///////////////////////////////////////////////////////////////////////////////
365
366SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
367SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
368SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
369SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
370SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
371 shader_type_mismatch);
372SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
373 shader_type_mismatch);
374SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
375SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
376
377namespace {
378
379// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
380// justAlpha indicates that skPaint's alpha should be used rather than the color
381// Callers may subsequently modify the GrPaint. Setting constantColor indicates
382// that the final paint will draw the same color at every pixel. This allows
383// an optimization where the the color filter can be applied to the skPaint's
384// color once while converting to GrPaint and then ignored.
385inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
386 const SkPaint& skPaint,
387 bool justAlpha,
388 bool constantColor,
389 GrPaint* grPaint) {
390
391 grPaint->setDither(skPaint.isDither());
392 grPaint->setAntiAlias(skPaint.isAntiAlias());
393
394 SkXfermode::Coeff sm;
395 SkXfermode::Coeff dm;
396
397 SkXfermode* mode = skPaint.getXfermode();
398 GrEffectRef* xferEffect = NULL;
399 if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
400 if (NULL != xferEffect) {
401 grPaint->addColorEffect(xferEffect)->unref();
402 sm = SkXfermode::kOne_Coeff;
403 dm = SkXfermode::kZero_Coeff;
404 }
405 } else {
406 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
407#if 0
408 return false;
409#else
410 // Fall back to src-over
411 sm = SkXfermode::kOne_Coeff;
412 dm = SkXfermode::kISA_Coeff;
413#endif
414 }
415 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
416
417 if (justAlpha) {
418 uint8_t alpha = skPaint.getAlpha();
419 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
420 // justAlpha is currently set to true only if there is a texture,
421 // so constantColor should not also be true.
422 SkASSERT(!constantColor);
423 } else {
424 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
425 }
426
427 SkColorFilter* colorFilter = skPaint.getColorFilter();
428 if (NULL != colorFilter) {
429 // if the source color is a constant then apply the filter here once rather than per pixel
430 // in a shader.
431 if (constantColor) {
432 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
433 grPaint->setColor(SkColor2GrColor(filtered));
434 } else {
435 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
436 if (NULL != effect.get()) {
437 grPaint->addColorEffect(effect);
438 }
439 }
440 }
441
442 return true;
443}
444
445// This function is similar to skPaint2GrPaintNoShader but also converts
446// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
447// be used is set on grPaint and returned in param act. constantColor has the
448// same meaning as in skPaint2GrPaintNoShader.
449inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
450 const SkPaint& skPaint,
451 bool constantColor,
452 GrPaint* grPaint) {
453 SkShader* shader = skPaint.getShader();
454 if (NULL == shader) {
455 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
456 }
457
commit-bot@chromium.org60770572014-01-13 15:57:05 +0000458 // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state and require
459 // the shader to set a render target .
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000460 GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000461
462 // setup the shader as the first color effect on the paint
463 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
464 if (NULL != effect.get()) {
465 grPaint->addColorEffect(effect);
466 // Now setup the rest of the paint.
467 return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
468 } else {
469 // We still don't have SkColorShader::asNewEffect() implemented.
470 SkShader::GradientInfo info;
471 SkColor color;
472
473 info.fColors = &color;
474 info.fColorOffsets = NULL;
475 info.fColorCount = 1;
476 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
477 SkPaint copy(skPaint);
478 copy.setShader(NULL);
479 // modulate the paint alpha by the shader's solid color alpha
480 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
481 copy.setColor(SkColorSetA(color, newA));
482 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
483 } else {
484 return false;
485 }
486 }
487}
488}
489
490///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000491
492SkBitmap::Config SkGpuDevice::config() const {
493 if (NULL == fRenderTarget) {
494 return SkBitmap::kNo_Config;
495 }
496
497 bool isOpaque;
498 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
499}
500
501void SkGpuDevice::clear(SkColor color) {
502 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
503 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
504 fNeedClear = false;
505}
506
507void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
508 CHECK_SHOULD_DRAW(draw, false);
509
510 GrPaint grPaint;
511 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
512 return;
513 }
514
515 fContext->drawPaint(grPaint);
516}
517
518// must be in SkCanvas::PointMode order
519static const GrPrimitiveType gPointMode2PrimtiveType[] = {
520 kPoints_GrPrimitiveType,
521 kLines_GrPrimitiveType,
522 kLineStrip_GrPrimitiveType
523};
524
525void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
526 size_t count, const SkPoint pts[], const SkPaint& paint) {
527 CHECK_FOR_ANNOTATION(paint);
528 CHECK_SHOULD_DRAW(draw, false);
529
530 SkScalar width = paint.getStrokeWidth();
531 if (width < 0) {
532 return;
533 }
534
535 // we only handle hairlines and paints without path effects or mask filters,
536 // else we let the SkDraw call our drawPath()
537 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
538 draw.drawPoints(mode, count, pts, paint, true);
539 return;
540 }
541
542 GrPaint grPaint;
543 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
544 return;
545 }
546
547 fContext->drawVertices(grPaint,
548 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000549 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000550 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000551 NULL,
552 NULL,
553 NULL,
554 0);
555}
556
557///////////////////////////////////////////////////////////////////////////////
558
559void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
560 const SkPaint& paint) {
561 CHECK_FOR_ANNOTATION(paint);
562 CHECK_SHOULD_DRAW(draw, false);
563
564 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
565 SkScalar width = paint.getStrokeWidth();
566
567 /*
568 We have special code for hairline strokes, miter-strokes, bevel-stroke
569 and fills. Anything else we just call our path code.
570 */
571 bool usePath = doStroke && width > 0 &&
572 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
573 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
574 // another two reasons we might need to call drawPath...
575 if (paint.getMaskFilter() || paint.getPathEffect()) {
576 usePath = true;
577 }
578 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
579#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
580 if (doStroke) {
581#endif
582 usePath = true;
583#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
584 } else {
585 usePath = !fContext->getMatrix().preservesRightAngles();
586 }
587#endif
588 }
589 // until we can both stroke and fill rectangles
590 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
591 usePath = true;
592 }
593
594 if (usePath) {
595 SkPath path;
596 path.addRect(rect);
597 this->drawPath(draw, path, paint, NULL, true);
598 return;
599 }
600
601 GrPaint grPaint;
602 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
603 return;
604 }
605
606 if (!doStroke) {
607 fContext->drawRect(grPaint, rect);
608 } else {
609 SkStrokeRec stroke(paint);
610 fContext->drawRect(grPaint, rect, &stroke);
611 }
612}
613
614///////////////////////////////////////////////////////////////////////////////
615
616void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
617 const SkPaint& paint) {
618 CHECK_FOR_ANNOTATION(paint);
619 CHECK_SHOULD_DRAW(draw, false);
620
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000621 GrPaint grPaint;
622 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
623 return;
624 }
625
626 SkStrokeRec stroke(paint);
627 if (paint.getMaskFilter()) {
628 // try to hit the fast path for drawing filtered round rects
629
630 SkRRect devRRect;
631 if (rect.transform(fContext->getMatrix(), &devRRect)) {
632 if (devRRect.allCornersCircular()) {
633 SkRect maskRect;
634 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
635 draw.fClip->getBounds(),
636 fContext->getMatrix(),
637 &maskRect)) {
638 SkIRect finalIRect;
639 maskRect.roundOut(&finalIRect);
640 if (draw.fClip->quickReject(finalIRect)) {
641 // clipped out
642 return;
643 }
644 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
645 // nothing to draw
646 return;
647 }
648 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
649 stroke, devRRect)) {
650 return;
651 }
652 }
653
654 }
655 }
656
657 }
658
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000659 bool usePath = !rect.isSimple();
660 // another two reasons we might need to call drawPath...
661 if (paint.getMaskFilter() || paint.getPathEffect()) {
662 usePath = true;
663 }
664 // until we can rotate rrects...
665 if (!usePath && !fContext->getMatrix().rectStaysRect()) {
666 usePath = true;
667 }
668
669 if (usePath) {
670 SkPath path;
671 path.addRRect(rect);
672 this->drawPath(draw, path, paint, NULL, true);
673 return;
674 }
675
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000676 fContext->drawRRect(grPaint, rect, stroke);
677}
678
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000679/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000680
681void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
682 const SkPaint& paint) {
683 CHECK_FOR_ANNOTATION(paint);
684 CHECK_SHOULD_DRAW(draw, false);
685
686 bool usePath = false;
687 // some basic reasons we might need to call drawPath...
688 if (paint.getMaskFilter() || paint.getPathEffect()) {
689 usePath = true;
690 }
691
692 if (usePath) {
693 SkPath path;
694 path.addOval(oval);
695 this->drawPath(draw, path, paint, NULL, true);
696 return;
697 }
698
699 GrPaint grPaint;
700 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
701 return;
702 }
703 SkStrokeRec stroke(paint);
704
705 fContext->drawOval(grPaint, oval, stroke);
706}
707
708#include "SkMaskFilter.h"
709#include "SkBounder.h"
710
711///////////////////////////////////////////////////////////////////////////////
712
713// helpers for applying mask filters
714namespace {
715
716// Draw a mask using the supplied paint. Since the coverage/geometry
717// is already burnt into the mask this boils down to a rect draw.
718// Return true if the mask was successfully drawn.
719bool draw_mask(GrContext* context, const SkRect& maskRect,
720 GrPaint* grp, GrTexture* mask) {
721 GrContext::AutoMatrix am;
722 if (!am.setIdentity(context, grp)) {
723 return false;
724 }
725
726 SkMatrix matrix;
727 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
728 matrix.postIDiv(mask->width(), mask->height());
729
730 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
731 context->drawRect(*grp, maskRect);
732 return true;
733}
734
735bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
736 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
737 GrPaint* grp, SkPaint::Style style) {
738 SkMask srcM, dstM;
739
740 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
741 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
742 return false;
743 }
744 SkAutoMaskFreeImage autoSrc(srcM.fImage);
745
746 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
747 return false;
748 }
749 // this will free-up dstM when we're done (allocated in filterMask())
750 SkAutoMaskFreeImage autoDst(dstM.fImage);
751
752 if (clip.quickReject(dstM.fBounds)) {
753 return false;
754 }
755 if (bounder && !bounder->doIRect(dstM.fBounds)) {
756 return false;
757 }
758
759 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
760 // the current clip (and identity matrix) and GrPaint settings
761 GrTextureDesc desc;
762 desc.fWidth = dstM.fBounds.width();
763 desc.fHeight = dstM.fBounds.height();
764 desc.fConfig = kAlpha_8_GrPixelConfig;
765
766 GrAutoScratchTexture ast(context, desc);
767 GrTexture* texture = ast.texture();
768
769 if (NULL == texture) {
770 return false;
771 }
772 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
773 dstM.fImage, dstM.fRowBytes);
774
775 SkRect maskRect = SkRect::Make(dstM.fBounds);
776
777 return draw_mask(context, maskRect, grp, texture);
778}
779
780// Create a mask of 'devPath' and place the result in 'mask'. Return true on
781// success; false otherwise.
782bool create_mask_GPU(GrContext* context,
783 const SkRect& maskRect,
784 const SkPath& devPath,
785 const SkStrokeRec& stroke,
786 bool doAA,
787 GrAutoScratchTexture* mask) {
788 GrTextureDesc desc;
789 desc.fFlags = kRenderTarget_GrTextureFlagBit;
790 desc.fWidth = SkScalarCeilToInt(maskRect.width());
791 desc.fHeight = SkScalarCeilToInt(maskRect.height());
792 // We actually only need A8, but it often isn't supported as a
793 // render target so default to RGBA_8888
794 desc.fConfig = kRGBA_8888_GrPixelConfig;
795 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
796 desc.fConfig = kAlpha_8_GrPixelConfig;
797 }
798
799 mask->set(context, desc);
800 if (NULL == mask->texture()) {
801 return false;
802 }
803
804 GrTexture* maskTexture = mask->texture();
805 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
806
807 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
808 GrContext::AutoClip ac(context, clipRect);
809
810 context->clear(NULL, 0x0, true);
811
812 GrPaint tempPaint;
813 if (doAA) {
814 tempPaint.setAntiAlias(true);
815 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
816 // blend coeff of zero requires dual source blending support in order
817 // to properly blend partially covered pixels. This means the AA
818 // code path may not be taken. So we use a dst blend coeff of ISA. We
819 // could special case AA draws to a dst surface with known alpha=0 to
820 // use a zero dst coeff when dual source blending isn't available.
821 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
822 }
823
824 GrContext::AutoMatrix am;
825
826 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
827 SkMatrix translate;
828 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
829 am.set(context, translate);
830 context->drawPath(tempPaint, devPath, stroke);
831 return true;
832}
833
834SkBitmap wrap_texture(GrTexture* texture) {
reed@google.combf790232013-12-13 19:45:58 +0000835 SkImageInfo info;
836 texture->asImageInfo(&info);
837
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000838 SkBitmap result;
reed@google.combf790232013-12-13 19:45:58 +0000839 result.setConfig(info);
840 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000841 return result;
842}
843
844};
845
846void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
847 const SkPaint& paint, const SkMatrix* prePathMatrix,
848 bool pathIsMutable) {
849 CHECK_FOR_ANNOTATION(paint);
850 CHECK_SHOULD_DRAW(draw, false);
851
852 GrPaint grPaint;
853 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
854 return;
855 }
856
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000857 // If we have a prematrix, apply it to the path, optimizing for the case
858 // where the original path can in fact be modified in place (even though
859 // its parameter type is const).
860 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000861 SkTLazy<SkPath> tmpPath;
862 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000863
864 if (prePathMatrix) {
865 SkPath* result = pathPtr;
866
867 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000868 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000869 pathIsMutable = true;
870 }
871 // should I push prePathMatrix on our MV stack temporarily, instead
872 // of applying it here? See SkDraw.cpp
873 pathPtr->transform(*prePathMatrix, result);
874 pathPtr = result;
875 }
876 // at this point we're done with prePathMatrix
877 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
878
879 SkStrokeRec stroke(paint);
880 SkPathEffect* pathEffect = paint.getPathEffect();
881 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000882 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000883 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000884 pathPtr = effectPath.get();
885 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000886 }
887
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000888 if (paint.getMaskFilter()) {
889 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000890 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
891 if (stroke.applyToPath(strokedPath, *pathPtr)) {
892 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000893 pathIsMutable = true;
894 stroke.setFillStyle();
895 }
896 }
897
898 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000899 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000900
901 // transform the path into device space
902 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000903
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000904 SkRect maskRect;
905 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
906 draw.fClip->getBounds(),
907 fContext->getMatrix(),
908 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000909 // The context's matrix may change while creating the mask, so save the CTM here to
910 // pass to filterMaskGPU.
911 const SkMatrix ctm = fContext->getMatrix();
912
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000913 SkIRect finalIRect;
914 maskRect.roundOut(&finalIRect);
915 if (draw.fClip->quickReject(finalIRect)) {
916 // clipped out
917 return;
918 }
919 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
920 // nothing to draw
921 return;
922 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000923
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000924 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000925 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000926 // the mask filter was able to draw itself directly, so there's nothing
927 // left to do.
928 return;
929 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000930
931 GrAutoScratchTexture mask;
932
933 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
934 grPaint.isAntiAlias(), &mask)) {
935 GrTexture* filtered;
936
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000937 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000938 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000939 // filterMaskGPU gives us ownership of a ref to the result
940 SkAutoTUnref<GrTexture> atu(filtered);
941
942 // If the scratch texture that we used as the filter src also holds the filter
943 // result then we must detach so that this texture isn't recycled for a later
944 // draw.
945 if (filtered == mask.texture()) {
946 mask.detach();
947 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
948 }
949
950 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
951 // This path is completely drawn
952 return;
953 }
954 }
955 }
956 }
957
958 // draw the mask on the CPU - this is a fallthrough path in case the
959 // GPU path fails
960 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
961 SkPaint::kFill_Style;
962 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
963 *draw.fClip, draw.fBounder, &grPaint, style);
964 return;
965 }
966
967 fContext->drawPath(grPaint, *pathPtr, stroke);
968}
969
970static const int kBmpSmallTileSize = 1 << 10;
971
972static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
973 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
974 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
975 return tilesX * tilesY;
976}
977
978static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
979 if (maxTileSize <= kBmpSmallTileSize) {
980 return maxTileSize;
981 }
982
983 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
984 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
985
986 maxTileTotalTileSize *= maxTileSize * maxTileSize;
987 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
988
989 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
990 return kBmpSmallTileSize;
991 } else {
992 return maxTileSize;
993 }
994}
995
996// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
997// pixels from the bitmap are necessary.
998static void determine_clipped_src_rect(const GrContext* context,
999 const SkBitmap& bitmap,
1000 const SkRect* srcRectPtr,
1001 SkIRect* clippedSrcIRect) {
1002 const GrClipData* clip = context->getClip();
1003 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
1004 SkMatrix inv;
1005 if (!context->getMatrix().invert(&inv)) {
1006 clippedSrcIRect->setEmpty();
1007 return;
1008 }
1009 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
1010 inv.mapRect(&clippedSrcRect);
1011 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001012 // we've setup src space 0,0 to map to the top left of the src rect.
1013 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001014 if (!clippedSrcRect.intersect(*srcRectPtr)) {
1015 clippedSrcIRect->setEmpty();
1016 return;
1017 }
1018 }
1019 clippedSrcRect.roundOut(clippedSrcIRect);
1020 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1021 if (!clippedSrcIRect->intersect(bmpBounds)) {
1022 clippedSrcIRect->setEmpty();
1023 }
1024}
1025
1026bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1027 const GrTextureParams& params,
1028 const SkRect* srcRectPtr,
1029 int maxTileSize,
1030 int* tileSize,
1031 SkIRect* clippedSrcRect) const {
1032 // if bitmap is explictly texture backed then just use the texture
1033 if (NULL != bitmap.getTexture()) {
1034 return false;
1035 }
1036
1037 // if it's larger than the max tile size, then we have no choice but tiling.
1038 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
1039 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1040 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
1041 return true;
1042 }
1043
1044 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
1045 return false;
1046 }
1047
1048 // if the entire texture is already in our cache then no reason to tile it
1049 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
1050 return false;
1051 }
1052
1053 // At this point we know we could do the draw by uploading the entire bitmap
1054 // as a texture. However, if the texture would be large compared to the
1055 // cache size and we don't require most of it for this draw then tile to
1056 // reduce the amount of upload and cache spill.
1057
1058 // assumption here is that sw bitmap size is a good proxy for its size as
1059 // a texture
1060 size_t bmpSize = bitmap.getSize();
1061 size_t cacheSize;
1062 fContext->getTextureCacheLimits(NULL, &cacheSize);
1063 if (bmpSize < cacheSize / 2) {
1064 return false;
1065 }
1066
1067 // Figure out how much of the src we will need based on the src rect and clipping.
1068 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1069 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1070 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
1071 kBmpSmallTileSize * kBmpSmallTileSize;
1072
1073 return usedTileBytes < 2 * bmpSize;
1074}
1075
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001076void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001077 const SkBitmap& bitmap,
1078 const SkMatrix& m,
1079 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001080 SkMatrix concat;
1081 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1082 if (!m.isIdentity()) {
1083 concat.setConcat(*draw->fMatrix, m);
1084 draw.writable()->fMatrix = &concat;
1085 }
1086 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001087}
1088
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001089// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001090// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1091// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001092static inline void clamped_outset_with_offset(SkIRect* iRect,
1093 int outset,
1094 SkPoint* offset,
1095 const SkIRect& clamp) {
1096 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001097
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001098 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1099 if (leftClampDelta > 0) {
1100 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001101 iRect->fLeft = clamp.fLeft;
1102 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001103 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001104 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001105
1106 int topClampDelta = clamp.fTop - iRect->fTop;
1107 if (topClampDelta > 0) {
1108 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001109 iRect->fTop = clamp.fTop;
1110 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001111 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001112 }
1113
1114 if (iRect->fRight > clamp.fRight) {
1115 iRect->fRight = clamp.fRight;
1116 }
1117 if (iRect->fBottom > clamp.fBottom) {
1118 iRect->fBottom = clamp.fBottom;
1119 }
1120}
1121
1122void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1123 const SkBitmap& bitmap,
1124 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001125 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001126 const SkPaint& paint,
1127 SkCanvas::DrawBitmapRectFlags flags) {
1128 CHECK_SHOULD_DRAW(draw, false);
1129
1130 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001131 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001132 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1133 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001134 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001135 SkScalar w = SkIntToScalar(bitmap.width());
1136 SkScalar h = SkIntToScalar(bitmap.height());
1137 dstSize.fWidth = w;
1138 dstSize.fHeight = h;
1139 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001140 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001141 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001142 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001143 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001144 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001145 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1146 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1147 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1148 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001149 }
1150
1151 if (paint.getMaskFilter()){
1152 // Convert the bitmap to a shader so that the rect can be drawn
1153 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001154 SkBitmap tmp; // subset of bitmap, if necessary
1155 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001156 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001157 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001158 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1159 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1160 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001161 // In bleed mode we position and trim the bitmap based on the src rect which is
1162 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1163 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1164 // compensate.
1165 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1166 SkIRect iSrc;
1167 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001168
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001169 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1170 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001171
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001172 if (!bitmap.extractSubset(&tmp, iSrc)) {
1173 return; // extraction failed
1174 }
1175 bitmapPtr = &tmp;
1176 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001177
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001178 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001179 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001180 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001181 } else {
1182 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001183 }
1184
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001185 SkPaint paintWithShader(paint);
1186 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001187 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001188 paintWithShader.getShader()->setLocalMatrix(localM);
1189 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1190 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001191
1192 return;
1193 }
1194
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001195 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1196 // the view matrix rather than a local matrix.
1197 SkMatrix m;
1198 m.setScale(dstSize.fWidth / srcRect.width(),
1199 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001200 fContext->concatMatrix(m);
1201
1202 GrTextureParams params;
1203 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1204 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001205
1206 int tileFilterPad;
1207 bool doBicubic = false;
1208
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001209 switch(paintFilterLevel) {
1210 case SkPaint::kNone_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001211 tileFilterPad = 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001212 textureFilterMode = GrTextureParams::kNone_FilterMode;
1213 break;
1214 case SkPaint::kLow_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001215 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001216 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1217 break;
1218 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001219 tileFilterPad = 1;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001220 if (fContext->getMatrix().getMinStretch() < SK_Scalar1) {
1221 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1222 } else {
1223 // Don't trigger MIP level generation unnecessarily.
1224 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1225 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001226 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001227 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001228 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001229 if (fContext->getMatrix().getMinStretch() >= SK_Scalar1) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001230 // We will install an effect that does the filtering in the shader.
1231 textureFilterMode = GrTextureParams::kNone_FilterMode;
1232 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1233 doBicubic = true;
1234 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001235 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1236 tileFilterPad = 1;
1237 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001238 break;
1239 default:
1240 SkErrorInternals::SetError( kInvalidPaint_SkError,
1241 "Sorry, I don't understand the filtering "
1242 "mode you asked for. Falling back to "
1243 "MIPMaps.");
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001244 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001245 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1246 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001247 }
1248
1249 params.setFilterMode(textureFilterMode);
1250
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001251 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001252 int tileSize;
1253
1254 SkIRect clippedSrcRect;
1255 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1256 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001257 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1258 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001259 } else {
1260 // take the simple case
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001261 this->internalDrawBitmap(bitmap, srcRect, params, paint, flags, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001262 }
1263}
1264
1265// Break 'bitmap' into several tiles to draw it since it has already
1266// been determined to be too large to fit in VRAM
1267void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1268 const SkRect& srcRect,
1269 const SkIRect& clippedSrcIRect,
1270 const GrTextureParams& params,
1271 const SkPaint& paint,
1272 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001273 int tileSize,
1274 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001275 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1276
1277 int nx = bitmap.width() / tileSize;
1278 int ny = bitmap.height() / tileSize;
1279 for (int x = 0; x <= nx; x++) {
1280 for (int y = 0; y <= ny; y++) {
1281 SkRect tileR;
1282 tileR.set(SkIntToScalar(x * tileSize),
1283 SkIntToScalar(y * tileSize),
1284 SkIntToScalar((x + 1) * tileSize),
1285 SkIntToScalar((y + 1) * tileSize));
1286
1287 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1288 continue;
1289 }
1290
1291 if (!tileR.intersect(srcRect)) {
1292 continue;
1293 }
1294
1295 SkBitmap tmpB;
1296 SkIRect iTileR;
1297 tileR.roundOut(&iTileR);
1298 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1299 SkIntToScalar(iTileR.fTop));
1300
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001301 // Adjust the context matrix to draw at the right x,y in device space
1302 SkMatrix tmpM;
1303 GrContext::AutoMatrix am;
1304 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1305 am.setPreConcat(fContext, tmpM);
1306
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001307 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001308 SkIRect iClampRect;
1309
1310 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1311 // In bleed mode we want to always expand the tile on all edges
1312 // but stay within the bitmap bounds
1313 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1314 } else {
1315 // In texture-domain/clamp mode we only want to expand the
1316 // tile on edges interior to "srcRect" (i.e., we want to
1317 // not bleed across the original clamped edges)
1318 srcRect.roundOut(&iClampRect);
1319 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001320 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1321 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001322 }
1323
1324 if (bitmap.extractSubset(&tmpB, iTileR)) {
1325 // now offset it to make it "local" to our tmp bitmap
1326 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001327
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001328 this->internalDrawBitmap(tmpB, tileR, params, paint, flags, bicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001329 }
1330 }
1331 }
1332}
1333
1334static bool has_aligned_samples(const SkRect& srcRect,
1335 const SkRect& transformedRect) {
1336 // detect pixel disalignment
1337 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1338 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1339 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1340 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1341 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1342 COLOR_BLEED_TOLERANCE &&
1343 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1344 COLOR_BLEED_TOLERANCE) {
1345 return true;
1346 }
1347 return false;
1348}
1349
1350static bool may_color_bleed(const SkRect& srcRect,
1351 const SkRect& transformedRect,
1352 const SkMatrix& m) {
1353 // Only gets called if has_aligned_samples returned false.
1354 // So we can assume that sampling is axis aligned but not texel aligned.
1355 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1356 SkRect innerSrcRect(srcRect), innerTransformedRect,
1357 outerTransformedRect(transformedRect);
1358 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1359 m.mapRect(&innerTransformedRect, innerSrcRect);
1360
1361 // The gap between outerTransformedRect and innerTransformedRect
1362 // represents the projection of the source border area, which is
1363 // problematic for color bleeding. We must check whether any
1364 // destination pixels sample the border area.
1365 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1366 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1367 SkIRect outer, inner;
1368 outerTransformedRect.round(&outer);
1369 innerTransformedRect.round(&inner);
1370 // If the inner and outer rects round to the same result, it means the
1371 // border does not overlap any pixel centers. Yay!
1372 return inner != outer;
1373}
1374
1375
1376/*
1377 * This is called by drawBitmap(), which has to handle images that may be too
1378 * large to be represented by a single texture.
1379 *
1380 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1381 * and that non-texture portion of the GrPaint has already been setup.
1382 */
1383void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1384 const SkRect& srcRect,
1385 const GrTextureParams& params,
1386 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001387 SkCanvas::DrawBitmapRectFlags flags,
1388 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001389 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1390 bitmap.height() <= fContext->getMaxTextureSize());
1391
1392 GrTexture* texture;
1393 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1394 if (NULL == texture) {
1395 return;
1396 }
1397
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001398 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001399 SkRect paintRect;
1400 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1401 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1402 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1403 SkScalarMul(srcRect.fTop, hInv),
1404 SkScalarMul(srcRect.fRight, wInv),
1405 SkScalarMul(srcRect.fBottom, hInv));
1406
1407 bool needsTextureDomain = false;
1408 if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) &&
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001409 (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode)) {
1410 // Need texture domain if drawing a sub rect
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001411 needsTextureDomain = srcRect.width() < bitmap.width() ||
1412 srcRect.height() < bitmap.height();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001413 if (!bicubic && needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001414 const SkMatrix& matrix = fContext->getMatrix();
1415 // sampling is axis-aligned
1416 SkRect transformedRect;
1417 matrix.mapRect(&transformedRect, srcRect);
1418
1419 if (has_aligned_samples(srcRect, transformedRect)) {
1420 // We could also turn off filtering here (but we already did a cache lookup with
1421 // params).
1422 needsTextureDomain = false;
1423 } else {
1424 needsTextureDomain = may_color_bleed(srcRect, transformedRect, matrix);
1425 }
1426 }
1427 }
1428
1429 SkRect textureDomain = SkRect::MakeEmpty();
1430 SkAutoTUnref<GrEffectRef> effect;
1431 if (needsTextureDomain) {
1432 // Use a constrained texture domain to avoid color bleeding
1433 SkScalar left, top, right, bottom;
1434 if (srcRect.width() > SK_Scalar1) {
1435 SkScalar border = SK_ScalarHalf / texture->width();
1436 left = paintRect.left() + border;
1437 right = paintRect.right() - border;
1438 } else {
1439 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1440 }
1441 if (srcRect.height() > SK_Scalar1) {
1442 SkScalar border = SK_ScalarHalf / texture->height();
1443 top = paintRect.top() + border;
1444 bottom = paintRect.bottom() - border;
1445 } else {
1446 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1447 }
1448 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001449 if (bicubic) {
1450 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1451 } else {
1452 effect.reset(GrTextureDomainEffect::Create(texture,
1453 SkMatrix::I(),
1454 textureDomain,
1455 GrTextureDomain::kClamp_Mode,
1456 params.filterMode()));
1457 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001458 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001459 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1460 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1461 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001462 } else {
1463 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1464 }
1465
1466 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1467 // the rest from the SkPaint.
1468 GrPaint grPaint;
1469 grPaint.addColorEffect(effect);
1470 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1471 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1472 return;
1473 }
1474
1475 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1476}
1477
1478static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001479 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001480 int w, int h, const SkImageFilter::Context& ctx,
1481 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001482 SkASSERT(filter);
1483 SkDeviceImageFilterProxy proxy(device);
1484
1485 if (filter->canFilterImageGPU()) {
1486 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1487 // filter. Also set the clip wide open and the matrix to identity.
1488 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001489 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001490 } else {
1491 return false;
1492 }
1493}
1494
1495void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1496 int left, int top, const SkPaint& paint) {
1497 // drawSprite is defined to be in device coords.
1498 CHECK_SHOULD_DRAW(draw, true);
1499
1500 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1501 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1502 return;
1503 }
1504
1505 int w = bitmap.width();
1506 int h = bitmap.height();
1507
1508 GrTexture* texture;
1509 // draw sprite uses the default texture params
1510 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1511
1512 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001513 // This bitmap will own the filtered result as a texture.
1514 SkBitmap filteredBitmap;
1515
1516 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001517 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001518 SkMatrix matrix(*draw.fMatrix);
1519 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001520 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1521 SkImageFilter::Context ctx(matrix, clipBounds);
1522 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001523 &offset)) {
1524 texture = (GrTexture*) filteredBitmap.getTexture();
1525 w = filteredBitmap.width();
1526 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001527 left += offset.x();
1528 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001529 } else {
1530 return;
1531 }
1532 }
1533
1534 GrPaint grPaint;
1535 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1536
1537 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1538 return;
1539 }
1540
1541 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001542 SkRect::MakeXYWH(SkIntToScalar(left),
1543 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001544 SkIntToScalar(w),
1545 SkIntToScalar(h)),
1546 SkRect::MakeXYWH(0,
1547 0,
1548 SK_Scalar1 * w / texture->width(),
1549 SK_Scalar1 * h / texture->height()));
1550}
1551
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001552void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001553 const SkRect* src, const SkRect& dst,
1554 const SkPaint& paint,
1555 SkCanvas::DrawBitmapRectFlags flags) {
1556 SkMatrix matrix;
1557 SkRect bitmapBounds, tmpSrc;
1558
1559 bitmapBounds.set(0, 0,
1560 SkIntToScalar(bitmap.width()),
1561 SkIntToScalar(bitmap.height()));
1562
1563 // Compute matrix from the two rectangles
1564 if (NULL != src) {
1565 tmpSrc = *src;
1566 } else {
1567 tmpSrc = bitmapBounds;
1568 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001569
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001570 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1571
1572 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1573 if (NULL != src) {
1574 if (!bitmapBounds.contains(tmpSrc)) {
1575 if (!tmpSrc.intersect(bitmapBounds)) {
1576 return; // nothing to draw
1577 }
1578 }
1579 }
1580
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001581 SkRect tmpDst;
1582 matrix.mapRect(&tmpDst, tmpSrc);
1583
1584 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1585 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1586 // Translate so that tempDst's top left is at the origin.
1587 matrix = *origDraw.fMatrix;
1588 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1589 draw.writable()->fMatrix = &matrix;
1590 }
1591 SkSize dstSize;
1592 dstSize.fWidth = tmpDst.width();
1593 dstSize.fHeight = tmpDst.height();
1594
1595 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001596}
1597
1598void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1599 int x, int y, const SkPaint& paint) {
1600 // clear of the source device must occur before CHECK_SHOULD_DRAW
1601 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1602 if (dev->fNeedClear) {
1603 // TODO: could check here whether we really need to draw at all
1604 dev->clear(0x0);
1605 }
1606
1607 // drawDevice is defined to be in device coords.
1608 CHECK_SHOULD_DRAW(draw, true);
1609
1610 GrRenderTarget* devRT = dev->accessRenderTarget();
1611 GrTexture* devTex;
1612 if (NULL == (devTex = devRT->asTexture())) {
1613 return;
1614 }
1615
1616 const SkBitmap& bm = dev->accessBitmap(false);
1617 int w = bm.width();
1618 int h = bm.height();
1619
1620 SkImageFilter* filter = paint.getImageFilter();
1621 // This bitmap will own the filtered result as a texture.
1622 SkBitmap filteredBitmap;
1623
1624 if (NULL != filter) {
1625 SkIPoint offset = SkIPoint::Make(0, 0);
1626 SkMatrix matrix(*draw.fMatrix);
1627 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001628 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
1629 SkImageFilter::Context ctx(matrix, clipBounds);
1630 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001631 &offset)) {
1632 devTex = filteredBitmap.getTexture();
1633 w = filteredBitmap.width();
1634 h = filteredBitmap.height();
1635 x += offset.fX;
1636 y += offset.fY;
1637 } else {
1638 return;
1639 }
1640 }
1641
1642 GrPaint grPaint;
1643 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1644
1645 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1646 return;
1647 }
1648
1649 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1650 SkIntToScalar(y),
1651 SkIntToScalar(w),
1652 SkIntToScalar(h));
1653
1654 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1655 // scratch texture).
1656 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1657 SK_Scalar1 * h / devTex->height());
1658
1659 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1660}
1661
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001662bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001663 return filter->canFilterImageGPU();
1664}
1665
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001666bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001667 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001668 SkBitmap* result, SkIPoint* offset) {
1669 // want explicitly our impl, so guard against a subclass of us overriding it
1670 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1671 return false;
1672 }
1673
1674 SkAutoLockPixels alp(src, !src.getTexture());
1675 if (!src.getTexture() && !src.readyToDraw()) {
1676 return false;
1677 }
1678
1679 GrTexture* texture;
1680 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1681 // must be pushed upstack.
1682 SkAutoCachedTexture act(this, src, NULL, &texture);
1683
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001684 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1685 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001686}
1687
1688///////////////////////////////////////////////////////////////////////////////
1689
1690// must be in SkCanvas::VertexMode order
1691static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1692 kTriangles_GrPrimitiveType,
1693 kTriangleStrip_GrPrimitiveType,
1694 kTriangleFan_GrPrimitiveType,
1695};
1696
1697void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1698 int vertexCount, const SkPoint vertices[],
1699 const SkPoint texs[], const SkColor colors[],
1700 SkXfermode* xmode,
1701 const uint16_t indices[], int indexCount,
1702 const SkPaint& paint) {
1703 CHECK_SHOULD_DRAW(draw, false);
1704
1705 GrPaint grPaint;
1706 // we ignore the shader if texs is null.
1707 if (NULL == texs) {
1708 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
1709 return;
1710 }
1711 } else {
1712 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
1713 return;
1714 }
1715 }
1716
1717 if (NULL != xmode && NULL != texs && NULL != colors) {
1718 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1719 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1720#if 0
1721 return
1722#endif
1723 }
1724 }
1725
1726 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1727 if (NULL != colors) {
1728 // need to convert byte order and from non-PM to PM
1729 convertedColors.reset(vertexCount);
1730 for (int i = 0; i < vertexCount; ++i) {
1731 convertedColors[i] = SkColor2GrColor(colors[i]);
1732 }
1733 colors = convertedColors.get();
1734 }
1735 fContext->drawVertices(grPaint,
1736 gVertexMode2PrimitiveType[vmode],
1737 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001738 vertices,
1739 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001740 colors,
1741 indices,
1742 indexCount);
1743}
1744
1745///////////////////////////////////////////////////////////////////////////////
1746
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001747void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1748 size_t byteLength, SkScalar x, SkScalar y,
1749 const SkPaint& paint) {
1750 CHECK_SHOULD_DRAW(draw, false);
1751
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001752 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001753 GrPaint grPaint;
1754 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1755 return;
1756 }
1757
1758 SkDEBUGCODE(this->validate();)
1759
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001760 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1761 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001762 GrPaint grPaint;
1763 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1764 return;
1765 }
1766
1767 SkDEBUGCODE(this->validate();)
1768
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001769 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001770 } else {
1771 // this guy will just call our drawPath()
1772 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001773 }
1774}
1775
1776void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1777 size_t byteLength, const SkScalar pos[],
1778 SkScalar constY, int scalarsPerPos,
1779 const SkPaint& paint) {
1780 CHECK_SHOULD_DRAW(draw, false);
1781
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001782 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001783 GrPaint grPaint;
1784 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1785 return;
1786 }
1787
1788 SkDEBUGCODE(this->validate();)
1789
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001790 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001791 constY, scalarsPerPos);
1792 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001793 GrPaint grPaint;
1794 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1795 return;
1796 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001797
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001798 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001799
1800 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001801 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001802 } else {
1803 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1804 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001805 }
1806}
1807
1808void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1809 size_t len, const SkPath& path,
1810 const SkMatrix* m, const SkPaint& paint) {
1811 CHECK_SHOULD_DRAW(draw, false);
1812
1813 SkASSERT(draw.fDevice == this);
1814 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1815}
1816
1817///////////////////////////////////////////////////////////////////////////////
1818
1819bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1820 if (!paint.isLCDRenderText()) {
1821 // we're cool with the paint as is
1822 return false;
1823 }
1824
1825 if (paint.getShader() ||
1826 paint.getXfermode() || // unless its srcover
1827 paint.getMaskFilter() ||
1828 paint.getRasterizer() ||
1829 paint.getColorFilter() ||
1830 paint.getPathEffect() ||
1831 paint.isFakeBoldText() ||
1832 paint.getStyle() != SkPaint::kFill_Style) {
1833 // turn off lcd
1834 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1835 flags->fHinting = paint.getHinting();
1836 return true;
1837 }
1838 // we're cool with the paint as is
1839 return false;
1840}
1841
1842void SkGpuDevice::flush() {
1843 DO_DEFERRED_CLEAR();
1844 fContext->resolveRenderTarget(fRenderTarget);
1845}
1846
1847///////////////////////////////////////////////////////////////////////////////
1848
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001849SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001850 GrTextureDesc desc;
1851 desc.fConfig = fRenderTarget->config();
1852 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001853 desc.fWidth = info.width();
1854 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001855 desc.fSampleCnt = fRenderTarget->numSamples();
1856
1857 SkAutoTUnref<GrTexture> texture;
1858 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001859 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001860
1861#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1862 // layers are never draw in repeat modes, so we can request an approx
1863 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001864 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001865 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1866 GrContext::kApprox_ScratchTexMatch :
1867 GrContext::kExact_ScratchTexMatch;
1868 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1869#else
1870 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1871#endif
1872 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001873 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001874 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001875 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1876 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001877 return NULL;
1878 }
1879}
1880
reed@google.com76f10a32014-02-05 15:32:21 +00001881SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1882 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1883}
1884
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001885// In the future this may not be a static method if we need to incorporate the
1886// clip and matrix state into the key
1887SkPicture::AccelData::Key SkGpuDevice::ComputeAccelDataKey() {
1888 static const SkPicture::AccelData::Key gGPUID = SkPicture::AccelData::GenerateDomain();
1889
1890 return gGPUID;
1891}
1892
1893void SkGpuDevice::EXPERIMENTAL_optimize(SkPicture* picture) {
1894 SkPicture::AccelData::Key key = ComputeAccelDataKey();
1895
1896 GPUAccelData* data = SkNEW_ARGS(GPUAccelData, (key));
1897
1898 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001899
1900 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001901}
1902
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001903bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkPicture* picture) {
1904
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001905 SkPicture::AccelData::Key key = ComputeAccelDataKey();
1906
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001907 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001908 if (NULL == data) {
1909 return false;
1910 }
1911
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001912 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001913
1914//#define SK_PRINT_PULL_FORWARD_INFO 1
1915
1916#ifdef SK_PRINT_PULL_FORWARD_INFO
1917 static bool gPrintedAccelData = false;
1918
1919 if (!gPrintedAccelData) {
1920 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1921 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1922
skia.committer@gmail.com2c48ee82014-04-01 03:07:47 +00001923 SkDebugf("%d: Width: %d Height: %d SL: %d R: %d hasNestedLayers: %s\n",
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001924 i,
skia.committer@gmail.com2c48ee82014-04-01 03:07:47 +00001925 info.fSize.fWidth,
1926 info.fSize.fHeight,
1927 info.fSaveLayerOpID,
1928 info.fRestoreOpID,
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001929 info.fHasNestedLayers ? "T" : "F");
1930 }
1931 gPrintedAccelData = true;
1932 }
1933#endif
1934
1935 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1936 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1937 pullForward[i] = false;
1938 }
1939
1940 SkIRect clip;
1941
1942 fClipData.getConservativeBounds(this->width(), this->height(), &clip, NULL);
1943
1944 SkMatrix inv;
1945 if (!fContext->getMatrix().invert(&inv)) {
1946 return false;
1947 }
1948
1949 SkRect r = SkRect::Make(clip);
1950 inv.mapRect(&r);
1951 r.roundOut(&clip);
1952
1953 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(clip);
1954
1955#ifdef SK_PRINT_PULL_FORWARD_INFO
1956 SkDebugf("rect: %d %d %d %d\n", clip.fLeft, clip.fTop, clip.fRight, clip.fBottom);
1957#endif
1958
1959 for (int i = 0; i < ops.numOps(); ++i) {
1960 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1961 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1962
1963 if (ops.offset(i) > info.fSaveLayerOpID && ops.offset(i) < info.fRestoreOpID) {
1964 pullForward[j] = true;
1965 }
1966 }
1967 }
1968
1969#ifdef SK_PRINT_PULL_FORWARD_INFO
1970 SkDebugf("Need SaveLayers: ");
1971 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1972 if (pullForward[i]) {
robertphillips@google.come930a072014-04-03 00:34:27 +00001973 const GrAtlasedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1974
1975 SkDebugf("%d (%d), ", i, layer->layerID());
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001976 }
1977 }
1978 SkDebugf("\n");
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001979#endif
1980
1981 return false;
1982}