blob: d0d46041c134bdad94aad9ff3c388ed2ff11cab6 [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 GrAutoScratchTexture temp1, temp2;
873 // If we're doing a normal blur, we can clobber the pathTexture in the
874 // gaussianBlur. Otherwise, we need to save it for later compositing.
875 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000876 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
877 &temp1,
878 isNormalBlur ? NULL : &temp2,
879 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000880
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000881 if (!isNormalBlur) {
882 GrPaint paint;
883 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000884 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000885 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
886 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000887 // Blend pathTexture over blurTexture.
888 context->setRenderTarget(blurTexture->asRenderTarget());
889 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000890 if (SkMaskFilter::kInner_BlurType == blurType) {
891 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000892 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
893 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000894 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
895 // solid: dst = src + dst - src * dst
896 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000897 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
898 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000899 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
900 // outer: dst = dst * (1 - src)
901 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000902 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
903 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000904 }
905 context->drawRect(paint, srcRect);
906 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000907 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000908 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000909
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000910 if (grp->hasTextureOrMask()) {
911 GrMatrix inverse;
912 if (!matrix.invert(&inverse)) {
913 return false;
914 }
915 grp->preConcatActiveSamplerMatrices(inverse);
916 }
917
918 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
919 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000920 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000921 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000922 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000923
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000924 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
925 -finalRect.fTop);
926 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
927 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000928 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000929 return true;
930}
931
bsalomon@google.com85003222012-03-28 14:44:37 +0000932bool drawWithMaskFilter(GrContext* context, const SkPath& path,
933 SkMaskFilter* filter, const SkMatrix& matrix,
934 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000935 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000936 SkMask srcM, dstM;
937
938 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000939 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000940 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000941 return false;
942 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000943 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000944
945 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
946 return false;
947 }
948 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000949 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000950
951 if (clip.quickReject(dstM.fBounds)) {
952 return false;
953 }
954 if (bounder && !bounder->doIRect(dstM.fBounds)) {
955 return false;
956 }
957
958 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
959 // the current clip (and identity matrix) and grpaint settings
960
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000961 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000962 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000963
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000964 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000965
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000966 GrTextureDesc desc;
967 desc.fWidth = dstM.fBounds.width();
968 desc.fHeight = dstM.fBounds.height();
969 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000970
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000971 GrAutoScratchTexture ast(context, desc);
972 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000973
reed@google.com69302852011-02-16 18:08:07 +0000974 if (NULL == texture) {
975 return false;
976 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000977 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000978 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000979
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000980 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
981 grp->preConcatActiveSamplerMatrices(ivm);
982 }
983
984 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
985 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000986 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000987 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000988 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000989
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000990 GrRect d;
991 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000992 GrIntToScalar(dstM.fBounds.fTop),
993 GrIntToScalar(dstM.fBounds.fRight),
994 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000995
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000996 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
997 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
998 -dstM.fBounds.fTop*SK_Scalar1);
999 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001000 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001001 return true;
1002}
reed@google.com69302852011-02-16 18:08:07 +00001003
bsalomon@google.com85003222012-03-28 14:44:37 +00001004}
1005
1006///////////////////////////////////////////////////////////////////////////////
1007
reed@google.com0c219b62011-02-16 21:31:18 +00001008void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001009 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001010 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001011 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001012 CHECK_SHOULD_DRAW(draw);
1013
reed@google.comfe626382011-09-21 13:50:35 +00001014 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001015
bsalomon@google.com5782d712011-01-21 21:03:59 +00001016 GrPaint grPaint;
1017 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001018 if (!skPaint2GrPaintShader(this,
1019 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001020 true,
1021 &act,
1022 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001023 return;
1024 }
1025
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001026 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1027 // if we can, we draw lots faster (raster device does this same test)
1028 SkScalar hairlineCoverage;
1029 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1030 doFill = false;
1031 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1032 grPaint.fCoverage);
1033 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001034
reed@google.comfe626382011-09-21 13:50:35 +00001035 // If we have a prematrix, apply it to the path, optimizing for the case
1036 // where the original path can in fact be modified in place (even though
1037 // its parameter type is const).
1038 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1039 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001040
1041 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001042 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001043
reed@google.come3445642011-02-16 23:20:39 +00001044 if (!pathIsMutable) {
1045 result = &tmpPath;
1046 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001047 }
reed@google.come3445642011-02-16 23:20:39 +00001048 // should I push prePathMatrix on our MV stack temporarily, instead
1049 // of applying it here? See SkDraw.cpp
1050 pathPtr->transform(*prePathMatrix, result);
1051 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 }
reed@google.com0c219b62011-02-16 21:31:18 +00001053 // at this point we're done with prePathMatrix
1054 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001055
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001056 if (paint.getPathEffect() ||
1057 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001058 // it is safe to use tmpPath here, even if we already used it for the
1059 // prepathmatrix, since getFillPath can take the same object for its
1060 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001061 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001062 pathPtr = &tmpPath;
1063 }
1064
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001065 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001066 // avoid possibly allocating a new path in transform if we can
1067 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1068
1069 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001070 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001071 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001072 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001073 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001074 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001075 &grPaint, pathFillType)) {
1076 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1077 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001078 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001079 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001080 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001081 }
reed@google.com69302852011-02-16 18:08:07 +00001082 return;
1083 }
reed@google.com69302852011-02-16 18:08:07 +00001084
bsalomon@google.com47059542012-06-06 20:51:20 +00001085 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001086
reed@google.com0c219b62011-02-16 21:31:18 +00001087 if (doFill) {
1088 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001089 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001090 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 break;
1092 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001093 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001094 break;
1095 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001096 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001097 break;
1098 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001099 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 break;
1101 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001102 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001103 return;
1104 }
1105 }
1106
reed@google.com07f3ee12011-05-16 17:21:57 +00001107 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001108}
1109
bsalomon@google.comfb309512011-11-30 14:13:48 +00001110namespace {
1111
1112inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1113 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1114 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1115 return tilesX * tilesY;
1116}
1117
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001118inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001119 const SkIRect* srcRectPtr,
1120 int maxTextureSize) {
1121 static const int kSmallTileSize = 1 << 10;
1122 if (maxTextureSize <= kSmallTileSize) {
1123 return maxTextureSize;
1124 }
1125
1126 size_t maxTexTotalTileSize;
1127 size_t smallTotalTileSize;
1128
1129 if (NULL == srcRectPtr) {
1130 int w = bitmap.width();
1131 int h = bitmap.height();
1132 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1133 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1134 } else {
1135 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1136 srcRectPtr->fTop,
1137 srcRectPtr->fRight,
1138 srcRectPtr->fBottom,
1139 maxTextureSize);
1140 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1141 srcRectPtr->fTop,
1142 srcRectPtr->fRight,
1143 srcRectPtr->fBottom,
1144 kSmallTileSize);
1145 }
1146 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1147 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1148
1149 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1150 return kSmallTileSize;
1151 } else {
1152 return maxTextureSize;
1153 }
1154}
1155}
1156
1157bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1158 const GrSamplerState& sampler,
1159 const SkIRect* srcRectPtr,
1160 int* tileSize) const {
1161 SkASSERT(NULL != tileSize);
1162
1163 // if bitmap is explictly texture backed then just use the texture
1164 if (NULL != bitmap.getTexture()) {
1165 return false;
1166 }
1167 // if it's larger than the max texture size, then we have no choice but
1168 // tiling
1169 const int maxTextureSize = fContext->getMaxTextureSize();
1170 if (bitmap.width() > maxTextureSize ||
1171 bitmap.height() > maxTextureSize) {
1172 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1173 return true;
1174 }
1175 // if we are going to have to draw the whole thing, then don't tile
1176 if (NULL == srcRectPtr) {
1177 return false;
1178 }
1179 // if the entire texture is already in our cache then no reason to tile it
1180 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1181 return false;
1182 }
1183
1184 // At this point we know we could do the draw by uploading the entire bitmap
1185 // as a texture. However, if the texture would be large compared to the
1186 // cache size and we don't require most of it for this draw then tile to
1187 // reduce the amount of upload and cache spill.
1188
1189 // assumption here is that sw bitmap size is a good proxy for its size as
1190 // a texture
1191 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001192 size_t cacheSize;
1193 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001194 if (bmpSize < cacheSize / 2) {
1195 return false;
1196 }
1197
1198 SkFixed fracUsed =
1199 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1200 (srcRectPtr->height() << 16) / bitmap.height());
1201 if (fracUsed <= SK_FixedHalf) {
1202 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1203 return true;
1204 } else {
1205 return false;
1206 }
1207}
1208
reed@google.comac10a2d2010-12-22 21:39:39 +00001209void SkGpuDevice::drawBitmap(const SkDraw& draw,
1210 const SkBitmap& bitmap,
1211 const SkIRect* srcRectPtr,
1212 const SkMatrix& m,
1213 const SkPaint& paint) {
1214 CHECK_SHOULD_DRAW(draw);
1215
1216 SkIRect srcRect;
1217 if (NULL == srcRectPtr) {
1218 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1219 } else {
1220 srcRect = *srcRectPtr;
1221 }
1222
junov@google.comd935cfb2011-06-27 20:48:23 +00001223 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001224 // Convert the bitmap to a shader so that the rect can be drawn
1225 // through drawRect, which supports mask filters.
1226 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001227 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001228 if (srcRectPtr) {
1229 if (!bitmap.extractSubset(&tmp, srcRect)) {
1230 return; // extraction failed
1231 }
1232 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001233 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001234 }
1235 SkPaint paintWithTexture(paint);
1236 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1237 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001238 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001239 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001240
junov@google.com1d329782011-07-28 20:10:09 +00001241 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001242 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001243 // also affect the behavior of the mask filter.
1244 SkMatrix drawMatrix;
1245 drawMatrix.setConcat(*draw.fMatrix, m);
1246 SkDraw transformedDraw(draw);
1247 transformedDraw.fMatrix = &drawMatrix;
1248
1249 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1250
junov@google.comd935cfb2011-06-27 20:48:23 +00001251 return;
1252 }
1253
bsalomon@google.com5782d712011-01-21 21:03:59 +00001254 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001255 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001256 return;
1257 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001258 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001259 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001260 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001261 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001262 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001263 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001264
bsalomon@google.comfb309512011-11-30 14:13:48 +00001265 int tileSize;
1266 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1267 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001268 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001269 return;
1270 }
1271
1272 // undo the translate done by SkCanvas
1273 int DX = SkMax32(0, srcRect.fLeft);
1274 int DY = SkMax32(0, srcRect.fTop);
1275 // compute clip bounds in local coordinates
1276 SkIRect clipRect;
1277 {
1278 SkRect r;
1279 r.set(draw.fClip->getBounds());
1280 SkMatrix matrix, inverse;
1281 matrix.setConcat(*draw.fMatrix, m);
1282 if (!matrix.invert(&inverse)) {
1283 return;
1284 }
1285 inverse.mapRect(&r);
1286 r.roundOut(&clipRect);
1287 // apply the canvas' translate to our local clip
1288 clipRect.offset(DX, DY);
1289 }
1290
bsalomon@google.comfb309512011-11-30 14:13:48 +00001291 int nx = bitmap.width() / tileSize;
1292 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001293 for (int x = 0; x <= nx; x++) {
1294 for (int y = 0; y <= ny; y++) {
1295 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001296 tileR.set(x * tileSize, y * tileSize,
1297 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001298 if (!SkIRect::Intersects(tileR, clipRect)) {
1299 continue;
1300 }
1301
1302 SkIRect srcR = tileR;
1303 if (!srcR.intersect(srcRect)) {
1304 continue;
1305 }
1306
1307 SkBitmap tmpB;
1308 if (bitmap.extractSubset(&tmpB, tileR)) {
1309 // now offset it to make it "local" to our tmp bitmap
1310 srcR.offset(-tileR.fLeft, -tileR.fTop);
1311
1312 SkMatrix tmpM(m);
1313 {
1314 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1315 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1316 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1317 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001318 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001319 }
1320 }
1321 }
1322}
1323
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001324namespace {
1325
1326bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1327 // detect pixel disalignment
1328 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1329 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1330 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1331 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1332 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1333 COLOR_BLEED_TOLERANCE &&
1334 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1335 COLOR_BLEED_TOLERANCE) {
1336 return true;
1337 }
1338 return false;
1339}
1340
1341bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1342 const SkMatrix& m) {
1343 // Only gets called if hasAlignedSamples returned false.
1344 // So we can assume that sampling is axis aligned but not texel aligned.
1345 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1346 SkRect innerSrcRect(srcRect), innerTransformedRect,
1347 outerTransformedRect(transformedRect);
1348 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1349 m.mapRect(&innerTransformedRect, innerSrcRect);
1350
1351 // The gap between outerTransformedRect and innerTransformedRect
1352 // represents the projection of the source border area, which is
1353 // problematic for color bleeding. We must check whether any
1354 // destination pixels sample the border area.
1355 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1356 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1357 SkIRect outer, inner;
1358 outerTransformedRect.round(&outer);
1359 innerTransformedRect.round(&inner);
1360 // If the inner and outer rects round to the same result, it means the
1361 // border does not overlap any pixel centers. Yay!
1362 return inner != outer;
1363}
1364
1365} // unnamed namespace
1366
reed@google.comac10a2d2010-12-22 21:39:39 +00001367/*
1368 * This is called by drawBitmap(), which has to handle images that may be too
1369 * large to be represented by a single texture.
1370 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001371 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1372 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001373 */
1374void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1375 const SkBitmap& bitmap,
1376 const SkIRect& srcRect,
1377 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001378 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001379 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1380 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001381
reed@google.com9c49bc32011-07-07 13:42:37 +00001382 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001383 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001384 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001385 return;
1386 }
1387
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001388 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001389
1390 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1391 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001392 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001393
1394 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001395 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001396 if (NULL == texture) {
1397 return;
1398 }
1399
bsalomon@google.com452943d2011-10-31 17:37:14 +00001400 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001401
reed@google.com20efde72011-05-09 17:00:02 +00001402 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1403 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001404 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001405 float wInv = 1.f / bitmap.width();
1406 float hInv = 1.f / bitmap.height();
1407 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1408 SkFloatToScalar(srcRect.fTop * hInv),
1409 SkFloatToScalar(srcRect.fRight * wInv),
1410 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001411
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001412 bool needsTextureDomain = false;
1413 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1414 {
1415 // Need texture domain if drawing a sub rect.
1416 needsTextureDomain = srcRect.width() < bitmap.width() ||
1417 srcRect.height() < bitmap.height();
1418 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1419 // sampling is axis-aligned
1420 GrRect floatSrcRect, transformedRect;
1421 floatSrcRect.set(srcRect);
1422 SkMatrix srcToDeviceMatrix(m);
1423 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1424 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1425
1426 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1427 // Samples are texel-aligned, so filtering is futile
1428 sampler->setFilter(GrSamplerState::kNearest_Filter);
1429 needsTextureDomain = false;
1430 } else {
1431 needsTextureDomain = needsTextureDomain &&
1432 mayColorBleed(floatSrcRect, transformedRect, m);
1433 }
1434 }
1435 }
1436
1437 GrRect textureDomain = GrRect::MakeEmpty();
1438
1439 if (needsTextureDomain) {
1440 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001441 GrScalar left, top, right, bottom;
1442 if (srcRect.width() > 1) {
1443 GrScalar border = GR_ScalarHalf / bitmap.width();
1444 left = paintRect.left() + border;
1445 right = paintRect.right() - border;
1446 } else {
1447 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1448 }
1449 if (srcRect.height() > 1) {
1450 GrScalar border = GR_ScalarHalf / bitmap.height();
1451 top = paintRect.top() + border;
1452 bottom = paintRect.bottom() - border;
1453 } else {
1454 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1455 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001456 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001457 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1458 (texture,
1459 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001460 }
1461
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001462 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001463}
1464
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001465namespace {
1466
1467void apply_custom_stage(GrContext* context,
1468 GrTexture* srcTexture,
1469 GrTexture* dstTexture,
1470 const GrRect& rect,
1471 GrCustomStage* stage) {
1472 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001473 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001474 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001475 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001476
1477 GrMatrix sampleM;
1478 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1479 GrPaint paint;
1480 paint.reset();
1481 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1482 paint.textureSampler(0)->reset(sampleM);
1483 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001484 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001485}
1486
1487};
1488
reed@google.com8926b162012-03-23 15:36:36 +00001489static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1490 SkImageFilter* filter, const GrRect& rect) {
1491 GrAssert(filter);
1492
1493 SkSize blurSize;
1494 SkISize radius;
1495
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001496 GrTextureDesc desc;
1497 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1498 desc.fWidth = SkScalarCeilToInt(rect.width());
1499 desc.fHeight = SkScalarCeilToInt(rect.height());
1500 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001501 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001502
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001503 if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001504 GrAutoScratchTexture dst(context, desc);
1505 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1506 texture = dst.detach();
1507 stage->unref();
1508 } else if (filter->asABlur(&blurSize)) {
reed@google.com8926b162012-03-23 15:36:36 +00001509 GrAutoScratchTexture temp1, temp2;
1510 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1511 blurSize.width(),
1512 blurSize.height());
1513 texture->ref();
1514 } else if (filter->asADilate(&radius)) {
1515 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1516 texture = context->applyMorphology(texture, rect,
1517 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001518 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001519 radius);
1520 texture->ref();
1521 } else if (filter->asAnErode(&radius)) {
1522 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1523 texture = context->applyMorphology(texture, rect,
1524 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001525 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001526 radius);
1527 texture->ref();
1528 }
1529 return texture;
1530}
1531
reed@google.comac10a2d2010-12-22 21:39:39 +00001532void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1533 int left, int top, const SkPaint& paint) {
1534 CHECK_SHOULD_DRAW(draw);
1535
reed@google.com8926b162012-03-23 15:36:36 +00001536 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001537 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1538 return;
1539 }
1540
reed@google.com76dd2772012-01-05 21:15:07 +00001541 int w = bitmap.width();
1542 int h = bitmap.height();
1543
bsalomon@google.com5782d712011-01-21 21:03:59 +00001544 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001545 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001546 return;
1547 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001548
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001549 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001550
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001551 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001552
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001553 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001554 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001555 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001556 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001557
reed@google.com8926b162012-03-23 15:36:36 +00001558 SkImageFilter* filter = paint.getImageFilter();
1559 if (NULL != filter) {
1560 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001561 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001562 if (filteredTexture) {
1563 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1564 texture = filteredTexture;
1565 filteredTexture->unref();
1566 }
reed@google.com76dd2772012-01-05 21:15:07 +00001567 }
reed@google.com8926b162012-03-23 15:36:36 +00001568
bsalomon@google.com5782d712011-01-21 21:03:59 +00001569 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001570 GrRect::MakeXYWH(GrIntToScalar(left),
1571 GrIntToScalar(top),
1572 GrIntToScalar(w),
1573 GrIntToScalar(h)),
1574 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1575 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001576}
1577
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001578void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001579 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001580 // clear of the source device must occur before CHECK_SHOULD_DRAW
1581 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1582 if (dev->fNeedClear) {
1583 // TODO: could check here whether we really need to draw at all
1584 dev->clear(0x0);
1585 }
1586
reed@google.comac10a2d2010-12-22 21:39:39 +00001587 CHECK_SHOULD_DRAW(draw);
1588
bsalomon@google.com5782d712011-01-21 21:03:59 +00001589 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001590 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001591 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001592 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001593 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001594
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001595 GrTexture* devTex = grPaint.getTexture(0);
1596 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001597
reed@google.com8926b162012-03-23 15:36:36 +00001598 SkImageFilter* filter = paint.getImageFilter();
1599 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001600 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1601 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001602 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1603 rect);
1604 if (filteredTexture) {
1605 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1606 devTex = filteredTexture;
1607 filteredTexture->unref();
1608 }
1609 }
1610
bsalomon@google.com5782d712011-01-21 21:03:59 +00001611 const SkBitmap& bm = dev->accessBitmap(false);
1612 int w = bm.width();
1613 int h = bm.height();
1614
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001615 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001616
bsalomon@google.com97912912011-12-06 16:30:36 +00001617 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001618
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001619 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1620 GrIntToScalar(y),
1621 GrIntToScalar(w),
1622 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001623
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001624 // The device being drawn may not fill up its texture (saveLayer uses
1625 // the approximate ).
1626 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1627 GR_Scalar1 * h / devTex->height());
1628
1629 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001630}
1631
reed@google.com8926b162012-03-23 15:36:36 +00001632bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001633 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001634 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001635
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001636 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001637 !filter->asABlur(&size) &&
1638 !filter->asADilate(&radius) &&
1639 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001640 return false;
1641 }
reed@google.com8926b162012-03-23 15:36:36 +00001642 return true;
1643}
1644
1645bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1646 const SkMatrix& ctm,
1647 SkBitmap* result, SkIPoint* offset) {
1648 // want explicitly our impl, so guard against a subclass of us overriding it
1649 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001650 return false;
1651 }
reed@google.com8926b162012-03-23 15:36:36 +00001652
1653 SkAutoLockPixels alp(src, !src.getTexture());
1654 if (!src.getTexture() && !src.readyToDraw()) {
1655 return false;
1656 }
1657
1658 GrPaint paint;
1659 paint.reset();
1660
1661 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1662
1663 GrTexture* texture;
1664 SkAutoCachedTexture act(this, src, sampler, &texture);
1665
1666 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001667 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1668 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001669 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1670 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001671 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1672 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001673 resultTexture->unref();
1674 }
reed@google.com76dd2772012-01-05 21:15:07 +00001675 return true;
1676}
1677
reed@google.comac10a2d2010-12-22 21:39:39 +00001678///////////////////////////////////////////////////////////////////////////////
1679
1680// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001681static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001682 kTriangles_GrPrimitiveType,
1683 kTriangleStrip_GrPrimitiveType,
1684 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001685};
1686
1687void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1688 int vertexCount, const SkPoint vertices[],
1689 const SkPoint texs[], const SkColor colors[],
1690 SkXfermode* xmode,
1691 const uint16_t indices[], int indexCount,
1692 const SkPaint& paint) {
1693 CHECK_SHOULD_DRAW(draw);
1694
bsalomon@google.com5782d712011-01-21 21:03:59 +00001695 GrPaint grPaint;
1696 SkAutoCachedTexture act;
1697 // we ignore the shader if texs is null.
1698 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001699 if (!skPaint2GrPaintNoShader(paint,
1700 false,
1701 NULL == colors,
1702 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001703 return;
1704 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001705 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001706 if (!skPaint2GrPaintShader(this,
1707 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001708 NULL == colors,
1709 &act,
1710 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001711 return;
1712 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001713 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001714
1715 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001716 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001717 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1718#if 0
1719 return
1720#endif
1721 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001722 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001723
bsalomon@google.com498776a2011-08-16 19:20:44 +00001724 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1725 if (NULL != colors) {
1726 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001727 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001728 for (int i = 0; i < vertexCount; ++i) {
1729 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1730 }
1731 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001732 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001733 fContext->drawVertices(grPaint,
1734 gVertexMode2PrimitiveType[vmode],
1735 vertexCount,
1736 (GrPoint*) vertices,
1737 (GrPoint*) texs,
1738 colors,
1739 indices,
1740 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001741}
1742
1743///////////////////////////////////////////////////////////////////////////////
1744
1745static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001746 GrFontScaler* scaler = (GrFontScaler*)data;
1747 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001748}
1749
1750static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1751 void* auxData;
1752 GrFontScaler* scaler = NULL;
1753 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1754 scaler = (GrFontScaler*)auxData;
1755 }
1756 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001757 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001758 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1759 }
1760 return scaler;
1761}
1762
1763static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1764 SkFixed fx, SkFixed fy,
1765 const SkGlyph& glyph) {
1766 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1767
bungeman@google.com15865a72012-01-11 16:28:04 +00001768 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001769
1770 if (NULL == procs->fFontScaler) {
1771 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1772 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001773
bungeman@google.com15865a72012-01-11 16:28:04 +00001774 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1775 glyph.getSubXFixed(),
1776 glyph.getSubYFixed()),
1777 SkFixedFloorToFixed(fx),
1778 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001779 procs->fFontScaler);
1780}
1781
bsalomon@google.com5782d712011-01-21 21:03:59 +00001782SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001783
1784 // deferred allocation
1785 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001786 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001787 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1788 fDrawProcs->fContext = fContext;
1789 }
1790
1791 // init our (and GL's) state
1792 fDrawProcs->fTextContext = context;
1793 fDrawProcs->fFontScaler = NULL;
1794 return fDrawProcs;
1795}
1796
1797void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1798 size_t byteLength, SkScalar x, SkScalar y,
1799 const SkPaint& paint) {
1800 CHECK_SHOULD_DRAW(draw);
1801
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001802 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001803 // this guy will just call our drawPath()
1804 draw.drawText((const char*)text, byteLength, x, y, paint);
1805 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001806 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001807
1808 GrPaint grPaint;
1809 SkAutoCachedTexture act;
1810
bsalomon@google.com84405e02012-03-05 19:57:21 +00001811 if (!skPaint2GrPaintShader(this,
1812 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001813 true,
1814 &act,
1815 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001816 return;
1817 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001818 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1819 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001820 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1821 }
1822}
1823
1824void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1825 size_t byteLength, const SkScalar pos[],
1826 SkScalar constY, int scalarsPerPos,
1827 const SkPaint& paint) {
1828 CHECK_SHOULD_DRAW(draw);
1829
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001830 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001831 // this guy will just call our drawPath()
1832 draw.drawPosText((const char*)text, byteLength, pos, constY,
1833 scalarsPerPos, paint);
1834 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001835 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001836
1837 GrPaint grPaint;
1838 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001839 if (!skPaint2GrPaintShader(this,
1840 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001841 true,
1842 &act,
1843 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001844 return;
1845 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001846 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1847 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001848 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1849 scalarsPerPos, paint);
1850 }
1851}
1852
1853void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1854 size_t len, const SkPath& path,
1855 const SkMatrix* m, const SkPaint& paint) {
1856 CHECK_SHOULD_DRAW(draw);
1857
1858 SkASSERT(draw.fDevice == this);
1859 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1860}
1861
1862///////////////////////////////////////////////////////////////////////////////
1863
reed@google.comf67e4cf2011-03-15 20:56:58 +00001864bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1865 if (!paint.isLCDRenderText()) {
1866 // we're cool with the paint as is
1867 return false;
1868 }
1869
1870 if (paint.getShader() ||
1871 paint.getXfermode() || // unless its srcover
1872 paint.getMaskFilter() ||
1873 paint.getRasterizer() ||
1874 paint.getColorFilter() ||
1875 paint.getPathEffect() ||
1876 paint.isFakeBoldText() ||
1877 paint.getStyle() != SkPaint::kFill_Style) {
1878 // turn off lcd
1879 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1880 flags->fHinting = paint.getHinting();
1881 return true;
1882 }
1883 // we're cool with the paint as is
1884 return false;
1885}
1886
reed@google.com75d939b2011-12-07 15:07:23 +00001887void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001888 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001889 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001890}
1891
reed@google.comf67e4cf2011-03-15 20:56:58 +00001892///////////////////////////////////////////////////////////////////////////////
1893
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001894SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1895 const SkBitmap& bitmap,
1896 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001897 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001898 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001899
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001900 if (!bitmap.isVolatile()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001901 uint64_t key = bitmap.getGenerationID();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001902 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001903
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001904 GrTextureDesc desc;
1905 desc.fWidth = bitmap.width();
1906 desc.fHeight = bitmap.height();
1907 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
robertphillips@google.com56f22442012-06-08 14:21:26 +00001908 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001909
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001910 entry = ctx->findAndLockTexture(desc, sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001911 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001912 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1913 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001914 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001915 } else {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001916 entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001917 sampler, bitmap);
1918 }
1919 if (NULL == entry.texture()) {
1920 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1921 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001922 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001923 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001924}
1925
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001926void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1927 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001928}
1929
bsalomon@google.comfb309512011-11-30 14:13:48 +00001930bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1931 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001932 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001933 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001934
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001935 GrTextureDesc desc;
1936 desc.fWidth = bitmap.width();
1937 desc.fHeight = bitmap.height();
1938 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
1939 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001940
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001941 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001942}
1943
1944
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001945SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1946 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001947 bool isOpaque,
1948 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001949 GrTextureDesc desc;
1950 desc.fConfig = fRenderTarget->config();
1951 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1952 desc.fWidth = width;
1953 desc.fHeight = height;
1954 desc.fSampleCnt = fRenderTarget->numSamples();
1955
1956 GrContext::TextureCacheEntry cacheEntry;
1957 GrTexture* texture;
1958 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001959 // Skia's convention is to only clear a device if it is non-opaque.
1960 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001961
1962#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1963 // layers are never draw in repeat modes, so we can request an approx
1964 // match and ignore any padding.
1965 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1966 GrContext::kApprox_ScratchTexMatch :
1967 GrContext::kExact_ScratchTexMatch;
1968 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1969 texture = cacheEntry.texture();
1970#else
1971 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1972 texture = tunref.get();
1973#endif
1974 if (texture) {
1975 return SkNEW_ARGS(SkGpuDevice,(fContext,
1976 texture,
1977 cacheEntry,
1978 needClear));
1979 } else {
1980 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1981 width, height);
1982 return NULL;
1983 }
1984}
1985
1986SkGpuDevice::SkGpuDevice(GrContext* context,
1987 GrTexture* texture,
1988 TexCache cacheEntry,
1989 bool needClear)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001990 : SkDevice(make_bitmap(context, texture->asRenderTarget()))
1991 , fClipStack(NULL) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001992 GrAssert(texture && texture->asRenderTarget());
1993 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1994 this->initFromRenderTarget(context, texture->asRenderTarget());
1995 fCache = cacheEntry;
1996 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001997}
1998