blob: 26392cab2627cdc59325814cfba1eefe6e31ecc2 [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"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000017
18#include "SkGrTexturePixelRef.h"
19
commit-bot@chromium.org82139702014-03-10 22:53:20 +000020#include "SkBounder.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000021#include "SkColorFilter.h"
22#include "SkDeviceImageFilterProxy.h"
23#include "SkDrawProcs.h"
24#include "SkGlyphCache.h"
25#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000026#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000027#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000028#include "SkPicture.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000029#include "SkRRect.h"
30#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000031#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000032#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkUtils.h"
34#include "SkErrorInternals.h"
35
36#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
37
38#if 0
39 extern bool (*gShouldDrawProc)();
40 #define CHECK_SHOULD_DRAW(draw, forceI) \
41 do { \
42 if (gShouldDrawProc && !gShouldDrawProc()) return; \
43 this->prepareDraw(draw, forceI); \
44 } while (0)
45#else
46 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
47#endif
48
49// This constant represents the screen alignment criterion in texels for
50// requiring texture domain clamping to prevent color bleeding when drawing
51// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000052#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000053
54#define DO_DEFERRED_CLEAR() \
55 do { \
56 if (fNeedClear) { \
57 this->clear(SK_ColorTRANSPARENT); \
58 } \
59 } while (false) \
60
61///////////////////////////////////////////////////////////////////////////////
62
63#define CHECK_FOR_ANNOTATION(paint) \
64 do { if (paint.getAnnotation()) { return; } } while (0)
65
66///////////////////////////////////////////////////////////////////////////////
67
68
69class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
70public:
71 SkAutoCachedTexture()
72 : fDevice(NULL)
73 , fTexture(NULL) {
74 }
75
76 SkAutoCachedTexture(SkGpuDevice* device,
77 const SkBitmap& bitmap,
78 const GrTextureParams* params,
79 GrTexture** texture)
80 : fDevice(NULL)
81 , fTexture(NULL) {
82 SkASSERT(NULL != texture);
83 *texture = this->set(device, bitmap, params);
84 }
85
86 ~SkAutoCachedTexture() {
87 if (NULL != fTexture) {
88 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
89 }
90 }
91
92 GrTexture* set(SkGpuDevice* device,
93 const SkBitmap& bitmap,
94 const GrTextureParams* params) {
95 if (NULL != fTexture) {
96 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
97 fTexture = NULL;
98 }
99 fDevice = device;
100 GrTexture* result = (GrTexture*)bitmap.getTexture();
101 if (NULL == result) {
102 // Cannot return the native texture so look it up in our cache
103 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
104 result = fTexture;
105 }
106 return result;
107 }
108
109private:
110 SkGpuDevice* fDevice;
111 GrTexture* fTexture;
112};
113
114///////////////////////////////////////////////////////////////////////////////
115
116struct GrSkDrawProcs : public SkDrawProcs {
117public:
118 GrContext* fContext;
119 GrTextContext* fTextContext;
120 GrFontScaler* fFontScaler; // cached in the skia glyphcache
121};
122
123///////////////////////////////////////////////////////////////////////////////
124
125static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
126 switch (config) {
127 case kAlpha_8_GrPixelConfig:
128 *isOpaque = false;
129 return SkBitmap::kA8_Config;
130 case kRGB_565_GrPixelConfig:
131 *isOpaque = true;
132 return SkBitmap::kRGB_565_Config;
133 case kRGBA_4444_GrPixelConfig:
134 *isOpaque = false;
135 return SkBitmap::kARGB_4444_Config;
136 case kSkia8888_GrPixelConfig:
137 // we don't currently have a way of knowing whether
138 // a 8888 is opaque based on the config.
139 *isOpaque = false;
140 return SkBitmap::kARGB_8888_Config;
141 default:
142 *isOpaque = false;
143 return SkBitmap::kNo_Config;
144 }
145}
146
147/*
148 * GrRenderTarget does not know its opaqueness, only its config, so we have
149 * to make conservative guesses when we return an "equivalent" bitmap.
150 */
151static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
152 bool isOpaque;
153 SkBitmap::Config config = grConfig2skConfig(renderTarget->config(), &isOpaque);
154
155 SkBitmap bitmap;
156 bitmap.setConfig(config, renderTarget->width(), renderTarget->height(), 0,
157 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
158 return bitmap;
159}
160
161SkGpuDevice* SkGpuDevice::Create(GrSurface* surface) {
162 SkASSERT(NULL != surface);
163 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
164 return NULL;
165 }
166 if (surface->asTexture()) {
167 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture()));
168 } else {
169 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget()));
170 }
171}
172
173SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
174 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
175 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
176}
177
178SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
179 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
180 this->initFromRenderTarget(context, renderTarget, false);
181}
182
183void SkGpuDevice::initFromRenderTarget(GrContext* context,
184 GrRenderTarget* renderTarget,
185 bool cached) {
186 fDrawProcs = NULL;
187
188 fContext = context;
189 fContext->ref();
190
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000191 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties));
192 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +0000193
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000194 fRenderTarget = NULL;
195 fNeedClear = false;
196
197 SkASSERT(NULL != renderTarget);
198 fRenderTarget = renderTarget;
199 fRenderTarget->ref();
200
201 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
202 // on the RT but not vice-versa.
203 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
204 // busting chrome (for a currently unknown reason).
205 GrSurface* surface = fRenderTarget->asTexture();
206 if (NULL == surface) {
207 surface = fRenderTarget;
208 }
reed@google.combf790232013-12-13 19:45:58 +0000209
210 SkImageInfo info;
211 surface->asImageInfo(&info);
212 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, cached));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000213
reed@google.com672588b2014-01-08 15:42:01 +0000214 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000215}
216
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000217SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
218 int sampleCount) {
219 if (kUnknown_SkColorType == origInfo.colorType() ||
220 origInfo.width() < 0 || origInfo.height() < 0) {
221 return NULL;
222 }
223
224 SkImageInfo info = origInfo;
225 // TODO: perhas we can loosen this check now that colortype is more detailed
226 // e.g. can we support both RGBA and BGRA here?
227 if (kRGB_565_SkColorType == info.colorType()) {
228 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
229 } else {
230 info.fColorType = kPMColor_SkColorType;
231 if (kOpaque_SkAlphaType != info.alphaType()) {
232 info.fAlphaType = kPremul_SkAlphaType; // force this setting
233 }
234 }
235
236 GrTextureDesc desc;
237 desc.fFlags = kRenderTarget_GrTextureFlagBit;
238 desc.fWidth = info.width();
239 desc.fHeight = info.height();
240 desc.fConfig = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType());
241 desc.fSampleCnt = sampleCount;
242
243 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
244 if (!texture.get()) {
245 return NULL;
246 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000247
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000248 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
249}
250
251#ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG
252static SkBitmap make_bitmap(SkBitmap::Config config, int width, int height) {
253 SkBitmap bm;
254 bm.setConfig(SkImageInfo::Make(width, height,
255 SkBitmapConfigToColorType(config),
256 kPremul_SkAlphaType));
257 return bm;
258}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000259SkGpuDevice::SkGpuDevice(GrContext* context,
260 SkBitmap::Config config,
261 int width,
262 int height,
263 int sampleCount)
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000264 : SkBitmapDevice(make_bitmap(config, width, height))
reed@google.combf790232013-12-13 19:45:58 +0000265{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000266 fDrawProcs = NULL;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000267
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000268 fContext = context;
269 fContext->ref();
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000270
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000271 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties));
272 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000273
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000274 fRenderTarget = NULL;
275 fNeedClear = false;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000276
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000277 if (config != SkBitmap::kRGB_565_Config) {
278 config = SkBitmap::kARGB_8888_Config;
279 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000280
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000281 GrTextureDesc desc;
282 desc.fFlags = kRenderTarget_GrTextureFlagBit;
283 desc.fWidth = width;
284 desc.fHeight = height;
285 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
286 desc.fSampleCnt = sampleCount;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000287
reed@google.combf790232013-12-13 19:45:58 +0000288 SkImageInfo info;
289 if (!GrPixelConfig2ColorType(desc.fConfig, &info.fColorType)) {
290 sk_throw();
291 }
292 info.fWidth = width;
293 info.fHeight = height;
294 info.fAlphaType = kPremul_SkAlphaType;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000295
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000296 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000297
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000298 if (NULL != texture) {
299 fRenderTarget = texture->asRenderTarget();
300 fRenderTarget->ref();
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000301
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000302 SkASSERT(NULL != fRenderTarget);
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000303
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000304 // wrap the bitmap with a pixelref to expose our texture
reed@google.combf790232013-12-13 19:45:58 +0000305 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, texture));
reed@google.com672588b2014-01-08 15:42:01 +0000306 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000307 } else {
308 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
309 width, height);
310 SkASSERT(false);
311 }
312}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000313#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000314
315SkGpuDevice::~SkGpuDevice() {
316 if (fDrawProcs) {
317 delete fDrawProcs;
318 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000319
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000320 delete fMainTextContext;
321 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000322
323 // The GrContext takes a ref on the target. We don't want to cause the render
324 // target to be unnecessarily kept alive.
325 if (fContext->getRenderTarget() == fRenderTarget) {
326 fContext->setRenderTarget(NULL);
327 }
328
329 if (fContext->getClip() == &fClipData) {
330 fContext->setClip(NULL);
331 }
332
333 SkSafeUnref(fRenderTarget);
334 fContext->unref();
335}
336
337///////////////////////////////////////////////////////////////////////////////
338
339void SkGpuDevice::makeRenderTargetCurrent() {
340 DO_DEFERRED_CLEAR();
341 fContext->setRenderTarget(fRenderTarget);
342}
343
344///////////////////////////////////////////////////////////////////////////////
345
346namespace {
347GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
348 switch (config8888) {
349 case SkCanvas::kNative_Premul_Config8888:
350 *flags = 0;
351 return kSkia8888_GrPixelConfig;
352 case SkCanvas::kNative_Unpremul_Config8888:
353 *flags = GrContext::kUnpremul_PixelOpsFlag;
354 return kSkia8888_GrPixelConfig;
355 case SkCanvas::kBGRA_Premul_Config8888:
356 *flags = 0;
357 return kBGRA_8888_GrPixelConfig;
358 case SkCanvas::kBGRA_Unpremul_Config8888:
359 *flags = GrContext::kUnpremul_PixelOpsFlag;
360 return kBGRA_8888_GrPixelConfig;
361 case SkCanvas::kRGBA_Premul_Config8888:
362 *flags = 0;
363 return kRGBA_8888_GrPixelConfig;
364 case SkCanvas::kRGBA_Unpremul_Config8888:
365 *flags = GrContext::kUnpremul_PixelOpsFlag;
366 return kRGBA_8888_GrPixelConfig;
367 default:
368 GrCrash("Unexpected Config8888.");
369 *flags = 0; // suppress warning
370 return kSkia8888_GrPixelConfig;
371 }
372}
373}
374
375bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
376 int x, int y,
377 SkCanvas::Config8888 config8888) {
378 DO_DEFERRED_CLEAR();
379 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
380 SkASSERT(!bitmap.isNull());
381 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
382
383 SkAutoLockPixels alp(bitmap);
384 GrPixelConfig config;
385 uint32_t flags;
386 config = config8888_to_grconfig_and_flags(config8888, &flags);
387 return fContext->readRenderTargetPixels(fRenderTarget,
388 x, y,
389 bitmap.width(),
390 bitmap.height(),
391 config,
392 bitmap.getPixels(),
393 bitmap.rowBytes(),
394 flags);
395}
396
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000397bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
398 int x, int y) {
399 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
400 GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType());
401 if (kUnknown_GrPixelConfig == config) {
402 return false;
403 }
404 uint32_t flags = 0;
405 if (kUnpremul_SkAlphaType == info.alphaType()) {
406 flags = GrContext::kUnpremul_PixelOpsFlag;
407 }
408 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
409
410 // need to bump our genID for compatibility with clients that "know" we have a bitmap
411 this->onAccessBitmap().notifyPixelsChanged();
412
413 return true;
414}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000415
416void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
417 INHERITED::onAttachToCanvas(canvas);
418
419 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
420 fClipData.fClipStack = canvas->getClipStack();
421}
422
423void SkGpuDevice::onDetachFromCanvas() {
424 INHERITED::onDetachFromCanvas();
425 fClipData.fClipStack = NULL;
426}
427
428// call this every draw call, to ensure that the context reflects our state,
429// and not the state from some other canvas/device
430void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
431 SkASSERT(NULL != fClipData.fClipStack);
432
433 fContext->setRenderTarget(fRenderTarget);
434
435 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
436
437 if (forceIdentity) {
438 fContext->setIdentityMatrix();
439 } else {
440 fContext->setMatrix(*draw.fMatrix);
441 }
442 fClipData.fOrigin = this->getOrigin();
443
444 fContext->setClip(&fClipData);
445
446 DO_DEFERRED_CLEAR();
447}
448
449GrRenderTarget* SkGpuDevice::accessRenderTarget() {
450 DO_DEFERRED_CLEAR();
451 return fRenderTarget;
452}
453
454///////////////////////////////////////////////////////////////////////////////
455
456SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
457SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
458SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
459SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
460SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
461 shader_type_mismatch);
462SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
463 shader_type_mismatch);
464SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
465SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
466
467namespace {
468
469// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
470// justAlpha indicates that skPaint's alpha should be used rather than the color
471// Callers may subsequently modify the GrPaint. Setting constantColor indicates
472// that the final paint will draw the same color at every pixel. This allows
473// an optimization where the the color filter can be applied to the skPaint's
474// color once while converting to GrPaint and then ignored.
475inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
476 const SkPaint& skPaint,
477 bool justAlpha,
478 bool constantColor,
479 GrPaint* grPaint) {
480
481 grPaint->setDither(skPaint.isDither());
482 grPaint->setAntiAlias(skPaint.isAntiAlias());
483
484 SkXfermode::Coeff sm;
485 SkXfermode::Coeff dm;
486
487 SkXfermode* mode = skPaint.getXfermode();
488 GrEffectRef* xferEffect = NULL;
489 if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
490 if (NULL != xferEffect) {
491 grPaint->addColorEffect(xferEffect)->unref();
492 sm = SkXfermode::kOne_Coeff;
493 dm = SkXfermode::kZero_Coeff;
494 }
495 } else {
496 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
497#if 0
498 return false;
499#else
500 // Fall back to src-over
501 sm = SkXfermode::kOne_Coeff;
502 dm = SkXfermode::kISA_Coeff;
503#endif
504 }
505 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
506
507 if (justAlpha) {
508 uint8_t alpha = skPaint.getAlpha();
509 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
510 // justAlpha is currently set to true only if there is a texture,
511 // so constantColor should not also be true.
512 SkASSERT(!constantColor);
513 } else {
514 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
515 }
516
517 SkColorFilter* colorFilter = skPaint.getColorFilter();
518 if (NULL != colorFilter) {
519 // if the source color is a constant then apply the filter here once rather than per pixel
520 // in a shader.
521 if (constantColor) {
522 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
523 grPaint->setColor(SkColor2GrColor(filtered));
524 } else {
525 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
526 if (NULL != effect.get()) {
527 grPaint->addColorEffect(effect);
528 }
529 }
530 }
531
532 return true;
533}
534
535// This function is similar to skPaint2GrPaintNoShader but also converts
536// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
537// be used is set on grPaint and returned in param act. constantColor has the
538// same meaning as in skPaint2GrPaintNoShader.
539inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
540 const SkPaint& skPaint,
541 bool constantColor,
542 GrPaint* grPaint) {
543 SkShader* shader = skPaint.getShader();
544 if (NULL == shader) {
545 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
546 }
547
commit-bot@chromium.org60770572014-01-13 15:57:05 +0000548 // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state and require
549 // the shader to set a render target .
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550 GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000551
552 // setup the shader as the first color effect on the paint
553 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
554 if (NULL != effect.get()) {
555 grPaint->addColorEffect(effect);
556 // Now setup the rest of the paint.
557 return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
558 } else {
559 // We still don't have SkColorShader::asNewEffect() implemented.
560 SkShader::GradientInfo info;
561 SkColor color;
562
563 info.fColors = &color;
564 info.fColorOffsets = NULL;
565 info.fColorCount = 1;
566 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
567 SkPaint copy(skPaint);
568 copy.setShader(NULL);
569 // modulate the paint alpha by the shader's solid color alpha
570 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
571 copy.setColor(SkColorSetA(color, newA));
572 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
573 } else {
574 return false;
575 }
576 }
577}
578}
579
580///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000581
582SkBitmap::Config SkGpuDevice::config() const {
583 if (NULL == fRenderTarget) {
584 return SkBitmap::kNo_Config;
585 }
586
587 bool isOpaque;
588 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
589}
590
591void SkGpuDevice::clear(SkColor color) {
592 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
593 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
594 fNeedClear = false;
595}
596
597void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
598 CHECK_SHOULD_DRAW(draw, false);
599
600 GrPaint grPaint;
601 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
602 return;
603 }
604
605 fContext->drawPaint(grPaint);
606}
607
608// must be in SkCanvas::PointMode order
609static const GrPrimitiveType gPointMode2PrimtiveType[] = {
610 kPoints_GrPrimitiveType,
611 kLines_GrPrimitiveType,
612 kLineStrip_GrPrimitiveType
613};
614
615void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
616 size_t count, const SkPoint pts[], const SkPaint& paint) {
617 CHECK_FOR_ANNOTATION(paint);
618 CHECK_SHOULD_DRAW(draw, false);
619
620 SkScalar width = paint.getStrokeWidth();
621 if (width < 0) {
622 return;
623 }
624
625 // we only handle hairlines and paints without path effects or mask filters,
626 // else we let the SkDraw call our drawPath()
627 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
628 draw.drawPoints(mode, count, pts, paint, true);
629 return;
630 }
631
632 GrPaint grPaint;
633 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
634 return;
635 }
636
637 fContext->drawVertices(grPaint,
638 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000639 SkToS32(count),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000640 (GrPoint*)pts,
641 NULL,
642 NULL,
643 NULL,
644 0);
645}
646
647///////////////////////////////////////////////////////////////////////////////
648
649void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
650 const SkPaint& paint) {
651 CHECK_FOR_ANNOTATION(paint);
652 CHECK_SHOULD_DRAW(draw, false);
653
654 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
655 SkScalar width = paint.getStrokeWidth();
656
657 /*
658 We have special code for hairline strokes, miter-strokes, bevel-stroke
659 and fills. Anything else we just call our path code.
660 */
661 bool usePath = doStroke && width > 0 &&
662 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
663 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
664 // another two reasons we might need to call drawPath...
665 if (paint.getMaskFilter() || paint.getPathEffect()) {
666 usePath = true;
667 }
668 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
669#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
670 if (doStroke) {
671#endif
672 usePath = true;
673#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
674 } else {
675 usePath = !fContext->getMatrix().preservesRightAngles();
676 }
677#endif
678 }
679 // until we can both stroke and fill rectangles
680 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
681 usePath = true;
682 }
683
684 if (usePath) {
685 SkPath path;
686 path.addRect(rect);
687 this->drawPath(draw, path, paint, NULL, true);
688 return;
689 }
690
691 GrPaint grPaint;
692 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
693 return;
694 }
695
696 if (!doStroke) {
697 fContext->drawRect(grPaint, rect);
698 } else {
699 SkStrokeRec stroke(paint);
700 fContext->drawRect(grPaint, rect, &stroke);
701 }
702}
703
704///////////////////////////////////////////////////////////////////////////////
705
706void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
707 const SkPaint& paint) {
708 CHECK_FOR_ANNOTATION(paint);
709 CHECK_SHOULD_DRAW(draw, false);
710
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000711 GrPaint grPaint;
712 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
713 return;
714 }
715
716 SkStrokeRec stroke(paint);
717 if (paint.getMaskFilter()) {
718 // try to hit the fast path for drawing filtered round rects
719
720 SkRRect devRRect;
721 if (rect.transform(fContext->getMatrix(), &devRRect)) {
722 if (devRRect.allCornersCircular()) {
723 SkRect maskRect;
724 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
725 draw.fClip->getBounds(),
726 fContext->getMatrix(),
727 &maskRect)) {
728 SkIRect finalIRect;
729 maskRect.roundOut(&finalIRect);
730 if (draw.fClip->quickReject(finalIRect)) {
731 // clipped out
732 return;
733 }
734 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
735 // nothing to draw
736 return;
737 }
738 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
739 stroke, devRRect)) {
740 return;
741 }
742 }
743
744 }
745 }
746
747 }
748
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000749 bool usePath = !rect.isSimple();
750 // another two reasons we might need to call drawPath...
751 if (paint.getMaskFilter() || paint.getPathEffect()) {
752 usePath = true;
753 }
754 // until we can rotate rrects...
755 if (!usePath && !fContext->getMatrix().rectStaysRect()) {
756 usePath = true;
757 }
758
759 if (usePath) {
760 SkPath path;
761 path.addRRect(rect);
762 this->drawPath(draw, path, paint, NULL, true);
763 return;
764 }
765
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000766 fContext->drawRRect(grPaint, rect, stroke);
767}
768
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000769/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000770
771void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
772 const SkPaint& paint) {
773 CHECK_FOR_ANNOTATION(paint);
774 CHECK_SHOULD_DRAW(draw, false);
775
776 bool usePath = false;
777 // some basic reasons we might need to call drawPath...
778 if (paint.getMaskFilter() || paint.getPathEffect()) {
779 usePath = true;
780 }
781
782 if (usePath) {
783 SkPath path;
784 path.addOval(oval);
785 this->drawPath(draw, path, paint, NULL, true);
786 return;
787 }
788
789 GrPaint grPaint;
790 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
791 return;
792 }
793 SkStrokeRec stroke(paint);
794
795 fContext->drawOval(grPaint, oval, stroke);
796}
797
798#include "SkMaskFilter.h"
799#include "SkBounder.h"
800
801///////////////////////////////////////////////////////////////////////////////
802
803// helpers for applying mask filters
804namespace {
805
806// Draw a mask using the supplied paint. Since the coverage/geometry
807// is already burnt into the mask this boils down to a rect draw.
808// Return true if the mask was successfully drawn.
809bool draw_mask(GrContext* context, const SkRect& maskRect,
810 GrPaint* grp, GrTexture* mask) {
811 GrContext::AutoMatrix am;
812 if (!am.setIdentity(context, grp)) {
813 return false;
814 }
815
816 SkMatrix matrix;
817 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
818 matrix.postIDiv(mask->width(), mask->height());
819
820 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
821 context->drawRect(*grp, maskRect);
822 return true;
823}
824
825bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
826 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
827 GrPaint* grp, SkPaint::Style style) {
828 SkMask srcM, dstM;
829
830 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
831 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
832 return false;
833 }
834 SkAutoMaskFreeImage autoSrc(srcM.fImage);
835
836 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
837 return false;
838 }
839 // this will free-up dstM when we're done (allocated in filterMask())
840 SkAutoMaskFreeImage autoDst(dstM.fImage);
841
842 if (clip.quickReject(dstM.fBounds)) {
843 return false;
844 }
845 if (bounder && !bounder->doIRect(dstM.fBounds)) {
846 return false;
847 }
848
849 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
850 // the current clip (and identity matrix) and GrPaint settings
851 GrTextureDesc desc;
852 desc.fWidth = dstM.fBounds.width();
853 desc.fHeight = dstM.fBounds.height();
854 desc.fConfig = kAlpha_8_GrPixelConfig;
855
856 GrAutoScratchTexture ast(context, desc);
857 GrTexture* texture = ast.texture();
858
859 if (NULL == texture) {
860 return false;
861 }
862 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
863 dstM.fImage, dstM.fRowBytes);
864
865 SkRect maskRect = SkRect::Make(dstM.fBounds);
866
867 return draw_mask(context, maskRect, grp, texture);
868}
869
870// Create a mask of 'devPath' and place the result in 'mask'. Return true on
871// success; false otherwise.
872bool create_mask_GPU(GrContext* context,
873 const SkRect& maskRect,
874 const SkPath& devPath,
875 const SkStrokeRec& stroke,
876 bool doAA,
877 GrAutoScratchTexture* mask) {
878 GrTextureDesc desc;
879 desc.fFlags = kRenderTarget_GrTextureFlagBit;
880 desc.fWidth = SkScalarCeilToInt(maskRect.width());
881 desc.fHeight = SkScalarCeilToInt(maskRect.height());
882 // We actually only need A8, but it often isn't supported as a
883 // render target so default to RGBA_8888
884 desc.fConfig = kRGBA_8888_GrPixelConfig;
885 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
886 desc.fConfig = kAlpha_8_GrPixelConfig;
887 }
888
889 mask->set(context, desc);
890 if (NULL == mask->texture()) {
891 return false;
892 }
893
894 GrTexture* maskTexture = mask->texture();
895 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
896
897 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
898 GrContext::AutoClip ac(context, clipRect);
899
900 context->clear(NULL, 0x0, true);
901
902 GrPaint tempPaint;
903 if (doAA) {
904 tempPaint.setAntiAlias(true);
905 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
906 // blend coeff of zero requires dual source blending support in order
907 // to properly blend partially covered pixels. This means the AA
908 // code path may not be taken. So we use a dst blend coeff of ISA. We
909 // could special case AA draws to a dst surface with known alpha=0 to
910 // use a zero dst coeff when dual source blending isn't available.
911 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
912 }
913
914 GrContext::AutoMatrix am;
915
916 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
917 SkMatrix translate;
918 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
919 am.set(context, translate);
920 context->drawPath(tempPaint, devPath, stroke);
921 return true;
922}
923
924SkBitmap wrap_texture(GrTexture* texture) {
reed@google.combf790232013-12-13 19:45:58 +0000925 SkImageInfo info;
926 texture->asImageInfo(&info);
927
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000928 SkBitmap result;
reed@google.combf790232013-12-13 19:45:58 +0000929 result.setConfig(info);
930 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000931 return result;
932}
933
934};
935
936void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
937 const SkPaint& paint, const SkMatrix* prePathMatrix,
938 bool pathIsMutable) {
939 CHECK_FOR_ANNOTATION(paint);
940 CHECK_SHOULD_DRAW(draw, false);
941
942 GrPaint grPaint;
943 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
944 return;
945 }
946
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000947 // If we have a prematrix, apply it to the path, optimizing for the case
948 // where the original path can in fact be modified in place (even though
949 // its parameter type is const).
950 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000951 SkTLazy<SkPath> tmpPath;
952 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000953
954 if (prePathMatrix) {
955 SkPath* result = pathPtr;
956
957 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000958 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000959 pathIsMutable = true;
960 }
961 // should I push prePathMatrix on our MV stack temporarily, instead
962 // of applying it here? See SkDraw.cpp
963 pathPtr->transform(*prePathMatrix, result);
964 pathPtr = result;
965 }
966 // at this point we're done with prePathMatrix
967 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
968
969 SkStrokeRec stroke(paint);
970 SkPathEffect* pathEffect = paint.getPathEffect();
971 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000972 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000973 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000974 pathPtr = effectPath.get();
975 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000976 }
977
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000978 if (paint.getMaskFilter()) {
979 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000980 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
981 if (stroke.applyToPath(strokedPath, *pathPtr)) {
982 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000983 pathIsMutable = true;
984 stroke.setFillStyle();
985 }
986 }
987
988 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000989 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000990
991 // transform the path into device space
992 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000993
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000994 SkRect maskRect;
995 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
996 draw.fClip->getBounds(),
997 fContext->getMatrix(),
998 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000999 // The context's matrix may change while creating the mask, so save the CTM here to
1000 // pass to filterMaskGPU.
1001 const SkMatrix ctm = fContext->getMatrix();
1002
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001003 SkIRect finalIRect;
1004 maskRect.roundOut(&finalIRect);
1005 if (draw.fClip->quickReject(finalIRect)) {
1006 // clipped out
1007 return;
1008 }
1009 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
1010 // nothing to draw
1011 return;
1012 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001013
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +00001014 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +00001015 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +00001016 // the mask filter was able to draw itself directly, so there's nothing
1017 // left to do.
1018 return;
1019 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001020
1021 GrAutoScratchTexture mask;
1022
1023 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
1024 grPaint.isAntiAlias(), &mask)) {
1025 GrTexture* filtered;
1026
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +00001027 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +00001028 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001029 // filterMaskGPU gives us ownership of a ref to the result
1030 SkAutoTUnref<GrTexture> atu(filtered);
1031
1032 // If the scratch texture that we used as the filter src also holds the filter
1033 // result then we must detach so that this texture isn't recycled for a later
1034 // draw.
1035 if (filtered == mask.texture()) {
1036 mask.detach();
1037 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
1038 }
1039
1040 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
1041 // This path is completely drawn
1042 return;
1043 }
1044 }
1045 }
1046 }
1047
1048 // draw the mask on the CPU - this is a fallthrough path in case the
1049 // GPU path fails
1050 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
1051 SkPaint::kFill_Style;
1052 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
1053 *draw.fClip, draw.fBounder, &grPaint, style);
1054 return;
1055 }
1056
1057 fContext->drawPath(grPaint, *pathPtr, stroke);
1058}
1059
1060static const int kBmpSmallTileSize = 1 << 10;
1061
1062static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
1063 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
1064 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
1065 return tilesX * tilesY;
1066}
1067
1068static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
1069 if (maxTileSize <= kBmpSmallTileSize) {
1070 return maxTileSize;
1071 }
1072
1073 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
1074 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
1075
1076 maxTileTotalTileSize *= maxTileSize * maxTileSize;
1077 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
1078
1079 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
1080 return kBmpSmallTileSize;
1081 } else {
1082 return maxTileSize;
1083 }
1084}
1085
1086// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
1087// pixels from the bitmap are necessary.
1088static void determine_clipped_src_rect(const GrContext* context,
1089 const SkBitmap& bitmap,
1090 const SkRect* srcRectPtr,
1091 SkIRect* clippedSrcIRect) {
1092 const GrClipData* clip = context->getClip();
1093 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
1094 SkMatrix inv;
1095 if (!context->getMatrix().invert(&inv)) {
1096 clippedSrcIRect->setEmpty();
1097 return;
1098 }
1099 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
1100 inv.mapRect(&clippedSrcRect);
1101 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001102 // we've setup src space 0,0 to map to the top left of the src rect.
1103 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001104 if (!clippedSrcRect.intersect(*srcRectPtr)) {
1105 clippedSrcIRect->setEmpty();
1106 return;
1107 }
1108 }
1109 clippedSrcRect.roundOut(clippedSrcIRect);
1110 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1111 if (!clippedSrcIRect->intersect(bmpBounds)) {
1112 clippedSrcIRect->setEmpty();
1113 }
1114}
1115
1116bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1117 const GrTextureParams& params,
1118 const SkRect* srcRectPtr,
1119 int maxTileSize,
1120 int* tileSize,
1121 SkIRect* clippedSrcRect) const {
1122 // if bitmap is explictly texture backed then just use the texture
1123 if (NULL != bitmap.getTexture()) {
1124 return false;
1125 }
1126
1127 // if it's larger than the max tile size, then we have no choice but tiling.
1128 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
1129 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1130 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
1131 return true;
1132 }
1133
1134 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
1135 return false;
1136 }
1137
1138 // if the entire texture is already in our cache then no reason to tile it
1139 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
1140 return false;
1141 }
1142
1143 // At this point we know we could do the draw by uploading the entire bitmap
1144 // as a texture. However, if the texture would be large compared to the
1145 // cache size and we don't require most of it for this draw then tile to
1146 // reduce the amount of upload and cache spill.
1147
1148 // assumption here is that sw bitmap size is a good proxy for its size as
1149 // a texture
1150 size_t bmpSize = bitmap.getSize();
1151 size_t cacheSize;
1152 fContext->getTextureCacheLimits(NULL, &cacheSize);
1153 if (bmpSize < cacheSize / 2) {
1154 return false;
1155 }
1156
1157 // Figure out how much of the src we will need based on the src rect and clipping.
1158 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1159 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1160 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
1161 kBmpSmallTileSize * kBmpSmallTileSize;
1162
1163 return usedTileBytes < 2 * bmpSize;
1164}
1165
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001166void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167 const SkBitmap& bitmap,
1168 const SkMatrix& m,
1169 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001170 SkMatrix concat;
1171 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1172 if (!m.isIdentity()) {
1173 concat.setConcat(*draw->fMatrix, m);
1174 draw.writable()->fMatrix = &concat;
1175 }
1176 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001177}
1178
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001179// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001180// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1181// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001182static inline void clamped_outset_with_offset(SkIRect* iRect,
1183 int outset,
1184 SkPoint* offset,
1185 const SkIRect& clamp) {
1186 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001187
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001188 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1189 if (leftClampDelta > 0) {
1190 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001191 iRect->fLeft = clamp.fLeft;
1192 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001193 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001194 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001195
1196 int topClampDelta = clamp.fTop - iRect->fTop;
1197 if (topClampDelta > 0) {
1198 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001199 iRect->fTop = clamp.fTop;
1200 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001201 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001202 }
1203
1204 if (iRect->fRight > clamp.fRight) {
1205 iRect->fRight = clamp.fRight;
1206 }
1207 if (iRect->fBottom > clamp.fBottom) {
1208 iRect->fBottom = clamp.fBottom;
1209 }
1210}
1211
1212void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1213 const SkBitmap& bitmap,
1214 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001215 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001216 const SkPaint& paint,
1217 SkCanvas::DrawBitmapRectFlags flags) {
1218 CHECK_SHOULD_DRAW(draw, false);
1219
1220 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001221 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001222 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1223 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001224 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001225 SkScalar w = SkIntToScalar(bitmap.width());
1226 SkScalar h = SkIntToScalar(bitmap.height());
1227 dstSize.fWidth = w;
1228 dstSize.fHeight = h;
1229 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001230 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001231 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001232 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001233 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001234 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001235 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1236 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1237 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1238 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001239 }
1240
1241 if (paint.getMaskFilter()){
1242 // Convert the bitmap to a shader so that the rect can be drawn
1243 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001244 SkBitmap tmp; // subset of bitmap, if necessary
1245 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001246 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001247 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001248 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1249 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1250 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001251 // In bleed mode we position and trim the bitmap based on the src rect which is
1252 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1253 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1254 // compensate.
1255 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1256 SkIRect iSrc;
1257 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001258
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001259 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1260 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001261
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001262 if (!bitmap.extractSubset(&tmp, iSrc)) {
1263 return; // extraction failed
1264 }
1265 bitmapPtr = &tmp;
1266 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001267
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001268 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001269 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001270 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001271 } else {
1272 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001273 }
1274
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001275 SkPaint paintWithShader(paint);
1276 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001277 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001278 paintWithShader.getShader()->setLocalMatrix(localM);
1279 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1280 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001281
1282 return;
1283 }
1284
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001285 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1286 // the view matrix rather than a local matrix.
1287 SkMatrix m;
1288 m.setScale(dstSize.fWidth / srcRect.width(),
1289 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001290 fContext->concatMatrix(m);
1291
1292 GrTextureParams params;
1293 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1294 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001295
1296 int tileFilterPad;
1297 bool doBicubic = false;
1298
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001299 switch(paintFilterLevel) {
1300 case SkPaint::kNone_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001301 tileFilterPad = 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001302 textureFilterMode = GrTextureParams::kNone_FilterMode;
1303 break;
1304 case SkPaint::kLow_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001305 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001306 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1307 break;
1308 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001309 tileFilterPad = 1;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001310 if (fContext->getMatrix().getMinStretch() < SK_Scalar1) {
1311 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1312 } else {
1313 // Don't trigger MIP level generation unnecessarily.
1314 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1315 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001316 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001317 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001318 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001319 if (fContext->getMatrix().getMinStretch() >= SK_Scalar1) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001320 // We will install an effect that does the filtering in the shader.
1321 textureFilterMode = GrTextureParams::kNone_FilterMode;
1322 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1323 doBicubic = true;
1324 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001325 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1326 tileFilterPad = 1;
1327 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001328 break;
1329 default:
1330 SkErrorInternals::SetError( kInvalidPaint_SkError,
1331 "Sorry, I don't understand the filtering "
1332 "mode you asked for. Falling back to "
1333 "MIPMaps.");
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001334 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001335 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1336 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001337 }
1338
1339 params.setFilterMode(textureFilterMode);
1340
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001341 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001342 int tileSize;
1343
1344 SkIRect clippedSrcRect;
1345 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1346 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001347 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1348 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001349 } else {
1350 // take the simple case
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001351 this->internalDrawBitmap(bitmap, srcRect, params, paint, flags, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001352 }
1353}
1354
1355// Break 'bitmap' into several tiles to draw it since it has already
1356// been determined to be too large to fit in VRAM
1357void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1358 const SkRect& srcRect,
1359 const SkIRect& clippedSrcIRect,
1360 const GrTextureParams& params,
1361 const SkPaint& paint,
1362 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001363 int tileSize,
1364 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001365 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1366
1367 int nx = bitmap.width() / tileSize;
1368 int ny = bitmap.height() / tileSize;
1369 for (int x = 0; x <= nx; x++) {
1370 for (int y = 0; y <= ny; y++) {
1371 SkRect tileR;
1372 tileR.set(SkIntToScalar(x * tileSize),
1373 SkIntToScalar(y * tileSize),
1374 SkIntToScalar((x + 1) * tileSize),
1375 SkIntToScalar((y + 1) * tileSize));
1376
1377 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1378 continue;
1379 }
1380
1381 if (!tileR.intersect(srcRect)) {
1382 continue;
1383 }
1384
1385 SkBitmap tmpB;
1386 SkIRect iTileR;
1387 tileR.roundOut(&iTileR);
1388 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1389 SkIntToScalar(iTileR.fTop));
1390
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001391 // Adjust the context matrix to draw at the right x,y in device space
1392 SkMatrix tmpM;
1393 GrContext::AutoMatrix am;
1394 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1395 am.setPreConcat(fContext, tmpM);
1396
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001397 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001398 SkIRect iClampRect;
1399
1400 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1401 // In bleed mode we want to always expand the tile on all edges
1402 // but stay within the bitmap bounds
1403 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1404 } else {
1405 // In texture-domain/clamp mode we only want to expand the
1406 // tile on edges interior to "srcRect" (i.e., we want to
1407 // not bleed across the original clamped edges)
1408 srcRect.roundOut(&iClampRect);
1409 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001410 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1411 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001412 }
1413
1414 if (bitmap.extractSubset(&tmpB, iTileR)) {
1415 // now offset it to make it "local" to our tmp bitmap
1416 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001417
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001418 this->internalDrawBitmap(tmpB, tileR, params, paint, flags, bicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001419 }
1420 }
1421 }
1422}
1423
1424static bool has_aligned_samples(const SkRect& srcRect,
1425 const SkRect& transformedRect) {
1426 // detect pixel disalignment
1427 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1428 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1429 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1430 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1431 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1432 COLOR_BLEED_TOLERANCE &&
1433 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1434 COLOR_BLEED_TOLERANCE) {
1435 return true;
1436 }
1437 return false;
1438}
1439
1440static bool may_color_bleed(const SkRect& srcRect,
1441 const SkRect& transformedRect,
1442 const SkMatrix& m) {
1443 // Only gets called if has_aligned_samples returned false.
1444 // So we can assume that sampling is axis aligned but not texel aligned.
1445 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1446 SkRect innerSrcRect(srcRect), innerTransformedRect,
1447 outerTransformedRect(transformedRect);
1448 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1449 m.mapRect(&innerTransformedRect, innerSrcRect);
1450
1451 // The gap between outerTransformedRect and innerTransformedRect
1452 // represents the projection of the source border area, which is
1453 // problematic for color bleeding. We must check whether any
1454 // destination pixels sample the border area.
1455 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1456 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1457 SkIRect outer, inner;
1458 outerTransformedRect.round(&outer);
1459 innerTransformedRect.round(&inner);
1460 // If the inner and outer rects round to the same result, it means the
1461 // border does not overlap any pixel centers. Yay!
1462 return inner != outer;
1463}
1464
1465
1466/*
1467 * This is called by drawBitmap(), which has to handle images that may be too
1468 * large to be represented by a single texture.
1469 *
1470 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1471 * and that non-texture portion of the GrPaint has already been setup.
1472 */
1473void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1474 const SkRect& srcRect,
1475 const GrTextureParams& params,
1476 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001477 SkCanvas::DrawBitmapRectFlags flags,
1478 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001479 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1480 bitmap.height() <= fContext->getMaxTextureSize());
1481
1482 GrTexture* texture;
1483 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1484 if (NULL == texture) {
1485 return;
1486 }
1487
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001488 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001489 SkRect paintRect;
1490 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1491 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1492 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1493 SkScalarMul(srcRect.fTop, hInv),
1494 SkScalarMul(srcRect.fRight, wInv),
1495 SkScalarMul(srcRect.fBottom, hInv));
1496
1497 bool needsTextureDomain = false;
1498 if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) &&
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001499 (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode)) {
1500 // Need texture domain if drawing a sub rect
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001501 needsTextureDomain = srcRect.width() < bitmap.width() ||
1502 srcRect.height() < bitmap.height();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001503 if (!bicubic && needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001504 const SkMatrix& matrix = fContext->getMatrix();
1505 // sampling is axis-aligned
1506 SkRect transformedRect;
1507 matrix.mapRect(&transformedRect, srcRect);
1508
1509 if (has_aligned_samples(srcRect, transformedRect)) {
1510 // We could also turn off filtering here (but we already did a cache lookup with
1511 // params).
1512 needsTextureDomain = false;
1513 } else {
1514 needsTextureDomain = may_color_bleed(srcRect, transformedRect, matrix);
1515 }
1516 }
1517 }
1518
1519 SkRect textureDomain = SkRect::MakeEmpty();
1520 SkAutoTUnref<GrEffectRef> effect;
1521 if (needsTextureDomain) {
1522 // Use a constrained texture domain to avoid color bleeding
1523 SkScalar left, top, right, bottom;
1524 if (srcRect.width() > SK_Scalar1) {
1525 SkScalar border = SK_ScalarHalf / texture->width();
1526 left = paintRect.left() + border;
1527 right = paintRect.right() - border;
1528 } else {
1529 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1530 }
1531 if (srcRect.height() > SK_Scalar1) {
1532 SkScalar border = SK_ScalarHalf / texture->height();
1533 top = paintRect.top() + border;
1534 bottom = paintRect.bottom() - border;
1535 } else {
1536 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1537 }
1538 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001539 if (bicubic) {
1540 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1541 } else {
1542 effect.reset(GrTextureDomainEffect::Create(texture,
1543 SkMatrix::I(),
1544 textureDomain,
1545 GrTextureDomain::kClamp_Mode,
1546 params.filterMode()));
1547 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001548 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001549 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1550 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1551 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001552 } else {
1553 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1554 }
1555
1556 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1557 // the rest from the SkPaint.
1558 GrPaint grPaint;
1559 grPaint.addColorEffect(effect);
1560 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1561 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1562 return;
1563 }
1564
1565 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1566}
1567
1568static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001569 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001570 int w, int h, const SkImageFilter::Context& ctx,
1571 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001572 SkASSERT(filter);
1573 SkDeviceImageFilterProxy proxy(device);
1574
1575 if (filter->canFilterImageGPU()) {
1576 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1577 // filter. Also set the clip wide open and the matrix to identity.
1578 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001579 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001580 } else {
1581 return false;
1582 }
1583}
1584
1585void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1586 int left, int top, const SkPaint& paint) {
1587 // drawSprite is defined to be in device coords.
1588 CHECK_SHOULD_DRAW(draw, true);
1589
1590 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1591 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1592 return;
1593 }
1594
1595 int w = bitmap.width();
1596 int h = bitmap.height();
1597
1598 GrTexture* texture;
1599 // draw sprite uses the default texture params
1600 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1601
1602 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001603 // This bitmap will own the filtered result as a texture.
1604 SkBitmap filteredBitmap;
1605
1606 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001607 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001608 SkMatrix matrix(*draw.fMatrix);
1609 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001610 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1611 SkImageFilter::Context ctx(matrix, clipBounds);
1612 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001613 &offset)) {
1614 texture = (GrTexture*) filteredBitmap.getTexture();
1615 w = filteredBitmap.width();
1616 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001617 left += offset.x();
1618 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001619 } else {
1620 return;
1621 }
1622 }
1623
1624 GrPaint grPaint;
1625 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1626
1627 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1628 return;
1629 }
1630
1631 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001632 SkRect::MakeXYWH(SkIntToScalar(left),
1633 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001634 SkIntToScalar(w),
1635 SkIntToScalar(h)),
1636 SkRect::MakeXYWH(0,
1637 0,
1638 SK_Scalar1 * w / texture->width(),
1639 SK_Scalar1 * h / texture->height()));
1640}
1641
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001642void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001643 const SkRect* src, const SkRect& dst,
1644 const SkPaint& paint,
1645 SkCanvas::DrawBitmapRectFlags flags) {
1646 SkMatrix matrix;
1647 SkRect bitmapBounds, tmpSrc;
1648
1649 bitmapBounds.set(0, 0,
1650 SkIntToScalar(bitmap.width()),
1651 SkIntToScalar(bitmap.height()));
1652
1653 // Compute matrix from the two rectangles
1654 if (NULL != src) {
1655 tmpSrc = *src;
1656 } else {
1657 tmpSrc = bitmapBounds;
1658 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001659
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001660 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1661
1662 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1663 if (NULL != src) {
1664 if (!bitmapBounds.contains(tmpSrc)) {
1665 if (!tmpSrc.intersect(bitmapBounds)) {
1666 return; // nothing to draw
1667 }
1668 }
1669 }
1670
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001671 SkRect tmpDst;
1672 matrix.mapRect(&tmpDst, tmpSrc);
1673
1674 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1675 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1676 // Translate so that tempDst's top left is at the origin.
1677 matrix = *origDraw.fMatrix;
1678 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1679 draw.writable()->fMatrix = &matrix;
1680 }
1681 SkSize dstSize;
1682 dstSize.fWidth = tmpDst.width();
1683 dstSize.fHeight = tmpDst.height();
1684
1685 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001686}
1687
1688void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1689 int x, int y, const SkPaint& paint) {
1690 // clear of the source device must occur before CHECK_SHOULD_DRAW
1691 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1692 if (dev->fNeedClear) {
1693 // TODO: could check here whether we really need to draw at all
1694 dev->clear(0x0);
1695 }
1696
1697 // drawDevice is defined to be in device coords.
1698 CHECK_SHOULD_DRAW(draw, true);
1699
1700 GrRenderTarget* devRT = dev->accessRenderTarget();
1701 GrTexture* devTex;
1702 if (NULL == (devTex = devRT->asTexture())) {
1703 return;
1704 }
1705
1706 const SkBitmap& bm = dev->accessBitmap(false);
1707 int w = bm.width();
1708 int h = bm.height();
1709
1710 SkImageFilter* filter = paint.getImageFilter();
1711 // This bitmap will own the filtered result as a texture.
1712 SkBitmap filteredBitmap;
1713
1714 if (NULL != filter) {
1715 SkIPoint offset = SkIPoint::Make(0, 0);
1716 SkMatrix matrix(*draw.fMatrix);
1717 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001718 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
1719 SkImageFilter::Context ctx(matrix, clipBounds);
1720 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001721 &offset)) {
1722 devTex = filteredBitmap.getTexture();
1723 w = filteredBitmap.width();
1724 h = filteredBitmap.height();
1725 x += offset.fX;
1726 y += offset.fY;
1727 } else {
1728 return;
1729 }
1730 }
1731
1732 GrPaint grPaint;
1733 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1734
1735 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1736 return;
1737 }
1738
1739 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1740 SkIntToScalar(y),
1741 SkIntToScalar(w),
1742 SkIntToScalar(h));
1743
1744 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1745 // scratch texture).
1746 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1747 SK_Scalar1 * h / devTex->height());
1748
1749 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1750}
1751
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001752bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001753 return filter->canFilterImageGPU();
1754}
1755
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001756bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001757 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001758 SkBitmap* result, SkIPoint* offset) {
1759 // want explicitly our impl, so guard against a subclass of us overriding it
1760 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1761 return false;
1762 }
1763
1764 SkAutoLockPixels alp(src, !src.getTexture());
1765 if (!src.getTexture() && !src.readyToDraw()) {
1766 return false;
1767 }
1768
1769 GrTexture* texture;
1770 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1771 // must be pushed upstack.
1772 SkAutoCachedTexture act(this, src, NULL, &texture);
1773
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001774 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1775 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001776}
1777
1778///////////////////////////////////////////////////////////////////////////////
1779
1780// must be in SkCanvas::VertexMode order
1781static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1782 kTriangles_GrPrimitiveType,
1783 kTriangleStrip_GrPrimitiveType,
1784 kTriangleFan_GrPrimitiveType,
1785};
1786
1787void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1788 int vertexCount, const SkPoint vertices[],
1789 const SkPoint texs[], const SkColor colors[],
1790 SkXfermode* xmode,
1791 const uint16_t indices[], int indexCount,
1792 const SkPaint& paint) {
1793 CHECK_SHOULD_DRAW(draw, false);
1794
1795 GrPaint grPaint;
1796 // we ignore the shader if texs is null.
1797 if (NULL == texs) {
1798 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
1799 return;
1800 }
1801 } else {
1802 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
1803 return;
1804 }
1805 }
1806
1807 if (NULL != xmode && NULL != texs && NULL != colors) {
1808 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1809 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1810#if 0
1811 return
1812#endif
1813 }
1814 }
1815
1816 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1817 if (NULL != colors) {
1818 // need to convert byte order and from non-PM to PM
1819 convertedColors.reset(vertexCount);
1820 for (int i = 0; i < vertexCount; ++i) {
1821 convertedColors[i] = SkColor2GrColor(colors[i]);
1822 }
1823 colors = convertedColors.get();
1824 }
1825 fContext->drawVertices(grPaint,
1826 gVertexMode2PrimitiveType[vmode],
1827 vertexCount,
1828 (GrPoint*) vertices,
1829 (GrPoint*) texs,
1830 colors,
1831 indices,
1832 indexCount);
1833}
1834
1835///////////////////////////////////////////////////////////////////////////////
1836
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001837void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1838 size_t byteLength, SkScalar x, SkScalar y,
1839 const SkPaint& paint) {
1840 CHECK_SHOULD_DRAW(draw, false);
1841
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001842 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001843 GrPaint grPaint;
1844 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1845 return;
1846 }
1847
1848 SkDEBUGCODE(this->validate();)
1849
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001850 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1851 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001852 GrPaint grPaint;
1853 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1854 return;
1855 }
1856
1857 SkDEBUGCODE(this->validate();)
1858
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001859 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001860 } else {
1861 // this guy will just call our drawPath()
1862 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001863 }
1864}
1865
1866void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1867 size_t byteLength, const SkScalar pos[],
1868 SkScalar constY, int scalarsPerPos,
1869 const SkPaint& paint) {
1870 CHECK_SHOULD_DRAW(draw, false);
1871
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001872 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001873 GrPaint grPaint;
1874 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1875 return;
1876 }
1877
1878 SkDEBUGCODE(this->validate();)
1879
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001880 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001881 constY, scalarsPerPos);
1882 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001883 GrPaint grPaint;
1884 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1885 return;
1886 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001887
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001888 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001889
1890 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001891 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001892 } else {
1893 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1894 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001895 }
1896}
1897
1898void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1899 size_t len, const SkPath& path,
1900 const SkMatrix* m, const SkPaint& paint) {
1901 CHECK_SHOULD_DRAW(draw, false);
1902
1903 SkASSERT(draw.fDevice == this);
1904 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1905}
1906
1907///////////////////////////////////////////////////////////////////////////////
1908
1909bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1910 if (!paint.isLCDRenderText()) {
1911 // we're cool with the paint as is
1912 return false;
1913 }
1914
1915 if (paint.getShader() ||
1916 paint.getXfermode() || // unless its srcover
1917 paint.getMaskFilter() ||
1918 paint.getRasterizer() ||
1919 paint.getColorFilter() ||
1920 paint.getPathEffect() ||
1921 paint.isFakeBoldText() ||
1922 paint.getStyle() != SkPaint::kFill_Style) {
1923 // turn off lcd
1924 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1925 flags->fHinting = paint.getHinting();
1926 return true;
1927 }
1928 // we're cool with the paint as is
1929 return false;
1930}
1931
1932void SkGpuDevice::flush() {
1933 DO_DEFERRED_CLEAR();
1934 fContext->resolveRenderTarget(fRenderTarget);
1935}
1936
1937///////////////////////////////////////////////////////////////////////////////
1938
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001939SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001940 GrTextureDesc desc;
1941 desc.fConfig = fRenderTarget->config();
1942 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001943 desc.fWidth = info.width();
1944 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001945 desc.fSampleCnt = fRenderTarget->numSamples();
1946
1947 SkAutoTUnref<GrTexture> texture;
1948 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001949 bool needClear = !info.isOpaque();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001950
1951#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1952 // layers are never draw in repeat modes, so we can request an approx
1953 // match and ignore any padding.
1954 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1955 GrContext::kApprox_ScratchTexMatch :
1956 GrContext::kExact_ScratchTexMatch;
1957 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1958#else
1959 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1960#endif
1961 if (NULL != texture.get()) {
1962 return SkNEW_ARGS(SkGpuDevice,(fContext, texture, needClear));
1963 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001964 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1965 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001966 return NULL;
1967 }
1968}
1969
reed@google.com76f10a32014-02-05 15:32:21 +00001970SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1971 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1972}
1973
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001974SkGpuDevice::SkGpuDevice(GrContext* context,
1975 GrTexture* texture,
1976 bool needClear)
1977 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
1978
1979 SkASSERT(texture && texture->asRenderTarget());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001980 // This constructor is called from onCreateDevice. It has locked the RT in the texture
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001981 // cache. We pass true for the third argument so that it will get unlocked.
1982 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
1983 fNeedClear = needClear;
1984}
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001985
1986class GPUAccelData : public SkPicture::AccelData {
1987public:
1988 GPUAccelData(Key key) : INHERITED(key) { }
1989
1990protected:
1991
1992private:
1993 typedef SkPicture::AccelData INHERITED;
1994};
1995
1996// In the future this may not be a static method if we need to incorporate the
1997// clip and matrix state into the key
1998SkPicture::AccelData::Key SkGpuDevice::ComputeAccelDataKey() {
1999 static const SkPicture::AccelData::Key gGPUID = SkPicture::AccelData::GenerateDomain();
2000
2001 return gGPUID;
2002}
2003
2004void SkGpuDevice::EXPERIMENTAL_optimize(SkPicture* picture) {
2005 SkPicture::AccelData::Key key = ComputeAccelDataKey();
2006
2007 GPUAccelData* data = SkNEW_ARGS(GPUAccelData, (key));
2008
2009 picture->EXPERIMENTAL_addAccelData(data);
2010}
2011
2012bool SkGpuDevice::EXPERIMENTAL_drawPicture(const SkPicture& picture) {
2013 SkPicture::AccelData::Key key = ComputeAccelDataKey();
2014
2015 const SkPicture::AccelData* data = picture.EXPERIMENTAL_getAccelData(key);
2016 if (NULL == data) {
2017 return false;
2018 }
2019
2020#if 0
2021 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
2022#endif
2023
2024 return false;
2025}