blob: 4af16109a4b6df1481c65ce98306277f732ce5ca [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.org28fcae22014-04-11 17:15:40 +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();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000242 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000243 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
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000289 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000290 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
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000305 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000306 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 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660 SkPath path;
661 path.addRRect(rect);
662 this->drawPath(draw, path, paint, NULL, true);
663 return;
664 }
665
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000666 fContext->drawRRect(grPaint, rect, stroke);
667}
668
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000669void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
670 const SkRRect& inner, const SkPaint& paint) {
671 SkStrokeRec stroke(paint);
672 if (stroke.isFillStyle()) {
673
674 CHECK_FOR_ANNOTATION(paint);
675 CHECK_SHOULD_DRAW(draw, false);
676
677 GrPaint grPaint;
678 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
679 return;
680 }
681
682 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
683 fContext->drawDRRect(grPaint, outer, inner);
684 return;
685 }
686 }
687
688 SkPath path;
689 path.addRRect(outer);
690 path.addRRect(inner);
691 path.setFillType(SkPath::kEvenOdd_FillType);
692
693 this->drawPath(draw, path, paint, NULL, true);
694}
695
696
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000697/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000698
699void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
700 const SkPaint& paint) {
701 CHECK_FOR_ANNOTATION(paint);
702 CHECK_SHOULD_DRAW(draw, false);
703
704 bool usePath = false;
705 // some basic reasons we might need to call drawPath...
706 if (paint.getMaskFilter() || paint.getPathEffect()) {
707 usePath = true;
708 }
709
710 if (usePath) {
711 SkPath path;
712 path.addOval(oval);
713 this->drawPath(draw, path, paint, NULL, true);
714 return;
715 }
716
717 GrPaint grPaint;
718 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
719 return;
720 }
721 SkStrokeRec stroke(paint);
722
723 fContext->drawOval(grPaint, oval, stroke);
724}
725
726#include "SkMaskFilter.h"
727#include "SkBounder.h"
728
729///////////////////////////////////////////////////////////////////////////////
730
731// helpers for applying mask filters
732namespace {
733
734// Draw a mask using the supplied paint. Since the coverage/geometry
735// is already burnt into the mask this boils down to a rect draw.
736// Return true if the mask was successfully drawn.
737bool draw_mask(GrContext* context, const SkRect& maskRect,
738 GrPaint* grp, GrTexture* mask) {
739 GrContext::AutoMatrix am;
740 if (!am.setIdentity(context, grp)) {
741 return false;
742 }
743
744 SkMatrix matrix;
745 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
746 matrix.postIDiv(mask->width(), mask->height());
747
748 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
749 context->drawRect(*grp, maskRect);
750 return true;
751}
752
753bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
754 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
755 GrPaint* grp, SkPaint::Style style) {
756 SkMask srcM, dstM;
757
758 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
759 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
760 return false;
761 }
762 SkAutoMaskFreeImage autoSrc(srcM.fImage);
763
764 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
765 return false;
766 }
767 // this will free-up dstM when we're done (allocated in filterMask())
768 SkAutoMaskFreeImage autoDst(dstM.fImage);
769
770 if (clip.quickReject(dstM.fBounds)) {
771 return false;
772 }
773 if (bounder && !bounder->doIRect(dstM.fBounds)) {
774 return false;
775 }
776
777 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
778 // the current clip (and identity matrix) and GrPaint settings
779 GrTextureDesc desc;
780 desc.fWidth = dstM.fBounds.width();
781 desc.fHeight = dstM.fBounds.height();
782 desc.fConfig = kAlpha_8_GrPixelConfig;
783
784 GrAutoScratchTexture ast(context, desc);
785 GrTexture* texture = ast.texture();
786
787 if (NULL == texture) {
788 return false;
789 }
790 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
791 dstM.fImage, dstM.fRowBytes);
792
793 SkRect maskRect = SkRect::Make(dstM.fBounds);
794
795 return draw_mask(context, maskRect, grp, texture);
796}
797
798// Create a mask of 'devPath' and place the result in 'mask'. Return true on
799// success; false otherwise.
800bool create_mask_GPU(GrContext* context,
801 const SkRect& maskRect,
802 const SkPath& devPath,
803 const SkStrokeRec& stroke,
804 bool doAA,
805 GrAutoScratchTexture* mask) {
806 GrTextureDesc desc;
807 desc.fFlags = kRenderTarget_GrTextureFlagBit;
808 desc.fWidth = SkScalarCeilToInt(maskRect.width());
809 desc.fHeight = SkScalarCeilToInt(maskRect.height());
810 // We actually only need A8, but it often isn't supported as a
811 // render target so default to RGBA_8888
812 desc.fConfig = kRGBA_8888_GrPixelConfig;
813 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
814 desc.fConfig = kAlpha_8_GrPixelConfig;
815 }
816
817 mask->set(context, desc);
818 if (NULL == mask->texture()) {
819 return false;
820 }
821
822 GrTexture* maskTexture = mask->texture();
823 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
824
825 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
826 GrContext::AutoClip ac(context, clipRect);
827
828 context->clear(NULL, 0x0, true);
829
830 GrPaint tempPaint;
831 if (doAA) {
832 tempPaint.setAntiAlias(true);
833 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
834 // blend coeff of zero requires dual source blending support in order
835 // to properly blend partially covered pixels. This means the AA
836 // code path may not be taken. So we use a dst blend coeff of ISA. We
837 // could special case AA draws to a dst surface with known alpha=0 to
838 // use a zero dst coeff when dual source blending isn't available.
839 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
840 }
841
842 GrContext::AutoMatrix am;
843
844 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
845 SkMatrix translate;
846 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
847 am.set(context, translate);
848 context->drawPath(tempPaint, devPath, stroke);
849 return true;
850}
851
852SkBitmap wrap_texture(GrTexture* texture) {
reed@google.combf790232013-12-13 19:45:58 +0000853 SkImageInfo info;
854 texture->asImageInfo(&info);
855
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000856 SkBitmap result;
reed@google.combf790232013-12-13 19:45:58 +0000857 result.setConfig(info);
858 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000859 return result;
860}
861
862};
863
864void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
865 const SkPaint& paint, const SkMatrix* prePathMatrix,
866 bool pathIsMutable) {
867 CHECK_FOR_ANNOTATION(paint);
868 CHECK_SHOULD_DRAW(draw, false);
869
870 GrPaint grPaint;
871 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
872 return;
873 }
874
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000875 // If we have a prematrix, apply it to the path, optimizing for the case
876 // where the original path can in fact be modified in place (even though
877 // its parameter type is const).
878 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000879 SkTLazy<SkPath> tmpPath;
880 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000881
882 if (prePathMatrix) {
883 SkPath* result = pathPtr;
884
885 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000886 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000887 pathIsMutable = true;
888 }
889 // should I push prePathMatrix on our MV stack temporarily, instead
890 // of applying it here? See SkDraw.cpp
891 pathPtr->transform(*prePathMatrix, result);
892 pathPtr = result;
893 }
894 // at this point we're done with prePathMatrix
895 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
896
897 SkStrokeRec stroke(paint);
898 SkPathEffect* pathEffect = paint.getPathEffect();
899 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000900 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000901 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000902 pathPtr = effectPath.get();
903 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000904 }
905
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000906 if (paint.getMaskFilter()) {
907 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000908 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
909 if (stroke.applyToPath(strokedPath, *pathPtr)) {
910 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000911 pathIsMutable = true;
912 stroke.setFillStyle();
913 }
914 }
915
916 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000917 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000918
919 // transform the path into device space
920 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000921
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000922 SkRect maskRect;
923 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
924 draw.fClip->getBounds(),
925 fContext->getMatrix(),
926 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000927 // The context's matrix may change while creating the mask, so save the CTM here to
928 // pass to filterMaskGPU.
929 const SkMatrix ctm = fContext->getMatrix();
930
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000931 SkIRect finalIRect;
932 maskRect.roundOut(&finalIRect);
933 if (draw.fClip->quickReject(finalIRect)) {
934 // clipped out
935 return;
936 }
937 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
938 // nothing to draw
939 return;
940 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000941
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000942 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000943 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000944 // the mask filter was able to draw itself directly, so there's nothing
945 // left to do.
946 return;
947 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000948
949 GrAutoScratchTexture mask;
950
951 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
952 grPaint.isAntiAlias(), &mask)) {
953 GrTexture* filtered;
954
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000955 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000956 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000957 // filterMaskGPU gives us ownership of a ref to the result
958 SkAutoTUnref<GrTexture> atu(filtered);
959
960 // If the scratch texture that we used as the filter src also holds the filter
961 // result then we must detach so that this texture isn't recycled for a later
962 // draw.
963 if (filtered == mask.texture()) {
964 mask.detach();
965 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
966 }
967
968 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
969 // This path is completely drawn
970 return;
971 }
972 }
973 }
974 }
975
976 // draw the mask on the CPU - this is a fallthrough path in case the
977 // GPU path fails
978 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
979 SkPaint::kFill_Style;
980 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
981 *draw.fClip, draw.fBounder, &grPaint, style);
982 return;
983 }
984
985 fContext->drawPath(grPaint, *pathPtr, stroke);
986}
987
988static const int kBmpSmallTileSize = 1 << 10;
989
990static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
991 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
992 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
993 return tilesX * tilesY;
994}
995
996static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
997 if (maxTileSize <= kBmpSmallTileSize) {
998 return maxTileSize;
999 }
1000
1001 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
1002 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
1003
1004 maxTileTotalTileSize *= maxTileSize * maxTileSize;
1005 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
1006
1007 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
1008 return kBmpSmallTileSize;
1009 } else {
1010 return maxTileSize;
1011 }
1012}
1013
1014// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
1015// pixels from the bitmap are necessary.
1016static void determine_clipped_src_rect(const GrContext* context,
1017 const SkBitmap& bitmap,
1018 const SkRect* srcRectPtr,
1019 SkIRect* clippedSrcIRect) {
1020 const GrClipData* clip = context->getClip();
1021 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
1022 SkMatrix inv;
1023 if (!context->getMatrix().invert(&inv)) {
1024 clippedSrcIRect->setEmpty();
1025 return;
1026 }
1027 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
1028 inv.mapRect(&clippedSrcRect);
1029 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001030 // we've setup src space 0,0 to map to the top left of the src rect.
1031 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001032 if (!clippedSrcRect.intersect(*srcRectPtr)) {
1033 clippedSrcIRect->setEmpty();
1034 return;
1035 }
1036 }
1037 clippedSrcRect.roundOut(clippedSrcIRect);
1038 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1039 if (!clippedSrcIRect->intersect(bmpBounds)) {
1040 clippedSrcIRect->setEmpty();
1041 }
1042}
1043
1044bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1045 const GrTextureParams& params,
1046 const SkRect* srcRectPtr,
1047 int maxTileSize,
1048 int* tileSize,
1049 SkIRect* clippedSrcRect) const {
1050 // if bitmap is explictly texture backed then just use the texture
1051 if (NULL != bitmap.getTexture()) {
1052 return false;
1053 }
1054
1055 // if it's larger than the max tile size, then we have no choice but tiling.
1056 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
1057 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1058 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
1059 return true;
1060 }
1061
1062 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
1063 return false;
1064 }
1065
1066 // if the entire texture is already in our cache then no reason to tile it
1067 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
1068 return false;
1069 }
1070
1071 // At this point we know we could do the draw by uploading the entire bitmap
1072 // as a texture. However, if the texture would be large compared to the
1073 // cache size and we don't require most of it for this draw then tile to
1074 // reduce the amount of upload and cache spill.
1075
1076 // assumption here is that sw bitmap size is a good proxy for its size as
1077 // a texture
1078 size_t bmpSize = bitmap.getSize();
1079 size_t cacheSize;
1080 fContext->getTextureCacheLimits(NULL, &cacheSize);
1081 if (bmpSize < cacheSize / 2) {
1082 return false;
1083 }
1084
1085 // Figure out how much of the src we will need based on the src rect and clipping.
1086 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1087 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1088 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
1089 kBmpSmallTileSize * kBmpSmallTileSize;
1090
1091 return usedTileBytes < 2 * bmpSize;
1092}
1093
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001094void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001095 const SkBitmap& bitmap,
1096 const SkMatrix& m,
1097 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001098 SkMatrix concat;
1099 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1100 if (!m.isIdentity()) {
1101 concat.setConcat(*draw->fMatrix, m);
1102 draw.writable()->fMatrix = &concat;
1103 }
1104 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001105}
1106
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001107// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001108// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1109// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001110static inline void clamped_outset_with_offset(SkIRect* iRect,
1111 int outset,
1112 SkPoint* offset,
1113 const SkIRect& clamp) {
1114 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001115
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001116 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1117 if (leftClampDelta > 0) {
1118 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001119 iRect->fLeft = clamp.fLeft;
1120 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001121 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001122 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001123
1124 int topClampDelta = clamp.fTop - iRect->fTop;
1125 if (topClampDelta > 0) {
1126 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001127 iRect->fTop = clamp.fTop;
1128 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001129 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001130 }
1131
1132 if (iRect->fRight > clamp.fRight) {
1133 iRect->fRight = clamp.fRight;
1134 }
1135 if (iRect->fBottom > clamp.fBottom) {
1136 iRect->fBottom = clamp.fBottom;
1137 }
1138}
1139
1140void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1141 const SkBitmap& bitmap,
1142 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001143 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001144 const SkPaint& paint,
1145 SkCanvas::DrawBitmapRectFlags flags) {
1146 CHECK_SHOULD_DRAW(draw, false);
1147
1148 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001149 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001150 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1151 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001152 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001153 SkScalar w = SkIntToScalar(bitmap.width());
1154 SkScalar h = SkIntToScalar(bitmap.height());
1155 dstSize.fWidth = w;
1156 dstSize.fHeight = h;
1157 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001158 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001159 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001160 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001161 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001162 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001163 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1164 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1165 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1166 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167 }
1168
1169 if (paint.getMaskFilter()){
1170 // Convert the bitmap to a shader so that the rect can be drawn
1171 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001172 SkBitmap tmp; // subset of bitmap, if necessary
1173 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001174 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001175 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001176 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1177 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1178 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001179 // In bleed mode we position and trim the bitmap based on the src rect which is
1180 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1181 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1182 // compensate.
1183 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1184 SkIRect iSrc;
1185 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001186
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001187 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1188 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001189
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001190 if (!bitmap.extractSubset(&tmp, iSrc)) {
1191 return; // extraction failed
1192 }
1193 bitmapPtr = &tmp;
1194 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001195
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001196 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001197 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001198 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001199 } else {
1200 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001201 }
1202
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001203 SkPaint paintWithShader(paint);
1204 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001205 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001206 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1207 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001208
1209 return;
1210 }
1211
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001212 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1213 // the view matrix rather than a local matrix.
1214 SkMatrix m;
1215 m.setScale(dstSize.fWidth / srcRect.width(),
1216 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001217 fContext->concatMatrix(m);
1218
1219 GrTextureParams params;
1220 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1221 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001222
1223 int tileFilterPad;
1224 bool doBicubic = false;
1225
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001226 switch(paintFilterLevel) {
1227 case SkPaint::kNone_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001228 tileFilterPad = 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001229 textureFilterMode = GrTextureParams::kNone_FilterMode;
1230 break;
1231 case SkPaint::kLow_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001232 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001233 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1234 break;
1235 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001236 tileFilterPad = 1;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001237 if (fContext->getMatrix().getMinStretch() < SK_Scalar1) {
1238 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1239 } else {
1240 // Don't trigger MIP level generation unnecessarily.
1241 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1242 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001243 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001244 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001245 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001246 if (fContext->getMatrix().getMinStretch() >= SK_Scalar1) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001247 // We will install an effect that does the filtering in the shader.
1248 textureFilterMode = GrTextureParams::kNone_FilterMode;
1249 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1250 doBicubic = true;
1251 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001252 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1253 tileFilterPad = 1;
1254 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001255 break;
1256 default:
1257 SkErrorInternals::SetError( kInvalidPaint_SkError,
1258 "Sorry, I don't understand the filtering "
1259 "mode you asked for. Falling back to "
1260 "MIPMaps.");
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001261 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001262 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1263 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001264 }
1265
1266 params.setFilterMode(textureFilterMode);
1267
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001268 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001269 int tileSize;
1270
1271 SkIRect clippedSrcRect;
1272 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1273 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001274 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1275 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001276 } else {
1277 // take the simple case
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001278 this->internalDrawBitmap(bitmap, srcRect, params, paint, flags, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001279 }
1280}
1281
1282// Break 'bitmap' into several tiles to draw it since it has already
1283// been determined to be too large to fit in VRAM
1284void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1285 const SkRect& srcRect,
1286 const SkIRect& clippedSrcIRect,
1287 const GrTextureParams& params,
1288 const SkPaint& paint,
1289 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001290 int tileSize,
1291 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001292 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1293
1294 int nx = bitmap.width() / tileSize;
1295 int ny = bitmap.height() / tileSize;
1296 for (int x = 0; x <= nx; x++) {
1297 for (int y = 0; y <= ny; y++) {
1298 SkRect tileR;
1299 tileR.set(SkIntToScalar(x * tileSize),
1300 SkIntToScalar(y * tileSize),
1301 SkIntToScalar((x + 1) * tileSize),
1302 SkIntToScalar((y + 1) * tileSize));
1303
1304 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1305 continue;
1306 }
1307
1308 if (!tileR.intersect(srcRect)) {
1309 continue;
1310 }
1311
1312 SkBitmap tmpB;
1313 SkIRect iTileR;
1314 tileR.roundOut(&iTileR);
1315 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1316 SkIntToScalar(iTileR.fTop));
1317
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001318 // Adjust the context matrix to draw at the right x,y in device space
1319 SkMatrix tmpM;
1320 GrContext::AutoMatrix am;
1321 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1322 am.setPreConcat(fContext, tmpM);
1323
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001324 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001325 SkIRect iClampRect;
1326
1327 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1328 // In bleed mode we want to always expand the tile on all edges
1329 // but stay within the bitmap bounds
1330 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1331 } else {
1332 // In texture-domain/clamp mode we only want to expand the
1333 // tile on edges interior to "srcRect" (i.e., we want to
1334 // not bleed across the original clamped edges)
1335 srcRect.roundOut(&iClampRect);
1336 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001337 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1338 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001339 }
1340
1341 if (bitmap.extractSubset(&tmpB, iTileR)) {
1342 // now offset it to make it "local" to our tmp bitmap
1343 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001344
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001345 this->internalDrawBitmap(tmpB, tileR, params, paint, flags, bicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001346 }
1347 }
1348 }
1349}
1350
1351static bool has_aligned_samples(const SkRect& srcRect,
1352 const SkRect& transformedRect) {
1353 // detect pixel disalignment
1354 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1355 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1356 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1357 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1358 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1359 COLOR_BLEED_TOLERANCE &&
1360 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1361 COLOR_BLEED_TOLERANCE) {
1362 return true;
1363 }
1364 return false;
1365}
1366
1367static bool may_color_bleed(const SkRect& srcRect,
1368 const SkRect& transformedRect,
1369 const SkMatrix& m) {
1370 // Only gets called if has_aligned_samples returned false.
1371 // So we can assume that sampling is axis aligned but not texel aligned.
1372 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1373 SkRect innerSrcRect(srcRect), innerTransformedRect,
1374 outerTransformedRect(transformedRect);
1375 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1376 m.mapRect(&innerTransformedRect, innerSrcRect);
1377
1378 // The gap between outerTransformedRect and innerTransformedRect
1379 // represents the projection of the source border area, which is
1380 // problematic for color bleeding. We must check whether any
1381 // destination pixels sample the border area.
1382 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1383 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1384 SkIRect outer, inner;
1385 outerTransformedRect.round(&outer);
1386 innerTransformedRect.round(&inner);
1387 // If the inner and outer rects round to the same result, it means the
1388 // border does not overlap any pixel centers. Yay!
1389 return inner != outer;
1390}
1391
1392
1393/*
1394 * This is called by drawBitmap(), which has to handle images that may be too
1395 * large to be represented by a single texture.
1396 *
1397 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1398 * and that non-texture portion of the GrPaint has already been setup.
1399 */
1400void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1401 const SkRect& srcRect,
1402 const GrTextureParams& params,
1403 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001404 SkCanvas::DrawBitmapRectFlags flags,
1405 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001406 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1407 bitmap.height() <= fContext->getMaxTextureSize());
1408
1409 GrTexture* texture;
1410 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1411 if (NULL == texture) {
1412 return;
1413 }
1414
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001415 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001416 SkRect paintRect;
1417 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1418 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1419 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1420 SkScalarMul(srcRect.fTop, hInv),
1421 SkScalarMul(srcRect.fRight, wInv),
1422 SkScalarMul(srcRect.fBottom, hInv));
1423
1424 bool needsTextureDomain = false;
1425 if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) &&
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001426 (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode)) {
1427 // Need texture domain if drawing a sub rect
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001428 needsTextureDomain = srcRect.width() < bitmap.width() ||
1429 srcRect.height() < bitmap.height();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001430 if (!bicubic && needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001431 const SkMatrix& matrix = fContext->getMatrix();
1432 // sampling is axis-aligned
1433 SkRect transformedRect;
1434 matrix.mapRect(&transformedRect, srcRect);
1435
1436 if (has_aligned_samples(srcRect, transformedRect)) {
1437 // We could also turn off filtering here (but we already did a cache lookup with
1438 // params).
1439 needsTextureDomain = false;
1440 } else {
1441 needsTextureDomain = may_color_bleed(srcRect, transformedRect, matrix);
1442 }
1443 }
1444 }
1445
1446 SkRect textureDomain = SkRect::MakeEmpty();
1447 SkAutoTUnref<GrEffectRef> effect;
1448 if (needsTextureDomain) {
1449 // Use a constrained texture domain to avoid color bleeding
1450 SkScalar left, top, right, bottom;
1451 if (srcRect.width() > SK_Scalar1) {
1452 SkScalar border = SK_ScalarHalf / texture->width();
1453 left = paintRect.left() + border;
1454 right = paintRect.right() - border;
1455 } else {
1456 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1457 }
1458 if (srcRect.height() > SK_Scalar1) {
1459 SkScalar border = SK_ScalarHalf / texture->height();
1460 top = paintRect.top() + border;
1461 bottom = paintRect.bottom() - border;
1462 } else {
1463 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1464 }
1465 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001466 if (bicubic) {
1467 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1468 } else {
1469 effect.reset(GrTextureDomainEffect::Create(texture,
1470 SkMatrix::I(),
1471 textureDomain,
1472 GrTextureDomain::kClamp_Mode,
1473 params.filterMode()));
1474 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001475 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001476 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1477 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1478 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001479 } else {
1480 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1481 }
1482
1483 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1484 // the rest from the SkPaint.
1485 GrPaint grPaint;
1486 grPaint.addColorEffect(effect);
1487 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1488 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1489 return;
1490 }
1491
1492 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1493}
1494
1495static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001496 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001497 int w, int h, const SkImageFilter::Context& ctx,
1498 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001499 SkASSERT(filter);
1500 SkDeviceImageFilterProxy proxy(device);
1501
1502 if (filter->canFilterImageGPU()) {
1503 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1504 // filter. Also set the clip wide open and the matrix to identity.
1505 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001506 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001507 } else {
1508 return false;
1509 }
1510}
1511
1512void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1513 int left, int top, const SkPaint& paint) {
1514 // drawSprite is defined to be in device coords.
1515 CHECK_SHOULD_DRAW(draw, true);
1516
1517 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1518 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1519 return;
1520 }
1521
1522 int w = bitmap.width();
1523 int h = bitmap.height();
1524
1525 GrTexture* texture;
1526 // draw sprite uses the default texture params
1527 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1528
1529 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001530 // This bitmap will own the filtered result as a texture.
1531 SkBitmap filteredBitmap;
1532
1533 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001534 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001535 SkMatrix matrix(*draw.fMatrix);
1536 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001537 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001538 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1539 SkAutoUnref aur(cache);
1540 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001541 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001542 &offset)) {
1543 texture = (GrTexture*) filteredBitmap.getTexture();
1544 w = filteredBitmap.width();
1545 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001546 left += offset.x();
1547 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001548 } else {
1549 return;
1550 }
1551 }
1552
1553 GrPaint grPaint;
1554 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1555
1556 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1557 return;
1558 }
1559
1560 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001561 SkRect::MakeXYWH(SkIntToScalar(left),
1562 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001563 SkIntToScalar(w),
1564 SkIntToScalar(h)),
1565 SkRect::MakeXYWH(0,
1566 0,
1567 SK_Scalar1 * w / texture->width(),
1568 SK_Scalar1 * h / texture->height()));
1569}
1570
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001571void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001572 const SkRect* src, const SkRect& dst,
1573 const SkPaint& paint,
1574 SkCanvas::DrawBitmapRectFlags flags) {
1575 SkMatrix matrix;
1576 SkRect bitmapBounds, tmpSrc;
1577
1578 bitmapBounds.set(0, 0,
1579 SkIntToScalar(bitmap.width()),
1580 SkIntToScalar(bitmap.height()));
1581
1582 // Compute matrix from the two rectangles
1583 if (NULL != src) {
1584 tmpSrc = *src;
1585 } else {
1586 tmpSrc = bitmapBounds;
1587 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001588
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001589 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1590
1591 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1592 if (NULL != src) {
1593 if (!bitmapBounds.contains(tmpSrc)) {
1594 if (!tmpSrc.intersect(bitmapBounds)) {
1595 return; // nothing to draw
1596 }
1597 }
1598 }
1599
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001600 SkRect tmpDst;
1601 matrix.mapRect(&tmpDst, tmpSrc);
1602
1603 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1604 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1605 // Translate so that tempDst's top left is at the origin.
1606 matrix = *origDraw.fMatrix;
1607 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1608 draw.writable()->fMatrix = &matrix;
1609 }
1610 SkSize dstSize;
1611 dstSize.fWidth = tmpDst.width();
1612 dstSize.fHeight = tmpDst.height();
1613
1614 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001615}
1616
1617void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1618 int x, int y, const SkPaint& paint) {
1619 // clear of the source device must occur before CHECK_SHOULD_DRAW
1620 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1621 if (dev->fNeedClear) {
1622 // TODO: could check here whether we really need to draw at all
1623 dev->clear(0x0);
1624 }
1625
1626 // drawDevice is defined to be in device coords.
1627 CHECK_SHOULD_DRAW(draw, true);
1628
1629 GrRenderTarget* devRT = dev->accessRenderTarget();
1630 GrTexture* devTex;
1631 if (NULL == (devTex = devRT->asTexture())) {
1632 return;
1633 }
1634
1635 const SkBitmap& bm = dev->accessBitmap(false);
1636 int w = bm.width();
1637 int h = bm.height();
1638
1639 SkImageFilter* filter = paint.getImageFilter();
1640 // This bitmap will own the filtered result as a texture.
1641 SkBitmap filteredBitmap;
1642
1643 if (NULL != filter) {
1644 SkIPoint offset = SkIPoint::Make(0, 0);
1645 SkMatrix matrix(*draw.fMatrix);
1646 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001647 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001648 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1649 SkAutoUnref aur(cache);
1650 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001651 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001652 &offset)) {
1653 devTex = filteredBitmap.getTexture();
1654 w = filteredBitmap.width();
1655 h = filteredBitmap.height();
1656 x += offset.fX;
1657 y += offset.fY;
1658 } else {
1659 return;
1660 }
1661 }
1662
1663 GrPaint grPaint;
1664 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1665
1666 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1667 return;
1668 }
1669
1670 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1671 SkIntToScalar(y),
1672 SkIntToScalar(w),
1673 SkIntToScalar(h));
1674
1675 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1676 // scratch texture).
1677 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1678 SK_Scalar1 * h / devTex->height());
1679
1680 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1681}
1682
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001683bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001684 return filter->canFilterImageGPU();
1685}
1686
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001687bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001688 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001689 SkBitmap* result, SkIPoint* offset) {
1690 // want explicitly our impl, so guard against a subclass of us overriding it
1691 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1692 return false;
1693 }
1694
1695 SkAutoLockPixels alp(src, !src.getTexture());
1696 if (!src.getTexture() && !src.readyToDraw()) {
1697 return false;
1698 }
1699
1700 GrTexture* texture;
1701 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1702 // must be pushed upstack.
1703 SkAutoCachedTexture act(this, src, NULL, &texture);
1704
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001705 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1706 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001707}
1708
1709///////////////////////////////////////////////////////////////////////////////
1710
1711// must be in SkCanvas::VertexMode order
1712static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1713 kTriangles_GrPrimitiveType,
1714 kTriangleStrip_GrPrimitiveType,
1715 kTriangleFan_GrPrimitiveType,
1716};
1717
1718void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1719 int vertexCount, const SkPoint vertices[],
1720 const SkPoint texs[], const SkColor colors[],
1721 SkXfermode* xmode,
1722 const uint16_t indices[], int indexCount,
1723 const SkPaint& paint) {
1724 CHECK_SHOULD_DRAW(draw, false);
1725
1726 GrPaint grPaint;
1727 // we ignore the shader if texs is null.
1728 if (NULL == texs) {
1729 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
1730 return;
1731 }
1732 } else {
1733 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
1734 return;
1735 }
1736 }
1737
1738 if (NULL != xmode && NULL != texs && NULL != colors) {
1739 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1740 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1741#if 0
1742 return
1743#endif
1744 }
1745 }
1746
1747 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1748 if (NULL != colors) {
1749 // need to convert byte order and from non-PM to PM
1750 convertedColors.reset(vertexCount);
1751 for (int i = 0; i < vertexCount; ++i) {
1752 convertedColors[i] = SkColor2GrColor(colors[i]);
1753 }
1754 colors = convertedColors.get();
1755 }
1756 fContext->drawVertices(grPaint,
1757 gVertexMode2PrimitiveType[vmode],
1758 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001759 vertices,
1760 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001761 colors,
1762 indices,
1763 indexCount);
1764}
1765
1766///////////////////////////////////////////////////////////////////////////////
1767
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001768void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1769 size_t byteLength, SkScalar x, SkScalar y,
1770 const SkPaint& paint) {
1771 CHECK_SHOULD_DRAW(draw, false);
1772
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001773 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001774 GrPaint grPaint;
1775 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1776 return;
1777 }
1778
1779 SkDEBUGCODE(this->validate();)
1780
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001781 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1782 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001783 GrPaint grPaint;
1784 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1785 return;
1786 }
1787
1788 SkDEBUGCODE(this->validate();)
1789
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001790 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001791 } else {
1792 // this guy will just call our drawPath()
1793 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001794 }
1795}
1796
1797void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1798 size_t byteLength, const SkScalar pos[],
1799 SkScalar constY, int scalarsPerPos,
1800 const SkPaint& paint) {
1801 CHECK_SHOULD_DRAW(draw, false);
1802
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001803 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001804 GrPaint grPaint;
1805 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1806 return;
1807 }
1808
1809 SkDEBUGCODE(this->validate();)
1810
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001811 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001812 constY, scalarsPerPos);
1813 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001814 GrPaint grPaint;
1815 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1816 return;
1817 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001818
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001819 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001820
1821 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001822 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001823 } else {
1824 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1825 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001826 }
1827}
1828
1829void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1830 size_t len, const SkPath& path,
1831 const SkMatrix* m, const SkPaint& paint) {
1832 CHECK_SHOULD_DRAW(draw, false);
1833
1834 SkASSERT(draw.fDevice == this);
1835 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1836}
1837
1838///////////////////////////////////////////////////////////////////////////////
1839
1840bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1841 if (!paint.isLCDRenderText()) {
1842 // we're cool with the paint as is
1843 return false;
1844 }
1845
1846 if (paint.getShader() ||
1847 paint.getXfermode() || // unless its srcover
1848 paint.getMaskFilter() ||
1849 paint.getRasterizer() ||
1850 paint.getColorFilter() ||
1851 paint.getPathEffect() ||
1852 paint.isFakeBoldText() ||
1853 paint.getStyle() != SkPaint::kFill_Style) {
1854 // turn off lcd
1855 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1856 flags->fHinting = paint.getHinting();
1857 return true;
1858 }
1859 // we're cool with the paint as is
1860 return false;
1861}
1862
1863void SkGpuDevice::flush() {
1864 DO_DEFERRED_CLEAR();
1865 fContext->resolveRenderTarget(fRenderTarget);
1866}
1867
1868///////////////////////////////////////////////////////////////////////////////
1869
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001870SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001871 GrTextureDesc desc;
1872 desc.fConfig = fRenderTarget->config();
1873 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001874 desc.fWidth = info.width();
1875 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001876 desc.fSampleCnt = fRenderTarget->numSamples();
1877
1878 SkAutoTUnref<GrTexture> texture;
1879 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001880 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001881
1882#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1883 // layers are never draw in repeat modes, so we can request an approx
1884 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001885 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001886 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1887 GrContext::kApprox_ScratchTexMatch :
1888 GrContext::kExact_ScratchTexMatch;
1889 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1890#else
1891 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1892#endif
1893 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001894 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001895 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001896 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1897 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001898 return NULL;
1899 }
1900}
1901
reed@google.com76f10a32014-02-05 15:32:21 +00001902SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1903 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1904}
1905
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001906// In the future this may not be a static method if we need to incorporate the
1907// clip and matrix state into the key
1908SkPicture::AccelData::Key SkGpuDevice::ComputeAccelDataKey() {
1909 static const SkPicture::AccelData::Key gGPUID = SkPicture::AccelData::GenerateDomain();
1910
1911 return gGPUID;
1912}
1913
1914void SkGpuDevice::EXPERIMENTAL_optimize(SkPicture* picture) {
1915 SkPicture::AccelData::Key key = ComputeAccelDataKey();
1916
1917 GPUAccelData* data = SkNEW_ARGS(GPUAccelData, (key));
1918
1919 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001920
1921 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001922}
1923
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001924void SkGpuDevice::EXPERIMENTAL_purge(SkPicture* picture) {
1925
1926}
1927
1928bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001929
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001930 SkPicture::AccelData::Key key = ComputeAccelDataKey();
1931
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001932 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001933 if (NULL == data) {
1934 return false;
1935 }
1936
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001937 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001938
1939//#define SK_PRINT_PULL_FORWARD_INFO 1
1940
1941#ifdef SK_PRINT_PULL_FORWARD_INFO
1942 static bool gPrintedAccelData = false;
1943
1944 if (!gPrintedAccelData) {
1945 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1946 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1947
skia.committer@gmail.com2c48ee82014-04-01 03:07:47 +00001948 SkDebugf("%d: Width: %d Height: %d SL: %d R: %d hasNestedLayers: %s\n",
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001949 i,
skia.committer@gmail.com2c48ee82014-04-01 03:07:47 +00001950 info.fSize.fWidth,
1951 info.fSize.fHeight,
1952 info.fSaveLayerOpID,
1953 info.fRestoreOpID,
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001954 info.fHasNestedLayers ? "T" : "F");
1955 }
1956 gPrintedAccelData = true;
1957 }
1958#endif
1959
1960 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1961 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1962 pullForward[i] = false;
1963 }
1964
1965 SkIRect clip;
1966
1967 fClipData.getConservativeBounds(this->width(), this->height(), &clip, NULL);
1968
1969 SkMatrix inv;
1970 if (!fContext->getMatrix().invert(&inv)) {
1971 return false;
1972 }
1973
1974 SkRect r = SkRect::Make(clip);
1975 inv.mapRect(&r);
1976 r.roundOut(&clip);
1977
1978 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(clip);
1979
1980#ifdef SK_PRINT_PULL_FORWARD_INFO
1981 SkDebugf("rect: %d %d %d %d\n", clip.fLeft, clip.fTop, clip.fRight, clip.fBottom);
1982#endif
1983
1984 for (int i = 0; i < ops.numOps(); ++i) {
1985 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1986 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1987
1988 if (ops.offset(i) > info.fSaveLayerOpID && ops.offset(i) < info.fRestoreOpID) {
1989 pullForward[j] = true;
1990 }
1991 }
1992 }
1993
1994#ifdef SK_PRINT_PULL_FORWARD_INFO
1995 SkDebugf("Need SaveLayers: ");
1996 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1997 if (pullForward[i]) {
commit-bot@chromium.org365cd312014-04-11 15:53:47 +00001998 const GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
robertphillips@google.come930a072014-04-03 00:34:27 +00001999
2000 SkDebugf("%d (%d), ", i, layer->layerID());
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00002001 }
2002 }
2003 SkDebugf("\n");
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002004#endif
2005
2006 return false;
2007}