blob: 7cecf25bb2d834ce60976e37f280f13ceb5a86c3 [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"
16#if SK_DISTANCEFIELD_FONTS
17#include "GrDistanceFieldTextContext.h"
18#endif
19
20#include "SkGrTexturePixelRef.h"
21
22#include "SkColorFilter.h"
23#include "SkDeviceImageFilterProxy.h"
24#include "SkDrawProcs.h"
25#include "SkGlyphCache.h"
26#include "SkImageFilter.h"
27#include "SkPathEffect.h"
28#include "SkRRect.h"
29#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000030#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000031#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000032#include "SkUtils.h"
33#include "SkErrorInternals.h"
34
35#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
36
37#if 0
38 extern bool (*gShouldDrawProc)();
39 #define CHECK_SHOULD_DRAW(draw, forceI) \
40 do { \
41 if (gShouldDrawProc && !gShouldDrawProc()) return; \
42 this->prepareDraw(draw, forceI); \
43 } while (0)
44#else
45 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
46#endif
47
48// This constant represents the screen alignment criterion in texels for
49// requiring texture domain clamping to prevent color bleeding when drawing
50// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000051#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000052
53#define DO_DEFERRED_CLEAR() \
54 do { \
55 if (fNeedClear) { \
56 this->clear(SK_ColorTRANSPARENT); \
57 } \
58 } while (false) \
59
60///////////////////////////////////////////////////////////////////////////////
61
62#define CHECK_FOR_ANNOTATION(paint) \
63 do { if (paint.getAnnotation()) { return; } } while (0)
64
65///////////////////////////////////////////////////////////////////////////////
66
67
68class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
69public:
70 SkAutoCachedTexture()
71 : fDevice(NULL)
72 , fTexture(NULL) {
73 }
74
75 SkAutoCachedTexture(SkGpuDevice* device,
76 const SkBitmap& bitmap,
77 const GrTextureParams* params,
78 GrTexture** texture)
79 : fDevice(NULL)
80 , fTexture(NULL) {
81 SkASSERT(NULL != texture);
82 *texture = this->set(device, bitmap, params);
83 }
84
85 ~SkAutoCachedTexture() {
86 if (NULL != fTexture) {
87 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
88 }
89 }
90
91 GrTexture* set(SkGpuDevice* device,
92 const SkBitmap& bitmap,
93 const GrTextureParams* params) {
94 if (NULL != fTexture) {
95 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
96 fTexture = NULL;
97 }
98 fDevice = device;
99 GrTexture* result = (GrTexture*)bitmap.getTexture();
100 if (NULL == result) {
101 // Cannot return the native texture so look it up in our cache
102 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
103 result = fTexture;
104 }
105 return result;
106 }
107
108private:
109 SkGpuDevice* fDevice;
110 GrTexture* fTexture;
111};
112
113///////////////////////////////////////////////////////////////////////////////
114
115struct GrSkDrawProcs : public SkDrawProcs {
116public:
117 GrContext* fContext;
118 GrTextContext* fTextContext;
119 GrFontScaler* fFontScaler; // cached in the skia glyphcache
120};
121
122///////////////////////////////////////////////////////////////////////////////
123
124static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
125 switch (config) {
126 case kAlpha_8_GrPixelConfig:
127 *isOpaque = false;
128 return SkBitmap::kA8_Config;
129 case kRGB_565_GrPixelConfig:
130 *isOpaque = true;
131 return SkBitmap::kRGB_565_Config;
132 case kRGBA_4444_GrPixelConfig:
133 *isOpaque = false;
134 return SkBitmap::kARGB_4444_Config;
135 case kSkia8888_GrPixelConfig:
136 // we don't currently have a way of knowing whether
137 // a 8888 is opaque based on the config.
138 *isOpaque = false;
139 return SkBitmap::kARGB_8888_Config;
140 default:
141 *isOpaque = false;
142 return SkBitmap::kNo_Config;
143 }
144}
145
146/*
147 * GrRenderTarget does not know its opaqueness, only its config, so we have
148 * to make conservative guesses when we return an "equivalent" bitmap.
149 */
150static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
151 bool isOpaque;
152 SkBitmap::Config config = grConfig2skConfig(renderTarget->config(), &isOpaque);
153
154 SkBitmap bitmap;
155 bitmap.setConfig(config, renderTarget->width(), renderTarget->height(), 0,
156 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
157 return bitmap;
158}
159
160SkGpuDevice* SkGpuDevice::Create(GrSurface* surface) {
161 SkASSERT(NULL != surface);
162 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
163 return NULL;
164 }
165 if (surface->asTexture()) {
166 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture()));
167 } else {
168 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget()));
169 }
170}
171
172SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
173 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
174 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
175}
176
177SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
178 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
179 this->initFromRenderTarget(context, renderTarget, false);
180}
181
182void SkGpuDevice::initFromRenderTarget(GrContext* context,
183 GrRenderTarget* renderTarget,
184 bool cached) {
185 fDrawProcs = NULL;
186
187 fContext = context;
188 fContext->ref();
189
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +0000190#if SK_DISTANCEFIELD_FONTS
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#else
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000194 fMainTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
195 fFallbackTextContext = NULL;
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +0000196#endif
197
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000198 fRenderTarget = NULL;
199 fNeedClear = false;
200
201 SkASSERT(NULL != renderTarget);
202 fRenderTarget = renderTarget;
203 fRenderTarget->ref();
204
205 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
206 // on the RT but not vice-versa.
207 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
208 // busting chrome (for a currently unknown reason).
209 GrSurface* surface = fRenderTarget->asTexture();
210 if (NULL == surface) {
211 surface = fRenderTarget;
212 }
reed@google.combf790232013-12-13 19:45:58 +0000213
214 SkImageInfo info;
215 surface->asImageInfo(&info);
216 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, cached));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000217
reed@google.com672588b2014-01-08 15:42:01 +0000218 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000219}
220
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000221SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
222 int sampleCount) {
223 if (kUnknown_SkColorType == origInfo.colorType() ||
224 origInfo.width() < 0 || origInfo.height() < 0) {
225 return NULL;
226 }
227
228 SkImageInfo info = origInfo;
229 // TODO: perhas we can loosen this check now that colortype is more detailed
230 // e.g. can we support both RGBA and BGRA here?
231 if (kRGB_565_SkColorType == info.colorType()) {
232 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
233 } else {
234 info.fColorType = kPMColor_SkColorType;
235 if (kOpaque_SkAlphaType != info.alphaType()) {
236 info.fAlphaType = kPremul_SkAlphaType; // force this setting
237 }
238 }
239
240 GrTextureDesc desc;
241 desc.fFlags = kRenderTarget_GrTextureFlagBit;
242 desc.fWidth = info.width();
243 desc.fHeight = info.height();
244 desc.fConfig = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType());
245 desc.fSampleCnt = sampleCount;
246
247 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
248 if (!texture.get()) {
249 return NULL;
250 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000251
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000252 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
253}
254
255#ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG
256static SkBitmap make_bitmap(SkBitmap::Config config, int width, int height) {
257 SkBitmap bm;
258 bm.setConfig(SkImageInfo::Make(width, height,
259 SkBitmapConfigToColorType(config),
260 kPremul_SkAlphaType));
261 return bm;
262}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000263SkGpuDevice::SkGpuDevice(GrContext* context,
264 SkBitmap::Config config,
265 int width,
266 int height,
267 int sampleCount)
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000268 : SkBitmapDevice(make_bitmap(config, width, height))
reed@google.combf790232013-12-13 19:45:58 +0000269{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000270 fDrawProcs = NULL;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000271
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000272 fContext = context;
273 fContext->ref();
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000274
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +0000275#if SK_DISTANCEFIELD_FONTS
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000276 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties));
277 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +0000278#else
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000279 fMainTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
280 fFallbackTextContext = NULL;
commit-bot@chromium.orgcc40f062014-01-24 14:38:27 +0000281#endif
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000282
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000283 fRenderTarget = NULL;
284 fNeedClear = false;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000285
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000286 if (config != SkBitmap::kRGB_565_Config) {
287 config = SkBitmap::kARGB_8888_Config;
288 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000289
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000290 GrTextureDesc desc;
291 desc.fFlags = kRenderTarget_GrTextureFlagBit;
292 desc.fWidth = width;
293 desc.fHeight = height;
294 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
295 desc.fSampleCnt = sampleCount;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000296
reed@google.combf790232013-12-13 19:45:58 +0000297 SkImageInfo info;
298 if (!GrPixelConfig2ColorType(desc.fConfig, &info.fColorType)) {
299 sk_throw();
300 }
301 info.fWidth = width;
302 info.fHeight = height;
303 info.fAlphaType = kPremul_SkAlphaType;
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000304
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000305 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000306
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000307 if (NULL != texture) {
308 fRenderTarget = texture->asRenderTarget();
309 fRenderTarget->ref();
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000310
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000311 SkASSERT(NULL != fRenderTarget);
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000312
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000313 // wrap the bitmap with a pixelref to expose our texture
reed@google.combf790232013-12-13 19:45:58 +0000314 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, texture));
reed@google.com672588b2014-01-08 15:42:01 +0000315 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000316 } else {
317 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
318 width, height);
319 SkASSERT(false);
320 }
321}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000322#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323
324SkGpuDevice::~SkGpuDevice() {
325 if (fDrawProcs) {
326 delete fDrawProcs;
327 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000328
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000329 delete fMainTextContext;
330 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000331
332 // The GrContext takes a ref on the target. We don't want to cause the render
333 // target to be unnecessarily kept alive.
334 if (fContext->getRenderTarget() == fRenderTarget) {
335 fContext->setRenderTarget(NULL);
336 }
337
338 if (fContext->getClip() == &fClipData) {
339 fContext->setClip(NULL);
340 }
341
342 SkSafeUnref(fRenderTarget);
343 fContext->unref();
344}
345
346///////////////////////////////////////////////////////////////////////////////
347
348void SkGpuDevice::makeRenderTargetCurrent() {
349 DO_DEFERRED_CLEAR();
350 fContext->setRenderTarget(fRenderTarget);
351}
352
353///////////////////////////////////////////////////////////////////////////////
354
355namespace {
356GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
357 switch (config8888) {
358 case SkCanvas::kNative_Premul_Config8888:
359 *flags = 0;
360 return kSkia8888_GrPixelConfig;
361 case SkCanvas::kNative_Unpremul_Config8888:
362 *flags = GrContext::kUnpremul_PixelOpsFlag;
363 return kSkia8888_GrPixelConfig;
364 case SkCanvas::kBGRA_Premul_Config8888:
365 *flags = 0;
366 return kBGRA_8888_GrPixelConfig;
367 case SkCanvas::kBGRA_Unpremul_Config8888:
368 *flags = GrContext::kUnpremul_PixelOpsFlag;
369 return kBGRA_8888_GrPixelConfig;
370 case SkCanvas::kRGBA_Premul_Config8888:
371 *flags = 0;
372 return kRGBA_8888_GrPixelConfig;
373 case SkCanvas::kRGBA_Unpremul_Config8888:
374 *flags = GrContext::kUnpremul_PixelOpsFlag;
375 return kRGBA_8888_GrPixelConfig;
376 default:
377 GrCrash("Unexpected Config8888.");
378 *flags = 0; // suppress warning
379 return kSkia8888_GrPixelConfig;
380 }
381}
382}
383
384bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
385 int x, int y,
386 SkCanvas::Config8888 config8888) {
387 DO_DEFERRED_CLEAR();
388 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
389 SkASSERT(!bitmap.isNull());
390 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
391
392 SkAutoLockPixels alp(bitmap);
393 GrPixelConfig config;
394 uint32_t flags;
395 config = config8888_to_grconfig_and_flags(config8888, &flags);
396 return fContext->readRenderTargetPixels(fRenderTarget,
397 x, y,
398 bitmap.width(),
399 bitmap.height(),
400 config,
401 bitmap.getPixels(),
402 bitmap.rowBytes(),
403 flags);
404}
405
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000406#ifdef SK_SUPPORT_LEGACY_WRITEPIXELSCONFIG
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000407void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
408 SkCanvas::Config8888 config8888) {
409 SkAutoLockPixels alp(bitmap);
410 if (!bitmap.readyToDraw()) {
411 return;
412 }
413
414 GrPixelConfig config;
415 uint32_t flags;
416 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
417 config = config8888_to_grconfig_and_flags(config8888, &flags);
418 } else {
419 flags = 0;
420 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
421 }
422
423 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
424 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
425}
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000426#endif
427
428bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
429 int x, int y) {
430 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
431 GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType());
432 if (kUnknown_GrPixelConfig == config) {
433 return false;
434 }
435 uint32_t flags = 0;
436 if (kUnpremul_SkAlphaType == info.alphaType()) {
437 flags = GrContext::kUnpremul_PixelOpsFlag;
438 }
439 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
440
441 // need to bump our genID for compatibility with clients that "know" we have a bitmap
442 this->onAccessBitmap().notifyPixelsChanged();
443
444 return true;
445}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000446
447void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
448 INHERITED::onAttachToCanvas(canvas);
449
450 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
451 fClipData.fClipStack = canvas->getClipStack();
452}
453
454void SkGpuDevice::onDetachFromCanvas() {
455 INHERITED::onDetachFromCanvas();
456 fClipData.fClipStack = NULL;
457}
458
459// call this every draw call, to ensure that the context reflects our state,
460// and not the state from some other canvas/device
461void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
462 SkASSERT(NULL != fClipData.fClipStack);
463
464 fContext->setRenderTarget(fRenderTarget);
465
466 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
467
468 if (forceIdentity) {
469 fContext->setIdentityMatrix();
470 } else {
471 fContext->setMatrix(*draw.fMatrix);
472 }
473 fClipData.fOrigin = this->getOrigin();
474
475 fContext->setClip(&fClipData);
476
477 DO_DEFERRED_CLEAR();
478}
479
480GrRenderTarget* SkGpuDevice::accessRenderTarget() {
481 DO_DEFERRED_CLEAR();
482 return fRenderTarget;
483}
484
485///////////////////////////////////////////////////////////////////////////////
486
487SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
488SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
489SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
490SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
491SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
492 shader_type_mismatch);
493SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
494 shader_type_mismatch);
495SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
496SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
497
498namespace {
499
500// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
501// justAlpha indicates that skPaint's alpha should be used rather than the color
502// Callers may subsequently modify the GrPaint. Setting constantColor indicates
503// that the final paint will draw the same color at every pixel. This allows
504// an optimization where the the color filter can be applied to the skPaint's
505// color once while converting to GrPaint and then ignored.
506inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
507 const SkPaint& skPaint,
508 bool justAlpha,
509 bool constantColor,
510 GrPaint* grPaint) {
511
512 grPaint->setDither(skPaint.isDither());
513 grPaint->setAntiAlias(skPaint.isAntiAlias());
514
515 SkXfermode::Coeff sm;
516 SkXfermode::Coeff dm;
517
518 SkXfermode* mode = skPaint.getXfermode();
519 GrEffectRef* xferEffect = NULL;
520 if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
521 if (NULL != xferEffect) {
522 grPaint->addColorEffect(xferEffect)->unref();
523 sm = SkXfermode::kOne_Coeff;
524 dm = SkXfermode::kZero_Coeff;
525 }
526 } else {
527 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
528#if 0
529 return false;
530#else
531 // Fall back to src-over
532 sm = SkXfermode::kOne_Coeff;
533 dm = SkXfermode::kISA_Coeff;
534#endif
535 }
536 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
537
538 if (justAlpha) {
539 uint8_t alpha = skPaint.getAlpha();
540 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
541 // justAlpha is currently set to true only if there is a texture,
542 // so constantColor should not also be true.
543 SkASSERT(!constantColor);
544 } else {
545 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
546 }
547
548 SkColorFilter* colorFilter = skPaint.getColorFilter();
549 if (NULL != colorFilter) {
550 // if the source color is a constant then apply the filter here once rather than per pixel
551 // in a shader.
552 if (constantColor) {
553 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
554 grPaint->setColor(SkColor2GrColor(filtered));
555 } else {
556 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
557 if (NULL != effect.get()) {
558 grPaint->addColorEffect(effect);
559 }
560 }
561 }
562
563 return true;
564}
565
566// This function is similar to skPaint2GrPaintNoShader but also converts
567// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
568// be used is set on grPaint and returned in param act. constantColor has the
569// same meaning as in skPaint2GrPaintNoShader.
570inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
571 const SkPaint& skPaint,
572 bool constantColor,
573 GrPaint* grPaint) {
574 SkShader* shader = skPaint.getShader();
575 if (NULL == shader) {
576 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
577 }
578
commit-bot@chromium.org60770572014-01-13 15:57:05 +0000579 // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state and require
580 // the shader to set a render target .
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000581 GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000582
583 // setup the shader as the first color effect on the paint
584 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
585 if (NULL != effect.get()) {
586 grPaint->addColorEffect(effect);
587 // Now setup the rest of the paint.
588 return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
589 } else {
590 // We still don't have SkColorShader::asNewEffect() implemented.
591 SkShader::GradientInfo info;
592 SkColor color;
593
594 info.fColors = &color;
595 info.fColorOffsets = NULL;
596 info.fColorCount = 1;
597 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
598 SkPaint copy(skPaint);
599 copy.setShader(NULL);
600 // modulate the paint alpha by the shader's solid color alpha
601 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
602 copy.setColor(SkColorSetA(color, newA));
603 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
604 } else {
605 return false;
606 }
607 }
608}
609}
610
611///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000612
613SkBitmap::Config SkGpuDevice::config() const {
614 if (NULL == fRenderTarget) {
615 return SkBitmap::kNo_Config;
616 }
617
618 bool isOpaque;
619 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
620}
621
622void SkGpuDevice::clear(SkColor color) {
623 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
624 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
625 fNeedClear = false;
626}
627
628void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
629 CHECK_SHOULD_DRAW(draw, false);
630
631 GrPaint grPaint;
632 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
633 return;
634 }
635
636 fContext->drawPaint(grPaint);
637}
638
639// must be in SkCanvas::PointMode order
640static const GrPrimitiveType gPointMode2PrimtiveType[] = {
641 kPoints_GrPrimitiveType,
642 kLines_GrPrimitiveType,
643 kLineStrip_GrPrimitiveType
644};
645
646void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
647 size_t count, const SkPoint pts[], const SkPaint& paint) {
648 CHECK_FOR_ANNOTATION(paint);
649 CHECK_SHOULD_DRAW(draw, false);
650
651 SkScalar width = paint.getStrokeWidth();
652 if (width < 0) {
653 return;
654 }
655
656 // we only handle hairlines and paints without path effects or mask filters,
657 // else we let the SkDraw call our drawPath()
658 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
659 draw.drawPoints(mode, count, pts, paint, true);
660 return;
661 }
662
663 GrPaint grPaint;
664 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
665 return;
666 }
667
668 fContext->drawVertices(grPaint,
669 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000670 SkToS32(count),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000671 (GrPoint*)pts,
672 NULL,
673 NULL,
674 NULL,
675 0);
676}
677
678///////////////////////////////////////////////////////////////////////////////
679
680void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
681 const SkPaint& paint) {
682 CHECK_FOR_ANNOTATION(paint);
683 CHECK_SHOULD_DRAW(draw, false);
684
685 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
686 SkScalar width = paint.getStrokeWidth();
687
688 /*
689 We have special code for hairline strokes, miter-strokes, bevel-stroke
690 and fills. Anything else we just call our path code.
691 */
692 bool usePath = doStroke && width > 0 &&
693 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
694 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
695 // another two reasons we might need to call drawPath...
696 if (paint.getMaskFilter() || paint.getPathEffect()) {
697 usePath = true;
698 }
699 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
700#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
701 if (doStroke) {
702#endif
703 usePath = true;
704#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
705 } else {
706 usePath = !fContext->getMatrix().preservesRightAngles();
707 }
708#endif
709 }
710 // until we can both stroke and fill rectangles
711 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
712 usePath = true;
713 }
714
715 if (usePath) {
716 SkPath path;
717 path.addRect(rect);
718 this->drawPath(draw, path, paint, NULL, true);
719 return;
720 }
721
722 GrPaint grPaint;
723 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
724 return;
725 }
726
727 if (!doStroke) {
728 fContext->drawRect(grPaint, rect);
729 } else {
730 SkStrokeRec stroke(paint);
731 fContext->drawRect(grPaint, rect, &stroke);
732 }
733}
734
735///////////////////////////////////////////////////////////////////////////////
736
737void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
738 const SkPaint& paint) {
739 CHECK_FOR_ANNOTATION(paint);
740 CHECK_SHOULD_DRAW(draw, false);
741
742 bool usePath = !rect.isSimple();
743 // another two reasons we might need to call drawPath...
744 if (paint.getMaskFilter() || paint.getPathEffect()) {
745 usePath = true;
746 }
747 // until we can rotate rrects...
748 if (!usePath && !fContext->getMatrix().rectStaysRect()) {
749 usePath = true;
750 }
751
752 if (usePath) {
753 SkPath path;
754 path.addRRect(rect);
755 this->drawPath(draw, path, paint, NULL, true);
756 return;
757 }
758
759 GrPaint grPaint;
760 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
761 return;
762 }
763
764 SkStrokeRec stroke(paint);
765 fContext->drawRRect(grPaint, rect, stroke);
766}
767
768///////////////////////////////////////////////////////////////////////////////
769
770void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
771 const SkPaint& paint) {
772 CHECK_FOR_ANNOTATION(paint);
773 CHECK_SHOULD_DRAW(draw, false);
774
775 bool usePath = false;
776 // some basic reasons we might need to call drawPath...
777 if (paint.getMaskFilter() || paint.getPathEffect()) {
778 usePath = true;
779 }
780
781 if (usePath) {
782 SkPath path;
783 path.addOval(oval);
784 this->drawPath(draw, path, paint, NULL, true);
785 return;
786 }
787
788 GrPaint grPaint;
789 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
790 return;
791 }
792 SkStrokeRec stroke(paint);
793
794 fContext->drawOval(grPaint, oval, stroke);
795}
796
797#include "SkMaskFilter.h"
798#include "SkBounder.h"
799
800///////////////////////////////////////////////////////////////////////////////
801
802// helpers for applying mask filters
803namespace {
804
805// Draw a mask using the supplied paint. Since the coverage/geometry
806// is already burnt into the mask this boils down to a rect draw.
807// Return true if the mask was successfully drawn.
808bool draw_mask(GrContext* context, const SkRect& maskRect,
809 GrPaint* grp, GrTexture* mask) {
810 GrContext::AutoMatrix am;
811 if (!am.setIdentity(context, grp)) {
812 return false;
813 }
814
815 SkMatrix matrix;
816 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
817 matrix.postIDiv(mask->width(), mask->height());
818
819 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
820 context->drawRect(*grp, maskRect);
821 return true;
822}
823
824bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
825 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
826 GrPaint* grp, SkPaint::Style style) {
827 SkMask srcM, dstM;
828
829 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
830 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
831 return false;
832 }
833 SkAutoMaskFreeImage autoSrc(srcM.fImage);
834
835 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
836 return false;
837 }
838 // this will free-up dstM when we're done (allocated in filterMask())
839 SkAutoMaskFreeImage autoDst(dstM.fImage);
840
841 if (clip.quickReject(dstM.fBounds)) {
842 return false;
843 }
844 if (bounder && !bounder->doIRect(dstM.fBounds)) {
845 return false;
846 }
847
848 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
849 // the current clip (and identity matrix) and GrPaint settings
850 GrTextureDesc desc;
851 desc.fWidth = dstM.fBounds.width();
852 desc.fHeight = dstM.fBounds.height();
853 desc.fConfig = kAlpha_8_GrPixelConfig;
854
855 GrAutoScratchTexture ast(context, desc);
856 GrTexture* texture = ast.texture();
857
858 if (NULL == texture) {
859 return false;
860 }
861 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
862 dstM.fImage, dstM.fRowBytes);
863
864 SkRect maskRect = SkRect::Make(dstM.fBounds);
865
866 return draw_mask(context, maskRect, grp, texture);
867}
868
869// Create a mask of 'devPath' and place the result in 'mask'. Return true on
870// success; false otherwise.
871bool create_mask_GPU(GrContext* context,
872 const SkRect& maskRect,
873 const SkPath& devPath,
874 const SkStrokeRec& stroke,
875 bool doAA,
876 GrAutoScratchTexture* mask) {
877 GrTextureDesc desc;
878 desc.fFlags = kRenderTarget_GrTextureFlagBit;
879 desc.fWidth = SkScalarCeilToInt(maskRect.width());
880 desc.fHeight = SkScalarCeilToInt(maskRect.height());
881 // We actually only need A8, but it often isn't supported as a
882 // render target so default to RGBA_8888
883 desc.fConfig = kRGBA_8888_GrPixelConfig;
884 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
885 desc.fConfig = kAlpha_8_GrPixelConfig;
886 }
887
888 mask->set(context, desc);
889 if (NULL == mask->texture()) {
890 return false;
891 }
892
893 GrTexture* maskTexture = mask->texture();
894 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
895
896 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
897 GrContext::AutoClip ac(context, clipRect);
898
899 context->clear(NULL, 0x0, true);
900
901 GrPaint tempPaint;
902 if (doAA) {
903 tempPaint.setAntiAlias(true);
904 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
905 // blend coeff of zero requires dual source blending support in order
906 // to properly blend partially covered pixels. This means the AA
907 // code path may not be taken. So we use a dst blend coeff of ISA. We
908 // could special case AA draws to a dst surface with known alpha=0 to
909 // use a zero dst coeff when dual source blending isn't available.
910 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
911 }
912
913 GrContext::AutoMatrix am;
914
915 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
916 SkMatrix translate;
917 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
918 am.set(context, translate);
919 context->drawPath(tempPaint, devPath, stroke);
920 return true;
921}
922
923SkBitmap wrap_texture(GrTexture* texture) {
reed@google.combf790232013-12-13 19:45:58 +0000924 SkImageInfo info;
925 texture->asImageInfo(&info);
926
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000927 SkBitmap result;
reed@google.combf790232013-12-13 19:45:58 +0000928 result.setConfig(info);
929 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000930 return result;
931}
932
933};
934
935void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
936 const SkPaint& paint, const SkMatrix* prePathMatrix,
937 bool pathIsMutable) {
938 CHECK_FOR_ANNOTATION(paint);
939 CHECK_SHOULD_DRAW(draw, false);
940
941 GrPaint grPaint;
942 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
943 return;
944 }
945
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000946 // If we have a prematrix, apply it to the path, optimizing for the case
947 // where the original path can in fact be modified in place (even though
948 // its parameter type is const).
949 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000950 SkTLazy<SkPath> tmpPath;
951 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000952
953 if (prePathMatrix) {
954 SkPath* result = pathPtr;
955
956 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000957 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958 pathIsMutable = true;
959 }
960 // should I push prePathMatrix on our MV stack temporarily, instead
961 // of applying it here? See SkDraw.cpp
962 pathPtr->transform(*prePathMatrix, result);
963 pathPtr = result;
964 }
965 // at this point we're done with prePathMatrix
966 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
967
968 SkStrokeRec stroke(paint);
969 SkPathEffect* pathEffect = paint.getPathEffect();
970 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000971 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000972 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000973 pathPtr = effectPath.get();
974 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000975 }
976
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000977 if (paint.getMaskFilter()) {
978 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000979 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
980 if (stroke.applyToPath(strokedPath, *pathPtr)) {
981 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982 pathIsMutable = true;
983 stroke.setFillStyle();
984 }
985 }
986
987 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000988 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000989
990 // transform the path into device space
991 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000992
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000993 SkRect maskRect;
994 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
995 draw.fClip->getBounds(),
996 fContext->getMatrix(),
997 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000998 // The context's matrix may change while creating the mask, so save the CTM here to
999 // pass to filterMaskGPU.
1000 const SkMatrix ctm = fContext->getMatrix();
1001
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001002 SkIRect finalIRect;
1003 maskRect.roundOut(&finalIRect);
1004 if (draw.fClip->quickReject(finalIRect)) {
1005 // clipped out
1006 return;
1007 }
1008 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
1009 // nothing to draw
1010 return;
1011 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001012
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +00001013 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
1014 SkStrokeRec(paint), *devPathPtr)) {
1015 // the mask filter was able to draw itself directly, so there's nothing
1016 // left to do.
1017 return;
1018 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001019
1020 GrAutoScratchTexture mask;
1021
1022 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
1023 grPaint.isAntiAlias(), &mask)) {
1024 GrTexture* filtered;
1025
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +00001026 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +00001027 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001028 // filterMaskGPU gives us ownership of a ref to the result
1029 SkAutoTUnref<GrTexture> atu(filtered);
1030
1031 // If the scratch texture that we used as the filter src also holds the filter
1032 // result then we must detach so that this texture isn't recycled for a later
1033 // draw.
1034 if (filtered == mask.texture()) {
1035 mask.detach();
1036 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
1037 }
1038
1039 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
1040 // This path is completely drawn
1041 return;
1042 }
1043 }
1044 }
1045 }
1046
1047 // draw the mask on the CPU - this is a fallthrough path in case the
1048 // GPU path fails
1049 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
1050 SkPaint::kFill_Style;
1051 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
1052 *draw.fClip, draw.fBounder, &grPaint, style);
1053 return;
1054 }
1055
1056 fContext->drawPath(grPaint, *pathPtr, stroke);
1057}
1058
1059static const int kBmpSmallTileSize = 1 << 10;
1060
1061static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
1062 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
1063 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
1064 return tilesX * tilesY;
1065}
1066
1067static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
1068 if (maxTileSize <= kBmpSmallTileSize) {
1069 return maxTileSize;
1070 }
1071
1072 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
1073 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
1074
1075 maxTileTotalTileSize *= maxTileSize * maxTileSize;
1076 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
1077
1078 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
1079 return kBmpSmallTileSize;
1080 } else {
1081 return maxTileSize;
1082 }
1083}
1084
1085// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
1086// pixels from the bitmap are necessary.
1087static void determine_clipped_src_rect(const GrContext* context,
1088 const SkBitmap& bitmap,
1089 const SkRect* srcRectPtr,
1090 SkIRect* clippedSrcIRect) {
1091 const GrClipData* clip = context->getClip();
1092 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
1093 SkMatrix inv;
1094 if (!context->getMatrix().invert(&inv)) {
1095 clippedSrcIRect->setEmpty();
1096 return;
1097 }
1098 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
1099 inv.mapRect(&clippedSrcRect);
1100 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001101 // we've setup src space 0,0 to map to the top left of the src rect.
1102 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001103 if (!clippedSrcRect.intersect(*srcRectPtr)) {
1104 clippedSrcIRect->setEmpty();
1105 return;
1106 }
1107 }
1108 clippedSrcRect.roundOut(clippedSrcIRect);
1109 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1110 if (!clippedSrcIRect->intersect(bmpBounds)) {
1111 clippedSrcIRect->setEmpty();
1112 }
1113}
1114
1115bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1116 const GrTextureParams& params,
1117 const SkRect* srcRectPtr,
1118 int maxTileSize,
1119 int* tileSize,
1120 SkIRect* clippedSrcRect) const {
1121 // if bitmap is explictly texture backed then just use the texture
1122 if (NULL != bitmap.getTexture()) {
1123 return false;
1124 }
1125
1126 // if it's larger than the max tile size, then we have no choice but tiling.
1127 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
1128 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1129 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
1130 return true;
1131 }
1132
1133 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
1134 return false;
1135 }
1136
1137 // if the entire texture is already in our cache then no reason to tile it
1138 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
1139 return false;
1140 }
1141
1142 // At this point we know we could do the draw by uploading the entire bitmap
1143 // as a texture. However, if the texture would be large compared to the
1144 // cache size and we don't require most of it for this draw then tile to
1145 // reduce the amount of upload and cache spill.
1146
1147 // assumption here is that sw bitmap size is a good proxy for its size as
1148 // a texture
1149 size_t bmpSize = bitmap.getSize();
1150 size_t cacheSize;
1151 fContext->getTextureCacheLimits(NULL, &cacheSize);
1152 if (bmpSize < cacheSize / 2) {
1153 return false;
1154 }
1155
1156 // Figure out how much of the src we will need based on the src rect and clipping.
1157 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
1158 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1159 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
1160 kBmpSmallTileSize * kBmpSmallTileSize;
1161
1162 return usedTileBytes < 2 * bmpSize;
1163}
1164
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001165void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001166 const SkBitmap& bitmap,
1167 const SkMatrix& m,
1168 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001169 SkMatrix concat;
1170 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1171 if (!m.isIdentity()) {
1172 concat.setConcat(*draw->fMatrix, m);
1173 draw.writable()->fMatrix = &concat;
1174 }
1175 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001176}
1177
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001178// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001179// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1180// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001181static inline void clamped_outset_with_offset(SkIRect* iRect,
1182 int outset,
1183 SkPoint* offset,
1184 const SkIRect& clamp) {
1185 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001186
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001187 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1188 if (leftClampDelta > 0) {
1189 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001190 iRect->fLeft = clamp.fLeft;
1191 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001192 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001193 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001194
1195 int topClampDelta = clamp.fTop - iRect->fTop;
1196 if (topClampDelta > 0) {
1197 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001198 iRect->fTop = clamp.fTop;
1199 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001200 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001201 }
1202
1203 if (iRect->fRight > clamp.fRight) {
1204 iRect->fRight = clamp.fRight;
1205 }
1206 if (iRect->fBottom > clamp.fBottom) {
1207 iRect->fBottom = clamp.fBottom;
1208 }
1209}
1210
1211void 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,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001276 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001277 paintWithShader.getShader()->setLocalMatrix(localM);
1278 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1279 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001280
1281 return;
1282 }
1283
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001284 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1285 // the view matrix rather than a local matrix.
1286 SkMatrix m;
1287 m.setScale(dstSize.fWidth / srcRect.width(),
1288 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001289 fContext->concatMatrix(m);
1290
1291 GrTextureParams params;
1292 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1293 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001294
1295 int tileFilterPad;
1296 bool doBicubic = false;
1297
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001298 switch(paintFilterLevel) {
1299 case SkPaint::kNone_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001300 tileFilterPad = 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001301 textureFilterMode = GrTextureParams::kNone_FilterMode;
1302 break;
1303 case SkPaint::kLow_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001304 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001305 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1306 break;
1307 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001308 tileFilterPad = 1;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001309 if (fContext->getMatrix().getMinStretch() < SK_Scalar1) {
1310 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1311 } else {
1312 // Don't trigger MIP level generation unnecessarily.
1313 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1314 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001315 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001316 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001317 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001318 if (fContext->getMatrix().getMinStretch() >= SK_Scalar1) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001319 // We will install an effect that does the filtering in the shader.
1320 textureFilterMode = GrTextureParams::kNone_FilterMode;
1321 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1322 doBicubic = true;
1323 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001324 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1325 tileFilterPad = 1;
1326 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001327 break;
1328 default:
1329 SkErrorInternals::SetError( kInvalidPaint_SkError,
1330 "Sorry, I don't understand the filtering "
1331 "mode you asked for. Falling back to "
1332 "MIPMaps.");
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001333 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001334 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1335 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001336 }
1337
1338 params.setFilterMode(textureFilterMode);
1339
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001340 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001341 int tileSize;
1342
1343 SkIRect clippedSrcRect;
1344 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1345 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001346 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1347 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001348 } else {
1349 // take the simple case
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001350 this->internalDrawBitmap(bitmap, srcRect, params, paint, flags, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001351 }
1352}
1353
1354// Break 'bitmap' into several tiles to draw it since it has already
1355// been determined to be too large to fit in VRAM
1356void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1357 const SkRect& srcRect,
1358 const SkIRect& clippedSrcIRect,
1359 const GrTextureParams& params,
1360 const SkPaint& paint,
1361 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001362 int tileSize,
1363 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001364 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1365
1366 int nx = bitmap.width() / tileSize;
1367 int ny = bitmap.height() / tileSize;
1368 for (int x = 0; x <= nx; x++) {
1369 for (int y = 0; y <= ny; y++) {
1370 SkRect tileR;
1371 tileR.set(SkIntToScalar(x * tileSize),
1372 SkIntToScalar(y * tileSize),
1373 SkIntToScalar((x + 1) * tileSize),
1374 SkIntToScalar((y + 1) * tileSize));
1375
1376 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1377 continue;
1378 }
1379
1380 if (!tileR.intersect(srcRect)) {
1381 continue;
1382 }
1383
1384 SkBitmap tmpB;
1385 SkIRect iTileR;
1386 tileR.roundOut(&iTileR);
1387 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1388 SkIntToScalar(iTileR.fTop));
1389
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001390 // Adjust the context matrix to draw at the right x,y in device space
1391 SkMatrix tmpM;
1392 GrContext::AutoMatrix am;
1393 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1394 am.setPreConcat(fContext, tmpM);
1395
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001396 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001397 SkIRect iClampRect;
1398
1399 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1400 // In bleed mode we want to always expand the tile on all edges
1401 // but stay within the bitmap bounds
1402 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1403 } else {
1404 // In texture-domain/clamp mode we only want to expand the
1405 // tile on edges interior to "srcRect" (i.e., we want to
1406 // not bleed across the original clamped edges)
1407 srcRect.roundOut(&iClampRect);
1408 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001409 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1410 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001411 }
1412
1413 if (bitmap.extractSubset(&tmpB, iTileR)) {
1414 // now offset it to make it "local" to our tmp bitmap
1415 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001416
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001417 this->internalDrawBitmap(tmpB, tileR, params, paint, flags, bicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001418 }
1419 }
1420 }
1421}
1422
1423static bool has_aligned_samples(const SkRect& srcRect,
1424 const SkRect& transformedRect) {
1425 // detect pixel disalignment
1426 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1427 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1428 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1429 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1430 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1431 COLOR_BLEED_TOLERANCE &&
1432 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1433 COLOR_BLEED_TOLERANCE) {
1434 return true;
1435 }
1436 return false;
1437}
1438
1439static bool may_color_bleed(const SkRect& srcRect,
1440 const SkRect& transformedRect,
1441 const SkMatrix& m) {
1442 // Only gets called if has_aligned_samples returned false.
1443 // So we can assume that sampling is axis aligned but not texel aligned.
1444 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1445 SkRect innerSrcRect(srcRect), innerTransformedRect,
1446 outerTransformedRect(transformedRect);
1447 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1448 m.mapRect(&innerTransformedRect, innerSrcRect);
1449
1450 // The gap between outerTransformedRect and innerTransformedRect
1451 // represents the projection of the source border area, which is
1452 // problematic for color bleeding. We must check whether any
1453 // destination pixels sample the border area.
1454 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1455 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1456 SkIRect outer, inner;
1457 outerTransformedRect.round(&outer);
1458 innerTransformedRect.round(&inner);
1459 // If the inner and outer rects round to the same result, it means the
1460 // border does not overlap any pixel centers. Yay!
1461 return inner != outer;
1462}
1463
1464
1465/*
1466 * This is called by drawBitmap(), which has to handle images that may be too
1467 * large to be represented by a single texture.
1468 *
1469 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1470 * and that non-texture portion of the GrPaint has already been setup.
1471 */
1472void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1473 const SkRect& srcRect,
1474 const GrTextureParams& params,
1475 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001476 SkCanvas::DrawBitmapRectFlags flags,
1477 bool bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001478 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1479 bitmap.height() <= fContext->getMaxTextureSize());
1480
1481 GrTexture* texture;
1482 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1483 if (NULL == texture) {
1484 return;
1485 }
1486
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001487 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001488 SkRect paintRect;
1489 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1490 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1491 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1492 SkScalarMul(srcRect.fTop, hInv),
1493 SkScalarMul(srcRect.fRight, wInv),
1494 SkScalarMul(srcRect.fBottom, hInv));
1495
1496 bool needsTextureDomain = false;
1497 if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) &&
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001498 (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode)) {
1499 // Need texture domain if drawing a sub rect
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001500 needsTextureDomain = srcRect.width() < bitmap.width() ||
1501 srcRect.height() < bitmap.height();
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001502 if (!bicubic && needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001503 const SkMatrix& matrix = fContext->getMatrix();
1504 // sampling is axis-aligned
1505 SkRect transformedRect;
1506 matrix.mapRect(&transformedRect, srcRect);
1507
1508 if (has_aligned_samples(srcRect, transformedRect)) {
1509 // We could also turn off filtering here (but we already did a cache lookup with
1510 // params).
1511 needsTextureDomain = false;
1512 } else {
1513 needsTextureDomain = may_color_bleed(srcRect, transformedRect, matrix);
1514 }
1515 }
1516 }
1517
1518 SkRect textureDomain = SkRect::MakeEmpty();
1519 SkAutoTUnref<GrEffectRef> effect;
1520 if (needsTextureDomain) {
1521 // Use a constrained texture domain to avoid color bleeding
1522 SkScalar left, top, right, bottom;
1523 if (srcRect.width() > SK_Scalar1) {
1524 SkScalar border = SK_ScalarHalf / texture->width();
1525 left = paintRect.left() + border;
1526 right = paintRect.right() - border;
1527 } else {
1528 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1529 }
1530 if (srcRect.height() > SK_Scalar1) {
1531 SkScalar border = SK_ScalarHalf / texture->height();
1532 top = paintRect.top() + border;
1533 bottom = paintRect.bottom() - border;
1534 } else {
1535 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1536 }
1537 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001538 if (bicubic) {
1539 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1540 } else {
1541 effect.reset(GrTextureDomainEffect::Create(texture,
1542 SkMatrix::I(),
1543 textureDomain,
1544 GrTextureDomain::kClamp_Mode,
1545 params.filterMode()));
1546 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001547 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001548 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1549 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1550 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001551 } else {
1552 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1553 }
1554
1555 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1556 // the rest from the SkPaint.
1557 GrPaint grPaint;
1558 grPaint.addColorEffect(effect);
1559 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1560 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1561 return;
1562 }
1563
1564 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1565}
1566
1567static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001568 GrTexture* texture, const SkImageFilter* filter,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001569 int w, int h, const SkMatrix& ctm, SkBitmap* result,
1570 SkIPoint* offset) {
1571 SkASSERT(filter);
1572 SkDeviceImageFilterProxy proxy(device);
1573
1574 if (filter->canFilterImageGPU()) {
1575 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1576 // filter. Also set the clip wide open and the matrix to identity.
1577 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
1578 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctm, result, offset);
1579 } else {
1580 return false;
1581 }
1582}
1583
1584void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1585 int left, int top, const SkPaint& paint) {
1586 // drawSprite is defined to be in device coords.
1587 CHECK_SHOULD_DRAW(draw, true);
1588
1589 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1590 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1591 return;
1592 }
1593
1594 int w = bitmap.width();
1595 int h = bitmap.height();
1596
1597 GrTexture* texture;
1598 // draw sprite uses the default texture params
1599 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1600
1601 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001602 // This bitmap will own the filtered result as a texture.
1603 SkBitmap filteredBitmap;
1604
1605 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001606 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001607 SkMatrix matrix(*draw.fMatrix);
1608 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
1609 if (filter_texture(this, fContext, texture, filter, w, h, matrix, &filteredBitmap,
1610 &offset)) {
1611 texture = (GrTexture*) filteredBitmap.getTexture();
1612 w = filteredBitmap.width();
1613 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001614 left += offset.x();
1615 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001616 } else {
1617 return;
1618 }
1619 }
1620
1621 GrPaint grPaint;
1622 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1623
1624 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1625 return;
1626 }
1627
1628 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001629 SkRect::MakeXYWH(SkIntToScalar(left),
1630 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001631 SkIntToScalar(w),
1632 SkIntToScalar(h)),
1633 SkRect::MakeXYWH(0,
1634 0,
1635 SK_Scalar1 * w / texture->width(),
1636 SK_Scalar1 * h / texture->height()));
1637}
1638
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001639void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001640 const SkRect* src, const SkRect& dst,
1641 const SkPaint& paint,
1642 SkCanvas::DrawBitmapRectFlags flags) {
1643 SkMatrix matrix;
1644 SkRect bitmapBounds, tmpSrc;
1645
1646 bitmapBounds.set(0, 0,
1647 SkIntToScalar(bitmap.width()),
1648 SkIntToScalar(bitmap.height()));
1649
1650 // Compute matrix from the two rectangles
1651 if (NULL != src) {
1652 tmpSrc = *src;
1653 } else {
1654 tmpSrc = bitmapBounds;
1655 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001656
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001657 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1658
1659 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1660 if (NULL != src) {
1661 if (!bitmapBounds.contains(tmpSrc)) {
1662 if (!tmpSrc.intersect(bitmapBounds)) {
1663 return; // nothing to draw
1664 }
1665 }
1666 }
1667
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001668 SkRect tmpDst;
1669 matrix.mapRect(&tmpDst, tmpSrc);
1670
1671 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1672 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1673 // Translate so that tempDst's top left is at the origin.
1674 matrix = *origDraw.fMatrix;
1675 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1676 draw.writable()->fMatrix = &matrix;
1677 }
1678 SkSize dstSize;
1679 dstSize.fWidth = tmpDst.width();
1680 dstSize.fHeight = tmpDst.height();
1681
1682 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001683}
1684
1685void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1686 int x, int y, const SkPaint& paint) {
1687 // clear of the source device must occur before CHECK_SHOULD_DRAW
1688 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1689 if (dev->fNeedClear) {
1690 // TODO: could check here whether we really need to draw at all
1691 dev->clear(0x0);
1692 }
1693
1694 // drawDevice is defined to be in device coords.
1695 CHECK_SHOULD_DRAW(draw, true);
1696
1697 GrRenderTarget* devRT = dev->accessRenderTarget();
1698 GrTexture* devTex;
1699 if (NULL == (devTex = devRT->asTexture())) {
1700 return;
1701 }
1702
1703 const SkBitmap& bm = dev->accessBitmap(false);
1704 int w = bm.width();
1705 int h = bm.height();
1706
1707 SkImageFilter* filter = paint.getImageFilter();
1708 // This bitmap will own the filtered result as a texture.
1709 SkBitmap filteredBitmap;
1710
1711 if (NULL != filter) {
1712 SkIPoint offset = SkIPoint::Make(0, 0);
1713 SkMatrix matrix(*draw.fMatrix);
1714 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
1715 if (filter_texture(this, fContext, devTex, filter, w, h, matrix, &filteredBitmap,
1716 &offset)) {
1717 devTex = filteredBitmap.getTexture();
1718 w = filteredBitmap.width();
1719 h = filteredBitmap.height();
1720 x += offset.fX;
1721 y += offset.fY;
1722 } else {
1723 return;
1724 }
1725 }
1726
1727 GrPaint grPaint;
1728 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1729
1730 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1731 return;
1732 }
1733
1734 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1735 SkIntToScalar(y),
1736 SkIntToScalar(w),
1737 SkIntToScalar(h));
1738
1739 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1740 // scratch texture).
1741 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1742 SK_Scalar1 * h / devTex->height());
1743
1744 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1745}
1746
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001747bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001748 return filter->canFilterImageGPU();
1749}
1750
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001751bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001752 const SkMatrix& ctm,
1753 SkBitmap* result, SkIPoint* offset) {
1754 // want explicitly our impl, so guard against a subclass of us overriding it
1755 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1756 return false;
1757 }
1758
1759 SkAutoLockPixels alp(src, !src.getTexture());
1760 if (!src.getTexture() && !src.readyToDraw()) {
1761 return false;
1762 }
1763
1764 GrTexture* texture;
1765 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1766 // must be pushed upstack.
1767 SkAutoCachedTexture act(this, src, NULL, &texture);
1768
1769 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctm, result,
1770 offset);
1771}
1772
1773///////////////////////////////////////////////////////////////////////////////
1774
1775// must be in SkCanvas::VertexMode order
1776static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1777 kTriangles_GrPrimitiveType,
1778 kTriangleStrip_GrPrimitiveType,
1779 kTriangleFan_GrPrimitiveType,
1780};
1781
1782void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1783 int vertexCount, const SkPoint vertices[],
1784 const SkPoint texs[], const SkColor colors[],
1785 SkXfermode* xmode,
1786 const uint16_t indices[], int indexCount,
1787 const SkPaint& paint) {
1788 CHECK_SHOULD_DRAW(draw, false);
1789
1790 GrPaint grPaint;
1791 // we ignore the shader if texs is null.
1792 if (NULL == texs) {
1793 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
1794 return;
1795 }
1796 } else {
1797 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
1798 return;
1799 }
1800 }
1801
1802 if (NULL != xmode && NULL != texs && NULL != colors) {
1803 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1804 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1805#if 0
1806 return
1807#endif
1808 }
1809 }
1810
1811 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1812 if (NULL != colors) {
1813 // need to convert byte order and from non-PM to PM
1814 convertedColors.reset(vertexCount);
1815 for (int i = 0; i < vertexCount; ++i) {
1816 convertedColors[i] = SkColor2GrColor(colors[i]);
1817 }
1818 colors = convertedColors.get();
1819 }
1820 fContext->drawVertices(grPaint,
1821 gVertexMode2PrimitiveType[vmode],
1822 vertexCount,
1823 (GrPoint*) vertices,
1824 (GrPoint*) texs,
1825 colors,
1826 indices,
1827 indexCount);
1828}
1829
1830///////////////////////////////////////////////////////////////////////////////
1831
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001832void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1833 size_t byteLength, SkScalar x, SkScalar y,
1834 const SkPaint& paint) {
1835 CHECK_SHOULD_DRAW(draw, false);
1836
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001837 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001838 GrPaint grPaint;
1839 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1840 return;
1841 }
1842
1843 SkDEBUGCODE(this->validate();)
1844
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001845 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1846 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001847 GrPaint grPaint;
1848 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1849 return;
1850 }
1851
1852 SkDEBUGCODE(this->validate();)
1853
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001854 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001855 } else {
1856 // this guy will just call our drawPath()
1857 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001858 }
1859}
1860
1861void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1862 size_t byteLength, const SkScalar pos[],
1863 SkScalar constY, int scalarsPerPos,
1864 const SkPaint& paint) {
1865 CHECK_SHOULD_DRAW(draw, false);
1866
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001867 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001868 GrPaint grPaint;
1869 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1870 return;
1871 }
1872
1873 SkDEBUGCODE(this->validate();)
1874
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001875 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001876 constY, scalarsPerPos);
1877 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001878 GrPaint grPaint;
1879 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
1880 return;
1881 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001882
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001883 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001884
1885 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001886 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001887 } else {
1888 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1889 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001890 }
1891}
1892
1893void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1894 size_t len, const SkPath& path,
1895 const SkMatrix* m, const SkPaint& paint) {
1896 CHECK_SHOULD_DRAW(draw, false);
1897
1898 SkASSERT(draw.fDevice == this);
1899 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1900}
1901
1902///////////////////////////////////////////////////////////////////////////////
1903
1904bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1905 if (!paint.isLCDRenderText()) {
1906 // we're cool with the paint as is
1907 return false;
1908 }
1909
1910 if (paint.getShader() ||
1911 paint.getXfermode() || // unless its srcover
1912 paint.getMaskFilter() ||
1913 paint.getRasterizer() ||
1914 paint.getColorFilter() ||
1915 paint.getPathEffect() ||
1916 paint.isFakeBoldText() ||
1917 paint.getStyle() != SkPaint::kFill_Style) {
1918 // turn off lcd
1919 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1920 flags->fHinting = paint.getHinting();
1921 return true;
1922 }
1923 // we're cool with the paint as is
1924 return false;
1925}
1926
1927void SkGpuDevice::flush() {
1928 DO_DEFERRED_CLEAR();
1929 fContext->resolveRenderTarget(fRenderTarget);
1930}
1931
1932///////////////////////////////////////////////////////////////////////////////
1933
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001934SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001935 GrTextureDesc desc;
1936 desc.fConfig = fRenderTarget->config();
1937 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001938 desc.fWidth = info.width();
1939 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001940 desc.fSampleCnt = fRenderTarget->numSamples();
1941
1942 SkAutoTUnref<GrTexture> texture;
1943 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001944 bool needClear = !info.isOpaque();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001945
1946#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1947 // layers are never draw in repeat modes, so we can request an approx
1948 // match and ignore any padding.
1949 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1950 GrContext::kApprox_ScratchTexMatch :
1951 GrContext::kExact_ScratchTexMatch;
1952 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1953#else
1954 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1955#endif
1956 if (NULL != texture.get()) {
1957 return SkNEW_ARGS(SkGpuDevice,(fContext, texture, needClear));
1958 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001959 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1960 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001961 return NULL;
1962 }
1963}
1964
reed@google.com76f10a32014-02-05 15:32:21 +00001965SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1966 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1967}
1968
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001969SkGpuDevice::SkGpuDevice(GrContext* context,
1970 GrTexture* texture,
1971 bool needClear)
1972 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
1973
1974 SkASSERT(texture && texture->asRenderTarget());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001975 // This constructor is called from onCreateDevice. It has locked the RT in the texture
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001976 // cache. We pass true for the third argument so that it will get unlocked.
1977 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
1978 fNeedClear = needClear;
1979}