blob: 1dc17fb61095667a31aad8d85ebcd6c4c0f8d882 [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
reed@google.comac10a2d2010-12-22 21:39:39 +0000364///////////////////////////////////////////////////////////////////////////////
365
366static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000367 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000368 const SkRegion& clipRegion,
369 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000370 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000371
372 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000373 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000374 const SkIRect& skBounds = clipRegion.getBounds();
375 GrRect bounds;
376 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
377 GrIntToScalar(skBounds.fTop),
378 GrIntToScalar(skBounds.fRight),
379 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000380 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000381 bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000382 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000383}
384
385// call this ever each draw call, to ensure that the context reflects our state,
386// and not the state from some other canvas/device
387void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000388 GrAssert(NULL != fClipStack);
389
reed@google.comac10a2d2010-12-22 21:39:39 +0000390 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000391 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000392
393 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000394 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000395 convert_matrixclip(fContext, *draw.fMatrix,
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000396 *fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000397 fNeedPrepareRenderTarget = false;
398 }
399}
400
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000401void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
402 const SkClipStack& clipStack) {
403 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
404 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000405 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000406}
407
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000408void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
409
410 GrAssert(NULL != fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000411
reed@google.comac10a2d2010-12-22 21:39:39 +0000412 fContext->setRenderTarget(fRenderTarget);
413
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000414 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000415
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000416 convert_matrixclip(fContext, matrix, *fClipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000417
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000418 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000419}
420
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000421SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000422 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000423 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000424}
425
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000426bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000427 if (NULL != fTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000428 // FIXME: cannot use GrSingleTextureEffect here: fails
429 // assert in line 1617: null != devTex; generalizing GrPaint::getTexture()
430 // to grab textures off of GrCustomStages breaks gms in various ways -
431 // particularly since table color filter requires multiple textures
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000432 paint->setTexture(kBitmapTextureIdx, fTexture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000433 //paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
434 //SkNEW_ARGS(GrSingleTextureEffect, (fTexture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000435 return true;
436 }
437 return false;
438}
439
440///////////////////////////////////////////////////////////////////////////////
441
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000442SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
443SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
444SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
445SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
446SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
447 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000448SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
449 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000450SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
451SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000452
bsalomon@google.com84405e02012-03-05 19:57:21 +0000453namespace {
454
455// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
456// justAlpha indicates that skPaint's alpha should be used rather than the color
457// Callers may subsequently modify the GrPaint. Setting constantColor indicates
458// that the final paint will draw the same color at every pixel. This allows
459// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000460// color once while converting to GrPaint and then ignored.
461inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
462 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000463 bool justAlpha,
464 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000465 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000466 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000467
468 grPaint->fDither = skPaint.isDither();
469 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000470 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000471
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000472 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
473 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000474
475 SkXfermode* mode = skPaint.getXfermode();
476 if (mode) {
477 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000478 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000479#if 0
480 return false;
481#endif
482 }
483 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000484 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
485 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
486
bsalomon@google.com5782d712011-01-21 21:03:59 +0000487 if (justAlpha) {
488 uint8_t alpha = skPaint.getAlpha();
489 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000490 // justAlpha is currently set to true only if there is a texture,
491 // so constantColor should not also be true.
492 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000493 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000494 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000495 GrAssert(NULL == grPaint->getTexture(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000496 }
Scroggo97c88c22011-05-11 14:05:25 +0000497 SkColorFilter* colorFilter = skPaint.getColorFilter();
498 SkColor color;
499 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000500 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000501 SkBitmap colorTransformTable;
Scroggo97c88c22011-05-11 14:05:25 +0000502 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000503 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000504 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000505 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000506 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000507 } else {
508 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000509 grPaint->fColor = SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000510 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000511 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000512 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
513 grPaint->fColorMatrixEnabled = true;
514 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
515 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
twiz@google.com58071162012-07-18 21:41:50 +0000516 } else if (colorFilter != NULL && colorFilter->asComponentTable(
517 &colorTransformTable)) {
518 grPaint->resetColorFilter();
519
520 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
521 GrTexture* texture = act->set(dev, colorTransformTable, colorSampler);
522
bsalomon@google.com25f3e9b2012-07-20 14:23:09 +0000523 colorSampler->reset(GrSamplerState::kClamp_WrapMode, GrSamplerState::kNearest_Filter);
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000524 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000525 } else {
526 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000527 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000528 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000529}
530
bsalomon@google.com84405e02012-03-05 19:57:21 +0000531// This function is similar to skPaint2GrPaintNoShader but also converts
532// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
533// be used is set on grPaint and returned in param act. constantColor has the
534// same meaning as in skPaint2GrPaintNoShader.
535inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
536 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000537 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000538 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000539 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000540 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000541 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000542 return skPaint2GrPaintNoShader(dev,
543 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000544 false,
545 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000546 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000547 grPaint);
twiz@google.com58071162012-07-18 21:41:50 +0000548 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
549 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000550 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000551 }
552
reed@google.comac10a2d2010-12-22 21:39:39 +0000553 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000554 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000555 SkShader::TileMode tileModes[2];
556 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000557 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000558 tileModes, twoPointParams);
559
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000560 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000561 SkShader::GradientInfo info;
562 SkColor color;
563
564 info.fColors = &color;
565 info.fColorOffsets = NULL;
566 info.fColorCount = 1;
567 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
568 SkPaint copy(skPaint);
569 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000570 // modulate the paint alpha by the shader's solid color alpha
571 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
572 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000573 return skPaint2GrPaintNoShader(dev,
574 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000575 false,
576 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000577 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000578 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000579 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000580 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000581 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000582
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000583 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
twiz@google.com58071162012-07-18 21:41:50 +0000584 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, sampler);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000585 if (NULL == texture) {
586 SkDebugf("Couldn't convert bitmap to texture.\n");
587 return false;
588 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000589
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000590 switch (bmptype) {
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000591 case SkShader::kRadial_BitmapType:
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000592 sampler->setCustomStage(SkNEW_ARGS(GrRadialGradient, (texture)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000593 sampler->setFilter(GrSamplerState::kBilinear_Filter);
594 break;
595 case SkShader::kSweep_BitmapType:
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000596 sampler->setCustomStage(SkNEW_ARGS(GrSweepGradient, (texture)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000597 sampler->setFilter(GrSamplerState::kBilinear_Filter);
598 break;
599 case SkShader::kTwoPointRadial_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000600 sampler->setCustomStage(SkNEW_ARGS(GrRadial2Gradient,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000601 (texture,
602 twoPointParams[0],
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000603 twoPointParams[1],
604 twoPointParams[2] < 0)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000605 sampler->setFilter(GrSamplerState::kBilinear_Filter);
606 break;
rileya@google.com3e332582012-07-03 13:43:35 +0000607 case SkShader::kTwoPointConical_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000608 sampler->setCustomStage(SkNEW_ARGS(GrConical2Gradient,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000609 (texture,
610 twoPointParams[0],
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000611 twoPointParams[1],
612 twoPointParams[2])))->unref();
rileya@google.com3e332582012-07-03 13:43:35 +0000613 sampler->setFilter(GrSamplerState::kBilinear_Filter);
614 break;
rileya@google.com22e57f92012-07-19 15:16:19 +0000615 case SkShader::kLinear_BitmapType:
616 sampler->setCustomStage(SkNEW_ARGS(GrLinearGradient, (texture)))->unref();
rileya@google.com5ce42ca2012-07-19 19:42:10 +0000617 sampler->setFilter(GrSamplerState::kBilinear_Filter);
rileya@google.com22e57f92012-07-19 15:16:19 +0000618 break;
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000619 default:
620 if (skPaint.isFilterBitmap()) {
621 sampler->setFilter(GrSamplerState::kBilinear_Filter);
622 } else {
623 sampler->setFilter(GrSamplerState::kNearest_Filter);
624 }
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000625 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000626 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000627 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000628 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
629 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000630
reed@google.comac10a2d2010-12-22 21:39:39 +0000631 // since our texture coords will be in local space, we wack the texture
632 // matrix to map them back into 0...1 before we load it
633 SkMatrix localM;
634 if (shader->getLocalMatrix(&localM)) {
635 SkMatrix inverse;
636 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000637 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000638 }
639 }
640 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000641 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
642 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000643 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000644 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000645 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000646 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000647 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000648
649 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000650}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000651}
reed@google.comac10a2d2010-12-22 21:39:39 +0000652
653///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000654void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000655 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000656}
657
reed@google.comac10a2d2010-12-22 21:39:39 +0000658void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
659 CHECK_SHOULD_DRAW(draw);
660
bsalomon@google.com5782d712011-01-21 21:03:59 +0000661 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000662 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000663 if (!skPaint2GrPaintShader(this,
664 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000665 true,
twiz@google.com58071162012-07-18 21:41:50 +0000666 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000667 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000668 return;
669 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000670
671 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000672}
673
674// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000675static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000676 kPoints_GrPrimitiveType,
677 kLines_GrPrimitiveType,
678 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000679};
680
681void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000682 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000683 CHECK_SHOULD_DRAW(draw);
684
685 SkScalar width = paint.getStrokeWidth();
686 if (width < 0) {
687 return;
688 }
689
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000690 // we only handle hairlines and paints without path effects or mask filters,
691 // else we let the SkDraw call our drawPath()
692 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000693 draw.drawPoints(mode, count, pts, paint, true);
694 return;
695 }
696
bsalomon@google.com5782d712011-01-21 21:03:59 +0000697 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000698 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000699 if (!skPaint2GrPaintShader(this,
700 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000701 true,
twiz@google.com58071162012-07-18 21:41:50 +0000702 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000703 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000704 return;
705 }
706
bsalomon@google.com5782d712011-01-21 21:03:59 +0000707 fContext->drawVertices(grPaint,
708 gPointMode2PrimtiveType[mode],
709 count,
710 (GrPoint*)pts,
711 NULL,
712 NULL,
713 NULL,
714 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000715}
716
reed@google.comc9aa5872011-04-05 21:05:37 +0000717///////////////////////////////////////////////////////////////////////////////
718
reed@google.comac10a2d2010-12-22 21:39:39 +0000719void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
720 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000721 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000722 CHECK_SHOULD_DRAW(draw);
723
bungeman@google.com79bd8772011-07-18 15:34:08 +0000724 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000725 SkScalar width = paint.getStrokeWidth();
726
727 /*
728 We have special code for hairline strokes, miter-strokes, and fills.
729 Anything else we just call our path code.
730 */
731 bool usePath = doStroke && width > 0 &&
732 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000733 // another two reasons we might need to call drawPath...
734 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000735 usePath = true;
736 }
reed@google.com67db6642011-05-26 11:46:35 +0000737 // until we aa rotated rects...
738 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
739 usePath = true;
740 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000741 // small miter limit means right angles show bevel...
742 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
743 paint.getStrokeMiter() < SK_ScalarSqrt2)
744 {
745 usePath = true;
746 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000747 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000748 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
749 usePath = true;
750 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000751
752 if (usePath) {
753 SkPath path;
754 path.addRect(rect);
755 this->drawPath(draw, path, paint, NULL, true);
756 return;
757 }
758
759 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000760 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000761 if (!skPaint2GrPaintShader(this,
762 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000763 true,
twiz@google.com58071162012-07-18 21:41:50 +0000764 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000765 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000766 return;
767 }
reed@google.com20efde72011-05-09 17:00:02 +0000768 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000769}
770
reed@google.com69302852011-02-16 18:08:07 +0000771#include "SkMaskFilter.h"
772#include "SkBounder.h"
773
bsalomon@google.com85003222012-03-28 14:44:37 +0000774///////////////////////////////////////////////////////////////////////////////
775
776// helpers for applying mask filters
777namespace {
778
779GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000780 switch (fillType) {
781 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000782 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000783 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000784 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000785 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000786 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000787 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000788 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000789 default:
790 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000791 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000792 }
793}
794
bsalomon@google.com85003222012-03-28 14:44:37 +0000795// We prefer to blur small rect with small radius via CPU.
796#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
797#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
798inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
799 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
800 rect.height() <= MIN_GPU_BLUR_SIZE &&
801 radius <= MIN_GPU_BLUR_RADIUS) {
802 return true;
803 }
804 return false;
805}
806
807bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
808 SkMaskFilter* filter, const SkMatrix& matrix,
809 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000810 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000811#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000812 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000813#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000814 SkMaskFilter::BlurInfo info;
815 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000816 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000817 return false;
818 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000819 SkScalar radius = info.fIgnoreTransform ? info.fRadius
820 : matrix.mapRadius(info.fRadius);
821 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000822 if (radius <= 0) {
823 return false;
824 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000825
826 SkRect srcRect = path.getBounds();
827 if (shouldDrawBlurWithCPU(srcRect, radius)) {
828 return false;
829 }
830
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000831 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000832 float sigma3 = sigma * 3.0f;
833
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000834 SkRect clipRect;
835 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000836
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000837 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000838 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
839 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000840 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000841 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000842 SkIRect finalIRect;
843 finalRect.roundOut(&finalIRect);
844 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000845 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000846 }
847 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000848 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000849 }
850 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000851 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000852 GrTextureDesc desc;
853 desc.fFlags = kRenderTarget_GrTextureFlagBit;
854 desc.fWidth = SkScalarCeilToInt(srcRect.width());
855 desc.fHeight = SkScalarCeilToInt(srcRect.height());
856 // We actually only need A8, but it often isn't supported as a
857 // render target so default to RGBA_8888
858 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000859
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000860 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
861 desc.fConfig = kAlpha_8_GrPixelConfig;
862 }
863
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000864 GrAutoScratchTexture pathEntry(context, desc);
865 GrTexture* pathTexture = pathEntry.texture();
866 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000867 return false;
868 }
869 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000870 // Once this code moves into GrContext, this should be changed to use
871 // an AutoClipRestore.
872 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000873 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000874
875 GrClip newClip(srcRect);
876 context->setClip(newClip);
877
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000878 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000879 GrPaint tempPaint;
880 tempPaint.reset();
881
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000882 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000883 tempPaint.fAntiAlias = grp->fAntiAlias;
884 if (tempPaint.fAntiAlias) {
885 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
886 // blend coeff of zero requires dual source blending support in order
887 // to properly blend partially covered pixels. This means the AA
888 // code path may not be taken. So we use a dst blend coeff of ISA. We
889 // could special case AA draws to a dst surface with known alpha=0 to
890 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000891 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
892 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000893 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000894 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000895 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000896
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000897 // If we're doing a normal blur, we can clobber the pathTexture in the
898 // gaussianBlur. Otherwise, we need to save it for later compositing.
899 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000900 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
901 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000902
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000903 if (!isNormalBlur) {
904 GrPaint paint;
905 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000906 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000907 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
908 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000909 // Blend pathTexture over blurTexture.
910 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000911 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
912 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000913 if (SkMaskFilter::kInner_BlurType == blurType) {
914 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000915 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
916 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000917 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
918 // solid: dst = src + dst - src * dst
919 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000920 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
921 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000922 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
923 // outer: dst = dst * (1 - src)
924 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000925 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
926 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000927 }
928 context->drawRect(paint, srcRect);
929 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000930 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000931 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000932
bsalomon@google.come3d32162012-07-20 13:37:06 +0000933 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
934 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000935 }
936
937 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
938 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000939 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000940 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000941 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000942
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000943 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
944 -finalRect.fTop);
945 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
946 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000947 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000948 return true;
949}
950
bsalomon@google.com85003222012-03-28 14:44:37 +0000951bool drawWithMaskFilter(GrContext* context, const SkPath& path,
952 SkMaskFilter* filter, const SkMatrix& matrix,
953 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000954 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000955 SkMask srcM, dstM;
956
957 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000958 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000959 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000960 return false;
961 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000962 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000963
964 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
965 return false;
966 }
967 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000968 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000969
970 if (clip.quickReject(dstM.fBounds)) {
971 return false;
972 }
973 if (bounder && !bounder->doIRect(dstM.fBounds)) {
974 return false;
975 }
976
977 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
978 // the current clip (and identity matrix) and grpaint settings
979
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000980 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000981
bsalomon@google.come3d32162012-07-20 13:37:06 +0000982 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
983 return false;
984 }
985
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000986 GrTextureDesc desc;
987 desc.fWidth = dstM.fBounds.width();
988 desc.fHeight = dstM.fBounds.height();
989 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000990
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000991 GrAutoScratchTexture ast(context, desc);
992 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000993
reed@google.com69302852011-02-16 18:08:07 +0000994 if (NULL == texture) {
995 return false;
996 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000997 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000998 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000999
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001000 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1001 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001002 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001003 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +00001004 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +00001005
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001006 GrRect d;
1007 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001008 GrIntToScalar(dstM.fBounds.fTop),
1009 GrIntToScalar(dstM.fBounds.fRight),
1010 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001011
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001012 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1013 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1014 -dstM.fBounds.fTop*SK_Scalar1);
1015 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001016 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001017 return true;
1018}
reed@google.com69302852011-02-16 18:08:07 +00001019
bsalomon@google.com85003222012-03-28 14:44:37 +00001020}
1021
1022///////////////////////////////////////////////////////////////////////////////
1023
reed@google.com0c219b62011-02-16 21:31:18 +00001024void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001025 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001026 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001027 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001028 CHECK_SHOULD_DRAW(draw);
1029
reed@google.comfe626382011-09-21 13:50:35 +00001030 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001031
bsalomon@google.com5782d712011-01-21 21:03:59 +00001032 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001033 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001034 if (!skPaint2GrPaintShader(this,
1035 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001036 true,
twiz@google.com58071162012-07-18 21:41:50 +00001037 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001038 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001039 return;
1040 }
1041
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001042 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1043 // if we can, we draw lots faster (raster device does this same test)
1044 SkScalar hairlineCoverage;
1045 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1046 doFill = false;
1047 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1048 grPaint.fCoverage);
1049 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001050
reed@google.comfe626382011-09-21 13:50:35 +00001051 // If we have a prematrix, apply it to the path, optimizing for the case
1052 // where the original path can in fact be modified in place (even though
1053 // its parameter type is const).
1054 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1055 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001056
1057 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001058 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001059
reed@google.come3445642011-02-16 23:20:39 +00001060 if (!pathIsMutable) {
1061 result = &tmpPath;
1062 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001063 }
reed@google.come3445642011-02-16 23:20:39 +00001064 // should I push prePathMatrix on our MV stack temporarily, instead
1065 // of applying it here? See SkDraw.cpp
1066 pathPtr->transform(*prePathMatrix, result);
1067 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001068 }
reed@google.com0c219b62011-02-16 21:31:18 +00001069 // at this point we're done with prePathMatrix
1070 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001071
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001072 if (paint.getPathEffect() ||
1073 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001074 // it is safe to use tmpPath here, even if we already used it for the
1075 // prepathmatrix, since getFillPath can take the same object for its
1076 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001077 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001078 pathPtr = &tmpPath;
1079 }
1080
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001081 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001082 // avoid possibly allocating a new path in transform if we can
1083 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1084
1085 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001086 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001087 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001088 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001089 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001090 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001091 &grPaint, pathFillType)) {
1092 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1093 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001094 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001095 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001096 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001097 }
reed@google.com69302852011-02-16 18:08:07 +00001098 return;
1099 }
reed@google.com69302852011-02-16 18:08:07 +00001100
bsalomon@google.com47059542012-06-06 20:51:20 +00001101 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001102
reed@google.com0c219b62011-02-16 21:31:18 +00001103 if (doFill) {
1104 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001105 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001106 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001107 break;
1108 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001109 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001110 break;
1111 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001112 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001113 break;
1114 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001115 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001116 break;
1117 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001118 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001119 return;
1120 }
1121 }
1122
reed@google.com07f3ee12011-05-16 17:21:57 +00001123 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001124}
1125
bsalomon@google.comfb309512011-11-30 14:13:48 +00001126namespace {
1127
1128inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1129 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1130 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1131 return tilesX * tilesY;
1132}
1133
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001134inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001135 const SkIRect* srcRectPtr,
1136 int maxTextureSize) {
1137 static const int kSmallTileSize = 1 << 10;
1138 if (maxTextureSize <= kSmallTileSize) {
1139 return maxTextureSize;
1140 }
1141
1142 size_t maxTexTotalTileSize;
1143 size_t smallTotalTileSize;
1144
1145 if (NULL == srcRectPtr) {
1146 int w = bitmap.width();
1147 int h = bitmap.height();
1148 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1149 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1150 } else {
1151 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1152 srcRectPtr->fTop,
1153 srcRectPtr->fRight,
1154 srcRectPtr->fBottom,
1155 maxTextureSize);
1156 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1157 srcRectPtr->fTop,
1158 srcRectPtr->fRight,
1159 srcRectPtr->fBottom,
1160 kSmallTileSize);
1161 }
1162 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1163 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1164
1165 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1166 return kSmallTileSize;
1167 } else {
1168 return maxTextureSize;
1169 }
1170}
1171}
1172
1173bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1174 const GrSamplerState& sampler,
1175 const SkIRect* srcRectPtr,
1176 int* tileSize) const {
1177 SkASSERT(NULL != tileSize);
1178
1179 // if bitmap is explictly texture backed then just use the texture
1180 if (NULL != bitmap.getTexture()) {
1181 return false;
1182 }
1183 // if it's larger than the max texture size, then we have no choice but
1184 // tiling
1185 const int maxTextureSize = fContext->getMaxTextureSize();
1186 if (bitmap.width() > maxTextureSize ||
1187 bitmap.height() > maxTextureSize) {
1188 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1189 return true;
1190 }
1191 // if we are going to have to draw the whole thing, then don't tile
1192 if (NULL == srcRectPtr) {
1193 return false;
1194 }
1195 // if the entire texture is already in our cache then no reason to tile it
1196 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1197 return false;
1198 }
1199
1200 // At this point we know we could do the draw by uploading the entire bitmap
1201 // as a texture. However, if the texture would be large compared to the
1202 // cache size and we don't require most of it for this draw then tile to
1203 // reduce the amount of upload and cache spill.
1204
1205 // assumption here is that sw bitmap size is a good proxy for its size as
1206 // a texture
1207 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001208 size_t cacheSize;
1209 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001210 if (bmpSize < cacheSize / 2) {
1211 return false;
1212 }
1213
1214 SkFixed fracUsed =
1215 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1216 (srcRectPtr->height() << 16) / bitmap.height());
1217 if (fracUsed <= SK_FixedHalf) {
1218 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1219 return true;
1220 } else {
1221 return false;
1222 }
1223}
1224
reed@google.comac10a2d2010-12-22 21:39:39 +00001225void SkGpuDevice::drawBitmap(const SkDraw& draw,
1226 const SkBitmap& bitmap,
1227 const SkIRect* srcRectPtr,
1228 const SkMatrix& m,
1229 const SkPaint& paint) {
1230 CHECK_SHOULD_DRAW(draw);
1231
1232 SkIRect srcRect;
1233 if (NULL == srcRectPtr) {
1234 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1235 } else {
1236 srcRect = *srcRectPtr;
1237 }
1238
junov@google.comd935cfb2011-06-27 20:48:23 +00001239 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001240 // Convert the bitmap to a shader so that the rect can be drawn
1241 // through drawRect, which supports mask filters.
1242 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001243 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001244 if (srcRectPtr) {
1245 if (!bitmap.extractSubset(&tmp, srcRect)) {
1246 return; // extraction failed
1247 }
1248 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001249 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001250 }
1251 SkPaint paintWithTexture(paint);
1252 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1253 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001254 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001255 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001256
junov@google.com1d329782011-07-28 20:10:09 +00001257 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001258 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001259 // also affect the behavior of the mask filter.
1260 SkMatrix drawMatrix;
1261 drawMatrix.setConcat(*draw.fMatrix, m);
1262 SkDraw transformedDraw(draw);
1263 transformedDraw.fMatrix = &drawMatrix;
1264
1265 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1266
junov@google.comd935cfb2011-06-27 20:48:23 +00001267 return;
1268 }
1269
bsalomon@google.com5782d712011-01-21 21:03:59 +00001270 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001271 SkAutoCachedTexture colorLutTexture;
1272 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001273 return;
1274 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001275 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001276 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001277 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001278 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001279 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001280 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001281
bsalomon@google.comfb309512011-11-30 14:13:48 +00001282 int tileSize;
1283 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1284 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001285 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 return;
1287 }
1288
1289 // undo the translate done by SkCanvas
1290 int DX = SkMax32(0, srcRect.fLeft);
1291 int DY = SkMax32(0, srcRect.fTop);
1292 // compute clip bounds in local coordinates
1293 SkIRect clipRect;
1294 {
1295 SkRect r;
1296 r.set(draw.fClip->getBounds());
1297 SkMatrix matrix, inverse;
1298 matrix.setConcat(*draw.fMatrix, m);
1299 if (!matrix.invert(&inverse)) {
1300 return;
1301 }
1302 inverse.mapRect(&r);
1303 r.roundOut(&clipRect);
1304 // apply the canvas' translate to our local clip
1305 clipRect.offset(DX, DY);
1306 }
1307
bsalomon@google.comfb309512011-11-30 14:13:48 +00001308 int nx = bitmap.width() / tileSize;
1309 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001310 for (int x = 0; x <= nx; x++) {
1311 for (int y = 0; y <= ny; y++) {
1312 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001313 tileR.set(x * tileSize, y * tileSize,
1314 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001315 if (!SkIRect::Intersects(tileR, clipRect)) {
1316 continue;
1317 }
1318
1319 SkIRect srcR = tileR;
1320 if (!srcR.intersect(srcRect)) {
1321 continue;
1322 }
1323
1324 SkBitmap tmpB;
1325 if (bitmap.extractSubset(&tmpB, tileR)) {
1326 // now offset it to make it "local" to our tmp bitmap
1327 srcR.offset(-tileR.fLeft, -tileR.fTop);
1328
1329 SkMatrix tmpM(m);
1330 {
1331 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1332 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1333 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1334 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001335 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001336 }
1337 }
1338 }
1339}
1340
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001341namespace {
1342
1343bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1344 // detect pixel disalignment
1345 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1346 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1347 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1348 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1349 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1350 COLOR_BLEED_TOLERANCE &&
1351 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1352 COLOR_BLEED_TOLERANCE) {
1353 return true;
1354 }
1355 return false;
1356}
1357
1358bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1359 const SkMatrix& m) {
1360 // Only gets called if hasAlignedSamples returned false.
1361 // So we can assume that sampling is axis aligned but not texel aligned.
1362 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1363 SkRect innerSrcRect(srcRect), innerTransformedRect,
1364 outerTransformedRect(transformedRect);
1365 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1366 m.mapRect(&innerTransformedRect, innerSrcRect);
1367
1368 // The gap between outerTransformedRect and innerTransformedRect
1369 // represents the projection of the source border area, which is
1370 // problematic for color bleeding. We must check whether any
1371 // destination pixels sample the border area.
1372 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1373 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1374 SkIRect outer, inner;
1375 outerTransformedRect.round(&outer);
1376 innerTransformedRect.round(&inner);
1377 // If the inner and outer rects round to the same result, it means the
1378 // border does not overlap any pixel centers. Yay!
1379 return inner != outer;
1380}
1381
1382} // unnamed namespace
1383
reed@google.comac10a2d2010-12-22 21:39:39 +00001384/*
1385 * This is called by drawBitmap(), which has to handle images that may be too
1386 * large to be represented by a single texture.
1387 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001388 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1389 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001390 */
1391void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1392 const SkBitmap& bitmap,
1393 const SkIRect& srcRect,
1394 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001395 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001396 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1397 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001398
reed@google.com9c49bc32011-07-07 13:42:37 +00001399 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001400 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001401 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001402 return;
1403 }
1404
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001405 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001406
1407 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1408 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001409 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001410
1411 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001412 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001413 if (NULL == texture) {
1414 return;
1415 }
1416
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001417 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1418 (GrSingleTextureEffect, (texture)))->unref();
reed@google.com46799cd2011-02-22 20:56:26 +00001419
reed@google.com20efde72011-05-09 17:00:02 +00001420 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1421 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001422 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001423 float wInv = 1.f / bitmap.width();
1424 float hInv = 1.f / bitmap.height();
1425 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1426 SkFloatToScalar(srcRect.fTop * hInv),
1427 SkFloatToScalar(srcRect.fRight * wInv),
1428 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001429
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001430 bool needsTextureDomain = false;
1431 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1432 {
1433 // Need texture domain if drawing a sub rect.
1434 needsTextureDomain = srcRect.width() < bitmap.width() ||
1435 srcRect.height() < bitmap.height();
1436 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1437 // sampling is axis-aligned
1438 GrRect floatSrcRect, transformedRect;
1439 floatSrcRect.set(srcRect);
1440 SkMatrix srcToDeviceMatrix(m);
1441 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1442 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1443
1444 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1445 // Samples are texel-aligned, so filtering is futile
1446 sampler->setFilter(GrSamplerState::kNearest_Filter);
1447 needsTextureDomain = false;
1448 } else {
1449 needsTextureDomain = needsTextureDomain &&
1450 mayColorBleed(floatSrcRect, transformedRect, m);
1451 }
1452 }
1453 }
1454
1455 GrRect textureDomain = GrRect::MakeEmpty();
1456
1457 if (needsTextureDomain) {
1458 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001459 GrScalar left, top, right, bottom;
1460 if (srcRect.width() > 1) {
1461 GrScalar border = GR_ScalarHalf / bitmap.width();
1462 left = paintRect.left() + border;
1463 right = paintRect.right() - border;
1464 } else {
1465 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1466 }
1467 if (srcRect.height() > 1) {
1468 GrScalar border = GR_ScalarHalf / bitmap.height();
1469 top = paintRect.top() + border;
1470 bottom = paintRect.bottom() - border;
1471 } else {
1472 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1473 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001474 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001475 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1476 (texture,
1477 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001478 }
1479
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001480 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001481}
1482
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001483namespace {
1484
1485void apply_custom_stage(GrContext* context,
1486 GrTexture* srcTexture,
1487 GrTexture* dstTexture,
1488 const GrRect& rect,
1489 GrCustomStage* stage) {
1490 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001491 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001492 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001493 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001494
1495 GrMatrix sampleM;
1496 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1497 GrPaint paint;
1498 paint.reset();
1499 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1500 paint.textureSampler(0)->reset(sampleM);
1501 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001502 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001503}
1504
1505};
1506
reed@google.com8926b162012-03-23 15:36:36 +00001507static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1508 SkImageFilter* filter, const GrRect& rect) {
1509 GrAssert(filter);
1510
1511 SkSize blurSize;
1512 SkISize radius;
1513
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001514 GrTextureDesc desc;
1515 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1516 desc.fWidth = SkScalarCeilToInt(rect.width());
1517 desc.fHeight = SkScalarCeilToInt(rect.height());
1518 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001519 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001520
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001521 if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001522 GrAutoScratchTexture dst(context, desc);
1523 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1524 texture = dst.detach();
1525 stage->unref();
1526 } else if (filter->asABlur(&blurSize)) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001527 texture = context->gaussianBlur(texture, false, rect,
reed@google.com8926b162012-03-23 15:36:36 +00001528 blurSize.width(),
1529 blurSize.height());
reed@google.com8926b162012-03-23 15:36:36 +00001530 } else if (filter->asADilate(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001531 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001532 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001533 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001534 } else if (filter->asAnErode(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001535 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001536 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001537 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001538 }
1539 return texture;
1540}
1541
reed@google.comac10a2d2010-12-22 21:39:39 +00001542void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1543 int left, int top, const SkPaint& paint) {
1544 CHECK_SHOULD_DRAW(draw);
1545
reed@google.com8926b162012-03-23 15:36:36 +00001546 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001547 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1548 return;
1549 }
1550
reed@google.com76dd2772012-01-05 21:15:07 +00001551 int w = bitmap.width();
1552 int h = bitmap.height();
1553
bsalomon@google.com5782d712011-01-21 21:03:59 +00001554 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001555 SkAutoCachedTexture colorLutTexture;
1556 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001557 return;
1558 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001559
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001560 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001561
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001562 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001563
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001564 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001565 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001566 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001567 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1568 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001569
reed@google.com8926b162012-03-23 15:36:36 +00001570 SkImageFilter* filter = paint.getImageFilter();
1571 if (NULL != filter) {
1572 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001573 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001574 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001575 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1576 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001577 texture = filteredTexture;
1578 filteredTexture->unref();
1579 }
reed@google.com76dd2772012-01-05 21:15:07 +00001580 }
reed@google.com8926b162012-03-23 15:36:36 +00001581
bsalomon@google.com5782d712011-01-21 21:03:59 +00001582 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001583 GrRect::MakeXYWH(GrIntToScalar(left),
1584 GrIntToScalar(top),
1585 GrIntToScalar(w),
1586 GrIntToScalar(h)),
1587 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1588 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001589}
1590
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001591void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001592 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001593 // clear of the source device must occur before CHECK_SHOULD_DRAW
1594 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1595 if (dev->fNeedClear) {
1596 // TODO: could check here whether we really need to draw at all
1597 dev->clear(0x0);
1598 }
1599
reed@google.comac10a2d2010-12-22 21:39:39 +00001600 CHECK_SHOULD_DRAW(draw);
1601
bsalomon@google.com5782d712011-01-21 21:03:59 +00001602 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001603 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001604 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001605 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001606 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001607 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001608
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001609 GrTexture* devTex = grPaint.getTexture(0);
1610 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001611
reed@google.com8926b162012-03-23 15:36:36 +00001612 SkImageFilter* filter = paint.getImageFilter();
1613 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001614 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1615 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001616 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1617 rect);
1618 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001619 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1620 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001621 devTex = filteredTexture;
1622 filteredTexture->unref();
1623 }
1624 }
1625
bsalomon@google.com5782d712011-01-21 21:03:59 +00001626 const SkBitmap& bm = dev->accessBitmap(false);
1627 int w = bm.width();
1628 int h = bm.height();
1629
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001630 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001631
bsalomon@google.com97912912011-12-06 16:30:36 +00001632 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001633
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001634 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1635 GrIntToScalar(y),
1636 GrIntToScalar(w),
1637 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001638
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001639 // The device being drawn may not fill up its texture (saveLayer uses
1640 // the approximate ).
1641 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1642 GR_Scalar1 * h / devTex->height());
1643
1644 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001645}
1646
reed@google.com8926b162012-03-23 15:36:36 +00001647bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001648 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001649 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001650
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001651 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001652 !filter->asABlur(&size) &&
1653 !filter->asADilate(&radius) &&
1654 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001655 return false;
1656 }
reed@google.com8926b162012-03-23 15:36:36 +00001657 return true;
1658}
1659
1660bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1661 const SkMatrix& ctm,
1662 SkBitmap* result, SkIPoint* offset) {
1663 // want explicitly our impl, so guard against a subclass of us overriding it
1664 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001665 return false;
1666 }
reed@google.com8926b162012-03-23 15:36:36 +00001667
1668 SkAutoLockPixels alp(src, !src.getTexture());
1669 if (!src.getTexture() && !src.readyToDraw()) {
1670 return false;
1671 }
1672
1673 GrPaint paint;
1674 paint.reset();
1675
1676 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1677
1678 GrTexture* texture;
1679 SkAutoCachedTexture act(this, src, sampler, &texture);
1680
1681 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001682 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1683 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001684 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1685 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001686 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1687 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001688 resultTexture->unref();
1689 }
reed@google.com76dd2772012-01-05 21:15:07 +00001690 return true;
1691}
1692
reed@google.comac10a2d2010-12-22 21:39:39 +00001693///////////////////////////////////////////////////////////////////////////////
1694
1695// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001696static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001697 kTriangles_GrPrimitiveType,
1698 kTriangleStrip_GrPrimitiveType,
1699 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001700};
1701
1702void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1703 int vertexCount, const SkPoint vertices[],
1704 const SkPoint texs[], const SkColor colors[],
1705 SkXfermode* xmode,
1706 const uint16_t indices[], int indexCount,
1707 const SkPaint& paint) {
1708 CHECK_SHOULD_DRAW(draw);
1709
bsalomon@google.com5782d712011-01-21 21:03:59 +00001710 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001711 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001712 // we ignore the shader if texs is null.
1713 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001714 if (!skPaint2GrPaintNoShader(this,
1715 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001716 false,
1717 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001718 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001719 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001720 return;
1721 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001722 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001723 if (!skPaint2GrPaintShader(this,
1724 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001725 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001726 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001727 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001728 return;
1729 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001730 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001731
1732 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001733 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001734 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1735#if 0
1736 return
1737#endif
1738 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001739 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001740
bsalomon@google.com498776a2011-08-16 19:20:44 +00001741 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1742 if (NULL != colors) {
1743 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001744 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001745 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001746 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001747 }
1748 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001749 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001750 fContext->drawVertices(grPaint,
1751 gVertexMode2PrimitiveType[vmode],
1752 vertexCount,
1753 (GrPoint*) vertices,
1754 (GrPoint*) texs,
1755 colors,
1756 indices,
1757 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001758}
1759
1760///////////////////////////////////////////////////////////////////////////////
1761
1762static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001763 GrFontScaler* scaler = (GrFontScaler*)data;
1764 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001765}
1766
1767static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1768 void* auxData;
1769 GrFontScaler* scaler = NULL;
1770 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1771 scaler = (GrFontScaler*)auxData;
1772 }
1773 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001774 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001775 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1776 }
1777 return scaler;
1778}
1779
1780static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1781 SkFixed fx, SkFixed fy,
1782 const SkGlyph& glyph) {
1783 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1784
bungeman@google.com15865a72012-01-11 16:28:04 +00001785 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001786
1787 if (NULL == procs->fFontScaler) {
1788 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1789 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001790
bungeman@google.com15865a72012-01-11 16:28:04 +00001791 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1792 glyph.getSubXFixed(),
1793 glyph.getSubYFixed()),
1794 SkFixedFloorToFixed(fx),
1795 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001796 procs->fFontScaler);
1797}
1798
bsalomon@google.com5782d712011-01-21 21:03:59 +00001799SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001800
1801 // deferred allocation
1802 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001803 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001804 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1805 fDrawProcs->fContext = fContext;
1806 }
1807
1808 // init our (and GL's) state
1809 fDrawProcs->fTextContext = context;
1810 fDrawProcs->fFontScaler = NULL;
1811 return fDrawProcs;
1812}
1813
1814void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1815 size_t byteLength, SkScalar x, SkScalar y,
1816 const SkPaint& paint) {
1817 CHECK_SHOULD_DRAW(draw);
1818
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001819 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001820 // this guy will just call our drawPath()
1821 draw.drawText((const char*)text, byteLength, x, y, paint);
1822 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001823 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001824
1825 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001826 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001827 if (!skPaint2GrPaintShader(this,
1828 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001829 true,
twiz@google.com58071162012-07-18 21:41:50 +00001830 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001831 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001832 return;
1833 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001834 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1835 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001836 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1837 }
1838}
1839
1840void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1841 size_t byteLength, const SkScalar pos[],
1842 SkScalar constY, int scalarsPerPos,
1843 const SkPaint& paint) {
1844 CHECK_SHOULD_DRAW(draw);
1845
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001846 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001847 // this guy will just call our drawPath()
1848 draw.drawPosText((const char*)text, byteLength, pos, constY,
1849 scalarsPerPos, paint);
1850 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001851 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001852
1853 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001854 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001855 if (!skPaint2GrPaintShader(this,
1856 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001857 true,
twiz@google.com58071162012-07-18 21:41:50 +00001858 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001859 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001860 return;
1861 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001862 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1863 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001864 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1865 scalarsPerPos, paint);
1866 }
1867}
1868
1869void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1870 size_t len, const SkPath& path,
1871 const SkMatrix* m, const SkPaint& paint) {
1872 CHECK_SHOULD_DRAW(draw);
1873
1874 SkASSERT(draw.fDevice == this);
1875 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1876}
1877
1878///////////////////////////////////////////////////////////////////////////////
1879
reed@google.comf67e4cf2011-03-15 20:56:58 +00001880bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1881 if (!paint.isLCDRenderText()) {
1882 // we're cool with the paint as is
1883 return false;
1884 }
1885
1886 if (paint.getShader() ||
1887 paint.getXfermode() || // unless its srcover
1888 paint.getMaskFilter() ||
1889 paint.getRasterizer() ||
1890 paint.getColorFilter() ||
1891 paint.getPathEffect() ||
1892 paint.isFakeBoldText() ||
1893 paint.getStyle() != SkPaint::kFill_Style) {
1894 // turn off lcd
1895 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1896 flags->fHinting = paint.getHinting();
1897 return true;
1898 }
1899 // we're cool with the paint as is
1900 return false;
1901}
1902
reed@google.com75d939b2011-12-07 15:07:23 +00001903void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001904 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001905 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001906}
1907
reed@google.comf67e4cf2011-03-15 20:56:58 +00001908///////////////////////////////////////////////////////////////////////////////
1909
bsalomon@google.comfb309512011-11-30 14:13:48 +00001910bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1911 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001912 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001913 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001914
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001915 GrTextureDesc desc;
1916 desc.fWidth = bitmap.width();
1917 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001918 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001919 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001920
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001921 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001922}
1923
1924
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001925SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1926 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001927 bool isOpaque,
1928 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001929 GrTextureDesc desc;
1930 desc.fConfig = fRenderTarget->config();
1931 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1932 desc.fWidth = width;
1933 desc.fHeight = height;
1934 desc.fSampleCnt = fRenderTarget->numSamples();
1935
1936 GrContext::TextureCacheEntry cacheEntry;
1937 GrTexture* texture;
1938 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001939 // Skia's convention is to only clear a device if it is non-opaque.
1940 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001941
1942#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1943 // layers are never draw in repeat modes, so we can request an approx
1944 // match and ignore any padding.
1945 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1946 GrContext::kApprox_ScratchTexMatch :
1947 GrContext::kExact_ScratchTexMatch;
1948 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1949 texture = cacheEntry.texture();
1950#else
1951 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1952 texture = tunref.get();
1953#endif
1954 if (texture) {
1955 return SkNEW_ARGS(SkGpuDevice,(fContext,
1956 texture,
1957 cacheEntry,
1958 needClear));
1959 } else {
1960 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1961 width, height);
1962 return NULL;
1963 }
1964}
1965
1966SkGpuDevice::SkGpuDevice(GrContext* context,
1967 GrTexture* texture,
1968 TexCache cacheEntry,
1969 bool needClear)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001970 : SkDevice(make_bitmap(context, texture->asRenderTarget()))
1971 , fClipStack(NULL) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001972 GrAssert(texture && texture->asRenderTarget());
1973 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1974 this->initFromRenderTarget(context, texture->asRenderTarget());
1975 fCache = cacheEntry;
1976 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001977}
1978