blob: f290239db1c777adb83f0b8e569972ba25d46c04 [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();
356}
357
358void SkGpuDevice::onDetachFromCanvas() {
359 INHERITED::onDetachFromCanvas();
360
361 fClipStack = NULL;
362}
363
robertphillips@google.com607fe072012-07-24 13:54:00 +0000364#ifdef SK_DEBUG
365static void check_bounds(const SkClipStack& clipStack,
366 const SkRegion& clipRegion,
367 const SkIPoint& origin,
368 int renderTargetWidth,
369 int renderTargetHeight) {
370
371 SkIRect bound;
372 SkClipStack::BoundsType boundType;
373 SkRect temp;
374
375 bound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
376
377 clipStack.getBounds(&temp, &boundType);
378 if (SkClipStack::kNormal_BoundsType == boundType) {
379 SkIRect temp2;
380
381 temp.roundOut(&temp2);
382
383 temp2.offset(-origin.fX, -origin.fY);
384
385 if (!bound.intersect(temp2)) {
386 bound.setEmpty();
387 }
388 }
389
390// GrAssert(bound.contains(clipRegion.getBounds()));
391}
392#endif
393
reed@google.comac10a2d2010-12-22 21:39:39 +0000394///////////////////////////////////////////////////////////////////////////////
395
396static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000397 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000398 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000399 const SkIPoint& origin,
400 int renderTargetWidth, int renderTargetHeight) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000401 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000402
403 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000404 iter.reset(clipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000405
406#ifdef SK_DEBUG
407 check_bounds(clipStack, clipRegion, origin,
408 renderTargetWidth, renderTargetHeight);
409#endif
410
411 SkRect bounds;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000412 bool isIntersectionOfRects = false;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000413 clipStack.getConservativeBounds(-origin.fX,
414 -origin.fY,
415 renderTargetWidth,
416 renderTargetHeight,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000417 &bounds,
418 &isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000419
reed@google.com6f8f2922011-03-04 22:27:10 +0000420 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000421 bounds);
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000422
423 GrAssert(grc.isRect() == isIntersectionOfRects);
424
bsalomon@google.comd302f142011-03-03 13:54:13 +0000425 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000426}
427
428// call this ever each draw call, to ensure that the context reflects our state,
429// and not the state from some other canvas/device
430void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000431 GrAssert(NULL != fClipStack);
432
reed@google.comac10a2d2010-12-22 21:39:39 +0000433 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000434 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000435
436 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000437 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000438
bsalomon@google.comd302f142011-03-03 13:54:13 +0000439 convert_matrixclip(fContext, *draw.fMatrix,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000440 *fClipStack, *draw.fClip, this->getOrigin(),
441 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000442 fNeedPrepareRenderTarget = false;
443 }
444}
445
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000446void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
447 const SkClipStack& clipStack) {
448 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
449 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000450 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000451}
452
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000453void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
454
455 GrAssert(NULL != fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000456
reed@google.comac10a2d2010-12-22 21:39:39 +0000457 fContext->setRenderTarget(fRenderTarget);
458
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000459 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000460
robertphillips@google.com607fe072012-07-24 13:54:00 +0000461 convert_matrixclip(fContext, matrix, *fClipStack, clip, this->getOrigin(),
462 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000463
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000464 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000465}
466
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000467SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000468 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000469 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000470}
471
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000472bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000473 if (NULL != fTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000474 // FIXME: cannot use GrSingleTextureEffect here: fails
475 // assert in line 1617: null != devTex; generalizing GrPaint::getTexture()
476 // to grab textures off of GrCustomStages breaks gms in various ways -
477 // particularly since table color filter requires multiple textures
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000478 paint->setTexture(kBitmapTextureIdx, fTexture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000479 //paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
480 //SkNEW_ARGS(GrSingleTextureEffect, (fTexture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000481 return true;
482 }
483 return false;
484}
485
486///////////////////////////////////////////////////////////////////////////////
487
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000488SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
489SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
490SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
491SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
492SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
493 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000494SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
495 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000496SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
497SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000498
bsalomon@google.com84405e02012-03-05 19:57:21 +0000499namespace {
500
501// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
502// justAlpha indicates that skPaint's alpha should be used rather than the color
503// Callers may subsequently modify the GrPaint. Setting constantColor indicates
504// that the final paint will draw the same color at every pixel. This allows
505// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000506// color once while converting to GrPaint and then ignored.
507inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
508 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000509 bool justAlpha,
510 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000511 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000512 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000513
514 grPaint->fDither = skPaint.isDither();
515 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000516 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000517
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000518 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
519 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000520
521 SkXfermode* mode = skPaint.getXfermode();
522 if (mode) {
523 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000524 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000525#if 0
526 return false;
527#endif
528 }
529 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000530 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
531 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
532
bsalomon@google.com5782d712011-01-21 21:03:59 +0000533 if (justAlpha) {
534 uint8_t alpha = skPaint.getAlpha();
535 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000536 // justAlpha is currently set to true only if there is a texture,
537 // so constantColor should not also be true.
538 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000539 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000540 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000541 GrAssert(NULL == grPaint->getTexture(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000542 }
Scroggo97c88c22011-05-11 14:05:25 +0000543 SkColorFilter* colorFilter = skPaint.getColorFilter();
544 SkColor color;
545 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000546 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000547 SkBitmap colorTransformTable;
Scroggo97c88c22011-05-11 14:05:25 +0000548 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000549 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000550 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000551 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000552 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000553 } else {
554 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000555 grPaint->fColor = SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000556 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000557 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000558 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
559 grPaint->fColorMatrixEnabled = true;
560 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
561 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
twiz@google.com58071162012-07-18 21:41:50 +0000562 } else if (colorFilter != NULL && colorFilter->asComponentTable(
563 &colorTransformTable)) {
564 grPaint->resetColorFilter();
565
566 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000567 GrTexture* texture = act->set(dev, colorTransformTable, colorSampler->textureParams());
twiz@google.com58071162012-07-18 21:41:50 +0000568
bsalomon@google.comb8670992012-07-25 21:27:09 +0000569 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000570 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000571 } else {
572 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000573 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000574 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000575}
576
bsalomon@google.com84405e02012-03-05 19:57:21 +0000577// This function is similar to skPaint2GrPaintNoShader but also converts
578// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
579// be used is set on grPaint and returned in param act. constantColor has the
580// same meaning as in skPaint2GrPaintNoShader.
581inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
582 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000583 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000584 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000585 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000586 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000587 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000588 return skPaint2GrPaintNoShader(dev,
589 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000590 false,
591 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000592 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000593 grPaint);
twiz@google.com58071162012-07-18 21:41:50 +0000594 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
595 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000596 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000597 }
598
rileya@google.com91f319c2012-07-25 17:18:31 +0000599 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
600 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
601
602 if (NULL != stage) {
603 sampler->setCustomStage(stage)->unref();
604 SkMatrix localM;
605 if (shader->getLocalMatrix(&localM)) {
606 SkMatrix inverse;
607 if (localM.invert(&inverse)) {
608 sampler->matrix()->preConcat(inverse);
609 }
610 }
611 return true;
612 }
613
reed@google.comac10a2d2010-12-22 21:39:39 +0000614 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000615 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000616 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000617 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000618 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000619
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000620 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000621 SkShader::GradientInfo info;
622 SkColor color;
623
624 info.fColors = &color;
625 info.fColorOffsets = NULL;
626 info.fColorCount = 1;
627 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
628 SkPaint copy(skPaint);
629 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000630 // modulate the paint alpha by the shader's solid color alpha
631 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
632 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000633 return skPaint2GrPaintNoShader(dev,
634 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000635 false,
636 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000637 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000638 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000639 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000640 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000641 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000642
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000643 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.comb8670992012-07-25 21:27:09 +0000644 sampler->textureParams()->reset(tileModes, skPaint.isFilterBitmap());
645 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, sampler->textureParams());
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000646
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000647 if (NULL == texture) {
648 SkDebugf("Couldn't convert bitmap to texture.\n");
649 return false;
650 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000651
rileya@google.com91f319c2012-07-25 17:18:31 +0000652 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000653
reed@google.comac10a2d2010-12-22 21:39:39 +0000654 // since our texture coords will be in local space, we wack the texture
655 // matrix to map them back into 0...1 before we load it
656 SkMatrix localM;
657 if (shader->getLocalMatrix(&localM)) {
658 SkMatrix inverse;
659 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000660 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000661 }
662 }
663 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000664 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
665 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000666 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000667 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000668
669 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000670}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000671}
reed@google.comac10a2d2010-12-22 21:39:39 +0000672
673///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000674void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000675 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000676}
677
reed@google.comac10a2d2010-12-22 21:39:39 +0000678void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
679 CHECK_SHOULD_DRAW(draw);
680
bsalomon@google.com5782d712011-01-21 21:03:59 +0000681 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000682 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000683 if (!skPaint2GrPaintShader(this,
684 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000685 true,
twiz@google.com58071162012-07-18 21:41:50 +0000686 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000687 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000688 return;
689 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000690
691 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000692}
693
694// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000695static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000696 kPoints_GrPrimitiveType,
697 kLines_GrPrimitiveType,
698 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000699};
700
701void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000702 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000703 CHECK_SHOULD_DRAW(draw);
704
705 SkScalar width = paint.getStrokeWidth();
706 if (width < 0) {
707 return;
708 }
709
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000710 // we only handle hairlines and paints without path effects or mask filters,
711 // else we let the SkDraw call our drawPath()
712 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000713 draw.drawPoints(mode, count, pts, paint, true);
714 return;
715 }
716
bsalomon@google.com5782d712011-01-21 21:03:59 +0000717 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000718 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000719 if (!skPaint2GrPaintShader(this,
720 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000721 true,
twiz@google.com58071162012-07-18 21:41:50 +0000722 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000723 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000724 return;
725 }
726
bsalomon@google.com5782d712011-01-21 21:03:59 +0000727 fContext->drawVertices(grPaint,
728 gPointMode2PrimtiveType[mode],
729 count,
730 (GrPoint*)pts,
731 NULL,
732 NULL,
733 NULL,
734 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000735}
736
reed@google.comc9aa5872011-04-05 21:05:37 +0000737///////////////////////////////////////////////////////////////////////////////
738
reed@google.comac10a2d2010-12-22 21:39:39 +0000739void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
740 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000741 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000742 CHECK_SHOULD_DRAW(draw);
743
bungeman@google.com79bd8772011-07-18 15:34:08 +0000744 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000745 SkScalar width = paint.getStrokeWidth();
746
747 /*
748 We have special code for hairline strokes, miter-strokes, and fills.
749 Anything else we just call our path code.
750 */
751 bool usePath = doStroke && width > 0 &&
752 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000753 // another two reasons we might need to call drawPath...
754 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000755 usePath = true;
756 }
reed@google.com67db6642011-05-26 11:46:35 +0000757 // until we aa rotated rects...
758 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
759 usePath = true;
760 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000761 // small miter limit means right angles show bevel...
762 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
763 paint.getStrokeMiter() < SK_ScalarSqrt2)
764 {
765 usePath = true;
766 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000767 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000768 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
769 usePath = true;
770 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000771
772 if (usePath) {
773 SkPath path;
774 path.addRect(rect);
775 this->drawPath(draw, path, paint, NULL, true);
776 return;
777 }
778
779 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000780 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000781 if (!skPaint2GrPaintShader(this,
782 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000783 true,
twiz@google.com58071162012-07-18 21:41:50 +0000784 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000785 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000786 return;
787 }
reed@google.com20efde72011-05-09 17:00:02 +0000788 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000789}
790
reed@google.com69302852011-02-16 18:08:07 +0000791#include "SkMaskFilter.h"
792#include "SkBounder.h"
793
bsalomon@google.com85003222012-03-28 14:44:37 +0000794///////////////////////////////////////////////////////////////////////////////
795
796// helpers for applying mask filters
797namespace {
798
799GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000800 switch (fillType) {
801 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000802 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000803 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000804 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000805 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000806 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000807 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000808 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809 default:
810 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000811 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000812 }
813}
814
bsalomon@google.com85003222012-03-28 14:44:37 +0000815// We prefer to blur small rect with small radius via CPU.
816#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
817#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
818inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
819 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
820 rect.height() <= MIN_GPU_BLUR_SIZE &&
821 radius <= MIN_GPU_BLUR_RADIUS) {
822 return true;
823 }
824 return false;
825}
826
827bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
828 SkMaskFilter* filter, const SkMatrix& matrix,
829 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000830 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000831#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000832 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000833#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000834 SkMaskFilter::BlurInfo info;
835 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000836 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000837 return false;
838 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000839 SkScalar radius = info.fIgnoreTransform ? info.fRadius
840 : matrix.mapRadius(info.fRadius);
841 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000842 if (radius <= 0) {
843 return false;
844 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000845
846 SkRect srcRect = path.getBounds();
847 if (shouldDrawBlurWithCPU(srcRect, radius)) {
848 return false;
849 }
850
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000851 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000852 float sigma3 = sigma * 3.0f;
853
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000854 SkRect clipRect;
855 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000856
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000857 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000858 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
859 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000860 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000861 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000862 SkIRect finalIRect;
863 finalRect.roundOut(&finalIRect);
864 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000865 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000866 }
867 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000868 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000869 }
870 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000871 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000872 GrTextureDesc desc;
873 desc.fFlags = kRenderTarget_GrTextureFlagBit;
874 desc.fWidth = SkScalarCeilToInt(srcRect.width());
875 desc.fHeight = SkScalarCeilToInt(srcRect.height());
876 // We actually only need A8, but it often isn't supported as a
877 // render target so default to RGBA_8888
878 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000879
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000880 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
881 desc.fConfig = kAlpha_8_GrPixelConfig;
882 }
883
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000884 GrAutoScratchTexture pathEntry(context, desc);
885 GrTexture* pathTexture = pathEntry.texture();
886 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000887 return false;
888 }
889 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000890 // Once this code moves into GrContext, this should be changed to use
891 // an AutoClipRestore.
892 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000893 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000894
895 GrClip newClip(srcRect);
896 context->setClip(newClip);
897
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000898 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000899 GrPaint tempPaint;
900 tempPaint.reset();
901
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000902 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000903 tempPaint.fAntiAlias = grp->fAntiAlias;
904 if (tempPaint.fAntiAlias) {
905 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
906 // blend coeff of zero requires dual source blending support in order
907 // to properly blend partially covered pixels. This means the AA
908 // code path may not be taken. So we use a dst blend coeff of ISA. We
909 // could special case AA draws to a dst surface with known alpha=0 to
910 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000911 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
912 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000913 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000914 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000915 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000916
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000917 // If we're doing a normal blur, we can clobber the pathTexture in the
918 // gaussianBlur. Otherwise, we need to save it for later compositing.
919 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000920 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
921 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000922
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000923 if (!isNormalBlur) {
924 GrPaint paint;
925 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +0000926 paint.textureSampler(0)->textureParams()->setClampNoFilter();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000927 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
928 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000929 // Blend pathTexture over blurTexture.
930 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000931 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
932 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000933 if (SkMaskFilter::kInner_BlurType == blurType) {
934 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000935 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
936 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000937 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
938 // solid: dst = src + dst - src * dst
939 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000940 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
941 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000942 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
943 // outer: dst = dst * (1 - src)
944 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000945 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
946 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000947 }
948 context->drawRect(paint, srcRect);
949 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000950 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000951 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000952
bsalomon@google.come3d32162012-07-20 13:37:06 +0000953 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
954 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000955 }
956
957 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
958 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000959 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000960 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000961 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000962
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000963 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
964 -finalRect.fTop);
965 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
966 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000967 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000968 return true;
969}
970
bsalomon@google.com85003222012-03-28 14:44:37 +0000971bool drawWithMaskFilter(GrContext* context, const SkPath& path,
972 SkMaskFilter* filter, const SkMatrix& matrix,
973 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000974 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000975 SkMask srcM, dstM;
976
977 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000978 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000979 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000980 return false;
981 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000982 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000983
984 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
985 return false;
986 }
987 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000988 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000989
990 if (clip.quickReject(dstM.fBounds)) {
991 return false;
992 }
993 if (bounder && !bounder->doIRect(dstM.fBounds)) {
994 return false;
995 }
996
997 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
998 // the current clip (and identity matrix) and grpaint settings
999
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001000 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001001
bsalomon@google.come3d32162012-07-20 13:37:06 +00001002 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
1003 return false;
1004 }
1005
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001006 GrTextureDesc desc;
1007 desc.fWidth = dstM.fBounds.width();
1008 desc.fHeight = dstM.fBounds.height();
1009 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +00001010
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001011 GrAutoScratchTexture ast(context, desc);
1012 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001013
reed@google.com69302852011-02-16 18:08:07 +00001014 if (NULL == texture) {
1015 return false;
1016 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001017 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001018 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001019
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001020 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1021 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001022 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001023 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +00001024 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +00001025
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001026 GrRect d;
1027 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001028 GrIntToScalar(dstM.fBounds.fTop),
1029 GrIntToScalar(dstM.fBounds.fRight),
1030 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001031
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001032 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1033 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1034 -dstM.fBounds.fTop*SK_Scalar1);
1035 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001036 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001037 return true;
1038}
reed@google.com69302852011-02-16 18:08:07 +00001039
bsalomon@google.com85003222012-03-28 14:44:37 +00001040}
1041
1042///////////////////////////////////////////////////////////////////////////////
1043
reed@google.com0c219b62011-02-16 21:31:18 +00001044void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001045 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001046 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001047 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001048 CHECK_SHOULD_DRAW(draw);
1049
reed@google.comfe626382011-09-21 13:50:35 +00001050 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001051
bsalomon@google.com5782d712011-01-21 21:03:59 +00001052 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001053 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001054 if (!skPaint2GrPaintShader(this,
1055 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001056 true,
twiz@google.com58071162012-07-18 21:41:50 +00001057 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001058 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001059 return;
1060 }
1061
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001062 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1063 // if we can, we draw lots faster (raster device does this same test)
1064 SkScalar hairlineCoverage;
1065 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1066 doFill = false;
1067 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1068 grPaint.fCoverage);
1069 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001070
reed@google.comfe626382011-09-21 13:50:35 +00001071 // If we have a prematrix, apply it to the path, optimizing for the case
1072 // where the original path can in fact be modified in place (even though
1073 // its parameter type is const).
1074 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1075 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001076
1077 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001078 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001079
reed@google.come3445642011-02-16 23:20:39 +00001080 if (!pathIsMutable) {
1081 result = &tmpPath;
1082 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001083 }
reed@google.come3445642011-02-16 23:20:39 +00001084 // should I push prePathMatrix on our MV stack temporarily, instead
1085 // of applying it here? See SkDraw.cpp
1086 pathPtr->transform(*prePathMatrix, result);
1087 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001088 }
reed@google.com0c219b62011-02-16 21:31:18 +00001089 // at this point we're done with prePathMatrix
1090 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001091
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001092 if (paint.getPathEffect() ||
1093 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001094 // it is safe to use tmpPath here, even if we already used it for the
1095 // prepathmatrix, since getFillPath can take the same object for its
1096 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001097 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001098 pathPtr = &tmpPath;
1099 }
1100
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001101 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001102 // avoid possibly allocating a new path in transform if we can
1103 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1104
1105 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001106 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001107 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001108 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001109 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001110 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001111 &grPaint, pathFillType)) {
1112 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1113 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001114 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001115 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001116 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001117 }
reed@google.com69302852011-02-16 18:08:07 +00001118 return;
1119 }
reed@google.com69302852011-02-16 18:08:07 +00001120
bsalomon@google.com47059542012-06-06 20:51:20 +00001121 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001122
reed@google.com0c219b62011-02-16 21:31:18 +00001123 if (doFill) {
1124 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001125 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001126 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001127 break;
1128 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001129 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001130 break;
1131 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001132 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001133 break;
1134 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001135 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001136 break;
1137 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001138 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001139 return;
1140 }
1141 }
1142
reed@google.com07f3ee12011-05-16 17:21:57 +00001143 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001144}
1145
bsalomon@google.comfb309512011-11-30 14:13:48 +00001146namespace {
1147
1148inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1149 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1150 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1151 return tilesX * tilesY;
1152}
1153
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001154inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001155 const SkIRect* srcRectPtr,
1156 int maxTextureSize) {
1157 static const int kSmallTileSize = 1 << 10;
1158 if (maxTextureSize <= kSmallTileSize) {
1159 return maxTextureSize;
1160 }
1161
1162 size_t maxTexTotalTileSize;
1163 size_t smallTotalTileSize;
1164
1165 if (NULL == srcRectPtr) {
1166 int w = bitmap.width();
1167 int h = bitmap.height();
1168 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1169 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1170 } else {
1171 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1172 srcRectPtr->fTop,
1173 srcRectPtr->fRight,
1174 srcRectPtr->fBottom,
1175 maxTextureSize);
1176 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1177 srcRectPtr->fTop,
1178 srcRectPtr->fRight,
1179 srcRectPtr->fBottom,
1180 kSmallTileSize);
1181 }
1182 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1183 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1184
1185 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1186 return kSmallTileSize;
1187 } else {
1188 return maxTextureSize;
1189 }
1190}
1191}
1192
1193bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001194 const GrTextureParams& params,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001195 const SkIRect* srcRectPtr,
1196 int* tileSize) const {
1197 SkASSERT(NULL != tileSize);
1198
1199 // if bitmap is explictly texture backed then just use the texture
1200 if (NULL != bitmap.getTexture()) {
1201 return false;
1202 }
1203 // if it's larger than the max texture size, then we have no choice but
1204 // tiling
1205 const int maxTextureSize = fContext->getMaxTextureSize();
1206 if (bitmap.width() > maxTextureSize ||
1207 bitmap.height() > maxTextureSize) {
1208 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1209 return true;
1210 }
1211 // if we are going to have to draw the whole thing, then don't tile
1212 if (NULL == srcRectPtr) {
1213 return false;
1214 }
1215 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001216 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001217 return false;
1218 }
1219
1220 // At this point we know we could do the draw by uploading the entire bitmap
1221 // as a texture. However, if the texture would be large compared to the
1222 // cache size and we don't require most of it for this draw then tile to
1223 // reduce the amount of upload and cache spill.
1224
1225 // assumption here is that sw bitmap size is a good proxy for its size as
1226 // a texture
1227 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001228 size_t cacheSize;
1229 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001230 if (bmpSize < cacheSize / 2) {
1231 return false;
1232 }
1233
1234 SkFixed fracUsed =
1235 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1236 (srcRectPtr->height() << 16) / bitmap.height());
1237 if (fracUsed <= SK_FixedHalf) {
1238 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1239 return true;
1240 } else {
1241 return false;
1242 }
1243}
1244
reed@google.comac10a2d2010-12-22 21:39:39 +00001245void SkGpuDevice::drawBitmap(const SkDraw& draw,
1246 const SkBitmap& bitmap,
1247 const SkIRect* srcRectPtr,
1248 const SkMatrix& m,
1249 const SkPaint& paint) {
1250 CHECK_SHOULD_DRAW(draw);
1251
1252 SkIRect srcRect;
1253 if (NULL == srcRectPtr) {
1254 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1255 } else {
1256 srcRect = *srcRectPtr;
1257 }
1258
junov@google.comd935cfb2011-06-27 20:48:23 +00001259 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001260 // Convert the bitmap to a shader so that the rect can be drawn
1261 // through drawRect, which supports mask filters.
1262 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001263 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001264 if (srcRectPtr) {
1265 if (!bitmap.extractSubset(&tmp, srcRect)) {
1266 return; // extraction failed
1267 }
1268 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001269 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001270 }
1271 SkPaint paintWithTexture(paint);
1272 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1273 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001274 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001275 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001276
junov@google.com1d329782011-07-28 20:10:09 +00001277 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001278 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001279 // also affect the behavior of the mask filter.
1280 SkMatrix drawMatrix;
1281 drawMatrix.setConcat(*draw.fMatrix, m);
1282 SkDraw transformedDraw(draw);
1283 transformedDraw.fMatrix = &drawMatrix;
1284
1285 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1286
junov@google.comd935cfb2011-06-27 20:48:23 +00001287 return;
1288 }
1289
bsalomon@google.com5782d712011-01-21 21:03:59 +00001290 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001291 SkAutoCachedTexture colorLutTexture;
1292 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001293 return;
1294 }
bsalomon@google.comb8670992012-07-25 21:27:09 +00001295 GrTextureParams* params = grPaint.textureSampler(kBitmapTextureIdx)->textureParams();
1296 params->setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001297
bsalomon@google.comfb309512011-11-30 14:13:48 +00001298 int tileSize;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001299 if (!this->shouldTileBitmap(bitmap, *params, srcRectPtr, &tileSize)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001300 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001301 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 return;
1303 }
1304
1305 // undo the translate done by SkCanvas
1306 int DX = SkMax32(0, srcRect.fLeft);
1307 int DY = SkMax32(0, srcRect.fTop);
1308 // compute clip bounds in local coordinates
1309 SkIRect clipRect;
1310 {
1311 SkRect r;
1312 r.set(draw.fClip->getBounds());
1313 SkMatrix matrix, inverse;
1314 matrix.setConcat(*draw.fMatrix, m);
1315 if (!matrix.invert(&inverse)) {
1316 return;
1317 }
1318 inverse.mapRect(&r);
1319 r.roundOut(&clipRect);
1320 // apply the canvas' translate to our local clip
1321 clipRect.offset(DX, DY);
1322 }
1323
bsalomon@google.comfb309512011-11-30 14:13:48 +00001324 int nx = bitmap.width() / tileSize;
1325 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001326 for (int x = 0; x <= nx; x++) {
1327 for (int y = 0; y <= ny; y++) {
1328 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001329 tileR.set(x * tileSize, y * tileSize,
1330 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001331 if (!SkIRect::Intersects(tileR, clipRect)) {
1332 continue;
1333 }
1334
1335 SkIRect srcR = tileR;
1336 if (!srcR.intersect(srcRect)) {
1337 continue;
1338 }
1339
1340 SkBitmap tmpB;
1341 if (bitmap.extractSubset(&tmpB, tileR)) {
1342 // now offset it to make it "local" to our tmp bitmap
1343 srcR.offset(-tileR.fLeft, -tileR.fTop);
1344
1345 SkMatrix tmpM(m);
1346 {
1347 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1348 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1349 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1350 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001351 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001352 }
1353 }
1354 }
1355}
1356
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001357namespace {
1358
1359bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1360 // detect pixel disalignment
1361 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1362 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1363 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1364 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1365 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1366 COLOR_BLEED_TOLERANCE &&
1367 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1368 COLOR_BLEED_TOLERANCE) {
1369 return true;
1370 }
1371 return false;
1372}
1373
1374bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1375 const SkMatrix& m) {
1376 // Only gets called if hasAlignedSamples returned false.
1377 // So we can assume that sampling is axis aligned but not texel aligned.
1378 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1379 SkRect innerSrcRect(srcRect), innerTransformedRect,
1380 outerTransformedRect(transformedRect);
1381 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1382 m.mapRect(&innerTransformedRect, innerSrcRect);
1383
1384 // The gap between outerTransformedRect and innerTransformedRect
1385 // represents the projection of the source border area, which is
1386 // problematic for color bleeding. We must check whether any
1387 // destination pixels sample the border area.
1388 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1389 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1390 SkIRect outer, inner;
1391 outerTransformedRect.round(&outer);
1392 innerTransformedRect.round(&inner);
1393 // If the inner and outer rects round to the same result, it means the
1394 // border does not overlap any pixel centers. Yay!
1395 return inner != outer;
1396}
1397
1398} // unnamed namespace
1399
reed@google.comac10a2d2010-12-22 21:39:39 +00001400/*
1401 * This is called by drawBitmap(), which has to handle images that may be too
1402 * large to be represented by a single texture.
1403 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001404 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1405 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001406 */
1407void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1408 const SkBitmap& bitmap,
1409 const SkIRect& srcRect,
1410 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001411 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001412 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1413 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001414
reed@google.com9c49bc32011-07-07 13:42:37 +00001415 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001416 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001417 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001418 return;
1419 }
1420
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001421 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001422
bsalomon@google.comb8670992012-07-25 21:27:09 +00001423 sampler->textureParams()->setClamp();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001424 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001425
1426 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001427 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001428 if (NULL == texture) {
1429 return;
1430 }
1431
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001432 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1433 (GrSingleTextureEffect, (texture)))->unref();
reed@google.com46799cd2011-02-22 20:56:26 +00001434
reed@google.com20efde72011-05-09 17:00:02 +00001435 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1436 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001437 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001438 float wInv = 1.f / bitmap.width();
1439 float hInv = 1.f / bitmap.height();
1440 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1441 SkFloatToScalar(srcRect.fTop * hInv),
1442 SkFloatToScalar(srcRect.fRight * wInv),
1443 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001444
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001445 bool needsTextureDomain = false;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001446 if (sampler->textureParams()->isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001447 // Need texture domain if drawing a sub rect.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001448 needsTextureDomain = srcRect.width() < bitmap.width() || srcRect.height() < bitmap.height();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001449 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1450 // sampling is axis-aligned
1451 GrRect floatSrcRect, transformedRect;
1452 floatSrcRect.set(srcRect);
1453 SkMatrix srcToDeviceMatrix(m);
1454 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1455 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1456
1457 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1458 // Samples are texel-aligned, so filtering is futile
bsalomon@google.comb8670992012-07-25 21:27:09 +00001459 sampler->textureParams()->setBilerp(false);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001460 needsTextureDomain = false;
1461 } else {
1462 needsTextureDomain = needsTextureDomain &&
1463 mayColorBleed(floatSrcRect, transformedRect, m);
1464 }
1465 }
1466 }
1467
1468 GrRect textureDomain = GrRect::MakeEmpty();
1469
1470 if (needsTextureDomain) {
1471 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001472 GrScalar left, top, right, bottom;
1473 if (srcRect.width() > 1) {
1474 GrScalar border = GR_ScalarHalf / bitmap.width();
1475 left = paintRect.left() + border;
1476 right = paintRect.right() - border;
1477 } else {
1478 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1479 }
1480 if (srcRect.height() > 1) {
1481 GrScalar border = GR_ScalarHalf / bitmap.height();
1482 top = paintRect.top() + border;
1483 bottom = paintRect.bottom() - border;
1484 } else {
1485 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1486 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001487 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001488 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1489 (texture,
1490 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001491 }
1492
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001493 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001494}
1495
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001496namespace {
1497
1498void apply_custom_stage(GrContext* context,
1499 GrTexture* srcTexture,
1500 GrTexture* dstTexture,
1501 const GrRect& rect,
1502 GrCustomStage* stage) {
1503 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001504 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001505 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001506 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001507
1508 GrMatrix sampleM;
1509 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1510 GrPaint paint;
1511 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001512 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001513 paint.textureSampler(0)->reset(sampleM);
1514 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001515 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001516}
1517
1518};
1519
reed@google.com8926b162012-03-23 15:36:36 +00001520static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1521 SkImageFilter* filter, const GrRect& rect) {
1522 GrAssert(filter);
1523
1524 SkSize blurSize;
1525 SkISize radius;
1526
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001527 GrTextureDesc desc;
1528 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1529 desc.fWidth = SkScalarCeilToInt(rect.width());
1530 desc.fHeight = SkScalarCeilToInt(rect.height());
1531 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001532 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001533
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001534 if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001535 GrAutoScratchTexture dst(context, desc);
1536 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1537 texture = dst.detach();
1538 stage->unref();
1539 } else if (filter->asABlur(&blurSize)) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001540 texture = context->gaussianBlur(texture, false, rect,
reed@google.com8926b162012-03-23 15:36:36 +00001541 blurSize.width(),
1542 blurSize.height());
reed@google.com8926b162012-03-23 15:36:36 +00001543 } else if (filter->asADilate(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001544 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001545 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001546 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001547 } else if (filter->asAnErode(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001548 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001549 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001550 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001551 }
1552 return texture;
1553}
1554
reed@google.comac10a2d2010-12-22 21:39:39 +00001555void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1556 int left, int top, const SkPaint& paint) {
1557 CHECK_SHOULD_DRAW(draw);
1558
reed@google.com8926b162012-03-23 15:36:36 +00001559 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001560 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1561 return;
1562 }
1563
reed@google.com76dd2772012-01-05 21:15:07 +00001564 int w = bitmap.width();
1565 int h = bitmap.height();
1566
bsalomon@google.com5782d712011-01-21 21:03:59 +00001567 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001568 SkAutoCachedTexture colorLutTexture;
1569 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001570 return;
1571 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001572
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001573 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001574
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001575 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001576
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001577 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001578 sampler->reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001579 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001580 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1581 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001582
reed@google.com8926b162012-03-23 15:36:36 +00001583 SkImageFilter* filter = paint.getImageFilter();
1584 if (NULL != filter) {
1585 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001586 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001587 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001588 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1589 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001590 texture = filteredTexture;
1591 filteredTexture->unref();
1592 }
reed@google.com76dd2772012-01-05 21:15:07 +00001593 }
reed@google.com8926b162012-03-23 15:36:36 +00001594
bsalomon@google.com5782d712011-01-21 21:03:59 +00001595 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001596 GrRect::MakeXYWH(GrIntToScalar(left),
1597 GrIntToScalar(top),
1598 GrIntToScalar(w),
1599 GrIntToScalar(h)),
1600 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1601 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001602}
1603
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001604void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001605 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001606 // clear of the source device must occur before CHECK_SHOULD_DRAW
1607 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1608 if (dev->fNeedClear) {
1609 // TODO: could check here whether we really need to draw at all
1610 dev->clear(0x0);
1611 }
1612
reed@google.comac10a2d2010-12-22 21:39:39 +00001613 CHECK_SHOULD_DRAW(draw);
1614
bsalomon@google.com5782d712011-01-21 21:03:59 +00001615 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001616 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001617 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001618 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001619 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001620 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001621
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001622 GrTexture* devTex = grPaint.getTexture(0);
1623 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001624
reed@google.com8926b162012-03-23 15:36:36 +00001625 SkImageFilter* filter = paint.getImageFilter();
tomhudson@google.com2683a412012-07-20 19:15:06 +00001626 grPaint.textureSampler(kBitmapTextureIdx)->reset();
reed@google.com8926b162012-03-23 15:36:36 +00001627 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001628 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1629 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001630 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1631 rect);
1632 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001633 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1634 (GrSingleTextureEffect, (filteredTexture)))->unref();
tomhudson@google.com2683a412012-07-20 19:15:06 +00001635 grPaint.setTexture(kBitmapTextureIdx, NULL);
reed@google.com8926b162012-03-23 15:36:36 +00001636 devTex = filteredTexture;
1637 filteredTexture->unref();
1638 }
1639 }
1640
bsalomon@google.com5782d712011-01-21 21:03:59 +00001641 const SkBitmap& bm = dev->accessBitmap(false);
1642 int w = bm.width();
1643 int h = bm.height();
1644
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001645 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001646 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1647 GrIntToScalar(y),
1648 GrIntToScalar(w),
1649 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001650
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001651 // The device being drawn may not fill up its texture (saveLayer uses
1652 // the approximate ).
1653 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1654 GR_Scalar1 * h / devTex->height());
1655
1656 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001657}
1658
reed@google.com8926b162012-03-23 15:36:36 +00001659bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001660 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001661 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001662
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001663 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001664 !filter->asABlur(&size) &&
1665 !filter->asADilate(&radius) &&
1666 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001667 return false;
1668 }
reed@google.com8926b162012-03-23 15:36:36 +00001669 return true;
1670}
1671
1672bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1673 const SkMatrix& ctm,
1674 SkBitmap* result, SkIPoint* offset) {
1675 // want explicitly our impl, so guard against a subclass of us overriding it
1676 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001677 return false;
1678 }
reed@google.com8926b162012-03-23 15:36:36 +00001679
1680 SkAutoLockPixels alp(src, !src.getTexture());
1681 if (!src.getTexture() && !src.readyToDraw()) {
1682 return false;
1683 }
1684
1685 GrPaint paint;
1686 paint.reset();
1687
1688 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1689
1690 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001691 SkAutoCachedTexture act(this, src, sampler->textureParams(), &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001692
1693 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001694 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1695 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001696 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1697 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001698 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1699 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001700 resultTexture->unref();
1701 }
reed@google.com76dd2772012-01-05 21:15:07 +00001702 return true;
1703}
1704
reed@google.comac10a2d2010-12-22 21:39:39 +00001705///////////////////////////////////////////////////////////////////////////////
1706
1707// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001708static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001709 kTriangles_GrPrimitiveType,
1710 kTriangleStrip_GrPrimitiveType,
1711 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001712};
1713
1714void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1715 int vertexCount, const SkPoint vertices[],
1716 const SkPoint texs[], const SkColor colors[],
1717 SkXfermode* xmode,
1718 const uint16_t indices[], int indexCount,
1719 const SkPaint& paint) {
1720 CHECK_SHOULD_DRAW(draw);
1721
bsalomon@google.com5782d712011-01-21 21:03:59 +00001722 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001723 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001724 // we ignore the shader if texs is null.
1725 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001726 if (!skPaint2GrPaintNoShader(this,
1727 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001728 false,
1729 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001730 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001731 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001732 return;
1733 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001734 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001735 if (!skPaint2GrPaintShader(this,
1736 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001737 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001738 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001739 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001740 return;
1741 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001742 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001743
1744 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001745 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001746 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1747#if 0
1748 return
1749#endif
1750 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001751 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001752
bsalomon@google.com498776a2011-08-16 19:20:44 +00001753 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1754 if (NULL != colors) {
1755 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001756 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001757 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001758 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001759 }
1760 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001761 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001762 fContext->drawVertices(grPaint,
1763 gVertexMode2PrimitiveType[vmode],
1764 vertexCount,
1765 (GrPoint*) vertices,
1766 (GrPoint*) texs,
1767 colors,
1768 indices,
1769 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001770}
1771
1772///////////////////////////////////////////////////////////////////////////////
1773
1774static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001775 GrFontScaler* scaler = (GrFontScaler*)data;
1776 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001777}
1778
1779static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1780 void* auxData;
1781 GrFontScaler* scaler = NULL;
1782 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1783 scaler = (GrFontScaler*)auxData;
1784 }
1785 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001786 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001787 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1788 }
1789 return scaler;
1790}
1791
1792static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1793 SkFixed fx, SkFixed fy,
1794 const SkGlyph& glyph) {
1795 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1796
bungeman@google.com15865a72012-01-11 16:28:04 +00001797 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001798
1799 if (NULL == procs->fFontScaler) {
1800 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1801 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001802
bungeman@google.com15865a72012-01-11 16:28:04 +00001803 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1804 glyph.getSubXFixed(),
1805 glyph.getSubYFixed()),
1806 SkFixedFloorToFixed(fx),
1807 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001808 procs->fFontScaler);
1809}
1810
bsalomon@google.com5782d712011-01-21 21:03:59 +00001811SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001812
1813 // deferred allocation
1814 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001815 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001816 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1817 fDrawProcs->fContext = fContext;
1818 }
1819
1820 // init our (and GL's) state
1821 fDrawProcs->fTextContext = context;
1822 fDrawProcs->fFontScaler = NULL;
1823 return fDrawProcs;
1824}
1825
1826void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1827 size_t byteLength, SkScalar x, SkScalar y,
1828 const SkPaint& paint) {
1829 CHECK_SHOULD_DRAW(draw);
1830
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001831 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001832 // this guy will just call our drawPath()
1833 draw.drawText((const char*)text, byteLength, x, y, paint);
1834 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001835 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001836
1837 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001838 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001839 if (!skPaint2GrPaintShader(this,
1840 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001841 true,
twiz@google.com58071162012-07-18 21:41:50 +00001842 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001843 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001844 return;
1845 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001846 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1847 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001848 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1849 }
1850}
1851
1852void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1853 size_t byteLength, const SkScalar pos[],
1854 SkScalar constY, int scalarsPerPos,
1855 const SkPaint& paint) {
1856 CHECK_SHOULD_DRAW(draw);
1857
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001858 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001859 // this guy will just call our drawPath()
1860 draw.drawPosText((const char*)text, byteLength, pos, constY,
1861 scalarsPerPos, paint);
1862 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001863 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001864
1865 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001866 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001867 if (!skPaint2GrPaintShader(this,
1868 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001869 true,
twiz@google.com58071162012-07-18 21:41:50 +00001870 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001871 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001872 return;
1873 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001874 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1875 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001876 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1877 scalarsPerPos, paint);
1878 }
1879}
1880
1881void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1882 size_t len, const SkPath& path,
1883 const SkMatrix* m, const SkPaint& paint) {
1884 CHECK_SHOULD_DRAW(draw);
1885
1886 SkASSERT(draw.fDevice == this);
1887 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1888}
1889
1890///////////////////////////////////////////////////////////////////////////////
1891
reed@google.comf67e4cf2011-03-15 20:56:58 +00001892bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1893 if (!paint.isLCDRenderText()) {
1894 // we're cool with the paint as is
1895 return false;
1896 }
1897
1898 if (paint.getShader() ||
1899 paint.getXfermode() || // unless its srcover
1900 paint.getMaskFilter() ||
1901 paint.getRasterizer() ||
1902 paint.getColorFilter() ||
1903 paint.getPathEffect() ||
1904 paint.isFakeBoldText() ||
1905 paint.getStyle() != SkPaint::kFill_Style) {
1906 // turn off lcd
1907 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1908 flags->fHinting = paint.getHinting();
1909 return true;
1910 }
1911 // we're cool with the paint as is
1912 return false;
1913}
1914
reed@google.com75d939b2011-12-07 15:07:23 +00001915void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001916 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001917 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001918}
1919
reed@google.comf67e4cf2011-03-15 20:56:58 +00001920///////////////////////////////////////////////////////////////////////////////
1921
bsalomon@google.comfb309512011-11-30 14:13:48 +00001922bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001923 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001924 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001925 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001926
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001927 GrTextureDesc desc;
1928 desc.fWidth = bitmap.width();
1929 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001930 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001931 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001932
bsalomon@google.comb8670992012-07-25 21:27:09 +00001933 return this->context()->isTextureInCache(desc, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001934}
1935
1936
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001937SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1938 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001939 bool isOpaque,
1940 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001941 GrTextureDesc desc;
1942 desc.fConfig = fRenderTarget->config();
1943 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1944 desc.fWidth = width;
1945 desc.fHeight = height;
1946 desc.fSampleCnt = fRenderTarget->numSamples();
1947
1948 GrContext::TextureCacheEntry cacheEntry;
1949 GrTexture* texture;
1950 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001951 // Skia's convention is to only clear a device if it is non-opaque.
1952 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001953
1954#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1955 // layers are never draw in repeat modes, so we can request an approx
1956 // match and ignore any padding.
1957 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1958 GrContext::kApprox_ScratchTexMatch :
1959 GrContext::kExact_ScratchTexMatch;
1960 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1961 texture = cacheEntry.texture();
1962#else
1963 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1964 texture = tunref.get();
1965#endif
1966 if (texture) {
1967 return SkNEW_ARGS(SkGpuDevice,(fContext,
1968 texture,
1969 cacheEntry,
1970 needClear));
1971 } else {
1972 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1973 width, height);
1974 return NULL;
1975 }
1976}
1977
1978SkGpuDevice::SkGpuDevice(GrContext* context,
1979 GrTexture* texture,
1980 TexCache cacheEntry,
1981 bool needClear)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001982 : SkDevice(make_bitmap(context, texture->asRenderTarget()))
1983 , fClipStack(NULL) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001984 GrAssert(texture && texture->asRenderTarget());
1985 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1986 this->initFromRenderTarget(context, texture->asRenderTarget());
1987 fCache = cacheEntry;
1988 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001989}
1990