blob: 125628519255acb8489f7f43da8ed8e938627715 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
tomhudson@google.com898e7b52012-06-01 20:42:15 +00008#include "SkGpuDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00009
tomhudson@google.com898e7b52012-06-01 20:42:15 +000010#include "effects/GrGradientEffects.h"
twiz@google.com58071162012-07-18 21:41:50 +000011#include "effects/GrColorTableEffect.h"
tomhudson@google.com2f68e762012-07-17 18:43:21 +000012#include "effects/GrTextureDomainEffect.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000013
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrContext.h"
15#include "GrTextContext.h"
16
robertphillips@google.come9c04692012-06-29 00:30:13 +000017#include "SkGrTexturePixelRef.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
Scroggo97c88c22011-05-11 14:05:25 +000019#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020#include "SkDrawProcs.h"
21#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000022#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000023#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000024#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.com06cd7322012-03-30 18:45:35 +000026#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000027
28#if 0
29 extern bool (*gShouldDrawProc)();
30 #define CHECK_SHOULD_DRAW(draw) \
31 do { \
32 if (gShouldDrawProc && !gShouldDrawProc()) return; \
33 this->prepareRenderTarget(draw); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000034 GrAssert(!fNeedClear) \
reed@google.comac10a2d2010-12-22 21:39:39 +000035 } while (0)
36#else
bsalomon@google.com06cd7322012-03-30 18:45:35 +000037 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \
38 GrAssert(!fNeedClear)
reed@google.comac10a2d2010-12-22 21:39:39 +000039#endif
40
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000041// we use the same texture slot on GrPaint for bitmaps and shaders
42// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
43enum {
44 kBitmapTextureIdx = 0,
twiz@google.com58071162012-07-18 21:41:50 +000045 kShaderTextureIdx = 0,
46 kColorFilterTextureIdx = 1
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000047};
48
reed@google.comcde92112011-07-06 20:00:52 +000049
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000050#define MAX_BLUR_SIGMA 4.0f
51// FIXME: This value comes from from SkBlurMaskFilter.cpp.
52// Should probably be put in a common header someplace.
53#define MAX_BLUR_RADIUS SkIntToScalar(128)
54// This constant approximates the scaling done in the software path's
55// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
56// IMHO, it actually should be 1: we blur "less" than we should do
57// according to the CSS and canvas specs, simply because Safari does the same.
58// Firefox used to do the same too, until 4.0 where they fixed it. So at some
59// point we should probably get rid of these scaling constants and rebaseline
60// all the blur tests.
61#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000062// This constant represents the screen alignment criterion in texels for
63// requiring texture domain clamping to prevent color bleeding when drawing
64// a sub region of a larger source image.
65#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000066
67#define DO_DEFERRED_CLEAR \
68 do { \
69 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000070 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000071 fNeedClear = false; \
72 } \
73 } while (false) \
74
reed@google.comac10a2d2010-12-22 21:39:39 +000075///////////////////////////////////////////////////////////////////////////////
76
reed@google.comb0a34d82012-07-11 19:57:55 +000077#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
78 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
79
80///////////////////////////////////////////////////////////////////////////////
81
82
bsalomon@google.com84405e02012-03-05 19:57:21 +000083class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
84public:
85 SkAutoCachedTexture() { }
86 SkAutoCachedTexture(SkGpuDevice* device,
87 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000088 const GrTextureParams* params,
bsalomon@google.com84405e02012-03-05 19:57:21 +000089 GrTexture** texture) {
90 GrAssert(texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000091 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000092 }
reed@google.comac10a2d2010-12-22 21:39:39 +000093
bsalomon@google.com84405e02012-03-05 19:57:21 +000094 ~SkAutoCachedTexture() {
95 if (fTex.texture()) {
rileya@google.com24f3ad12012-07-18 21:47:40 +000096 GrUnlockCachedBitmapTexture(fDevice->context(), fTex);
bsalomon@google.com84405e02012-03-05 19:57:21 +000097 }
reed@google.comac10a2d2010-12-22 21:39:39 +000098 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000099
100 GrTexture* set(SkGpuDevice* device,
101 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000102 const GrTextureParams* params) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000103 if (fTex.texture()) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000104 GrUnlockCachedBitmapTexture(fDevice->context(), fTex);
bsalomon@google.com84405e02012-03-05 19:57:21 +0000105 }
106 fDevice = device;
107 GrTexture* texture = (GrTexture*)bitmap.getTexture();
108 if (texture) {
109 // return the native texture
110 fTex.reset();
111 } else {
112 // look it up in our cache
bsalomon@google.comb8670992012-07-25 21:27:09 +0000113 fTex = GrLockCachedBitmapTexture(device->context(), bitmap, params);
bsalomon@google.com84405e02012-03-05 19:57:21 +0000114 texture = fTex.texture();
115 }
116 return texture;
117 }
118
119private:
120 SkGpuDevice* fDevice;
121 GrContext::TextureCacheEntry fTex;
122};
reed@google.comac10a2d2010-12-22 21:39:39 +0000123
124///////////////////////////////////////////////////////////////////////////////
125
126bool gDoTraceDraw;
127
128struct GrSkDrawProcs : public SkDrawProcs {
129public:
130 GrContext* fContext;
131 GrTextContext* fTextContext;
132 GrFontScaler* fFontScaler; // cached in the skia glyphcache
133};
134
135///////////////////////////////////////////////////////////////////////////////
136
reed@google.comaf951c92011-06-16 19:10:39 +0000137static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
138 switch (config) {
139 case kAlpha_8_GrPixelConfig:
140 *isOpaque = false;
141 return SkBitmap::kA8_Config;
142 case kRGB_565_GrPixelConfig:
143 *isOpaque = true;
144 return SkBitmap::kRGB_565_Config;
145 case kRGBA_4444_GrPixelConfig:
146 *isOpaque = false;
147 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000148 case kSkia8888_PM_GrPixelConfig:
149 // we don't currently have a way of knowing whether
150 // a 8888 is opaque based on the config.
151 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000152 return SkBitmap::kARGB_8888_Config;
153 default:
154 *isOpaque = false;
155 return SkBitmap::kNo_Config;
156 }
157}
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
reed@google.comaf951c92011-06-16 19:10:39 +0000159static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000160 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000161
162 bool isOpaque;
163 SkBitmap bitmap;
164 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
165 renderTarget->width(), renderTarget->height());
166 bitmap.setIsOpaque(isOpaque);
167 return bitmap;
168}
169
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000170SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000171: SkDevice(make_bitmap(context, texture->asRenderTarget()))
172, fClipStack(NULL) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000173 this->initFromRenderTarget(context, texture->asRenderTarget());
174}
175
reed@google.comaf951c92011-06-16 19:10:39 +0000176SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000177: SkDevice(make_bitmap(context, renderTarget))
178, fClipStack(NULL) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000179 this->initFromRenderTarget(context, renderTarget);
180}
181
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000182void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000183 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000184 fNeedPrepareRenderTarget = false;
185 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000186
reed@google.comaf951c92011-06-16 19:10:39 +0000187 fContext = context;
188 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000189
reed@google.comaf951c92011-06-16 19:10:39 +0000190 fTexture = NULL;
191 fRenderTarget = NULL;
192 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000193
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000194 GrAssert(NULL != renderTarget);
195 fRenderTarget = renderTarget;
196 fRenderTarget->ref();
197 // if this RT is also a texture, hold a ref on it
198 fTexture = fRenderTarget->asTexture();
199 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000200
201 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
202 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
203 // the device via accessBitmap. This external ref may outlive the device.
204 // Since textures own their render targets (but not vice-versa) we
205 // are ensuring that both objects will live as long as the pixel ref.
206 SkPixelRef* pr;
207 if (fTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000208 pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000209 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000210 pr = SkNEW_ARGS(SkGrRenderTargetPixelRef, (fRenderTarget));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000211 }
reed@google.comaf951c92011-06-16 19:10:39 +0000212 this->setPixelRef(pr, 0)->unref();
213}
214
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000215SkGpuDevice::SkGpuDevice(GrContext* context,
216 SkBitmap::Config config,
217 int width,
218 int height)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000219 : SkDevice(config, width, height, false /*isOpaque*/)
220 , fClipStack(NULL) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 fNeedPrepareRenderTarget = false;
222 fDrawProcs = NULL;
223
reed@google.com7b201d22011-01-11 18:59:23 +0000224 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000225 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000226
reed@google.comac10a2d2010-12-22 21:39:39 +0000227 fTexture = NULL;
228 fRenderTarget = NULL;
229 fNeedClear = false;
230
reed@google.comaf951c92011-06-16 19:10:39 +0000231 if (config != SkBitmap::kRGB_565_Config) {
232 config = SkBitmap::kARGB_8888_Config;
233 }
234 SkBitmap bm;
235 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000237 GrTextureDesc desc;
238 desc.fFlags = kRenderTarget_GrTextureFlagBit;
239 desc.fWidth = width;
240 desc.fHeight = height;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000241 desc.fConfig = SkBitmapConfig2GrPixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
reed@google.comaf951c92011-06-16 19:10:39 +0000243 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000244
reed@google.comaf951c92011-06-16 19:10:39 +0000245 if (NULL != fTexture) {
246 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000247 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000248
reed@google.comaf951c92011-06-16 19:10:39 +0000249 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000250
reed@google.comaf951c92011-06-16 19:10:39 +0000251 // wrap the bitmap with a pixelref to expose our texture
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000252 SkGrTexturePixelRef* pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000253 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000254 } else {
255 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
256 width, height);
257 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000258 }
259}
260
261SkGpuDevice::~SkGpuDevice() {
262 if (fDrawProcs) {
263 delete fDrawProcs;
264 }
265
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000266 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
267 // This call gives the context a chance to relinquish it
268 fContext->setRenderTarget(NULL);
269
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000270 SkSafeUnref(fTexture);
271 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000272 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000273 GrAssert(NULL != fTexture);
274 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000275 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000276 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000277 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000278}
279
reed@google.comac10a2d2010-12-22 21:39:39 +0000280///////////////////////////////////////////////////////////////////////////////
281
282void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000283 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000284 fContext->setRenderTarget(fRenderTarget);
285 fContext->flush(true);
286 fNeedPrepareRenderTarget = true;
287}
288
289///////////////////////////////////////////////////////////////////////////////
290
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291namespace {
292GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
293 switch (config8888) {
294 case SkCanvas::kNative_Premul_Config8888:
295 return kSkia8888_PM_GrPixelConfig;
296 case SkCanvas::kNative_Unpremul_Config8888:
297 return kSkia8888_UPM_GrPixelConfig;
298 case SkCanvas::kBGRA_Premul_Config8888:
299 return kBGRA_8888_PM_GrPixelConfig;
300 case SkCanvas::kBGRA_Unpremul_Config8888:
301 return kBGRA_8888_UPM_GrPixelConfig;
302 case SkCanvas::kRGBA_Premul_Config8888:
303 return kRGBA_8888_PM_GrPixelConfig;
304 case SkCanvas::kRGBA_Unpremul_Config8888:
305 return kRGBA_8888_UPM_GrPixelConfig;
306 default:
307 GrCrash("Unexpected Config8888.");
308 return kSkia8888_PM_GrPixelConfig;
309 }
310}
311}
312
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000313bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
314 int x, int y,
315 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000316 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000317 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
318 SkASSERT(!bitmap.isNull());
319 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000320
bsalomon@google.com910267d2011-11-02 20:06:25 +0000321 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000322 GrPixelConfig config;
323 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000324 return fContext->readRenderTargetPixels(fRenderTarget,
325 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000326 bitmap.width(),
327 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000328 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000329 bitmap.getPixels(),
330 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000331}
332
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000333void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
334 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000335 SkAutoLockPixels alp(bitmap);
336 if (!bitmap.readyToDraw()) {
337 return;
338 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000339
340 GrPixelConfig config;
341 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
342 config = config8888_to_gr_config(config8888);
343 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000344 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000345 }
346
bsalomon@google.com6f379512011-11-16 20:36:03 +0000347 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
348 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000349}
350
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000351void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
352 INHERITED::onAttachToCanvas(canvas);
353
354 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
355 fClipStack = canvas->getClipStack();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000356
357 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000358}
359
360void SkGpuDevice::onDetachFromCanvas() {
361 INHERITED::onDetachFromCanvas();
362
363 fClipStack = NULL;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000364
365 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000366}
367
robertphillips@google.com607fe072012-07-24 13:54:00 +0000368#ifdef SK_DEBUG
369static void check_bounds(const SkClipStack& clipStack,
370 const SkRegion& clipRegion,
371 const SkIPoint& origin,
372 int renderTargetWidth,
373 int renderTargetHeight) {
374
375 SkIRect bound;
376 SkClipStack::BoundsType boundType;
377 SkRect temp;
378
379 bound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
380
381 clipStack.getBounds(&temp, &boundType);
382 if (SkClipStack::kNormal_BoundsType == boundType) {
383 SkIRect temp2;
384
385 temp.roundOut(&temp2);
386
387 temp2.offset(-origin.fX, -origin.fY);
388
389 if (!bound.intersect(temp2)) {
390 bound.setEmpty();
391 }
392 }
393
394// GrAssert(bound.contains(clipRegion.getBounds()));
395}
396#endif
397
reed@google.comac10a2d2010-12-22 21:39:39 +0000398///////////////////////////////////////////////////////////////////////////////
399
400static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000401 const SkClipStack& clipStack,
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000402 GrClipData& clipData,
reed@google.com6f8f2922011-03-04 22:27:10 +0000403 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000404 const SkIPoint& origin,
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000405 int renderTargetWidth, int renderTargetHeight,
406 GrClip* result) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000407 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000408
409 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000410 iter.reset(clipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000411
412#ifdef SK_DEBUG
413 check_bounds(clipStack, clipRegion, origin,
414 renderTargetWidth, renderTargetHeight);
415#endif
416
417 SkRect bounds;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000418 bool isIntersectionOfRects = false;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000419 clipStack.getConservativeBounds(-origin.fX,
420 -origin.fY,
421 renderTargetWidth,
422 renderTargetHeight,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000423 &bounds,
424 &isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000425
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000426 result->setFromIterator(&iter,
427 GrIntToScalar(-origin.x()),
428 GrIntToScalar(-origin.y()),
429 bounds);
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000430
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000431 GrAssert(result->isRect() == isIntersectionOfRects);
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000432
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000433 clipData.fClipStack = result;
434 clipData.fOrigin = origin;
435 context->setClip(&clipData);
reed@google.comac10a2d2010-12-22 21:39:39 +0000436}
437
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000438// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000439// and not the state from some other canvas/device
440void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000441 GrAssert(NULL != fClipStack);
442
reed@google.comac10a2d2010-12-22 21:39:39 +0000443 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000444 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000445
446 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000447 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000448
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000449 convert_matrixclip(fContext, *draw.fMatrix,
450 *fClipStack, fClipData, *draw.fClip, this->getOrigin(),
451 fRenderTarget->width(), fRenderTarget->height(),
452 &fGrClip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000453 fNeedPrepareRenderTarget = false;
454 }
455}
456
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000457void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
458 const SkClipStack& clipStack) {
459 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
460 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000461 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000462}
463
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000464void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
465
466 GrAssert(NULL != fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000467
reed@google.comac10a2d2010-12-22 21:39:39 +0000468 fContext->setRenderTarget(fRenderTarget);
469
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000470 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000471
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000472 convert_matrixclip(fContext, matrix, *fClipStack, fClipData, clip, this->getOrigin(),
473 fRenderTarget->width(), fRenderTarget->height(), &fGrClip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000474
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000475 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000476}
477
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000478SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000479 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000480 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000481}
482
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000483bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000484 if (NULL != fTexture) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000485 paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
486 SkNEW_ARGS(GrSingleTextureEffect, (fTexture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000487 return true;
488 }
489 return false;
490}
491
492///////////////////////////////////////////////////////////////////////////////
493
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000494SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
495SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
496SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
497SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
498SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
499 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000500SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
501 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000502SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
503SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000504
bsalomon@google.com84405e02012-03-05 19:57:21 +0000505namespace {
506
507// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
508// justAlpha indicates that skPaint's alpha should be used rather than the color
509// Callers may subsequently modify the GrPaint. Setting constantColor indicates
510// that the final paint will draw the same color at every pixel. This allows
511// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000512// color once while converting to GrPaint and then ignored.
513inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
514 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000515 bool justAlpha,
516 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000517 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000518 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000519
520 grPaint->fDither = skPaint.isDither();
521 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000522 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000523
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000524 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
525 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000526
527 SkXfermode* mode = skPaint.getXfermode();
528 if (mode) {
529 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000530 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000531#if 0
532 return false;
533#endif
534 }
535 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000536 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
537 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
538
bsalomon@google.com5782d712011-01-21 21:03:59 +0000539 if (justAlpha) {
540 uint8_t alpha = skPaint.getAlpha();
541 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000542 // justAlpha is currently set to true only if there is a texture,
543 // so constantColor should not also be true.
544 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000545 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000546 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000547 GrAssert(!grPaint->isTextureStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000548 }
Scroggo97c88c22011-05-11 14:05:25 +0000549 SkColorFilter* colorFilter = skPaint.getColorFilter();
550 SkColor color;
551 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000552 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000553 SkBitmap colorTransformTable;
Scroggo97c88c22011-05-11 14:05:25 +0000554 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000555 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000556 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000557 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000558 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000559 } else {
560 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000561 grPaint->fColor = SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000562 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000563 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000564 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
565 grPaint->fColorMatrixEnabled = true;
566 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
567 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
twiz@google.com58071162012-07-18 21:41:50 +0000568 } else if (colorFilter != NULL && colorFilter->asComponentTable(
569 &colorTransformTable)) {
570 grPaint->resetColorFilter();
571
572 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000573 GrTexture* texture = act->set(dev, colorTransformTable, colorSampler->textureParams());
twiz@google.com58071162012-07-18 21:41:50 +0000574
bsalomon@google.comb8670992012-07-25 21:27:09 +0000575 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000576 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000577 } else {
578 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000579 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000580 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000581}
582
bsalomon@google.com84405e02012-03-05 19:57:21 +0000583// This function is similar to skPaint2GrPaintNoShader but also converts
584// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
585// be used is set on grPaint and returned in param act. constantColor has the
586// same meaning as in skPaint2GrPaintNoShader.
587inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
588 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000589 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000590 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000591 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000592 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000593 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000594 return skPaint2GrPaintNoShader(dev,
595 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000596 false,
597 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000598 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000599 grPaint);
twiz@google.com58071162012-07-18 21:41:50 +0000600 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
601 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000602 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000603 }
604
rileya@google.com91f319c2012-07-25 17:18:31 +0000605 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
606 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
607
608 if (NULL != stage) {
609 sampler->setCustomStage(stage)->unref();
610 SkMatrix localM;
611 if (shader->getLocalMatrix(&localM)) {
612 SkMatrix inverse;
613 if (localM.invert(&inverse)) {
614 sampler->matrix()->preConcat(inverse);
615 }
616 }
617 return true;
618 }
619
reed@google.comac10a2d2010-12-22 21:39:39 +0000620 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000621 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000622 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000623 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000624 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000625
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000626 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000627 SkShader::GradientInfo info;
628 SkColor color;
629
630 info.fColors = &color;
631 info.fColorOffsets = NULL;
632 info.fColorCount = 1;
633 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
634 SkPaint copy(skPaint);
635 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000636 // modulate the paint alpha by the shader's solid color alpha
637 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
638 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000639 return skPaint2GrPaintNoShader(dev,
640 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000641 false,
642 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000643 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000644 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000645 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000646 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000647 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000648
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000649 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.comb8670992012-07-25 21:27:09 +0000650 sampler->textureParams()->reset(tileModes, skPaint.isFilterBitmap());
651 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, sampler->textureParams());
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000652
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000653 if (NULL == texture) {
654 SkDebugf("Couldn't convert bitmap to texture.\n");
655 return false;
656 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000657
rileya@google.com91f319c2012-07-25 17:18:31 +0000658 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000659
reed@google.comac10a2d2010-12-22 21:39:39 +0000660 // since our texture coords will be in local space, we wack the texture
661 // matrix to map them back into 0...1 before we load it
662 SkMatrix localM;
663 if (shader->getLocalMatrix(&localM)) {
664 SkMatrix inverse;
665 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000666 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000667 }
668 }
669 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000670 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
671 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000672 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000673 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000674
675 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000676}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000677}
reed@google.comac10a2d2010-12-22 21:39:39 +0000678
679///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000680void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000681 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000682}
683
reed@google.comac10a2d2010-12-22 21:39:39 +0000684void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
685 CHECK_SHOULD_DRAW(draw);
686
bsalomon@google.com5782d712011-01-21 21:03:59 +0000687 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000688 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000689 if (!skPaint2GrPaintShader(this,
690 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000691 true,
twiz@google.com58071162012-07-18 21:41:50 +0000692 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000693 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000694 return;
695 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000696
697 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000698}
699
700// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000701static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000702 kPoints_GrPrimitiveType,
703 kLines_GrPrimitiveType,
704 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000705};
706
707void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000708 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000709 CHECK_SHOULD_DRAW(draw);
710
711 SkScalar width = paint.getStrokeWidth();
712 if (width < 0) {
713 return;
714 }
715
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000716 // we only handle hairlines and paints without path effects or mask filters,
717 // else we let the SkDraw call our drawPath()
718 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000719 draw.drawPoints(mode, count, pts, paint, true);
720 return;
721 }
722
bsalomon@google.com5782d712011-01-21 21:03:59 +0000723 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000724 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000725 if (!skPaint2GrPaintShader(this,
726 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000727 true,
twiz@google.com58071162012-07-18 21:41:50 +0000728 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000729 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000730 return;
731 }
732
bsalomon@google.com5782d712011-01-21 21:03:59 +0000733 fContext->drawVertices(grPaint,
734 gPointMode2PrimtiveType[mode],
735 count,
736 (GrPoint*)pts,
737 NULL,
738 NULL,
739 NULL,
740 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000741}
742
reed@google.comc9aa5872011-04-05 21:05:37 +0000743///////////////////////////////////////////////////////////////////////////////
744
reed@google.comac10a2d2010-12-22 21:39:39 +0000745void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
746 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000747 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000748 CHECK_SHOULD_DRAW(draw);
749
bungeman@google.com79bd8772011-07-18 15:34:08 +0000750 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000751 SkScalar width = paint.getStrokeWidth();
752
753 /*
754 We have special code for hairline strokes, miter-strokes, and fills.
755 Anything else we just call our path code.
756 */
757 bool usePath = doStroke && width > 0 &&
758 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000759 // another two reasons we might need to call drawPath...
760 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000761 usePath = true;
762 }
reed@google.com67db6642011-05-26 11:46:35 +0000763 // until we aa rotated rects...
764 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
765 usePath = true;
766 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000767 // small miter limit means right angles show bevel...
768 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
769 paint.getStrokeMiter() < SK_ScalarSqrt2)
770 {
771 usePath = true;
772 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000773 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000774 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
775 usePath = true;
776 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000777
778 if (usePath) {
779 SkPath path;
780 path.addRect(rect);
781 this->drawPath(draw, path, paint, NULL, true);
782 return;
783 }
784
785 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000786 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000787 if (!skPaint2GrPaintShader(this,
788 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000789 true,
twiz@google.com58071162012-07-18 21:41:50 +0000790 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000791 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000792 return;
793 }
reed@google.com20efde72011-05-09 17:00:02 +0000794 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000795}
796
reed@google.com69302852011-02-16 18:08:07 +0000797#include "SkMaskFilter.h"
798#include "SkBounder.h"
799
bsalomon@google.com85003222012-03-28 14:44:37 +0000800///////////////////////////////////////////////////////////////////////////////
801
802// helpers for applying mask filters
803namespace {
804
805GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000806 switch (fillType) {
807 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000808 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000810 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000811 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000812 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000813 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000814 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000815 default:
816 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000817 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 }
819}
820
bsalomon@google.com85003222012-03-28 14:44:37 +0000821// We prefer to blur small rect with small radius via CPU.
822#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
823#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
824inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
825 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
826 rect.height() <= MIN_GPU_BLUR_SIZE &&
827 radius <= MIN_GPU_BLUR_RADIUS) {
828 return true;
829 }
830 return false;
831}
832
833bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
834 SkMaskFilter* filter, const SkMatrix& matrix,
835 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000836 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000837#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000838 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000839#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000840 SkMaskFilter::BlurInfo info;
841 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000842 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000843 return false;
844 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000845 SkScalar radius = info.fIgnoreTransform ? info.fRadius
846 : matrix.mapRadius(info.fRadius);
847 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000848 if (radius <= 0) {
849 return false;
850 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000851
852 SkRect srcRect = path.getBounds();
853 if (shouldDrawBlurWithCPU(srcRect, radius)) {
854 return false;
855 }
856
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000857 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000858 float sigma3 = sigma * 3.0f;
859
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000860 SkRect clipRect;
861 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000862
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000863 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000864 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
865 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000866 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000867 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000868 SkIRect finalIRect;
869 finalRect.roundOut(&finalIRect);
870 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000871 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000872 }
873 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000874 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000875 }
876 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000877 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000878 GrTextureDesc desc;
879 desc.fFlags = kRenderTarget_GrTextureFlagBit;
880 desc.fWidth = SkScalarCeilToInt(srcRect.width());
881 desc.fHeight = SkScalarCeilToInt(srcRect.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_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000885
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000886 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
887 desc.fConfig = kAlpha_8_GrPixelConfig;
888 }
889
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000890 GrAutoScratchTexture pathEntry(context, desc);
891 GrTexture* pathTexture = pathEntry.texture();
892 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000893 return false;
894 }
895 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000896 // Once this code moves into GrContext, this should be changed to use
897 // an AutoClipRestore.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000898 const GrClipData* oldClipData = context->getClip();
899
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000900 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000901
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000902 GrClip newClipStack(srcRect);
903 GrClipData newClipData;
904 newClipData.fClipStack = &newClipStack;
905 context->setClip(&newClipData);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000906
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000907 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000908 GrPaint tempPaint;
909 tempPaint.reset();
910
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000911 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000912 tempPaint.fAntiAlias = grp->fAntiAlias;
913 if (tempPaint.fAntiAlias) {
914 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
915 // blend coeff of zero requires dual source blending support in order
916 // to properly blend partially covered pixels. This means the AA
917 // code path may not be taken. So we use a dst blend coeff of ISA. We
918 // could special case AA draws to a dst surface with known alpha=0 to
919 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000920 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
921 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000922 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000923 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000924 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000925
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000926 // If we're doing a normal blur, we can clobber the pathTexture in the
927 // gaussianBlur. Otherwise, we need to save it for later compositing.
928 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000929 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
930 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000931
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000932 if (!isNormalBlur) {
933 GrPaint paint;
934 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +0000935 paint.textureSampler(0)->textureParams()->setClampNoFilter();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000936 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
937 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000938 // Blend pathTexture over blurTexture.
939 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000940 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
941 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000942 if (SkMaskFilter::kInner_BlurType == blurType) {
943 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000944 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
945 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000946 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
947 // solid: dst = src + dst - src * dst
948 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000949 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
950 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000951 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
952 // outer: dst = dst * (1 - src)
953 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000954 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
955 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000956 }
957 context->drawRect(paint, srcRect);
958 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000959 context->setRenderTarget(oldRenderTarget);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000960 context->setClip(oldClipData);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000961
bsalomon@google.come3d32162012-07-20 13:37:06 +0000962 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
963 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000964 }
965
966 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
967 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000968 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +0000969 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000970 grp->maskSampler(MASK_IDX)->setCustomStage(
971 SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)))->unref();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000972 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
973 -finalRect.fTop);
974 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
975 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000976 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000977 return true;
978}
979
bsalomon@google.com85003222012-03-28 14:44:37 +0000980bool drawWithMaskFilter(GrContext* context, const SkPath& path,
981 SkMaskFilter* filter, const SkMatrix& matrix,
982 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000983 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000984 SkMask srcM, dstM;
985
986 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000987 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000988 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000989 return false;
990 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000991 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000992
993 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
994 return false;
995 }
996 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000997 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000998
999 if (clip.quickReject(dstM.fBounds)) {
1000 return false;
1001 }
1002 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1003 return false;
1004 }
1005
1006 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1007 // the current clip (and identity matrix) and grpaint settings
1008
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001009 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001010
bsalomon@google.come3d32162012-07-20 13:37:06 +00001011 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
1012 return false;
1013 }
1014
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001015 GrTextureDesc desc;
1016 desc.fWidth = dstM.fBounds.width();
1017 desc.fHeight = dstM.fBounds.height();
1018 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +00001019
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001020 GrAutoScratchTexture ast(context, desc);
1021 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001022
reed@google.com69302852011-02-16 18:08:07 +00001023 if (NULL == texture) {
1024 return false;
1025 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001026 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001027 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001028
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001029 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1030 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001031 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +00001032 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001033 grp->maskSampler(MASK_IDX)->setCustomStage(
1034 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001035 GrRect d;
1036 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001037 GrIntToScalar(dstM.fBounds.fTop),
1038 GrIntToScalar(dstM.fBounds.fRight),
1039 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001040
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001041 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1042 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1043 -dstM.fBounds.fTop*SK_Scalar1);
1044 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001045 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001046 return true;
1047}
reed@google.com69302852011-02-16 18:08:07 +00001048
bsalomon@google.com85003222012-03-28 14:44:37 +00001049}
1050
1051///////////////////////////////////////////////////////////////////////////////
1052
reed@google.com0c219b62011-02-16 21:31:18 +00001053void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001054 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001055 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001056 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001057 CHECK_SHOULD_DRAW(draw);
1058
reed@google.comfe626382011-09-21 13:50:35 +00001059 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001060
bsalomon@google.com5782d712011-01-21 21:03:59 +00001061 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001062 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001063 if (!skPaint2GrPaintShader(this,
1064 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001065 true,
twiz@google.com58071162012-07-18 21:41:50 +00001066 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001067 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001068 return;
1069 }
1070
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001071 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1072 // if we can, we draw lots faster (raster device does this same test)
1073 SkScalar hairlineCoverage;
1074 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1075 doFill = false;
1076 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1077 grPaint.fCoverage);
1078 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001079
reed@google.comfe626382011-09-21 13:50:35 +00001080 // If we have a prematrix, apply it to the path, optimizing for the case
1081 // where the original path can in fact be modified in place (even though
1082 // its parameter type is const).
1083 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1084 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001085
1086 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001087 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001088
reed@google.come3445642011-02-16 23:20:39 +00001089 if (!pathIsMutable) {
1090 result = &tmpPath;
1091 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001092 }
reed@google.come3445642011-02-16 23:20:39 +00001093 // should I push prePathMatrix on our MV stack temporarily, instead
1094 // of applying it here? See SkDraw.cpp
1095 pathPtr->transform(*prePathMatrix, result);
1096 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001097 }
reed@google.com0c219b62011-02-16 21:31:18 +00001098 // at this point we're done with prePathMatrix
1099 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001100
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001101 if (paint.getPathEffect() ||
1102 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001103 // it is safe to use tmpPath here, even if we already used it for the
1104 // prepathmatrix, since getFillPath can take the same object for its
1105 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001106 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001107 pathPtr = &tmpPath;
1108 }
1109
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001110 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001111 // avoid possibly allocating a new path in transform if we can
1112 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1113
1114 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001115 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001116 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001117 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001118 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001119 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001120 &grPaint, pathFillType)) {
1121 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1122 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001123 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001124 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001125 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001126 }
reed@google.com69302852011-02-16 18:08:07 +00001127 return;
1128 }
reed@google.com69302852011-02-16 18:08:07 +00001129
bsalomon@google.com47059542012-06-06 20:51:20 +00001130 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001131
reed@google.com0c219b62011-02-16 21:31:18 +00001132 if (doFill) {
1133 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001134 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001135 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001136 break;
1137 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001138 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001139 break;
1140 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001141 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001142 break;
1143 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001144 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001145 break;
1146 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001147 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001148 return;
1149 }
1150 }
1151
reed@google.com07f3ee12011-05-16 17:21:57 +00001152 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001153}
1154
bsalomon@google.comfb309512011-11-30 14:13:48 +00001155namespace {
1156
1157inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1158 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1159 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1160 return tilesX * tilesY;
1161}
1162
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001163inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001164 const SkIRect* srcRectPtr,
1165 int maxTextureSize) {
1166 static const int kSmallTileSize = 1 << 10;
1167 if (maxTextureSize <= kSmallTileSize) {
1168 return maxTextureSize;
1169 }
1170
1171 size_t maxTexTotalTileSize;
1172 size_t smallTotalTileSize;
1173
1174 if (NULL == srcRectPtr) {
1175 int w = bitmap.width();
1176 int h = bitmap.height();
1177 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1178 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1179 } else {
1180 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1181 srcRectPtr->fTop,
1182 srcRectPtr->fRight,
1183 srcRectPtr->fBottom,
1184 maxTextureSize);
1185 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1186 srcRectPtr->fTop,
1187 srcRectPtr->fRight,
1188 srcRectPtr->fBottom,
1189 kSmallTileSize);
1190 }
1191 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1192 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1193
1194 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1195 return kSmallTileSize;
1196 } else {
1197 return maxTextureSize;
1198 }
1199}
1200}
1201
1202bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001203 const GrTextureParams& params,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001204 const SkIRect* srcRectPtr,
1205 int* tileSize) const {
1206 SkASSERT(NULL != tileSize);
1207
1208 // if bitmap is explictly texture backed then just use the texture
1209 if (NULL != bitmap.getTexture()) {
1210 return false;
1211 }
1212 // if it's larger than the max texture size, then we have no choice but
1213 // tiling
1214 const int maxTextureSize = fContext->getMaxTextureSize();
1215 if (bitmap.width() > maxTextureSize ||
1216 bitmap.height() > maxTextureSize) {
1217 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1218 return true;
1219 }
1220 // if we are going to have to draw the whole thing, then don't tile
1221 if (NULL == srcRectPtr) {
1222 return false;
1223 }
1224 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001225 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001226 return false;
1227 }
1228
1229 // At this point we know we could do the draw by uploading the entire bitmap
1230 // as a texture. However, if the texture would be large compared to the
1231 // cache size and we don't require most of it for this draw then tile to
1232 // reduce the amount of upload and cache spill.
1233
1234 // assumption here is that sw bitmap size is a good proxy for its size as
1235 // a texture
1236 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001237 size_t cacheSize;
1238 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001239 if (bmpSize < cacheSize / 2) {
1240 return false;
1241 }
1242
1243 SkFixed fracUsed =
1244 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1245 (srcRectPtr->height() << 16) / bitmap.height());
1246 if (fracUsed <= SK_FixedHalf) {
1247 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1248 return true;
1249 } else {
1250 return false;
1251 }
1252}
1253
reed@google.comac10a2d2010-12-22 21:39:39 +00001254void SkGpuDevice::drawBitmap(const SkDraw& draw,
1255 const SkBitmap& bitmap,
1256 const SkIRect* srcRectPtr,
1257 const SkMatrix& m,
1258 const SkPaint& paint) {
1259 CHECK_SHOULD_DRAW(draw);
1260
1261 SkIRect srcRect;
1262 if (NULL == srcRectPtr) {
1263 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1264 } else {
1265 srcRect = *srcRectPtr;
1266 }
1267
junov@google.comd935cfb2011-06-27 20:48:23 +00001268 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001269 // Convert the bitmap to a shader so that the rect can be drawn
1270 // through drawRect, which supports mask filters.
1271 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001272 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001273 if (srcRectPtr) {
1274 if (!bitmap.extractSubset(&tmp, srcRect)) {
1275 return; // extraction failed
1276 }
1277 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001278 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001279 }
1280 SkPaint paintWithTexture(paint);
1281 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1282 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001283 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001284 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001285
junov@google.com1d329782011-07-28 20:10:09 +00001286 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001287 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001288 // also affect the behavior of the mask filter.
1289 SkMatrix drawMatrix;
1290 drawMatrix.setConcat(*draw.fMatrix, m);
1291 SkDraw transformedDraw(draw);
1292 transformedDraw.fMatrix = &drawMatrix;
1293
1294 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1295
junov@google.comd935cfb2011-06-27 20:48:23 +00001296 return;
1297 }
1298
bsalomon@google.com5782d712011-01-21 21:03:59 +00001299 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001300 SkAutoCachedTexture colorLutTexture;
1301 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001302 return;
1303 }
bsalomon@google.comb8670992012-07-25 21:27:09 +00001304 GrTextureParams* params = grPaint.textureSampler(kBitmapTextureIdx)->textureParams();
1305 params->setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001306
bsalomon@google.comfb309512011-11-30 14:13:48 +00001307 int tileSize;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001308 if (!this->shouldTileBitmap(bitmap, *params, srcRectPtr, &tileSize)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001309 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001310 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001311 return;
1312 }
1313
1314 // undo the translate done by SkCanvas
1315 int DX = SkMax32(0, srcRect.fLeft);
1316 int DY = SkMax32(0, srcRect.fTop);
1317 // compute clip bounds in local coordinates
1318 SkIRect clipRect;
1319 {
1320 SkRect r;
1321 r.set(draw.fClip->getBounds());
1322 SkMatrix matrix, inverse;
1323 matrix.setConcat(*draw.fMatrix, m);
1324 if (!matrix.invert(&inverse)) {
1325 return;
1326 }
1327 inverse.mapRect(&r);
1328 r.roundOut(&clipRect);
1329 // apply the canvas' translate to our local clip
1330 clipRect.offset(DX, DY);
1331 }
1332
bsalomon@google.comfb309512011-11-30 14:13:48 +00001333 int nx = bitmap.width() / tileSize;
1334 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001335 for (int x = 0; x <= nx; x++) {
1336 for (int y = 0; y <= ny; y++) {
1337 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001338 tileR.set(x * tileSize, y * tileSize,
1339 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001340 if (!SkIRect::Intersects(tileR, clipRect)) {
1341 continue;
1342 }
1343
1344 SkIRect srcR = tileR;
1345 if (!srcR.intersect(srcRect)) {
1346 continue;
1347 }
1348
1349 SkBitmap tmpB;
1350 if (bitmap.extractSubset(&tmpB, tileR)) {
1351 // now offset it to make it "local" to our tmp bitmap
1352 srcR.offset(-tileR.fLeft, -tileR.fTop);
1353
1354 SkMatrix tmpM(m);
1355 {
1356 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1357 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1358 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1359 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001360 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001361 }
1362 }
1363 }
1364}
1365
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001366namespace {
1367
1368bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1369 // detect pixel disalignment
1370 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1371 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1372 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1373 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1374 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1375 COLOR_BLEED_TOLERANCE &&
1376 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1377 COLOR_BLEED_TOLERANCE) {
1378 return true;
1379 }
1380 return false;
1381}
1382
1383bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1384 const SkMatrix& m) {
1385 // Only gets called if hasAlignedSamples returned false.
1386 // So we can assume that sampling is axis aligned but not texel aligned.
1387 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1388 SkRect innerSrcRect(srcRect), innerTransformedRect,
1389 outerTransformedRect(transformedRect);
1390 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1391 m.mapRect(&innerTransformedRect, innerSrcRect);
1392
1393 // The gap between outerTransformedRect and innerTransformedRect
1394 // represents the projection of the source border area, which is
1395 // problematic for color bleeding. We must check whether any
1396 // destination pixels sample the border area.
1397 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1398 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1399 SkIRect outer, inner;
1400 outerTransformedRect.round(&outer);
1401 innerTransformedRect.round(&inner);
1402 // If the inner and outer rects round to the same result, it means the
1403 // border does not overlap any pixel centers. Yay!
1404 return inner != outer;
1405}
1406
1407} // unnamed namespace
1408
reed@google.comac10a2d2010-12-22 21:39:39 +00001409/*
1410 * This is called by drawBitmap(), which has to handle images that may be too
1411 * large to be represented by a single texture.
1412 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001413 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1414 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001415 */
1416void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1417 const SkBitmap& bitmap,
1418 const SkIRect& srcRect,
1419 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001420 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001421 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1422 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001423
reed@google.com9c49bc32011-07-07 13:42:37 +00001424 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001425 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001426 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001427 return;
1428 }
1429
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001430 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001431
bsalomon@google.comb8670992012-07-25 21:27:09 +00001432 sampler->textureParams()->setClamp();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001433 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001434
1435 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001436 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001437 if (NULL == texture) {
1438 return;
1439 }
1440
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001441 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1442 (GrSingleTextureEffect, (texture)))->unref();
reed@google.com46799cd2011-02-22 20:56:26 +00001443
reed@google.com20efde72011-05-09 17:00:02 +00001444 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1445 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001446 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001447 float wInv = 1.f / bitmap.width();
1448 float hInv = 1.f / bitmap.height();
1449 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1450 SkFloatToScalar(srcRect.fTop * hInv),
1451 SkFloatToScalar(srcRect.fRight * wInv),
1452 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001453
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001454 bool needsTextureDomain = false;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001455 if (sampler->textureParams()->isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001456 // Need texture domain if drawing a sub rect.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001457 needsTextureDomain = srcRect.width() < bitmap.width() || srcRect.height() < bitmap.height();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001458 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1459 // sampling is axis-aligned
1460 GrRect floatSrcRect, transformedRect;
1461 floatSrcRect.set(srcRect);
1462 SkMatrix srcToDeviceMatrix(m);
1463 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1464 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1465
1466 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1467 // Samples are texel-aligned, so filtering is futile
bsalomon@google.comb8670992012-07-25 21:27:09 +00001468 sampler->textureParams()->setBilerp(false);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001469 needsTextureDomain = false;
1470 } else {
1471 needsTextureDomain = needsTextureDomain &&
1472 mayColorBleed(floatSrcRect, transformedRect, m);
1473 }
1474 }
1475 }
1476
1477 GrRect textureDomain = GrRect::MakeEmpty();
1478
1479 if (needsTextureDomain) {
1480 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001481 GrScalar left, top, right, bottom;
1482 if (srcRect.width() > 1) {
1483 GrScalar border = GR_ScalarHalf / bitmap.width();
1484 left = paintRect.left() + border;
1485 right = paintRect.right() - border;
1486 } else {
1487 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1488 }
1489 if (srcRect.height() > 1) {
1490 GrScalar border = GR_ScalarHalf / bitmap.height();
1491 top = paintRect.top() + border;
1492 bottom = paintRect.bottom() - border;
1493 } else {
1494 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1495 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001496 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001497 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1498 (texture,
1499 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001500 }
1501
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001502 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001503}
1504
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001505namespace {
1506
1507void apply_custom_stage(GrContext* context,
1508 GrTexture* srcTexture,
1509 GrTexture* dstTexture,
1510 const GrRect& rect,
1511 GrCustomStage* stage) {
1512 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001513 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001514 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001515 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001516
1517 GrMatrix sampleM;
1518 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1519 GrPaint paint;
1520 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001521 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001522 paint.textureSampler(0)->reset(sampleM);
1523 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001524 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001525}
1526
1527};
1528
reed@google.com8926b162012-03-23 15:36:36 +00001529static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1530 SkImageFilter* filter, const GrRect& rect) {
1531 GrAssert(filter);
1532
1533 SkSize blurSize;
1534 SkISize radius;
1535
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001536 GrTextureDesc desc;
1537 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1538 desc.fWidth = SkScalarCeilToInt(rect.width());
1539 desc.fHeight = SkScalarCeilToInt(rect.height());
1540 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001541 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001542
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001543 if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001544 GrAutoScratchTexture dst(context, desc);
1545 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1546 texture = dst.detach();
1547 stage->unref();
1548 } else if (filter->asABlur(&blurSize)) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001549 texture = context->gaussianBlur(texture, false, rect,
reed@google.com8926b162012-03-23 15:36:36 +00001550 blurSize.width(),
1551 blurSize.height());
reed@google.com8926b162012-03-23 15:36:36 +00001552 } else if (filter->asADilate(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001553 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001554 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001555 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001556 } else if (filter->asAnErode(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001557 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001558 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001559 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001560 }
1561 return texture;
1562}
1563
reed@google.comac10a2d2010-12-22 21:39:39 +00001564void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1565 int left, int top, const SkPaint& paint) {
1566 CHECK_SHOULD_DRAW(draw);
1567
reed@google.com8926b162012-03-23 15:36:36 +00001568 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001569 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1570 return;
1571 }
1572
reed@google.com76dd2772012-01-05 21:15:07 +00001573 int w = bitmap.width();
1574 int h = bitmap.height();
1575
bsalomon@google.com5782d712011-01-21 21:03:59 +00001576 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001577 SkAutoCachedTexture colorLutTexture;
1578 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001579 return;
1580 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001581
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001582 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001583
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001584 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001585
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001586 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001587 sampler->reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001588 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001589 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1590 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001591
reed@google.com8926b162012-03-23 15:36:36 +00001592 SkImageFilter* filter = paint.getImageFilter();
1593 if (NULL != filter) {
1594 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001595 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001596 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001597 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1598 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001599 texture = filteredTexture;
1600 filteredTexture->unref();
1601 }
reed@google.com76dd2772012-01-05 21:15:07 +00001602 }
reed@google.com8926b162012-03-23 15:36:36 +00001603
bsalomon@google.com5782d712011-01-21 21:03:59 +00001604 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001605 GrRect::MakeXYWH(GrIntToScalar(left),
1606 GrIntToScalar(top),
1607 GrIntToScalar(w),
1608 GrIntToScalar(h)),
1609 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1610 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001611}
1612
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001613void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001614 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001615 // clear of the source device must occur before CHECK_SHOULD_DRAW
1616 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1617 if (dev->fNeedClear) {
1618 // TODO: could check here whether we really need to draw at all
1619 dev->clear(0x0);
1620 }
1621
reed@google.comac10a2d2010-12-22 21:39:39 +00001622 CHECK_SHOULD_DRAW(draw);
1623
bsalomon@google.com5782d712011-01-21 21:03:59 +00001624 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001625 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001626 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001627 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001628 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001629 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001630 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001631
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001632 GrTexture* devTex = grPaint.getTextureSampler(kBitmapTextureIdx).getCustomStage()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001633 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001634
reed@google.com8926b162012-03-23 15:36:36 +00001635 SkImageFilter* filter = paint.getImageFilter();
1636 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001637 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001638 SkIntToScalar(devTex->height()));
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001639 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001640 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001641 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1642 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001643 devTex = filteredTexture;
1644 filteredTexture->unref();
1645 }
1646 }
1647
bsalomon@google.com5782d712011-01-21 21:03:59 +00001648 const SkBitmap& bm = dev->accessBitmap(false);
1649 int w = bm.width();
1650 int h = bm.height();
1651
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001652 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001653 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1654 GrIntToScalar(y),
1655 GrIntToScalar(w),
1656 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001657
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001658 // The device being drawn may not fill up its texture (saveLayer uses
1659 // the approximate ).
1660 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1661 GR_Scalar1 * h / devTex->height());
1662
1663 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001664}
1665
reed@google.com8926b162012-03-23 15:36:36 +00001666bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001667 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001668 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001669
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001670 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001671 !filter->asABlur(&size) &&
1672 !filter->asADilate(&radius) &&
1673 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001674 return false;
1675 }
reed@google.com8926b162012-03-23 15:36:36 +00001676 return true;
1677}
1678
1679bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1680 const SkMatrix& ctm,
1681 SkBitmap* result, SkIPoint* offset) {
1682 // want explicitly our impl, so guard against a subclass of us overriding it
1683 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001684 return false;
1685 }
reed@google.com8926b162012-03-23 15:36:36 +00001686
1687 SkAutoLockPixels alp(src, !src.getTexture());
1688 if (!src.getTexture() && !src.readyToDraw()) {
1689 return false;
1690 }
1691
1692 GrPaint paint;
1693 paint.reset();
1694
1695 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1696
1697 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001698 SkAutoCachedTexture act(this, src, sampler->textureParams(), &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001699
1700 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001701 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1702 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001703 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1704 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001705 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1706 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001707 resultTexture->unref();
1708 }
reed@google.com76dd2772012-01-05 21:15:07 +00001709 return true;
1710}
1711
reed@google.comac10a2d2010-12-22 21:39:39 +00001712///////////////////////////////////////////////////////////////////////////////
1713
1714// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001715static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001716 kTriangles_GrPrimitiveType,
1717 kTriangleStrip_GrPrimitiveType,
1718 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001719};
1720
1721void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1722 int vertexCount, const SkPoint vertices[],
1723 const SkPoint texs[], const SkColor colors[],
1724 SkXfermode* xmode,
1725 const uint16_t indices[], int indexCount,
1726 const SkPaint& paint) {
1727 CHECK_SHOULD_DRAW(draw);
1728
bsalomon@google.com5782d712011-01-21 21:03:59 +00001729 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001730 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001731 // we ignore the shader if texs is null.
1732 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001733 if (!skPaint2GrPaintNoShader(this,
1734 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001735 false,
1736 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001737 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001738 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001739 return;
1740 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001741 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001742 if (!skPaint2GrPaintShader(this,
1743 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001744 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001745 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001746 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001747 return;
1748 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001749 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001750
1751 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001752 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001753 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1754#if 0
1755 return
1756#endif
1757 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001758 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001759
bsalomon@google.com498776a2011-08-16 19:20:44 +00001760 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1761 if (NULL != colors) {
1762 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001763 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001764 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001765 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001766 }
1767 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001768 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001769 fContext->drawVertices(grPaint,
1770 gVertexMode2PrimitiveType[vmode],
1771 vertexCount,
1772 (GrPoint*) vertices,
1773 (GrPoint*) texs,
1774 colors,
1775 indices,
1776 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001777}
1778
1779///////////////////////////////////////////////////////////////////////////////
1780
1781static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001782 GrFontScaler* scaler = (GrFontScaler*)data;
1783 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001784}
1785
1786static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1787 void* auxData;
1788 GrFontScaler* scaler = NULL;
1789 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1790 scaler = (GrFontScaler*)auxData;
1791 }
1792 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001793 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001794 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1795 }
1796 return scaler;
1797}
1798
1799static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1800 SkFixed fx, SkFixed fy,
1801 const SkGlyph& glyph) {
1802 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1803
bungeman@google.com15865a72012-01-11 16:28:04 +00001804 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001805
1806 if (NULL == procs->fFontScaler) {
1807 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1808 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001809
bungeman@google.com15865a72012-01-11 16:28:04 +00001810 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1811 glyph.getSubXFixed(),
1812 glyph.getSubYFixed()),
1813 SkFixedFloorToFixed(fx),
1814 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001815 procs->fFontScaler);
1816}
1817
bsalomon@google.com5782d712011-01-21 21:03:59 +00001818SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001819
1820 // deferred allocation
1821 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001822 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001823 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1824 fDrawProcs->fContext = fContext;
1825 }
1826
1827 // init our (and GL's) state
1828 fDrawProcs->fTextContext = context;
1829 fDrawProcs->fFontScaler = NULL;
1830 return fDrawProcs;
1831}
1832
1833void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1834 size_t byteLength, SkScalar x, SkScalar y,
1835 const SkPaint& paint) {
1836 CHECK_SHOULD_DRAW(draw);
1837
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001838 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001839 // this guy will just call our drawPath()
1840 draw.drawText((const char*)text, byteLength, x, y, paint);
1841 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001842 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001843
1844 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001845 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001846 if (!skPaint2GrPaintShader(this,
1847 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001848 true,
twiz@google.com58071162012-07-18 21:41:50 +00001849 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001850 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001851 return;
1852 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001853 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1854 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001855 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1856 }
1857}
1858
1859void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1860 size_t byteLength, const SkScalar pos[],
1861 SkScalar constY, int scalarsPerPos,
1862 const SkPaint& paint) {
1863 CHECK_SHOULD_DRAW(draw);
1864
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001865 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001866 // this guy will just call our drawPath()
1867 draw.drawPosText((const char*)text, byteLength, pos, constY,
1868 scalarsPerPos, paint);
1869 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001870 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001871
1872 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001873 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001874 if (!skPaint2GrPaintShader(this,
1875 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001876 true,
twiz@google.com58071162012-07-18 21:41:50 +00001877 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001878 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001879 return;
1880 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001881 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1882 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001883 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1884 scalarsPerPos, paint);
1885 }
1886}
1887
1888void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1889 size_t len, const SkPath& path,
1890 const SkMatrix* m, const SkPaint& paint) {
1891 CHECK_SHOULD_DRAW(draw);
1892
1893 SkASSERT(draw.fDevice == this);
1894 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1895}
1896
1897///////////////////////////////////////////////////////////////////////////////
1898
reed@google.comf67e4cf2011-03-15 20:56:58 +00001899bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1900 if (!paint.isLCDRenderText()) {
1901 // we're cool with the paint as is
1902 return false;
1903 }
1904
1905 if (paint.getShader() ||
1906 paint.getXfermode() || // unless its srcover
1907 paint.getMaskFilter() ||
1908 paint.getRasterizer() ||
1909 paint.getColorFilter() ||
1910 paint.getPathEffect() ||
1911 paint.isFakeBoldText() ||
1912 paint.getStyle() != SkPaint::kFill_Style) {
1913 // turn off lcd
1914 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1915 flags->fHinting = paint.getHinting();
1916 return true;
1917 }
1918 // we're cool with the paint as is
1919 return false;
1920}
1921
reed@google.com75d939b2011-12-07 15:07:23 +00001922void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001923 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001924 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001925}
1926
reed@google.comf67e4cf2011-03-15 20:56:58 +00001927///////////////////////////////////////////////////////////////////////////////
1928
bsalomon@google.comfb309512011-11-30 14:13:48 +00001929bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001930 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001931 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001932 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001933
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001934 GrTextureDesc desc;
1935 desc.fWidth = bitmap.width();
1936 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001937 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001938 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001939
bsalomon@google.comb8670992012-07-25 21:27:09 +00001940 return this->context()->isTextureInCache(desc, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001941}
1942
1943
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001944SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1945 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001946 bool isOpaque,
1947 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001948 GrTextureDesc desc;
1949 desc.fConfig = fRenderTarget->config();
1950 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1951 desc.fWidth = width;
1952 desc.fHeight = height;
1953 desc.fSampleCnt = fRenderTarget->numSamples();
1954
1955 GrContext::TextureCacheEntry cacheEntry;
1956 GrTexture* texture;
1957 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001958 // Skia's convention is to only clear a device if it is non-opaque.
1959 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001960
1961#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1962 // layers are never draw in repeat modes, so we can request an approx
1963 // match and ignore any padding.
1964 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1965 GrContext::kApprox_ScratchTexMatch :
1966 GrContext::kExact_ScratchTexMatch;
1967 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1968 texture = cacheEntry.texture();
1969#else
1970 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1971 texture = tunref.get();
1972#endif
1973 if (texture) {
1974 return SkNEW_ARGS(SkGpuDevice,(fContext,
1975 texture,
1976 cacheEntry,
1977 needClear));
1978 } else {
1979 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1980 width, height);
1981 return NULL;
1982 }
1983}
1984
1985SkGpuDevice::SkGpuDevice(GrContext* context,
1986 GrTexture* texture,
1987 TexCache cacheEntry,
1988 bool needClear)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001989 : SkDevice(make_bitmap(context, texture->asRenderTarget()))
1990 , fClipStack(NULL) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001991 GrAssert(texture && texture->asRenderTarget());
1992 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1993 this->initFromRenderTarget(context, texture->asRenderTarget());
1994 fCache = cacheEntry;
1995 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001996}
1997