blob: c6f377d230bf00a02fba838adfed8ad0997c6ba6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
tomhudson@google.com898e7b52012-06-01 20:42:15 +00009#include "SkGpuDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000010
tomhudson@google.com898e7b52012-06-01 20:42:15 +000011#include "effects/GrGradientEffects.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000012
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrContext.h"
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000014#include "GrDefaultTextContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrTextContext.h"
16
reed@google.comac10a2d2010-12-22 21:39:39 +000017#include "SkGrTexturePixelRef.h"
18
Scroggo97c88c22011-05-11 14:05:25 +000019#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020#include "SkDrawProcs.h"
21#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000022#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000023#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000024#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
reed@google.comfa35e3d2012-06-26 20:16:17 +000026SK_DEFINE_INST_COUNT(GrTextContext)
27
bsalomon@google.com06cd7322012-03-30 18:45:35 +000028#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000029
30#if 0
31 extern bool (*gShouldDrawProc)();
32 #define CHECK_SHOULD_DRAW(draw) \
33 do { \
34 if (gShouldDrawProc && !gShouldDrawProc()) return; \
35 this->prepareRenderTarget(draw); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000036 GrAssert(!fNeedClear) \
reed@google.comac10a2d2010-12-22 21:39:39 +000037 } while (0)
38#else
bsalomon@google.com06cd7322012-03-30 18:45:35 +000039 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \
40 GrAssert(!fNeedClear)
reed@google.comac10a2d2010-12-22 21:39:39 +000041#endif
42
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000043// we use the same texture slot on GrPaint for bitmaps and shaders
44// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
45enum {
46 kBitmapTextureIdx = 0,
47 kShaderTextureIdx = 0
48};
49
reed@google.comcde92112011-07-06 20:00:52 +000050
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000051#define MAX_BLUR_SIGMA 4.0f
52// FIXME: This value comes from from SkBlurMaskFilter.cpp.
53// Should probably be put in a common header someplace.
54#define MAX_BLUR_RADIUS SkIntToScalar(128)
55// This constant approximates the scaling done in the software path's
56// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
57// IMHO, it actually should be 1: we blur "less" than we should do
58// according to the CSS and canvas specs, simply because Safari does the same.
59// Firefox used to do the same too, until 4.0 where they fixed it. So at some
60// point we should probably get rid of these scaling constants and rebaseline
61// all the blur tests.
62#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000063// This constant represents the screen alignment criterion in texels for
64// requiring texture domain clamping to prevent color bleeding when drawing
65// a sub region of a larger source image.
66#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000067
68#define DO_DEFERRED_CLEAR \
69 do { \
70 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000071 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000072 fNeedClear = false; \
73 } \
74 } while (false) \
75
reed@google.comac10a2d2010-12-22 21:39:39 +000076///////////////////////////////////////////////////////////////////////////////
77
bsalomon@google.com84405e02012-03-05 19:57:21 +000078class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
79public:
80 SkAutoCachedTexture() { }
81 SkAutoCachedTexture(SkGpuDevice* device,
82 const SkBitmap& bitmap,
83 const GrSamplerState* sampler,
84 GrTexture** texture) {
85 GrAssert(texture);
86 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000087 }
reed@google.comac10a2d2010-12-22 21:39:39 +000088
bsalomon@google.com84405e02012-03-05 19:57:21 +000089 ~SkAutoCachedTexture() {
90 if (fTex.texture()) {
91 fDevice->unlockCachedTexture(fTex);
92 }
reed@google.comac10a2d2010-12-22 21:39:39 +000093 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000094
95 GrTexture* set(SkGpuDevice* device,
96 const SkBitmap& bitmap,
97 const GrSamplerState* sampler) {
98 if (fTex.texture()) {
99 fDevice->unlockCachedTexture(fTex);
100 }
101 fDevice = device;
102 GrTexture* texture = (GrTexture*)bitmap.getTexture();
103 if (texture) {
104 // return the native texture
105 fTex.reset();
106 } else {
107 // look it up in our cache
108 fTex = device->lockCachedTexture(bitmap, sampler);
109 texture = fTex.texture();
110 }
111 return texture;
112 }
113
114private:
115 SkGpuDevice* fDevice;
116 GrContext::TextureCacheEntry fTex;
117};
reed@google.comac10a2d2010-12-22 21:39:39 +0000118
119///////////////////////////////////////////////////////////////////////////////
120
121bool gDoTraceDraw;
122
123struct GrSkDrawProcs : public SkDrawProcs {
124public:
125 GrContext* fContext;
126 GrTextContext* fTextContext;
127 GrFontScaler* fFontScaler; // cached in the skia glyphcache
128};
129
130///////////////////////////////////////////////////////////////////////////////
131
reed@google.comaf951c92011-06-16 19:10:39 +0000132static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
133 switch (config) {
134 case kAlpha_8_GrPixelConfig:
135 *isOpaque = false;
136 return SkBitmap::kA8_Config;
137 case kRGB_565_GrPixelConfig:
138 *isOpaque = true;
139 return SkBitmap::kRGB_565_Config;
140 case kRGBA_4444_GrPixelConfig:
141 *isOpaque = false;
142 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000143 case kSkia8888_PM_GrPixelConfig:
144 // we don't currently have a way of knowing whether
145 // a 8888 is opaque based on the config.
146 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000147 return SkBitmap::kARGB_8888_Config;
148 default:
149 *isOpaque = false;
150 return SkBitmap::kNo_Config;
151 }
152}
reed@google.comac10a2d2010-12-22 21:39:39 +0000153
reed@google.comaf951c92011-06-16 19:10:39 +0000154static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000155 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000156
157 bool isOpaque;
158 SkBitmap bitmap;
159 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
160 renderTarget->width(), renderTarget->height());
161 bitmap.setIsOpaque(isOpaque);
162 return bitmap;
163}
164
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000165SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
166: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
167 this->initFromRenderTarget(context, texture->asRenderTarget());
168}
169
reed@google.comaf951c92011-06-16 19:10:39 +0000170SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
171: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000172 this->initFromRenderTarget(context, renderTarget);
173}
174
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000175void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000176 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000177 fNeedPrepareRenderTarget = false;
178 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000179
reed@google.comaf951c92011-06-16 19:10:39 +0000180 fContext = context;
181 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000182
reed@google.comaf951c92011-06-16 19:10:39 +0000183 fTexture = NULL;
184 fRenderTarget = NULL;
185 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000186
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000187 GrAssert(NULL != renderTarget);
188 fRenderTarget = renderTarget;
189 fRenderTarget->ref();
190 // if this RT is also a texture, hold a ref on it
191 fTexture = fRenderTarget->asTexture();
192 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000193
194 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
195 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
196 // the device via accessBitmap. This external ref may outlive the device.
197 // Since textures own their render targets (but not vice-versa) we
198 // are ensuring that both objects will live as long as the pixel ref.
199 SkPixelRef* pr;
200 if (fTexture) {
201 pr = new SkGrTexturePixelRef(fTexture);
202 } else {
203 pr = new SkGrRenderTargetPixelRef(fRenderTarget);
204 }
reed@google.comaf951c92011-06-16 19:10:39 +0000205 this->setPixelRef(pr, 0)->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000206
207 fTextContext = NULL;
reed@google.comaf951c92011-06-16 19:10:39 +0000208}
209
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000210SkGpuDevice::SkGpuDevice(GrContext* context,
211 SkBitmap::Config config,
212 int width,
213 int height)
214 : SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 fNeedPrepareRenderTarget = false;
216 fDrawProcs = NULL;
217
reed@google.com7b201d22011-01-11 18:59:23 +0000218 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000219 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000220
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 fTexture = NULL;
222 fRenderTarget = NULL;
223 fNeedClear = false;
224
reed@google.comaf951c92011-06-16 19:10:39 +0000225 if (config != SkBitmap::kRGB_565_Config) {
226 config = SkBitmap::kARGB_8888_Config;
227 }
228 SkBitmap bm;
229 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000230
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000231 GrTextureDesc desc;
232 desc.fFlags = kRenderTarget_GrTextureFlagBit;
233 desc.fWidth = width;
234 desc.fHeight = height;
235 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
reed@google.comaf951c92011-06-16 19:10:39 +0000237 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000238
reed@google.comaf951c92011-06-16 19:10:39 +0000239 if (NULL != fTexture) {
240 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000241 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
reed@google.comaf951c92011-06-16 19:10:39 +0000243 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000244
reed@google.comaf951c92011-06-16 19:10:39 +0000245 // wrap the bitmap with a pixelref to expose our texture
246 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000247 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000248 } else {
249 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
250 width, height);
251 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000252 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000253
254 fTextContext = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000255}
256
257SkGpuDevice::~SkGpuDevice() {
258 if (fDrawProcs) {
259 delete fDrawProcs;
260 }
261
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000262 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
263 // This call gives the context a chance to relinquish it
264 fContext->setRenderTarget(NULL);
265
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000266 SkSafeUnref(fTexture);
267 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000268 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000269 GrAssert(NULL != fTexture);
270 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000271 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000272 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000273 fContext->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000274
275 if (NULL != fTextContext) {
276 fTextContext->unref();
277 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000278}
279
reed@google.comac10a2d2010-12-22 21:39:39 +0000280///////////////////////////////////////////////////////////////////////////////
281
282void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000283 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000284 fContext->setRenderTarget(fRenderTarget);
285 fContext->flush(true);
286 fNeedPrepareRenderTarget = true;
287}
288
289///////////////////////////////////////////////////////////////////////////////
290
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291namespace {
292GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
293 switch (config8888) {
294 case SkCanvas::kNative_Premul_Config8888:
295 return kSkia8888_PM_GrPixelConfig;
296 case SkCanvas::kNative_Unpremul_Config8888:
297 return kSkia8888_UPM_GrPixelConfig;
298 case SkCanvas::kBGRA_Premul_Config8888:
299 return kBGRA_8888_PM_GrPixelConfig;
300 case SkCanvas::kBGRA_Unpremul_Config8888:
301 return kBGRA_8888_UPM_GrPixelConfig;
302 case SkCanvas::kRGBA_Premul_Config8888:
303 return kRGBA_8888_PM_GrPixelConfig;
304 case SkCanvas::kRGBA_Unpremul_Config8888:
305 return kRGBA_8888_UPM_GrPixelConfig;
306 default:
307 GrCrash("Unexpected Config8888.");
308 return kSkia8888_PM_GrPixelConfig;
309 }
310}
311}
312
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000313bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
314 int x, int y,
315 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000316 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000317 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
318 SkASSERT(!bitmap.isNull());
319 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000320
bsalomon@google.com910267d2011-11-02 20:06:25 +0000321 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000322 GrPixelConfig config;
323 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000324 return fContext->readRenderTargetPixels(fRenderTarget,
325 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000326 bitmap.width(),
327 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000328 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000329 bitmap.getPixels(),
330 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000331}
332
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000333void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
334 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000335 SkAutoLockPixels alp(bitmap);
336 if (!bitmap.readyToDraw()) {
337 return;
338 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000339
340 GrPixelConfig config;
341 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
342 config = config8888_to_gr_config(config8888);
343 } else {
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000344 config= SkGr::BitmapConfig2PixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000345 }
346
bsalomon@google.com6f379512011-11-16 20:36:03 +0000347 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
348 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000349}
350
351///////////////////////////////////////////////////////////////////////////////
352
353static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000354 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000355 const SkRegion& clipRegion,
356 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000357 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000358
359 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000360 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000361 const SkIRect& skBounds = clipRegion.getBounds();
362 GrRect bounds;
363 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
364 GrIntToScalar(skBounds.fTop),
365 GrIntToScalar(skBounds.fRight),
366 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000367 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
368 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000369 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000370}
371
372// call this ever each draw call, to ensure that the context reflects our state,
373// and not the state from some other canvas/device
374void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
375 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000376 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000377
378 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000379 SkASSERT(draw.fClipStack);
380 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000381 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000382 fNeedPrepareRenderTarget = false;
383 }
384}
385
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000386void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
387 const SkClipStack& clipStack) {
388 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
389 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000390 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000391}
392
393void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000394 const SkRegion& clip, const SkClipStack& clipStack) {
395
reed@google.comac10a2d2010-12-22 21:39:39 +0000396 fContext->setRenderTarget(fRenderTarget);
397
bsalomon@google.comd302f142011-03-03 13:54:13 +0000398 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000399
reed@google.com6f8f2922011-03-04 22:27:10 +0000400 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000401
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000402 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000403}
404
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000405SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000406 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000407 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000408}
409
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000410bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000411 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000412 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000413 return true;
414 }
415 return false;
416}
417
418///////////////////////////////////////////////////////////////////////////////
419
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000420SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
421SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
422SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
423SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
424SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
425 shader_type_mismatch);
426SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000427
bsalomon@google.com84405e02012-03-05 19:57:21 +0000428namespace {
429
430// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
431// justAlpha indicates that skPaint's alpha should be used rather than the color
432// Callers may subsequently modify the GrPaint. Setting constantColor indicates
433// that the final paint will draw the same color at every pixel. This allows
434// an optimization where the the color filter can be applied to the skPaint's
435// color once while converting to GrPain and then ignored.
436inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
437 bool justAlpha,
438 bool constantColor,
439 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000440
441 grPaint->fDither = skPaint.isDither();
442 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000443 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000444
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000445 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
446 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000447
448 SkXfermode* mode = skPaint.getXfermode();
449 if (mode) {
450 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000451 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000452#if 0
453 return false;
454#endif
455 }
456 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000457 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
458 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
459
bsalomon@google.com5782d712011-01-21 21:03:59 +0000460 if (justAlpha) {
461 uint8_t alpha = skPaint.getAlpha();
462 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000463 // justAlpha is currently set to true only if there is a texture,
464 // so constantColor should not also be true.
465 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000466 } else {
467 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000468 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000469 }
Scroggo97c88c22011-05-11 14:05:25 +0000470 SkColorFilter* colorFilter = skPaint.getColorFilter();
471 SkColor color;
472 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000473 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000474 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000475 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000476 if (!constantColor) {
477 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
478 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000479 } else {
480 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
481 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000482 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000483 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000484 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
485 grPaint->fColorMatrixEnabled = true;
486 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
487 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
488 } else {
489 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000490 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000491 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000492}
493
bsalomon@google.com84405e02012-03-05 19:57:21 +0000494// This function is similar to skPaint2GrPaintNoShader but also converts
495// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
496// be used is set on grPaint and returned in param act. constantColor has the
497// same meaning as in skPaint2GrPaintNoShader.
498inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
499 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000500 bool constantColor,
501 SkGpuDevice::SkAutoCachedTexture* act,
502 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000503
bsalomon@google.com5782d712011-01-21 21:03:59 +0000504 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000505
bsalomon@google.com5782d712011-01-21 21:03:59 +0000506 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000507 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000508 return skPaint2GrPaintNoShader(skPaint,
509 false,
510 constantColor,
511 grPaint);
512 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000513 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000514 }
515
reed@google.comac10a2d2010-12-22 21:39:39 +0000516 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000517 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000518 SkShader::TileMode tileModes[2];
519 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000520 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000521 tileModes, twoPointParams);
522
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000523 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000524 SkShader::GradientInfo info;
525 SkColor color;
526
527 info.fColors = &color;
528 info.fColorOffsets = NULL;
529 info.fColorCount = 1;
530 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
531 SkPaint copy(skPaint);
532 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000533 // modulate the paint alpha by the shader's solid color alpha
534 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
535 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000536 return skPaint2GrPaintNoShader(copy,
537 false,
538 constantColor,
539 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000540 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000541 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000542 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000543 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000544 switch (bmptype) {
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000545 case SkShader::kRadial_BitmapType:
546 sampler->setCustomStage(new GrRadialGradient())->unref();
547 sampler->setFilter(GrSamplerState::kBilinear_Filter);
548 break;
549 case SkShader::kSweep_BitmapType:
550 sampler->setCustomStage(new GrSweepGradient())->unref();
551 sampler->setFilter(GrSamplerState::kBilinear_Filter);
552 break;
553 case SkShader::kTwoPointRadial_BitmapType:
554 sampler->setCustomStage(new
555 GrRadial2Gradient(twoPointParams[0],
556 twoPointParams[1],
557 twoPointParams[2] < 0))->unref();
558 sampler->setFilter(GrSamplerState::kBilinear_Filter);
559 break;
560 default:
561 if (skPaint.isFilterBitmap()) {
562 sampler->setFilter(GrSamplerState::kBilinear_Filter);
563 } else {
564 sampler->setFilter(GrSamplerState::kNearest_Filter);
565 }
566 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000567 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000568 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
569 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000570
bsalomon@google.com84405e02012-03-05 19:57:21 +0000571 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000572 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000573 SkDebugf("Couldn't convert bitmap to texture.\n");
574 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000575 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000576 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000577
578 // since our texture coords will be in local space, we wack the texture
579 // matrix to map them back into 0...1 before we load it
580 SkMatrix localM;
581 if (shader->getLocalMatrix(&localM)) {
582 SkMatrix inverse;
583 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000584 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000585 }
586 }
587 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000588 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
589 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000590 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000591 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000592 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000593 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000594 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000595
596 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000597}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000598}
reed@google.comac10a2d2010-12-22 21:39:39 +0000599
600///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000601void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000602 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000603}
604
reed@google.comac10a2d2010-12-22 21:39:39 +0000605void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
606 CHECK_SHOULD_DRAW(draw);
607
bsalomon@google.com5782d712011-01-21 21:03:59 +0000608 GrPaint grPaint;
609 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000610 if (!skPaint2GrPaintShader(this,
611 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000612 true,
613 &act,
614 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000615 return;
616 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000617
618 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000619}
620
621// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000622static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000623 kPoints_GrPrimitiveType,
624 kLines_GrPrimitiveType,
625 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000626};
627
628void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000629 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000630 CHECK_SHOULD_DRAW(draw);
631
632 SkScalar width = paint.getStrokeWidth();
633 if (width < 0) {
634 return;
635 }
636
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000637 // we only handle hairlines and paints without path effects or mask filters,
638 // else we let the SkDraw call our drawPath()
639 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000640 draw.drawPoints(mode, count, pts, paint, true);
641 return;
642 }
643
bsalomon@google.com5782d712011-01-21 21:03:59 +0000644 GrPaint grPaint;
645 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000646 if (!skPaint2GrPaintShader(this,
647 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000648 true,
649 &act,
650 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000651 return;
652 }
653
bsalomon@google.com5782d712011-01-21 21:03:59 +0000654 fContext->drawVertices(grPaint,
655 gPointMode2PrimtiveType[mode],
656 count,
657 (GrPoint*)pts,
658 NULL,
659 NULL,
660 NULL,
661 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000662}
663
reed@google.comc9aa5872011-04-05 21:05:37 +0000664///////////////////////////////////////////////////////////////////////////////
665
reed@google.comac10a2d2010-12-22 21:39:39 +0000666void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
667 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000668 CHECK_SHOULD_DRAW(draw);
669
bungeman@google.com79bd8772011-07-18 15:34:08 +0000670 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000671 SkScalar width = paint.getStrokeWidth();
672
673 /*
674 We have special code for hairline strokes, miter-strokes, and fills.
675 Anything else we just call our path code.
676 */
677 bool usePath = doStroke && width > 0 &&
678 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000679 // another two reasons we might need to call drawPath...
680 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000681 usePath = true;
682 }
reed@google.com67db6642011-05-26 11:46:35 +0000683 // until we aa rotated rects...
684 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
685 usePath = true;
686 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000687 // small miter limit means right angles show bevel...
688 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
689 paint.getStrokeMiter() < SK_ScalarSqrt2)
690 {
691 usePath = true;
692 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000693 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000694 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
695 usePath = true;
696 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000697
698 if (usePath) {
699 SkPath path;
700 path.addRect(rect);
701 this->drawPath(draw, path, paint, NULL, true);
702 return;
703 }
704
705 GrPaint grPaint;
706 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000707 if (!skPaint2GrPaintShader(this,
708 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000709 true,
710 &act,
711 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000712 return;
713 }
reed@google.com20efde72011-05-09 17:00:02 +0000714 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000715}
716
reed@google.com69302852011-02-16 18:08:07 +0000717#include "SkMaskFilter.h"
718#include "SkBounder.h"
719
bsalomon@google.com85003222012-03-28 14:44:37 +0000720///////////////////////////////////////////////////////////////////////////////
721
722// helpers for applying mask filters
723namespace {
724
725GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000726 switch (fillType) {
727 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000728 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000729 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000730 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000731 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000732 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000733 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000734 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000735 default:
736 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000737 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000738 }
739}
740
bsalomon@google.com85003222012-03-28 14:44:37 +0000741// We prefer to blur small rect with small radius via CPU.
742#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
743#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
744inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
745 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
746 rect.height() <= MIN_GPU_BLUR_SIZE &&
747 radius <= MIN_GPU_BLUR_RADIUS) {
748 return true;
749 }
750 return false;
751}
752
753bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
754 SkMaskFilter* filter, const SkMatrix& matrix,
755 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000756 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000757#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000758 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000759#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000760 SkMaskFilter::BlurInfo info;
761 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000762 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000763 return false;
764 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000765 SkScalar radius = info.fIgnoreTransform ? info.fRadius
766 : matrix.mapRadius(info.fRadius);
767 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000768 if (radius <= 0) {
769 return false;
770 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000771
772 SkRect srcRect = path.getBounds();
773 if (shouldDrawBlurWithCPU(srcRect, radius)) {
774 return false;
775 }
776
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000777 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000778 float sigma3 = sigma * 3.0f;
779
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000780 SkRect clipRect;
781 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000782
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000783 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000784 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
785 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000786 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000787 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000788 SkIRect finalIRect;
789 finalRect.roundOut(&finalIRect);
790 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000791 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000792 }
793 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000794 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000795 }
796 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000797 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000798 GrTextureDesc desc;
799 desc.fFlags = kRenderTarget_GrTextureFlagBit;
800 desc.fWidth = SkScalarCeilToInt(srcRect.width());
801 desc.fHeight = SkScalarCeilToInt(srcRect.height());
802 // We actually only need A8, but it often isn't supported as a
803 // render target so default to RGBA_8888
804 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000805
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000806 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
807 desc.fConfig = kAlpha_8_GrPixelConfig;
808 }
809
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000810 GrAutoScratchTexture pathEntry(context, desc);
811 GrTexture* pathTexture = pathEntry.texture();
812 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000813 return false;
814 }
815 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000816 // Once this code moves into GrContext, this should be changed to use
817 // an AutoClipRestore.
818 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000819 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000820 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000821 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000822 GrPaint tempPaint;
823 tempPaint.reset();
824
825 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000826 tempPaint.fAntiAlias = grp->fAntiAlias;
827 if (tempPaint.fAntiAlias) {
828 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
829 // blend coeff of zero requires dual source blending support in order
830 // to properly blend partially covered pixels. This means the AA
831 // code path may not be taken. So we use a dst blend coeff of ISA. We
832 // could special case AA draws to a dst surface with known alpha=0 to
833 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000834 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
835 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000836 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000837 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000838 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000839
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000840 GrAutoScratchTexture temp1, temp2;
841 // If we're doing a normal blur, we can clobber the pathTexture in the
842 // gaussianBlur. Otherwise, we need to save it for later compositing.
843 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000844 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
845 &temp1,
846 isNormalBlur ? NULL : &temp2,
847 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000848
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000849 if (!isNormalBlur) {
850 GrPaint paint;
851 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000852 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000853 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
854 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000855 // Blend pathTexture over blurTexture.
856 context->setRenderTarget(blurTexture->asRenderTarget());
857 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000858 if (SkMaskFilter::kInner_BlurType == blurType) {
859 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000860 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
861 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000862 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
863 // solid: dst = src + dst - src * dst
864 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000865 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
866 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000867 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
868 // outer: dst = dst * (1 - src)
869 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000870 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
871 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000872 }
873 context->drawRect(paint, srcRect);
874 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000875 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000876 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000877
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000878 if (grp->hasTextureOrMask()) {
879 GrMatrix inverse;
880 if (!matrix.invert(&inverse)) {
881 return false;
882 }
883 grp->preConcatActiveSamplerMatrices(inverse);
884 }
885
886 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
887 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000888 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000889 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000890 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000891
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000892 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
893 -finalRect.fTop);
894 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
895 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000896 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000897 return true;
898}
899
bsalomon@google.com85003222012-03-28 14:44:37 +0000900bool drawWithMaskFilter(GrContext* context, const SkPath& path,
901 SkMaskFilter* filter, const SkMatrix& matrix,
902 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000903 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000904 SkMask srcM, dstM;
905
906 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000907 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000908 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000909 return false;
910 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000911 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000912
913 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
914 return false;
915 }
916 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000917 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000918
919 if (clip.quickReject(dstM.fBounds)) {
920 return false;
921 }
922 if (bounder && !bounder->doIRect(dstM.fBounds)) {
923 return false;
924 }
925
926 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
927 // the current clip (and identity matrix) and grpaint settings
928
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000929 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000930 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000931
reed@google.com0c219b62011-02-16 21:31:18 +0000932 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000933
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000934 GrTextureDesc desc;
935 desc.fWidth = dstM.fBounds.width();
936 desc.fHeight = dstM.fBounds.height();
937 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000938
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000939 GrAutoScratchTexture ast(context, desc);
940 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000941
reed@google.com69302852011-02-16 18:08:07 +0000942 if (NULL == texture) {
943 return false;
944 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000945 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000946 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000947
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000948 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
949 grp->preConcatActiveSamplerMatrices(ivm);
950 }
951
952 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
953 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000954 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000955 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000956 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000957
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000958 GrRect d;
959 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000960 GrIntToScalar(dstM.fBounds.fTop),
961 GrIntToScalar(dstM.fBounds.fRight),
962 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000963
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000964 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
965 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
966 -dstM.fBounds.fTop*SK_Scalar1);
967 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000968 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000969 return true;
970}
reed@google.com69302852011-02-16 18:08:07 +0000971
bsalomon@google.com85003222012-03-28 14:44:37 +0000972}
973
974///////////////////////////////////////////////////////////////////////////////
975
reed@google.com0c219b62011-02-16 21:31:18 +0000976void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000977 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000978 bool pathIsMutable) {
979 CHECK_SHOULD_DRAW(draw);
980
reed@google.comfe626382011-09-21 13:50:35 +0000981 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000982
bsalomon@google.com5782d712011-01-21 21:03:59 +0000983 GrPaint grPaint;
984 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000985 if (!skPaint2GrPaintShader(this,
986 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000987 true,
988 &act,
989 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000990 return;
991 }
992
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000993 // can we cheat, and threat a thin stroke as a hairline w/ coverage
994 // if we can, we draw lots faster (raster device does this same test)
995 SkScalar hairlineCoverage;
996 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
997 doFill = false;
998 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
999 grPaint.fCoverage);
1000 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001001
reed@google.comfe626382011-09-21 13:50:35 +00001002 // If we have a prematrix, apply it to the path, optimizing for the case
1003 // where the original path can in fact be modified in place (even though
1004 // its parameter type is const).
1005 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1006 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001007
1008 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001009 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001010
reed@google.come3445642011-02-16 23:20:39 +00001011 if (!pathIsMutable) {
1012 result = &tmpPath;
1013 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001014 }
reed@google.come3445642011-02-16 23:20:39 +00001015 // should I push prePathMatrix on our MV stack temporarily, instead
1016 // of applying it here? See SkDraw.cpp
1017 pathPtr->transform(*prePathMatrix, result);
1018 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001019 }
reed@google.com0c219b62011-02-16 21:31:18 +00001020 // at this point we're done with prePathMatrix
1021 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001022
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001023 if (paint.getPathEffect() ||
1024 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001025 // it is safe to use tmpPath here, even if we already used it for the
1026 // prepathmatrix, since getFillPath can take the same object for its
1027 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001028 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001029 pathPtr = &tmpPath;
1030 }
1031
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001032 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001033 // avoid possibly allocating a new path in transform if we can
1034 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1035
1036 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001037 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001038 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001039 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001040 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001041 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001042 &grPaint, pathFillType)) {
1043 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1044 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001045 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001046 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001047 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001048 }
reed@google.com69302852011-02-16 18:08:07 +00001049 return;
1050 }
reed@google.com69302852011-02-16 18:08:07 +00001051
bsalomon@google.com47059542012-06-06 20:51:20 +00001052 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001053
reed@google.com0c219b62011-02-16 21:31:18 +00001054 if (doFill) {
1055 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001056 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001057 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 break;
1059 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001060 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001061 break;
1062 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001063 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 break;
1065 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001066 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001067 break;
1068 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001069 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001070 return;
1071 }
1072 }
1073
reed@google.com07f3ee12011-05-16 17:21:57 +00001074 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001075}
1076
bsalomon@google.comfb309512011-11-30 14:13:48 +00001077namespace {
1078
1079inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1080 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1081 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1082 return tilesX * tilesY;
1083}
1084
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001085inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001086 const SkIRect* srcRectPtr,
1087 int maxTextureSize) {
1088 static const int kSmallTileSize = 1 << 10;
1089 if (maxTextureSize <= kSmallTileSize) {
1090 return maxTextureSize;
1091 }
1092
1093 size_t maxTexTotalTileSize;
1094 size_t smallTotalTileSize;
1095
1096 if (NULL == srcRectPtr) {
1097 int w = bitmap.width();
1098 int h = bitmap.height();
1099 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1100 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1101 } else {
1102 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1103 srcRectPtr->fTop,
1104 srcRectPtr->fRight,
1105 srcRectPtr->fBottom,
1106 maxTextureSize);
1107 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1108 srcRectPtr->fTop,
1109 srcRectPtr->fRight,
1110 srcRectPtr->fBottom,
1111 kSmallTileSize);
1112 }
1113 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1114 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1115
1116 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1117 return kSmallTileSize;
1118 } else {
1119 return maxTextureSize;
1120 }
1121}
1122}
1123
1124bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1125 const GrSamplerState& sampler,
1126 const SkIRect* srcRectPtr,
1127 int* tileSize) const {
1128 SkASSERT(NULL != tileSize);
1129
1130 // if bitmap is explictly texture backed then just use the texture
1131 if (NULL != bitmap.getTexture()) {
1132 return false;
1133 }
1134 // if it's larger than the max texture size, then we have no choice but
1135 // tiling
1136 const int maxTextureSize = fContext->getMaxTextureSize();
1137 if (bitmap.width() > maxTextureSize ||
1138 bitmap.height() > maxTextureSize) {
1139 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1140 return true;
1141 }
1142 // if we are going to have to draw the whole thing, then don't tile
1143 if (NULL == srcRectPtr) {
1144 return false;
1145 }
1146 // if the entire texture is already in our cache then no reason to tile it
1147 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1148 return false;
1149 }
1150
1151 // At this point we know we could do the draw by uploading the entire bitmap
1152 // as a texture. However, if the texture would be large compared to the
1153 // cache size and we don't require most of it for this draw then tile to
1154 // reduce the amount of upload and cache spill.
1155
1156 // assumption here is that sw bitmap size is a good proxy for its size as
1157 // a texture
1158 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001159 size_t cacheSize;
1160 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001161 if (bmpSize < cacheSize / 2) {
1162 return false;
1163 }
1164
1165 SkFixed fracUsed =
1166 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1167 (srcRectPtr->height() << 16) / bitmap.height());
1168 if (fracUsed <= SK_FixedHalf) {
1169 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1170 return true;
1171 } else {
1172 return false;
1173 }
1174}
1175
reed@google.comac10a2d2010-12-22 21:39:39 +00001176void SkGpuDevice::drawBitmap(const SkDraw& draw,
1177 const SkBitmap& bitmap,
1178 const SkIRect* srcRectPtr,
1179 const SkMatrix& m,
1180 const SkPaint& paint) {
1181 CHECK_SHOULD_DRAW(draw);
1182
1183 SkIRect srcRect;
1184 if (NULL == srcRectPtr) {
1185 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1186 } else {
1187 srcRect = *srcRectPtr;
1188 }
1189
junov@google.comd935cfb2011-06-27 20:48:23 +00001190 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001191 // Convert the bitmap to a shader so that the rect can be drawn
1192 // through drawRect, which supports mask filters.
1193 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001194 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001195 if (srcRectPtr) {
1196 if (!bitmap.extractSubset(&tmp, srcRect)) {
1197 return; // extraction failed
1198 }
1199 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001200 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001201 }
1202 SkPaint paintWithTexture(paint);
1203 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1204 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001205 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001206 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001207
junov@google.com1d329782011-07-28 20:10:09 +00001208 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001209 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001210 // also affect the behavior of the mask filter.
1211 SkMatrix drawMatrix;
1212 drawMatrix.setConcat(*draw.fMatrix, m);
1213 SkDraw transformedDraw(draw);
1214 transformedDraw.fMatrix = &drawMatrix;
1215
1216 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1217
junov@google.comd935cfb2011-06-27 20:48:23 +00001218 return;
1219 }
1220
bsalomon@google.com5782d712011-01-21 21:03:59 +00001221 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001222 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001223 return;
1224 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001225 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001226 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001227 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001228 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001229 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001230 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001231
bsalomon@google.comfb309512011-11-30 14:13:48 +00001232 int tileSize;
1233 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1234 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001235 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001236 return;
1237 }
1238
1239 // undo the translate done by SkCanvas
1240 int DX = SkMax32(0, srcRect.fLeft);
1241 int DY = SkMax32(0, srcRect.fTop);
1242 // compute clip bounds in local coordinates
1243 SkIRect clipRect;
1244 {
1245 SkRect r;
1246 r.set(draw.fClip->getBounds());
1247 SkMatrix matrix, inverse;
1248 matrix.setConcat(*draw.fMatrix, m);
1249 if (!matrix.invert(&inverse)) {
1250 return;
1251 }
1252 inverse.mapRect(&r);
1253 r.roundOut(&clipRect);
1254 // apply the canvas' translate to our local clip
1255 clipRect.offset(DX, DY);
1256 }
1257
bsalomon@google.comfb309512011-11-30 14:13:48 +00001258 int nx = bitmap.width() / tileSize;
1259 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001260 for (int x = 0; x <= nx; x++) {
1261 for (int y = 0; y <= ny; y++) {
1262 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001263 tileR.set(x * tileSize, y * tileSize,
1264 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 if (!SkIRect::Intersects(tileR, clipRect)) {
1266 continue;
1267 }
1268
1269 SkIRect srcR = tileR;
1270 if (!srcR.intersect(srcRect)) {
1271 continue;
1272 }
1273
1274 SkBitmap tmpB;
1275 if (bitmap.extractSubset(&tmpB, tileR)) {
1276 // now offset it to make it "local" to our tmp bitmap
1277 srcR.offset(-tileR.fLeft, -tileR.fTop);
1278
1279 SkMatrix tmpM(m);
1280 {
1281 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1282 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1283 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1284 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001285 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 }
1287 }
1288 }
1289}
1290
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001291namespace {
1292
1293bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1294 // detect pixel disalignment
1295 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1296 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1297 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1298 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1299 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1300 COLOR_BLEED_TOLERANCE &&
1301 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1302 COLOR_BLEED_TOLERANCE) {
1303 return true;
1304 }
1305 return false;
1306}
1307
1308bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1309 const SkMatrix& m) {
1310 // Only gets called if hasAlignedSamples returned false.
1311 // So we can assume that sampling is axis aligned but not texel aligned.
1312 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1313 SkRect innerSrcRect(srcRect), innerTransformedRect,
1314 outerTransformedRect(transformedRect);
1315 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1316 m.mapRect(&innerTransformedRect, innerSrcRect);
1317
1318 // The gap between outerTransformedRect and innerTransformedRect
1319 // represents the projection of the source border area, which is
1320 // problematic for color bleeding. We must check whether any
1321 // destination pixels sample the border area.
1322 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1323 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1324 SkIRect outer, inner;
1325 outerTransformedRect.round(&outer);
1326 innerTransformedRect.round(&inner);
1327 // If the inner and outer rects round to the same result, it means the
1328 // border does not overlap any pixel centers. Yay!
1329 return inner != outer;
1330}
1331
1332} // unnamed namespace
1333
reed@google.comac10a2d2010-12-22 21:39:39 +00001334/*
1335 * This is called by drawBitmap(), which has to handle images that may be too
1336 * large to be represented by a single texture.
1337 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001338 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1339 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001340 */
1341void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1342 const SkBitmap& bitmap,
1343 const SkIRect& srcRect,
1344 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001345 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001346 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1347 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001348
reed@google.com9c49bc32011-07-07 13:42:37 +00001349 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001350 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001351 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001352 return;
1353 }
1354
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001355 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001356
1357 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1358 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001359 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001360
1361 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001362 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001363 if (NULL == texture) {
1364 return;
1365 }
1366
bsalomon@google.com452943d2011-10-31 17:37:14 +00001367 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001368
reed@google.com20efde72011-05-09 17:00:02 +00001369 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1370 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001371 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001372 float wInv = 1.f / bitmap.width();
1373 float hInv = 1.f / bitmap.height();
1374 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1375 SkFloatToScalar(srcRect.fTop * hInv),
1376 SkFloatToScalar(srcRect.fRight * wInv),
1377 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001378
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001379 bool needsTextureDomain = false;
1380 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1381 {
1382 // Need texture domain if drawing a sub rect.
1383 needsTextureDomain = srcRect.width() < bitmap.width() ||
1384 srcRect.height() < bitmap.height();
1385 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1386 // sampling is axis-aligned
1387 GrRect floatSrcRect, transformedRect;
1388 floatSrcRect.set(srcRect);
1389 SkMatrix srcToDeviceMatrix(m);
1390 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1391 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1392
1393 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1394 // Samples are texel-aligned, so filtering is futile
1395 sampler->setFilter(GrSamplerState::kNearest_Filter);
1396 needsTextureDomain = false;
1397 } else {
1398 needsTextureDomain = needsTextureDomain &&
1399 mayColorBleed(floatSrcRect, transformedRect, m);
1400 }
1401 }
1402 }
1403
1404 GrRect textureDomain = GrRect::MakeEmpty();
1405
1406 if (needsTextureDomain) {
1407 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001408 GrScalar left, top, right, bottom;
1409 if (srcRect.width() > 1) {
1410 GrScalar border = GR_ScalarHalf / bitmap.width();
1411 left = paintRect.left() + border;
1412 right = paintRect.right() - border;
1413 } else {
1414 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1415 }
1416 if (srcRect.height() > 1) {
1417 GrScalar border = GR_ScalarHalf / bitmap.height();
1418 top = paintRect.top() + border;
1419 bottom = paintRect.bottom() - border;
1420 } else {
1421 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1422 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001423 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001424 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001425 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001426
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001427 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001428}
1429
reed@google.com8926b162012-03-23 15:36:36 +00001430static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1431 SkImageFilter* filter, const GrRect& rect) {
1432 GrAssert(filter);
1433
1434 SkSize blurSize;
1435 SkISize radius;
1436
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001437 GrTextureDesc desc;
1438 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1439 desc.fWidth = SkScalarCeilToInt(rect.width());
1440 desc.fHeight = SkScalarCeilToInt(rect.height());
1441 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
reed@google.com8926b162012-03-23 15:36:36 +00001442
1443 if (filter->asABlur(&blurSize)) {
1444 GrAutoScratchTexture temp1, temp2;
1445 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1446 blurSize.width(),
1447 blurSize.height());
1448 texture->ref();
1449 } else if (filter->asADilate(&radius)) {
1450 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1451 texture = context->applyMorphology(texture, rect,
1452 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001453 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001454 radius);
1455 texture->ref();
1456 } else if (filter->asAnErode(&radius)) {
1457 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1458 texture = context->applyMorphology(texture, rect,
1459 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001460 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001461 radius);
1462 texture->ref();
1463 }
1464 return texture;
1465}
1466
reed@google.comac10a2d2010-12-22 21:39:39 +00001467void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1468 int left, int top, const SkPaint& paint) {
1469 CHECK_SHOULD_DRAW(draw);
1470
reed@google.com8926b162012-03-23 15:36:36 +00001471 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001472 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1473 return;
1474 }
1475
reed@google.com76dd2772012-01-05 21:15:07 +00001476 int w = bitmap.width();
1477 int h = bitmap.height();
1478
bsalomon@google.com5782d712011-01-21 21:03:59 +00001479 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001480 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001481 return;
1482 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001483
bsalomon@google.com5782d712011-01-21 21:03:59 +00001484 GrAutoMatrix avm(fContext, GrMatrix::I());
1485
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001486 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001487
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001488 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001489 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001490 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001491 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001492
reed@google.com8926b162012-03-23 15:36:36 +00001493 SkImageFilter* filter = paint.getImageFilter();
1494 if (NULL != filter) {
1495 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001496 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001497 if (filteredTexture) {
1498 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1499 texture = filteredTexture;
1500 filteredTexture->unref();
1501 }
reed@google.com76dd2772012-01-05 21:15:07 +00001502 }
reed@google.com8926b162012-03-23 15:36:36 +00001503
bsalomon@google.com5782d712011-01-21 21:03:59 +00001504 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001505 GrRect::MakeXYWH(GrIntToScalar(left),
1506 GrIntToScalar(top),
1507 GrIntToScalar(w),
1508 GrIntToScalar(h)),
1509 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1510 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001511}
1512
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001513void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001514 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001515 // clear of the source device must occur before CHECK_SHOULD_DRAW
1516 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1517 if (dev->fNeedClear) {
1518 // TODO: could check here whether we really need to draw at all
1519 dev->clear(0x0);
1520 }
1521
reed@google.comac10a2d2010-12-22 21:39:39 +00001522 CHECK_SHOULD_DRAW(draw);
1523
bsalomon@google.com5782d712011-01-21 21:03:59 +00001524 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001525 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001526 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001527 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001528 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001529
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001530 GrTexture* devTex = grPaint.getTexture(0);
1531 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001532
reed@google.com8926b162012-03-23 15:36:36 +00001533 SkImageFilter* filter = paint.getImageFilter();
1534 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001535 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1536 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001537 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1538 rect);
1539 if (filteredTexture) {
1540 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1541 devTex = filteredTexture;
1542 filteredTexture->unref();
1543 }
1544 }
1545
bsalomon@google.com5782d712011-01-21 21:03:59 +00001546 const SkBitmap& bm = dev->accessBitmap(false);
1547 int w = bm.width();
1548 int h = bm.height();
1549
1550 GrAutoMatrix avm(fContext, GrMatrix::I());
1551
bsalomon@google.com97912912011-12-06 16:30:36 +00001552 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001553
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001554 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1555 GrIntToScalar(y),
1556 GrIntToScalar(w),
1557 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001558
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001559 // The device being drawn may not fill up its texture (saveLayer uses
1560 // the approximate ).
1561 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1562 GR_Scalar1 * h / devTex->height());
1563
1564 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001565}
1566
reed@google.com8926b162012-03-23 15:36:36 +00001567bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001568 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001569 SkISize radius;
1570 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001571 return false;
1572 }
reed@google.com8926b162012-03-23 15:36:36 +00001573 return true;
1574}
1575
1576bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1577 const SkMatrix& ctm,
1578 SkBitmap* result, SkIPoint* offset) {
1579 // want explicitly our impl, so guard against a subclass of us overriding it
1580 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001581 return false;
1582 }
reed@google.com8926b162012-03-23 15:36:36 +00001583
1584 SkAutoLockPixels alp(src, !src.getTexture());
1585 if (!src.getTexture() && !src.readyToDraw()) {
1586 return false;
1587 }
1588
1589 GrPaint paint;
1590 paint.reset();
1591
1592 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1593
1594 GrTexture* texture;
1595 SkAutoCachedTexture act(this, src, sampler, &texture);
1596
1597 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001598 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1599 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001600 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1601 if (resultTexture) {
1602 result->setPixelRef(new SkGrTexturePixelRef(resultTexture))->unref();
1603 resultTexture->unref();
1604 }
reed@google.com76dd2772012-01-05 21:15:07 +00001605 return true;
1606}
1607
reed@google.comac10a2d2010-12-22 21:39:39 +00001608///////////////////////////////////////////////////////////////////////////////
1609
1610// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001611static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001612 kTriangles_GrPrimitiveType,
1613 kTriangleStrip_GrPrimitiveType,
1614 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001615};
1616
1617void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1618 int vertexCount, const SkPoint vertices[],
1619 const SkPoint texs[], const SkColor colors[],
1620 SkXfermode* xmode,
1621 const uint16_t indices[], int indexCount,
1622 const SkPaint& paint) {
1623 CHECK_SHOULD_DRAW(draw);
1624
bsalomon@google.com5782d712011-01-21 21:03:59 +00001625 GrPaint grPaint;
1626 SkAutoCachedTexture act;
1627 // we ignore the shader if texs is null.
1628 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001629 if (!skPaint2GrPaintNoShader(paint,
1630 false,
1631 NULL == colors,
1632 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001633 return;
1634 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001635 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001636 if (!skPaint2GrPaintShader(this,
1637 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001638 NULL == colors,
1639 &act,
1640 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001641 return;
1642 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001643 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001644
1645 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001646 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001647 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1648#if 0
1649 return
1650#endif
1651 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001652 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001653
bsalomon@google.com498776a2011-08-16 19:20:44 +00001654 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1655 if (NULL != colors) {
1656 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001657 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001658 for (int i = 0; i < vertexCount; ++i) {
1659 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1660 }
1661 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001662 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001663 fContext->drawVertices(grPaint,
1664 gVertexMode2PrimitiveType[vmode],
1665 vertexCount,
1666 (GrPoint*) vertices,
1667 (GrPoint*) texs,
1668 colors,
1669 indices,
1670 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001671}
1672
1673///////////////////////////////////////////////////////////////////////////////
1674
1675static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001676 GrFontScaler* scaler = (GrFontScaler*)data;
1677 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001678}
1679
1680static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1681 void* auxData;
1682 GrFontScaler* scaler = NULL;
1683 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1684 scaler = (GrFontScaler*)auxData;
1685 }
1686 if (NULL == scaler) {
1687 scaler = new SkGrFontScaler(cache);
1688 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1689 }
1690 return scaler;
1691}
1692
1693static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1694 SkFixed fx, SkFixed fy,
1695 const SkGlyph& glyph) {
1696 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1697
bungeman@google.com15865a72012-01-11 16:28:04 +00001698 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001699
1700 if (NULL == procs->fFontScaler) {
1701 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1702 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001703
bungeman@google.com15865a72012-01-11 16:28:04 +00001704 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1705 glyph.getSubXFixed(),
1706 glyph.getSubYFixed()),
1707 SkFixedFloorToFixed(fx),
1708 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001709 procs->fFontScaler);
1710}
1711
bsalomon@google.com5782d712011-01-21 21:03:59 +00001712SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001713
1714 // deferred allocation
1715 if (NULL == fDrawProcs) {
1716 fDrawProcs = new GrSkDrawProcs;
1717 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1718 fDrawProcs->fContext = fContext;
1719 }
1720
1721 // init our (and GL's) state
1722 fDrawProcs->fTextContext = context;
1723 fDrawProcs->fFontScaler = NULL;
1724 return fDrawProcs;
1725}
1726
1727void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1728 size_t byteLength, SkScalar x, SkScalar y,
1729 const SkPaint& paint) {
1730 CHECK_SHOULD_DRAW(draw);
1731
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001732 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001733 // this guy will just call our drawPath()
1734 draw.drawText((const char*)text, byteLength, x, y, paint);
1735 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001736 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001737
1738 GrPaint grPaint;
1739 SkAutoCachedTexture act;
1740
bsalomon@google.com84405e02012-03-05 19:57:21 +00001741 if (!skPaint2GrPaintShader(this,
1742 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001743 true,
1744 &act,
1745 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001746 return;
1747 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001748 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1749 grPaint, draw.fExtMatrix);
1750 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001751 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1752 }
1753}
1754
1755void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1756 size_t byteLength, const SkScalar pos[],
1757 SkScalar constY, int scalarsPerPos,
1758 const SkPaint& paint) {
1759 CHECK_SHOULD_DRAW(draw);
1760
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001761 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001762 // this guy will just call our drawPath()
1763 draw.drawPosText((const char*)text, byteLength, pos, constY,
1764 scalarsPerPos, paint);
1765 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001766 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001767
1768 GrPaint grPaint;
1769 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001770 if (!skPaint2GrPaintShader(this,
1771 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001772 true,
1773 &act,
1774 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001775 return;
1776 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001777 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1778 grPaint, draw.fExtMatrix);
1779 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001780 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1781 scalarsPerPos, paint);
1782 }
1783}
1784
1785void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1786 size_t len, const SkPath& path,
1787 const SkMatrix* m, const SkPaint& paint) {
1788 CHECK_SHOULD_DRAW(draw);
1789
1790 SkASSERT(draw.fDevice == this);
1791 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1792}
1793
1794///////////////////////////////////////////////////////////////////////////////
1795
reed@google.comf67e4cf2011-03-15 20:56:58 +00001796bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1797 if (!paint.isLCDRenderText()) {
1798 // we're cool with the paint as is
1799 return false;
1800 }
1801
1802 if (paint.getShader() ||
1803 paint.getXfermode() || // unless its srcover
1804 paint.getMaskFilter() ||
1805 paint.getRasterizer() ||
1806 paint.getColorFilter() ||
1807 paint.getPathEffect() ||
1808 paint.isFakeBoldText() ||
1809 paint.getStyle() != SkPaint::kFill_Style) {
1810 // turn off lcd
1811 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1812 flags->fHinting = paint.getHinting();
1813 return true;
1814 }
1815 // we're cool with the paint as is
1816 return false;
1817}
1818
reed@google.com75d939b2011-12-07 15:07:23 +00001819void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001820 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001821 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001822}
1823
reed@google.comf67e4cf2011-03-15 20:56:58 +00001824///////////////////////////////////////////////////////////////////////////////
1825
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001826SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1827 const SkBitmap& bitmap,
1828 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001829 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001830 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001831
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001832 if (!bitmap.isVolatile()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001833 uint64_t key = bitmap.getGenerationID();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001834 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001835
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001836 GrTextureDesc desc;
1837 desc.fWidth = bitmap.width();
1838 desc.fHeight = bitmap.height();
1839 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
robertphillips@google.com56f22442012-06-08 14:21:26 +00001840 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001841
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001842 entry = ctx->findAndLockTexture(desc, sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001843 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001844 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1845 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001846 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001847 } else {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001848 entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001849 sampler, bitmap);
1850 }
1851 if (NULL == entry.texture()) {
1852 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1853 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001854 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001855 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001856}
1857
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001858void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1859 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001860}
1861
bsalomon@google.comfb309512011-11-30 14:13:48 +00001862bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1863 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001864 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001865 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001866
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001867 GrTextureDesc desc;
1868 desc.fWidth = bitmap.width();
1869 desc.fHeight = bitmap.height();
1870 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
1871 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001872
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001873 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001874}
1875
1876
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001877SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1878 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001879 bool isOpaque,
1880 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001881 GrTextureDesc desc;
1882 desc.fConfig = fRenderTarget->config();
1883 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1884 desc.fWidth = width;
1885 desc.fHeight = height;
1886 desc.fSampleCnt = fRenderTarget->numSamples();
1887
1888 GrContext::TextureCacheEntry cacheEntry;
1889 GrTexture* texture;
1890 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001891 // Skia's convention is to only clear a device if it is non-opaque.
1892 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001893
1894#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1895 // layers are never draw in repeat modes, so we can request an approx
1896 // match and ignore any padding.
1897 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1898 GrContext::kApprox_ScratchTexMatch :
1899 GrContext::kExact_ScratchTexMatch;
1900 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1901 texture = cacheEntry.texture();
1902#else
1903 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1904 texture = tunref.get();
1905#endif
1906 if (texture) {
1907 return SkNEW_ARGS(SkGpuDevice,(fContext,
1908 texture,
1909 cacheEntry,
1910 needClear));
1911 } else {
1912 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1913 width, height);
1914 return NULL;
1915 }
1916}
1917
1918SkGpuDevice::SkGpuDevice(GrContext* context,
1919 GrTexture* texture,
1920 TexCache cacheEntry,
1921 bool needClear)
1922 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1923 GrAssert(texture && texture->asRenderTarget());
1924 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1925 this->initFromRenderTarget(context, texture->asRenderTarget());
1926 fCache = cacheEntry;
1927 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001928}
1929
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001930GrTextContext* SkGpuDevice::getTextContext() {
1931 if (NULL == fTextContext) {
1932 fTextContext = new GrDefaultTextContext();
1933 }
1934 return fTextContext;
1935}