blob: 1da5a0a0a492df5a68facfc22293d74597877a93 [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,
88 const GrSamplerState* sampler,
89 GrTexture** texture) {
90 GrAssert(texture);
91 *texture = this->set(device, bitmap, sampler);
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,
102 const GrSamplerState* sampler) {
103 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
rileya@google.com24f3ad12012-07-18 21:47:40 +0000113 fTex = GrLockCachedBitmapTexture(device->context(), bitmap, sampler);
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;
412 clipStack.getConservativeBounds(-origin.fX,
413 -origin.fY,
414 renderTargetWidth,
415 renderTargetHeight,
416 &bounds);
417
reed@google.com6f8f2922011-03-04 22:27:10 +0000418 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000419 bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000420 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000421}
422
423// call this ever each draw call, to ensure that the context reflects our state,
424// and not the state from some other canvas/device
425void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000426 GrAssert(NULL != fClipStack);
427
reed@google.comac10a2d2010-12-22 21:39:39 +0000428 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000429 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000430
431 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000432 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000433
bsalomon@google.comd302f142011-03-03 13:54:13 +0000434 convert_matrixclip(fContext, *draw.fMatrix,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000435 *fClipStack, *draw.fClip, this->getOrigin(),
436 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000437 fNeedPrepareRenderTarget = false;
438 }
439}
440
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000441void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
442 const SkClipStack& clipStack) {
443 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
444 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000445 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000446}
447
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000448void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
449
450 GrAssert(NULL != fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000451
reed@google.comac10a2d2010-12-22 21:39:39 +0000452 fContext->setRenderTarget(fRenderTarget);
453
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000454 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000455
robertphillips@google.com607fe072012-07-24 13:54:00 +0000456 convert_matrixclip(fContext, matrix, *fClipStack, clip, this->getOrigin(),
457 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000458
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000459 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000460}
461
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000462SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000463 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000464 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000465}
466
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000467bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000468 if (NULL != fTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000469 // FIXME: cannot use GrSingleTextureEffect here: fails
470 // assert in line 1617: null != devTex; generalizing GrPaint::getTexture()
471 // to grab textures off of GrCustomStages breaks gms in various ways -
472 // particularly since table color filter requires multiple textures
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000473 paint->setTexture(kBitmapTextureIdx, fTexture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000474 //paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
475 //SkNEW_ARGS(GrSingleTextureEffect, (fTexture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000476 return true;
477 }
478 return false;
479}
480
481///////////////////////////////////////////////////////////////////////////////
482
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000483SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
484SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
485SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
486SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
487SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
488 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000489SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
490 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000491SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
492SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000493
bsalomon@google.com84405e02012-03-05 19:57:21 +0000494namespace {
495
496// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
497// justAlpha indicates that skPaint's alpha should be used rather than the color
498// Callers may subsequently modify the GrPaint. Setting constantColor indicates
499// that the final paint will draw the same color at every pixel. This allows
500// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000501// color once while converting to GrPaint and then ignored.
502inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
503 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000504 bool justAlpha,
505 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000506 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000507 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000508
509 grPaint->fDither = skPaint.isDither();
510 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000511 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000512
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000513 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
514 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000515
516 SkXfermode* mode = skPaint.getXfermode();
517 if (mode) {
518 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000519 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000520#if 0
521 return false;
522#endif
523 }
524 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000525 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
526 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
527
bsalomon@google.com5782d712011-01-21 21:03:59 +0000528 if (justAlpha) {
529 uint8_t alpha = skPaint.getAlpha();
530 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000531 // justAlpha is currently set to true only if there is a texture,
532 // so constantColor should not also be true.
533 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000534 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000535 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000536 GrAssert(NULL == grPaint->getTexture(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000537 }
Scroggo97c88c22011-05-11 14:05:25 +0000538 SkColorFilter* colorFilter = skPaint.getColorFilter();
539 SkColor color;
540 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000541 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000542 SkBitmap colorTransformTable;
Scroggo97c88c22011-05-11 14:05:25 +0000543 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000544 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000545 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000546 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000547 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000548 } else {
549 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000550 grPaint->fColor = SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000551 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000552 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000553 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
554 grPaint->fColorMatrixEnabled = true;
555 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
556 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
twiz@google.com58071162012-07-18 21:41:50 +0000557 } else if (colorFilter != NULL && colorFilter->asComponentTable(
558 &colorTransformTable)) {
559 grPaint->resetColorFilter();
560
561 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
562 GrTexture* texture = act->set(dev, colorTransformTable, colorSampler);
563
bsalomon@google.com25f3e9b2012-07-20 14:23:09 +0000564 colorSampler->reset(GrSamplerState::kClamp_WrapMode, GrSamplerState::kNearest_Filter);
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000565 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000566 } else {
567 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000568 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000569 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000570}
571
bsalomon@google.com84405e02012-03-05 19:57:21 +0000572// This function is similar to skPaint2GrPaintNoShader but also converts
573// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
574// be used is set on grPaint and returned in param act. constantColor has the
575// same meaning as in skPaint2GrPaintNoShader.
576inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
577 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000578 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000579 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000580 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000581 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000582 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000583 return skPaint2GrPaintNoShader(dev,
584 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000585 false,
586 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000587 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000588 grPaint);
twiz@google.com58071162012-07-18 21:41:50 +0000589 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
590 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000591 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000592 }
593
reed@google.comac10a2d2010-12-22 21:39:39 +0000594 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000595 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000596 SkShader::TileMode tileModes[2];
597 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000598 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 tileModes, twoPointParams);
600
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000601 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000602 SkShader::GradientInfo info;
603 SkColor color;
604
605 info.fColors = &color;
606 info.fColorOffsets = NULL;
607 info.fColorCount = 1;
608 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
609 SkPaint copy(skPaint);
610 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000611 // modulate the paint alpha by the shader's solid color alpha
612 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
613 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000614 return skPaint2GrPaintNoShader(dev,
615 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000616 false,
617 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000618 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000619 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000620 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000621 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000622 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000623
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000624 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
twiz@google.com58071162012-07-18 21:41:50 +0000625 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, sampler);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000626 if (NULL == texture) {
627 SkDebugf("Couldn't convert bitmap to texture.\n");
628 return false;
629 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000630
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000631 switch (bmptype) {
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000632 case SkShader::kRadial_BitmapType:
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000633 sampler->setCustomStage(SkNEW_ARGS(GrRadialGradient, (texture)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000634 sampler->setFilter(GrSamplerState::kBilinear_Filter);
635 break;
636 case SkShader::kSweep_BitmapType:
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000637 sampler->setCustomStage(SkNEW_ARGS(GrSweepGradient, (texture)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000638 sampler->setFilter(GrSamplerState::kBilinear_Filter);
639 break;
640 case SkShader::kTwoPointRadial_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000641 sampler->setCustomStage(SkNEW_ARGS(GrRadial2Gradient,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000642 (texture,
643 twoPointParams[0],
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000644 twoPointParams[1],
645 twoPointParams[2] < 0)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000646 sampler->setFilter(GrSamplerState::kBilinear_Filter);
647 break;
rileya@google.com3e332582012-07-03 13:43:35 +0000648 case SkShader::kTwoPointConical_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000649 sampler->setCustomStage(SkNEW_ARGS(GrConical2Gradient,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000650 (texture,
651 twoPointParams[0],
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000652 twoPointParams[1],
653 twoPointParams[2])))->unref();
rileya@google.com3e332582012-07-03 13:43:35 +0000654 sampler->setFilter(GrSamplerState::kBilinear_Filter);
655 break;
rileya@google.com22e57f92012-07-19 15:16:19 +0000656 case SkShader::kLinear_BitmapType:
657 sampler->setCustomStage(SkNEW_ARGS(GrLinearGradient, (texture)))->unref();
rileya@google.com5ce42ca2012-07-19 19:42:10 +0000658 sampler->setFilter(GrSamplerState::kBilinear_Filter);
rileya@google.com22e57f92012-07-19 15:16:19 +0000659 break;
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000660 default:
661 if (skPaint.isFilterBitmap()) {
662 sampler->setFilter(GrSamplerState::kBilinear_Filter);
663 } else {
664 sampler->setFilter(GrSamplerState::kNearest_Filter);
665 }
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000666 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000667 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000668 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000669 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
670 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000671
reed@google.comac10a2d2010-12-22 21:39:39 +0000672 // since our texture coords will be in local space, we wack the texture
673 // matrix to map them back into 0...1 before we load it
674 SkMatrix localM;
675 if (shader->getLocalMatrix(&localM)) {
676 SkMatrix inverse;
677 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000678 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000679 }
680 }
681 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000682 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
683 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000684 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000685 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000686 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000687 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000688 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000689
690 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000691}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000692}
reed@google.comac10a2d2010-12-22 21:39:39 +0000693
694///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000695void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000696 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000697}
698
reed@google.comac10a2d2010-12-22 21:39:39 +0000699void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
700 CHECK_SHOULD_DRAW(draw);
701
bsalomon@google.com5782d712011-01-21 21:03:59 +0000702 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000703 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000704 if (!skPaint2GrPaintShader(this,
705 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000706 true,
twiz@google.com58071162012-07-18 21:41:50 +0000707 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000708 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000709 return;
710 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000711
712 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000713}
714
715// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000716static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000717 kPoints_GrPrimitiveType,
718 kLines_GrPrimitiveType,
719 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000720};
721
722void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000723 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000724 CHECK_SHOULD_DRAW(draw);
725
726 SkScalar width = paint.getStrokeWidth();
727 if (width < 0) {
728 return;
729 }
730
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000731 // we only handle hairlines and paints without path effects or mask filters,
732 // else we let the SkDraw call our drawPath()
733 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000734 draw.drawPoints(mode, count, pts, paint, true);
735 return;
736 }
737
bsalomon@google.com5782d712011-01-21 21:03:59 +0000738 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000739 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000740 if (!skPaint2GrPaintShader(this,
741 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000742 true,
twiz@google.com58071162012-07-18 21:41:50 +0000743 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000744 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000745 return;
746 }
747
bsalomon@google.com5782d712011-01-21 21:03:59 +0000748 fContext->drawVertices(grPaint,
749 gPointMode2PrimtiveType[mode],
750 count,
751 (GrPoint*)pts,
752 NULL,
753 NULL,
754 NULL,
755 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000756}
757
reed@google.comc9aa5872011-04-05 21:05:37 +0000758///////////////////////////////////////////////////////////////////////////////
759
reed@google.comac10a2d2010-12-22 21:39:39 +0000760void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
761 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000762 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000763 CHECK_SHOULD_DRAW(draw);
764
bungeman@google.com79bd8772011-07-18 15:34:08 +0000765 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000766 SkScalar width = paint.getStrokeWidth();
767
768 /*
769 We have special code for hairline strokes, miter-strokes, and fills.
770 Anything else we just call our path code.
771 */
772 bool usePath = doStroke && width > 0 &&
773 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000774 // another two reasons we might need to call drawPath...
775 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000776 usePath = true;
777 }
reed@google.com67db6642011-05-26 11:46:35 +0000778 // until we aa rotated rects...
779 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
780 usePath = true;
781 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000782 // small miter limit means right angles show bevel...
783 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
784 paint.getStrokeMiter() < SK_ScalarSqrt2)
785 {
786 usePath = true;
787 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000788 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000789 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
790 usePath = true;
791 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000792
793 if (usePath) {
794 SkPath path;
795 path.addRect(rect);
796 this->drawPath(draw, path, paint, NULL, true);
797 return;
798 }
799
800 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000801 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000802 if (!skPaint2GrPaintShader(this,
803 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000804 true,
twiz@google.com58071162012-07-18 21:41:50 +0000805 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000806 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000807 return;
808 }
reed@google.com20efde72011-05-09 17:00:02 +0000809 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000810}
811
reed@google.com69302852011-02-16 18:08:07 +0000812#include "SkMaskFilter.h"
813#include "SkBounder.h"
814
bsalomon@google.com85003222012-03-28 14:44:37 +0000815///////////////////////////////////////////////////////////////////////////////
816
817// helpers for applying mask filters
818namespace {
819
820GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000821 switch (fillType) {
822 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000823 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000824 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000825 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000826 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000827 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000828 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000829 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000830 default:
831 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000832 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000833 }
834}
835
bsalomon@google.com85003222012-03-28 14:44:37 +0000836// We prefer to blur small rect with small radius via CPU.
837#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
838#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
839inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
840 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
841 rect.height() <= MIN_GPU_BLUR_SIZE &&
842 radius <= MIN_GPU_BLUR_RADIUS) {
843 return true;
844 }
845 return false;
846}
847
848bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
849 SkMaskFilter* filter, const SkMatrix& matrix,
850 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000851 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000852#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000853 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000854#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000855 SkMaskFilter::BlurInfo info;
856 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000857 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000858 return false;
859 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000860 SkScalar radius = info.fIgnoreTransform ? info.fRadius
861 : matrix.mapRadius(info.fRadius);
862 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000863 if (radius <= 0) {
864 return false;
865 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000866
867 SkRect srcRect = path.getBounds();
868 if (shouldDrawBlurWithCPU(srcRect, radius)) {
869 return false;
870 }
871
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000872 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000873 float sigma3 = sigma * 3.0f;
874
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000875 SkRect clipRect;
876 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000877
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000878 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000879 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
880 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000881 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000882 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000883 SkIRect finalIRect;
884 finalRect.roundOut(&finalIRect);
885 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000886 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000887 }
888 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000889 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000890 }
891 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000892 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000893 GrTextureDesc desc;
894 desc.fFlags = kRenderTarget_GrTextureFlagBit;
895 desc.fWidth = SkScalarCeilToInt(srcRect.width());
896 desc.fHeight = SkScalarCeilToInt(srcRect.height());
897 // We actually only need A8, but it often isn't supported as a
898 // render target so default to RGBA_8888
899 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000900
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000901 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
902 desc.fConfig = kAlpha_8_GrPixelConfig;
903 }
904
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000905 GrAutoScratchTexture pathEntry(context, desc);
906 GrTexture* pathTexture = pathEntry.texture();
907 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000908 return false;
909 }
910 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000911 // Once this code moves into GrContext, this should be changed to use
912 // an AutoClipRestore.
913 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000914 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000915
916 GrClip newClip(srcRect);
917 context->setClip(newClip);
918
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000919 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000920 GrPaint tempPaint;
921 tempPaint.reset();
922
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000923 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000924 tempPaint.fAntiAlias = grp->fAntiAlias;
925 if (tempPaint.fAntiAlias) {
926 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
927 // blend coeff of zero requires dual source blending support in order
928 // to properly blend partially covered pixels. This means the AA
929 // code path may not be taken. So we use a dst blend coeff of ISA. We
930 // could special case AA draws to a dst surface with known alpha=0 to
931 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000932 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
933 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000934 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000935 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000936 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000937
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000938 // If we're doing a normal blur, we can clobber the pathTexture in the
939 // gaussianBlur. Otherwise, we need to save it for later compositing.
940 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000941 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
942 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000943
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000944 if (!isNormalBlur) {
945 GrPaint paint;
946 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000947 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000948 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
949 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000950 // Blend pathTexture over blurTexture.
951 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000952 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
953 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000954 if (SkMaskFilter::kInner_BlurType == blurType) {
955 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000956 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
957 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000958 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
959 // solid: dst = src + dst - src * dst
960 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000961 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
962 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000963 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
964 // outer: dst = dst * (1 - src)
965 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000966 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
967 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000968 }
969 context->drawRect(paint, srcRect);
970 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000971 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000972 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000973
bsalomon@google.come3d32162012-07-20 13:37:06 +0000974 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
975 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000976 }
977
978 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
979 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000980 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000981 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000982 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000983
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000984 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
985 -finalRect.fTop);
986 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
987 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000988 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000989 return true;
990}
991
bsalomon@google.com85003222012-03-28 14:44:37 +0000992bool drawWithMaskFilter(GrContext* context, const SkPath& path,
993 SkMaskFilter* filter, const SkMatrix& matrix,
994 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000995 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000996 SkMask srcM, dstM;
997
998 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000999 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001000 style)) {
reed@google.com69302852011-02-16 18:08:07 +00001001 return false;
1002 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001003 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001004
1005 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
1006 return false;
1007 }
1008 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +00001009 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001010
1011 if (clip.quickReject(dstM.fBounds)) {
1012 return false;
1013 }
1014 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1015 return false;
1016 }
1017
1018 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1019 // the current clip (and identity matrix) and grpaint settings
1020
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001021 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001022
bsalomon@google.come3d32162012-07-20 13:37:06 +00001023 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
1024 return false;
1025 }
1026
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001027 GrTextureDesc desc;
1028 desc.fWidth = dstM.fBounds.width();
1029 desc.fHeight = dstM.fBounds.height();
1030 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +00001031
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001032 GrAutoScratchTexture ast(context, desc);
1033 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001034
reed@google.com69302852011-02-16 18:08:07 +00001035 if (NULL == texture) {
1036 return false;
1037 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001038 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001039 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001040
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001041 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1042 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001043 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001044 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +00001045 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +00001046
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001047 GrRect d;
1048 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001049 GrIntToScalar(dstM.fBounds.fTop),
1050 GrIntToScalar(dstM.fBounds.fRight),
1051 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001052
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001053 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1054 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1055 -dstM.fBounds.fTop*SK_Scalar1);
1056 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001057 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001058 return true;
1059}
reed@google.com69302852011-02-16 18:08:07 +00001060
bsalomon@google.com85003222012-03-28 14:44:37 +00001061}
1062
1063///////////////////////////////////////////////////////////////////////////////
1064
reed@google.com0c219b62011-02-16 21:31:18 +00001065void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001066 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001067 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001068 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001069 CHECK_SHOULD_DRAW(draw);
1070
reed@google.comfe626382011-09-21 13:50:35 +00001071 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001072
bsalomon@google.com5782d712011-01-21 21:03:59 +00001073 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001074 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001075 if (!skPaint2GrPaintShader(this,
1076 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001077 true,
twiz@google.com58071162012-07-18 21:41:50 +00001078 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001079 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001080 return;
1081 }
1082
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001083 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1084 // if we can, we draw lots faster (raster device does this same test)
1085 SkScalar hairlineCoverage;
1086 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1087 doFill = false;
1088 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1089 grPaint.fCoverage);
1090 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001091
reed@google.comfe626382011-09-21 13:50:35 +00001092 // If we have a prematrix, apply it to the path, optimizing for the case
1093 // where the original path can in fact be modified in place (even though
1094 // its parameter type is const).
1095 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1096 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001097
1098 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001099 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001100
reed@google.come3445642011-02-16 23:20:39 +00001101 if (!pathIsMutable) {
1102 result = &tmpPath;
1103 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001104 }
reed@google.come3445642011-02-16 23:20:39 +00001105 // should I push prePathMatrix on our MV stack temporarily, instead
1106 // of applying it here? See SkDraw.cpp
1107 pathPtr->transform(*prePathMatrix, result);
1108 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001109 }
reed@google.com0c219b62011-02-16 21:31:18 +00001110 // at this point we're done with prePathMatrix
1111 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001112
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001113 if (paint.getPathEffect() ||
1114 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001115 // it is safe to use tmpPath here, even if we already used it for the
1116 // prepathmatrix, since getFillPath can take the same object for its
1117 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001118 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001119 pathPtr = &tmpPath;
1120 }
1121
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001122 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001123 // avoid possibly allocating a new path in transform if we can
1124 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1125
1126 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001127 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001128 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001129 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001130 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001131 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001132 &grPaint, pathFillType)) {
1133 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1134 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001135 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001136 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001137 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001138 }
reed@google.com69302852011-02-16 18:08:07 +00001139 return;
1140 }
reed@google.com69302852011-02-16 18:08:07 +00001141
bsalomon@google.com47059542012-06-06 20:51:20 +00001142 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001143
reed@google.com0c219b62011-02-16 21:31:18 +00001144 if (doFill) {
1145 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001146 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001147 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001148 break;
1149 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001150 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001151 break;
1152 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001153 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001154 break;
1155 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001156 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001157 break;
1158 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001159 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001160 return;
1161 }
1162 }
1163
reed@google.com07f3ee12011-05-16 17:21:57 +00001164 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001165}
1166
bsalomon@google.comfb309512011-11-30 14:13:48 +00001167namespace {
1168
1169inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1170 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1171 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1172 return tilesX * tilesY;
1173}
1174
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001175inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001176 const SkIRect* srcRectPtr,
1177 int maxTextureSize) {
1178 static const int kSmallTileSize = 1 << 10;
1179 if (maxTextureSize <= kSmallTileSize) {
1180 return maxTextureSize;
1181 }
1182
1183 size_t maxTexTotalTileSize;
1184 size_t smallTotalTileSize;
1185
1186 if (NULL == srcRectPtr) {
1187 int w = bitmap.width();
1188 int h = bitmap.height();
1189 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1190 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1191 } else {
1192 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1193 srcRectPtr->fTop,
1194 srcRectPtr->fRight,
1195 srcRectPtr->fBottom,
1196 maxTextureSize);
1197 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1198 srcRectPtr->fTop,
1199 srcRectPtr->fRight,
1200 srcRectPtr->fBottom,
1201 kSmallTileSize);
1202 }
1203 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1204 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1205
1206 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1207 return kSmallTileSize;
1208 } else {
1209 return maxTextureSize;
1210 }
1211}
1212}
1213
1214bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1215 const GrSamplerState& sampler,
1216 const SkIRect* srcRectPtr,
1217 int* tileSize) const {
1218 SkASSERT(NULL != tileSize);
1219
1220 // if bitmap is explictly texture backed then just use the texture
1221 if (NULL != bitmap.getTexture()) {
1222 return false;
1223 }
1224 // if it's larger than the max texture size, then we have no choice but
1225 // tiling
1226 const int maxTextureSize = fContext->getMaxTextureSize();
1227 if (bitmap.width() > maxTextureSize ||
1228 bitmap.height() > maxTextureSize) {
1229 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1230 return true;
1231 }
1232 // if we are going to have to draw the whole thing, then don't tile
1233 if (NULL == srcRectPtr) {
1234 return false;
1235 }
1236 // if the entire texture is already in our cache then no reason to tile it
1237 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1238 return false;
1239 }
1240
1241 // At this point we know we could do the draw by uploading the entire bitmap
1242 // as a texture. However, if the texture would be large compared to the
1243 // cache size and we don't require most of it for this draw then tile to
1244 // reduce the amount of upload and cache spill.
1245
1246 // assumption here is that sw bitmap size is a good proxy for its size as
1247 // a texture
1248 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001249 size_t cacheSize;
1250 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001251 if (bmpSize < cacheSize / 2) {
1252 return false;
1253 }
1254
1255 SkFixed fracUsed =
1256 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1257 (srcRectPtr->height() << 16) / bitmap.height());
1258 if (fracUsed <= SK_FixedHalf) {
1259 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1260 return true;
1261 } else {
1262 return false;
1263 }
1264}
1265
reed@google.comac10a2d2010-12-22 21:39:39 +00001266void SkGpuDevice::drawBitmap(const SkDraw& draw,
1267 const SkBitmap& bitmap,
1268 const SkIRect* srcRectPtr,
1269 const SkMatrix& m,
1270 const SkPaint& paint) {
1271 CHECK_SHOULD_DRAW(draw);
1272
1273 SkIRect srcRect;
1274 if (NULL == srcRectPtr) {
1275 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1276 } else {
1277 srcRect = *srcRectPtr;
1278 }
1279
junov@google.comd935cfb2011-06-27 20:48:23 +00001280 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001281 // Convert the bitmap to a shader so that the rect can be drawn
1282 // through drawRect, which supports mask filters.
1283 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001284 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001285 if (srcRectPtr) {
1286 if (!bitmap.extractSubset(&tmp, srcRect)) {
1287 return; // extraction failed
1288 }
1289 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001290 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001291 }
1292 SkPaint paintWithTexture(paint);
1293 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1294 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001295 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001296 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001297
junov@google.com1d329782011-07-28 20:10:09 +00001298 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001299 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001300 // also affect the behavior of the mask filter.
1301 SkMatrix drawMatrix;
1302 drawMatrix.setConcat(*draw.fMatrix, m);
1303 SkDraw transformedDraw(draw);
1304 transformedDraw.fMatrix = &drawMatrix;
1305
1306 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1307
junov@google.comd935cfb2011-06-27 20:48:23 +00001308 return;
1309 }
1310
bsalomon@google.com5782d712011-01-21 21:03:59 +00001311 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001312 SkAutoCachedTexture colorLutTexture;
1313 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001314 return;
1315 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001316 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001317 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001318 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001319 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001320 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001321 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001322
bsalomon@google.comfb309512011-11-30 14:13:48 +00001323 int tileSize;
1324 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1325 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001326 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 return;
1328 }
1329
1330 // undo the translate done by SkCanvas
1331 int DX = SkMax32(0, srcRect.fLeft);
1332 int DY = SkMax32(0, srcRect.fTop);
1333 // compute clip bounds in local coordinates
1334 SkIRect clipRect;
1335 {
1336 SkRect r;
1337 r.set(draw.fClip->getBounds());
1338 SkMatrix matrix, inverse;
1339 matrix.setConcat(*draw.fMatrix, m);
1340 if (!matrix.invert(&inverse)) {
1341 return;
1342 }
1343 inverse.mapRect(&r);
1344 r.roundOut(&clipRect);
1345 // apply the canvas' translate to our local clip
1346 clipRect.offset(DX, DY);
1347 }
1348
bsalomon@google.comfb309512011-11-30 14:13:48 +00001349 int nx = bitmap.width() / tileSize;
1350 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001351 for (int x = 0; x <= nx; x++) {
1352 for (int y = 0; y <= ny; y++) {
1353 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001354 tileR.set(x * tileSize, y * tileSize,
1355 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001356 if (!SkIRect::Intersects(tileR, clipRect)) {
1357 continue;
1358 }
1359
1360 SkIRect srcR = tileR;
1361 if (!srcR.intersect(srcRect)) {
1362 continue;
1363 }
1364
1365 SkBitmap tmpB;
1366 if (bitmap.extractSubset(&tmpB, tileR)) {
1367 // now offset it to make it "local" to our tmp bitmap
1368 srcR.offset(-tileR.fLeft, -tileR.fTop);
1369
1370 SkMatrix tmpM(m);
1371 {
1372 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1373 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1374 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1375 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001376 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001377 }
1378 }
1379 }
1380}
1381
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001382namespace {
1383
1384bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1385 // detect pixel disalignment
1386 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1387 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1388 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1389 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1390 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1391 COLOR_BLEED_TOLERANCE &&
1392 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1393 COLOR_BLEED_TOLERANCE) {
1394 return true;
1395 }
1396 return false;
1397}
1398
1399bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1400 const SkMatrix& m) {
1401 // Only gets called if hasAlignedSamples returned false.
1402 // So we can assume that sampling is axis aligned but not texel aligned.
1403 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1404 SkRect innerSrcRect(srcRect), innerTransformedRect,
1405 outerTransformedRect(transformedRect);
1406 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1407 m.mapRect(&innerTransformedRect, innerSrcRect);
1408
1409 // The gap between outerTransformedRect and innerTransformedRect
1410 // represents the projection of the source border area, which is
1411 // problematic for color bleeding. We must check whether any
1412 // destination pixels sample the border area.
1413 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1414 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1415 SkIRect outer, inner;
1416 outerTransformedRect.round(&outer);
1417 innerTransformedRect.round(&inner);
1418 // If the inner and outer rects round to the same result, it means the
1419 // border does not overlap any pixel centers. Yay!
1420 return inner != outer;
1421}
1422
1423} // unnamed namespace
1424
reed@google.comac10a2d2010-12-22 21:39:39 +00001425/*
1426 * This is called by drawBitmap(), which has to handle images that may be too
1427 * large to be represented by a single texture.
1428 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001429 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1430 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001431 */
1432void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1433 const SkBitmap& bitmap,
1434 const SkIRect& srcRect,
1435 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001436 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001437 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1438 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001439
reed@google.com9c49bc32011-07-07 13:42:37 +00001440 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001441 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001442 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001443 return;
1444 }
1445
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001446 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001447
1448 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1449 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001450 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001451
1452 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001453 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001454 if (NULL == texture) {
1455 return;
1456 }
1457
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001458 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1459 (GrSingleTextureEffect, (texture)))->unref();
reed@google.com46799cd2011-02-22 20:56:26 +00001460
reed@google.com20efde72011-05-09 17:00:02 +00001461 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1462 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001463 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001464 float wInv = 1.f / bitmap.width();
1465 float hInv = 1.f / bitmap.height();
1466 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1467 SkFloatToScalar(srcRect.fTop * hInv),
1468 SkFloatToScalar(srcRect.fRight * wInv),
1469 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001470
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001471 bool needsTextureDomain = false;
1472 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1473 {
1474 // Need texture domain if drawing a sub rect.
1475 needsTextureDomain = srcRect.width() < bitmap.width() ||
1476 srcRect.height() < bitmap.height();
1477 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1478 // sampling is axis-aligned
1479 GrRect floatSrcRect, transformedRect;
1480 floatSrcRect.set(srcRect);
1481 SkMatrix srcToDeviceMatrix(m);
1482 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1483 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1484
1485 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1486 // Samples are texel-aligned, so filtering is futile
1487 sampler->setFilter(GrSamplerState::kNearest_Filter);
1488 needsTextureDomain = false;
1489 } else {
1490 needsTextureDomain = needsTextureDomain &&
1491 mayColorBleed(floatSrcRect, transformedRect, m);
1492 }
1493 }
1494 }
1495
1496 GrRect textureDomain = GrRect::MakeEmpty();
1497
1498 if (needsTextureDomain) {
1499 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001500 GrScalar left, top, right, bottom;
1501 if (srcRect.width() > 1) {
1502 GrScalar border = GR_ScalarHalf / bitmap.width();
1503 left = paintRect.left() + border;
1504 right = paintRect.right() - border;
1505 } else {
1506 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1507 }
1508 if (srcRect.height() > 1) {
1509 GrScalar border = GR_ScalarHalf / bitmap.height();
1510 top = paintRect.top() + border;
1511 bottom = paintRect.bottom() - border;
1512 } else {
1513 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1514 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001515 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001516 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1517 (texture,
1518 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001519 }
1520
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001521 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001522}
1523
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001524namespace {
1525
1526void apply_custom_stage(GrContext* context,
1527 GrTexture* srcTexture,
1528 GrTexture* dstTexture,
1529 const GrRect& rect,
1530 GrCustomStage* stage) {
1531 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001532 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001533 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001534 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001535
1536 GrMatrix sampleM;
1537 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1538 GrPaint paint;
1539 paint.reset();
1540 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1541 paint.textureSampler(0)->reset(sampleM);
1542 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001543 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001544}
1545
1546};
1547
reed@google.com8926b162012-03-23 15:36:36 +00001548static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1549 SkImageFilter* filter, const GrRect& rect) {
1550 GrAssert(filter);
1551
1552 SkSize blurSize;
1553 SkISize radius;
1554
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001555 GrTextureDesc desc;
1556 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1557 desc.fWidth = SkScalarCeilToInt(rect.width());
1558 desc.fHeight = SkScalarCeilToInt(rect.height());
1559 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001560 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001561
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001562 if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001563 GrAutoScratchTexture dst(context, desc);
1564 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1565 texture = dst.detach();
1566 stage->unref();
1567 } else if (filter->asABlur(&blurSize)) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001568 texture = context->gaussianBlur(texture, false, rect,
reed@google.com8926b162012-03-23 15:36:36 +00001569 blurSize.width(),
1570 blurSize.height());
reed@google.com8926b162012-03-23 15:36:36 +00001571 } else if (filter->asADilate(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001572 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001573 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001574 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001575 } else if (filter->asAnErode(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001576 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001577 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001578 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001579 }
1580 return texture;
1581}
1582
reed@google.comac10a2d2010-12-22 21:39:39 +00001583void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1584 int left, int top, const SkPaint& paint) {
1585 CHECK_SHOULD_DRAW(draw);
1586
reed@google.com8926b162012-03-23 15:36:36 +00001587 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001588 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1589 return;
1590 }
1591
reed@google.com76dd2772012-01-05 21:15:07 +00001592 int w = bitmap.width();
1593 int h = bitmap.height();
1594
bsalomon@google.com5782d712011-01-21 21:03:59 +00001595 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001596 SkAutoCachedTexture colorLutTexture;
1597 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001598 return;
1599 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001600
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001601 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001602
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001603 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001604
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001605 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001606 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001607 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001608 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1609 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001610
reed@google.com8926b162012-03-23 15:36:36 +00001611 SkImageFilter* filter = paint.getImageFilter();
1612 if (NULL != filter) {
1613 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001614 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001615 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001616 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1617 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001618 texture = filteredTexture;
1619 filteredTexture->unref();
1620 }
reed@google.com76dd2772012-01-05 21:15:07 +00001621 }
reed@google.com8926b162012-03-23 15:36:36 +00001622
bsalomon@google.com5782d712011-01-21 21:03:59 +00001623 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001624 GrRect::MakeXYWH(GrIntToScalar(left),
1625 GrIntToScalar(top),
1626 GrIntToScalar(w),
1627 GrIntToScalar(h)),
1628 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1629 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001630}
1631
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001632void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001633 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001634 // clear of the source device must occur before CHECK_SHOULD_DRAW
1635 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1636 if (dev->fNeedClear) {
1637 // TODO: could check here whether we really need to draw at all
1638 dev->clear(0x0);
1639 }
1640
reed@google.comac10a2d2010-12-22 21:39:39 +00001641 CHECK_SHOULD_DRAW(draw);
1642
bsalomon@google.com5782d712011-01-21 21:03:59 +00001643 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001644 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001645 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001646 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001647 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001648 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001649
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001650 GrTexture* devTex = grPaint.getTexture(0);
1651 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001652
reed@google.com8926b162012-03-23 15:36:36 +00001653 SkImageFilter* filter = paint.getImageFilter();
tomhudson@google.com2683a412012-07-20 19:15:06 +00001654 grPaint.textureSampler(kBitmapTextureIdx)->reset();
reed@google.com8926b162012-03-23 15:36:36 +00001655 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001656 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1657 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001658 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1659 rect);
1660 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001661 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1662 (GrSingleTextureEffect, (filteredTexture)))->unref();
tomhudson@google.com2683a412012-07-20 19:15:06 +00001663 grPaint.setTexture(kBitmapTextureIdx, NULL);
reed@google.com8926b162012-03-23 15:36:36 +00001664 devTex = filteredTexture;
1665 filteredTexture->unref();
1666 }
1667 }
1668
bsalomon@google.com5782d712011-01-21 21:03:59 +00001669 const SkBitmap& bm = dev->accessBitmap(false);
1670 int w = bm.width();
1671 int h = bm.height();
1672
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001673 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001674 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1675 GrIntToScalar(y),
1676 GrIntToScalar(w),
1677 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001678
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001679 // The device being drawn may not fill up its texture (saveLayer uses
1680 // the approximate ).
1681 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1682 GR_Scalar1 * h / devTex->height());
1683
1684 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001685}
1686
reed@google.com8926b162012-03-23 15:36:36 +00001687bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001688 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001689 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001690
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001691 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001692 !filter->asABlur(&size) &&
1693 !filter->asADilate(&radius) &&
1694 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001695 return false;
1696 }
reed@google.com8926b162012-03-23 15:36:36 +00001697 return true;
1698}
1699
1700bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1701 const SkMatrix& ctm,
1702 SkBitmap* result, SkIPoint* offset) {
1703 // want explicitly our impl, so guard against a subclass of us overriding it
1704 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001705 return false;
1706 }
reed@google.com8926b162012-03-23 15:36:36 +00001707
1708 SkAutoLockPixels alp(src, !src.getTexture());
1709 if (!src.getTexture() && !src.readyToDraw()) {
1710 return false;
1711 }
1712
1713 GrPaint paint;
1714 paint.reset();
1715
1716 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1717
1718 GrTexture* texture;
1719 SkAutoCachedTexture act(this, src, sampler, &texture);
1720
1721 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001722 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1723 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001724 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1725 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001726 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1727 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001728 resultTexture->unref();
1729 }
reed@google.com76dd2772012-01-05 21:15:07 +00001730 return true;
1731}
1732
reed@google.comac10a2d2010-12-22 21:39:39 +00001733///////////////////////////////////////////////////////////////////////////////
1734
1735// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001736static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001737 kTriangles_GrPrimitiveType,
1738 kTriangleStrip_GrPrimitiveType,
1739 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001740};
1741
1742void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1743 int vertexCount, const SkPoint vertices[],
1744 const SkPoint texs[], const SkColor colors[],
1745 SkXfermode* xmode,
1746 const uint16_t indices[], int indexCount,
1747 const SkPaint& paint) {
1748 CHECK_SHOULD_DRAW(draw);
1749
bsalomon@google.com5782d712011-01-21 21:03:59 +00001750 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001751 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001752 // we ignore the shader if texs is null.
1753 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001754 if (!skPaint2GrPaintNoShader(this,
1755 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001756 false,
1757 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001758 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001759 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001760 return;
1761 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001762 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001763 if (!skPaint2GrPaintShader(this,
1764 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001765 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001766 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001767 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001768 return;
1769 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001770 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001771
1772 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001773 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001774 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1775#if 0
1776 return
1777#endif
1778 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001779 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001780
bsalomon@google.com498776a2011-08-16 19:20:44 +00001781 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1782 if (NULL != colors) {
1783 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001784 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001785 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001786 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001787 }
1788 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001789 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001790 fContext->drawVertices(grPaint,
1791 gVertexMode2PrimitiveType[vmode],
1792 vertexCount,
1793 (GrPoint*) vertices,
1794 (GrPoint*) texs,
1795 colors,
1796 indices,
1797 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001798}
1799
1800///////////////////////////////////////////////////////////////////////////////
1801
1802static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001803 GrFontScaler* scaler = (GrFontScaler*)data;
1804 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001805}
1806
1807static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1808 void* auxData;
1809 GrFontScaler* scaler = NULL;
1810 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1811 scaler = (GrFontScaler*)auxData;
1812 }
1813 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001814 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001815 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1816 }
1817 return scaler;
1818}
1819
1820static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1821 SkFixed fx, SkFixed fy,
1822 const SkGlyph& glyph) {
1823 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1824
bungeman@google.com15865a72012-01-11 16:28:04 +00001825 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001826
1827 if (NULL == procs->fFontScaler) {
1828 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1829 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001830
bungeman@google.com15865a72012-01-11 16:28:04 +00001831 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1832 glyph.getSubXFixed(),
1833 glyph.getSubYFixed()),
1834 SkFixedFloorToFixed(fx),
1835 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001836 procs->fFontScaler);
1837}
1838
bsalomon@google.com5782d712011-01-21 21:03:59 +00001839SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001840
1841 // deferred allocation
1842 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001843 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001844 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1845 fDrawProcs->fContext = fContext;
1846 }
1847
1848 // init our (and GL's) state
1849 fDrawProcs->fTextContext = context;
1850 fDrawProcs->fFontScaler = NULL;
1851 return fDrawProcs;
1852}
1853
1854void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1855 size_t byteLength, SkScalar x, SkScalar y,
1856 const SkPaint& paint) {
1857 CHECK_SHOULD_DRAW(draw);
1858
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001859 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001860 // this guy will just call our drawPath()
1861 draw.drawText((const char*)text, byteLength, x, y, 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::drawText(myDraw, text, byteLength, x, y, paint);
1877 }
1878}
1879
1880void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1881 size_t byteLength, const SkScalar pos[],
1882 SkScalar constY, int scalarsPerPos,
1883 const SkPaint& paint) {
1884 CHECK_SHOULD_DRAW(draw);
1885
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001886 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001887 // this guy will just call our drawPath()
1888 draw.drawPosText((const char*)text, byteLength, pos, constY,
1889 scalarsPerPos, paint);
1890 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001891 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001892
1893 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001894 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001895 if (!skPaint2GrPaintShader(this,
1896 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001897 true,
twiz@google.com58071162012-07-18 21:41:50 +00001898 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001899 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001900 return;
1901 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001902 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1903 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001904 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1905 scalarsPerPos, paint);
1906 }
1907}
1908
1909void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1910 size_t len, const SkPath& path,
1911 const SkMatrix* m, const SkPaint& paint) {
1912 CHECK_SHOULD_DRAW(draw);
1913
1914 SkASSERT(draw.fDevice == this);
1915 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1916}
1917
1918///////////////////////////////////////////////////////////////////////////////
1919
reed@google.comf67e4cf2011-03-15 20:56:58 +00001920bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1921 if (!paint.isLCDRenderText()) {
1922 // we're cool with the paint as is
1923 return false;
1924 }
1925
1926 if (paint.getShader() ||
1927 paint.getXfermode() || // unless its srcover
1928 paint.getMaskFilter() ||
1929 paint.getRasterizer() ||
1930 paint.getColorFilter() ||
1931 paint.getPathEffect() ||
1932 paint.isFakeBoldText() ||
1933 paint.getStyle() != SkPaint::kFill_Style) {
1934 // turn off lcd
1935 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1936 flags->fHinting = paint.getHinting();
1937 return true;
1938 }
1939 // we're cool with the paint as is
1940 return false;
1941}
1942
reed@google.com75d939b2011-12-07 15:07:23 +00001943void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001944 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001945 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001946}
1947
reed@google.comf67e4cf2011-03-15 20:56:58 +00001948///////////////////////////////////////////////////////////////////////////////
1949
bsalomon@google.comfb309512011-11-30 14:13:48 +00001950bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1951 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001952 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001953 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001954
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001955 GrTextureDesc desc;
1956 desc.fWidth = bitmap.width();
1957 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001958 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001959 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001960
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001961 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001962}
1963
1964
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001965SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1966 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001967 bool isOpaque,
1968 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001969 GrTextureDesc desc;
1970 desc.fConfig = fRenderTarget->config();
1971 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1972 desc.fWidth = width;
1973 desc.fHeight = height;
1974 desc.fSampleCnt = fRenderTarget->numSamples();
1975
1976 GrContext::TextureCacheEntry cacheEntry;
1977 GrTexture* texture;
1978 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001979 // Skia's convention is to only clear a device if it is non-opaque.
1980 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001981
1982#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1983 // layers are never draw in repeat modes, so we can request an approx
1984 // match and ignore any padding.
1985 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1986 GrContext::kApprox_ScratchTexMatch :
1987 GrContext::kExact_ScratchTexMatch;
1988 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1989 texture = cacheEntry.texture();
1990#else
1991 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1992 texture = tunref.get();
1993#endif
1994 if (texture) {
1995 return SkNEW_ARGS(SkGpuDevice,(fContext,
1996 texture,
1997 cacheEntry,
1998 needClear));
1999 } else {
2000 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
2001 width, height);
2002 return NULL;
2003 }
2004}
2005
2006SkGpuDevice::SkGpuDevice(GrContext* context,
2007 GrTexture* texture,
2008 TexCache cacheEntry,
2009 bool needClear)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00002010 : SkDevice(make_bitmap(context, texture->asRenderTarget()))
2011 , fClipStack(NULL) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00002012 GrAssert(texture && texture->asRenderTarget());
2013 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
2014 this->initFromRenderTarget(context, texture->asRenderTarget());
2015 fCache = cacheEntry;
2016 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00002017}
2018