blob: 5e4d89360b4b44625c0303bfbdd2cd3c2ddc06b9 [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"
tomhudson@google.com2f68e762012-07-17 18:43:21 +000011#include "effects/GrTextureDomainEffect.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000012
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrContext.h"
14#include "GrTextContext.h"
15
robertphillips@google.come9c04692012-06-29 00:30:13 +000016#include "SkGrTexturePixelRef.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
Scroggo97c88c22011-05-11 14:05:25 +000018#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019#include "SkDrawProcs.h"
20#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000021#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000022#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000023#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.com06cd7322012-03-30 18:45:35 +000025#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000026
27#if 0
28 extern bool (*gShouldDrawProc)();
29 #define CHECK_SHOULD_DRAW(draw) \
30 do { \
31 if (gShouldDrawProc && !gShouldDrawProc()) return; \
32 this->prepareRenderTarget(draw); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000033 GrAssert(!fNeedClear) \
reed@google.comac10a2d2010-12-22 21:39:39 +000034 } while (0)
35#else
bsalomon@google.com06cd7322012-03-30 18:45:35 +000036 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \
37 GrAssert(!fNeedClear)
reed@google.comac10a2d2010-12-22 21:39:39 +000038#endif
39
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000040// we use the same texture slot on GrPaint for bitmaps and shaders
41// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
42enum {
43 kBitmapTextureIdx = 0,
44 kShaderTextureIdx = 0
45};
46
reed@google.comcde92112011-07-06 20:00:52 +000047
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000048#define MAX_BLUR_SIGMA 4.0f
49// FIXME: This value comes from from SkBlurMaskFilter.cpp.
50// Should probably be put in a common header someplace.
51#define MAX_BLUR_RADIUS SkIntToScalar(128)
52// This constant approximates the scaling done in the software path's
53// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
54// IMHO, it actually should be 1: we blur "less" than we should do
55// according to the CSS and canvas specs, simply because Safari does the same.
56// Firefox used to do the same too, until 4.0 where they fixed it. So at some
57// point we should probably get rid of these scaling constants and rebaseline
58// all the blur tests.
59#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000060// This constant represents the screen alignment criterion in texels for
61// requiring texture domain clamping to prevent color bleeding when drawing
62// a sub region of a larger source image.
63#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000064
65#define DO_DEFERRED_CLEAR \
66 do { \
67 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000068 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000069 fNeedClear = false; \
70 } \
71 } while (false) \
72
reed@google.comac10a2d2010-12-22 21:39:39 +000073///////////////////////////////////////////////////////////////////////////////
74
reed@google.comb0a34d82012-07-11 19:57:55 +000075#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
76 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
77
78///////////////////////////////////////////////////////////////////////////////
79
80
bsalomon@google.com84405e02012-03-05 19:57:21 +000081class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
82public:
83 SkAutoCachedTexture() { }
84 SkAutoCachedTexture(SkGpuDevice* device,
85 const SkBitmap& bitmap,
86 const GrSamplerState* sampler,
87 GrTexture** texture) {
88 GrAssert(texture);
89 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000090 }
reed@google.comac10a2d2010-12-22 21:39:39 +000091
bsalomon@google.com84405e02012-03-05 19:57:21 +000092 ~SkAutoCachedTexture() {
93 if (fTex.texture()) {
94 fDevice->unlockCachedTexture(fTex);
95 }
reed@google.comac10a2d2010-12-22 21:39:39 +000096 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000097
98 GrTexture* set(SkGpuDevice* device,
99 const SkBitmap& bitmap,
100 const GrSamplerState* sampler) {
101 if (fTex.texture()) {
102 fDevice->unlockCachedTexture(fTex);
103 }
104 fDevice = device;
105 GrTexture* texture = (GrTexture*)bitmap.getTexture();
106 if (texture) {
107 // return the native texture
108 fTex.reset();
109 } else {
110 // look it up in our cache
111 fTex = device->lockCachedTexture(bitmap, sampler);
112 texture = fTex.texture();
113 }
114 return texture;
115 }
116
117private:
118 SkGpuDevice* fDevice;
119 GrContext::TextureCacheEntry fTex;
120};
reed@google.comac10a2d2010-12-22 21:39:39 +0000121
122///////////////////////////////////////////////////////////////////////////////
123
124bool gDoTraceDraw;
125
126struct GrSkDrawProcs : public SkDrawProcs {
127public:
128 GrContext* fContext;
129 GrTextContext* fTextContext;
130 GrFontScaler* fFontScaler; // cached in the skia glyphcache
131};
132
133///////////////////////////////////////////////////////////////////////////////
134
reed@google.comaf951c92011-06-16 19:10:39 +0000135static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
136 switch (config) {
137 case kAlpha_8_GrPixelConfig:
138 *isOpaque = false;
139 return SkBitmap::kA8_Config;
140 case kRGB_565_GrPixelConfig:
141 *isOpaque = true;
142 return SkBitmap::kRGB_565_Config;
143 case kRGBA_4444_GrPixelConfig:
144 *isOpaque = false;
145 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000146 case kSkia8888_PM_GrPixelConfig:
147 // we don't currently have a way of knowing whether
148 // a 8888 is opaque based on the config.
149 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000150 return SkBitmap::kARGB_8888_Config;
151 default:
152 *isOpaque = false;
153 return SkBitmap::kNo_Config;
154 }
155}
reed@google.comac10a2d2010-12-22 21:39:39 +0000156
reed@google.comaf951c92011-06-16 19:10:39 +0000157static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000158 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000159
160 bool isOpaque;
161 SkBitmap bitmap;
162 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
163 renderTarget->width(), renderTarget->height());
164 bitmap.setIsOpaque(isOpaque);
165 return bitmap;
166}
167
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000168SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000169: SkDevice(make_bitmap(context, texture->asRenderTarget()))
170, fClipStack(NULL) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000171 this->initFromRenderTarget(context, texture->asRenderTarget());
172}
173
reed@google.comaf951c92011-06-16 19:10:39 +0000174SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000175: SkDevice(make_bitmap(context, renderTarget))
176, fClipStack(NULL) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000177 this->initFromRenderTarget(context, renderTarget);
178}
179
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000180void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000181 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000182 fNeedPrepareRenderTarget = false;
183 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000184
reed@google.comaf951c92011-06-16 19:10:39 +0000185 fContext = context;
186 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000187
reed@google.comaf951c92011-06-16 19:10:39 +0000188 fTexture = NULL;
189 fRenderTarget = NULL;
190 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000191
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000192 GrAssert(NULL != renderTarget);
193 fRenderTarget = renderTarget;
194 fRenderTarget->ref();
195 // if this RT is also a texture, hold a ref on it
196 fTexture = fRenderTarget->asTexture();
197 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000198
199 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
200 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
201 // the device via accessBitmap. This external ref may outlive the device.
202 // Since textures own their render targets (but not vice-versa) we
203 // are ensuring that both objects will live as long as the pixel ref.
204 SkPixelRef* pr;
205 if (fTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000206 pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000207 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000208 pr = SkNEW_ARGS(SkGrRenderTargetPixelRef, (fRenderTarget));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000209 }
reed@google.comaf951c92011-06-16 19:10:39 +0000210 this->setPixelRef(pr, 0)->unref();
211}
212
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000213SkGpuDevice::SkGpuDevice(GrContext* context,
214 SkBitmap::Config config,
215 int width,
216 int height)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000217 : SkDevice(config, width, height, false /*isOpaque*/)
218 , fClipStack(NULL) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 fNeedPrepareRenderTarget = false;
220 fDrawProcs = NULL;
221
reed@google.com7b201d22011-01-11 18:59:23 +0000222 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000223 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000224
reed@google.comac10a2d2010-12-22 21:39:39 +0000225 fTexture = NULL;
226 fRenderTarget = NULL;
227 fNeedClear = false;
228
reed@google.comaf951c92011-06-16 19:10:39 +0000229 if (config != SkBitmap::kRGB_565_Config) {
230 config = SkBitmap::kARGB_8888_Config;
231 }
232 SkBitmap bm;
233 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000234
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000235 GrTextureDesc desc;
236 desc.fFlags = kRenderTarget_GrTextureFlagBit;
237 desc.fWidth = width;
238 desc.fHeight = height;
239 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000240
reed@google.comaf951c92011-06-16 19:10:39 +0000241 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000242
reed@google.comaf951c92011-06-16 19:10:39 +0000243 if (NULL != fTexture) {
244 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000245 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000246
reed@google.comaf951c92011-06-16 19:10:39 +0000247 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000248
reed@google.comaf951c92011-06-16 19:10:39 +0000249 // wrap the bitmap with a pixelref to expose our texture
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000250 SkGrTexturePixelRef* pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000251 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000252 } else {
253 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
254 width, height);
255 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000256 }
257}
258
259SkGpuDevice::~SkGpuDevice() {
260 if (fDrawProcs) {
261 delete fDrawProcs;
262 }
263
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000264 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
265 // This call gives the context a chance to relinquish it
266 fContext->setRenderTarget(NULL);
267
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000268 SkSafeUnref(fTexture);
269 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000270 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000271 GrAssert(NULL != fTexture);
272 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000273 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000274 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000275 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000276}
277
reed@google.comac10a2d2010-12-22 21:39:39 +0000278///////////////////////////////////////////////////////////////////////////////
279
280void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000281 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000282 fContext->setRenderTarget(fRenderTarget);
283 fContext->flush(true);
284 fNeedPrepareRenderTarget = true;
285}
286
287///////////////////////////////////////////////////////////////////////////////
288
bsalomon@google.comc4364992011-11-07 15:54:49 +0000289namespace {
290GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
291 switch (config8888) {
292 case SkCanvas::kNative_Premul_Config8888:
293 return kSkia8888_PM_GrPixelConfig;
294 case SkCanvas::kNative_Unpremul_Config8888:
295 return kSkia8888_UPM_GrPixelConfig;
296 case SkCanvas::kBGRA_Premul_Config8888:
297 return kBGRA_8888_PM_GrPixelConfig;
298 case SkCanvas::kBGRA_Unpremul_Config8888:
299 return kBGRA_8888_UPM_GrPixelConfig;
300 case SkCanvas::kRGBA_Premul_Config8888:
301 return kRGBA_8888_PM_GrPixelConfig;
302 case SkCanvas::kRGBA_Unpremul_Config8888:
303 return kRGBA_8888_UPM_GrPixelConfig;
304 default:
305 GrCrash("Unexpected Config8888.");
306 return kSkia8888_PM_GrPixelConfig;
307 }
308}
309}
310
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000311bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
312 int x, int y,
313 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000314 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000315 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
316 SkASSERT(!bitmap.isNull());
317 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000318
bsalomon@google.com910267d2011-11-02 20:06:25 +0000319 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000320 GrPixelConfig config;
321 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000322 return fContext->readRenderTargetPixels(fRenderTarget,
323 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000324 bitmap.width(),
325 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000326 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000327 bitmap.getPixels(),
328 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000329}
330
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000331void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
332 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000333 SkAutoLockPixels alp(bitmap);
334 if (!bitmap.readyToDraw()) {
335 return;
336 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000337
338 GrPixelConfig config;
339 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
340 config = config8888_to_gr_config(config8888);
341 } else {
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000342 config= SkGr::BitmapConfig2PixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000343 }
344
bsalomon@google.com6f379512011-11-16 20:36:03 +0000345 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
346 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000347}
348
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000349void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
350 INHERITED::onAttachToCanvas(canvas);
351
352 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
353 fClipStack = canvas->getClipStack();
354}
355
356void SkGpuDevice::onDetachFromCanvas() {
357 INHERITED::onDetachFromCanvas();
358
359 fClipStack = NULL;
360}
361
reed@google.comac10a2d2010-12-22 21:39:39 +0000362///////////////////////////////////////////////////////////////////////////////
363
364static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000365 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000366 const SkRegion& clipRegion,
367 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000368 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000369
370 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000371 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000372 const SkIRect& skBounds = clipRegion.getBounds();
373 GrRect bounds;
374 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
375 GrIntToScalar(skBounds.fTop),
376 GrIntToScalar(skBounds.fRight),
377 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000378 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000379 bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000380 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000381}
382
383// call this ever each draw call, to ensure that the context reflects our state,
384// and not the state from some other canvas/device
385void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000386 GrAssert(NULL != fClipStack);
387
reed@google.comac10a2d2010-12-22 21:39:39 +0000388 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000389 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000390
391 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000392 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000393 convert_matrixclip(fContext, *draw.fMatrix,
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000394 *fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000395 fNeedPrepareRenderTarget = false;
396 }
397}
398
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000399void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
400 const SkClipStack& clipStack) {
401 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
402 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000403 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000404}
405
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000406void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
407
408 GrAssert(NULL != fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000409
reed@google.comac10a2d2010-12-22 21:39:39 +0000410 fContext->setRenderTarget(fRenderTarget);
411
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000412 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000413
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000414 convert_matrixclip(fContext, matrix, *fClipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000415
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000416 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000417}
418
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000419SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000420 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000421 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000422}
423
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000424bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000425 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000426 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000427 return true;
428 }
429 return false;
430}
431
432///////////////////////////////////////////////////////////////////////////////
433
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000434SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
435SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
436SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
437SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
438SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
439 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000440SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
441 shader_type_mismatch);
442SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 5, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000443
bsalomon@google.com84405e02012-03-05 19:57:21 +0000444namespace {
445
446// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
447// justAlpha indicates that skPaint's alpha should be used rather than the color
448// Callers may subsequently modify the GrPaint. Setting constantColor indicates
449// that the final paint will draw the same color at every pixel. This allows
450// an optimization where the the color filter can be applied to the skPaint's
451// color once while converting to GrPain and then ignored.
452inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
453 bool justAlpha,
454 bool constantColor,
455 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000456
457 grPaint->fDither = skPaint.isDither();
458 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000459 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000460
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000461 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
462 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463
464 SkXfermode* mode = skPaint.getXfermode();
465 if (mode) {
466 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000467 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000468#if 0
469 return false;
470#endif
471 }
472 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000473 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
474 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
475
bsalomon@google.com5782d712011-01-21 21:03:59 +0000476 if (justAlpha) {
477 uint8_t alpha = skPaint.getAlpha();
478 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000479 // justAlpha is currently set to true only if there is a texture,
480 // so constantColor should not also be true.
481 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000482 } else {
483 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000484 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000485 }
Scroggo97c88c22011-05-11 14:05:25 +0000486 SkColorFilter* colorFilter = skPaint.getColorFilter();
487 SkColor color;
488 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000489 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000490 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000491 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000492 if (!constantColor) {
493 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
494 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000495 } else {
496 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
497 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000498 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000499 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000500 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
501 grPaint->fColorMatrixEnabled = true;
502 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
503 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
504 } else {
505 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000506 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000507 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000508}
509
bsalomon@google.com84405e02012-03-05 19:57:21 +0000510// This function is similar to skPaint2GrPaintNoShader but also converts
511// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
512// be used is set on grPaint and returned in param act. constantColor has the
513// same meaning as in skPaint2GrPaintNoShader.
514inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
515 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000516 bool constantColor,
517 SkGpuDevice::SkAutoCachedTexture* act,
518 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000519
bsalomon@google.com5782d712011-01-21 21:03:59 +0000520 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000521
bsalomon@google.com5782d712011-01-21 21:03:59 +0000522 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000523 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000524 return skPaint2GrPaintNoShader(skPaint,
525 false,
526 constantColor,
527 grPaint);
528 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000529 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000530 }
531
reed@google.comac10a2d2010-12-22 21:39:39 +0000532 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000533 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000534 SkShader::TileMode tileModes[2];
535 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000536 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000537 tileModes, twoPointParams);
538
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000539 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000540 SkShader::GradientInfo info;
541 SkColor color;
542
543 info.fColors = &color;
544 info.fColorOffsets = NULL;
545 info.fColorCount = 1;
546 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
547 SkPaint copy(skPaint);
548 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000549 // modulate the paint alpha by the shader's solid color alpha
550 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
551 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000552 return skPaint2GrPaintNoShader(copy,
553 false,
554 constantColor,
555 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000556 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000557 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000558 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000559
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000560 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000561 GrTexture* texture = act->set(dev, bitmap, sampler);
562 if (NULL == texture) {
563 SkDebugf("Couldn't convert bitmap to texture.\n");
564 return false;
565 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000566
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000567 switch (bmptype) {
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000568 case SkShader::kRadial_BitmapType:
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000569 sampler->setCustomStage(SkNEW_ARGS(GrRadialGradient, (texture)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000570 sampler->setFilter(GrSamplerState::kBilinear_Filter);
571 break;
572 case SkShader::kSweep_BitmapType:
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000573 sampler->setCustomStage(SkNEW_ARGS(GrSweepGradient, (texture)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000574 sampler->setFilter(GrSamplerState::kBilinear_Filter);
575 break;
576 case SkShader::kTwoPointRadial_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000577 sampler->setCustomStage(SkNEW_ARGS(GrRadial2Gradient,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000578 (texture,
579 twoPointParams[0],
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000580 twoPointParams[1],
581 twoPointParams[2] < 0)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000582 sampler->setFilter(GrSamplerState::kBilinear_Filter);
583 break;
rileya@google.com3e332582012-07-03 13:43:35 +0000584 case SkShader::kTwoPointConical_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000585 sampler->setCustomStage(SkNEW_ARGS(GrConical2Gradient,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000586 (texture,
587 twoPointParams[0],
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000588 twoPointParams[1],
589 twoPointParams[2])))->unref();
rileya@google.com3e332582012-07-03 13:43:35 +0000590 sampler->setFilter(GrSamplerState::kBilinear_Filter);
591 break;
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000592 default:
593 if (skPaint.isFilterBitmap()) {
594 sampler->setFilter(GrSamplerState::kBilinear_Filter);
595 } else {
596 sampler->setFilter(GrSamplerState::kNearest_Filter);
597 }
tomhudson@google.come742bf02012-07-13 19:54:19 +0000598 // TODO - once we have a trivial GrCustomStage for texture drawing,
599 // create that here & get rid of the paint's texture
600 grPaint->setTexture(kShaderTextureIdx, texture);
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000601 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000602 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000603 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
604 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000605
reed@google.comac10a2d2010-12-22 21:39:39 +0000606 // since our texture coords will be in local space, we wack the texture
607 // matrix to map them back into 0...1 before we load it
608 SkMatrix localM;
609 if (shader->getLocalMatrix(&localM)) {
610 SkMatrix inverse;
611 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000612 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000613 }
614 }
615 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000616 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
617 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000618 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000619 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000620 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000621 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000622 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000623
624 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000625}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000626}
reed@google.comac10a2d2010-12-22 21:39:39 +0000627
628///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000629void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000630 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000631}
632
reed@google.comac10a2d2010-12-22 21:39:39 +0000633void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
634 CHECK_SHOULD_DRAW(draw);
635
bsalomon@google.com5782d712011-01-21 21:03:59 +0000636 GrPaint grPaint;
637 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000638 if (!skPaint2GrPaintShader(this,
639 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000640 true,
641 &act,
642 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000643 return;
644 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000645
646 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000647}
648
649// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000650static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000651 kPoints_GrPrimitiveType,
652 kLines_GrPrimitiveType,
653 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000654};
655
656void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000657 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000658 CHECK_SHOULD_DRAW(draw);
659
660 SkScalar width = paint.getStrokeWidth();
661 if (width < 0) {
662 return;
663 }
664
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000665 // we only handle hairlines and paints without path effects or mask filters,
666 // else we let the SkDraw call our drawPath()
667 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000668 draw.drawPoints(mode, count, pts, paint, true);
669 return;
670 }
671
bsalomon@google.com5782d712011-01-21 21:03:59 +0000672 GrPaint grPaint;
673 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000674 if (!skPaint2GrPaintShader(this,
675 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000676 true,
677 &act,
678 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000679 return;
680 }
681
bsalomon@google.com5782d712011-01-21 21:03:59 +0000682 fContext->drawVertices(grPaint,
683 gPointMode2PrimtiveType[mode],
684 count,
685 (GrPoint*)pts,
686 NULL,
687 NULL,
688 NULL,
689 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000690}
691
reed@google.comc9aa5872011-04-05 21:05:37 +0000692///////////////////////////////////////////////////////////////////////////////
693
reed@google.comac10a2d2010-12-22 21:39:39 +0000694void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
695 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000696 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000697 CHECK_SHOULD_DRAW(draw);
698
bungeman@google.com79bd8772011-07-18 15:34:08 +0000699 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000700 SkScalar width = paint.getStrokeWidth();
701
702 /*
703 We have special code for hairline strokes, miter-strokes, and fills.
704 Anything else we just call our path code.
705 */
706 bool usePath = doStroke && width > 0 &&
707 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000708 // another two reasons we might need to call drawPath...
709 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000710 usePath = true;
711 }
reed@google.com67db6642011-05-26 11:46:35 +0000712 // until we aa rotated rects...
713 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
714 usePath = true;
715 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000716 // small miter limit means right angles show bevel...
717 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
718 paint.getStrokeMiter() < SK_ScalarSqrt2)
719 {
720 usePath = true;
721 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000722 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000723 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
724 usePath = true;
725 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000726
727 if (usePath) {
728 SkPath path;
729 path.addRect(rect);
730 this->drawPath(draw, path, paint, NULL, true);
731 return;
732 }
733
734 GrPaint grPaint;
735 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000736 if (!skPaint2GrPaintShader(this,
737 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000738 true,
739 &act,
740 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000741 return;
742 }
reed@google.com20efde72011-05-09 17:00:02 +0000743 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000744}
745
reed@google.com69302852011-02-16 18:08:07 +0000746#include "SkMaskFilter.h"
747#include "SkBounder.h"
748
bsalomon@google.com85003222012-03-28 14:44:37 +0000749///////////////////////////////////////////////////////////////////////////////
750
751// helpers for applying mask filters
752namespace {
753
754GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000755 switch (fillType) {
756 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000757 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000758 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000759 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000760 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000761 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000762 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000763 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000764 default:
765 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000766 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000767 }
768}
769
bsalomon@google.com85003222012-03-28 14:44:37 +0000770// We prefer to blur small rect with small radius via CPU.
771#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
772#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
773inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
774 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
775 rect.height() <= MIN_GPU_BLUR_SIZE &&
776 radius <= MIN_GPU_BLUR_RADIUS) {
777 return true;
778 }
779 return false;
780}
781
782bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
783 SkMaskFilter* filter, const SkMatrix& matrix,
784 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000785 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000786#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000787 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000788#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000789 SkMaskFilter::BlurInfo info;
790 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000791 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000792 return false;
793 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000794 SkScalar radius = info.fIgnoreTransform ? info.fRadius
795 : matrix.mapRadius(info.fRadius);
796 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000797 if (radius <= 0) {
798 return false;
799 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000800
801 SkRect srcRect = path.getBounds();
802 if (shouldDrawBlurWithCPU(srcRect, radius)) {
803 return false;
804 }
805
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000806 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000807 float sigma3 = sigma * 3.0f;
808
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000809 SkRect clipRect;
810 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000811
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000812 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000813 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
814 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000815 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000816 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000817 SkIRect finalIRect;
818 finalRect.roundOut(&finalIRect);
819 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000820 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000821 }
822 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000823 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000824 }
825 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000826 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000827 GrTextureDesc desc;
828 desc.fFlags = kRenderTarget_GrTextureFlagBit;
829 desc.fWidth = SkScalarCeilToInt(srcRect.width());
830 desc.fHeight = SkScalarCeilToInt(srcRect.height());
831 // We actually only need A8, but it often isn't supported as a
832 // render target so default to RGBA_8888
833 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000834
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000835 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
836 desc.fConfig = kAlpha_8_GrPixelConfig;
837 }
838
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000839 GrAutoScratchTexture pathEntry(context, desc);
840 GrTexture* pathTexture = pathEntry.texture();
841 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000842 return false;
843 }
844 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000845 // Once this code moves into GrContext, this should be changed to use
846 // an AutoClipRestore.
847 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000848 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000849
850 GrClip newClip(srcRect);
851 context->setClip(newClip);
852
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000853 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000854 GrPaint tempPaint;
855 tempPaint.reset();
856
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000857 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000858 tempPaint.fAntiAlias = grp->fAntiAlias;
859 if (tempPaint.fAntiAlias) {
860 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
861 // blend coeff of zero requires dual source blending support in order
862 // to properly blend partially covered pixels. This means the AA
863 // code path may not be taken. So we use a dst blend coeff of ISA. We
864 // could special case AA draws to a dst surface with known alpha=0 to
865 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000866 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
867 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000868 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000869 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000870 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000872 // If we're doing a normal blur, we can clobber the pathTexture in the
873 // gaussianBlur. Otherwise, we need to save it for later compositing.
874 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000875 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
876 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000877
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000878 if (!isNormalBlur) {
879 GrPaint paint;
880 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000881 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000882 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
883 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000884 // Blend pathTexture over blurTexture.
885 context->setRenderTarget(blurTexture->asRenderTarget());
886 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000887 if (SkMaskFilter::kInner_BlurType == blurType) {
888 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000889 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
890 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000891 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
892 // solid: dst = src + dst - src * dst
893 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000894 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
895 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000896 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
897 // outer: dst = dst * (1 - src)
898 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000899 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
900 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000901 }
902 context->drawRect(paint, srcRect);
903 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000904 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000905 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000906
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000907 if (grp->hasTextureOrMask()) {
908 GrMatrix inverse;
909 if (!matrix.invert(&inverse)) {
910 return false;
911 }
912 grp->preConcatActiveSamplerMatrices(inverse);
913 }
914
915 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
916 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000917 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000918 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000919 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000920
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000921 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
922 -finalRect.fTop);
923 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
924 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000925 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000926 return true;
927}
928
bsalomon@google.com85003222012-03-28 14:44:37 +0000929bool drawWithMaskFilter(GrContext* context, const SkPath& path,
930 SkMaskFilter* filter, const SkMatrix& matrix,
931 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000932 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000933 SkMask srcM, dstM;
934
935 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000936 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000937 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000938 return false;
939 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000940 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000941
942 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
943 return false;
944 }
945 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000946 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000947
948 if (clip.quickReject(dstM.fBounds)) {
949 return false;
950 }
951 if (bounder && !bounder->doIRect(dstM.fBounds)) {
952 return false;
953 }
954
955 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
956 // the current clip (and identity matrix) and grpaint settings
957
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000958 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000959 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000960
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000961 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000962
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000963 GrTextureDesc desc;
964 desc.fWidth = dstM.fBounds.width();
965 desc.fHeight = dstM.fBounds.height();
966 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000967
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000968 GrAutoScratchTexture ast(context, desc);
969 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000970
reed@google.com69302852011-02-16 18:08:07 +0000971 if (NULL == texture) {
972 return false;
973 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000974 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000975 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000976
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000977 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
978 grp->preConcatActiveSamplerMatrices(ivm);
979 }
980
981 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
982 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000983 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000984 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000985 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000986
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000987 GrRect d;
988 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000989 GrIntToScalar(dstM.fBounds.fTop),
990 GrIntToScalar(dstM.fBounds.fRight),
991 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000992
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000993 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
994 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
995 -dstM.fBounds.fTop*SK_Scalar1);
996 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000997 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000998 return true;
999}
reed@google.com69302852011-02-16 18:08:07 +00001000
bsalomon@google.com85003222012-03-28 14:44:37 +00001001}
1002
1003///////////////////////////////////////////////////////////////////////////////
1004
reed@google.com0c219b62011-02-16 21:31:18 +00001005void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001006 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001007 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001008 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001009 CHECK_SHOULD_DRAW(draw);
1010
reed@google.comfe626382011-09-21 13:50:35 +00001011 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001012
bsalomon@google.com5782d712011-01-21 21:03:59 +00001013 GrPaint grPaint;
1014 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001015 if (!skPaint2GrPaintShader(this,
1016 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001017 true,
1018 &act,
1019 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001020 return;
1021 }
1022
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001023 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1024 // if we can, we draw lots faster (raster device does this same test)
1025 SkScalar hairlineCoverage;
1026 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1027 doFill = false;
1028 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1029 grPaint.fCoverage);
1030 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001031
reed@google.comfe626382011-09-21 13:50:35 +00001032 // If we have a prematrix, apply it to the path, optimizing for the case
1033 // where the original path can in fact be modified in place (even though
1034 // its parameter type is const).
1035 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1036 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001037
1038 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001039 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001040
reed@google.come3445642011-02-16 23:20:39 +00001041 if (!pathIsMutable) {
1042 result = &tmpPath;
1043 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001044 }
reed@google.come3445642011-02-16 23:20:39 +00001045 // should I push prePathMatrix on our MV stack temporarily, instead
1046 // of applying it here? See SkDraw.cpp
1047 pathPtr->transform(*prePathMatrix, result);
1048 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001049 }
reed@google.com0c219b62011-02-16 21:31:18 +00001050 // at this point we're done with prePathMatrix
1051 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001052
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001053 if (paint.getPathEffect() ||
1054 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001055 // it is safe to use tmpPath here, even if we already used it for the
1056 // prepathmatrix, since getFillPath can take the same object for its
1057 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001058 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001059 pathPtr = &tmpPath;
1060 }
1061
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001062 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001063 // avoid possibly allocating a new path in transform if we can
1064 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1065
1066 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001067 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001068 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001069 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001070 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001071 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001072 &grPaint, pathFillType)) {
1073 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1074 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001075 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001076 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001077 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001078 }
reed@google.com69302852011-02-16 18:08:07 +00001079 return;
1080 }
reed@google.com69302852011-02-16 18:08:07 +00001081
bsalomon@google.com47059542012-06-06 20:51:20 +00001082 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001083
reed@google.com0c219b62011-02-16 21:31:18 +00001084 if (doFill) {
1085 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001086 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001087 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001088 break;
1089 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001090 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 break;
1092 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001093 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001094 break;
1095 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001096 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001097 break;
1098 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001099 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 return;
1101 }
1102 }
1103
reed@google.com07f3ee12011-05-16 17:21:57 +00001104 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001105}
1106
bsalomon@google.comfb309512011-11-30 14:13:48 +00001107namespace {
1108
1109inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1110 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1111 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1112 return tilesX * tilesY;
1113}
1114
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001115inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001116 const SkIRect* srcRectPtr,
1117 int maxTextureSize) {
1118 static const int kSmallTileSize = 1 << 10;
1119 if (maxTextureSize <= kSmallTileSize) {
1120 return maxTextureSize;
1121 }
1122
1123 size_t maxTexTotalTileSize;
1124 size_t smallTotalTileSize;
1125
1126 if (NULL == srcRectPtr) {
1127 int w = bitmap.width();
1128 int h = bitmap.height();
1129 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1130 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1131 } else {
1132 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1133 srcRectPtr->fTop,
1134 srcRectPtr->fRight,
1135 srcRectPtr->fBottom,
1136 maxTextureSize);
1137 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1138 srcRectPtr->fTop,
1139 srcRectPtr->fRight,
1140 srcRectPtr->fBottom,
1141 kSmallTileSize);
1142 }
1143 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1144 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1145
1146 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1147 return kSmallTileSize;
1148 } else {
1149 return maxTextureSize;
1150 }
1151}
1152}
1153
1154bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1155 const GrSamplerState& sampler,
1156 const SkIRect* srcRectPtr,
1157 int* tileSize) const {
1158 SkASSERT(NULL != tileSize);
1159
1160 // if bitmap is explictly texture backed then just use the texture
1161 if (NULL != bitmap.getTexture()) {
1162 return false;
1163 }
1164 // if it's larger than the max texture size, then we have no choice but
1165 // tiling
1166 const int maxTextureSize = fContext->getMaxTextureSize();
1167 if (bitmap.width() > maxTextureSize ||
1168 bitmap.height() > maxTextureSize) {
1169 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1170 return true;
1171 }
1172 // if we are going to have to draw the whole thing, then don't tile
1173 if (NULL == srcRectPtr) {
1174 return false;
1175 }
1176 // if the entire texture is already in our cache then no reason to tile it
1177 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1178 return false;
1179 }
1180
1181 // At this point we know we could do the draw by uploading the entire bitmap
1182 // as a texture. However, if the texture would be large compared to the
1183 // cache size and we don't require most of it for this draw then tile to
1184 // reduce the amount of upload and cache spill.
1185
1186 // assumption here is that sw bitmap size is a good proxy for its size as
1187 // a texture
1188 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001189 size_t cacheSize;
1190 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001191 if (bmpSize < cacheSize / 2) {
1192 return false;
1193 }
1194
1195 SkFixed fracUsed =
1196 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1197 (srcRectPtr->height() << 16) / bitmap.height());
1198 if (fracUsed <= SK_FixedHalf) {
1199 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1200 return true;
1201 } else {
1202 return false;
1203 }
1204}
1205
reed@google.comac10a2d2010-12-22 21:39:39 +00001206void SkGpuDevice::drawBitmap(const SkDraw& draw,
1207 const SkBitmap& bitmap,
1208 const SkIRect* srcRectPtr,
1209 const SkMatrix& m,
1210 const SkPaint& paint) {
1211 CHECK_SHOULD_DRAW(draw);
1212
1213 SkIRect srcRect;
1214 if (NULL == srcRectPtr) {
1215 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1216 } else {
1217 srcRect = *srcRectPtr;
1218 }
1219
junov@google.comd935cfb2011-06-27 20:48:23 +00001220 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001221 // Convert the bitmap to a shader so that the rect can be drawn
1222 // through drawRect, which supports mask filters.
1223 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001224 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001225 if (srcRectPtr) {
1226 if (!bitmap.extractSubset(&tmp, srcRect)) {
1227 return; // extraction failed
1228 }
1229 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001230 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001231 }
1232 SkPaint paintWithTexture(paint);
1233 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1234 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001235 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001236 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001237
junov@google.com1d329782011-07-28 20:10:09 +00001238 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001239 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001240 // also affect the behavior of the mask filter.
1241 SkMatrix drawMatrix;
1242 drawMatrix.setConcat(*draw.fMatrix, m);
1243 SkDraw transformedDraw(draw);
1244 transformedDraw.fMatrix = &drawMatrix;
1245
1246 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1247
junov@google.comd935cfb2011-06-27 20:48:23 +00001248 return;
1249 }
1250
bsalomon@google.com5782d712011-01-21 21:03:59 +00001251 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001252 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001253 return;
1254 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001255 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001256 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001257 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001258 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001259 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001260 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001261
bsalomon@google.comfb309512011-11-30 14:13:48 +00001262 int tileSize;
1263 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1264 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001265 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001266 return;
1267 }
1268
1269 // undo the translate done by SkCanvas
1270 int DX = SkMax32(0, srcRect.fLeft);
1271 int DY = SkMax32(0, srcRect.fTop);
1272 // compute clip bounds in local coordinates
1273 SkIRect clipRect;
1274 {
1275 SkRect r;
1276 r.set(draw.fClip->getBounds());
1277 SkMatrix matrix, inverse;
1278 matrix.setConcat(*draw.fMatrix, m);
1279 if (!matrix.invert(&inverse)) {
1280 return;
1281 }
1282 inverse.mapRect(&r);
1283 r.roundOut(&clipRect);
1284 // apply the canvas' translate to our local clip
1285 clipRect.offset(DX, DY);
1286 }
1287
bsalomon@google.comfb309512011-11-30 14:13:48 +00001288 int nx = bitmap.width() / tileSize;
1289 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 for (int x = 0; x <= nx; x++) {
1291 for (int y = 0; y <= ny; y++) {
1292 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001293 tileR.set(x * tileSize, y * tileSize,
1294 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001295 if (!SkIRect::Intersects(tileR, clipRect)) {
1296 continue;
1297 }
1298
1299 SkIRect srcR = tileR;
1300 if (!srcR.intersect(srcRect)) {
1301 continue;
1302 }
1303
1304 SkBitmap tmpB;
1305 if (bitmap.extractSubset(&tmpB, tileR)) {
1306 // now offset it to make it "local" to our tmp bitmap
1307 srcR.offset(-tileR.fLeft, -tileR.fTop);
1308
1309 SkMatrix tmpM(m);
1310 {
1311 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1312 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1313 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1314 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001315 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 }
1317 }
1318 }
1319}
1320
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001321namespace {
1322
1323bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1324 // detect pixel disalignment
1325 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1326 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1327 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1328 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1329 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1330 COLOR_BLEED_TOLERANCE &&
1331 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1332 COLOR_BLEED_TOLERANCE) {
1333 return true;
1334 }
1335 return false;
1336}
1337
1338bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1339 const SkMatrix& m) {
1340 // Only gets called if hasAlignedSamples returned false.
1341 // So we can assume that sampling is axis aligned but not texel aligned.
1342 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1343 SkRect innerSrcRect(srcRect), innerTransformedRect,
1344 outerTransformedRect(transformedRect);
1345 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1346 m.mapRect(&innerTransformedRect, innerSrcRect);
1347
1348 // The gap between outerTransformedRect and innerTransformedRect
1349 // represents the projection of the source border area, which is
1350 // problematic for color bleeding. We must check whether any
1351 // destination pixels sample the border area.
1352 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1353 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1354 SkIRect outer, inner;
1355 outerTransformedRect.round(&outer);
1356 innerTransformedRect.round(&inner);
1357 // If the inner and outer rects round to the same result, it means the
1358 // border does not overlap any pixel centers. Yay!
1359 return inner != outer;
1360}
1361
1362} // unnamed namespace
1363
reed@google.comac10a2d2010-12-22 21:39:39 +00001364/*
1365 * This is called by drawBitmap(), which has to handle images that may be too
1366 * large to be represented by a single texture.
1367 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001368 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1369 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001370 */
1371void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1372 const SkBitmap& bitmap,
1373 const SkIRect& srcRect,
1374 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001375 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001376 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1377 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001378
reed@google.com9c49bc32011-07-07 13:42:37 +00001379 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001380 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001381 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001382 return;
1383 }
1384
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001385 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001386
1387 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1388 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001389 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001390
1391 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001392 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001393 if (NULL == texture) {
1394 return;
1395 }
1396
bsalomon@google.com452943d2011-10-31 17:37:14 +00001397 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001398
reed@google.com20efde72011-05-09 17:00:02 +00001399 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1400 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001401 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001402 float wInv = 1.f / bitmap.width();
1403 float hInv = 1.f / bitmap.height();
1404 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1405 SkFloatToScalar(srcRect.fTop * hInv),
1406 SkFloatToScalar(srcRect.fRight * wInv),
1407 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001408
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001409 bool needsTextureDomain = false;
1410 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1411 {
1412 // Need texture domain if drawing a sub rect.
1413 needsTextureDomain = srcRect.width() < bitmap.width() ||
1414 srcRect.height() < bitmap.height();
1415 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1416 // sampling is axis-aligned
1417 GrRect floatSrcRect, transformedRect;
1418 floatSrcRect.set(srcRect);
1419 SkMatrix srcToDeviceMatrix(m);
1420 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1421 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1422
1423 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1424 // Samples are texel-aligned, so filtering is futile
1425 sampler->setFilter(GrSamplerState::kNearest_Filter);
1426 needsTextureDomain = false;
1427 } else {
1428 needsTextureDomain = needsTextureDomain &&
1429 mayColorBleed(floatSrcRect, transformedRect, m);
1430 }
1431 }
1432 }
1433
1434 GrRect textureDomain = GrRect::MakeEmpty();
1435
1436 if (needsTextureDomain) {
1437 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001438 GrScalar left, top, right, bottom;
1439 if (srcRect.width() > 1) {
1440 GrScalar border = GR_ScalarHalf / bitmap.width();
1441 left = paintRect.left() + border;
1442 right = paintRect.right() - border;
1443 } else {
1444 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1445 }
1446 if (srcRect.height() > 1) {
1447 GrScalar border = GR_ScalarHalf / bitmap.height();
1448 top = paintRect.top() + border;
1449 bottom = paintRect.bottom() - border;
1450 } else {
1451 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1452 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001453 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001454 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1455 (texture,
1456 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001457 }
1458
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001459 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001460}
1461
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001462namespace {
1463
1464void apply_custom_stage(GrContext* context,
1465 GrTexture* srcTexture,
1466 GrTexture* dstTexture,
1467 const GrRect& rect,
1468 GrCustomStage* stage) {
1469 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001470 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001471 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001472 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001473
1474 GrMatrix sampleM;
1475 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1476 GrPaint paint;
1477 paint.reset();
1478 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1479 paint.textureSampler(0)->reset(sampleM);
1480 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001481 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001482}
1483
1484};
1485
reed@google.com8926b162012-03-23 15:36:36 +00001486static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1487 SkImageFilter* filter, const GrRect& rect) {
1488 GrAssert(filter);
1489
1490 SkSize blurSize;
1491 SkISize radius;
1492
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001493 GrTextureDesc desc;
1494 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1495 desc.fWidth = SkScalarCeilToInt(rect.width());
1496 desc.fHeight = SkScalarCeilToInt(rect.height());
1497 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001498 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001499
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001500 if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001501 GrAutoScratchTexture dst(context, desc);
1502 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1503 texture = dst.detach();
1504 stage->unref();
1505 } else if (filter->asABlur(&blurSize)) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001506 texture = context->gaussianBlur(texture, false, rect,
reed@google.com8926b162012-03-23 15:36:36 +00001507 blurSize.width(),
1508 blurSize.height());
reed@google.com8926b162012-03-23 15:36:36 +00001509 } else if (filter->asADilate(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001510 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001511 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001512 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001513 } else if (filter->asAnErode(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001514 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001515 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001516 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001517 }
1518 return texture;
1519}
1520
reed@google.comac10a2d2010-12-22 21:39:39 +00001521void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1522 int left, int top, const SkPaint& paint) {
1523 CHECK_SHOULD_DRAW(draw);
1524
reed@google.com8926b162012-03-23 15:36:36 +00001525 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001526 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1527 return;
1528 }
1529
reed@google.com76dd2772012-01-05 21:15:07 +00001530 int w = bitmap.width();
1531 int h = bitmap.height();
1532
bsalomon@google.com5782d712011-01-21 21:03:59 +00001533 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001534 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001535 return;
1536 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001537
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001538 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001539
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001540 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001541
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001542 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001543 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001544 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001545 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001546
reed@google.com8926b162012-03-23 15:36:36 +00001547 SkImageFilter* filter = paint.getImageFilter();
1548 if (NULL != filter) {
1549 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001550 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001551 if (filteredTexture) {
1552 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1553 texture = filteredTexture;
1554 filteredTexture->unref();
1555 }
reed@google.com76dd2772012-01-05 21:15:07 +00001556 }
reed@google.com8926b162012-03-23 15:36:36 +00001557
bsalomon@google.com5782d712011-01-21 21:03:59 +00001558 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001559 GrRect::MakeXYWH(GrIntToScalar(left),
1560 GrIntToScalar(top),
1561 GrIntToScalar(w),
1562 GrIntToScalar(h)),
1563 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1564 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001565}
1566
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001567void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001568 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001569 // clear of the source device must occur before CHECK_SHOULD_DRAW
1570 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1571 if (dev->fNeedClear) {
1572 // TODO: could check here whether we really need to draw at all
1573 dev->clear(0x0);
1574 }
1575
reed@google.comac10a2d2010-12-22 21:39:39 +00001576 CHECK_SHOULD_DRAW(draw);
1577
bsalomon@google.com5782d712011-01-21 21:03:59 +00001578 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001579 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001580 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001581 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001582 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001583
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001584 GrTexture* devTex = grPaint.getTexture(0);
1585 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001586
reed@google.com8926b162012-03-23 15:36:36 +00001587 SkImageFilter* filter = paint.getImageFilter();
1588 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001589 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1590 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001591 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1592 rect);
1593 if (filteredTexture) {
1594 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1595 devTex = filteredTexture;
1596 filteredTexture->unref();
1597 }
1598 }
1599
bsalomon@google.com5782d712011-01-21 21:03:59 +00001600 const SkBitmap& bm = dev->accessBitmap(false);
1601 int w = bm.width();
1602 int h = bm.height();
1603
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001604 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001605
bsalomon@google.com97912912011-12-06 16:30:36 +00001606 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001607
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001608 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1609 GrIntToScalar(y),
1610 GrIntToScalar(w),
1611 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001612
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001613 // The device being drawn may not fill up its texture (saveLayer uses
1614 // the approximate ).
1615 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1616 GR_Scalar1 * h / devTex->height());
1617
1618 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001619}
1620
reed@google.com8926b162012-03-23 15:36:36 +00001621bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001622 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001623 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001624
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001625 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001626 !filter->asABlur(&size) &&
1627 !filter->asADilate(&radius) &&
1628 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001629 return false;
1630 }
reed@google.com8926b162012-03-23 15:36:36 +00001631 return true;
1632}
1633
1634bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1635 const SkMatrix& ctm,
1636 SkBitmap* result, SkIPoint* offset) {
1637 // want explicitly our impl, so guard against a subclass of us overriding it
1638 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001639 return false;
1640 }
reed@google.com8926b162012-03-23 15:36:36 +00001641
1642 SkAutoLockPixels alp(src, !src.getTexture());
1643 if (!src.getTexture() && !src.readyToDraw()) {
1644 return false;
1645 }
1646
1647 GrPaint paint;
1648 paint.reset();
1649
1650 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1651
1652 GrTexture* texture;
1653 SkAutoCachedTexture act(this, src, sampler, &texture);
1654
1655 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001656 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1657 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001658 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1659 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001660 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1661 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001662 resultTexture->unref();
1663 }
reed@google.com76dd2772012-01-05 21:15:07 +00001664 return true;
1665}
1666
reed@google.comac10a2d2010-12-22 21:39:39 +00001667///////////////////////////////////////////////////////////////////////////////
1668
1669// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001670static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001671 kTriangles_GrPrimitiveType,
1672 kTriangleStrip_GrPrimitiveType,
1673 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001674};
1675
1676void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1677 int vertexCount, const SkPoint vertices[],
1678 const SkPoint texs[], const SkColor colors[],
1679 SkXfermode* xmode,
1680 const uint16_t indices[], int indexCount,
1681 const SkPaint& paint) {
1682 CHECK_SHOULD_DRAW(draw);
1683
bsalomon@google.com5782d712011-01-21 21:03:59 +00001684 GrPaint grPaint;
1685 SkAutoCachedTexture act;
1686 // we ignore the shader if texs is null.
1687 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001688 if (!skPaint2GrPaintNoShader(paint,
1689 false,
1690 NULL == colors,
1691 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001692 return;
1693 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001694 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001695 if (!skPaint2GrPaintShader(this,
1696 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001697 NULL == colors,
1698 &act,
1699 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001700 return;
1701 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001702 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001703
1704 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001705 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001706 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1707#if 0
1708 return
1709#endif
1710 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001711 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001712
bsalomon@google.com498776a2011-08-16 19:20:44 +00001713 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1714 if (NULL != colors) {
1715 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001716 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001717 for (int i = 0; i < vertexCount; ++i) {
1718 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1719 }
1720 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001721 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001722 fContext->drawVertices(grPaint,
1723 gVertexMode2PrimitiveType[vmode],
1724 vertexCount,
1725 (GrPoint*) vertices,
1726 (GrPoint*) texs,
1727 colors,
1728 indices,
1729 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001730}
1731
1732///////////////////////////////////////////////////////////////////////////////
1733
1734static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001735 GrFontScaler* scaler = (GrFontScaler*)data;
1736 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001737}
1738
1739static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1740 void* auxData;
1741 GrFontScaler* scaler = NULL;
1742 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1743 scaler = (GrFontScaler*)auxData;
1744 }
1745 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001746 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001747 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1748 }
1749 return scaler;
1750}
1751
1752static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1753 SkFixed fx, SkFixed fy,
1754 const SkGlyph& glyph) {
1755 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1756
bungeman@google.com15865a72012-01-11 16:28:04 +00001757 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001758
1759 if (NULL == procs->fFontScaler) {
1760 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1761 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001762
bungeman@google.com15865a72012-01-11 16:28:04 +00001763 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1764 glyph.getSubXFixed(),
1765 glyph.getSubYFixed()),
1766 SkFixedFloorToFixed(fx),
1767 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001768 procs->fFontScaler);
1769}
1770
bsalomon@google.com5782d712011-01-21 21:03:59 +00001771SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001772
1773 // deferred allocation
1774 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001775 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001776 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1777 fDrawProcs->fContext = fContext;
1778 }
1779
1780 // init our (and GL's) state
1781 fDrawProcs->fTextContext = context;
1782 fDrawProcs->fFontScaler = NULL;
1783 return fDrawProcs;
1784}
1785
1786void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1787 size_t byteLength, SkScalar x, SkScalar y,
1788 const SkPaint& paint) {
1789 CHECK_SHOULD_DRAW(draw);
1790
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001791 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001792 // this guy will just call our drawPath()
1793 draw.drawText((const char*)text, byteLength, x, y, paint);
1794 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001795 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001796
1797 GrPaint grPaint;
1798 SkAutoCachedTexture act;
1799
bsalomon@google.com84405e02012-03-05 19:57:21 +00001800 if (!skPaint2GrPaintShader(this,
1801 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001802 true,
1803 &act,
1804 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001805 return;
1806 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001807 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1808 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001809 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1810 }
1811}
1812
1813void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1814 size_t byteLength, const SkScalar pos[],
1815 SkScalar constY, int scalarsPerPos,
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.drawPosText((const char*)text, byteLength, pos, constY,
1822 scalarsPerPos, paint);
1823 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001824 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001825
1826 GrPaint grPaint;
1827 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001828 if (!skPaint2GrPaintShader(this,
1829 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001830 true,
1831 &act,
1832 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001833 return;
1834 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001835 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1836 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001837 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1838 scalarsPerPos, paint);
1839 }
1840}
1841
1842void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1843 size_t len, const SkPath& path,
1844 const SkMatrix* m, const SkPaint& paint) {
1845 CHECK_SHOULD_DRAW(draw);
1846
1847 SkASSERT(draw.fDevice == this);
1848 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1849}
1850
1851///////////////////////////////////////////////////////////////////////////////
1852
reed@google.comf67e4cf2011-03-15 20:56:58 +00001853bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1854 if (!paint.isLCDRenderText()) {
1855 // we're cool with the paint as is
1856 return false;
1857 }
1858
1859 if (paint.getShader() ||
1860 paint.getXfermode() || // unless its srcover
1861 paint.getMaskFilter() ||
1862 paint.getRasterizer() ||
1863 paint.getColorFilter() ||
1864 paint.getPathEffect() ||
1865 paint.isFakeBoldText() ||
1866 paint.getStyle() != SkPaint::kFill_Style) {
1867 // turn off lcd
1868 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1869 flags->fHinting = paint.getHinting();
1870 return true;
1871 }
1872 // we're cool with the paint as is
1873 return false;
1874}
1875
reed@google.com75d939b2011-12-07 15:07:23 +00001876void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001877 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001878 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001879}
1880
reed@google.comf67e4cf2011-03-15 20:56:58 +00001881///////////////////////////////////////////////////////////////////////////////
1882
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001883SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1884 const SkBitmap& bitmap,
1885 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001886 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001887 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001888
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001889 if (!bitmap.isVolatile()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001890 uint64_t key = bitmap.getGenerationID();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001891 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001892
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001893 GrTextureDesc desc;
1894 desc.fWidth = bitmap.width();
1895 desc.fHeight = bitmap.height();
1896 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
robertphillips@google.com56f22442012-06-08 14:21:26 +00001897 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001898
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001899 entry = ctx->findAndLockTexture(desc, sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001900 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001901 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1902 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001903 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001904 } else {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001905 entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001906 sampler, bitmap);
1907 }
1908 if (NULL == entry.texture()) {
1909 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1910 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001911 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001912 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001913}
1914
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001915void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1916 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001917}
1918
bsalomon@google.comfb309512011-11-30 14:13:48 +00001919bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1920 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001921 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001922 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001923
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001924 GrTextureDesc desc;
1925 desc.fWidth = bitmap.width();
1926 desc.fHeight = bitmap.height();
1927 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
1928 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001929
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001930 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001931}
1932
1933
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001934SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1935 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001936 bool isOpaque,
1937 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001938 GrTextureDesc desc;
1939 desc.fConfig = fRenderTarget->config();
1940 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1941 desc.fWidth = width;
1942 desc.fHeight = height;
1943 desc.fSampleCnt = fRenderTarget->numSamples();
1944
1945 GrContext::TextureCacheEntry cacheEntry;
1946 GrTexture* texture;
1947 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001948 // Skia's convention is to only clear a device if it is non-opaque.
1949 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001950
1951#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1952 // layers are never draw in repeat modes, so we can request an approx
1953 // match and ignore any padding.
1954 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1955 GrContext::kApprox_ScratchTexMatch :
1956 GrContext::kExact_ScratchTexMatch;
1957 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1958 texture = cacheEntry.texture();
1959#else
1960 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1961 texture = tunref.get();
1962#endif
1963 if (texture) {
1964 return SkNEW_ARGS(SkGpuDevice,(fContext,
1965 texture,
1966 cacheEntry,
1967 needClear));
1968 } else {
1969 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1970 width, height);
1971 return NULL;
1972 }
1973}
1974
1975SkGpuDevice::SkGpuDevice(GrContext* context,
1976 GrTexture* texture,
1977 TexCache cacheEntry,
1978 bool needClear)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001979 : SkDevice(make_bitmap(context, texture->asRenderTarget()))
1980 , fClipStack(NULL) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001981 GrAssert(texture && texture->asRenderTarget());
1982 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1983 this->initFromRenderTarget(context, texture->asRenderTarget());
1984 fCache = cacheEntry;
1985 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001986}
1987