blob: d87b1dc5e984f77b00d9dd8b772325a28af1f87e [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"
robertphillips@google.combeb1af22014-05-07 21:31:09 +000031#include "SkPicturePlayback.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000032#include "SkRRect.h"
33#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000034#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000035#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000036#include "SkUtils.h"
37#include "SkErrorInternals.h"
38
39#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
40
41#if 0
42 extern bool (*gShouldDrawProc)();
43 #define CHECK_SHOULD_DRAW(draw, forceI) \
44 do { \
45 if (gShouldDrawProc && !gShouldDrawProc()) return; \
46 this->prepareDraw(draw, forceI); \
47 } while (0)
48#else
49 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
50#endif
51
52// This constant represents the screen alignment criterion in texels for
53// requiring texture domain clamping to prevent color bleeding when drawing
54// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000055#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000056
57#define DO_DEFERRED_CLEAR() \
58 do { \
59 if (fNeedClear) { \
60 this->clear(SK_ColorTRANSPARENT); \
61 } \
62 } while (false) \
63
64///////////////////////////////////////////////////////////////////////////////
65
66#define CHECK_FOR_ANNOTATION(paint) \
67 do { if (paint.getAnnotation()) { return; } } while (0)
68
69///////////////////////////////////////////////////////////////////////////////
70
71
72class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
73public:
74 SkAutoCachedTexture()
75 : fDevice(NULL)
76 , fTexture(NULL) {
77 }
78
79 SkAutoCachedTexture(SkGpuDevice* device,
80 const SkBitmap& bitmap,
81 const GrTextureParams* params,
82 GrTexture** texture)
83 : fDevice(NULL)
84 , fTexture(NULL) {
85 SkASSERT(NULL != texture);
86 *texture = this->set(device, bitmap, params);
87 }
88
89 ~SkAutoCachedTexture() {
90 if (NULL != fTexture) {
91 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
92 }
93 }
94
95 GrTexture* set(SkGpuDevice* device,
96 const SkBitmap& bitmap,
97 const GrTextureParams* params) {
98 if (NULL != fTexture) {
99 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
100 fTexture = NULL;
101 }
102 fDevice = device;
103 GrTexture* result = (GrTexture*)bitmap.getTexture();
104 if (NULL == result) {
105 // Cannot return the native texture so look it up in our cache
106 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
107 result = fTexture;
108 }
109 return result;
110 }
111
112private:
113 SkGpuDevice* fDevice;
114 GrTexture* fTexture;
115};
116
117///////////////////////////////////////////////////////////////////////////////
118
119struct GrSkDrawProcs : public SkDrawProcs {
120public:
121 GrContext* fContext;
122 GrTextContext* fTextContext;
123 GrFontScaler* fFontScaler; // cached in the skia glyphcache
124};
125
126///////////////////////////////////////////////////////////////////////////////
127
128static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
129 switch (config) {
130 case kAlpha_8_GrPixelConfig:
131 *isOpaque = false;
132 return SkBitmap::kA8_Config;
133 case kRGB_565_GrPixelConfig:
134 *isOpaque = true;
135 return SkBitmap::kRGB_565_Config;
136 case kRGBA_4444_GrPixelConfig:
137 *isOpaque = false;
138 return SkBitmap::kARGB_4444_Config;
139 case kSkia8888_GrPixelConfig:
140 // we don't currently have a way of knowing whether
141 // a 8888 is opaque based on the config.
142 *isOpaque = false;
143 return SkBitmap::kARGB_8888_Config;
144 default:
145 *isOpaque = false;
146 return SkBitmap::kNo_Config;
147 }
148}
149
150/*
151 * GrRenderTarget does not know its opaqueness, only its config, so we have
152 * to make conservative guesses when we return an "equivalent" bitmap.
153 */
154static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
155 bool isOpaque;
156 SkBitmap::Config config = grConfig2skConfig(renderTarget->config(), &isOpaque);
157
158 SkBitmap bitmap;
159 bitmap.setConfig(config, renderTarget->width(), renderTarget->height(), 0,
160 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
161 return bitmap;
162}
163
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000164SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000165 SkASSERT(NULL != surface);
166 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
167 return NULL;
168 }
169 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000170 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000171 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000172 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000173 }
174}
175
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000176SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000177 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000178 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000179}
180
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000181SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000182 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000183 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000184}
185
186void SkGpuDevice::initFromRenderTarget(GrContext* context,
187 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000188 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000189 fDrawProcs = NULL;
190
191 fContext = context;
192 fContext->ref();
193
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +0000194 bool useDFFonts = !!(flags & kDFFonts_Flag);
195 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties,
196 useDFFonts));
commit-bot@chromium.org47841822014-03-27 14:19:17 +0000197 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
198
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000199 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000200 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000201
202 SkASSERT(NULL != renderTarget);
203 fRenderTarget = renderTarget;
204 fRenderTarget->ref();
205
206 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
207 // on the RT but not vice-versa.
208 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
209 // busting chrome (for a currently unknown reason).
210 GrSurface* surface = fRenderTarget->asTexture();
211 if (NULL == surface) {
212 surface = fRenderTarget;
213 }
reed@google.combf790232013-12-13 19:45:58 +0000214
215 SkImageInfo info;
216 surface->asImageInfo(&info);
bungeman@google.coma95a0662014-03-19 23:26:50 +0000217 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, SkToBool(flags & kCached_Flag)));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000218
reed@google.com672588b2014-01-08 15:42:01 +0000219 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000220}
221
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000222SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
223 int sampleCount) {
224 if (kUnknown_SkColorType == origInfo.colorType() ||
225 origInfo.width() < 0 || origInfo.height() < 0) {
226 return NULL;
227 }
228
229 SkImageInfo info = origInfo;
230 // TODO: perhas we can loosen this check now that colortype is more detailed
231 // e.g. can we support both RGBA and BGRA here?
232 if (kRGB_565_SkColorType == info.colorType()) {
233 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
234 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000235 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000236 if (kOpaque_SkAlphaType != info.alphaType()) {
237 info.fAlphaType = kPremul_SkAlphaType; // force this setting
238 }
239 }
240
241 GrTextureDesc desc;
242 desc.fFlags = kRenderTarget_GrTextureFlagBit;
243 desc.fWidth = info.width();
244 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000245 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000246 desc.fSampleCnt = sampleCount;
247
248 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
249 if (!texture.get()) {
250 return NULL;
251 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000252
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000253 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
254}
255
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000256SkGpuDevice::~SkGpuDevice() {
257 if (fDrawProcs) {
258 delete fDrawProcs;
259 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000260
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000261 delete fMainTextContext;
262 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000263
264 // The GrContext takes a ref on the target. We don't want to cause the render
265 // target to be unnecessarily kept alive.
266 if (fContext->getRenderTarget() == fRenderTarget) {
267 fContext->setRenderTarget(NULL);
268 }
269
270 if (fContext->getClip() == &fClipData) {
271 fContext->setClip(NULL);
272 }
273
274 SkSafeUnref(fRenderTarget);
275 fContext->unref();
276}
277
278///////////////////////////////////////////////////////////////////////////////
279
280void SkGpuDevice::makeRenderTargetCurrent() {
281 DO_DEFERRED_CLEAR();
282 fContext->setRenderTarget(fRenderTarget);
283}
284
285///////////////////////////////////////////////////////////////////////////////
286
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000287bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
288 int x, int y) {
289 DO_DEFERRED_CLEAR();
290
291 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000292 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000293 if (kUnknown_GrPixelConfig == config) {
294 return false;
295 }
296
297 uint32_t flags = 0;
298 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
299 flags = GrContext::kUnpremul_PixelOpsFlag;
300 }
301 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
302 config, dstPixels, dstRowBytes, flags);
303}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000304
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000305bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
306 int x, int y) {
307 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000308 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000309 if (kUnknown_GrPixelConfig == config) {
310 return false;
311 }
312 uint32_t flags = 0;
313 if (kUnpremul_SkAlphaType == info.alphaType()) {
314 flags = GrContext::kUnpremul_PixelOpsFlag;
315 }
316 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
317
318 // need to bump our genID for compatibility with clients that "know" we have a bitmap
319 this->onAccessBitmap().notifyPixelsChanged();
320
321 return true;
322}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000324const SkBitmap& SkGpuDevice::onAccessBitmap() {
325 DO_DEFERRED_CLEAR();
326 return INHERITED::onAccessBitmap();
327}
328
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000329void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
330 INHERITED::onAttachToCanvas(canvas);
331
332 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
333 fClipData.fClipStack = canvas->getClipStack();
334}
335
336void SkGpuDevice::onDetachFromCanvas() {
337 INHERITED::onDetachFromCanvas();
338 fClipData.fClipStack = NULL;
339}
340
341// call this every draw call, to ensure that the context reflects our state,
342// and not the state from some other canvas/device
343void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
344 SkASSERT(NULL != fClipData.fClipStack);
345
346 fContext->setRenderTarget(fRenderTarget);
347
348 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
349
350 if (forceIdentity) {
351 fContext->setIdentityMatrix();
352 } else {
353 fContext->setMatrix(*draw.fMatrix);
354 }
355 fClipData.fOrigin = this->getOrigin();
356
357 fContext->setClip(&fClipData);
358
359 DO_DEFERRED_CLEAR();
360}
361
362GrRenderTarget* SkGpuDevice::accessRenderTarget() {
363 DO_DEFERRED_CLEAR();
364 return fRenderTarget;
365}
366
367///////////////////////////////////////////////////////////////////////////////
368
369SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
370SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
371SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
372SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
373SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
374 shader_type_mismatch);
375SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
376 shader_type_mismatch);
377SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
378SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
379
380namespace {
381
382// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
383// justAlpha indicates that skPaint's alpha should be used rather than the color
384// Callers may subsequently modify the GrPaint. Setting constantColor indicates
385// that the final paint will draw the same color at every pixel. This allows
386// an optimization where the the color filter can be applied to the skPaint's
387// color once while converting to GrPaint and then ignored.
388inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
389 const SkPaint& skPaint,
390 bool justAlpha,
391 bool constantColor,
392 GrPaint* grPaint) {
393
394 grPaint->setDither(skPaint.isDither());
395 grPaint->setAntiAlias(skPaint.isAntiAlias());
396
397 SkXfermode::Coeff sm;
398 SkXfermode::Coeff dm;
399
400 SkXfermode* mode = skPaint.getXfermode();
401 GrEffectRef* xferEffect = NULL;
402 if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
403 if (NULL != xferEffect) {
404 grPaint->addColorEffect(xferEffect)->unref();
405 sm = SkXfermode::kOne_Coeff;
406 dm = SkXfermode::kZero_Coeff;
407 }
408 } else {
409 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
410#if 0
411 return false;
412#else
413 // Fall back to src-over
414 sm = SkXfermode::kOne_Coeff;
415 dm = SkXfermode::kISA_Coeff;
416#endif
417 }
418 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
419
420 if (justAlpha) {
421 uint8_t alpha = skPaint.getAlpha();
422 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
423 // justAlpha is currently set to true only if there is a texture,
424 // so constantColor should not also be true.
425 SkASSERT(!constantColor);
426 } else {
427 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
428 }
429
430 SkColorFilter* colorFilter = skPaint.getColorFilter();
431 if (NULL != colorFilter) {
432 // if the source color is a constant then apply the filter here once rather than per pixel
433 // in a shader.
434 if (constantColor) {
435 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
436 grPaint->setColor(SkColor2GrColor(filtered));
437 } else {
438 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
439 if (NULL != effect.get()) {
440 grPaint->addColorEffect(effect);
441 }
442 }
443 }
444
445 return true;
446}
447
448// This function is similar to skPaint2GrPaintNoShader but also converts
449// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
450// be used is set on grPaint and returned in param act. constantColor has the
451// same meaning as in skPaint2GrPaintNoShader.
452inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
453 const SkPaint& skPaint,
454 bool constantColor,
455 GrPaint* grPaint) {
456 SkShader* shader = skPaint.getShader();
457 if (NULL == shader) {
458 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
459 }
460
commit-bot@chromium.org60770572014-01-13 15:57:05 +0000461 // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state and require
462 // the shader to set a render target .
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000463 GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000464
465 // setup the shader as the first color effect on the paint
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000466 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint, NULL));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000467 if (NULL != effect.get()) {
468 grPaint->addColorEffect(effect);
469 // Now setup the rest of the paint.
470 return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
471 } else {
472 // We still don't have SkColorShader::asNewEffect() implemented.
473 SkShader::GradientInfo info;
474 SkColor color;
475
476 info.fColors = &color;
477 info.fColorOffsets = NULL;
478 info.fColorCount = 1;
479 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
480 SkPaint copy(skPaint);
481 copy.setShader(NULL);
482 // modulate the paint alpha by the shader's solid color alpha
483 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
484 copy.setColor(SkColorSetA(color, newA));
485 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
486 } else {
487 return false;
488 }
489 }
490}
491}
492
493///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000494
495SkBitmap::Config SkGpuDevice::config() const {
496 if (NULL == fRenderTarget) {
497 return SkBitmap::kNo_Config;
498 }
499
500 bool isOpaque;
501 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
502}
503
504void SkGpuDevice::clear(SkColor color) {
505 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
506 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
507 fNeedClear = false;
508}
509
510void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
511 CHECK_SHOULD_DRAW(draw, false);
512
513 GrPaint grPaint;
514 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
515 return;
516 }
517
518 fContext->drawPaint(grPaint);
519}
520
521// must be in SkCanvas::PointMode order
522static const GrPrimitiveType gPointMode2PrimtiveType[] = {
523 kPoints_GrPrimitiveType,
524 kLines_GrPrimitiveType,
525 kLineStrip_GrPrimitiveType
526};
527
528void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
529 size_t count, const SkPoint pts[], const SkPaint& paint) {
530 CHECK_FOR_ANNOTATION(paint);
531 CHECK_SHOULD_DRAW(draw, false);
532
533 SkScalar width = paint.getStrokeWidth();
534 if (width < 0) {
535 return;
536 }
537
538 // we only handle hairlines and paints without path effects or mask filters,
539 // else we let the SkDraw call our drawPath()
540 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
541 draw.drawPoints(mode, count, pts, paint, true);
542 return;
543 }
544
545 GrPaint grPaint;
546 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
547 return;
548 }
549
550 fContext->drawVertices(grPaint,
551 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000552 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000553 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000554 NULL,
555 NULL,
556 NULL,
557 0);
558}
559
560///////////////////////////////////////////////////////////////////////////////
561
562void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
563 const SkPaint& paint) {
564 CHECK_FOR_ANNOTATION(paint);
565 CHECK_SHOULD_DRAW(draw, false);
566
567 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
568 SkScalar width = paint.getStrokeWidth();
569
570 /*
571 We have special code for hairline strokes, miter-strokes, bevel-stroke
572 and fills. Anything else we just call our path code.
573 */
574 bool usePath = doStroke && width > 0 &&
575 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
576 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
577 // another two reasons we might need to call drawPath...
578 if (paint.getMaskFilter() || paint.getPathEffect()) {
579 usePath = true;
580 }
581 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
582#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
583 if (doStroke) {
584#endif
585 usePath = true;
586#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
587 } else {
588 usePath = !fContext->getMatrix().preservesRightAngles();
589 }
590#endif
591 }
592 // until we can both stroke and fill rectangles
593 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
594 usePath = true;
595 }
596
597 if (usePath) {
598 SkPath path;
599 path.addRect(rect);
600 this->drawPath(draw, path, paint, NULL, true);
601 return;
602 }
603
604 GrPaint grPaint;
605 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
606 return;
607 }
608
609 if (!doStroke) {
610 fContext->drawRect(grPaint, rect);
611 } else {
612 SkStrokeRec stroke(paint);
613 fContext->drawRect(grPaint, rect, &stroke);
614 }
615}
616
617///////////////////////////////////////////////////////////////////////////////
618
619void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
620 const SkPaint& paint) {
621 CHECK_FOR_ANNOTATION(paint);
622 CHECK_SHOULD_DRAW(draw, false);
623
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000624 GrPaint grPaint;
625 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
626 return;
627 }
628
629 SkStrokeRec stroke(paint);
630 if (paint.getMaskFilter()) {
631 // try to hit the fast path for drawing filtered round rects
632
633 SkRRect devRRect;
634 if (rect.transform(fContext->getMatrix(), &devRRect)) {
635 if (devRRect.allCornersCircular()) {
636 SkRect maskRect;
637 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
638 draw.fClip->getBounds(),
639 fContext->getMatrix(),
640 &maskRect)) {
641 SkIRect finalIRect;
642 maskRect.roundOut(&finalIRect);
643 if (draw.fClip->quickReject(finalIRect)) {
644 // clipped out
645 return;
646 }
647 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
648 // nothing to draw
649 return;
650 }
651 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
652 stroke, devRRect)) {
653 return;
654 }
655 }
656
657 }
658 }
659
660 }
661
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000662 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000663 SkPath path;
664 path.addRRect(rect);
665 this->drawPath(draw, path, paint, NULL, true);
666 return;
667 }
668
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000669 fContext->drawRRect(grPaint, rect, stroke);
670}
671
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000672void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
673 const SkRRect& inner, const SkPaint& paint) {
674 SkStrokeRec stroke(paint);
675 if (stroke.isFillStyle()) {
676
677 CHECK_FOR_ANNOTATION(paint);
678 CHECK_SHOULD_DRAW(draw, false);
679
680 GrPaint grPaint;
681 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
682 return;
683 }
684
685 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
686 fContext->drawDRRect(grPaint, outer, inner);
687 return;
688 }
689 }
690
691 SkPath path;
692 path.addRRect(outer);
693 path.addRRect(inner);
694 path.setFillType(SkPath::kEvenOdd_FillType);
695
696 this->drawPath(draw, path, paint, NULL, true);
697}
698
699
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000700/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000701
702void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
703 const SkPaint& paint) {
704 CHECK_FOR_ANNOTATION(paint);
705 CHECK_SHOULD_DRAW(draw, false);
706
707 bool usePath = false;
708 // some basic reasons we might need to call drawPath...
709 if (paint.getMaskFilter() || paint.getPathEffect()) {
710 usePath = true;
711 }
712
713 if (usePath) {
714 SkPath path;
715 path.addOval(oval);
716 this->drawPath(draw, path, paint, NULL, true);
717 return;
718 }
719
720 GrPaint grPaint;
721 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
722 return;
723 }
724 SkStrokeRec stroke(paint);
725
726 fContext->drawOval(grPaint, oval, stroke);
727}
728
729#include "SkMaskFilter.h"
730#include "SkBounder.h"
731
732///////////////////////////////////////////////////////////////////////////////
733
734// helpers for applying mask filters
735namespace {
736
737// Draw a mask using the supplied paint. Since the coverage/geometry
738// is already burnt into the mask this boils down to a rect draw.
739// Return true if the mask was successfully drawn.
740bool draw_mask(GrContext* context, const SkRect& maskRect,
741 GrPaint* grp, GrTexture* mask) {
742 GrContext::AutoMatrix am;
743 if (!am.setIdentity(context, grp)) {
744 return false;
745 }
746
747 SkMatrix matrix;
748 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
749 matrix.postIDiv(mask->width(), mask->height());
750
751 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
752 context->drawRect(*grp, maskRect);
753 return true;
754}
755
756bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
757 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
758 GrPaint* grp, SkPaint::Style style) {
759 SkMask srcM, dstM;
760
761 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
762 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
763 return false;
764 }
765 SkAutoMaskFreeImage autoSrc(srcM.fImage);
766
767 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
768 return false;
769 }
770 // this will free-up dstM when we're done (allocated in filterMask())
771 SkAutoMaskFreeImage autoDst(dstM.fImage);
772
773 if (clip.quickReject(dstM.fBounds)) {
774 return false;
775 }
776 if (bounder && !bounder->doIRect(dstM.fBounds)) {
777 return false;
778 }
779
780 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
781 // the current clip (and identity matrix) and GrPaint settings
782 GrTextureDesc desc;
783 desc.fWidth = dstM.fBounds.width();
784 desc.fHeight = dstM.fBounds.height();
785 desc.fConfig = kAlpha_8_GrPixelConfig;
786
787 GrAutoScratchTexture ast(context, desc);
788 GrTexture* texture = ast.texture();
789
790 if (NULL == texture) {
791 return false;
792 }
793 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
794 dstM.fImage, dstM.fRowBytes);
795
796 SkRect maskRect = SkRect::Make(dstM.fBounds);
797
798 return draw_mask(context, maskRect, grp, texture);
799}
800
801// Create a mask of 'devPath' and place the result in 'mask'. Return true on
802// success; false otherwise.
803bool create_mask_GPU(GrContext* context,
804 const SkRect& maskRect,
805 const SkPath& devPath,
806 const SkStrokeRec& stroke,
807 bool doAA,
808 GrAutoScratchTexture* mask) {
809 GrTextureDesc desc;
810 desc.fFlags = kRenderTarget_GrTextureFlagBit;
811 desc.fWidth = SkScalarCeilToInt(maskRect.width());
812 desc.fHeight = SkScalarCeilToInt(maskRect.height());
813 // We actually only need A8, but it often isn't supported as a
814 // render target so default to RGBA_8888
815 desc.fConfig = kRGBA_8888_GrPixelConfig;
816 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
817 desc.fConfig = kAlpha_8_GrPixelConfig;
818 }
819
820 mask->set(context, desc);
821 if (NULL == mask->texture()) {
822 return false;
823 }
824
825 GrTexture* maskTexture = mask->texture();
826 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
827
828 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
829 GrContext::AutoClip ac(context, clipRect);
830
831 context->clear(NULL, 0x0, true);
832
833 GrPaint tempPaint;
834 if (doAA) {
835 tempPaint.setAntiAlias(true);
836 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
837 // blend coeff of zero requires dual source blending support in order
838 // to properly blend partially covered pixels. This means the AA
839 // code path may not be taken. So we use a dst blend coeff of ISA. We
840 // could special case AA draws to a dst surface with known alpha=0 to
841 // use a zero dst coeff when dual source blending isn't available.
842 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
843 }
844
845 GrContext::AutoMatrix am;
846
847 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
848 SkMatrix translate;
849 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
850 am.set(context, translate);
851 context->drawPath(tempPaint, devPath, stroke);
852 return true;
853}
854
855SkBitmap wrap_texture(GrTexture* texture) {
reed@google.combf790232013-12-13 19:45:58 +0000856 SkImageInfo info;
857 texture->asImageInfo(&info);
858
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000859 SkBitmap result;
reed@google.combf790232013-12-13 19:45:58 +0000860 result.setConfig(info);
861 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000862 return result;
863}
864
865};
866
867void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
868 const SkPaint& paint, const SkMatrix* prePathMatrix,
869 bool pathIsMutable) {
870 CHECK_FOR_ANNOTATION(paint);
871 CHECK_SHOULD_DRAW(draw, false);
872
873 GrPaint grPaint;
874 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
875 return;
876 }
877
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000878 // If we have a prematrix, apply it to the path, optimizing for the case
879 // where the original path can in fact be modified in place (even though
880 // its parameter type is const).
881 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000882 SkTLazy<SkPath> tmpPath;
883 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000884
885 if (prePathMatrix) {
886 SkPath* result = pathPtr;
887
888 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000889 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000890 pathIsMutable = true;
891 }
892 // should I push prePathMatrix on our MV stack temporarily, instead
893 // of applying it here? See SkDraw.cpp
894 pathPtr->transform(*prePathMatrix, result);
895 pathPtr = result;
896 }
897 // at this point we're done with prePathMatrix
898 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
899
900 SkStrokeRec stroke(paint);
901 SkPathEffect* pathEffect = paint.getPathEffect();
902 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000903 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000904 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000905 pathPtr = effectPath.get();
906 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000907 }
908
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000909 if (paint.getMaskFilter()) {
910 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000911 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
912 if (stroke.applyToPath(strokedPath, *pathPtr)) {
913 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000914 pathIsMutable = true;
915 stroke.setFillStyle();
916 }
917 }
918
919 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000920 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000921
922 // transform the path into device space
923 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000924
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000925 SkRect maskRect;
926 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
927 draw.fClip->getBounds(),
928 fContext->getMatrix(),
929 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000930 // The context's matrix may change while creating the mask, so save the CTM here to
931 // pass to filterMaskGPU.
932 const SkMatrix ctm = fContext->getMatrix();
933
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000934 SkIRect finalIRect;
935 maskRect.roundOut(&finalIRect);
936 if (draw.fClip->quickReject(finalIRect)) {
937 // clipped out
938 return;
939 }
940 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
941 // nothing to draw
942 return;
943 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000944
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000945 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000946 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000947 // the mask filter was able to draw itself directly, so there's nothing
948 // left to do.
949 return;
950 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000951
952 GrAutoScratchTexture mask;
953
954 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
955 grPaint.isAntiAlias(), &mask)) {
956 GrTexture* filtered;
957
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000958 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000959 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000960 // filterMaskGPU gives us ownership of a ref to the result
961 SkAutoTUnref<GrTexture> atu(filtered);
962
963 // If the scratch texture that we used as the filter src also holds the filter
964 // result then we must detach so that this texture isn't recycled for a later
965 // draw.
966 if (filtered == mask.texture()) {
967 mask.detach();
968 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
969 }
970
971 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
972 // This path is completely drawn
973 return;
974 }
975 }
976 }
977 }
978
979 // draw the mask on the CPU - this is a fallthrough path in case the
980 // GPU path fails
981 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
982 SkPaint::kFill_Style;
983 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
984 *draw.fClip, draw.fBounder, &grPaint, style);
985 return;
986 }
987
988 fContext->drawPath(grPaint, *pathPtr, stroke);
989}
990
991static const int kBmpSmallTileSize = 1 << 10;
992
993static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
994 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
995 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
996 return tilesX * tilesY;
997}
998
999static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
1000 if (maxTileSize <= kBmpSmallTileSize) {
1001 return maxTileSize;
1002 }
1003
1004 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
1005 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
1006
1007 maxTileTotalTileSize *= maxTileSize * maxTileSize;
1008 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
1009
1010 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
1011 return kBmpSmallTileSize;
1012 } else {
1013 return maxTileSize;
1014 }
1015}
1016
1017// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
1018// pixels from the bitmap are necessary.
1019static void determine_clipped_src_rect(const GrContext* context,
1020 const SkBitmap& bitmap,
1021 const SkRect* srcRectPtr,
1022 SkIRect* clippedSrcIRect) {
1023 const GrClipData* clip = context->getClip();
1024 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
1025 SkMatrix inv;
1026 if (!context->getMatrix().invert(&inv)) {
1027 clippedSrcIRect->setEmpty();
1028 return;
1029 }
1030 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
1031 inv.mapRect(&clippedSrcRect);
1032 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001033 // we've setup src space 0,0 to map to the top left of the src rect.
1034 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001035 if (!clippedSrcRect.intersect(*srcRectPtr)) {
1036 clippedSrcIRect->setEmpty();
1037 return;
1038 }
1039 }
1040 clippedSrcRect.roundOut(clippedSrcIRect);
1041 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1042 if (!clippedSrcIRect->intersect(bmpBounds)) {
1043 clippedSrcIRect->setEmpty();
1044 }
1045}
1046
1047bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1048 const GrTextureParams& params,
1049 const SkRect* srcRectPtr,
1050 int maxTileSize,
1051 int* tileSize,
1052 SkIRect* clippedSrcRect) const {
1053 // if bitmap is explictly texture backed then just use the texture
1054 if (NULL != bitmap.getTexture()) {
1055 return false;
1056 }
1057
1058 // if it's larger than the max tile size, then we have no choice but tiling.
1059 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
1060 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1061 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
1062 return true;
1063 }
1064
1065 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
1066 return false;
1067 }
1068
1069 // if the entire texture is already in our cache then no reason to tile it
1070 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
1071 return false;
1072 }
1073
1074 // At this point we know we could do the draw by uploading the entire bitmap
1075 // as a texture. However, if the texture would be large compared to the
1076 // cache size and we don't require most of it for this draw then tile to
1077 // reduce the amount of upload and cache spill.
1078
1079 // assumption here is that sw bitmap size is a good proxy for its size as
1080 // a texture
1081 size_t bmpSize = bitmap.getSize();
1082 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001083 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001084 if (bmpSize < cacheSize / 2) {
1085 return false;
1086 }
1087
1088 // Figure out how much of the src we will need based on the src rect and clipping.
1089 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1090 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1091 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
1092 kBmpSmallTileSize * kBmpSmallTileSize;
1093
1094 return usedTileBytes < 2 * bmpSize;
1095}
1096
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001097void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001098 const SkBitmap& bitmap,
1099 const SkMatrix& m,
1100 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001101 SkMatrix concat;
1102 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1103 if (!m.isIdentity()) {
1104 concat.setConcat(*draw->fMatrix, m);
1105 draw.writable()->fMatrix = &concat;
1106 }
1107 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001108}
1109
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001110// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001111// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1112// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001113static inline void clamped_outset_with_offset(SkIRect* iRect,
1114 int outset,
1115 SkPoint* offset,
1116 const SkIRect& clamp) {
1117 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001118
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001119 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1120 if (leftClampDelta > 0) {
1121 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001122 iRect->fLeft = clamp.fLeft;
1123 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001124 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001125 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001126
1127 int topClampDelta = clamp.fTop - iRect->fTop;
1128 if (topClampDelta > 0) {
1129 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001130 iRect->fTop = clamp.fTop;
1131 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001132 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001133 }
1134
1135 if (iRect->fRight > clamp.fRight) {
1136 iRect->fRight = clamp.fRight;
1137 }
1138 if (iRect->fBottom > clamp.fBottom) {
1139 iRect->fBottom = clamp.fBottom;
1140 }
1141}
1142
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001143static bool has_aligned_samples(const SkRect& srcRect,
1144 const SkRect& transformedRect) {
1145 // detect pixel disalignment
1146 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1147 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1148 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1149 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1150 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1151 COLOR_BLEED_TOLERANCE &&
1152 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1153 COLOR_BLEED_TOLERANCE) {
1154 return true;
1155 }
1156 return false;
1157}
1158
1159static bool may_color_bleed(const SkRect& srcRect,
1160 const SkRect& transformedRect,
1161 const SkMatrix& m) {
1162 // Only gets called if has_aligned_samples returned false.
1163 // So we can assume that sampling is axis aligned but not texel aligned.
1164 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1165 SkRect innerSrcRect(srcRect), innerTransformedRect,
1166 outerTransformedRect(transformedRect);
1167 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1168 m.mapRect(&innerTransformedRect, innerSrcRect);
1169
1170 // The gap between outerTransformedRect and innerTransformedRect
1171 // represents the projection of the source border area, which is
1172 // problematic for color bleeding. We must check whether any
1173 // destination pixels sample the border area.
1174 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1175 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1176 SkIRect outer, inner;
1177 outerTransformedRect.round(&outer);
1178 innerTransformedRect.round(&inner);
1179 // If the inner and outer rects round to the same result, it means the
1180 // border does not overlap any pixel centers. Yay!
1181 return inner != outer;
1182}
1183
1184static bool needs_texture_domain(const SkBitmap& bitmap,
1185 const SkRect& srcRect,
1186 GrTextureParams &params,
1187 const SkMatrix& contextMatrix,
1188 bool bicubic) {
1189 bool needsTextureDomain = false;
1190
1191 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1192 // Need texture domain if drawing a sub rect
1193 needsTextureDomain = srcRect.width() < bitmap.width() ||
1194 srcRect.height() < bitmap.height();
1195 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1196 // sampling is axis-aligned
1197 SkRect transformedRect;
1198 contextMatrix.mapRect(&transformedRect, srcRect);
1199
1200 if (has_aligned_samples(srcRect, transformedRect)) {
1201 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1202 needsTextureDomain = false;
1203 } else {
1204 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1205 }
1206 }
1207 }
1208 return needsTextureDomain;
1209}
1210
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001211void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1212 const SkBitmap& bitmap,
1213 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001214 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001215 const SkPaint& paint,
1216 SkCanvas::DrawBitmapRectFlags flags) {
1217 CHECK_SHOULD_DRAW(draw, false);
1218
1219 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001220 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001221 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1222 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001223 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001224 SkScalar w = SkIntToScalar(bitmap.width());
1225 SkScalar h = SkIntToScalar(bitmap.height());
1226 dstSize.fWidth = w;
1227 dstSize.fHeight = h;
1228 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001229 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001230 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001231 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001232 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001233 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001234 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1235 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1236 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1237 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001238 }
1239
1240 if (paint.getMaskFilter()){
1241 // Convert the bitmap to a shader so that the rect can be drawn
1242 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001243 SkBitmap tmp; // subset of bitmap, if necessary
1244 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001245 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001246 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001247 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1248 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1249 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001250 // In bleed mode we position and trim the bitmap based on the src rect which is
1251 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1252 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1253 // compensate.
1254 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1255 SkIRect iSrc;
1256 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001257
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001258 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1259 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001260
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001261 if (!bitmap.extractSubset(&tmp, iSrc)) {
1262 return; // extraction failed
1263 }
1264 bitmapPtr = &tmp;
1265 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001266
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001267 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001268 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001269 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001270 } else {
1271 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001272 }
1273
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001274 SkPaint paintWithShader(paint);
1275 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001276 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001277 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1278 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001279
1280 return;
1281 }
1282
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001283 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1284 // the view matrix rather than a local matrix.
1285 SkMatrix m;
1286 m.setScale(dstSize.fWidth / srcRect.width(),
1287 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001288 fContext->concatMatrix(m);
1289
1290 GrTextureParams params;
1291 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1292 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001293
1294 int tileFilterPad;
1295 bool doBicubic = false;
1296
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001297 switch(paintFilterLevel) {
1298 case SkPaint::kNone_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001299 tileFilterPad = 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001300 textureFilterMode = GrTextureParams::kNone_FilterMode;
1301 break;
1302 case SkPaint::kLow_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001303 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001304 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1305 break;
1306 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001307 tileFilterPad = 1;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001308 if (fContext->getMatrix().getMinStretch() < SK_Scalar1) {
1309 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1310 } else {
1311 // Don't trigger MIP level generation unnecessarily.
1312 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1313 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001314 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001315 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001316 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001317 if (fContext->getMatrix().getMinStretch() >= SK_Scalar1) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001318 // We will install an effect that does the filtering in the shader.
1319 textureFilterMode = GrTextureParams::kNone_FilterMode;
1320 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1321 doBicubic = true;
1322 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001323 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1324 tileFilterPad = 1;
1325 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001326 break;
1327 default:
1328 SkErrorInternals::SetError( kInvalidPaint_SkError,
1329 "Sorry, I don't understand the filtering "
1330 "mode you asked for. Falling back to "
1331 "MIPMaps.");
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001332 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001333 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1334 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001335 }
1336
1337 params.setFilterMode(textureFilterMode);
1338
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001339 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001340 int tileSize;
1341
1342 SkIRect clippedSrcRect;
1343 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1344 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001345 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1346 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001347 } else {
1348 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001349 bool needsTextureDomain = needs_texture_domain(bitmap,
1350 srcRect,
1351 params,
1352 fContext->getMatrix(),
1353 doBicubic);
1354 this->internalDrawBitmap(bitmap,
1355 srcRect,
1356 params,
1357 paint,
1358 flags,
1359 doBicubic,
1360 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001361 }
1362}
1363
1364// Break 'bitmap' into several tiles to draw it since it has already
1365// been determined to be too large to fit in VRAM
1366void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1367 const SkRect& srcRect,
1368 const SkIRect& clippedSrcIRect,
1369 const GrTextureParams& params,
1370 const SkPaint& paint,
1371 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001372 int tileSize,
1373 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001374 // The following pixel lock is technically redundant, but it is desirable
1375 // to lock outside of the tile loop to prevent redecoding the whole image
1376 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1377 // is larger than the limit of the discardable memory pool.
1378 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001379 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1380
1381 int nx = bitmap.width() / tileSize;
1382 int ny = bitmap.height() / tileSize;
1383 for (int x = 0; x <= nx; x++) {
1384 for (int y = 0; y <= ny; y++) {
1385 SkRect tileR;
1386 tileR.set(SkIntToScalar(x * tileSize),
1387 SkIntToScalar(y * tileSize),
1388 SkIntToScalar((x + 1) * tileSize),
1389 SkIntToScalar((y + 1) * tileSize));
1390
1391 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1392 continue;
1393 }
1394
1395 if (!tileR.intersect(srcRect)) {
1396 continue;
1397 }
1398
1399 SkBitmap tmpB;
1400 SkIRect iTileR;
1401 tileR.roundOut(&iTileR);
1402 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1403 SkIntToScalar(iTileR.fTop));
1404
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001405 // Adjust the context matrix to draw at the right x,y in device space
1406 SkMatrix tmpM;
1407 GrContext::AutoMatrix am;
1408 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1409 am.setPreConcat(fContext, tmpM);
1410
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001411 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001412 SkIRect iClampRect;
1413
1414 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1415 // In bleed mode we want to always expand the tile on all edges
1416 // but stay within the bitmap bounds
1417 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1418 } else {
1419 // In texture-domain/clamp mode we only want to expand the
1420 // tile on edges interior to "srcRect" (i.e., we want to
1421 // not bleed across the original clamped edges)
1422 srcRect.roundOut(&iClampRect);
1423 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001424 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1425 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001426 }
1427
1428 if (bitmap.extractSubset(&tmpB, iTileR)) {
1429 // now offset it to make it "local" to our tmp bitmap
1430 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001431 GrTextureParams paramsTemp = params;
1432 bool needsTextureDomain = needs_texture_domain(bitmap,
1433 srcRect,
1434 paramsTemp,
1435 fContext->getMatrix(),
1436 bicubic);
1437 this->internalDrawBitmap(tmpB,
1438 tileR,
1439 paramsTemp,
1440 paint,
1441 flags,
1442 bicubic,
1443 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001444 }
1445 }
1446 }
1447}
1448
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001449
1450/*
1451 * This is called by drawBitmap(), which has to handle images that may be too
1452 * large to be represented by a single texture.
1453 *
1454 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1455 * and that non-texture portion of the GrPaint has already been setup.
1456 */
1457void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1458 const SkRect& srcRect,
1459 const GrTextureParams& params,
1460 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001461 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001462 bool bicubic,
1463 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001464 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1465 bitmap.height() <= fContext->getMaxTextureSize());
1466
1467 GrTexture* texture;
1468 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1469 if (NULL == texture) {
1470 return;
1471 }
1472
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001473 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001474 SkRect paintRect;
1475 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1476 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1477 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1478 SkScalarMul(srcRect.fTop, hInv),
1479 SkScalarMul(srcRect.fRight, wInv),
1480 SkScalarMul(srcRect.fBottom, hInv));
1481
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001482 SkRect textureDomain = SkRect::MakeEmpty();
1483 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001484 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001485 // Use a constrained texture domain to avoid color bleeding
1486 SkScalar left, top, right, bottom;
1487 if (srcRect.width() > SK_Scalar1) {
1488 SkScalar border = SK_ScalarHalf / texture->width();
1489 left = paintRect.left() + border;
1490 right = paintRect.right() - border;
1491 } else {
1492 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1493 }
1494 if (srcRect.height() > SK_Scalar1) {
1495 SkScalar border = SK_ScalarHalf / texture->height();
1496 top = paintRect.top() + border;
1497 bottom = paintRect.bottom() - border;
1498 } else {
1499 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1500 }
1501 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001502 if (bicubic) {
1503 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1504 } else {
1505 effect.reset(GrTextureDomainEffect::Create(texture,
1506 SkMatrix::I(),
1507 textureDomain,
1508 GrTextureDomain::kClamp_Mode,
1509 params.filterMode()));
1510 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001511 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001512 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1513 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1514 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001515 } else {
1516 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1517 }
1518
1519 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1520 // the rest from the SkPaint.
1521 GrPaint grPaint;
1522 grPaint.addColorEffect(effect);
1523 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1524 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1525 return;
1526 }
1527
1528 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1529}
1530
1531static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001532 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001533 int w, int h, const SkImageFilter::Context& ctx,
1534 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001535 SkASSERT(filter);
1536 SkDeviceImageFilterProxy proxy(device);
1537
1538 if (filter->canFilterImageGPU()) {
1539 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1540 // filter. Also set the clip wide open and the matrix to identity.
1541 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001542 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001543 } else {
1544 return false;
1545 }
1546}
1547
1548void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1549 int left, int top, const SkPaint& paint) {
1550 // drawSprite is defined to be in device coords.
1551 CHECK_SHOULD_DRAW(draw, true);
1552
1553 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1554 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1555 return;
1556 }
1557
1558 int w = bitmap.width();
1559 int h = bitmap.height();
1560
1561 GrTexture* texture;
1562 // draw sprite uses the default texture params
1563 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1564
1565 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001566 // This bitmap will own the filtered result as a texture.
1567 SkBitmap filteredBitmap;
1568
1569 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001570 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001571 SkMatrix matrix(*draw.fMatrix);
1572 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001573 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001574 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1575 SkAutoUnref aur(cache);
1576 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001577 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001578 &offset)) {
1579 texture = (GrTexture*) filteredBitmap.getTexture();
1580 w = filteredBitmap.width();
1581 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001582 left += offset.x();
1583 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001584 } else {
1585 return;
1586 }
1587 }
1588
1589 GrPaint grPaint;
1590 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1591
1592 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1593 return;
1594 }
1595
1596 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001597 SkRect::MakeXYWH(SkIntToScalar(left),
1598 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001599 SkIntToScalar(w),
1600 SkIntToScalar(h)),
1601 SkRect::MakeXYWH(0,
1602 0,
1603 SK_Scalar1 * w / texture->width(),
1604 SK_Scalar1 * h / texture->height()));
1605}
1606
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001607void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001608 const SkRect* src, const SkRect& dst,
1609 const SkPaint& paint,
1610 SkCanvas::DrawBitmapRectFlags flags) {
1611 SkMatrix matrix;
1612 SkRect bitmapBounds, tmpSrc;
1613
1614 bitmapBounds.set(0, 0,
1615 SkIntToScalar(bitmap.width()),
1616 SkIntToScalar(bitmap.height()));
1617
1618 // Compute matrix from the two rectangles
1619 if (NULL != src) {
1620 tmpSrc = *src;
1621 } else {
1622 tmpSrc = bitmapBounds;
1623 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001624
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001625 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1626
1627 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1628 if (NULL != src) {
1629 if (!bitmapBounds.contains(tmpSrc)) {
1630 if (!tmpSrc.intersect(bitmapBounds)) {
1631 return; // nothing to draw
1632 }
1633 }
1634 }
1635
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001636 SkRect tmpDst;
1637 matrix.mapRect(&tmpDst, tmpSrc);
1638
1639 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1640 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1641 // Translate so that tempDst's top left is at the origin.
1642 matrix = *origDraw.fMatrix;
1643 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1644 draw.writable()->fMatrix = &matrix;
1645 }
1646 SkSize dstSize;
1647 dstSize.fWidth = tmpDst.width();
1648 dstSize.fHeight = tmpDst.height();
1649
1650 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001651}
1652
1653void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1654 int x, int y, const SkPaint& paint) {
1655 // clear of the source device must occur before CHECK_SHOULD_DRAW
1656 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1657 if (dev->fNeedClear) {
1658 // TODO: could check here whether we really need to draw at all
1659 dev->clear(0x0);
1660 }
1661
1662 // drawDevice is defined to be in device coords.
1663 CHECK_SHOULD_DRAW(draw, true);
1664
1665 GrRenderTarget* devRT = dev->accessRenderTarget();
1666 GrTexture* devTex;
1667 if (NULL == (devTex = devRT->asTexture())) {
1668 return;
1669 }
1670
1671 const SkBitmap& bm = dev->accessBitmap(false);
1672 int w = bm.width();
1673 int h = bm.height();
1674
1675 SkImageFilter* filter = paint.getImageFilter();
1676 // This bitmap will own the filtered result as a texture.
1677 SkBitmap filteredBitmap;
1678
1679 if (NULL != filter) {
1680 SkIPoint offset = SkIPoint::Make(0, 0);
1681 SkMatrix matrix(*draw.fMatrix);
1682 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001683 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001684 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1685 SkAutoUnref aur(cache);
1686 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001687 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001688 &offset)) {
1689 devTex = filteredBitmap.getTexture();
1690 w = filteredBitmap.width();
1691 h = filteredBitmap.height();
1692 x += offset.fX;
1693 y += offset.fY;
1694 } else {
1695 return;
1696 }
1697 }
1698
1699 GrPaint grPaint;
1700 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1701
1702 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1703 return;
1704 }
1705
1706 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1707 SkIntToScalar(y),
1708 SkIntToScalar(w),
1709 SkIntToScalar(h));
1710
1711 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1712 // scratch texture).
1713 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1714 SK_Scalar1 * h / devTex->height());
1715
1716 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1717}
1718
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001719bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001720 return filter->canFilterImageGPU();
1721}
1722
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001723bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001724 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001725 SkBitmap* result, SkIPoint* offset) {
1726 // want explicitly our impl, so guard against a subclass of us overriding it
1727 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1728 return false;
1729 }
1730
1731 SkAutoLockPixels alp(src, !src.getTexture());
1732 if (!src.getTexture() && !src.readyToDraw()) {
1733 return false;
1734 }
1735
1736 GrTexture* texture;
1737 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1738 // must be pushed upstack.
1739 SkAutoCachedTexture act(this, src, NULL, &texture);
1740
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001741 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1742 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001743}
1744
1745///////////////////////////////////////////////////////////////////////////////
1746
1747// must be in SkCanvas::VertexMode order
1748static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1749 kTriangles_GrPrimitiveType,
1750 kTriangleStrip_GrPrimitiveType,
1751 kTriangleFan_GrPrimitiveType,
1752};
1753
1754void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1755 int vertexCount, const SkPoint vertices[],
1756 const SkPoint texs[], const SkColor colors[],
1757 SkXfermode* xmode,
1758 const uint16_t indices[], int indexCount,
1759 const SkPaint& paint) {
1760 CHECK_SHOULD_DRAW(draw, false);
1761
1762 GrPaint grPaint;
1763 // we ignore the shader if texs is null.
1764 if (NULL == texs) {
1765 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
1766 return;
1767 }
1768 } else {
1769 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
1770 return;
1771 }
1772 }
1773
1774 if (NULL != xmode && NULL != texs && NULL != colors) {
1775 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1776 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1777#if 0
1778 return
1779#endif
1780 }
1781 }
1782
1783 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1784 if (NULL != colors) {
1785 // need to convert byte order and from non-PM to PM
1786 convertedColors.reset(vertexCount);
1787 for (int i = 0; i < vertexCount; ++i) {
1788 convertedColors[i] = SkColor2GrColor(colors[i]);
1789 }
1790 colors = convertedColors.get();
1791 }
1792 fContext->drawVertices(grPaint,
1793 gVertexMode2PrimitiveType[vmode],
1794 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001795 vertices,
1796 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001797 colors,
1798 indices,
1799 indexCount);
1800}
1801
1802///////////////////////////////////////////////////////////////////////////////
1803
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001804void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1805 size_t byteLength, SkScalar x, SkScalar y,
1806 const SkPaint& paint) {
1807 CHECK_SHOULD_DRAW(draw, false);
1808
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001809 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001810 GrPaint grPaint;
1811 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1812 return;
1813 }
1814
1815 SkDEBUGCODE(this->validate();)
1816
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001817 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1818 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001819 GrPaint grPaint;
1820 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1821 return;
1822 }
1823
1824 SkDEBUGCODE(this->validate();)
1825
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001826 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001827 } else {
1828 // this guy will just call our drawPath()
1829 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001830 }
1831}
1832
1833void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1834 size_t byteLength, const SkScalar pos[],
1835 SkScalar constY, int scalarsPerPos,
1836 const SkPaint& paint) {
1837 CHECK_SHOULD_DRAW(draw, false);
1838
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001839 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001840 GrPaint grPaint;
1841 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1842 return;
1843 }
1844
1845 SkDEBUGCODE(this->validate();)
1846
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001847 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001848 constY, scalarsPerPos);
1849 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001850 GrPaint grPaint;
1851 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1852 return;
1853 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001854
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001855 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001856
1857 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001858 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001859 } else {
1860 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1861 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001862 }
1863}
1864
1865void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1866 size_t len, const SkPath& path,
1867 const SkMatrix* m, const SkPaint& paint) {
1868 CHECK_SHOULD_DRAW(draw, false);
1869
1870 SkASSERT(draw.fDevice == this);
1871 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1872}
1873
1874///////////////////////////////////////////////////////////////////////////////
1875
1876bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1877 if (!paint.isLCDRenderText()) {
1878 // we're cool with the paint as is
1879 return false;
1880 }
1881
1882 if (paint.getShader() ||
1883 paint.getXfermode() || // unless its srcover
1884 paint.getMaskFilter() ||
1885 paint.getRasterizer() ||
1886 paint.getColorFilter() ||
1887 paint.getPathEffect() ||
1888 paint.isFakeBoldText() ||
1889 paint.getStyle() != SkPaint::kFill_Style) {
1890 // turn off lcd
1891 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1892 flags->fHinting = paint.getHinting();
1893 return true;
1894 }
1895 // we're cool with the paint as is
1896 return false;
1897}
1898
1899void SkGpuDevice::flush() {
1900 DO_DEFERRED_CLEAR();
1901 fContext->resolveRenderTarget(fRenderTarget);
1902}
1903
1904///////////////////////////////////////////////////////////////////////////////
1905
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001906SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001907 GrTextureDesc desc;
1908 desc.fConfig = fRenderTarget->config();
1909 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001910 desc.fWidth = info.width();
1911 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001912 desc.fSampleCnt = fRenderTarget->numSamples();
1913
1914 SkAutoTUnref<GrTexture> texture;
1915 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001916 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001917
1918#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1919 // layers are never draw in repeat modes, so we can request an approx
1920 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001921 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001922 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1923 GrContext::kApprox_ScratchTexMatch :
1924 GrContext::kExact_ScratchTexMatch;
1925 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1926#else
1927 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1928#endif
1929 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001930 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001931 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001932 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1933 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001934 return NULL;
1935 }
1936}
1937
reed@google.com76f10a32014-02-05 15:32:21 +00001938SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1939 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1940}
1941
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001942void SkGpuDevice::EXPERIMENTAL_optimize(SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001943 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001944
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001945 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1946 if (NULL != existing) {
1947 return;
1948 }
1949
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001950 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001951
1952 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001953
1954 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001955}
1956
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001957static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1958 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1959 result->setConfig(info);
1960 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1961}
1962
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001963void SkGpuDevice::EXPERIMENTAL_purge(SkPicture* picture) {
1964
1965}
1966
1967bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001968
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001969 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001970
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
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001978 if (0 == gpuData->numSaveLayers()) {
1979 return false;
1980 }
1981
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001982 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1983 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1984 pullForward[i] = false;
1985 }
1986
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001987 SkRect clipBounds;
1988 if (!canvas->getClipBounds(&clipBounds)) {
1989 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001990 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001991 SkIRect query;
1992 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001993
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001994 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001995
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001996 // This code pre-renders the entire layer since it will be cached and potentially
1997 // reused with different clips (e.g., in different tiles). Because of this the
1998 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1999 // is used to limit which clips are pre-rendered.
2000 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00002001
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002002 if (ops.valid()) {
2003 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00002004 // the BBH to limit the pre-rendering to just the layers needed to cover
2005 // the region being drawn
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002006 for (int i = 0; i < ops.numOps(); ++i) {
2007 uint32_t offset = ops.offset(i);
2008
2009 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00002010 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002011 // their parent in the list.
2012 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
2013 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
2014
2015 if (pullForward[j]) {
2016 continue; // already pulling forward
2017 }
2018
2019 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
2020 continue; // the op isn't in this range
2021 }
2022
2023 // TODO: once this code is more stable unsuitable layers can
2024 // just be omitted during the optimization stage
2025 if (!info.fValid ||
2026 kSaveLayerMaxSize < info.fSize.fWidth ||
2027 kSaveLayerMaxSize < info.fSize.fHeight ||
2028 info.fIsNested) {
2029 continue; // this layer is unsuitable
2030 }
2031
2032 pullForward[j] = true;
2033 }
2034 }
2035 } else {
2036 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00002037 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00002038 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
2039 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
2040
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00002041 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
2042 info.fOffset.fY,
2043 info.fSize.fWidth,
2044 info.fSize.fHeight);
2045
2046 if (!SkIRect::Intersects(query, layerRect)) {
2047 continue;
2048 }
2049
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002050 // TODO: once this code is more stable unsuitable layers can
2051 // just be omitted during the optimization stage
2052 if (!info.fValid ||
2053 kSaveLayerMaxSize < info.fSize.fWidth ||
2054 kSaveLayerMaxSize < info.fSize.fHeight ||
2055 info.fIsNested) {
2056 continue;
2057 }
2058
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00002059 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002060 }
2061 }
2062
2063 SkPicturePlayback::PlaybackReplacements replacements;
2064
2065 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
2066 if (pullForward[i]) {
2067 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
2068
2069 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
2070
2071 if (NULL != picture->fPlayback) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00002072 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002073 replacements.push();
2074 layerInfo->fStart = info.fSaveLayerOpID;
2075 layerInfo->fStop = info.fRestoreOpID;
2076 layerInfo->fPos = info.fOffset;
2077
2078 GrTextureDesc desc;
2079 desc.fFlags = kRenderTarget_GrTextureFlagBit;
2080 desc.fWidth = info.fSize.fWidth;
2081 desc.fHeight = info.fSize.fHeight;
2082 desc.fConfig = kSkia8888_GrPixelConfig;
2083 // TODO: need to deal with sample count
2084
2085 bool bNeedsRendering = true;
2086
2087 // This just uses scratch textures and doesn't cache the texture.
2088 // This can yield a lot of re-rendering
2089 if (NULL == layer->getTexture()) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00002090 layer->setTexture(fContext->lockAndRefScratchTexture(desc,
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002091 GrContext::kApprox_ScratchTexMatch));
2092 if (NULL == layer->getTexture()) {
2093 continue;
2094 }
2095 } else {
2096 bNeedsRendering = false;
2097 }
2098
2099 layerInfo->fBM = SkNEW(SkBitmap);
2100 wrap_texture(layer->getTexture(), desc.fWidth, desc.fHeight, layerInfo->fBM);
2101
2102 SkASSERT(info.fPaint);
2103 layerInfo->fPaint = info.fPaint;
2104
2105 if (bNeedsRendering) {
2106 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
2107 layer->getTexture()->asRenderTarget()));
2108
2109 SkCanvas* canvas = surface->getCanvas();
2110
2111 canvas->setMatrix(info.fCTM);
2112 canvas->clear(SK_ColorTRANSPARENT);
2113
2114 picture->fPlayback->setDrawLimits(info.fSaveLayerOpID, info.fRestoreOpID);
2115 picture->fPlayback->draw(*canvas, NULL);
2116 picture->fPlayback->setDrawLimits(0, 0);
2117 canvas->flush();
2118 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00002119 }
2120 }
2121 }
2122
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002123 // Playback using new layers
2124 picture->fPlayback->setReplacements(&replacements);
2125 picture->fPlayback->draw(*canvas, NULL);
2126 picture->fPlayback->setReplacements(NULL);
2127
2128 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
2129 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
2130
2131 if (NULL != layer->getTexture()) {
2132 fContext->unlockScratchTexture(layer->getTexture());
2133 layer->setTexture(NULL);
2134 }
2135 }
2136
2137 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002138}