blob: 3119a9e7e37f2d3711bf5f46573566d723e7b73d [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.org6fcd1ef2014-05-02 12:39:41 +0000193 bool useDFFonts = !!(flags & kDFFonts_Flag);
194 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties,
195 useDFFonts));
commit-bot@chromium.org47841822014-03-27 14:19:17 +0000196 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
197
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000198 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000199 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000200
201 SkASSERT(NULL != renderTarget);
202 fRenderTarget = renderTarget;
203 fRenderTarget->ref();
204
205 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
206 // on the RT but not vice-versa.
207 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
208 // busting chrome (for a currently unknown reason).
209 GrSurface* surface = fRenderTarget->asTexture();
210 if (NULL == surface) {
211 surface = fRenderTarget;
212 }
reed@google.combf790232013-12-13 19:45:58 +0000213
214 SkImageInfo info;
215 surface->asImageInfo(&info);
bungeman@google.coma95a0662014-03-19 23:26:50 +0000216 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, SkToBool(flags & kCached_Flag)));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000217
reed@google.com672588b2014-01-08 15:42:01 +0000218 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000219}
220
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000221SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
222 int sampleCount) {
223 if (kUnknown_SkColorType == origInfo.colorType() ||
224 origInfo.width() < 0 || origInfo.height() < 0) {
225 return NULL;
226 }
227
228 SkImageInfo info = origInfo;
229 // TODO: perhas we can loosen this check now that colortype is more detailed
230 // e.g. can we support both RGBA and BGRA here?
231 if (kRGB_565_SkColorType == info.colorType()) {
232 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
233 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000234 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000235 if (kOpaque_SkAlphaType != info.alphaType()) {
236 info.fAlphaType = kPremul_SkAlphaType; // force this setting
237 }
238 }
239
240 GrTextureDesc desc;
241 desc.fFlags = kRenderTarget_GrTextureFlagBit;
242 desc.fWidth = info.width();
243 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000244 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000245 desc.fSampleCnt = sampleCount;
246
247 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
248 if (!texture.get()) {
249 return NULL;
250 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000251
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000252 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
253}
254
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000255SkGpuDevice::~SkGpuDevice() {
256 if (fDrawProcs) {
257 delete fDrawProcs;
258 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000259
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000260 delete fMainTextContext;
261 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000262
263 // The GrContext takes a ref on the target. We don't want to cause the render
264 // target to be unnecessarily kept alive.
265 if (fContext->getRenderTarget() == fRenderTarget) {
266 fContext->setRenderTarget(NULL);
267 }
268
269 if (fContext->getClip() == &fClipData) {
270 fContext->setClip(NULL);
271 }
272
273 SkSafeUnref(fRenderTarget);
274 fContext->unref();
275}
276
277///////////////////////////////////////////////////////////////////////////////
278
279void SkGpuDevice::makeRenderTargetCurrent() {
280 DO_DEFERRED_CLEAR();
281 fContext->setRenderTarget(fRenderTarget);
282}
283
284///////////////////////////////////////////////////////////////////////////////
285
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000286bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
287 int x, int y) {
288 DO_DEFERRED_CLEAR();
289
290 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000291 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000292 if (kUnknown_GrPixelConfig == config) {
293 return false;
294 }
295
296 uint32_t flags = 0;
297 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
298 flags = GrContext::kUnpremul_PixelOpsFlag;
299 }
300 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
301 config, dstPixels, dstRowBytes, flags);
302}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000303
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000304bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
305 int x, int y) {
306 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000307 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000308 if (kUnknown_GrPixelConfig == config) {
309 return false;
310 }
311 uint32_t flags = 0;
312 if (kUnpremul_SkAlphaType == info.alphaType()) {
313 flags = GrContext::kUnpremul_PixelOpsFlag;
314 }
315 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
316
317 // need to bump our genID for compatibility with clients that "know" we have a bitmap
318 this->onAccessBitmap().notifyPixelsChanged();
319
320 return true;
321}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000322
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000323const SkBitmap& SkGpuDevice::onAccessBitmap() {
324 DO_DEFERRED_CLEAR();
325 return INHERITED::onAccessBitmap();
326}
327
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000328void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
329 INHERITED::onAttachToCanvas(canvas);
330
331 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
332 fClipData.fClipStack = canvas->getClipStack();
333}
334
335void SkGpuDevice::onDetachFromCanvas() {
336 INHERITED::onDetachFromCanvas();
337 fClipData.fClipStack = NULL;
338}
339
340// call this every draw call, to ensure that the context reflects our state,
341// and not the state from some other canvas/device
342void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
343 SkASSERT(NULL != fClipData.fClipStack);
344
345 fContext->setRenderTarget(fRenderTarget);
346
347 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
348
349 if (forceIdentity) {
350 fContext->setIdentityMatrix();
351 } else {
352 fContext->setMatrix(*draw.fMatrix);
353 }
354 fClipData.fOrigin = this->getOrigin();
355
356 fContext->setClip(&fClipData);
357
358 DO_DEFERRED_CLEAR();
359}
360
361GrRenderTarget* SkGpuDevice::accessRenderTarget() {
362 DO_DEFERRED_CLEAR();
363 return fRenderTarget;
364}
365
366///////////////////////////////////////////////////////////////////////////////
367
368SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
369SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
370SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
371SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
372SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
373 shader_type_mismatch);
374SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
375 shader_type_mismatch);
376SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
377SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
378
379namespace {
380
381// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
382// justAlpha indicates that skPaint's alpha should be used rather than the color
383// Callers may subsequently modify the GrPaint. Setting constantColor indicates
384// that the final paint will draw the same color at every pixel. This allows
385// an optimization where the the color filter can be applied to the skPaint's
386// color once while converting to GrPaint and then ignored.
387inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
388 const SkPaint& skPaint,
389 bool justAlpha,
390 bool constantColor,
391 GrPaint* grPaint) {
392
393 grPaint->setDither(skPaint.isDither());
394 grPaint->setAntiAlias(skPaint.isAntiAlias());
395
396 SkXfermode::Coeff sm;
397 SkXfermode::Coeff dm;
398
399 SkXfermode* mode = skPaint.getXfermode();
400 GrEffectRef* xferEffect = NULL;
401 if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
402 if (NULL != xferEffect) {
403 grPaint->addColorEffect(xferEffect)->unref();
404 sm = SkXfermode::kOne_Coeff;
405 dm = SkXfermode::kZero_Coeff;
406 }
407 } else {
408 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
409#if 0
410 return false;
411#else
412 // Fall back to src-over
413 sm = SkXfermode::kOne_Coeff;
414 dm = SkXfermode::kISA_Coeff;
415#endif
416 }
417 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
418
419 if (justAlpha) {
420 uint8_t alpha = skPaint.getAlpha();
421 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
422 // justAlpha is currently set to true only if there is a texture,
423 // so constantColor should not also be true.
424 SkASSERT(!constantColor);
425 } else {
426 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
427 }
428
429 SkColorFilter* colorFilter = skPaint.getColorFilter();
430 if (NULL != colorFilter) {
431 // if the source color is a constant then apply the filter here once rather than per pixel
432 // in a shader.
433 if (constantColor) {
434 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
435 grPaint->setColor(SkColor2GrColor(filtered));
436 } else {
437 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
438 if (NULL != effect.get()) {
439 grPaint->addColorEffect(effect);
440 }
441 }
442 }
443
444 return true;
445}
446
447// This function is similar to skPaint2GrPaintNoShader but also converts
448// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
449// be used is set on grPaint and returned in param act. constantColor has the
450// same meaning as in skPaint2GrPaintNoShader.
451inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
452 const SkPaint& skPaint,
453 bool constantColor,
454 GrPaint* grPaint) {
455 SkShader* shader = skPaint.getShader();
456 if (NULL == shader) {
457 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
458 }
459
commit-bot@chromium.org60770572014-01-13 15:57:05 +0000460 // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state and require
461 // the shader to set a render target .
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000462 GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000463
464 // setup the shader as the first color effect on the paint
465 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
466 if (NULL != effect.get()) {
467 grPaint->addColorEffect(effect);
468 // Now setup the rest of the paint.
469 return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
470 } else {
471 // We still don't have SkColorShader::asNewEffect() implemented.
472 SkShader::GradientInfo info;
473 SkColor color;
474
475 info.fColors = &color;
476 info.fColorOffsets = NULL;
477 info.fColorCount = 1;
478 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
479 SkPaint copy(skPaint);
480 copy.setShader(NULL);
481 // modulate the paint alpha by the shader's solid color alpha
482 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
483 copy.setColor(SkColorSetA(color, newA));
484 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
485 } else {
486 return false;
487 }
488 }
489}
490}
491
492///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000493
494SkBitmap::Config SkGpuDevice::config() const {
495 if (NULL == fRenderTarget) {
496 return SkBitmap::kNo_Config;
497 }
498
499 bool isOpaque;
500 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
501}
502
503void SkGpuDevice::clear(SkColor color) {
504 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
505 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
506 fNeedClear = false;
507}
508
509void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
510 CHECK_SHOULD_DRAW(draw, false);
511
512 GrPaint grPaint;
513 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
514 return;
515 }
516
517 fContext->drawPaint(grPaint);
518}
519
520// must be in SkCanvas::PointMode order
521static const GrPrimitiveType gPointMode2PrimtiveType[] = {
522 kPoints_GrPrimitiveType,
523 kLines_GrPrimitiveType,
524 kLineStrip_GrPrimitiveType
525};
526
527void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
528 size_t count, const SkPoint pts[], const SkPaint& paint) {
529 CHECK_FOR_ANNOTATION(paint);
530 CHECK_SHOULD_DRAW(draw, false);
531
532 SkScalar width = paint.getStrokeWidth();
533 if (width < 0) {
534 return;
535 }
536
537 // we only handle hairlines and paints without path effects or mask filters,
538 // else we let the SkDraw call our drawPath()
539 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
540 draw.drawPoints(mode, count, pts, paint, true);
541 return;
542 }
543
544 GrPaint grPaint;
545 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
546 return;
547 }
548
549 fContext->drawVertices(grPaint,
550 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000551 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000552 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000553 NULL,
554 NULL,
555 NULL,
556 0);
557}
558
559///////////////////////////////////////////////////////////////////////////////
560
561void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
562 const SkPaint& paint) {
563 CHECK_FOR_ANNOTATION(paint);
564 CHECK_SHOULD_DRAW(draw, false);
565
566 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
567 SkScalar width = paint.getStrokeWidth();
568
569 /*
570 We have special code for hairline strokes, miter-strokes, bevel-stroke
571 and fills. Anything else we just call our path code.
572 */
573 bool usePath = doStroke && width > 0 &&
574 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
575 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
576 // another two reasons we might need to call drawPath...
577 if (paint.getMaskFilter() || paint.getPathEffect()) {
578 usePath = true;
579 }
580 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
581#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
582 if (doStroke) {
583#endif
584 usePath = true;
585#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
586 } else {
587 usePath = !fContext->getMatrix().preservesRightAngles();
588 }
589#endif
590 }
591 // until we can both stroke and fill rectangles
592 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
593 usePath = true;
594 }
595
596 if (usePath) {
597 SkPath path;
598 path.addRect(rect);
599 this->drawPath(draw, path, paint, NULL, true);
600 return;
601 }
602
603 GrPaint grPaint;
604 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
605 return;
606 }
607
608 if (!doStroke) {
609 fContext->drawRect(grPaint, rect);
610 } else {
611 SkStrokeRec stroke(paint);
612 fContext->drawRect(grPaint, rect, &stroke);
613 }
614}
615
616///////////////////////////////////////////////////////////////////////////////
617
618void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
619 const SkPaint& paint) {
620 CHECK_FOR_ANNOTATION(paint);
621 CHECK_SHOULD_DRAW(draw, false);
622
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000623 GrPaint grPaint;
624 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
625 return;
626 }
627
628 SkStrokeRec stroke(paint);
629 if (paint.getMaskFilter()) {
630 // try to hit the fast path for drawing filtered round rects
631
632 SkRRect devRRect;
633 if (rect.transform(fContext->getMatrix(), &devRRect)) {
634 if (devRRect.allCornersCircular()) {
635 SkRect maskRect;
636 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
637 draw.fClip->getBounds(),
638 fContext->getMatrix(),
639 &maskRect)) {
640 SkIRect finalIRect;
641 maskRect.roundOut(&finalIRect);
642 if (draw.fClip->quickReject(finalIRect)) {
643 // clipped out
644 return;
645 }
646 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
647 // nothing to draw
648 return;
649 }
650 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
651 stroke, devRRect)) {
652 return;
653 }
654 }
655
656 }
657 }
658
659 }
660
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000661 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000662 SkPath path;
663 path.addRRect(rect);
664 this->drawPath(draw, path, paint, NULL, true);
665 return;
666 }
667
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000668 fContext->drawRRect(grPaint, rect, stroke);
669}
670
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000671void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
672 const SkRRect& inner, const SkPaint& paint) {
673 SkStrokeRec stroke(paint);
674 if (stroke.isFillStyle()) {
675
676 CHECK_FOR_ANNOTATION(paint);
677 CHECK_SHOULD_DRAW(draw, false);
678
679 GrPaint grPaint;
680 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
681 return;
682 }
683
684 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
685 fContext->drawDRRect(grPaint, outer, inner);
686 return;
687 }
688 }
689
690 SkPath path;
691 path.addRRect(outer);
692 path.addRRect(inner);
693 path.setFillType(SkPath::kEvenOdd_FillType);
694
695 this->drawPath(draw, path, paint, NULL, true);
696}
697
698
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000699/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000700
701void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
702 const SkPaint& paint) {
703 CHECK_FOR_ANNOTATION(paint);
704 CHECK_SHOULD_DRAW(draw, false);
705
706 bool usePath = false;
707 // some basic reasons we might need to call drawPath...
708 if (paint.getMaskFilter() || paint.getPathEffect()) {
709 usePath = true;
710 }
711
712 if (usePath) {
713 SkPath path;
714 path.addOval(oval);
715 this->drawPath(draw, path, paint, NULL, true);
716 return;
717 }
718
719 GrPaint grPaint;
720 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
721 return;
722 }
723 SkStrokeRec stroke(paint);
724
725 fContext->drawOval(grPaint, oval, stroke);
726}
727
728#include "SkMaskFilter.h"
729#include "SkBounder.h"
730
731///////////////////////////////////////////////////////////////////////////////
732
733// helpers for applying mask filters
734namespace {
735
736// Draw a mask using the supplied paint. Since the coverage/geometry
737// is already burnt into the mask this boils down to a rect draw.
738// Return true if the mask was successfully drawn.
739bool draw_mask(GrContext* context, const SkRect& maskRect,
740 GrPaint* grp, GrTexture* mask) {
741 GrContext::AutoMatrix am;
742 if (!am.setIdentity(context, grp)) {
743 return false;
744 }
745
746 SkMatrix matrix;
747 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
748 matrix.postIDiv(mask->width(), mask->height());
749
750 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
751 context->drawRect(*grp, maskRect);
752 return true;
753}
754
755bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
756 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
757 GrPaint* grp, SkPaint::Style style) {
758 SkMask srcM, dstM;
759
760 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
761 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
762 return false;
763 }
764 SkAutoMaskFreeImage autoSrc(srcM.fImage);
765
766 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
767 return false;
768 }
769 // this will free-up dstM when we're done (allocated in filterMask())
770 SkAutoMaskFreeImage autoDst(dstM.fImage);
771
772 if (clip.quickReject(dstM.fBounds)) {
773 return false;
774 }
775 if (bounder && !bounder->doIRect(dstM.fBounds)) {
776 return false;
777 }
778
779 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
780 // the current clip (and identity matrix) and GrPaint settings
781 GrTextureDesc desc;
782 desc.fWidth = dstM.fBounds.width();
783 desc.fHeight = dstM.fBounds.height();
784 desc.fConfig = kAlpha_8_GrPixelConfig;
785
786 GrAutoScratchTexture ast(context, desc);
787 GrTexture* texture = ast.texture();
788
789 if (NULL == texture) {
790 return false;
791 }
792 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
793 dstM.fImage, dstM.fRowBytes);
794
795 SkRect maskRect = SkRect::Make(dstM.fBounds);
796
797 return draw_mask(context, maskRect, grp, texture);
798}
799
800// Create a mask of 'devPath' and place the result in 'mask'. Return true on
801// success; false otherwise.
802bool create_mask_GPU(GrContext* context,
803 const SkRect& maskRect,
804 const SkPath& devPath,
805 const SkStrokeRec& stroke,
806 bool doAA,
807 GrAutoScratchTexture* mask) {
808 GrTextureDesc desc;
809 desc.fFlags = kRenderTarget_GrTextureFlagBit;
810 desc.fWidth = SkScalarCeilToInt(maskRect.width());
811 desc.fHeight = SkScalarCeilToInt(maskRect.height());
812 // We actually only need A8, but it often isn't supported as a
813 // render target so default to RGBA_8888
814 desc.fConfig = kRGBA_8888_GrPixelConfig;
815 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
816 desc.fConfig = kAlpha_8_GrPixelConfig;
817 }
818
819 mask->set(context, desc);
820 if (NULL == mask->texture()) {
821 return false;
822 }
823
824 GrTexture* maskTexture = mask->texture();
825 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
826
827 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
828 GrContext::AutoClip ac(context, clipRect);
829
830 context->clear(NULL, 0x0, true);
831
832 GrPaint tempPaint;
833 if (doAA) {
834 tempPaint.setAntiAlias(true);
835 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
836 // blend coeff of zero requires dual source blending support in order
837 // to properly blend partially covered pixels. This means the AA
838 // code path may not be taken. So we use a dst blend coeff of ISA. We
839 // could special case AA draws to a dst surface with known alpha=0 to
840 // use a zero dst coeff when dual source blending isn't available.
841 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
842 }
843
844 GrContext::AutoMatrix am;
845
846 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
847 SkMatrix translate;
848 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
849 am.set(context, translate);
850 context->drawPath(tempPaint, devPath, stroke);
851 return true;
852}
853
854SkBitmap wrap_texture(GrTexture* texture) {
reed@google.combf790232013-12-13 19:45:58 +0000855 SkImageInfo info;
856 texture->asImageInfo(&info);
857
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000858 SkBitmap result;
reed@google.combf790232013-12-13 19:45:58 +0000859 result.setConfig(info);
860 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000861 return result;
862}
863
864};
865
866void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
867 const SkPaint& paint, const SkMatrix* prePathMatrix,
868 bool pathIsMutable) {
869 CHECK_FOR_ANNOTATION(paint);
870 CHECK_SHOULD_DRAW(draw, false);
871
872 GrPaint grPaint;
873 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
874 return;
875 }
876
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000877 // If we have a prematrix, apply it to the path, optimizing for the case
878 // where the original path can in fact be modified in place (even though
879 // its parameter type is const).
880 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000881 SkTLazy<SkPath> tmpPath;
882 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000883
884 if (prePathMatrix) {
885 SkPath* result = pathPtr;
886
887 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000888 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000889 pathIsMutable = true;
890 }
891 // should I push prePathMatrix on our MV stack temporarily, instead
892 // of applying it here? See SkDraw.cpp
893 pathPtr->transform(*prePathMatrix, result);
894 pathPtr = result;
895 }
896 // at this point we're done with prePathMatrix
897 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
898
899 SkStrokeRec stroke(paint);
900 SkPathEffect* pathEffect = paint.getPathEffect();
901 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000902 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000903 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000904 pathPtr = effectPath.get();
905 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000906 }
907
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000908 if (paint.getMaskFilter()) {
909 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000910 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
911 if (stroke.applyToPath(strokedPath, *pathPtr)) {
912 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000913 pathIsMutable = true;
914 stroke.setFillStyle();
915 }
916 }
917
918 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000919 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000920
921 // transform the path into device space
922 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000923
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000924 SkRect maskRect;
925 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
926 draw.fClip->getBounds(),
927 fContext->getMatrix(),
928 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000929 // The context's matrix may change while creating the mask, so save the CTM here to
930 // pass to filterMaskGPU.
931 const SkMatrix ctm = fContext->getMatrix();
932
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933 SkIRect finalIRect;
934 maskRect.roundOut(&finalIRect);
935 if (draw.fClip->quickReject(finalIRect)) {
936 // clipped out
937 return;
938 }
939 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
940 // nothing to draw
941 return;
942 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000943
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000944 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000945 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000946 // the mask filter was able to draw itself directly, so there's nothing
947 // left to do.
948 return;
949 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000950
951 GrAutoScratchTexture mask;
952
953 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
954 grPaint.isAntiAlias(), &mask)) {
955 GrTexture* filtered;
956
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000957 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000958 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000959 // filterMaskGPU gives us ownership of a ref to the result
960 SkAutoTUnref<GrTexture> atu(filtered);
961
962 // If the scratch texture that we used as the filter src also holds the filter
963 // result then we must detach so that this texture isn't recycled for a later
964 // draw.
965 if (filtered == mask.texture()) {
966 mask.detach();
967 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
968 }
969
970 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
971 // This path is completely drawn
972 return;
973 }
974 }
975 }
976 }
977
978 // draw the mask on the CPU - this is a fallthrough path in case the
979 // GPU path fails
980 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
981 SkPaint::kFill_Style;
982 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
983 *draw.fClip, draw.fBounder, &grPaint, style);
984 return;
985 }
986
987 fContext->drawPath(grPaint, *pathPtr, stroke);
988}
989
990static const int kBmpSmallTileSize = 1 << 10;
991
992static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
993 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
994 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
995 return tilesX * tilesY;
996}
997
998static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
999 if (maxTileSize <= kBmpSmallTileSize) {
1000 return maxTileSize;
1001 }
1002
1003 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
1004 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
1005
1006 maxTileTotalTileSize *= maxTileSize * maxTileSize;
1007 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
1008
1009 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
1010 return kBmpSmallTileSize;
1011 } else {
1012 return maxTileSize;
1013 }
1014}
1015
1016// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
1017// pixels from the bitmap are necessary.
1018static void determine_clipped_src_rect(const GrContext* context,
1019 const SkBitmap& bitmap,
1020 const SkRect* srcRectPtr,
1021 SkIRect* clippedSrcIRect) {
1022 const GrClipData* clip = context->getClip();
1023 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
1024 SkMatrix inv;
1025 if (!context->getMatrix().invert(&inv)) {
1026 clippedSrcIRect->setEmpty();
1027 return;
1028 }
1029 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
1030 inv.mapRect(&clippedSrcRect);
1031 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001032 // we've setup src space 0,0 to map to the top left of the src rect.
1033 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001034 if (!clippedSrcRect.intersect(*srcRectPtr)) {
1035 clippedSrcIRect->setEmpty();
1036 return;
1037 }
1038 }
1039 clippedSrcRect.roundOut(clippedSrcIRect);
1040 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1041 if (!clippedSrcIRect->intersect(bmpBounds)) {
1042 clippedSrcIRect->setEmpty();
1043 }
1044}
1045
1046bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1047 const GrTextureParams& params,
1048 const SkRect* srcRectPtr,
1049 int maxTileSize,
1050 int* tileSize,
1051 SkIRect* clippedSrcRect) const {
1052 // if bitmap is explictly texture backed then just use the texture
1053 if (NULL != bitmap.getTexture()) {
1054 return false;
1055 }
1056
1057 // if it's larger than the max tile size, then we have no choice but tiling.
1058 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
1059 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1060 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
1061 return true;
1062 }
1063
1064 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
1065 return false;
1066 }
1067
1068 // if the entire texture is already in our cache then no reason to tile it
1069 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
1070 return false;
1071 }
1072
1073 // At this point we know we could do the draw by uploading the entire bitmap
1074 // as a texture. However, if the texture would be large compared to the
1075 // cache size and we don't require most of it for this draw then tile to
1076 // reduce the amount of upload and cache spill.
1077
1078 // assumption here is that sw bitmap size is a good proxy for its size as
1079 // a texture
1080 size_t bmpSize = bitmap.getSize();
1081 size_t cacheSize;
1082 fContext->getTextureCacheLimits(NULL, &cacheSize);
1083 if (bmpSize < cacheSize / 2) {
1084 return false;
1085 }
1086
1087 // Figure out how much of the src we will need based on the src rect and clipping.
1088 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1089 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1090 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
1091 kBmpSmallTileSize * kBmpSmallTileSize;
1092
1093 return usedTileBytes < 2 * bmpSize;
1094}
1095
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001096void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001097 const SkBitmap& bitmap,
1098 const SkMatrix& m,
1099 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001100 SkMatrix concat;
1101 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1102 if (!m.isIdentity()) {
1103 concat.setConcat(*draw->fMatrix, m);
1104 draw.writable()->fMatrix = &concat;
1105 }
1106 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001107}
1108
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001109// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001110// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1111// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001112static inline void clamped_outset_with_offset(SkIRect* iRect,
1113 int outset,
1114 SkPoint* offset,
1115 const SkIRect& clamp) {
1116 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001117
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001118 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1119 if (leftClampDelta > 0) {
1120 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001121 iRect->fLeft = clamp.fLeft;
1122 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001123 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001124 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001125
1126 int topClampDelta = clamp.fTop - iRect->fTop;
1127 if (topClampDelta > 0) {
1128 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001129 iRect->fTop = clamp.fTop;
1130 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001131 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001132 }
1133
1134 if (iRect->fRight > clamp.fRight) {
1135 iRect->fRight = clamp.fRight;
1136 }
1137 if (iRect->fBottom > clamp.fBottom) {
1138 iRect->fBottom = clamp.fBottom;
1139 }
1140}
1141
1142void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1143 const SkBitmap& bitmap,
1144 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001145 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001146 const SkPaint& paint,
1147 SkCanvas::DrawBitmapRectFlags flags) {
1148 CHECK_SHOULD_DRAW(draw, false);
1149
1150 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001151 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001152 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1153 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001154 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001155 SkScalar w = SkIntToScalar(bitmap.width());
1156 SkScalar h = SkIntToScalar(bitmap.height());
1157 dstSize.fWidth = w;
1158 dstSize.fHeight = h;
1159 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001160 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001161 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001162 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001163 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001164 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001165 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1166 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1167 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1168 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001169 }
1170
1171 if (paint.getMaskFilter()){
1172 // Convert the bitmap to a shader so that the rect can be drawn
1173 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001174 SkBitmap tmp; // subset of bitmap, if necessary
1175 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001176 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001177 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001178 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1179 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1180 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001181 // In bleed mode we position and trim the bitmap based on the src rect which is
1182 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1183 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1184 // compensate.
1185 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1186 SkIRect iSrc;
1187 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001188
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001189 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1190 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001191
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001192 if (!bitmap.extractSubset(&tmp, iSrc)) {
1193 return; // extraction failed
1194 }
1195 bitmapPtr = &tmp;
1196 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001197
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001198 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001199 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001200 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001201 } else {
1202 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001203 }
1204
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001205 SkPaint paintWithShader(paint);
1206 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001207 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001208 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1209 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001210
1211 return;
1212 }
1213
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001214 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1215 // the view matrix rather than a local matrix.
1216 SkMatrix m;
1217 m.setScale(dstSize.fWidth / srcRect.width(),
1218 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001219 fContext->concatMatrix(m);
1220
1221 GrTextureParams params;
1222 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1223 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001224
1225 int tileFilterPad;
1226 bool doBicubic = false;
1227
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001228 switch(paintFilterLevel) {
1229 case SkPaint::kNone_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001230 tileFilterPad = 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001231 textureFilterMode = GrTextureParams::kNone_FilterMode;
1232 break;
1233 case SkPaint::kLow_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001234 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001235 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1236 break;
1237 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001238 tileFilterPad = 1;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001239 if (fContext->getMatrix().getMinStretch() < SK_Scalar1) {
1240 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1241 } else {
1242 // Don't trigger MIP level generation unnecessarily.
1243 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1244 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001245 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001246 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001247 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001248 if (fContext->getMatrix().getMinStretch() >= SK_Scalar1) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001249 // We will install an effect that does the filtering in the shader.
1250 textureFilterMode = GrTextureParams::kNone_FilterMode;
1251 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1252 doBicubic = true;
1253 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001254 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1255 tileFilterPad = 1;
1256 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001257 break;
1258 default:
1259 SkErrorInternals::SetError( kInvalidPaint_SkError,
1260 "Sorry, I don't understand the filtering "
1261 "mode you asked for. Falling back to "
1262 "MIPMaps.");
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001263 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001264 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1265 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001266 }
1267
1268 params.setFilterMode(textureFilterMode);
1269
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001270 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001271 int tileSize;
1272
1273 SkIRect clippedSrcRect;
1274 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1275 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001276 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1277 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001278 } else {
1279 // take the simple case
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001280 this->internalDrawBitmap(bitmap, srcRect, params, paint, flags, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001281 }
1282}
1283
1284// Break 'bitmap' into several tiles to draw it since it has already
1285// been determined to be too large to fit in VRAM
1286void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1287 const SkRect& srcRect,
1288 const SkIRect& clippedSrcIRect,
1289 const GrTextureParams& params,
1290 const SkPaint& paint,
1291 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001292 int tileSize,
1293 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001294 // The following pixel lock is technically redundant, but it is desirable
1295 // to lock outside of the tile loop to prevent redecoding the whole image
1296 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1297 // is larger than the limit of the discardable memory pool.
1298 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001299 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1300
1301 int nx = bitmap.width() / tileSize;
1302 int ny = bitmap.height() / tileSize;
1303 for (int x = 0; x <= nx; x++) {
1304 for (int y = 0; y <= ny; y++) {
1305 SkRect tileR;
1306 tileR.set(SkIntToScalar(x * tileSize),
1307 SkIntToScalar(y * tileSize),
1308 SkIntToScalar((x + 1) * tileSize),
1309 SkIntToScalar((y + 1) * tileSize));
1310
1311 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1312 continue;
1313 }
1314
1315 if (!tileR.intersect(srcRect)) {
1316 continue;
1317 }
1318
1319 SkBitmap tmpB;
1320 SkIRect iTileR;
1321 tileR.roundOut(&iTileR);
1322 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1323 SkIntToScalar(iTileR.fTop));
1324
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001325 // Adjust the context matrix to draw at the right x,y in device space
1326 SkMatrix tmpM;
1327 GrContext::AutoMatrix am;
1328 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1329 am.setPreConcat(fContext, tmpM);
1330
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001331 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001332 SkIRect iClampRect;
1333
1334 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1335 // In bleed mode we want to always expand the tile on all edges
1336 // but stay within the bitmap bounds
1337 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1338 } else {
1339 // In texture-domain/clamp mode we only want to expand the
1340 // tile on edges interior to "srcRect" (i.e., we want to
1341 // not bleed across the original clamped edges)
1342 srcRect.roundOut(&iClampRect);
1343 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001344 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1345 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001346 }
1347
1348 if (bitmap.extractSubset(&tmpB, iTileR)) {
1349 // now offset it to make it "local" to our tmp bitmap
1350 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001351
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001352 this->internalDrawBitmap(tmpB, tileR, params, paint, flags, bicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001353 }
1354 }
1355 }
1356}
1357
1358static bool has_aligned_samples(const SkRect& srcRect,
1359 const SkRect& transformedRect) {
1360 // detect pixel disalignment
1361 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1362 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1363 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1364 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1365 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1366 COLOR_BLEED_TOLERANCE &&
1367 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1368 COLOR_BLEED_TOLERANCE) {
1369 return true;
1370 }
1371 return false;
1372}
1373
1374static bool may_color_bleed(const SkRect& srcRect,
1375 const SkRect& transformedRect,
1376 const SkMatrix& m) {
1377 // Only gets called if has_aligned_samples returned false.
1378 // So we can assume that sampling is axis aligned but not texel aligned.
1379 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1380 SkRect innerSrcRect(srcRect), innerTransformedRect,
1381 outerTransformedRect(transformedRect);
1382 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1383 m.mapRect(&innerTransformedRect, innerSrcRect);
1384
1385 // The gap between outerTransformedRect and innerTransformedRect
1386 // represents the projection of the source border area, which is
1387 // problematic for color bleeding. We must check whether any
1388 // destination pixels sample the border area.
1389 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1390 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1391 SkIRect outer, inner;
1392 outerTransformedRect.round(&outer);
1393 innerTransformedRect.round(&inner);
1394 // If the inner and outer rects round to the same result, it means the
1395 // border does not overlap any pixel centers. Yay!
1396 return inner != outer;
1397}
1398
1399
1400/*
1401 * This is called by drawBitmap(), which has to handle images that may be too
1402 * large to be represented by a single texture.
1403 *
1404 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1405 * and that non-texture portion of the GrPaint has already been setup.
1406 */
1407void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1408 const SkRect& srcRect,
1409 const GrTextureParams& params,
1410 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001411 SkCanvas::DrawBitmapRectFlags flags,
1412 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001413 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1414 bitmap.height() <= fContext->getMaxTextureSize());
1415
1416 GrTexture* texture;
1417 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1418 if (NULL == texture) {
1419 return;
1420 }
1421
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001422 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001423 SkRect paintRect;
1424 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1425 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1426 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1427 SkScalarMul(srcRect.fTop, hInv),
1428 SkScalarMul(srcRect.fRight, wInv),
1429 SkScalarMul(srcRect.fBottom, hInv));
1430
1431 bool needsTextureDomain = false;
1432 if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) &&
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001433 (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode)) {
1434 // Need texture domain if drawing a sub rect
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001435 needsTextureDomain = srcRect.width() < bitmap.width() ||
1436 srcRect.height() < bitmap.height();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001437 if (!bicubic && needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001438 const SkMatrix& matrix = fContext->getMatrix();
1439 // sampling is axis-aligned
1440 SkRect transformedRect;
1441 matrix.mapRect(&transformedRect, srcRect);
1442
1443 if (has_aligned_samples(srcRect, transformedRect)) {
1444 // We could also turn off filtering here (but we already did a cache lookup with
1445 // params).
1446 needsTextureDomain = false;
1447 } else {
1448 needsTextureDomain = may_color_bleed(srcRect, transformedRect, matrix);
1449 }
1450 }
1451 }
1452
1453 SkRect textureDomain = SkRect::MakeEmpty();
1454 SkAutoTUnref<GrEffectRef> effect;
1455 if (needsTextureDomain) {
1456 // Use a constrained texture domain to avoid color bleeding
1457 SkScalar left, top, right, bottom;
1458 if (srcRect.width() > SK_Scalar1) {
1459 SkScalar border = SK_ScalarHalf / texture->width();
1460 left = paintRect.left() + border;
1461 right = paintRect.right() - border;
1462 } else {
1463 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1464 }
1465 if (srcRect.height() > SK_Scalar1) {
1466 SkScalar border = SK_ScalarHalf / texture->height();
1467 top = paintRect.top() + border;
1468 bottom = paintRect.bottom() - border;
1469 } else {
1470 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1471 }
1472 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001473 if (bicubic) {
1474 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1475 } else {
1476 effect.reset(GrTextureDomainEffect::Create(texture,
1477 SkMatrix::I(),
1478 textureDomain,
1479 GrTextureDomain::kClamp_Mode,
1480 params.filterMode()));
1481 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001482 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001483 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1484 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1485 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001486 } else {
1487 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1488 }
1489
1490 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1491 // the rest from the SkPaint.
1492 GrPaint grPaint;
1493 grPaint.addColorEffect(effect);
1494 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1495 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1496 return;
1497 }
1498
1499 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1500}
1501
1502static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001503 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001504 int w, int h, const SkImageFilter::Context& ctx,
1505 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001506 SkASSERT(filter);
1507 SkDeviceImageFilterProxy proxy(device);
1508
1509 if (filter->canFilterImageGPU()) {
1510 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1511 // filter. Also set the clip wide open and the matrix to identity.
1512 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001513 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001514 } else {
1515 return false;
1516 }
1517}
1518
1519void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1520 int left, int top, const SkPaint& paint) {
1521 // drawSprite is defined to be in device coords.
1522 CHECK_SHOULD_DRAW(draw, true);
1523
1524 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1525 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1526 return;
1527 }
1528
1529 int w = bitmap.width();
1530 int h = bitmap.height();
1531
1532 GrTexture* texture;
1533 // draw sprite uses the default texture params
1534 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1535
1536 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001537 // This bitmap will own the filtered result as a texture.
1538 SkBitmap filteredBitmap;
1539
1540 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001541 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001542 SkMatrix matrix(*draw.fMatrix);
1543 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001544 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001545 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1546 SkAutoUnref aur(cache);
1547 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001548 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001549 &offset)) {
1550 texture = (GrTexture*) filteredBitmap.getTexture();
1551 w = filteredBitmap.width();
1552 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001553 left += offset.x();
1554 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001555 } else {
1556 return;
1557 }
1558 }
1559
1560 GrPaint grPaint;
1561 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1562
1563 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1564 return;
1565 }
1566
1567 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001568 SkRect::MakeXYWH(SkIntToScalar(left),
1569 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001570 SkIntToScalar(w),
1571 SkIntToScalar(h)),
1572 SkRect::MakeXYWH(0,
1573 0,
1574 SK_Scalar1 * w / texture->width(),
1575 SK_Scalar1 * h / texture->height()));
1576}
1577
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001578void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001579 const SkRect* src, const SkRect& dst,
1580 const SkPaint& paint,
1581 SkCanvas::DrawBitmapRectFlags flags) {
1582 SkMatrix matrix;
1583 SkRect bitmapBounds, tmpSrc;
1584
1585 bitmapBounds.set(0, 0,
1586 SkIntToScalar(bitmap.width()),
1587 SkIntToScalar(bitmap.height()));
1588
1589 // Compute matrix from the two rectangles
1590 if (NULL != src) {
1591 tmpSrc = *src;
1592 } else {
1593 tmpSrc = bitmapBounds;
1594 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001595
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001596 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1597
1598 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1599 if (NULL != src) {
1600 if (!bitmapBounds.contains(tmpSrc)) {
1601 if (!tmpSrc.intersect(bitmapBounds)) {
1602 return; // nothing to draw
1603 }
1604 }
1605 }
1606
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001607 SkRect tmpDst;
1608 matrix.mapRect(&tmpDst, tmpSrc);
1609
1610 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1611 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1612 // Translate so that tempDst's top left is at the origin.
1613 matrix = *origDraw.fMatrix;
1614 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1615 draw.writable()->fMatrix = &matrix;
1616 }
1617 SkSize dstSize;
1618 dstSize.fWidth = tmpDst.width();
1619 dstSize.fHeight = tmpDst.height();
1620
1621 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001622}
1623
1624void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1625 int x, int y, const SkPaint& paint) {
1626 // clear of the source device must occur before CHECK_SHOULD_DRAW
1627 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1628 if (dev->fNeedClear) {
1629 // TODO: could check here whether we really need to draw at all
1630 dev->clear(0x0);
1631 }
1632
1633 // drawDevice is defined to be in device coords.
1634 CHECK_SHOULD_DRAW(draw, true);
1635
1636 GrRenderTarget* devRT = dev->accessRenderTarget();
1637 GrTexture* devTex;
1638 if (NULL == (devTex = devRT->asTexture())) {
1639 return;
1640 }
1641
1642 const SkBitmap& bm = dev->accessBitmap(false);
1643 int w = bm.width();
1644 int h = bm.height();
1645
1646 SkImageFilter* filter = paint.getImageFilter();
1647 // This bitmap will own the filtered result as a texture.
1648 SkBitmap filteredBitmap;
1649
1650 if (NULL != filter) {
1651 SkIPoint offset = SkIPoint::Make(0, 0);
1652 SkMatrix matrix(*draw.fMatrix);
1653 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001654 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001655 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1656 SkAutoUnref aur(cache);
1657 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001658 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001659 &offset)) {
1660 devTex = filteredBitmap.getTexture();
1661 w = filteredBitmap.width();
1662 h = filteredBitmap.height();
1663 x += offset.fX;
1664 y += offset.fY;
1665 } else {
1666 return;
1667 }
1668 }
1669
1670 GrPaint grPaint;
1671 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1672
1673 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1674 return;
1675 }
1676
1677 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1678 SkIntToScalar(y),
1679 SkIntToScalar(w),
1680 SkIntToScalar(h));
1681
1682 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1683 // scratch texture).
1684 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1685 SK_Scalar1 * h / devTex->height());
1686
1687 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1688}
1689
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001690bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001691 return filter->canFilterImageGPU();
1692}
1693
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001694bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001695 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001696 SkBitmap* result, SkIPoint* offset) {
1697 // want explicitly our impl, so guard against a subclass of us overriding it
1698 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1699 return false;
1700 }
1701
1702 SkAutoLockPixels alp(src, !src.getTexture());
1703 if (!src.getTexture() && !src.readyToDraw()) {
1704 return false;
1705 }
1706
1707 GrTexture* texture;
1708 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1709 // must be pushed upstack.
1710 SkAutoCachedTexture act(this, src, NULL, &texture);
1711
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001712 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1713 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001714}
1715
1716///////////////////////////////////////////////////////////////////////////////
1717
1718// must be in SkCanvas::VertexMode order
1719static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1720 kTriangles_GrPrimitiveType,
1721 kTriangleStrip_GrPrimitiveType,
1722 kTriangleFan_GrPrimitiveType,
1723};
1724
1725void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1726 int vertexCount, const SkPoint vertices[],
1727 const SkPoint texs[], const SkColor colors[],
1728 SkXfermode* xmode,
1729 const uint16_t indices[], int indexCount,
1730 const SkPaint& paint) {
1731 CHECK_SHOULD_DRAW(draw, false);
1732
1733 GrPaint grPaint;
1734 // we ignore the shader if texs is null.
1735 if (NULL == texs) {
1736 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
1737 return;
1738 }
1739 } else {
1740 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
1741 return;
1742 }
1743 }
1744
1745 if (NULL != xmode && NULL != texs && NULL != colors) {
1746 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1747 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1748#if 0
1749 return
1750#endif
1751 }
1752 }
1753
1754 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1755 if (NULL != colors) {
1756 // need to convert byte order and from non-PM to PM
1757 convertedColors.reset(vertexCount);
1758 for (int i = 0; i < vertexCount; ++i) {
1759 convertedColors[i] = SkColor2GrColor(colors[i]);
1760 }
1761 colors = convertedColors.get();
1762 }
1763 fContext->drawVertices(grPaint,
1764 gVertexMode2PrimitiveType[vmode],
1765 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001766 vertices,
1767 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001768 colors,
1769 indices,
1770 indexCount);
1771}
1772
1773///////////////////////////////////////////////////////////////////////////////
1774
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001775void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1776 size_t byteLength, SkScalar x, SkScalar y,
1777 const SkPaint& paint) {
1778 CHECK_SHOULD_DRAW(draw, false);
1779
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001780 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001781 GrPaint grPaint;
1782 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1783 return;
1784 }
1785
1786 SkDEBUGCODE(this->validate();)
1787
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001788 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1789 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001790 GrPaint grPaint;
1791 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1792 return;
1793 }
1794
1795 SkDEBUGCODE(this->validate();)
1796
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001797 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001798 } else {
1799 // this guy will just call our drawPath()
1800 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001801 }
1802}
1803
1804void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1805 size_t byteLength, const SkScalar pos[],
1806 SkScalar constY, int scalarsPerPos,
1807 const SkPaint& paint) {
1808 CHECK_SHOULD_DRAW(draw, false);
1809
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001810 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001811 GrPaint grPaint;
1812 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1813 return;
1814 }
1815
1816 SkDEBUGCODE(this->validate();)
1817
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001818 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001819 constY, scalarsPerPos);
1820 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001821 GrPaint grPaint;
1822 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1823 return;
1824 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001825
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001826 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001827
1828 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001829 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001830 } else {
1831 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1832 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001833 }
1834}
1835
1836void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1837 size_t len, const SkPath& path,
1838 const SkMatrix* m, const SkPaint& paint) {
1839 CHECK_SHOULD_DRAW(draw, false);
1840
1841 SkASSERT(draw.fDevice == this);
1842 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1843}
1844
1845///////////////////////////////////////////////////////////////////////////////
1846
1847bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1848 if (!paint.isLCDRenderText()) {
1849 // we're cool with the paint as is
1850 return false;
1851 }
1852
1853 if (paint.getShader() ||
1854 paint.getXfermode() || // unless its srcover
1855 paint.getMaskFilter() ||
1856 paint.getRasterizer() ||
1857 paint.getColorFilter() ||
1858 paint.getPathEffect() ||
1859 paint.isFakeBoldText() ||
1860 paint.getStyle() != SkPaint::kFill_Style) {
1861 // turn off lcd
1862 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1863 flags->fHinting = paint.getHinting();
1864 return true;
1865 }
1866 // we're cool with the paint as is
1867 return false;
1868}
1869
1870void SkGpuDevice::flush() {
1871 DO_DEFERRED_CLEAR();
1872 fContext->resolveRenderTarget(fRenderTarget);
1873}
1874
1875///////////////////////////////////////////////////////////////////////////////
1876
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001877SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001878 GrTextureDesc desc;
1879 desc.fConfig = fRenderTarget->config();
1880 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001881 desc.fWidth = info.width();
1882 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001883 desc.fSampleCnt = fRenderTarget->numSamples();
1884
1885 SkAutoTUnref<GrTexture> texture;
1886 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001887 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001888
1889#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1890 // layers are never draw in repeat modes, so we can request an approx
1891 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001892 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001893 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1894 GrContext::kApprox_ScratchTexMatch :
1895 GrContext::kExact_ScratchTexMatch;
1896 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1897#else
1898 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1899#endif
1900 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001901 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001902 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001903 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1904 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001905 return NULL;
1906 }
1907}
1908
reed@google.com76f10a32014-02-05 15:32:21 +00001909SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1910 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1911}
1912
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001913void SkGpuDevice::EXPERIMENTAL_optimize(SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001914 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001915
1916 GPUAccelData* data = SkNEW_ARGS(GPUAccelData, (key));
1917
1918 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001919
1920 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001921}
1922
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001923void SkGpuDevice::EXPERIMENTAL_purge(SkPicture* picture) {
1924
1925}
1926
1927bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001928
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001929 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001930
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001931 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001932 if (NULL == data) {
1933 return false;
1934 }
1935
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001936 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001937
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001938 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1939 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1940 pullForward[i] = false;
1941 }
1942
1943 SkIRect clip;
1944
1945 fClipData.getConservativeBounds(this->width(), this->height(), &clip, NULL);
1946
1947 SkMatrix inv;
1948 if (!fContext->getMatrix().invert(&inv)) {
1949 return false;
1950 }
1951
1952 SkRect r = SkRect::Make(clip);
1953 inv.mapRect(&r);
1954 r.roundOut(&clip);
1955
1956 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(clip);
1957
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001958 for (int i = 0; i < ops.numOps(); ++i) {
1959 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1960 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1961
1962 if (ops.offset(i) > info.fSaveLayerOpID && ops.offset(i) < info.fRestoreOpID) {
1963 pullForward[j] = true;
1964 }
1965 }
1966 }
1967
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001968 return false;
1969}