blob: 6443fa28e768b67d2dcf24f7d7c4c531846ccbe3 [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"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000011
reed@google.comac10a2d2010-12-22 21:39:39 +000012#include "GrContext.h"
13#include "GrTextContext.h"
14
robertphillips@google.come9c04692012-06-29 00:30:13 +000015#include "SkGrTexturePixelRef.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
Scroggo97c88c22011-05-11 14:05:25 +000017#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018#include "SkDrawProcs.h"
19#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000020#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000021#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000022#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
bsalomon@google.com06cd7322012-03-30 18:45:35 +000024#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000025
26#if 0
27 extern bool (*gShouldDrawProc)();
28 #define CHECK_SHOULD_DRAW(draw) \
29 do { \
30 if (gShouldDrawProc && !gShouldDrawProc()) return; \
31 this->prepareRenderTarget(draw); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000032 GrAssert(!fNeedClear) \
reed@google.comac10a2d2010-12-22 21:39:39 +000033 } while (0)
34#else
bsalomon@google.com06cd7322012-03-30 18:45:35 +000035 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \
36 GrAssert(!fNeedClear)
reed@google.comac10a2d2010-12-22 21:39:39 +000037#endif
38
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000039// we use the same texture slot on GrPaint for bitmaps and shaders
40// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
41enum {
42 kBitmapTextureIdx = 0,
43 kShaderTextureIdx = 0
44};
45
reed@google.comcde92112011-07-06 20:00:52 +000046
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000047#define MAX_BLUR_SIGMA 4.0f
48// FIXME: This value comes from from SkBlurMaskFilter.cpp.
49// Should probably be put in a common header someplace.
50#define MAX_BLUR_RADIUS SkIntToScalar(128)
51// This constant approximates the scaling done in the software path's
52// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
53// IMHO, it actually should be 1: we blur "less" than we should do
54// according to the CSS and canvas specs, simply because Safari does the same.
55// Firefox used to do the same too, until 4.0 where they fixed it. So at some
56// point we should probably get rid of these scaling constants and rebaseline
57// all the blur tests.
58#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000059// This constant represents the screen alignment criterion in texels for
60// requiring texture domain clamping to prevent color bleeding when drawing
61// a sub region of a larger source image.
62#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000063
64#define DO_DEFERRED_CLEAR \
65 do { \
66 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000067 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000068 fNeedClear = false; \
69 } \
70 } while (false) \
71
reed@google.comac10a2d2010-12-22 21:39:39 +000072///////////////////////////////////////////////////////////////////////////////
73
reed@google.comb0a34d82012-07-11 19:57:55 +000074#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
75 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
76
77///////////////////////////////////////////////////////////////////////////////
78
79
bsalomon@google.com84405e02012-03-05 19:57:21 +000080class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
81public:
82 SkAutoCachedTexture() { }
83 SkAutoCachedTexture(SkGpuDevice* device,
84 const SkBitmap& bitmap,
85 const GrSamplerState* sampler,
86 GrTexture** texture) {
87 GrAssert(texture);
88 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000089 }
reed@google.comac10a2d2010-12-22 21:39:39 +000090
bsalomon@google.com84405e02012-03-05 19:57:21 +000091 ~SkAutoCachedTexture() {
92 if (fTex.texture()) {
93 fDevice->unlockCachedTexture(fTex);
94 }
reed@google.comac10a2d2010-12-22 21:39:39 +000095 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000096
97 GrTexture* set(SkGpuDevice* device,
98 const SkBitmap& bitmap,
99 const GrSamplerState* sampler) {
100 if (fTex.texture()) {
101 fDevice->unlockCachedTexture(fTex);
102 }
103 fDevice = device;
104 GrTexture* texture = (GrTexture*)bitmap.getTexture();
105 if (texture) {
106 // return the native texture
107 fTex.reset();
108 } else {
109 // look it up in our cache
110 fTex = device->lockCachedTexture(bitmap, sampler);
111 texture = fTex.texture();
112 }
113 return texture;
114 }
115
116private:
117 SkGpuDevice* fDevice;
118 GrContext::TextureCacheEntry fTex;
119};
reed@google.comac10a2d2010-12-22 21:39:39 +0000120
121///////////////////////////////////////////////////////////////////////////////
122
123bool gDoTraceDraw;
124
125struct GrSkDrawProcs : public SkDrawProcs {
126public:
127 GrContext* fContext;
128 GrTextContext* fTextContext;
129 GrFontScaler* fFontScaler; // cached in the skia glyphcache
130};
131
132///////////////////////////////////////////////////////////////////////////////
133
reed@google.comaf951c92011-06-16 19:10:39 +0000134static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
135 switch (config) {
136 case kAlpha_8_GrPixelConfig:
137 *isOpaque = false;
138 return SkBitmap::kA8_Config;
139 case kRGB_565_GrPixelConfig:
140 *isOpaque = true;
141 return SkBitmap::kRGB_565_Config;
142 case kRGBA_4444_GrPixelConfig:
143 *isOpaque = false;
144 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000145 case kSkia8888_PM_GrPixelConfig:
146 // we don't currently have a way of knowing whether
147 // a 8888 is opaque based on the config.
148 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000149 return SkBitmap::kARGB_8888_Config;
150 default:
151 *isOpaque = false;
152 return SkBitmap::kNo_Config;
153 }
154}
reed@google.comac10a2d2010-12-22 21:39:39 +0000155
reed@google.comaf951c92011-06-16 19:10:39 +0000156static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000157 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000158
159 bool isOpaque;
160 SkBitmap bitmap;
161 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
162 renderTarget->width(), renderTarget->height());
163 bitmap.setIsOpaque(isOpaque);
164 return bitmap;
165}
166
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000167SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000168: SkDevice(make_bitmap(context, texture->asRenderTarget()))
169, fClipStack(NULL) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000170 this->initFromRenderTarget(context, texture->asRenderTarget());
171}
172
reed@google.comaf951c92011-06-16 19:10:39 +0000173SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000174: SkDevice(make_bitmap(context, renderTarget))
175, fClipStack(NULL) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000176 this->initFromRenderTarget(context, renderTarget);
177}
178
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000179void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000180 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000181 fNeedPrepareRenderTarget = false;
182 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000183
reed@google.comaf951c92011-06-16 19:10:39 +0000184 fContext = context;
185 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000186
reed@google.comaf951c92011-06-16 19:10:39 +0000187 fTexture = NULL;
188 fRenderTarget = NULL;
189 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000190
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000191 GrAssert(NULL != renderTarget);
192 fRenderTarget = renderTarget;
193 fRenderTarget->ref();
194 // if this RT is also a texture, hold a ref on it
195 fTexture = fRenderTarget->asTexture();
196 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000197
198 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
199 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
200 // the device via accessBitmap. This external ref may outlive the device.
201 // Since textures own their render targets (but not vice-versa) we
202 // are ensuring that both objects will live as long as the pixel ref.
203 SkPixelRef* pr;
204 if (fTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000205 pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000206 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000207 pr = SkNEW_ARGS(SkGrRenderTargetPixelRef, (fRenderTarget));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000208 }
reed@google.comaf951c92011-06-16 19:10:39 +0000209 this->setPixelRef(pr, 0)->unref();
210}
211
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000212SkGpuDevice::SkGpuDevice(GrContext* context,
213 SkBitmap::Config config,
214 int width,
215 int height)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000216 : SkDevice(config, width, height, false /*isOpaque*/)
217 , fClipStack(NULL) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 fNeedPrepareRenderTarget = false;
219 fDrawProcs = NULL;
220
reed@google.com7b201d22011-01-11 18:59:23 +0000221 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000222 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000223
reed@google.comac10a2d2010-12-22 21:39:39 +0000224 fTexture = NULL;
225 fRenderTarget = NULL;
226 fNeedClear = false;
227
reed@google.comaf951c92011-06-16 19:10:39 +0000228 if (config != SkBitmap::kRGB_565_Config) {
229 config = SkBitmap::kARGB_8888_Config;
230 }
231 SkBitmap bm;
232 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000234 GrTextureDesc desc;
235 desc.fFlags = kRenderTarget_GrTextureFlagBit;
236 desc.fWidth = width;
237 desc.fHeight = height;
238 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
reed@google.comaf951c92011-06-16 19:10:39 +0000240 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000241
reed@google.comaf951c92011-06-16 19:10:39 +0000242 if (NULL != fTexture) {
243 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000244 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000245
reed@google.comaf951c92011-06-16 19:10:39 +0000246 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000247
reed@google.comaf951c92011-06-16 19:10:39 +0000248 // wrap the bitmap with a pixelref to expose our texture
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000249 SkGrTexturePixelRef* pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000250 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000251 } else {
252 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
253 width, height);
254 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000255 }
256}
257
258SkGpuDevice::~SkGpuDevice() {
259 if (fDrawProcs) {
260 delete fDrawProcs;
261 }
262
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000263 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
264 // This call gives the context a chance to relinquish it
265 fContext->setRenderTarget(NULL);
266
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000267 SkSafeUnref(fTexture);
268 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000269 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000270 GrAssert(NULL != fTexture);
271 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000272 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000273 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000274 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000275}
276
reed@google.comac10a2d2010-12-22 21:39:39 +0000277///////////////////////////////////////////////////////////////////////////////
278
279void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000280 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000281 fContext->setRenderTarget(fRenderTarget);
282 fContext->flush(true);
283 fNeedPrepareRenderTarget = true;
284}
285
286///////////////////////////////////////////////////////////////////////////////
287
bsalomon@google.comc4364992011-11-07 15:54:49 +0000288namespace {
289GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
290 switch (config8888) {
291 case SkCanvas::kNative_Premul_Config8888:
292 return kSkia8888_PM_GrPixelConfig;
293 case SkCanvas::kNative_Unpremul_Config8888:
294 return kSkia8888_UPM_GrPixelConfig;
295 case SkCanvas::kBGRA_Premul_Config8888:
296 return kBGRA_8888_PM_GrPixelConfig;
297 case SkCanvas::kBGRA_Unpremul_Config8888:
298 return kBGRA_8888_UPM_GrPixelConfig;
299 case SkCanvas::kRGBA_Premul_Config8888:
300 return kRGBA_8888_PM_GrPixelConfig;
301 case SkCanvas::kRGBA_Unpremul_Config8888:
302 return kRGBA_8888_UPM_GrPixelConfig;
303 default:
304 GrCrash("Unexpected Config8888.");
305 return kSkia8888_PM_GrPixelConfig;
306 }
307}
308}
309
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000310bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
311 int x, int y,
312 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000313 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000314 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
315 SkASSERT(!bitmap.isNull());
316 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000317
bsalomon@google.com910267d2011-11-02 20:06:25 +0000318 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000319 GrPixelConfig config;
320 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000321 return fContext->readRenderTargetPixels(fRenderTarget,
322 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000323 bitmap.width(),
324 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000325 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000326 bitmap.getPixels(),
327 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000328}
329
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000330void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
331 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000332 SkAutoLockPixels alp(bitmap);
333 if (!bitmap.readyToDraw()) {
334 return;
335 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000336
337 GrPixelConfig config;
338 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
339 config = config8888_to_gr_config(config8888);
340 } else {
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000341 config= SkGr::BitmapConfig2PixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000342 }
343
bsalomon@google.com6f379512011-11-16 20:36:03 +0000344 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
345 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000346}
347
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000348void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
349 INHERITED::onAttachToCanvas(canvas);
350
351 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
352 fClipStack = canvas->getClipStack();
353}
354
355void SkGpuDevice::onDetachFromCanvas() {
356 INHERITED::onDetachFromCanvas();
357
358 fClipStack = NULL;
359}
360
reed@google.comac10a2d2010-12-22 21:39:39 +0000361///////////////////////////////////////////////////////////////////////////////
362
363static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000364 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000365 const SkRegion& clipRegion,
366 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000367 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000368
369 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000370 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000371 const SkIRect& skBounds = clipRegion.getBounds();
372 GrRect bounds;
373 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
374 GrIntToScalar(skBounds.fTop),
375 GrIntToScalar(skBounds.fRight),
376 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000377 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000378 bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000379 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000380}
381
382// call this ever each draw call, to ensure that the context reflects our state,
383// and not the state from some other canvas/device
384void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000385 GrAssert(NULL != fClipStack);
386
reed@google.comac10a2d2010-12-22 21:39:39 +0000387 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000388 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000389
390 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000391 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000392 convert_matrixclip(fContext, *draw.fMatrix,
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000393 *fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000394 fNeedPrepareRenderTarget = false;
395 }
396}
397
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000398void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
399 const SkClipStack& clipStack) {
400 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
401 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000402 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000403}
404
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000405void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
406
407 GrAssert(NULL != fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000408
reed@google.comac10a2d2010-12-22 21:39:39 +0000409 fContext->setRenderTarget(fRenderTarget);
410
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000411 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000412
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000413 convert_matrixclip(fContext, matrix, *fClipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000414
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000415 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000416}
417
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000418SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000419 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000420 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000421}
422
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000423bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000424 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000425 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000426 return true;
427 }
428 return false;
429}
430
431///////////////////////////////////////////////////////////////////////////////
432
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000433SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
434SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
435SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
436SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
437SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
438 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000439SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
440 shader_type_mismatch);
441SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 5, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000442
bsalomon@google.com84405e02012-03-05 19:57:21 +0000443namespace {
444
445// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
446// justAlpha indicates that skPaint's alpha should be used rather than the color
447// Callers may subsequently modify the GrPaint. Setting constantColor indicates
448// that the final paint will draw the same color at every pixel. This allows
449// an optimization where the the color filter can be applied to the skPaint's
450// color once while converting to GrPain and then ignored.
451inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
452 bool justAlpha,
453 bool constantColor,
454 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000455
456 grPaint->fDither = skPaint.isDither();
457 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000458 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000459
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000460 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
461 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000462
463 SkXfermode* mode = skPaint.getXfermode();
464 if (mode) {
465 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000466 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000467#if 0
468 return false;
469#endif
470 }
471 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000472 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
473 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
474
bsalomon@google.com5782d712011-01-21 21:03:59 +0000475 if (justAlpha) {
476 uint8_t alpha = skPaint.getAlpha();
477 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000478 // justAlpha is currently set to true only if there is a texture,
479 // so constantColor should not also be true.
480 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000481 } else {
482 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000483 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000484 }
Scroggo97c88c22011-05-11 14:05:25 +0000485 SkColorFilter* colorFilter = skPaint.getColorFilter();
486 SkColor color;
487 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000488 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000489 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000490 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000491 if (!constantColor) {
492 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
493 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000494 } else {
495 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
496 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000497 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000498 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000499 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
500 grPaint->fColorMatrixEnabled = true;
501 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
502 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
503 } else {
504 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000505 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000506 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000507}
508
bsalomon@google.com84405e02012-03-05 19:57:21 +0000509// This function is similar to skPaint2GrPaintNoShader but also converts
510// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
511// be used is set on grPaint and returned in param act. constantColor has the
512// same meaning as in skPaint2GrPaintNoShader.
513inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
514 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000515 bool constantColor,
516 SkGpuDevice::SkAutoCachedTexture* act,
517 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000518
bsalomon@google.com5782d712011-01-21 21:03:59 +0000519 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000520
bsalomon@google.com5782d712011-01-21 21:03:59 +0000521 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000522 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000523 return skPaint2GrPaintNoShader(skPaint,
524 false,
525 constantColor,
526 grPaint);
527 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000528 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000529 }
530
reed@google.comac10a2d2010-12-22 21:39:39 +0000531 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000532 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000533 SkShader::TileMode tileModes[2];
534 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000535 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000536 tileModes, twoPointParams);
537
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000538 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000539 SkShader::GradientInfo info;
540 SkColor color;
541
542 info.fColors = &color;
543 info.fColorOffsets = NULL;
544 info.fColorCount = 1;
545 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
546 SkPaint copy(skPaint);
547 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000548 // modulate the paint alpha by the shader's solid color alpha
549 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
550 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000551 return skPaint2GrPaintNoShader(copy,
552 false,
553 constantColor,
554 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000555 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000556 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000557 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000558
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000559 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000560 GrTexture* texture = act->set(dev, bitmap, sampler);
561 if (NULL == texture) {
562 SkDebugf("Couldn't convert bitmap to texture.\n");
563 return false;
564 }
565 grPaint->setTexture(kShaderTextureIdx, texture);
566
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 }
598 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000599 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000600 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
601 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000602
reed@google.comac10a2d2010-12-22 21:39:39 +0000603 // since our texture coords will be in local space, we wack the texture
604 // matrix to map them back into 0...1 before we load it
605 SkMatrix localM;
606 if (shader->getLocalMatrix(&localM)) {
607 SkMatrix inverse;
608 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000609 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000610 }
611 }
612 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000613 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
614 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000615 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000616 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000617 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000618 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000619 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000620
621 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000622}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000623}
reed@google.comac10a2d2010-12-22 21:39:39 +0000624
625///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000626void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000627 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000628}
629
reed@google.comac10a2d2010-12-22 21:39:39 +0000630void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
631 CHECK_SHOULD_DRAW(draw);
632
bsalomon@google.com5782d712011-01-21 21:03:59 +0000633 GrPaint grPaint;
634 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000635 if (!skPaint2GrPaintShader(this,
636 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000637 true,
638 &act,
639 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000640 return;
641 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000642
643 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000644}
645
646// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000647static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000648 kPoints_GrPrimitiveType,
649 kLines_GrPrimitiveType,
650 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000651};
652
653void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000654 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000655 CHECK_SHOULD_DRAW(draw);
656
657 SkScalar width = paint.getStrokeWidth();
658 if (width < 0) {
659 return;
660 }
661
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000662 // we only handle hairlines and paints without path effects or mask filters,
663 // else we let the SkDraw call our drawPath()
664 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000665 draw.drawPoints(mode, count, pts, paint, true);
666 return;
667 }
668
bsalomon@google.com5782d712011-01-21 21:03:59 +0000669 GrPaint grPaint;
670 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000671 if (!skPaint2GrPaintShader(this,
672 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000673 true,
674 &act,
675 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000676 return;
677 }
678
bsalomon@google.com5782d712011-01-21 21:03:59 +0000679 fContext->drawVertices(grPaint,
680 gPointMode2PrimtiveType[mode],
681 count,
682 (GrPoint*)pts,
683 NULL,
684 NULL,
685 NULL,
686 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000687}
688
reed@google.comc9aa5872011-04-05 21:05:37 +0000689///////////////////////////////////////////////////////////////////////////////
690
reed@google.comac10a2d2010-12-22 21:39:39 +0000691void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
692 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000693 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000694 CHECK_SHOULD_DRAW(draw);
695
bungeman@google.com79bd8772011-07-18 15:34:08 +0000696 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000697 SkScalar width = paint.getStrokeWidth();
698
699 /*
700 We have special code for hairline strokes, miter-strokes, and fills.
701 Anything else we just call our path code.
702 */
703 bool usePath = doStroke && width > 0 &&
704 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000705 // another two reasons we might need to call drawPath...
706 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000707 usePath = true;
708 }
reed@google.com67db6642011-05-26 11:46:35 +0000709 // until we aa rotated rects...
710 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
711 usePath = true;
712 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000713 // small miter limit means right angles show bevel...
714 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
715 paint.getStrokeMiter() < SK_ScalarSqrt2)
716 {
717 usePath = true;
718 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000719 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000720 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
721 usePath = true;
722 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000723
724 if (usePath) {
725 SkPath path;
726 path.addRect(rect);
727 this->drawPath(draw, path, paint, NULL, true);
728 return;
729 }
730
731 GrPaint grPaint;
732 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000733 if (!skPaint2GrPaintShader(this,
734 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000735 true,
736 &act,
737 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000738 return;
739 }
reed@google.com20efde72011-05-09 17:00:02 +0000740 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000741}
742
reed@google.com69302852011-02-16 18:08:07 +0000743#include "SkMaskFilter.h"
744#include "SkBounder.h"
745
bsalomon@google.com85003222012-03-28 14:44:37 +0000746///////////////////////////////////////////////////////////////////////////////
747
748// helpers for applying mask filters
749namespace {
750
751GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000752 switch (fillType) {
753 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000754 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000755 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000756 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000757 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000758 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000759 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000760 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000761 default:
762 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000763 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000764 }
765}
766
bsalomon@google.com85003222012-03-28 14:44:37 +0000767// We prefer to blur small rect with small radius via CPU.
768#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
769#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
770inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
771 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
772 rect.height() <= MIN_GPU_BLUR_SIZE &&
773 radius <= MIN_GPU_BLUR_RADIUS) {
774 return true;
775 }
776 return false;
777}
778
779bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
780 SkMaskFilter* filter, const SkMatrix& matrix,
781 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000782 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000783#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000784 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000785#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000786 SkMaskFilter::BlurInfo info;
787 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000788 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000789 return false;
790 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000791 SkScalar radius = info.fIgnoreTransform ? info.fRadius
792 : matrix.mapRadius(info.fRadius);
793 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000794 if (radius <= 0) {
795 return false;
796 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000797
798 SkRect srcRect = path.getBounds();
799 if (shouldDrawBlurWithCPU(srcRect, radius)) {
800 return false;
801 }
802
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000803 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000804 float sigma3 = sigma * 3.0f;
805
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000806 SkRect clipRect;
807 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000808
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000809 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000810 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
811 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000812 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000813 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000814 SkIRect finalIRect;
815 finalRect.roundOut(&finalIRect);
816 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000817 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 }
819 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000820 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000821 }
822 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000823 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000824 GrTextureDesc desc;
825 desc.fFlags = kRenderTarget_GrTextureFlagBit;
826 desc.fWidth = SkScalarCeilToInt(srcRect.width());
827 desc.fHeight = SkScalarCeilToInt(srcRect.height());
828 // We actually only need A8, but it often isn't supported as a
829 // render target so default to RGBA_8888
830 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000831
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000832 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
833 desc.fConfig = kAlpha_8_GrPixelConfig;
834 }
835
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000836 GrAutoScratchTexture pathEntry(context, desc);
837 GrTexture* pathTexture = pathEntry.texture();
838 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000839 return false;
840 }
841 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000842 // Once this code moves into GrContext, this should be changed to use
843 // an AutoClipRestore.
844 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000845 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000846
847 GrClip newClip(srcRect);
848 context->setClip(newClip);
849
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000850 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000851 GrPaint tempPaint;
852 tempPaint.reset();
853
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000854 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000855 tempPaint.fAntiAlias = grp->fAntiAlias;
856 if (tempPaint.fAntiAlias) {
857 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
858 // blend coeff of zero requires dual source blending support in order
859 // to properly blend partially covered pixels. This means the AA
860 // code path may not be taken. So we use a dst blend coeff of ISA. We
861 // could special case AA draws to a dst surface with known alpha=0 to
862 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000863 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
864 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000865 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000866 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000867 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000868
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000869 GrAutoScratchTexture temp1, temp2;
870 // If we're doing a normal blur, we can clobber the pathTexture in the
871 // gaussianBlur. Otherwise, we need to save it for later compositing.
872 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000873 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
874 &temp1,
875 isNormalBlur ? NULL : &temp2,
876 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000877
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000878 if (!isNormalBlur) {
879 GrPaint paint;
880 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000881 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000882 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
883 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000884 // Blend pathTexture over blurTexture.
885 context->setRenderTarget(blurTexture->asRenderTarget());
886 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000887 if (SkMaskFilter::kInner_BlurType == blurType) {
888 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000889 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
890 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000891 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
892 // solid: dst = src + dst - src * dst
893 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000894 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
895 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000896 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
897 // outer: dst = dst * (1 - src)
898 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000899 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
900 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000901 }
902 context->drawRect(paint, srcRect);
903 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000904 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000905 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000906
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000907 if (grp->hasTextureOrMask()) {
908 GrMatrix inverse;
909 if (!matrix.invert(&inverse)) {
910 return false;
911 }
912 grp->preConcatActiveSamplerMatrices(inverse);
913 }
914
915 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
916 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000917 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000918 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000919 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000920
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000921 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
922 -finalRect.fTop);
923 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
924 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000925 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000926 return true;
927}
928
bsalomon@google.com85003222012-03-28 14:44:37 +0000929bool drawWithMaskFilter(GrContext* context, const SkPath& path,
930 SkMaskFilter* filter, const SkMatrix& matrix,
931 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000932 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000933 SkMask srcM, dstM;
934
935 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000936 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000937 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000938 return false;
939 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000940 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000941
942 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
943 return false;
944 }
945 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000946 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000947
948 if (clip.quickReject(dstM.fBounds)) {
949 return false;
950 }
951 if (bounder && !bounder->doIRect(dstM.fBounds)) {
952 return false;
953 }
954
955 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
956 // the current clip (and identity matrix) and grpaint settings
957
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000958 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000959 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000960
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000961 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000962
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000963 GrTextureDesc desc;
964 desc.fWidth = dstM.fBounds.width();
965 desc.fHeight = dstM.fBounds.height();
966 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000967
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000968 GrAutoScratchTexture ast(context, desc);
969 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000970
reed@google.com69302852011-02-16 18:08:07 +0000971 if (NULL == texture) {
972 return false;
973 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000974 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000975 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000976
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000977 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
978 grp->preConcatActiveSamplerMatrices(ivm);
979 }
980
981 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
982 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000983 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000984 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000985 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000986
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000987 GrRect d;
988 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000989 GrIntToScalar(dstM.fBounds.fTop),
990 GrIntToScalar(dstM.fBounds.fRight),
991 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000992
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000993 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
994 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
995 -dstM.fBounds.fTop*SK_Scalar1);
996 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000997 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000998 return true;
999}
reed@google.com69302852011-02-16 18:08:07 +00001000
bsalomon@google.com85003222012-03-28 14:44:37 +00001001}
1002
1003///////////////////////////////////////////////////////////////////////////////
1004
reed@google.com0c219b62011-02-16 21:31:18 +00001005void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001006 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001007 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001008 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001009 CHECK_SHOULD_DRAW(draw);
1010
reed@google.comfe626382011-09-21 13:50:35 +00001011 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001012
bsalomon@google.com5782d712011-01-21 21:03:59 +00001013 GrPaint grPaint;
1014 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001015 if (!skPaint2GrPaintShader(this,
1016 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001017 true,
1018 &act,
1019 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001020 return;
1021 }
1022
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001023 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1024 // if we can, we draw lots faster (raster device does this same test)
1025 SkScalar hairlineCoverage;
1026 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1027 doFill = false;
1028 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1029 grPaint.fCoverage);
1030 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001031
reed@google.comfe626382011-09-21 13:50:35 +00001032 // If we have a prematrix, apply it to the path, optimizing for the case
1033 // where the original path can in fact be modified in place (even though
1034 // its parameter type is const).
1035 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1036 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001037
1038 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001039 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001040
reed@google.come3445642011-02-16 23:20:39 +00001041 if (!pathIsMutable) {
1042 result = &tmpPath;
1043 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001044 }
reed@google.come3445642011-02-16 23:20:39 +00001045 // should I push prePathMatrix on our MV stack temporarily, instead
1046 // of applying it here? See SkDraw.cpp
1047 pathPtr->transform(*prePathMatrix, result);
1048 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001049 }
reed@google.com0c219b62011-02-16 21:31:18 +00001050 // at this point we're done with prePathMatrix
1051 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001052
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001053 if (paint.getPathEffect() ||
1054 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001055 // it is safe to use tmpPath here, even if we already used it for the
1056 // prepathmatrix, since getFillPath can take the same object for its
1057 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001058 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001059 pathPtr = &tmpPath;
1060 }
1061
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001062 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001063 // avoid possibly allocating a new path in transform if we can
1064 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1065
1066 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001067 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001068 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001069 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001070 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001071 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001072 &grPaint, pathFillType)) {
1073 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1074 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001075 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001076 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001077 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001078 }
reed@google.com69302852011-02-16 18:08:07 +00001079 return;
1080 }
reed@google.com69302852011-02-16 18:08:07 +00001081
bsalomon@google.com47059542012-06-06 20:51:20 +00001082 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001083
reed@google.com0c219b62011-02-16 21:31:18 +00001084 if (doFill) {
1085 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001086 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001087 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001088 break;
1089 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001090 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 break;
1092 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001093 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001094 break;
1095 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001096 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001097 break;
1098 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001099 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 return;
1101 }
1102 }
1103
reed@google.com07f3ee12011-05-16 17:21:57 +00001104 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001105}
1106
bsalomon@google.comfb309512011-11-30 14:13:48 +00001107namespace {
1108
1109inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1110 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1111 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1112 return tilesX * tilesY;
1113}
1114
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001115inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001116 const SkIRect* srcRectPtr,
1117 int maxTextureSize) {
1118 static const int kSmallTileSize = 1 << 10;
1119 if (maxTextureSize <= kSmallTileSize) {
1120 return maxTextureSize;
1121 }
1122
1123 size_t maxTexTotalTileSize;
1124 size_t smallTotalTileSize;
1125
1126 if (NULL == srcRectPtr) {
1127 int w = bitmap.width();
1128 int h = bitmap.height();
1129 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1130 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1131 } else {
1132 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1133 srcRectPtr->fTop,
1134 srcRectPtr->fRight,
1135 srcRectPtr->fBottom,
1136 maxTextureSize);
1137 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1138 srcRectPtr->fTop,
1139 srcRectPtr->fRight,
1140 srcRectPtr->fBottom,
1141 kSmallTileSize);
1142 }
1143 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1144 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1145
1146 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1147 return kSmallTileSize;
1148 } else {
1149 return maxTextureSize;
1150 }
1151}
1152}
1153
1154bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1155 const GrSamplerState& sampler,
1156 const SkIRect* srcRectPtr,
1157 int* tileSize) const {
1158 SkASSERT(NULL != tileSize);
1159
1160 // if bitmap is explictly texture backed then just use the texture
1161 if (NULL != bitmap.getTexture()) {
1162 return false;
1163 }
1164 // if it's larger than the max texture size, then we have no choice but
1165 // tiling
1166 const int maxTextureSize = fContext->getMaxTextureSize();
1167 if (bitmap.width() > maxTextureSize ||
1168 bitmap.height() > maxTextureSize) {
1169 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1170 return true;
1171 }
1172 // if we are going to have to draw the whole thing, then don't tile
1173 if (NULL == srcRectPtr) {
1174 return false;
1175 }
1176 // if the entire texture is already in our cache then no reason to tile it
1177 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1178 return false;
1179 }
1180
1181 // At this point we know we could do the draw by uploading the entire bitmap
1182 // as a texture. However, if the texture would be large compared to the
1183 // cache size and we don't require most of it for this draw then tile to
1184 // reduce the amount of upload and cache spill.
1185
1186 // assumption here is that sw bitmap size is a good proxy for its size as
1187 // a texture
1188 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001189 size_t cacheSize;
1190 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001191 if (bmpSize < cacheSize / 2) {
1192 return false;
1193 }
1194
1195 SkFixed fracUsed =
1196 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1197 (srcRectPtr->height() << 16) / bitmap.height());
1198 if (fracUsed <= SK_FixedHalf) {
1199 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1200 return true;
1201 } else {
1202 return false;
1203 }
1204}
1205
reed@google.comac10a2d2010-12-22 21:39:39 +00001206void SkGpuDevice::drawBitmap(const SkDraw& draw,
1207 const SkBitmap& bitmap,
1208 const SkIRect* srcRectPtr,
1209 const SkMatrix& m,
1210 const SkPaint& paint) {
1211 CHECK_SHOULD_DRAW(draw);
1212
1213 SkIRect srcRect;
1214 if (NULL == srcRectPtr) {
1215 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1216 } else {
1217 srcRect = *srcRectPtr;
1218 }
1219
junov@google.comd935cfb2011-06-27 20:48:23 +00001220 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001221 // Convert the bitmap to a shader so that the rect can be drawn
1222 // through drawRect, which supports mask filters.
1223 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001224 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001225 if (srcRectPtr) {
1226 if (!bitmap.extractSubset(&tmp, srcRect)) {
1227 return; // extraction failed
1228 }
1229 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001230 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001231 }
1232 SkPaint paintWithTexture(paint);
1233 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1234 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001235 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001236 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001237
junov@google.com1d329782011-07-28 20:10:09 +00001238 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001239 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001240 // also affect the behavior of the mask filter.
1241 SkMatrix drawMatrix;
1242 drawMatrix.setConcat(*draw.fMatrix, m);
1243 SkDraw transformedDraw(draw);
1244 transformedDraw.fMatrix = &drawMatrix;
1245
1246 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1247
junov@google.comd935cfb2011-06-27 20:48:23 +00001248 return;
1249 }
1250
bsalomon@google.com5782d712011-01-21 21:03:59 +00001251 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001252 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001253 return;
1254 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001255 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001256 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001257 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001258 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001259 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001260 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001261
bsalomon@google.comfb309512011-11-30 14:13:48 +00001262 int tileSize;
1263 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1264 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001265 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001266 return;
1267 }
1268
1269 // undo the translate done by SkCanvas
1270 int DX = SkMax32(0, srcRect.fLeft);
1271 int DY = SkMax32(0, srcRect.fTop);
1272 // compute clip bounds in local coordinates
1273 SkIRect clipRect;
1274 {
1275 SkRect r;
1276 r.set(draw.fClip->getBounds());
1277 SkMatrix matrix, inverse;
1278 matrix.setConcat(*draw.fMatrix, m);
1279 if (!matrix.invert(&inverse)) {
1280 return;
1281 }
1282 inverse.mapRect(&r);
1283 r.roundOut(&clipRect);
1284 // apply the canvas' translate to our local clip
1285 clipRect.offset(DX, DY);
1286 }
1287
bsalomon@google.comfb309512011-11-30 14:13:48 +00001288 int nx = bitmap.width() / tileSize;
1289 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 for (int x = 0; x <= nx; x++) {
1291 for (int y = 0; y <= ny; y++) {
1292 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001293 tileR.set(x * tileSize, y * tileSize,
1294 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001295 if (!SkIRect::Intersects(tileR, clipRect)) {
1296 continue;
1297 }
1298
1299 SkIRect srcR = tileR;
1300 if (!srcR.intersect(srcRect)) {
1301 continue;
1302 }
1303
1304 SkBitmap tmpB;
1305 if (bitmap.extractSubset(&tmpB, tileR)) {
1306 // now offset it to make it "local" to our tmp bitmap
1307 srcR.offset(-tileR.fLeft, -tileR.fTop);
1308
1309 SkMatrix tmpM(m);
1310 {
1311 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1312 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1313 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1314 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001315 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 }
1317 }
1318 }
1319}
1320
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001321namespace {
1322
1323bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1324 // detect pixel disalignment
1325 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1326 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1327 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1328 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1329 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1330 COLOR_BLEED_TOLERANCE &&
1331 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1332 COLOR_BLEED_TOLERANCE) {
1333 return true;
1334 }
1335 return false;
1336}
1337
1338bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1339 const SkMatrix& m) {
1340 // Only gets called if hasAlignedSamples returned false.
1341 // So we can assume that sampling is axis aligned but not texel aligned.
1342 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1343 SkRect innerSrcRect(srcRect), innerTransformedRect,
1344 outerTransformedRect(transformedRect);
1345 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1346 m.mapRect(&innerTransformedRect, innerSrcRect);
1347
1348 // The gap between outerTransformedRect and innerTransformedRect
1349 // represents the projection of the source border area, which is
1350 // problematic for color bleeding. We must check whether any
1351 // destination pixels sample the border area.
1352 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1353 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1354 SkIRect outer, inner;
1355 outerTransformedRect.round(&outer);
1356 innerTransformedRect.round(&inner);
1357 // If the inner and outer rects round to the same result, it means the
1358 // border does not overlap any pixel centers. Yay!
1359 return inner != outer;
1360}
1361
1362} // unnamed namespace
1363
reed@google.comac10a2d2010-12-22 21:39:39 +00001364/*
1365 * This is called by drawBitmap(), which has to handle images that may be too
1366 * large to be represented by a single texture.
1367 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001368 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1369 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001370 */
1371void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1372 const SkBitmap& bitmap,
1373 const SkIRect& srcRect,
1374 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001375 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001376 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1377 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001378
reed@google.com9c49bc32011-07-07 13:42:37 +00001379 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001380 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001381 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001382 return;
1383 }
1384
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001385 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001386
1387 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1388 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001389 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001390
1391 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001392 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001393 if (NULL == texture) {
1394 return;
1395 }
1396
bsalomon@google.com452943d2011-10-31 17:37:14 +00001397 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001398
reed@google.com20efde72011-05-09 17:00:02 +00001399 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1400 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001401 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001402 float wInv = 1.f / bitmap.width();
1403 float hInv = 1.f / bitmap.height();
1404 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1405 SkFloatToScalar(srcRect.fTop * hInv),
1406 SkFloatToScalar(srcRect.fRight * wInv),
1407 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001408
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001409 bool needsTextureDomain = false;
1410 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1411 {
1412 // Need texture domain if drawing a sub rect.
1413 needsTextureDomain = srcRect.width() < bitmap.width() ||
1414 srcRect.height() < bitmap.height();
1415 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1416 // sampling is axis-aligned
1417 GrRect floatSrcRect, transformedRect;
1418 floatSrcRect.set(srcRect);
1419 SkMatrix srcToDeviceMatrix(m);
1420 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1421 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1422
1423 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1424 // Samples are texel-aligned, so filtering is futile
1425 sampler->setFilter(GrSamplerState::kNearest_Filter);
1426 needsTextureDomain = false;
1427 } else {
1428 needsTextureDomain = needsTextureDomain &&
1429 mayColorBleed(floatSrcRect, transformedRect, m);
1430 }
1431 }
1432 }
1433
1434 GrRect textureDomain = GrRect::MakeEmpty();
1435
1436 if (needsTextureDomain) {
1437 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001438 GrScalar left, top, right, bottom;
1439 if (srcRect.width() > 1) {
1440 GrScalar border = GR_ScalarHalf / bitmap.width();
1441 left = paintRect.left() + border;
1442 right = paintRect.right() - border;
1443 } else {
1444 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1445 }
1446 if (srcRect.height() > 1) {
1447 GrScalar border = GR_ScalarHalf / bitmap.height();
1448 top = paintRect.top() + border;
1449 bottom = paintRect.bottom() - border;
1450 } else {
1451 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1452 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001453 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001454 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001455 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001456
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001457 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001458}
1459
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001460namespace {
1461
1462void apply_custom_stage(GrContext* context,
1463 GrTexture* srcTexture,
1464 GrTexture* dstTexture,
1465 const GrRect& rect,
1466 GrCustomStage* stage) {
1467 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001468 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001469 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001470 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001471
1472 GrMatrix sampleM;
1473 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1474 GrPaint paint;
1475 paint.reset();
1476 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1477 paint.textureSampler(0)->reset(sampleM);
1478 paint.textureSampler(0)->setCustomStage(stage);
1479 paint.setTexture(0, srcTexture);
1480 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001481}
1482
1483};
1484
reed@google.com8926b162012-03-23 15:36:36 +00001485static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1486 SkImageFilter* filter, const GrRect& rect) {
1487 GrAssert(filter);
1488
1489 SkSize blurSize;
1490 SkISize radius;
1491
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001492 GrTextureDesc desc;
1493 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1494 desc.fWidth = SkScalarCeilToInt(rect.width());
1495 desc.fHeight = SkScalarCeilToInt(rect.height());
1496 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001497 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001498
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001499 if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001500 GrAutoScratchTexture dst(context, desc);
1501 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1502 texture = dst.detach();
1503 stage->unref();
1504 } else if (filter->asABlur(&blurSize)) {
reed@google.com8926b162012-03-23 15:36:36 +00001505 GrAutoScratchTexture temp1, temp2;
1506 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1507 blurSize.width(),
1508 blurSize.height());
1509 texture->ref();
1510 } else if (filter->asADilate(&radius)) {
1511 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1512 texture = context->applyMorphology(texture, rect,
1513 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001514 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001515 radius);
1516 texture->ref();
1517 } else if (filter->asAnErode(&radius)) {
1518 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1519 texture = context->applyMorphology(texture, rect,
1520 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001521 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001522 radius);
1523 texture->ref();
1524 }
1525 return texture;
1526}
1527
reed@google.comac10a2d2010-12-22 21:39:39 +00001528void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1529 int left, int top, const SkPaint& paint) {
1530 CHECK_SHOULD_DRAW(draw);
1531
reed@google.com8926b162012-03-23 15:36:36 +00001532 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001533 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1534 return;
1535 }
1536
reed@google.com76dd2772012-01-05 21:15:07 +00001537 int w = bitmap.width();
1538 int h = bitmap.height();
1539
bsalomon@google.com5782d712011-01-21 21:03:59 +00001540 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001541 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001542 return;
1543 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001544
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001545 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001546
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001547 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001548
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001549 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001550 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001551 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001552 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001553
reed@google.com8926b162012-03-23 15:36:36 +00001554 SkImageFilter* filter = paint.getImageFilter();
1555 if (NULL != filter) {
1556 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001557 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001558 if (filteredTexture) {
1559 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1560 texture = filteredTexture;
1561 filteredTexture->unref();
1562 }
reed@google.com76dd2772012-01-05 21:15:07 +00001563 }
reed@google.com8926b162012-03-23 15:36:36 +00001564
bsalomon@google.com5782d712011-01-21 21:03:59 +00001565 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001566 GrRect::MakeXYWH(GrIntToScalar(left),
1567 GrIntToScalar(top),
1568 GrIntToScalar(w),
1569 GrIntToScalar(h)),
1570 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1571 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001572}
1573
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001574void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001575 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001576 // clear of the source device must occur before CHECK_SHOULD_DRAW
1577 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1578 if (dev->fNeedClear) {
1579 // TODO: could check here whether we really need to draw at all
1580 dev->clear(0x0);
1581 }
1582
reed@google.comac10a2d2010-12-22 21:39:39 +00001583 CHECK_SHOULD_DRAW(draw);
1584
bsalomon@google.com5782d712011-01-21 21:03:59 +00001585 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001586 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001587 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001588 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001589 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001590
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001591 GrTexture* devTex = grPaint.getTexture(0);
1592 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001593
reed@google.com8926b162012-03-23 15:36:36 +00001594 SkImageFilter* filter = paint.getImageFilter();
1595 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001596 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1597 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001598 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1599 rect);
1600 if (filteredTexture) {
1601 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1602 devTex = filteredTexture;
1603 filteredTexture->unref();
1604 }
1605 }
1606
bsalomon@google.com5782d712011-01-21 21:03:59 +00001607 const SkBitmap& bm = dev->accessBitmap(false);
1608 int w = bm.width();
1609 int h = bm.height();
1610
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001611 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001612
bsalomon@google.com97912912011-12-06 16:30:36 +00001613 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001614
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001615 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1616 GrIntToScalar(y),
1617 GrIntToScalar(w),
1618 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001619
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001620 // The device being drawn may not fill up its texture (saveLayer uses
1621 // the approximate ).
1622 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1623 GR_Scalar1 * h / devTex->height());
1624
1625 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001626}
1627
reed@google.com8926b162012-03-23 15:36:36 +00001628bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001629 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001630 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001631
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001632 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001633 !filter->asABlur(&size) &&
1634 !filter->asADilate(&radius) &&
1635 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001636 return false;
1637 }
reed@google.com8926b162012-03-23 15:36:36 +00001638 return true;
1639}
1640
1641bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1642 const SkMatrix& ctm,
1643 SkBitmap* result, SkIPoint* offset) {
1644 // want explicitly our impl, so guard against a subclass of us overriding it
1645 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001646 return false;
1647 }
reed@google.com8926b162012-03-23 15:36:36 +00001648
1649 SkAutoLockPixels alp(src, !src.getTexture());
1650 if (!src.getTexture() && !src.readyToDraw()) {
1651 return false;
1652 }
1653
1654 GrPaint paint;
1655 paint.reset();
1656
1657 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1658
1659 GrTexture* texture;
1660 SkAutoCachedTexture act(this, src, sampler, &texture);
1661
1662 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001663 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1664 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001665 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1666 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001667 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1668 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001669 resultTexture->unref();
1670 }
reed@google.com76dd2772012-01-05 21:15:07 +00001671 return true;
1672}
1673
reed@google.comac10a2d2010-12-22 21:39:39 +00001674///////////////////////////////////////////////////////////////////////////////
1675
1676// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001677static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001678 kTriangles_GrPrimitiveType,
1679 kTriangleStrip_GrPrimitiveType,
1680 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001681};
1682
1683void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1684 int vertexCount, const SkPoint vertices[],
1685 const SkPoint texs[], const SkColor colors[],
1686 SkXfermode* xmode,
1687 const uint16_t indices[], int indexCount,
1688 const SkPaint& paint) {
1689 CHECK_SHOULD_DRAW(draw);
1690
bsalomon@google.com5782d712011-01-21 21:03:59 +00001691 GrPaint grPaint;
1692 SkAutoCachedTexture act;
1693 // we ignore the shader if texs is null.
1694 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001695 if (!skPaint2GrPaintNoShader(paint,
1696 false,
1697 NULL == colors,
1698 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001699 return;
1700 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001701 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001702 if (!skPaint2GrPaintShader(this,
1703 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001704 NULL == colors,
1705 &act,
1706 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001707 return;
1708 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001709 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001710
1711 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001712 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001713 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1714#if 0
1715 return
1716#endif
1717 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001718 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001719
bsalomon@google.com498776a2011-08-16 19:20:44 +00001720 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1721 if (NULL != colors) {
1722 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001723 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001724 for (int i = 0; i < vertexCount; ++i) {
1725 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1726 }
1727 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001728 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001729 fContext->drawVertices(grPaint,
1730 gVertexMode2PrimitiveType[vmode],
1731 vertexCount,
1732 (GrPoint*) vertices,
1733 (GrPoint*) texs,
1734 colors,
1735 indices,
1736 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001737}
1738
1739///////////////////////////////////////////////////////////////////////////////
1740
1741static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001742 GrFontScaler* scaler = (GrFontScaler*)data;
1743 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001744}
1745
1746static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1747 void* auxData;
1748 GrFontScaler* scaler = NULL;
1749 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1750 scaler = (GrFontScaler*)auxData;
1751 }
1752 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001753 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001754 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1755 }
1756 return scaler;
1757}
1758
1759static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1760 SkFixed fx, SkFixed fy,
1761 const SkGlyph& glyph) {
1762 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1763
bungeman@google.com15865a72012-01-11 16:28:04 +00001764 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001765
1766 if (NULL == procs->fFontScaler) {
1767 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1768 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001769
bungeman@google.com15865a72012-01-11 16:28:04 +00001770 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1771 glyph.getSubXFixed(),
1772 glyph.getSubYFixed()),
1773 SkFixedFloorToFixed(fx),
1774 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001775 procs->fFontScaler);
1776}
1777
bsalomon@google.com5782d712011-01-21 21:03:59 +00001778SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001779
1780 // deferred allocation
1781 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001782 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001783 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1784 fDrawProcs->fContext = fContext;
1785 }
1786
1787 // init our (and GL's) state
1788 fDrawProcs->fTextContext = context;
1789 fDrawProcs->fFontScaler = NULL;
1790 return fDrawProcs;
1791}
1792
1793void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1794 size_t byteLength, SkScalar x, SkScalar y,
1795 const SkPaint& paint) {
1796 CHECK_SHOULD_DRAW(draw);
1797
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001798 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001799 // this guy will just call our drawPath()
1800 draw.drawText((const char*)text, byteLength, x, y, paint);
1801 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001802 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001803
1804 GrPaint grPaint;
1805 SkAutoCachedTexture act;
1806
bsalomon@google.com84405e02012-03-05 19:57:21 +00001807 if (!skPaint2GrPaintShader(this,
1808 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001809 true,
1810 &act,
1811 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001812 return;
1813 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001814 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1815 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001816 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1817 }
1818}
1819
1820void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1821 size_t byteLength, const SkScalar pos[],
1822 SkScalar constY, int scalarsPerPos,
1823 const SkPaint& paint) {
1824 CHECK_SHOULD_DRAW(draw);
1825
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001826 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001827 // this guy will just call our drawPath()
1828 draw.drawPosText((const char*)text, byteLength, pos, constY,
1829 scalarsPerPos, paint);
1830 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001831 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001832
1833 GrPaint grPaint;
1834 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001835 if (!skPaint2GrPaintShader(this,
1836 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001837 true,
1838 &act,
1839 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001840 return;
1841 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001842 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1843 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001844 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1845 scalarsPerPos, paint);
1846 }
1847}
1848
1849void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1850 size_t len, const SkPath& path,
1851 const SkMatrix* m, const SkPaint& paint) {
1852 CHECK_SHOULD_DRAW(draw);
1853
1854 SkASSERT(draw.fDevice == this);
1855 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1856}
1857
1858///////////////////////////////////////////////////////////////////////////////
1859
reed@google.comf67e4cf2011-03-15 20:56:58 +00001860bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1861 if (!paint.isLCDRenderText()) {
1862 // we're cool with the paint as is
1863 return false;
1864 }
1865
1866 if (paint.getShader() ||
1867 paint.getXfermode() || // unless its srcover
1868 paint.getMaskFilter() ||
1869 paint.getRasterizer() ||
1870 paint.getColorFilter() ||
1871 paint.getPathEffect() ||
1872 paint.isFakeBoldText() ||
1873 paint.getStyle() != SkPaint::kFill_Style) {
1874 // turn off lcd
1875 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1876 flags->fHinting = paint.getHinting();
1877 return true;
1878 }
1879 // we're cool with the paint as is
1880 return false;
1881}
1882
reed@google.com75d939b2011-12-07 15:07:23 +00001883void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001884 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001885 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001886}
1887
reed@google.comf67e4cf2011-03-15 20:56:58 +00001888///////////////////////////////////////////////////////////////////////////////
1889
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001890SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1891 const SkBitmap& bitmap,
1892 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001893 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001894 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001895
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001896 if (!bitmap.isVolatile()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001897 uint64_t key = bitmap.getGenerationID();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001898 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001899
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001900 GrTextureDesc desc;
1901 desc.fWidth = bitmap.width();
1902 desc.fHeight = bitmap.height();
1903 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
robertphillips@google.com56f22442012-06-08 14:21:26 +00001904 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001905
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001906 entry = ctx->findAndLockTexture(desc, sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001907 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001908 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1909 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001910 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001911 } else {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001912 entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001913 sampler, bitmap);
1914 }
1915 if (NULL == entry.texture()) {
1916 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1917 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001918 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001919 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001920}
1921
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001922void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1923 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001924}
1925
bsalomon@google.comfb309512011-11-30 14:13:48 +00001926bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1927 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001928 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001929 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001930
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001931 GrTextureDesc desc;
1932 desc.fWidth = bitmap.width();
1933 desc.fHeight = bitmap.height();
1934 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
1935 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001936
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001937 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001938}
1939
1940
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001941SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1942 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001943 bool isOpaque,
1944 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001945 GrTextureDesc desc;
1946 desc.fConfig = fRenderTarget->config();
1947 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1948 desc.fWidth = width;
1949 desc.fHeight = height;
1950 desc.fSampleCnt = fRenderTarget->numSamples();
1951
1952 GrContext::TextureCacheEntry cacheEntry;
1953 GrTexture* texture;
1954 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001955 // Skia's convention is to only clear a device if it is non-opaque.
1956 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001957
1958#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1959 // layers are never draw in repeat modes, so we can request an approx
1960 // match and ignore any padding.
1961 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1962 GrContext::kApprox_ScratchTexMatch :
1963 GrContext::kExact_ScratchTexMatch;
1964 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1965 texture = cacheEntry.texture();
1966#else
1967 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1968 texture = tunref.get();
1969#endif
1970 if (texture) {
1971 return SkNEW_ARGS(SkGpuDevice,(fContext,
1972 texture,
1973 cacheEntry,
1974 needClear));
1975 } else {
1976 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1977 width, height);
1978 return NULL;
1979 }
1980}
1981
1982SkGpuDevice::SkGpuDevice(GrContext* context,
1983 GrTexture* texture,
1984 TexCache cacheEntry,
1985 bool needClear)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001986 : SkDevice(make_bitmap(context, texture->asRenderTarget()))
1987 , fClipStack(NULL) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001988 GrAssert(texture && texture->asRenderTarget());
1989 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1990 this->initFromRenderTarget(context, texture->asRenderTarget());
1991 fCache = cacheEntry;
1992 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001993}
1994