blob: cb33d7687aa11364599aa47a73164397c933a507 [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) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000485 // FIXME: cannot use GrSingleTextureEffect here: fails
486 // assert in line 1617: null != devTex; generalizing GrPaint::getTexture()
487 // to grab textures off of GrCustomStages breaks gms in various ways -
488 // particularly since table color filter requires multiple textures
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000489 paint->setTexture(kBitmapTextureIdx, fTexture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000490 //paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
491 //SkNEW_ARGS(GrSingleTextureEffect, (fTexture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000492 return true;
493 }
494 return false;
495}
496
497///////////////////////////////////////////////////////////////////////////////
498
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000499SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
500SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
501SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
502SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
503SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
504 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000505SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
506 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000507SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
508SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000509
bsalomon@google.com84405e02012-03-05 19:57:21 +0000510namespace {
511
512// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
513// justAlpha indicates that skPaint's alpha should be used rather than the color
514// Callers may subsequently modify the GrPaint. Setting constantColor indicates
515// that the final paint will draw the same color at every pixel. This allows
516// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000517// color once while converting to GrPaint and then ignored.
518inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
519 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000520 bool justAlpha,
521 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000522 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000523 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000524
525 grPaint->fDither = skPaint.isDither();
526 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000527 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000528
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000529 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
530 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000531
532 SkXfermode* mode = skPaint.getXfermode();
533 if (mode) {
534 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000535 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000536#if 0
537 return false;
538#endif
539 }
540 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000541 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
542 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
543
bsalomon@google.com5782d712011-01-21 21:03:59 +0000544 if (justAlpha) {
545 uint8_t alpha = skPaint.getAlpha();
546 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000547 // justAlpha is currently set to true only if there is a texture,
548 // so constantColor should not also be true.
549 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000550 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000551 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000552 GrAssert(NULL == grPaint->getTexture(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000553 }
Scroggo97c88c22011-05-11 14:05:25 +0000554 SkColorFilter* colorFilter = skPaint.getColorFilter();
555 SkColor color;
556 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000557 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000558 SkBitmap colorTransformTable;
Scroggo97c88c22011-05-11 14:05:25 +0000559 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000560 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000561 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000562 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000563 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000564 } else {
565 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000566 grPaint->fColor = SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000567 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000568 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000569 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
570 grPaint->fColorMatrixEnabled = true;
571 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
572 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
twiz@google.com58071162012-07-18 21:41:50 +0000573 } else if (colorFilter != NULL && colorFilter->asComponentTable(
574 &colorTransformTable)) {
575 grPaint->resetColorFilter();
576
577 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000578 GrTexture* texture = act->set(dev, colorTransformTable, colorSampler->textureParams());
twiz@google.com58071162012-07-18 21:41:50 +0000579
bsalomon@google.comb8670992012-07-25 21:27:09 +0000580 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000581 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000582 } else {
583 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000584 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000585 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000586}
587
bsalomon@google.com84405e02012-03-05 19:57:21 +0000588// This function is similar to skPaint2GrPaintNoShader but also converts
589// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
590// be used is set on grPaint and returned in param act. constantColor has the
591// same meaning as in skPaint2GrPaintNoShader.
592inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
593 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000594 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000595 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000596 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000597 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000598 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000599 return skPaint2GrPaintNoShader(dev,
600 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000601 false,
602 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000603 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000604 grPaint);
twiz@google.com58071162012-07-18 21:41:50 +0000605 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
606 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000607 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000608 }
609
rileya@google.com91f319c2012-07-25 17:18:31 +0000610 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
611 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
612
613 if (NULL != stage) {
614 sampler->setCustomStage(stage)->unref();
615 SkMatrix localM;
616 if (shader->getLocalMatrix(&localM)) {
617 SkMatrix inverse;
618 if (localM.invert(&inverse)) {
619 sampler->matrix()->preConcat(inverse);
620 }
621 }
622 return true;
623 }
624
reed@google.comac10a2d2010-12-22 21:39:39 +0000625 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000626 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000627 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000628 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000629 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000630
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000631 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000632 SkShader::GradientInfo info;
633 SkColor color;
634
635 info.fColors = &color;
636 info.fColorOffsets = NULL;
637 info.fColorCount = 1;
638 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
639 SkPaint copy(skPaint);
640 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000641 // modulate the paint alpha by the shader's solid color alpha
642 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
643 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000644 return skPaint2GrPaintNoShader(dev,
645 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000646 false,
647 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000648 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000649 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000650 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000651 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000652 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000653
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000654 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.comb8670992012-07-25 21:27:09 +0000655 sampler->textureParams()->reset(tileModes, skPaint.isFilterBitmap());
656 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, sampler->textureParams());
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000657
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000658 if (NULL == texture) {
659 SkDebugf("Couldn't convert bitmap to texture.\n");
660 return false;
661 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000662
rileya@google.com91f319c2012-07-25 17:18:31 +0000663 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000664
reed@google.comac10a2d2010-12-22 21:39:39 +0000665 // since our texture coords will be in local space, we wack the texture
666 // matrix to map them back into 0...1 before we load it
667 SkMatrix localM;
668 if (shader->getLocalMatrix(&localM)) {
669 SkMatrix inverse;
670 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000671 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000672 }
673 }
674 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000675 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
676 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000677 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000678 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000679
680 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000681}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000682}
reed@google.comac10a2d2010-12-22 21:39:39 +0000683
684///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000685void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000686 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000687}
688
reed@google.comac10a2d2010-12-22 21:39:39 +0000689void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
690 CHECK_SHOULD_DRAW(draw);
691
bsalomon@google.com5782d712011-01-21 21:03:59 +0000692 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000693 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000694 if (!skPaint2GrPaintShader(this,
695 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000696 true,
twiz@google.com58071162012-07-18 21:41:50 +0000697 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000698 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000699 return;
700 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000701
702 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000703}
704
705// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000706static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000707 kPoints_GrPrimitiveType,
708 kLines_GrPrimitiveType,
709 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000710};
711
712void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000713 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000714 CHECK_SHOULD_DRAW(draw);
715
716 SkScalar width = paint.getStrokeWidth();
717 if (width < 0) {
718 return;
719 }
720
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000721 // we only handle hairlines and paints without path effects or mask filters,
722 // else we let the SkDraw call our drawPath()
723 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000724 draw.drawPoints(mode, count, pts, paint, true);
725 return;
726 }
727
bsalomon@google.com5782d712011-01-21 21:03:59 +0000728 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000729 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000730 if (!skPaint2GrPaintShader(this,
731 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000732 true,
twiz@google.com58071162012-07-18 21:41:50 +0000733 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000734 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000735 return;
736 }
737
bsalomon@google.com5782d712011-01-21 21:03:59 +0000738 fContext->drawVertices(grPaint,
739 gPointMode2PrimtiveType[mode],
740 count,
741 (GrPoint*)pts,
742 NULL,
743 NULL,
744 NULL,
745 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000746}
747
reed@google.comc9aa5872011-04-05 21:05:37 +0000748///////////////////////////////////////////////////////////////////////////////
749
reed@google.comac10a2d2010-12-22 21:39:39 +0000750void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
751 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000752 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000753 CHECK_SHOULD_DRAW(draw);
754
bungeman@google.com79bd8772011-07-18 15:34:08 +0000755 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000756 SkScalar width = paint.getStrokeWidth();
757
758 /*
759 We have special code for hairline strokes, miter-strokes, and fills.
760 Anything else we just call our path code.
761 */
762 bool usePath = doStroke && width > 0 &&
763 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000764 // another two reasons we might need to call drawPath...
765 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000766 usePath = true;
767 }
reed@google.com67db6642011-05-26 11:46:35 +0000768 // until we aa rotated rects...
769 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
770 usePath = true;
771 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000772 // small miter limit means right angles show bevel...
773 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
774 paint.getStrokeMiter() < SK_ScalarSqrt2)
775 {
776 usePath = true;
777 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000778 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000779 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
780 usePath = true;
781 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000782
783 if (usePath) {
784 SkPath path;
785 path.addRect(rect);
786 this->drawPath(draw, path, paint, NULL, true);
787 return;
788 }
789
790 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000791 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000792 if (!skPaint2GrPaintShader(this,
793 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000794 true,
twiz@google.com58071162012-07-18 21:41:50 +0000795 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000796 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000797 return;
798 }
reed@google.com20efde72011-05-09 17:00:02 +0000799 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000800}
801
reed@google.com69302852011-02-16 18:08:07 +0000802#include "SkMaskFilter.h"
803#include "SkBounder.h"
804
bsalomon@google.com85003222012-03-28 14:44:37 +0000805///////////////////////////////////////////////////////////////////////////////
806
807// helpers for applying mask filters
808namespace {
809
810GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000811 switch (fillType) {
812 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000813 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000814 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000815 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000816 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000817 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000819 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000820 default:
821 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000822 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000823 }
824}
825
bsalomon@google.com85003222012-03-28 14:44:37 +0000826// We prefer to blur small rect with small radius via CPU.
827#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
828#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
829inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
830 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
831 rect.height() <= MIN_GPU_BLUR_SIZE &&
832 radius <= MIN_GPU_BLUR_RADIUS) {
833 return true;
834 }
835 return false;
836}
837
838bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
839 SkMaskFilter* filter, const SkMatrix& matrix,
840 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000841 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000842#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000843 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000844#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000845 SkMaskFilter::BlurInfo info;
846 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000847 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000848 return false;
849 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000850 SkScalar radius = info.fIgnoreTransform ? info.fRadius
851 : matrix.mapRadius(info.fRadius);
852 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000853 if (radius <= 0) {
854 return false;
855 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000856
857 SkRect srcRect = path.getBounds();
858 if (shouldDrawBlurWithCPU(srcRect, radius)) {
859 return false;
860 }
861
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000862 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000863 float sigma3 = sigma * 3.0f;
864
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000865 SkRect clipRect;
866 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000867
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000868 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000869 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
870 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000871 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000872 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000873 SkIRect finalIRect;
874 finalRect.roundOut(&finalIRect);
875 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000876 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000877 }
878 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000879 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000880 }
881 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000882 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000883 GrTextureDesc desc;
884 desc.fFlags = kRenderTarget_GrTextureFlagBit;
885 desc.fWidth = SkScalarCeilToInt(srcRect.width());
886 desc.fHeight = SkScalarCeilToInt(srcRect.height());
887 // We actually only need A8, but it often isn't supported as a
888 // render target so default to RGBA_8888
889 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000890
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000891 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
892 desc.fConfig = kAlpha_8_GrPixelConfig;
893 }
894
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000895 GrAutoScratchTexture pathEntry(context, desc);
896 GrTexture* pathTexture = pathEntry.texture();
897 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000898 return false;
899 }
900 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000901 // Once this code moves into GrContext, this should be changed to use
902 // an AutoClipRestore.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000903 const GrClipData* oldClipData = context->getClip();
904
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000905 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000906
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000907 GrClip newClipStack(srcRect);
908 GrClipData newClipData;
909 newClipData.fClipStack = &newClipStack;
910 context->setClip(&newClipData);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000911
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000912 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000913 GrPaint tempPaint;
914 tempPaint.reset();
915
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000916 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000917 tempPaint.fAntiAlias = grp->fAntiAlias;
918 if (tempPaint.fAntiAlias) {
919 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
920 // blend coeff of zero requires dual source blending support in order
921 // to properly blend partially covered pixels. This means the AA
922 // code path may not be taken. So we use a dst blend coeff of ISA. We
923 // could special case AA draws to a dst surface with known alpha=0 to
924 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000925 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
926 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000927 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000928 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000929 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000930
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000931 // If we're doing a normal blur, we can clobber the pathTexture in the
932 // gaussianBlur. Otherwise, we need to save it for later compositing.
933 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000934 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
935 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000936
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000937 if (!isNormalBlur) {
938 GrPaint paint;
939 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +0000940 paint.textureSampler(0)->textureParams()->setClampNoFilter();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000941 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
942 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000943 // Blend pathTexture over blurTexture.
944 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000945 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
946 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000947 if (SkMaskFilter::kInner_BlurType == blurType) {
948 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000949 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
950 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000951 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
952 // solid: dst = src + dst - src * dst
953 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000954 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
955 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000956 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
957 // outer: dst = dst * (1 - src)
958 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000959 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
960 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000961 }
962 context->drawRect(paint, srcRect);
963 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000964 context->setRenderTarget(oldRenderTarget);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000965 context->setClip(oldClipData);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000966
bsalomon@google.come3d32162012-07-20 13:37:06 +0000967 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
968 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000969 }
970
971 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
972 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000973 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000974 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000975 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000976
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000977 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
978 -finalRect.fTop);
979 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
980 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000981 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000982 return true;
983}
984
bsalomon@google.com85003222012-03-28 14:44:37 +0000985bool drawWithMaskFilter(GrContext* context, const SkPath& path,
986 SkMaskFilter* filter, const SkMatrix& matrix,
987 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000988 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000989 SkMask srcM, dstM;
990
991 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000992 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000993 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000994 return false;
995 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000996 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000997
998 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
999 return false;
1000 }
1001 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +00001002 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001003
1004 if (clip.quickReject(dstM.fBounds)) {
1005 return false;
1006 }
1007 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1008 return false;
1009 }
1010
1011 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1012 // the current clip (and identity matrix) and grpaint settings
1013
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001014 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001015
bsalomon@google.come3d32162012-07-20 13:37:06 +00001016 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
1017 return false;
1018 }
1019
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001020 GrTextureDesc desc;
1021 desc.fWidth = dstM.fBounds.width();
1022 desc.fHeight = dstM.fBounds.height();
1023 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +00001024
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001025 GrAutoScratchTexture ast(context, desc);
1026 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001027
reed@google.com69302852011-02-16 18:08:07 +00001028 if (NULL == texture) {
1029 return false;
1030 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001031 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001032 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001033
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001034 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1035 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001036 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001037 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +00001038 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +00001039
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001040 GrRect d;
1041 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001042 GrIntToScalar(dstM.fBounds.fTop),
1043 GrIntToScalar(dstM.fBounds.fRight),
1044 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001045
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001046 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1047 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1048 -dstM.fBounds.fTop*SK_Scalar1);
1049 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001050 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001051 return true;
1052}
reed@google.com69302852011-02-16 18:08:07 +00001053
bsalomon@google.com85003222012-03-28 14:44:37 +00001054}
1055
1056///////////////////////////////////////////////////////////////////////////////
1057
reed@google.com0c219b62011-02-16 21:31:18 +00001058void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001059 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001060 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001061 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001062 CHECK_SHOULD_DRAW(draw);
1063
reed@google.comfe626382011-09-21 13:50:35 +00001064 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001065
bsalomon@google.com5782d712011-01-21 21:03:59 +00001066 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001067 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001068 if (!skPaint2GrPaintShader(this,
1069 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001070 true,
twiz@google.com58071162012-07-18 21:41:50 +00001071 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001072 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001073 return;
1074 }
1075
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001076 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1077 // if we can, we draw lots faster (raster device does this same test)
1078 SkScalar hairlineCoverage;
1079 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1080 doFill = false;
1081 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1082 grPaint.fCoverage);
1083 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001084
reed@google.comfe626382011-09-21 13:50:35 +00001085 // If we have a prematrix, apply it to the path, optimizing for the case
1086 // where the original path can in fact be modified in place (even though
1087 // its parameter type is const).
1088 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1089 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001090
1091 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001092 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001093
reed@google.come3445642011-02-16 23:20:39 +00001094 if (!pathIsMutable) {
1095 result = &tmpPath;
1096 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001097 }
reed@google.come3445642011-02-16 23:20:39 +00001098 // should I push prePathMatrix on our MV stack temporarily, instead
1099 // of applying it here? See SkDraw.cpp
1100 pathPtr->transform(*prePathMatrix, result);
1101 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001102 }
reed@google.com0c219b62011-02-16 21:31:18 +00001103 // at this point we're done with prePathMatrix
1104 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001105
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001106 if (paint.getPathEffect() ||
1107 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001108 // it is safe to use tmpPath here, even if we already used it for the
1109 // prepathmatrix, since getFillPath can take the same object for its
1110 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001111 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001112 pathPtr = &tmpPath;
1113 }
1114
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001115 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001116 // avoid possibly allocating a new path in transform if we can
1117 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1118
1119 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001120 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001121 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001122 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001123 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001124 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001125 &grPaint, pathFillType)) {
1126 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1127 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001128 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001129 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001130 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001131 }
reed@google.com69302852011-02-16 18:08:07 +00001132 return;
1133 }
reed@google.com69302852011-02-16 18:08:07 +00001134
bsalomon@google.com47059542012-06-06 20:51:20 +00001135 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001136
reed@google.com0c219b62011-02-16 21:31:18 +00001137 if (doFill) {
1138 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001139 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001140 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001141 break;
1142 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001143 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001144 break;
1145 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001146 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001147 break;
1148 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001149 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001150 break;
1151 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001152 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001153 return;
1154 }
1155 }
1156
reed@google.com07f3ee12011-05-16 17:21:57 +00001157 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001158}
1159
bsalomon@google.comfb309512011-11-30 14:13:48 +00001160namespace {
1161
1162inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1163 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1164 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1165 return tilesX * tilesY;
1166}
1167
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001168inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001169 const SkIRect* srcRectPtr,
1170 int maxTextureSize) {
1171 static const int kSmallTileSize = 1 << 10;
1172 if (maxTextureSize <= kSmallTileSize) {
1173 return maxTextureSize;
1174 }
1175
1176 size_t maxTexTotalTileSize;
1177 size_t smallTotalTileSize;
1178
1179 if (NULL == srcRectPtr) {
1180 int w = bitmap.width();
1181 int h = bitmap.height();
1182 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1183 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1184 } else {
1185 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1186 srcRectPtr->fTop,
1187 srcRectPtr->fRight,
1188 srcRectPtr->fBottom,
1189 maxTextureSize);
1190 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1191 srcRectPtr->fTop,
1192 srcRectPtr->fRight,
1193 srcRectPtr->fBottom,
1194 kSmallTileSize);
1195 }
1196 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1197 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1198
1199 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1200 return kSmallTileSize;
1201 } else {
1202 return maxTextureSize;
1203 }
1204}
1205}
1206
1207bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001208 const GrTextureParams& params,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001209 const SkIRect* srcRectPtr,
1210 int* tileSize) const {
1211 SkASSERT(NULL != tileSize);
1212
1213 // if bitmap is explictly texture backed then just use the texture
1214 if (NULL != bitmap.getTexture()) {
1215 return false;
1216 }
1217 // if it's larger than the max texture size, then we have no choice but
1218 // tiling
1219 const int maxTextureSize = fContext->getMaxTextureSize();
1220 if (bitmap.width() > maxTextureSize ||
1221 bitmap.height() > maxTextureSize) {
1222 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1223 return true;
1224 }
1225 // if we are going to have to draw the whole thing, then don't tile
1226 if (NULL == srcRectPtr) {
1227 return false;
1228 }
1229 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001230 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001231 return false;
1232 }
1233
1234 // At this point we know we could do the draw by uploading the entire bitmap
1235 // as a texture. However, if the texture would be large compared to the
1236 // cache size and we don't require most of it for this draw then tile to
1237 // reduce the amount of upload and cache spill.
1238
1239 // assumption here is that sw bitmap size is a good proxy for its size as
1240 // a texture
1241 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001242 size_t cacheSize;
1243 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001244 if (bmpSize < cacheSize / 2) {
1245 return false;
1246 }
1247
1248 SkFixed fracUsed =
1249 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1250 (srcRectPtr->height() << 16) / bitmap.height());
1251 if (fracUsed <= SK_FixedHalf) {
1252 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1253 return true;
1254 } else {
1255 return false;
1256 }
1257}
1258
reed@google.comac10a2d2010-12-22 21:39:39 +00001259void SkGpuDevice::drawBitmap(const SkDraw& draw,
1260 const SkBitmap& bitmap,
1261 const SkIRect* srcRectPtr,
1262 const SkMatrix& m,
1263 const SkPaint& paint) {
1264 CHECK_SHOULD_DRAW(draw);
1265
1266 SkIRect srcRect;
1267 if (NULL == srcRectPtr) {
1268 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1269 } else {
1270 srcRect = *srcRectPtr;
1271 }
1272
junov@google.comd935cfb2011-06-27 20:48:23 +00001273 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001274 // Convert the bitmap to a shader so that the rect can be drawn
1275 // through drawRect, which supports mask filters.
1276 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001277 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001278 if (srcRectPtr) {
1279 if (!bitmap.extractSubset(&tmp, srcRect)) {
1280 return; // extraction failed
1281 }
1282 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001283 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001284 }
1285 SkPaint paintWithTexture(paint);
1286 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1287 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001288 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001289 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001290
junov@google.com1d329782011-07-28 20:10:09 +00001291 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001292 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001293 // also affect the behavior of the mask filter.
1294 SkMatrix drawMatrix;
1295 drawMatrix.setConcat(*draw.fMatrix, m);
1296 SkDraw transformedDraw(draw);
1297 transformedDraw.fMatrix = &drawMatrix;
1298
1299 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1300
junov@google.comd935cfb2011-06-27 20:48:23 +00001301 return;
1302 }
1303
bsalomon@google.com5782d712011-01-21 21:03:59 +00001304 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001305 SkAutoCachedTexture colorLutTexture;
1306 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001307 return;
1308 }
bsalomon@google.comb8670992012-07-25 21:27:09 +00001309 GrTextureParams* params = grPaint.textureSampler(kBitmapTextureIdx)->textureParams();
1310 params->setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001311
bsalomon@google.comfb309512011-11-30 14:13:48 +00001312 int tileSize;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001313 if (!this->shouldTileBitmap(bitmap, *params, srcRectPtr, &tileSize)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001314 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001315 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 return;
1317 }
1318
1319 // undo the translate done by SkCanvas
1320 int DX = SkMax32(0, srcRect.fLeft);
1321 int DY = SkMax32(0, srcRect.fTop);
1322 // compute clip bounds in local coordinates
1323 SkIRect clipRect;
1324 {
1325 SkRect r;
1326 r.set(draw.fClip->getBounds());
1327 SkMatrix matrix, inverse;
1328 matrix.setConcat(*draw.fMatrix, m);
1329 if (!matrix.invert(&inverse)) {
1330 return;
1331 }
1332 inverse.mapRect(&r);
1333 r.roundOut(&clipRect);
1334 // apply the canvas' translate to our local clip
1335 clipRect.offset(DX, DY);
1336 }
1337
bsalomon@google.comfb309512011-11-30 14:13:48 +00001338 int nx = bitmap.width() / tileSize;
1339 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001340 for (int x = 0; x <= nx; x++) {
1341 for (int y = 0; y <= ny; y++) {
1342 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001343 tileR.set(x * tileSize, y * tileSize,
1344 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001345 if (!SkIRect::Intersects(tileR, clipRect)) {
1346 continue;
1347 }
1348
1349 SkIRect srcR = tileR;
1350 if (!srcR.intersect(srcRect)) {
1351 continue;
1352 }
1353
1354 SkBitmap tmpB;
1355 if (bitmap.extractSubset(&tmpB, tileR)) {
1356 // now offset it to make it "local" to our tmp bitmap
1357 srcR.offset(-tileR.fLeft, -tileR.fTop);
1358
1359 SkMatrix tmpM(m);
1360 {
1361 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1362 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1363 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1364 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001365 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001366 }
1367 }
1368 }
1369}
1370
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001371namespace {
1372
1373bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1374 // detect pixel disalignment
1375 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1376 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1377 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1378 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1379 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1380 COLOR_BLEED_TOLERANCE &&
1381 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1382 COLOR_BLEED_TOLERANCE) {
1383 return true;
1384 }
1385 return false;
1386}
1387
1388bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1389 const SkMatrix& m) {
1390 // Only gets called if hasAlignedSamples returned false.
1391 // So we can assume that sampling is axis aligned but not texel aligned.
1392 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1393 SkRect innerSrcRect(srcRect), innerTransformedRect,
1394 outerTransformedRect(transformedRect);
1395 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1396 m.mapRect(&innerTransformedRect, innerSrcRect);
1397
1398 // The gap between outerTransformedRect and innerTransformedRect
1399 // represents the projection of the source border area, which is
1400 // problematic for color bleeding. We must check whether any
1401 // destination pixels sample the border area.
1402 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1403 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1404 SkIRect outer, inner;
1405 outerTransformedRect.round(&outer);
1406 innerTransformedRect.round(&inner);
1407 // If the inner and outer rects round to the same result, it means the
1408 // border does not overlap any pixel centers. Yay!
1409 return inner != outer;
1410}
1411
1412} // unnamed namespace
1413
reed@google.comac10a2d2010-12-22 21:39:39 +00001414/*
1415 * This is called by drawBitmap(), which has to handle images that may be too
1416 * large to be represented by a single texture.
1417 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001418 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1419 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001420 */
1421void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1422 const SkBitmap& bitmap,
1423 const SkIRect& srcRect,
1424 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001425 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001426 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1427 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001428
reed@google.com9c49bc32011-07-07 13:42:37 +00001429 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001430 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001431 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001432 return;
1433 }
1434
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001435 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001436
bsalomon@google.comb8670992012-07-25 21:27:09 +00001437 sampler->textureParams()->setClamp();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001438 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001439
1440 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001441 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001442 if (NULL == texture) {
1443 return;
1444 }
1445
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001446 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1447 (GrSingleTextureEffect, (texture)))->unref();
reed@google.com46799cd2011-02-22 20:56:26 +00001448
reed@google.com20efde72011-05-09 17:00:02 +00001449 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1450 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001451 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001452 float wInv = 1.f / bitmap.width();
1453 float hInv = 1.f / bitmap.height();
1454 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1455 SkFloatToScalar(srcRect.fTop * hInv),
1456 SkFloatToScalar(srcRect.fRight * wInv),
1457 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001458
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001459 bool needsTextureDomain = false;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001460 if (sampler->textureParams()->isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001461 // Need texture domain if drawing a sub rect.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001462 needsTextureDomain = srcRect.width() < bitmap.width() || srcRect.height() < bitmap.height();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001463 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1464 // sampling is axis-aligned
1465 GrRect floatSrcRect, transformedRect;
1466 floatSrcRect.set(srcRect);
1467 SkMatrix srcToDeviceMatrix(m);
1468 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1469 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1470
1471 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1472 // Samples are texel-aligned, so filtering is futile
bsalomon@google.comb8670992012-07-25 21:27:09 +00001473 sampler->textureParams()->setBilerp(false);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001474 needsTextureDomain = false;
1475 } else {
1476 needsTextureDomain = needsTextureDomain &&
1477 mayColorBleed(floatSrcRect, transformedRect, m);
1478 }
1479 }
1480 }
1481
1482 GrRect textureDomain = GrRect::MakeEmpty();
1483
1484 if (needsTextureDomain) {
1485 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001486 GrScalar left, top, right, bottom;
1487 if (srcRect.width() > 1) {
1488 GrScalar border = GR_ScalarHalf / bitmap.width();
1489 left = paintRect.left() + border;
1490 right = paintRect.right() - border;
1491 } else {
1492 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1493 }
1494 if (srcRect.height() > 1) {
1495 GrScalar border = GR_ScalarHalf / bitmap.height();
1496 top = paintRect.top() + border;
1497 bottom = paintRect.bottom() - border;
1498 } else {
1499 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1500 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001501 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001502 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1503 (texture,
1504 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001505 }
1506
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001507 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001508}
1509
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001510namespace {
1511
1512void apply_custom_stage(GrContext* context,
1513 GrTexture* srcTexture,
1514 GrTexture* dstTexture,
1515 const GrRect& rect,
1516 GrCustomStage* stage) {
1517 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001518 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001519 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001520 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001521
1522 GrMatrix sampleM;
1523 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1524 GrPaint paint;
1525 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001526 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001527 paint.textureSampler(0)->reset(sampleM);
1528 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001529 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001530}
1531
1532};
1533
reed@google.com8926b162012-03-23 15:36:36 +00001534static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1535 SkImageFilter* filter, const GrRect& rect) {
1536 GrAssert(filter);
1537
1538 SkSize blurSize;
1539 SkISize radius;
1540
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001541 GrTextureDesc desc;
1542 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1543 desc.fWidth = SkScalarCeilToInt(rect.width());
1544 desc.fHeight = SkScalarCeilToInt(rect.height());
1545 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001546 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001547
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001548 if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001549 GrAutoScratchTexture dst(context, desc);
1550 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1551 texture = dst.detach();
1552 stage->unref();
1553 } else if (filter->asABlur(&blurSize)) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001554 texture = context->gaussianBlur(texture, false, rect,
reed@google.com8926b162012-03-23 15:36:36 +00001555 blurSize.width(),
1556 blurSize.height());
reed@google.com8926b162012-03-23 15:36:36 +00001557 } else if (filter->asADilate(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001558 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001559 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001560 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001561 } else if (filter->asAnErode(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001562 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001563 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001564 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001565 }
1566 return texture;
1567}
1568
reed@google.comac10a2d2010-12-22 21:39:39 +00001569void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1570 int left, int top, const SkPaint& paint) {
1571 CHECK_SHOULD_DRAW(draw);
1572
reed@google.com8926b162012-03-23 15:36:36 +00001573 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001574 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1575 return;
1576 }
1577
reed@google.com76dd2772012-01-05 21:15:07 +00001578 int w = bitmap.width();
1579 int h = bitmap.height();
1580
bsalomon@google.com5782d712011-01-21 21:03:59 +00001581 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001582 SkAutoCachedTexture colorLutTexture;
1583 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001584 return;
1585 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001586
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001587 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001588
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001589 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001590
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001591 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001592 sampler->reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001593 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001594 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1595 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001596
reed@google.com8926b162012-03-23 15:36:36 +00001597 SkImageFilter* filter = paint.getImageFilter();
1598 if (NULL != filter) {
1599 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001600 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001601 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001602 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1603 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001604 texture = filteredTexture;
1605 filteredTexture->unref();
1606 }
reed@google.com76dd2772012-01-05 21:15:07 +00001607 }
reed@google.com8926b162012-03-23 15:36:36 +00001608
bsalomon@google.com5782d712011-01-21 21:03:59 +00001609 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001610 GrRect::MakeXYWH(GrIntToScalar(left),
1611 GrIntToScalar(top),
1612 GrIntToScalar(w),
1613 GrIntToScalar(h)),
1614 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1615 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001616}
1617
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001618void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001619 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001620 // clear of the source device must occur before CHECK_SHOULD_DRAW
1621 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1622 if (dev->fNeedClear) {
1623 // TODO: could check here whether we really need to draw at all
1624 dev->clear(0x0);
1625 }
1626
reed@google.comac10a2d2010-12-22 21:39:39 +00001627 CHECK_SHOULD_DRAW(draw);
1628
bsalomon@google.com5782d712011-01-21 21:03:59 +00001629 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001630 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001631 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001632 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001633 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001634 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001635
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001636 GrTexture* devTex = grPaint.getTexture(0);
1637 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001638
reed@google.com8926b162012-03-23 15:36:36 +00001639 SkImageFilter* filter = paint.getImageFilter();
tomhudson@google.com2683a412012-07-20 19:15:06 +00001640 grPaint.textureSampler(kBitmapTextureIdx)->reset();
reed@google.com8926b162012-03-23 15:36:36 +00001641 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001642 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1643 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001644 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1645 rect);
1646 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001647 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1648 (GrSingleTextureEffect, (filteredTexture)))->unref();
tomhudson@google.com2683a412012-07-20 19:15:06 +00001649 grPaint.setTexture(kBitmapTextureIdx, NULL);
reed@google.com8926b162012-03-23 15:36:36 +00001650 devTex = filteredTexture;
1651 filteredTexture->unref();
1652 }
1653 }
1654
bsalomon@google.com5782d712011-01-21 21:03:59 +00001655 const SkBitmap& bm = dev->accessBitmap(false);
1656 int w = bm.width();
1657 int h = bm.height();
1658
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001659 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001660 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1661 GrIntToScalar(y),
1662 GrIntToScalar(w),
1663 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001664
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001665 // The device being drawn may not fill up its texture (saveLayer uses
1666 // the approximate ).
1667 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1668 GR_Scalar1 * h / devTex->height());
1669
1670 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001671}
1672
reed@google.com8926b162012-03-23 15:36:36 +00001673bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001674 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001675 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001676
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001677 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001678 !filter->asABlur(&size) &&
1679 !filter->asADilate(&radius) &&
1680 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001681 return false;
1682 }
reed@google.com8926b162012-03-23 15:36:36 +00001683 return true;
1684}
1685
1686bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1687 const SkMatrix& ctm,
1688 SkBitmap* result, SkIPoint* offset) {
1689 // want explicitly our impl, so guard against a subclass of us overriding it
1690 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001691 return false;
1692 }
reed@google.com8926b162012-03-23 15:36:36 +00001693
1694 SkAutoLockPixels alp(src, !src.getTexture());
1695 if (!src.getTexture() && !src.readyToDraw()) {
1696 return false;
1697 }
1698
1699 GrPaint paint;
1700 paint.reset();
1701
1702 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1703
1704 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001705 SkAutoCachedTexture act(this, src, sampler->textureParams(), &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001706
1707 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001708 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1709 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001710 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1711 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001712 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1713 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001714 resultTexture->unref();
1715 }
reed@google.com76dd2772012-01-05 21:15:07 +00001716 return true;
1717}
1718
reed@google.comac10a2d2010-12-22 21:39:39 +00001719///////////////////////////////////////////////////////////////////////////////
1720
1721// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001722static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001723 kTriangles_GrPrimitiveType,
1724 kTriangleStrip_GrPrimitiveType,
1725 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001726};
1727
1728void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1729 int vertexCount, const SkPoint vertices[],
1730 const SkPoint texs[], const SkColor colors[],
1731 SkXfermode* xmode,
1732 const uint16_t indices[], int indexCount,
1733 const SkPaint& paint) {
1734 CHECK_SHOULD_DRAW(draw);
1735
bsalomon@google.com5782d712011-01-21 21:03:59 +00001736 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001737 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001738 // we ignore the shader if texs is null.
1739 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001740 if (!skPaint2GrPaintNoShader(this,
1741 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001742 false,
1743 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001744 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001745 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001746 return;
1747 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001748 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001749 if (!skPaint2GrPaintShader(this,
1750 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001751 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001752 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001753 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001754 return;
1755 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001757
1758 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001759 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001760 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1761#if 0
1762 return
1763#endif
1764 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001765 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001766
bsalomon@google.com498776a2011-08-16 19:20:44 +00001767 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1768 if (NULL != colors) {
1769 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001770 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001771 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001772 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001773 }
1774 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001775 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001776 fContext->drawVertices(grPaint,
1777 gVertexMode2PrimitiveType[vmode],
1778 vertexCount,
1779 (GrPoint*) vertices,
1780 (GrPoint*) texs,
1781 colors,
1782 indices,
1783 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001784}
1785
1786///////////////////////////////////////////////////////////////////////////////
1787
1788static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001789 GrFontScaler* scaler = (GrFontScaler*)data;
1790 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001791}
1792
1793static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1794 void* auxData;
1795 GrFontScaler* scaler = NULL;
1796 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1797 scaler = (GrFontScaler*)auxData;
1798 }
1799 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001800 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001801 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1802 }
1803 return scaler;
1804}
1805
1806static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1807 SkFixed fx, SkFixed fy,
1808 const SkGlyph& glyph) {
1809 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1810
bungeman@google.com15865a72012-01-11 16:28:04 +00001811 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001812
1813 if (NULL == procs->fFontScaler) {
1814 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1815 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001816
bungeman@google.com15865a72012-01-11 16:28:04 +00001817 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1818 glyph.getSubXFixed(),
1819 glyph.getSubYFixed()),
1820 SkFixedFloorToFixed(fx),
1821 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001822 procs->fFontScaler);
1823}
1824
bsalomon@google.com5782d712011-01-21 21:03:59 +00001825SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001826
1827 // deferred allocation
1828 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001829 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001830 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1831 fDrawProcs->fContext = fContext;
1832 }
1833
1834 // init our (and GL's) state
1835 fDrawProcs->fTextContext = context;
1836 fDrawProcs->fFontScaler = NULL;
1837 return fDrawProcs;
1838}
1839
1840void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1841 size_t byteLength, SkScalar x, SkScalar y,
1842 const SkPaint& paint) {
1843 CHECK_SHOULD_DRAW(draw);
1844
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001845 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001846 // this guy will just call our drawPath()
1847 draw.drawText((const char*)text, byteLength, x, y, paint);
1848 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001849 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001850
1851 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001852 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001853 if (!skPaint2GrPaintShader(this,
1854 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001855 true,
twiz@google.com58071162012-07-18 21:41:50 +00001856 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001857 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001858 return;
1859 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001860 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1861 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001862 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1863 }
1864}
1865
1866void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1867 size_t byteLength, const SkScalar pos[],
1868 SkScalar constY, int scalarsPerPos,
1869 const SkPaint& paint) {
1870 CHECK_SHOULD_DRAW(draw);
1871
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001872 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001873 // this guy will just call our drawPath()
1874 draw.drawPosText((const char*)text, byteLength, pos, constY,
1875 scalarsPerPos, paint);
1876 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001877 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001878
1879 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001880 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001881 if (!skPaint2GrPaintShader(this,
1882 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001883 true,
twiz@google.com58071162012-07-18 21:41:50 +00001884 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001885 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001886 return;
1887 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001888 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1889 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001890 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1891 scalarsPerPos, paint);
1892 }
1893}
1894
1895void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1896 size_t len, const SkPath& path,
1897 const SkMatrix* m, const SkPaint& paint) {
1898 CHECK_SHOULD_DRAW(draw);
1899
1900 SkASSERT(draw.fDevice == this);
1901 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1902}
1903
1904///////////////////////////////////////////////////////////////////////////////
1905
reed@google.comf67e4cf2011-03-15 20:56:58 +00001906bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1907 if (!paint.isLCDRenderText()) {
1908 // we're cool with the paint as is
1909 return false;
1910 }
1911
1912 if (paint.getShader() ||
1913 paint.getXfermode() || // unless its srcover
1914 paint.getMaskFilter() ||
1915 paint.getRasterizer() ||
1916 paint.getColorFilter() ||
1917 paint.getPathEffect() ||
1918 paint.isFakeBoldText() ||
1919 paint.getStyle() != SkPaint::kFill_Style) {
1920 // turn off lcd
1921 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1922 flags->fHinting = paint.getHinting();
1923 return true;
1924 }
1925 // we're cool with the paint as is
1926 return false;
1927}
1928
reed@google.com75d939b2011-12-07 15:07:23 +00001929void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001930 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001931 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001932}
1933
reed@google.comf67e4cf2011-03-15 20:56:58 +00001934///////////////////////////////////////////////////////////////////////////////
1935
bsalomon@google.comfb309512011-11-30 14:13:48 +00001936bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001937 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001938 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001939 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001940
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001941 GrTextureDesc desc;
1942 desc.fWidth = bitmap.width();
1943 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001944 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001945 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001946
bsalomon@google.comb8670992012-07-25 21:27:09 +00001947 return this->context()->isTextureInCache(desc, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001948}
1949
1950
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001951SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1952 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001953 bool isOpaque,
1954 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001955 GrTextureDesc desc;
1956 desc.fConfig = fRenderTarget->config();
1957 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1958 desc.fWidth = width;
1959 desc.fHeight = height;
1960 desc.fSampleCnt = fRenderTarget->numSamples();
1961
1962 GrContext::TextureCacheEntry cacheEntry;
1963 GrTexture* texture;
1964 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001965 // Skia's convention is to only clear a device if it is non-opaque.
1966 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001967
1968#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1969 // layers are never draw in repeat modes, so we can request an approx
1970 // match and ignore any padding.
1971 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1972 GrContext::kApprox_ScratchTexMatch :
1973 GrContext::kExact_ScratchTexMatch;
1974 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1975 texture = cacheEntry.texture();
1976#else
1977 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1978 texture = tunref.get();
1979#endif
1980 if (texture) {
1981 return SkNEW_ARGS(SkGpuDevice,(fContext,
1982 texture,
1983 cacheEntry,
1984 needClear));
1985 } else {
1986 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1987 width, height);
1988 return NULL;
1989 }
1990}
1991
1992SkGpuDevice::SkGpuDevice(GrContext* context,
1993 GrTexture* texture,
1994 TexCache cacheEntry,
1995 bool needClear)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001996 : SkDevice(make_bitmap(context, texture->asRenderTarget()))
1997 , fClipStack(NULL) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001998 GrAssert(texture && texture->asRenderTarget());
1999 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
2000 this->initFromRenderTarget(context, texture->asRenderTarget());
2001 fCache = cacheEntry;
2002 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00002003}
2004