blob: ee4850fa9b87f079c753aee8ed7105f9ffedf109 [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 {
232 info.fColorType = kPMColor_SkColorType;
233 if (kOpaque_SkAlphaType != info.alphaType()) {
234 info.fAlphaType = kPremul_SkAlphaType; // force this setting
235 }
236 }
237
238 GrTextureDesc desc;
239 desc.fFlags = kRenderTarget_GrTextureFlagBit;
240 desc.fWidth = info.width();
241 desc.fHeight = info.height();
242 desc.fConfig = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType());
243 desc.fSampleCnt = sampleCount;
244
245 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
246 if (!texture.get()) {
247 return NULL;
248 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000249
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000250 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
251}
252
253#ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG
254static SkBitmap make_bitmap(SkBitmap::Config config, int width, int height) {
255 SkBitmap bm;
256 bm.setConfig(SkImageInfo::Make(width, height,
257 SkBitmapConfigToColorType(config),
258 kPremul_SkAlphaType));
259 return bm;
260}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000261SkGpuDevice::SkGpuDevice(GrContext* context,
262 SkBitmap::Config config,
263 int width,
264 int height,
265 int sampleCount)
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000266 : SkBitmapDevice(make_bitmap(config, width, height))
reed@google.combf790232013-12-13 19:45:58 +0000267{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000268 fDrawProcs = NULL;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000269
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000270 fContext = context;
271 fContext->ref();
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000272
commit-bot@chromium.org47841822014-03-27 14:19:17 +0000273 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties));
274 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
275
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000276 fRenderTarget = NULL;
277 fNeedClear = false;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000278
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000279 if (config != SkBitmap::kRGB_565_Config) {
280 config = SkBitmap::kARGB_8888_Config;
281 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000282
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000283 GrTextureDesc desc;
284 desc.fFlags = kRenderTarget_GrTextureFlagBit;
285 desc.fWidth = width;
286 desc.fHeight = height;
287 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
288 desc.fSampleCnt = sampleCount;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000289
reed@google.combf790232013-12-13 19:45:58 +0000290 SkImageInfo info;
291 if (!GrPixelConfig2ColorType(desc.fConfig, &info.fColorType)) {
292 sk_throw();
293 }
294 info.fWidth = width;
295 info.fHeight = height;
296 info.fAlphaType = kPremul_SkAlphaType;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000297
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000298 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000299
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000300 if (NULL != texture) {
301 fRenderTarget = texture->asRenderTarget();
302 fRenderTarget->ref();
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000303
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000304 SkASSERT(NULL != fRenderTarget);
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000305
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000306 // wrap the bitmap with a pixelref to expose our texture
reed@google.combf790232013-12-13 19:45:58 +0000307 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, texture));
reed@google.com672588b2014-01-08 15:42:01 +0000308 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000309 } else {
310 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
311 width, height);
312 SkASSERT(false);
313 }
314}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000315#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000316
317SkGpuDevice::~SkGpuDevice() {
318 if (fDrawProcs) {
319 delete fDrawProcs;
320 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000321
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000322 delete fMainTextContext;
323 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000324
325 // The GrContext takes a ref on the target. We don't want to cause the render
326 // target to be unnecessarily kept alive.
327 if (fContext->getRenderTarget() == fRenderTarget) {
328 fContext->setRenderTarget(NULL);
329 }
330
331 if (fContext->getClip() == &fClipData) {
332 fContext->setClip(NULL);
333 }
334
335 SkSafeUnref(fRenderTarget);
336 fContext->unref();
337}
338
339///////////////////////////////////////////////////////////////////////////////
340
341void SkGpuDevice::makeRenderTargetCurrent() {
342 DO_DEFERRED_CLEAR();
343 fContext->setRenderTarget(fRenderTarget);
344}
345
346///////////////////////////////////////////////////////////////////////////////
347
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000348bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
349 int x, int y) {
350 DO_DEFERRED_CLEAR();
351
352 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
353 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo.colorType(), dstInfo.alphaType());
354 if (kUnknown_GrPixelConfig == config) {
355 return false;
356 }
357
358 uint32_t flags = 0;
359 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
360 flags = GrContext::kUnpremul_PixelOpsFlag;
361 }
362 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
363 config, dstPixels, dstRowBytes, flags);
364}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000365
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000366bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
367 int x, int y) {
368 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
369 GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType());
370 if (kUnknown_GrPixelConfig == config) {
371 return false;
372 }
373 uint32_t flags = 0;
374 if (kUnpremul_SkAlphaType == info.alphaType()) {
375 flags = GrContext::kUnpremul_PixelOpsFlag;
376 }
377 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
378
379 // need to bump our genID for compatibility with clients that "know" we have a bitmap
380 this->onAccessBitmap().notifyPixelsChanged();
381
382 return true;
383}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000384
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000385const SkBitmap& SkGpuDevice::onAccessBitmap() {
386 DO_DEFERRED_CLEAR();
387 return INHERITED::onAccessBitmap();
388}
389
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000390void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
391 INHERITED::onAttachToCanvas(canvas);
392
393 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
394 fClipData.fClipStack = canvas->getClipStack();
395}
396
397void SkGpuDevice::onDetachFromCanvas() {
398 INHERITED::onDetachFromCanvas();
399 fClipData.fClipStack = NULL;
400}
401
402// call this every draw call, to ensure that the context reflects our state,
403// and not the state from some other canvas/device
404void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
405 SkASSERT(NULL != fClipData.fClipStack);
406
407 fContext->setRenderTarget(fRenderTarget);
408
409 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
410
411 if (forceIdentity) {
412 fContext->setIdentityMatrix();
413 } else {
414 fContext->setMatrix(*draw.fMatrix);
415 }
416 fClipData.fOrigin = this->getOrigin();
417
418 fContext->setClip(&fClipData);
419
420 DO_DEFERRED_CLEAR();
421}
422
423GrRenderTarget* SkGpuDevice::accessRenderTarget() {
424 DO_DEFERRED_CLEAR();
425 return fRenderTarget;
426}
427
428///////////////////////////////////////////////////////////////////////////////
429
430SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
431SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
432SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
433SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
434SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
435 shader_type_mismatch);
436SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
437 shader_type_mismatch);
438SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
439SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
440
441namespace {
442
443// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
444// justAlpha indicates that skPaint's alpha should be used rather than the color
445// Callers may subsequently modify the GrPaint. Setting constantColor indicates
446// that the final paint will draw the same color at every pixel. This allows
447// an optimization where the the color filter can be applied to the skPaint's
448// color once while converting to GrPaint and then ignored.
449inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
450 const SkPaint& skPaint,
451 bool justAlpha,
452 bool constantColor,
453 GrPaint* grPaint) {
454
455 grPaint->setDither(skPaint.isDither());
456 grPaint->setAntiAlias(skPaint.isAntiAlias());
457
458 SkXfermode::Coeff sm;
459 SkXfermode::Coeff dm;
460
461 SkXfermode* mode = skPaint.getXfermode();
462 GrEffectRef* xferEffect = NULL;
463 if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
464 if (NULL != xferEffect) {
465 grPaint->addColorEffect(xferEffect)->unref();
466 sm = SkXfermode::kOne_Coeff;
467 dm = SkXfermode::kZero_Coeff;
468 }
469 } else {
470 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
471#if 0
472 return false;
473#else
474 // Fall back to src-over
475 sm = SkXfermode::kOne_Coeff;
476 dm = SkXfermode::kISA_Coeff;
477#endif
478 }
479 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
480
481 if (justAlpha) {
482 uint8_t alpha = skPaint.getAlpha();
483 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
484 // justAlpha is currently set to true only if there is a texture,
485 // so constantColor should not also be true.
486 SkASSERT(!constantColor);
487 } else {
488 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
489 }
490
491 SkColorFilter* colorFilter = skPaint.getColorFilter();
492 if (NULL != colorFilter) {
493 // if the source color is a constant then apply the filter here once rather than per pixel
494 // in a shader.
495 if (constantColor) {
496 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
497 grPaint->setColor(SkColor2GrColor(filtered));
498 } else {
499 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
500 if (NULL != effect.get()) {
501 grPaint->addColorEffect(effect);
502 }
503 }
504 }
505
506 return true;
507}
508
509// This function is similar to skPaint2GrPaintNoShader but also converts
510// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
511// be used is set on grPaint and returned in param act. constantColor has the
512// same meaning as in skPaint2GrPaintNoShader.
513inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
514 const SkPaint& skPaint,
515 bool constantColor,
516 GrPaint* grPaint) {
517 SkShader* shader = skPaint.getShader();
518 if (NULL == shader) {
519 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
520 }
521
commit-bot@chromium.org60770572014-01-13 15:57:05 +0000522 // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state and require
523 // the shader to set a render target .
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000524 GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000525
526 // setup the shader as the first color effect on the paint
527 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
528 if (NULL != effect.get()) {
529 grPaint->addColorEffect(effect);
530 // Now setup the rest of the paint.
531 return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
532 } else {
533 // We still don't have SkColorShader::asNewEffect() implemented.
534 SkShader::GradientInfo info;
535 SkColor color;
536
537 info.fColors = &color;
538 info.fColorOffsets = NULL;
539 info.fColorCount = 1;
540 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
541 SkPaint copy(skPaint);
542 copy.setShader(NULL);
543 // modulate the paint alpha by the shader's solid color alpha
544 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
545 copy.setColor(SkColorSetA(color, newA));
546 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
547 } else {
548 return false;
549 }
550 }
551}
552}
553
554///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000555
556SkBitmap::Config SkGpuDevice::config() const {
557 if (NULL == fRenderTarget) {
558 return SkBitmap::kNo_Config;
559 }
560
561 bool isOpaque;
562 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
563}
564
565void SkGpuDevice::clear(SkColor color) {
566 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
567 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
568 fNeedClear = false;
569}
570
571void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
572 CHECK_SHOULD_DRAW(draw, false);
573
574 GrPaint grPaint;
575 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
576 return;
577 }
578
579 fContext->drawPaint(grPaint);
580}
581
582// must be in SkCanvas::PointMode order
583static const GrPrimitiveType gPointMode2PrimtiveType[] = {
584 kPoints_GrPrimitiveType,
585 kLines_GrPrimitiveType,
586 kLineStrip_GrPrimitiveType
587};
588
589void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
590 size_t count, const SkPoint pts[], const SkPaint& paint) {
591 CHECK_FOR_ANNOTATION(paint);
592 CHECK_SHOULD_DRAW(draw, false);
593
594 SkScalar width = paint.getStrokeWidth();
595 if (width < 0) {
596 return;
597 }
598
599 // we only handle hairlines and paints without path effects or mask filters,
600 // else we let the SkDraw call our drawPath()
601 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
602 draw.drawPoints(mode, count, pts, paint, true);
603 return;
604 }
605
606 GrPaint grPaint;
607 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
608 return;
609 }
610
611 fContext->drawVertices(grPaint,
612 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000613 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000614 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000615 NULL,
616 NULL,
617 NULL,
618 0);
619}
620
621///////////////////////////////////////////////////////////////////////////////
622
623void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
624 const SkPaint& paint) {
625 CHECK_FOR_ANNOTATION(paint);
626 CHECK_SHOULD_DRAW(draw, false);
627
628 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
629 SkScalar width = paint.getStrokeWidth();
630
631 /*
632 We have special code for hairline strokes, miter-strokes, bevel-stroke
633 and fills. Anything else we just call our path code.
634 */
635 bool usePath = doStroke && width > 0 &&
636 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
637 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
638 // another two reasons we might need to call drawPath...
639 if (paint.getMaskFilter() || paint.getPathEffect()) {
640 usePath = true;
641 }
642 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
643#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
644 if (doStroke) {
645#endif
646 usePath = true;
647#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
648 } else {
649 usePath = !fContext->getMatrix().preservesRightAngles();
650 }
651#endif
652 }
653 // until we can both stroke and fill rectangles
654 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
655 usePath = true;
656 }
657
658 if (usePath) {
659 SkPath path;
660 path.addRect(rect);
661 this->drawPath(draw, path, paint, NULL, true);
662 return;
663 }
664
665 GrPaint grPaint;
666 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
667 return;
668 }
669
670 if (!doStroke) {
671 fContext->drawRect(grPaint, rect);
672 } else {
673 SkStrokeRec stroke(paint);
674 fContext->drawRect(grPaint, rect, &stroke);
675 }
676}
677
678///////////////////////////////////////////////////////////////////////////////
679
680void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
681 const SkPaint& paint) {
682 CHECK_FOR_ANNOTATION(paint);
683 CHECK_SHOULD_DRAW(draw, false);
684
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000685 GrPaint grPaint;
686 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
687 return;
688 }
689
690 SkStrokeRec stroke(paint);
691 if (paint.getMaskFilter()) {
692 // try to hit the fast path for drawing filtered round rects
693
694 SkRRect devRRect;
695 if (rect.transform(fContext->getMatrix(), &devRRect)) {
696 if (devRRect.allCornersCircular()) {
697 SkRect maskRect;
698 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
699 draw.fClip->getBounds(),
700 fContext->getMatrix(),
701 &maskRect)) {
702 SkIRect finalIRect;
703 maskRect.roundOut(&finalIRect);
704 if (draw.fClip->quickReject(finalIRect)) {
705 // clipped out
706 return;
707 }
708 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
709 // nothing to draw
710 return;
711 }
712 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
713 stroke, devRRect)) {
714 return;
715 }
716 }
717
718 }
719 }
720
721 }
722
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000723 bool usePath = !rect.isSimple();
724 // another two reasons we might need to call drawPath...
725 if (paint.getMaskFilter() || paint.getPathEffect()) {
726 usePath = true;
727 }
728 // until we can rotate rrects...
729 if (!usePath && !fContext->getMatrix().rectStaysRect()) {
730 usePath = true;
731 }
732
733 if (usePath) {
734 SkPath path;
735 path.addRRect(rect);
736 this->drawPath(draw, path, paint, NULL, true);
737 return;
738 }
739
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000740 fContext->drawRRect(grPaint, rect, stroke);
741}
742
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000743/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000744
745void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
746 const SkPaint& paint) {
747 CHECK_FOR_ANNOTATION(paint);
748 CHECK_SHOULD_DRAW(draw, false);
749
750 bool usePath = false;
751 // some basic reasons we might need to call drawPath...
752 if (paint.getMaskFilter() || paint.getPathEffect()) {
753 usePath = true;
754 }
755
756 if (usePath) {
757 SkPath path;
758 path.addOval(oval);
759 this->drawPath(draw, path, paint, NULL, true);
760 return;
761 }
762
763 GrPaint grPaint;
764 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
765 return;
766 }
767 SkStrokeRec stroke(paint);
768
769 fContext->drawOval(grPaint, oval, stroke);
770}
771
772#include "SkMaskFilter.h"
773#include "SkBounder.h"
774
775///////////////////////////////////////////////////////////////////////////////
776
777// helpers for applying mask filters
778namespace {
779
780// Draw a mask using the supplied paint. Since the coverage/geometry
781// is already burnt into the mask this boils down to a rect draw.
782// Return true if the mask was successfully drawn.
783bool draw_mask(GrContext* context, const SkRect& maskRect,
784 GrPaint* grp, GrTexture* mask) {
785 GrContext::AutoMatrix am;
786 if (!am.setIdentity(context, grp)) {
787 return false;
788 }
789
790 SkMatrix matrix;
791 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
792 matrix.postIDiv(mask->width(), mask->height());
793
794 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
795 context->drawRect(*grp, maskRect);
796 return true;
797}
798
799bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
800 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
801 GrPaint* grp, SkPaint::Style style) {
802 SkMask srcM, dstM;
803
804 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
805 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
806 return false;
807 }
808 SkAutoMaskFreeImage autoSrc(srcM.fImage);
809
810 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
811 return false;
812 }
813 // this will free-up dstM when we're done (allocated in filterMask())
814 SkAutoMaskFreeImage autoDst(dstM.fImage);
815
816 if (clip.quickReject(dstM.fBounds)) {
817 return false;
818 }
819 if (bounder && !bounder->doIRect(dstM.fBounds)) {
820 return false;
821 }
822
823 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
824 // the current clip (and identity matrix) and GrPaint settings
825 GrTextureDesc desc;
826 desc.fWidth = dstM.fBounds.width();
827 desc.fHeight = dstM.fBounds.height();
828 desc.fConfig = kAlpha_8_GrPixelConfig;
829
830 GrAutoScratchTexture ast(context, desc);
831 GrTexture* texture = ast.texture();
832
833 if (NULL == texture) {
834 return false;
835 }
836 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
837 dstM.fImage, dstM.fRowBytes);
838
839 SkRect maskRect = SkRect::Make(dstM.fBounds);
840
841 return draw_mask(context, maskRect, grp, texture);
842}
843
844// Create a mask of 'devPath' and place the result in 'mask'. Return true on
845// success; false otherwise.
846bool create_mask_GPU(GrContext* context,
847 const SkRect& maskRect,
848 const SkPath& devPath,
849 const SkStrokeRec& stroke,
850 bool doAA,
851 GrAutoScratchTexture* mask) {
852 GrTextureDesc desc;
853 desc.fFlags = kRenderTarget_GrTextureFlagBit;
854 desc.fWidth = SkScalarCeilToInt(maskRect.width());
855 desc.fHeight = SkScalarCeilToInt(maskRect.height());
856 // We actually only need A8, but it often isn't supported as a
857 // render target so default to RGBA_8888
858 desc.fConfig = kRGBA_8888_GrPixelConfig;
859 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
860 desc.fConfig = kAlpha_8_GrPixelConfig;
861 }
862
863 mask->set(context, desc);
864 if (NULL == mask->texture()) {
865 return false;
866 }
867
868 GrTexture* maskTexture = mask->texture();
869 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
870
871 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
872 GrContext::AutoClip ac(context, clipRect);
873
874 context->clear(NULL, 0x0, true);
875
876 GrPaint tempPaint;
877 if (doAA) {
878 tempPaint.setAntiAlias(true);
879 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
880 // blend coeff of zero requires dual source blending support in order
881 // to properly blend partially covered pixels. This means the AA
882 // code path may not be taken. So we use a dst blend coeff of ISA. We
883 // could special case AA draws to a dst surface with known alpha=0 to
884 // use a zero dst coeff when dual source blending isn't available.
885 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
886 }
887
888 GrContext::AutoMatrix am;
889
890 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
891 SkMatrix translate;
892 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
893 am.set(context, translate);
894 context->drawPath(tempPaint, devPath, stroke);
895 return true;
896}
897
898SkBitmap wrap_texture(GrTexture* texture) {
reed@google.combf790232013-12-13 19:45:58 +0000899 SkImageInfo info;
900 texture->asImageInfo(&info);
901
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000902 SkBitmap result;
reed@google.combf790232013-12-13 19:45:58 +0000903 result.setConfig(info);
904 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000905 return result;
906}
907
908};
909
910void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
911 const SkPaint& paint, const SkMatrix* prePathMatrix,
912 bool pathIsMutable) {
913 CHECK_FOR_ANNOTATION(paint);
914 CHECK_SHOULD_DRAW(draw, false);
915
916 GrPaint grPaint;
917 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
918 return;
919 }
920
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000921 // If we have a prematrix, apply it to the path, optimizing for the case
922 // where the original path can in fact be modified in place (even though
923 // its parameter type is const).
924 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000925 SkTLazy<SkPath> tmpPath;
926 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000927
928 if (prePathMatrix) {
929 SkPath* result = pathPtr;
930
931 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000932 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933 pathIsMutable = true;
934 }
935 // should I push prePathMatrix on our MV stack temporarily, instead
936 // of applying it here? See SkDraw.cpp
937 pathPtr->transform(*prePathMatrix, result);
938 pathPtr = result;
939 }
940 // at this point we're done with prePathMatrix
941 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
942
943 SkStrokeRec stroke(paint);
944 SkPathEffect* pathEffect = paint.getPathEffect();
945 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000946 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000947 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000948 pathPtr = effectPath.get();
949 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000950 }
951
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000952 if (paint.getMaskFilter()) {
953 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000954 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
955 if (stroke.applyToPath(strokedPath, *pathPtr)) {
956 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000957 pathIsMutable = true;
958 stroke.setFillStyle();
959 }
960 }
961
962 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000963 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000964
965 // transform the path into device space
966 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000967
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000968 SkRect maskRect;
969 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
970 draw.fClip->getBounds(),
971 fContext->getMatrix(),
972 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000973 // The context's matrix may change while creating the mask, so save the CTM here to
974 // pass to filterMaskGPU.
975 const SkMatrix ctm = fContext->getMatrix();
976
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000977 SkIRect finalIRect;
978 maskRect.roundOut(&finalIRect);
979 if (draw.fClip->quickReject(finalIRect)) {
980 // clipped out
981 return;
982 }
983 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
984 // nothing to draw
985 return;
986 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000987
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000988 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000989 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000990 // the mask filter was able to draw itself directly, so there's nothing
991 // left to do.
992 return;
993 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000994
995 GrAutoScratchTexture mask;
996
997 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
998 grPaint.isAntiAlias(), &mask)) {
999 GrTexture* filtered;
1000
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +00001001 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +00001002 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001003 // filterMaskGPU gives us ownership of a ref to the result
1004 SkAutoTUnref<GrTexture> atu(filtered);
1005
1006 // If the scratch texture that we used as the filter src also holds the filter
1007 // result then we must detach so that this texture isn't recycled for a later
1008 // draw.
1009 if (filtered == mask.texture()) {
1010 mask.detach();
1011 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
1012 }
1013
1014 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
1015 // This path is completely drawn
1016 return;
1017 }
1018 }
1019 }
1020 }
1021
1022 // draw the mask on the CPU - this is a fallthrough path in case the
1023 // GPU path fails
1024 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
1025 SkPaint::kFill_Style;
1026 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
1027 *draw.fClip, draw.fBounder, &grPaint, style);
1028 return;
1029 }
1030
1031 fContext->drawPath(grPaint, *pathPtr, stroke);
1032}
1033
1034static const int kBmpSmallTileSize = 1 << 10;
1035
1036static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
1037 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
1038 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
1039 return tilesX * tilesY;
1040}
1041
1042static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
1043 if (maxTileSize <= kBmpSmallTileSize) {
1044 return maxTileSize;
1045 }
1046
1047 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
1048 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
1049
1050 maxTileTotalTileSize *= maxTileSize * maxTileSize;
1051 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
1052
1053 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
1054 return kBmpSmallTileSize;
1055 } else {
1056 return maxTileSize;
1057 }
1058}
1059
1060// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
1061// pixels from the bitmap are necessary.
1062static void determine_clipped_src_rect(const GrContext* context,
1063 const SkBitmap& bitmap,
1064 const SkRect* srcRectPtr,
1065 SkIRect* clippedSrcIRect) {
1066 const GrClipData* clip = context->getClip();
1067 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
1068 SkMatrix inv;
1069 if (!context->getMatrix().invert(&inv)) {
1070 clippedSrcIRect->setEmpty();
1071 return;
1072 }
1073 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
1074 inv.mapRect(&clippedSrcRect);
1075 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001076 // we've setup src space 0,0 to map to the top left of the src rect.
1077 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001078 if (!clippedSrcRect.intersect(*srcRectPtr)) {
1079 clippedSrcIRect->setEmpty();
1080 return;
1081 }
1082 }
1083 clippedSrcRect.roundOut(clippedSrcIRect);
1084 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1085 if (!clippedSrcIRect->intersect(bmpBounds)) {
1086 clippedSrcIRect->setEmpty();
1087 }
1088}
1089
1090bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1091 const GrTextureParams& params,
1092 const SkRect* srcRectPtr,
1093 int maxTileSize,
1094 int* tileSize,
1095 SkIRect* clippedSrcRect) const {
1096 // if bitmap is explictly texture backed then just use the texture
1097 if (NULL != bitmap.getTexture()) {
1098 return false;
1099 }
1100
1101 // if it's larger than the max tile size, then we have no choice but tiling.
1102 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
1103 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1104 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
1105 return true;
1106 }
1107
1108 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
1109 return false;
1110 }
1111
1112 // if the entire texture is already in our cache then no reason to tile it
1113 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
1114 return false;
1115 }
1116
1117 // At this point we know we could do the draw by uploading the entire bitmap
1118 // as a texture. However, if the texture would be large compared to the
1119 // cache size and we don't require most of it for this draw then tile to
1120 // reduce the amount of upload and cache spill.
1121
1122 // assumption here is that sw bitmap size is a good proxy for its size as
1123 // a texture
1124 size_t bmpSize = bitmap.getSize();
1125 size_t cacheSize;
1126 fContext->getTextureCacheLimits(NULL, &cacheSize);
1127 if (bmpSize < cacheSize / 2) {
1128 return false;
1129 }
1130
1131 // Figure out how much of the src we will need based on the src rect and clipping.
1132 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1133 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1134 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
1135 kBmpSmallTileSize * kBmpSmallTileSize;
1136
1137 return usedTileBytes < 2 * bmpSize;
1138}
1139
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001140void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001141 const SkBitmap& bitmap,
1142 const SkMatrix& m,
1143 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001144 SkMatrix concat;
1145 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1146 if (!m.isIdentity()) {
1147 concat.setConcat(*draw->fMatrix, m);
1148 draw.writable()->fMatrix = &concat;
1149 }
1150 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001151}
1152
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001153// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001154// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1155// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001156static inline void clamped_outset_with_offset(SkIRect* iRect,
1157 int outset,
1158 SkPoint* offset,
1159 const SkIRect& clamp) {
1160 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001161
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001162 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1163 if (leftClampDelta > 0) {
1164 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001165 iRect->fLeft = clamp.fLeft;
1166 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001167 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001168 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001169
1170 int topClampDelta = clamp.fTop - iRect->fTop;
1171 if (topClampDelta > 0) {
1172 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001173 iRect->fTop = clamp.fTop;
1174 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001175 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001176 }
1177
1178 if (iRect->fRight > clamp.fRight) {
1179 iRect->fRight = clamp.fRight;
1180 }
1181 if (iRect->fBottom > clamp.fBottom) {
1182 iRect->fBottom = clamp.fBottom;
1183 }
1184}
1185
1186void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1187 const SkBitmap& bitmap,
1188 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001189 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001190 const SkPaint& paint,
1191 SkCanvas::DrawBitmapRectFlags flags) {
1192 CHECK_SHOULD_DRAW(draw, false);
1193
1194 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001195 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001196 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1197 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001198 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001199 SkScalar w = SkIntToScalar(bitmap.width());
1200 SkScalar h = SkIntToScalar(bitmap.height());
1201 dstSize.fWidth = w;
1202 dstSize.fHeight = h;
1203 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001204 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001205 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001206 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001207 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001208 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001209 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1210 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1211 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1212 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001213 }
1214
1215 if (paint.getMaskFilter()){
1216 // Convert the bitmap to a shader so that the rect can be drawn
1217 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001218 SkBitmap tmp; // subset of bitmap, if necessary
1219 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001220 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001221 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001222 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1223 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1224 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001225 // In bleed mode we position and trim the bitmap based on the src rect which is
1226 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1227 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1228 // compensate.
1229 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1230 SkIRect iSrc;
1231 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001232
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001233 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1234 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001235
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001236 if (!bitmap.extractSubset(&tmp, iSrc)) {
1237 return; // extraction failed
1238 }
1239 bitmapPtr = &tmp;
1240 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001241
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001242 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001243 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001244 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001245 } else {
1246 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001247 }
1248
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001249 SkPaint paintWithShader(paint);
1250 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001251 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001252 paintWithShader.getShader()->setLocalMatrix(localM);
1253 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1254 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001255
1256 return;
1257 }
1258
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001259 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1260 // the view matrix rather than a local matrix.
1261 SkMatrix m;
1262 m.setScale(dstSize.fWidth / srcRect.width(),
1263 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001264 fContext->concatMatrix(m);
1265
1266 GrTextureParams params;
1267 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1268 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001269
1270 int tileFilterPad;
1271 bool doBicubic = false;
1272
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001273 switch(paintFilterLevel) {
1274 case SkPaint::kNone_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001275 tileFilterPad = 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001276 textureFilterMode = GrTextureParams::kNone_FilterMode;
1277 break;
1278 case SkPaint::kLow_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001279 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001280 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1281 break;
1282 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001283 tileFilterPad = 1;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001284 if (fContext->getMatrix().getMinStretch() < SK_Scalar1) {
1285 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1286 } else {
1287 // Don't trigger MIP level generation unnecessarily.
1288 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1289 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001290 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001291 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001292 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001293 if (fContext->getMatrix().getMinStretch() >= SK_Scalar1) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001294 // We will install an effect that does the filtering in the shader.
1295 textureFilterMode = GrTextureParams::kNone_FilterMode;
1296 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1297 doBicubic = true;
1298 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001299 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1300 tileFilterPad = 1;
1301 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001302 break;
1303 default:
1304 SkErrorInternals::SetError( kInvalidPaint_SkError,
1305 "Sorry, I don't understand the filtering "
1306 "mode you asked for. Falling back to "
1307 "MIPMaps.");
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001308 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001309 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1310 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001311 }
1312
1313 params.setFilterMode(textureFilterMode);
1314
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001315 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001316 int tileSize;
1317
1318 SkIRect clippedSrcRect;
1319 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1320 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001321 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1322 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001323 } else {
1324 // take the simple case
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001325 this->internalDrawBitmap(bitmap, srcRect, params, paint, flags, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001326 }
1327}
1328
1329// Break 'bitmap' into several tiles to draw it since it has already
1330// been determined to be too large to fit in VRAM
1331void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1332 const SkRect& srcRect,
1333 const SkIRect& clippedSrcIRect,
1334 const GrTextureParams& params,
1335 const SkPaint& paint,
1336 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001337 int tileSize,
1338 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001339 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1340
1341 int nx = bitmap.width() / tileSize;
1342 int ny = bitmap.height() / tileSize;
1343 for (int x = 0; x <= nx; x++) {
1344 for (int y = 0; y <= ny; y++) {
1345 SkRect tileR;
1346 tileR.set(SkIntToScalar(x * tileSize),
1347 SkIntToScalar(y * tileSize),
1348 SkIntToScalar((x + 1) * tileSize),
1349 SkIntToScalar((y + 1) * tileSize));
1350
1351 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1352 continue;
1353 }
1354
1355 if (!tileR.intersect(srcRect)) {
1356 continue;
1357 }
1358
1359 SkBitmap tmpB;
1360 SkIRect iTileR;
1361 tileR.roundOut(&iTileR);
1362 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1363 SkIntToScalar(iTileR.fTop));
1364
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001365 // Adjust the context matrix to draw at the right x,y in device space
1366 SkMatrix tmpM;
1367 GrContext::AutoMatrix am;
1368 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1369 am.setPreConcat(fContext, tmpM);
1370
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001371 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001372 SkIRect iClampRect;
1373
1374 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1375 // In bleed mode we want to always expand the tile on all edges
1376 // but stay within the bitmap bounds
1377 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1378 } else {
1379 // In texture-domain/clamp mode we only want to expand the
1380 // tile on edges interior to "srcRect" (i.e., we want to
1381 // not bleed across the original clamped edges)
1382 srcRect.roundOut(&iClampRect);
1383 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001384 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1385 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001386 }
1387
1388 if (bitmap.extractSubset(&tmpB, iTileR)) {
1389 // now offset it to make it "local" to our tmp bitmap
1390 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001391
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001392 this->internalDrawBitmap(tmpB, tileR, params, paint, flags, bicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001393 }
1394 }
1395 }
1396}
1397
1398static bool has_aligned_samples(const SkRect& srcRect,
1399 const SkRect& transformedRect) {
1400 // detect pixel disalignment
1401 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1402 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1403 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1404 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1405 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1406 COLOR_BLEED_TOLERANCE &&
1407 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1408 COLOR_BLEED_TOLERANCE) {
1409 return true;
1410 }
1411 return false;
1412}
1413
1414static bool may_color_bleed(const SkRect& srcRect,
1415 const SkRect& transformedRect,
1416 const SkMatrix& m) {
1417 // Only gets called if has_aligned_samples returned false.
1418 // So we can assume that sampling is axis aligned but not texel aligned.
1419 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1420 SkRect innerSrcRect(srcRect), innerTransformedRect,
1421 outerTransformedRect(transformedRect);
1422 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1423 m.mapRect(&innerTransformedRect, innerSrcRect);
1424
1425 // The gap between outerTransformedRect and innerTransformedRect
1426 // represents the projection of the source border area, which is
1427 // problematic for color bleeding. We must check whether any
1428 // destination pixels sample the border area.
1429 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1430 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1431 SkIRect outer, inner;
1432 outerTransformedRect.round(&outer);
1433 innerTransformedRect.round(&inner);
1434 // If the inner and outer rects round to the same result, it means the
1435 // border does not overlap any pixel centers. Yay!
1436 return inner != outer;
1437}
1438
1439
1440/*
1441 * This is called by drawBitmap(), which has to handle images that may be too
1442 * large to be represented by a single texture.
1443 *
1444 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1445 * and that non-texture portion of the GrPaint has already been setup.
1446 */
1447void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1448 const SkRect& srcRect,
1449 const GrTextureParams& params,
1450 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001451 SkCanvas::DrawBitmapRectFlags flags,
1452 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001453 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1454 bitmap.height() <= fContext->getMaxTextureSize());
1455
1456 GrTexture* texture;
1457 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1458 if (NULL == texture) {
1459 return;
1460 }
1461
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001462 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001463 SkRect paintRect;
1464 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1465 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1466 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1467 SkScalarMul(srcRect.fTop, hInv),
1468 SkScalarMul(srcRect.fRight, wInv),
1469 SkScalarMul(srcRect.fBottom, hInv));
1470
1471 bool needsTextureDomain = false;
1472 if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) &&
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001473 (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode)) {
1474 // Need texture domain if drawing a sub rect
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001475 needsTextureDomain = srcRect.width() < bitmap.width() ||
1476 srcRect.height() < bitmap.height();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001477 if (!bicubic && needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001478 const SkMatrix& matrix = fContext->getMatrix();
1479 // sampling is axis-aligned
1480 SkRect transformedRect;
1481 matrix.mapRect(&transformedRect, srcRect);
1482
1483 if (has_aligned_samples(srcRect, transformedRect)) {
1484 // We could also turn off filtering here (but we already did a cache lookup with
1485 // params).
1486 needsTextureDomain = false;
1487 } else {
1488 needsTextureDomain = may_color_bleed(srcRect, transformedRect, matrix);
1489 }
1490 }
1491 }
1492
1493 SkRect textureDomain = SkRect::MakeEmpty();
1494 SkAutoTUnref<GrEffectRef> effect;
1495 if (needsTextureDomain) {
1496 // Use a constrained texture domain to avoid color bleeding
1497 SkScalar left, top, right, bottom;
1498 if (srcRect.width() > SK_Scalar1) {
1499 SkScalar border = SK_ScalarHalf / texture->width();
1500 left = paintRect.left() + border;
1501 right = paintRect.right() - border;
1502 } else {
1503 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1504 }
1505 if (srcRect.height() > SK_Scalar1) {
1506 SkScalar border = SK_ScalarHalf / texture->height();
1507 top = paintRect.top() + border;
1508 bottom = paintRect.bottom() - border;
1509 } else {
1510 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1511 }
1512 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001513 if (bicubic) {
1514 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1515 } else {
1516 effect.reset(GrTextureDomainEffect::Create(texture,
1517 SkMatrix::I(),
1518 textureDomain,
1519 GrTextureDomain::kClamp_Mode,
1520 params.filterMode()));
1521 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001522 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001523 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1524 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1525 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001526 } else {
1527 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1528 }
1529
1530 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1531 // the rest from the SkPaint.
1532 GrPaint grPaint;
1533 grPaint.addColorEffect(effect);
1534 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1535 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1536 return;
1537 }
1538
1539 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1540}
1541
1542static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001543 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001544 int w, int h, const SkImageFilter::Context& ctx,
1545 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001546 SkASSERT(filter);
1547 SkDeviceImageFilterProxy proxy(device);
1548
1549 if (filter->canFilterImageGPU()) {
1550 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1551 // filter. Also set the clip wide open and the matrix to identity.
1552 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001553 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001554 } else {
1555 return false;
1556 }
1557}
1558
1559void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1560 int left, int top, const SkPaint& paint) {
1561 // drawSprite is defined to be in device coords.
1562 CHECK_SHOULD_DRAW(draw, true);
1563
1564 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1565 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1566 return;
1567 }
1568
1569 int w = bitmap.width();
1570 int h = bitmap.height();
1571
1572 GrTexture* texture;
1573 // draw sprite uses the default texture params
1574 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1575
1576 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001577 // This bitmap will own the filtered result as a texture.
1578 SkBitmap filteredBitmap;
1579
1580 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001581 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001582 SkMatrix matrix(*draw.fMatrix);
1583 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001584 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1585 SkImageFilter::Context ctx(matrix, clipBounds);
1586 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001587 &offset)) {
1588 texture = (GrTexture*) filteredBitmap.getTexture();
1589 w = filteredBitmap.width();
1590 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001591 left += offset.x();
1592 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001593 } else {
1594 return;
1595 }
1596 }
1597
1598 GrPaint grPaint;
1599 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1600
1601 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1602 return;
1603 }
1604
1605 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001606 SkRect::MakeXYWH(SkIntToScalar(left),
1607 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001608 SkIntToScalar(w),
1609 SkIntToScalar(h)),
1610 SkRect::MakeXYWH(0,
1611 0,
1612 SK_Scalar1 * w / texture->width(),
1613 SK_Scalar1 * h / texture->height()));
1614}
1615
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001616void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001617 const SkRect* src, const SkRect& dst,
1618 const SkPaint& paint,
1619 SkCanvas::DrawBitmapRectFlags flags) {
1620 SkMatrix matrix;
1621 SkRect bitmapBounds, tmpSrc;
1622
1623 bitmapBounds.set(0, 0,
1624 SkIntToScalar(bitmap.width()),
1625 SkIntToScalar(bitmap.height()));
1626
1627 // Compute matrix from the two rectangles
1628 if (NULL != src) {
1629 tmpSrc = *src;
1630 } else {
1631 tmpSrc = bitmapBounds;
1632 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001633
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001634 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1635
1636 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1637 if (NULL != src) {
1638 if (!bitmapBounds.contains(tmpSrc)) {
1639 if (!tmpSrc.intersect(bitmapBounds)) {
1640 return; // nothing to draw
1641 }
1642 }
1643 }
1644
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001645 SkRect tmpDst;
1646 matrix.mapRect(&tmpDst, tmpSrc);
1647
1648 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1649 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1650 // Translate so that tempDst's top left is at the origin.
1651 matrix = *origDraw.fMatrix;
1652 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1653 draw.writable()->fMatrix = &matrix;
1654 }
1655 SkSize dstSize;
1656 dstSize.fWidth = tmpDst.width();
1657 dstSize.fHeight = tmpDst.height();
1658
1659 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001660}
1661
1662void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1663 int x, int y, const SkPaint& paint) {
1664 // clear of the source device must occur before CHECK_SHOULD_DRAW
1665 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1666 if (dev->fNeedClear) {
1667 // TODO: could check here whether we really need to draw at all
1668 dev->clear(0x0);
1669 }
1670
1671 // drawDevice is defined to be in device coords.
1672 CHECK_SHOULD_DRAW(draw, true);
1673
1674 GrRenderTarget* devRT = dev->accessRenderTarget();
1675 GrTexture* devTex;
1676 if (NULL == (devTex = devRT->asTexture())) {
1677 return;
1678 }
1679
1680 const SkBitmap& bm = dev->accessBitmap(false);
1681 int w = bm.width();
1682 int h = bm.height();
1683
1684 SkImageFilter* filter = paint.getImageFilter();
1685 // This bitmap will own the filtered result as a texture.
1686 SkBitmap filteredBitmap;
1687
1688 if (NULL != filter) {
1689 SkIPoint offset = SkIPoint::Make(0, 0);
1690 SkMatrix matrix(*draw.fMatrix);
1691 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001692 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
1693 SkImageFilter::Context ctx(matrix, clipBounds);
1694 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001695 &offset)) {
1696 devTex = filteredBitmap.getTexture();
1697 w = filteredBitmap.width();
1698 h = filteredBitmap.height();
1699 x += offset.fX;
1700 y += offset.fY;
1701 } else {
1702 return;
1703 }
1704 }
1705
1706 GrPaint grPaint;
1707 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1708
1709 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1710 return;
1711 }
1712
1713 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1714 SkIntToScalar(y),
1715 SkIntToScalar(w),
1716 SkIntToScalar(h));
1717
1718 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1719 // scratch texture).
1720 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1721 SK_Scalar1 * h / devTex->height());
1722
1723 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1724}
1725
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001726bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001727 return filter->canFilterImageGPU();
1728}
1729
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001730bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001731 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001732 SkBitmap* result, SkIPoint* offset) {
1733 // want explicitly our impl, so guard against a subclass of us overriding it
1734 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1735 return false;
1736 }
1737
1738 SkAutoLockPixels alp(src, !src.getTexture());
1739 if (!src.getTexture() && !src.readyToDraw()) {
1740 return false;
1741 }
1742
1743 GrTexture* texture;
1744 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1745 // must be pushed upstack.
1746 SkAutoCachedTexture act(this, src, NULL, &texture);
1747
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001748 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1749 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001750}
1751
1752///////////////////////////////////////////////////////////////////////////////
1753
1754// must be in SkCanvas::VertexMode order
1755static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1756 kTriangles_GrPrimitiveType,
1757 kTriangleStrip_GrPrimitiveType,
1758 kTriangleFan_GrPrimitiveType,
1759};
1760
1761void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1762 int vertexCount, const SkPoint vertices[],
1763 const SkPoint texs[], const SkColor colors[],
1764 SkXfermode* xmode,
1765 const uint16_t indices[], int indexCount,
1766 const SkPaint& paint) {
1767 CHECK_SHOULD_DRAW(draw, false);
1768
1769 GrPaint grPaint;
1770 // we ignore the shader if texs is null.
1771 if (NULL == texs) {
1772 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
1773 return;
1774 }
1775 } else {
1776 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
1777 return;
1778 }
1779 }
1780
1781 if (NULL != xmode && NULL != texs && NULL != colors) {
1782 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1783 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1784#if 0
1785 return
1786#endif
1787 }
1788 }
1789
1790 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1791 if (NULL != colors) {
1792 // need to convert byte order and from non-PM to PM
1793 convertedColors.reset(vertexCount);
1794 for (int i = 0; i < vertexCount; ++i) {
1795 convertedColors[i] = SkColor2GrColor(colors[i]);
1796 }
1797 colors = convertedColors.get();
1798 }
1799 fContext->drawVertices(grPaint,
1800 gVertexMode2PrimitiveType[vmode],
1801 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001802 vertices,
1803 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001804 colors,
1805 indices,
1806 indexCount);
1807}
1808
1809///////////////////////////////////////////////////////////////////////////////
1810
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001811void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1812 size_t byteLength, SkScalar x, SkScalar y,
1813 const SkPaint& paint) {
1814 CHECK_SHOULD_DRAW(draw, false);
1815
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001816 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001817 GrPaint grPaint;
1818 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1819 return;
1820 }
1821
1822 SkDEBUGCODE(this->validate();)
1823
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001824 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1825 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001826 GrPaint grPaint;
1827 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1828 return;
1829 }
1830
1831 SkDEBUGCODE(this->validate();)
1832
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001833 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001834 } else {
1835 // this guy will just call our drawPath()
1836 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001837 }
1838}
1839
1840void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1841 size_t byteLength, const SkScalar pos[],
1842 SkScalar constY, int scalarsPerPos,
1843 const SkPaint& paint) {
1844 CHECK_SHOULD_DRAW(draw, false);
1845
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001846 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001847 GrPaint grPaint;
1848 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1849 return;
1850 }
1851
1852 SkDEBUGCODE(this->validate();)
1853
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001854 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001855 constY, scalarsPerPos);
1856 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001857 GrPaint grPaint;
1858 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1859 return;
1860 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001861
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001862 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001863
1864 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001865 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001866 } else {
1867 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1868 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001869 }
1870}
1871
1872void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1873 size_t len, const SkPath& path,
1874 const SkMatrix* m, const SkPaint& paint) {
1875 CHECK_SHOULD_DRAW(draw, false);
1876
1877 SkASSERT(draw.fDevice == this);
1878 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1879}
1880
1881///////////////////////////////////////////////////////////////////////////////
1882
1883bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1884 if (!paint.isLCDRenderText()) {
1885 // we're cool with the paint as is
1886 return false;
1887 }
1888
1889 if (paint.getShader() ||
1890 paint.getXfermode() || // unless its srcover
1891 paint.getMaskFilter() ||
1892 paint.getRasterizer() ||
1893 paint.getColorFilter() ||
1894 paint.getPathEffect() ||
1895 paint.isFakeBoldText() ||
1896 paint.getStyle() != SkPaint::kFill_Style) {
1897 // turn off lcd
1898 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1899 flags->fHinting = paint.getHinting();
1900 return true;
1901 }
1902 // we're cool with the paint as is
1903 return false;
1904}
1905
1906void SkGpuDevice::flush() {
1907 DO_DEFERRED_CLEAR();
1908 fContext->resolveRenderTarget(fRenderTarget);
1909}
1910
1911///////////////////////////////////////////////////////////////////////////////
1912
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001913SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001914 GrTextureDesc desc;
1915 desc.fConfig = fRenderTarget->config();
1916 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001917 desc.fWidth = info.width();
1918 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001919 desc.fSampleCnt = fRenderTarget->numSamples();
1920
1921 SkAutoTUnref<GrTexture> texture;
1922 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001923 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001924
1925#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1926 // layers are never draw in repeat modes, so we can request an approx
1927 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001928 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001929 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1930 GrContext::kApprox_ScratchTexMatch :
1931 GrContext::kExact_ScratchTexMatch;
1932 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1933#else
1934 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1935#endif
1936 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001937 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001938 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001939 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1940 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001941 return NULL;
1942 }
1943}
1944
reed@google.com76f10a32014-02-05 15:32:21 +00001945SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1946 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1947}
1948
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001949// In the future this may not be a static method if we need to incorporate the
1950// clip and matrix state into the key
1951SkPicture::AccelData::Key SkGpuDevice::ComputeAccelDataKey() {
1952 static const SkPicture::AccelData::Key gGPUID = SkPicture::AccelData::GenerateDomain();
1953
1954 return gGPUID;
1955}
1956
1957void SkGpuDevice::EXPERIMENTAL_optimize(SkPicture* picture) {
1958 SkPicture::AccelData::Key key = ComputeAccelDataKey();
1959
1960 GPUAccelData* data = SkNEW_ARGS(GPUAccelData, (key));
1961
1962 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001963
1964 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001965}
1966
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001967bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkPicture* picture) {
1968
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001969 SkPicture::AccelData::Key key = ComputeAccelDataKey();
1970
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001971 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001972 if (NULL == data) {
1973 return false;
1974 }
1975
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001976 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001977
1978//#define SK_PRINT_PULL_FORWARD_INFO 1
1979
1980#ifdef SK_PRINT_PULL_FORWARD_INFO
1981 static bool gPrintedAccelData = false;
1982
1983 if (!gPrintedAccelData) {
1984 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1985 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1986
skia.committer@gmail.com2c48ee82014-04-01 03:07:47 +00001987 SkDebugf("%d: Width: %d Height: %d SL: %d R: %d hasNestedLayers: %s\n",
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001988 i,
skia.committer@gmail.com2c48ee82014-04-01 03:07:47 +00001989 info.fSize.fWidth,
1990 info.fSize.fHeight,
1991 info.fSaveLayerOpID,
1992 info.fRestoreOpID,
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001993 info.fHasNestedLayers ? "T" : "F");
1994 }
1995 gPrintedAccelData = true;
1996 }
1997#endif
1998
1999 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
2000 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
2001 pullForward[i] = false;
2002 }
2003
2004 SkIRect clip;
2005
2006 fClipData.getConservativeBounds(this->width(), this->height(), &clip, NULL);
2007
2008 SkMatrix inv;
2009 if (!fContext->getMatrix().invert(&inv)) {
2010 return false;
2011 }
2012
2013 SkRect r = SkRect::Make(clip);
2014 inv.mapRect(&r);
2015 r.roundOut(&clip);
2016
2017 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(clip);
2018
2019#ifdef SK_PRINT_PULL_FORWARD_INFO
2020 SkDebugf("rect: %d %d %d %d\n", clip.fLeft, clip.fTop, clip.fRight, clip.fBottom);
2021#endif
2022
2023 for (int i = 0; i < ops.numOps(); ++i) {
2024 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
2025 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
2026
2027 if (ops.offset(i) > info.fSaveLayerOpID && ops.offset(i) < info.fRestoreOpID) {
2028 pullForward[j] = true;
2029 }
2030 }
2031 }
2032
2033#ifdef SK_PRINT_PULL_FORWARD_INFO
2034 SkDebugf("Need SaveLayers: ");
2035 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
2036 if (pullForward[i]) {
robertphillips@google.come930a072014-04-03 00:34:27 +00002037 const GrAtlasedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
2038
2039 SkDebugf("%d (%d), ", i, layer->layerID());
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00002040 }
2041 }
2042 SkDebugf("\n");
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002043#endif
2044
2045 return false;
2046}