blob: c2e090c3be6017db9dbd5201fc04815bdd54bd11 [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
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrContext.h"
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000012#include "GrDefaultTextContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrTextContext.h"
14
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "SkGpuDevice.h"
16#include "SkGrTexturePixelRef.h"
17
Scroggo97c88c22011-05-11 14:05:25 +000018#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019#include "SkDrawProcs.h"
20#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000021#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000022#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000023#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.com06cd7322012-03-30 18:45:35 +000025#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000026
27#if 0
28 extern bool (*gShouldDrawProc)();
29 #define CHECK_SHOULD_DRAW(draw) \
30 do { \
31 if (gShouldDrawProc && !gShouldDrawProc()) return; \
32 this->prepareRenderTarget(draw); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000033 GrAssert(!fNeedClear) \
reed@google.comac10a2d2010-12-22 21:39:39 +000034 } while (0)
35#else
bsalomon@google.com06cd7322012-03-30 18:45:35 +000036 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \
37 GrAssert(!fNeedClear)
reed@google.comac10a2d2010-12-22 21:39:39 +000038#endif
39
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000040// we use the same texture slot on GrPaint for bitmaps and shaders
41// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
42enum {
43 kBitmapTextureIdx = 0,
44 kShaderTextureIdx = 0
45};
46
reed@google.comcde92112011-07-06 20:00:52 +000047
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000048#define MAX_BLUR_SIGMA 4.0f
49// FIXME: This value comes from from SkBlurMaskFilter.cpp.
50// Should probably be put in a common header someplace.
51#define MAX_BLUR_RADIUS SkIntToScalar(128)
52// This constant approximates the scaling done in the software path's
53// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
54// IMHO, it actually should be 1: we blur "less" than we should do
55// according to the CSS and canvas specs, simply because Safari does the same.
56// Firefox used to do the same too, until 4.0 where they fixed it. So at some
57// point we should probably get rid of these scaling constants and rebaseline
58// all the blur tests.
59#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000060// This constant represents the screen alignment criterion in texels for
61// requiring texture domain clamping to prevent color bleeding when drawing
62// a sub region of a larger source image.
63#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000064
65#define DO_DEFERRED_CLEAR \
66 do { \
67 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000068 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000069 fNeedClear = false; \
70 } \
71 } while (false) \
72
reed@google.comac10a2d2010-12-22 21:39:39 +000073///////////////////////////////////////////////////////////////////////////////
74
bsalomon@google.com84405e02012-03-05 19:57:21 +000075class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
76public:
77 SkAutoCachedTexture() { }
78 SkAutoCachedTexture(SkGpuDevice* device,
79 const SkBitmap& bitmap,
80 const GrSamplerState* sampler,
81 GrTexture** texture) {
82 GrAssert(texture);
83 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000084 }
reed@google.comac10a2d2010-12-22 21:39:39 +000085
bsalomon@google.com84405e02012-03-05 19:57:21 +000086 ~SkAutoCachedTexture() {
87 if (fTex.texture()) {
88 fDevice->unlockCachedTexture(fTex);
89 }
reed@google.comac10a2d2010-12-22 21:39:39 +000090 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000091
92 GrTexture* set(SkGpuDevice* device,
93 const SkBitmap& bitmap,
94 const GrSamplerState* sampler) {
95 if (fTex.texture()) {
96 fDevice->unlockCachedTexture(fTex);
97 }
98 fDevice = device;
99 GrTexture* texture = (GrTexture*)bitmap.getTexture();
100 if (texture) {
101 // return the native texture
102 fTex.reset();
103 } else {
104 // look it up in our cache
105 fTex = device->lockCachedTexture(bitmap, sampler);
106 texture = fTex.texture();
107 }
108 return texture;
109 }
110
111private:
112 SkGpuDevice* fDevice;
113 GrContext::TextureCacheEntry fTex;
114};
reed@google.comac10a2d2010-12-22 21:39:39 +0000115
116///////////////////////////////////////////////////////////////////////////////
117
118bool gDoTraceDraw;
119
120struct GrSkDrawProcs : public SkDrawProcs {
121public:
122 GrContext* fContext;
123 GrTextContext* fTextContext;
124 GrFontScaler* fFontScaler; // cached in the skia glyphcache
125};
126
127///////////////////////////////////////////////////////////////////////////////
128
reed@google.comaf951c92011-06-16 19:10:39 +0000129static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
130 switch (config) {
131 case kAlpha_8_GrPixelConfig:
132 *isOpaque = false;
133 return SkBitmap::kA8_Config;
134 case kRGB_565_GrPixelConfig:
135 *isOpaque = true;
136 return SkBitmap::kRGB_565_Config;
137 case kRGBA_4444_GrPixelConfig:
138 *isOpaque = false;
139 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000140 case kSkia8888_PM_GrPixelConfig:
141 // we don't currently have a way of knowing whether
142 // a 8888 is opaque based on the config.
143 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000144 return SkBitmap::kARGB_8888_Config;
145 default:
146 *isOpaque = false;
147 return SkBitmap::kNo_Config;
148 }
149}
reed@google.comac10a2d2010-12-22 21:39:39 +0000150
reed@google.comaf951c92011-06-16 19:10:39 +0000151static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000152 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000153
154 bool isOpaque;
155 SkBitmap bitmap;
156 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
157 renderTarget->width(), renderTarget->height());
158 bitmap.setIsOpaque(isOpaque);
159 return bitmap;
160}
161
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000162SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
163: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
164 this->initFromRenderTarget(context, texture->asRenderTarget());
165}
166
reed@google.comaf951c92011-06-16 19:10:39 +0000167SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
168: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000169 this->initFromRenderTarget(context, renderTarget);
170}
171
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000172void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000173 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000174 fNeedPrepareRenderTarget = false;
175 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000176
reed@google.comaf951c92011-06-16 19:10:39 +0000177 fContext = context;
178 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000179
reed@google.comaf951c92011-06-16 19:10:39 +0000180 fTexture = NULL;
181 fRenderTarget = NULL;
182 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000183
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000184 GrAssert(NULL != renderTarget);
185 fRenderTarget = renderTarget;
186 fRenderTarget->ref();
187 // if this RT is also a texture, hold a ref on it
188 fTexture = fRenderTarget->asTexture();
189 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000190
191 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
192 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
193 // the device via accessBitmap. This external ref may outlive the device.
194 // Since textures own their render targets (but not vice-versa) we
195 // are ensuring that both objects will live as long as the pixel ref.
196 SkPixelRef* pr;
197 if (fTexture) {
198 pr = new SkGrTexturePixelRef(fTexture);
199 } else {
200 pr = new SkGrRenderTargetPixelRef(fRenderTarget);
201 }
reed@google.comaf951c92011-06-16 19:10:39 +0000202 this->setPixelRef(pr, 0)->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000203
204 fTextContext = NULL;
reed@google.comaf951c92011-06-16 19:10:39 +0000205}
206
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000207SkGpuDevice::SkGpuDevice(GrContext* context,
208 SkBitmap::Config config,
209 int width,
210 int height)
211 : SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000212 fNeedPrepareRenderTarget = false;
213 fDrawProcs = NULL;
214
reed@google.com7b201d22011-01-11 18:59:23 +0000215 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000216 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000217
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 fTexture = NULL;
219 fRenderTarget = NULL;
220 fNeedClear = false;
221
reed@google.comaf951c92011-06-16 19:10:39 +0000222 if (config != SkBitmap::kRGB_565_Config) {
223 config = SkBitmap::kARGB_8888_Config;
224 }
225 SkBitmap bm;
226 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000227
reed@google.comaf951c92011-06-16 19:10:39 +0000228 const GrTextureDesc desc = {
229 kRenderTarget_GrTextureFlagBit,
reed@google.comaf951c92011-06-16 19:10:39 +0000230 width,
231 height,
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000232 SkGr::Bitmap2PixelConfig(bm),
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000233 0 // samples
reed@google.comaf951c92011-06-16 19:10:39 +0000234 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000235
reed@google.comaf951c92011-06-16 19:10:39 +0000236 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000237
reed@google.comaf951c92011-06-16 19:10:39 +0000238 if (NULL != fTexture) {
239 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000240 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000241
reed@google.comaf951c92011-06-16 19:10:39 +0000242 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000243
reed@google.comaf951c92011-06-16 19:10:39 +0000244 // wrap the bitmap with a pixelref to expose our texture
245 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000246 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000247 } else {
248 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
249 width, height);
250 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000251 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000252
253 fTextContext = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000254}
255
256SkGpuDevice::~SkGpuDevice() {
257 if (fDrawProcs) {
258 delete fDrawProcs;
259 }
260
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000261 SkSafeUnref(fTexture);
262 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000263 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000264 GrAssert(NULL != fTexture);
265 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000266 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000267 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000268 fContext->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000269
270 if (NULL != fTextContext) {
271 fTextContext->unref();
272 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000273}
274
reed@google.comac10a2d2010-12-22 21:39:39 +0000275///////////////////////////////////////////////////////////////////////////////
276
277void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000278 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000279 fContext->setRenderTarget(fRenderTarget);
280 fContext->flush(true);
281 fNeedPrepareRenderTarget = true;
282}
283
284///////////////////////////////////////////////////////////////////////////////
285
bsalomon@google.comc4364992011-11-07 15:54:49 +0000286namespace {
287GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
288 switch (config8888) {
289 case SkCanvas::kNative_Premul_Config8888:
290 return kSkia8888_PM_GrPixelConfig;
291 case SkCanvas::kNative_Unpremul_Config8888:
292 return kSkia8888_UPM_GrPixelConfig;
293 case SkCanvas::kBGRA_Premul_Config8888:
294 return kBGRA_8888_PM_GrPixelConfig;
295 case SkCanvas::kBGRA_Unpremul_Config8888:
296 return kBGRA_8888_UPM_GrPixelConfig;
297 case SkCanvas::kRGBA_Premul_Config8888:
298 return kRGBA_8888_PM_GrPixelConfig;
299 case SkCanvas::kRGBA_Unpremul_Config8888:
300 return kRGBA_8888_UPM_GrPixelConfig;
301 default:
302 GrCrash("Unexpected Config8888.");
303 return kSkia8888_PM_GrPixelConfig;
304 }
305}
306}
307
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000308bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
309 int x, int y,
310 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000311 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000312 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
313 SkASSERT(!bitmap.isNull());
314 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000315
bsalomon@google.com910267d2011-11-02 20:06:25 +0000316 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000317 GrPixelConfig config;
318 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000319 return fContext->readRenderTargetPixels(fRenderTarget,
320 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000321 bitmap.width(),
322 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000323 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000324 bitmap.getPixels(),
325 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000326}
327
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000328void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
329 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000330 SkAutoLockPixels alp(bitmap);
331 if (!bitmap.readyToDraw()) {
332 return;
333 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000334
335 GrPixelConfig config;
336 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
337 config = config8888_to_gr_config(config8888);
338 } else {
339 config= SkGr::BitmapConfig2PixelConfig(bitmap.config(),
340 bitmap.isOpaque());
341 }
342
bsalomon@google.com6f379512011-11-16 20:36:03 +0000343 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
344 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000345}
346
347///////////////////////////////////////////////////////////////////////////////
348
349static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000350 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000351 const SkRegion& clipRegion,
352 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000353 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000354
355 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000356 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000357 const SkIRect& skBounds = clipRegion.getBounds();
358 GrRect bounds;
359 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
360 GrIntToScalar(skBounds.fTop),
361 GrIntToScalar(skBounds.fRight),
362 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000363 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
364 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000365 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000366}
367
368// call this ever each draw call, to ensure that the context reflects our state,
369// and not the state from some other canvas/device
370void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
371 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000372 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000373
374 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000375 SkASSERT(draw.fClipStack);
376 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000377 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 fNeedPrepareRenderTarget = false;
379 }
380}
381
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000382void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
383 const SkClipStack& clipStack) {
384 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
385 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000386 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000387}
388
389void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000390 const SkRegion& clip, const SkClipStack& clipStack) {
391
reed@google.comac10a2d2010-12-22 21:39:39 +0000392 fContext->setRenderTarget(fRenderTarget);
393
bsalomon@google.comd302f142011-03-03 13:54:13 +0000394 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000395
reed@google.com6f8f2922011-03-04 22:27:10 +0000396 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000397
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000398 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000399}
400
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000401SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000402 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000403 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000404}
405
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000406bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000407 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000408 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000409 return true;
410 }
411 return false;
412}
413
414///////////////////////////////////////////////////////////////////////////////
415
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000416SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
417SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
418SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
419SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
420SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
421 shader_type_mismatch);
422SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000423
bsalomon@google.com5782d712011-01-21 21:03:59 +0000424static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
425 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
426 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
427 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
428 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
429 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
430};
431
bsalomon@google.com84405e02012-03-05 19:57:21 +0000432namespace {
433
434// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
435// justAlpha indicates that skPaint's alpha should be used rather than the color
436// Callers may subsequently modify the GrPaint. Setting constantColor indicates
437// that the final paint will draw the same color at every pixel. This allows
438// an optimization where the the color filter can be applied to the skPaint's
439// color once while converting to GrPain and then ignored.
440inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
441 bool justAlpha,
442 bool constantColor,
443 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000444
445 grPaint->fDither = skPaint.isDither();
446 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000447 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000448
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000449 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
450 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000451
452 SkXfermode* mode = skPaint.getXfermode();
453 if (mode) {
454 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000455 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000456#if 0
457 return false;
458#endif
459 }
460 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000461 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
462 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
463
bsalomon@google.com5782d712011-01-21 21:03:59 +0000464 if (justAlpha) {
465 uint8_t alpha = skPaint.getAlpha();
466 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000467 // justAlpha is currently set to true only if there is a texture,
468 // so constantColor should not also be true.
469 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000470 } else {
471 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000472 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000473 }
Scroggo97c88c22011-05-11 14:05:25 +0000474 SkColorFilter* colorFilter = skPaint.getColorFilter();
475 SkColor color;
476 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000477 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000478 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000479 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000480 if (!constantColor) {
481 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
482 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000483 } else {
484 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
485 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000486 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000487 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000488 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
489 grPaint->fColorMatrixEnabled = true;
490 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
491 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
492 } else {
493 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000494 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000495 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000496}
497
bsalomon@google.com84405e02012-03-05 19:57:21 +0000498// This function is similar to skPaint2GrPaintNoShader but also converts
499// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
500// be used is set on grPaint and returned in param act. constantColor has the
501// same meaning as in skPaint2GrPaintNoShader.
502inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
503 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000504 bool constantColor,
505 SkGpuDevice::SkAutoCachedTexture* act,
506 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000507
bsalomon@google.com5782d712011-01-21 21:03:59 +0000508 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000509
bsalomon@google.com5782d712011-01-21 21:03:59 +0000510 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000512 return skPaint2GrPaintNoShader(skPaint,
513 false,
514 constantColor,
515 grPaint);
516 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000517 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000518 }
519
reed@google.comac10a2d2010-12-22 21:39:39 +0000520 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000521 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000522 SkShader::TileMode tileModes[2];
523 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000524 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000525 tileModes, twoPointParams);
526
bsalomon@google.com5782d712011-01-21 21:03:59 +0000527 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
528 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000529 SkShader::GradientInfo info;
530 SkColor color;
531
532 info.fColors = &color;
533 info.fColorOffsets = NULL;
534 info.fColorCount = 1;
535 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
536 SkPaint copy(skPaint);
537 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000538 // modulate the paint alpha by the shader's solid color alpha
539 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
540 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000541 return skPaint2GrPaintNoShader(copy,
542 false,
543 constantColor,
544 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000545 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000546 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000547 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000548 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000549 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000550 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000551 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000552 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000553 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000554 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000555 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
556 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000557 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000558 sampler->setRadial2Params(twoPointParams[0],
559 twoPointParams[1],
560 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000561 }
562
bsalomon@google.com84405e02012-03-05 19:57:21 +0000563 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000564 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000565 SkDebugf("Couldn't convert bitmap to texture.\n");
566 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000567 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000568 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000569
570 // since our texture coords will be in local space, we wack the texture
571 // matrix to map them back into 0...1 before we load it
572 SkMatrix localM;
573 if (shader->getLocalMatrix(&localM)) {
574 SkMatrix inverse;
575 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000576 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000577 }
578 }
579 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000580 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
581 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000582 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000583 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000584 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000585 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000586 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000587
588 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000589}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000590}
reed@google.comac10a2d2010-12-22 21:39:39 +0000591
592///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000593
bsalomon@google.com398109c2011-04-14 18:40:27 +0000594void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000595 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.com31a58402011-04-27 21:00:02 +0000596 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000597}
598
reed@google.comac10a2d2010-12-22 21:39:39 +0000599void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
600 CHECK_SHOULD_DRAW(draw);
601
bsalomon@google.com5782d712011-01-21 21:03:59 +0000602 GrPaint grPaint;
603 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000604 if (!skPaint2GrPaintShader(this,
605 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000606 true,
607 &act,
608 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000609 return;
610 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000611
612 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000613}
614
615// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000616static const GrPrimitiveType gPointMode2PrimtiveType[] = {
617 kPoints_PrimitiveType,
618 kLines_PrimitiveType,
619 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000620};
621
622void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000623 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000624 CHECK_SHOULD_DRAW(draw);
625
626 SkScalar width = paint.getStrokeWidth();
627 if (width < 0) {
628 return;
629 }
630
631 // we only handle hairlines here, else we let the SkDraw call our drawPath()
632 if (width > 0) {
633 draw.drawPoints(mode, count, pts, paint, true);
634 return;
635 }
636
bsalomon@google.com5782d712011-01-21 21:03:59 +0000637 GrPaint grPaint;
638 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000639 if (!skPaint2GrPaintShader(this,
640 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000641 true,
642 &act,
643 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000644 return;
645 }
646
bsalomon@google.com5782d712011-01-21 21:03:59 +0000647 fContext->drawVertices(grPaint,
648 gPointMode2PrimtiveType[mode],
649 count,
650 (GrPoint*)pts,
651 NULL,
652 NULL,
653 NULL,
654 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000655}
656
reed@google.comc9aa5872011-04-05 21:05:37 +0000657///////////////////////////////////////////////////////////////////////////////
658
reed@google.comac10a2d2010-12-22 21:39:39 +0000659void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
660 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000661 CHECK_SHOULD_DRAW(draw);
662
bungeman@google.com79bd8772011-07-18 15:34:08 +0000663 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000664 SkScalar width = paint.getStrokeWidth();
665
666 /*
667 We have special code for hairline strokes, miter-strokes, and fills.
668 Anything else we just call our path code.
669 */
670 bool usePath = doStroke && width > 0 &&
671 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000672 // another two reasons we might need to call drawPath...
673 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000674 usePath = true;
675 }
reed@google.com67db6642011-05-26 11:46:35 +0000676 // until we aa rotated rects...
677 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
678 usePath = true;
679 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000680 // small miter limit means right angles show bevel...
681 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
682 paint.getStrokeMiter() < SK_ScalarSqrt2)
683 {
684 usePath = true;
685 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000686 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000687 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
688 usePath = true;
689 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000690
691 if (usePath) {
692 SkPath path;
693 path.addRect(rect);
694 this->drawPath(draw, path, paint, NULL, true);
695 return;
696 }
697
698 GrPaint grPaint;
699 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000700 if (!skPaint2GrPaintShader(this,
701 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000702 true,
703 &act,
704 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000705 return;
706 }
reed@google.com20efde72011-05-09 17:00:02 +0000707 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000708}
709
reed@google.com69302852011-02-16 18:08:07 +0000710#include "SkMaskFilter.h"
711#include "SkBounder.h"
712
bsalomon@google.com85003222012-03-28 14:44:37 +0000713///////////////////////////////////////////////////////////////////////////////
714
715// helpers for applying mask filters
716namespace {
717
718GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000719 switch (fillType) {
720 case SkPath::kWinding_FillType:
721 return kWinding_PathFill;
722 case SkPath::kEvenOdd_FillType:
723 return kEvenOdd_PathFill;
724 case SkPath::kInverseWinding_FillType:
725 return kInverseWinding_PathFill;
726 case SkPath::kInverseEvenOdd_FillType:
727 return kInverseEvenOdd_PathFill;
728 default:
729 SkDebugf("Unsupported path fill type\n");
730 return kHairLine_PathFill;
731 }
732}
733
bsalomon@google.com85003222012-03-28 14:44:37 +0000734// We prefer to blur small rect with small radius via CPU.
735#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
736#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
737inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
738 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
739 rect.height() <= MIN_GPU_BLUR_SIZE &&
740 radius <= MIN_GPU_BLUR_RADIUS) {
741 return true;
742 }
743 return false;
744}
745
746bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
747 SkMaskFilter* filter, const SkMatrix& matrix,
748 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000749 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000750#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000751 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000752#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000753 SkMaskFilter::BlurInfo info;
754 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000755 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000756 return false;
757 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000758 SkScalar radius = info.fIgnoreTransform ? info.fRadius
759 : matrix.mapRadius(info.fRadius);
760 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000761 if (radius <= 0) {
762 return false;
763 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000764
765 SkRect srcRect = path.getBounds();
766 if (shouldDrawBlurWithCPU(srcRect, radius)) {
767 return false;
768 }
769
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000770 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000771 float sigma3 = sigma * 3.0f;
772
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000773 SkRect clipRect;
774 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000775
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000776 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
777 srcRect.inset(-sigma3, -sigma3);
778 clipRect.inset(-sigma3, -sigma3);
779 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000780 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000781 SkIRect finalIRect;
782 finalRect.roundOut(&finalIRect);
783 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000784 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000785 }
786 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000787 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000788 }
789 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000790 srcRect.offset(offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000791 const GrTextureDesc desc = {
792 kRenderTarget_GrTextureFlagBit,
bungeman@google.comf8aa18c2012-03-19 21:04:52 +0000793 SkScalarCeilToInt(srcRect.width()),
794 SkScalarCeilToInt(srcRect.height()),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000795 // We actually only need A8, but it often isn't supported as a
796 // render target
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000797 kRGBA_8888_PM_GrPixelConfig,
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000798 0 // samples
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000799 };
800
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000801 GrAutoScratchTexture pathEntry(context, desc);
802 GrTexture* pathTexture = pathEntry.texture();
803 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000804 return false;
805 }
806 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000807 // Once this code moves into GrContext, this should be changed to use
808 // an AutoClipRestore.
809 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000810 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000811 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000812 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000813 GrPaint tempPaint;
814 tempPaint.reset();
815
816 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000817 tempPaint.fAntiAlias = grp->fAntiAlias;
818 if (tempPaint.fAntiAlias) {
819 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
820 // blend coeff of zero requires dual source blending support in order
821 // to properly blend partially covered pixels. This means the AA
822 // code path may not be taken. So we use a dst blend coeff of ISA. We
823 // could special case AA draws to a dst surface with known alpha=0 to
824 // use a zero dst coeff when dual source blending isn't available.
825 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
826 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
827 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000828 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000829 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000830
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000831 GrAutoScratchTexture temp1, temp2;
832 // If we're doing a normal blur, we can clobber the pathTexture in the
833 // gaussianBlur. Otherwise, we need to save it for later compositing.
834 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000835 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
836 &temp1,
837 isNormalBlur ? NULL : &temp2,
838 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000839
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000840 if (!isNormalBlur) {
841 GrPaint paint;
842 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000843 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000844 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
845 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000846 // Blend pathTexture over blurTexture.
847 context->setRenderTarget(blurTexture->asRenderTarget());
848 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000849 if (SkMaskFilter::kInner_BlurType == blurType) {
850 // inner: dst = dst * src
851 paint.fSrcBlendCoeff = kDC_BlendCoeff;
852 paint.fDstBlendCoeff = kZero_BlendCoeff;
853 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
854 // solid: dst = src + dst - src * dst
855 // = (1 - dst) * src + 1 * dst
856 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
857 paint.fDstBlendCoeff = kOne_BlendCoeff;
858 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
859 // outer: dst = dst * (1 - src)
860 // = 0 * src + (1 - src) * dst
861 paint.fSrcBlendCoeff = kZero_BlendCoeff;
862 paint.fDstBlendCoeff = kISC_BlendCoeff;
863 }
864 context->drawRect(paint, srcRect);
865 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000866 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000867 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000868
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000869 if (grp->hasTextureOrMask()) {
870 GrMatrix inverse;
871 if (!matrix.invert(&inverse)) {
872 return false;
873 }
874 grp->preConcatActiveSamplerMatrices(inverse);
875 }
876
877 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
878 // we assume the last mask index is available for use
879 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000880 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000881 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000882
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000883 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
884 -finalRect.fTop);
885 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
886 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000887 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000888 return true;
889}
890
bsalomon@google.com85003222012-03-28 14:44:37 +0000891bool drawWithMaskFilter(GrContext* context, const SkPath& path,
892 SkMaskFilter* filter, const SkMatrix& matrix,
893 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000894 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000895 SkMask srcM, dstM;
896
897 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000898 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000899 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000900 return false;
901 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000902 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000903
904 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
905 return false;
906 }
907 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000908 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000909
910 if (clip.quickReject(dstM.fBounds)) {
911 return false;
912 }
913 if (bounder && !bounder->doIRect(dstM.fBounds)) {
914 return false;
915 }
916
917 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
918 // the current clip (and identity matrix) and grpaint settings
919
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000920 // used to compute inverse view, if necessary
921 GrMatrix ivm = context->getMatrix();
922
reed@google.com0c219b62011-02-16 21:31:18 +0000923 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000924
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000925 const GrTextureDesc desc = {
926 kNone_GrTextureFlags,
reed@google.com69302852011-02-16 18:08:07 +0000927 dstM.fBounds.width(),
928 dstM.fBounds.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000929 kAlpha_8_GrPixelConfig,
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000930 0, // samples
reed@google.com69302852011-02-16 18:08:07 +0000931 };
932
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000933 GrAutoScratchTexture ast(context, desc);
934 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000935
reed@google.com69302852011-02-16 18:08:07 +0000936 if (NULL == texture) {
937 return false;
938 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000939 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000940 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000941
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000942 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
943 grp->preConcatActiveSamplerMatrices(ivm);
944 }
945
946 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
947 // we assume the last mask index is available for use
948 GrAssert(NULL == grp->getMask(MASK_IDX));
949 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000950 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000951
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000952 GrRect d;
953 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000954 GrIntToScalar(dstM.fBounds.fTop),
955 GrIntToScalar(dstM.fBounds.fRight),
956 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000957
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000958 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
959 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
960 -dstM.fBounds.fTop*SK_Scalar1);
961 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000962 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000963 return true;
964}
reed@google.com69302852011-02-16 18:08:07 +0000965
bsalomon@google.com85003222012-03-28 14:44:37 +0000966}
967
968///////////////////////////////////////////////////////////////////////////////
969
reed@google.com0c219b62011-02-16 21:31:18 +0000970void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000971 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000972 bool pathIsMutable) {
973 CHECK_SHOULD_DRAW(draw);
974
reed@google.comfe626382011-09-21 13:50:35 +0000975 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000976
bsalomon@google.com5782d712011-01-21 21:03:59 +0000977 GrPaint grPaint;
978 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000979 if (!skPaint2GrPaintShader(this,
980 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000981 true,
982 &act,
983 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000984 return;
985 }
986
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000987 // can we cheat, and threat a thin stroke as a hairline w/ coverage
988 // if we can, we draw lots faster (raster device does this same test)
989 SkScalar hairlineCoverage;
990 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
991 doFill = false;
992 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
993 grPaint.fCoverage);
994 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000995
reed@google.comfe626382011-09-21 13:50:35 +0000996 // If we have a prematrix, apply it to the path, optimizing for the case
997 // where the original path can in fact be modified in place (even though
998 // its parameter type is const).
999 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1000 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001001
1002 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001003 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001004
reed@google.come3445642011-02-16 23:20:39 +00001005 if (!pathIsMutable) {
1006 result = &tmpPath;
1007 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001008 }
reed@google.come3445642011-02-16 23:20:39 +00001009 // should I push prePathMatrix on our MV stack temporarily, instead
1010 // of applying it here? See SkDraw.cpp
1011 pathPtr->transform(*prePathMatrix, result);
1012 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001013 }
reed@google.com0c219b62011-02-16 21:31:18 +00001014 // at this point we're done with prePathMatrix
1015 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001016
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001017 if (paint.getPathEffect() ||
1018 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001019 // it is safe to use tmpPath here, even if we already used it for the
1020 // prepathmatrix, since getFillPath can take the same object for its
1021 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001022 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001023 pathPtr = &tmpPath;
1024 }
1025
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001026 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001027 // avoid possibly allocating a new path in transform if we can
1028 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1029
1030 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001031 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001032 GrPathFill pathFillType = doFill ?
1033 skToGrFillType(devPathPtr->getFillType()) : kHairLine_PathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001034 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001035 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001036 &grPaint, pathFillType)) {
1037 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1038 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001039 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001040 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001041 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001042 }
reed@google.com69302852011-02-16 18:08:07 +00001043 return;
1044 }
reed@google.com69302852011-02-16 18:08:07 +00001045
bsalomon@google.comffca4002011-02-22 20:34:01 +00001046 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001047
reed@google.com0c219b62011-02-16 21:31:18 +00001048 if (doFill) {
1049 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001050 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001051 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 break;
1053 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001054 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001055 break;
1056 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001057 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 break;
1059 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001060 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001061 break;
1062 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001063 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 return;
1065 }
1066 }
1067
reed@google.com07f3ee12011-05-16 17:21:57 +00001068 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001069}
1070
bsalomon@google.comfb309512011-11-30 14:13:48 +00001071namespace {
1072
1073inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1074 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1075 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1076 return tilesX * tilesY;
1077}
1078
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001079inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001080 const SkIRect* srcRectPtr,
1081 int maxTextureSize) {
1082 static const int kSmallTileSize = 1 << 10;
1083 if (maxTextureSize <= kSmallTileSize) {
1084 return maxTextureSize;
1085 }
1086
1087 size_t maxTexTotalTileSize;
1088 size_t smallTotalTileSize;
1089
1090 if (NULL == srcRectPtr) {
1091 int w = bitmap.width();
1092 int h = bitmap.height();
1093 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1094 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1095 } else {
1096 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1097 srcRectPtr->fTop,
1098 srcRectPtr->fRight,
1099 srcRectPtr->fBottom,
1100 maxTextureSize);
1101 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1102 srcRectPtr->fTop,
1103 srcRectPtr->fRight,
1104 srcRectPtr->fBottom,
1105 kSmallTileSize);
1106 }
1107 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1108 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1109
1110 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1111 return kSmallTileSize;
1112 } else {
1113 return maxTextureSize;
1114 }
1115}
1116}
1117
1118bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1119 const GrSamplerState& sampler,
1120 const SkIRect* srcRectPtr,
1121 int* tileSize) const {
1122 SkASSERT(NULL != tileSize);
1123
1124 // if bitmap is explictly texture backed then just use the texture
1125 if (NULL != bitmap.getTexture()) {
1126 return false;
1127 }
1128 // if it's larger than the max texture size, then we have no choice but
1129 // tiling
1130 const int maxTextureSize = fContext->getMaxTextureSize();
1131 if (bitmap.width() > maxTextureSize ||
1132 bitmap.height() > maxTextureSize) {
1133 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1134 return true;
1135 }
1136 // if we are going to have to draw the whole thing, then don't tile
1137 if (NULL == srcRectPtr) {
1138 return false;
1139 }
1140 // if the entire texture is already in our cache then no reason to tile it
1141 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1142 return false;
1143 }
1144
1145 // At this point we know we could do the draw by uploading the entire bitmap
1146 // as a texture. However, if the texture would be large compared to the
1147 // cache size and we don't require most of it for this draw then tile to
1148 // reduce the amount of upload and cache spill.
1149
1150 // assumption here is that sw bitmap size is a good proxy for its size as
1151 // a texture
1152 size_t bmpSize = bitmap.getSize();
1153 size_t cacheSize;
1154 fContext->getTextureCacheLimits(NULL, &cacheSize);
1155 if (bmpSize < cacheSize / 2) {
1156 return false;
1157 }
1158
1159 SkFixed fracUsed =
1160 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1161 (srcRectPtr->height() << 16) / bitmap.height());
1162 if (fracUsed <= SK_FixedHalf) {
1163 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1164 return true;
1165 } else {
1166 return false;
1167 }
1168}
1169
reed@google.comac10a2d2010-12-22 21:39:39 +00001170void SkGpuDevice::drawBitmap(const SkDraw& draw,
1171 const SkBitmap& bitmap,
1172 const SkIRect* srcRectPtr,
1173 const SkMatrix& m,
1174 const SkPaint& paint) {
1175 CHECK_SHOULD_DRAW(draw);
1176
1177 SkIRect srcRect;
1178 if (NULL == srcRectPtr) {
1179 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1180 } else {
1181 srcRect = *srcRectPtr;
1182 }
1183
junov@google.comd935cfb2011-06-27 20:48:23 +00001184 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001185 // Convert the bitmap to a shader so that the rect can be drawn
1186 // through drawRect, which supports mask filters.
1187 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001188 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001189 if (srcRectPtr) {
1190 if (!bitmap.extractSubset(&tmp, srcRect)) {
1191 return; // extraction failed
1192 }
1193 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001194 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001195 }
1196 SkPaint paintWithTexture(paint);
1197 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1198 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001199 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001200 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001201
junov@google.com1d329782011-07-28 20:10:09 +00001202 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001203 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001204 // also affect the behavior of the mask filter.
1205 SkMatrix drawMatrix;
1206 drawMatrix.setConcat(*draw.fMatrix, m);
1207 SkDraw transformedDraw(draw);
1208 transformedDraw.fMatrix = &drawMatrix;
1209
1210 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1211
junov@google.comd935cfb2011-06-27 20:48:23 +00001212 return;
1213 }
1214
bsalomon@google.com5782d712011-01-21 21:03:59 +00001215 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001216 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001217 return;
1218 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001219 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001220 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001221 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001222 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001223 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001224 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001225
bsalomon@google.comfb309512011-11-30 14:13:48 +00001226 int tileSize;
1227 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1228 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001229 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001230 return;
1231 }
1232
1233 // undo the translate done by SkCanvas
1234 int DX = SkMax32(0, srcRect.fLeft);
1235 int DY = SkMax32(0, srcRect.fTop);
1236 // compute clip bounds in local coordinates
1237 SkIRect clipRect;
1238 {
1239 SkRect r;
1240 r.set(draw.fClip->getBounds());
1241 SkMatrix matrix, inverse;
1242 matrix.setConcat(*draw.fMatrix, m);
1243 if (!matrix.invert(&inverse)) {
1244 return;
1245 }
1246 inverse.mapRect(&r);
1247 r.roundOut(&clipRect);
1248 // apply the canvas' translate to our local clip
1249 clipRect.offset(DX, DY);
1250 }
1251
bsalomon@google.comfb309512011-11-30 14:13:48 +00001252 int nx = bitmap.width() / tileSize;
1253 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001254 for (int x = 0; x <= nx; x++) {
1255 for (int y = 0; y <= ny; y++) {
1256 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001257 tileR.set(x * tileSize, y * tileSize,
1258 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001259 if (!SkIRect::Intersects(tileR, clipRect)) {
1260 continue;
1261 }
1262
1263 SkIRect srcR = tileR;
1264 if (!srcR.intersect(srcRect)) {
1265 continue;
1266 }
1267
1268 SkBitmap tmpB;
1269 if (bitmap.extractSubset(&tmpB, tileR)) {
1270 // now offset it to make it "local" to our tmp bitmap
1271 srcR.offset(-tileR.fLeft, -tileR.fTop);
1272
1273 SkMatrix tmpM(m);
1274 {
1275 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1276 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1277 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1278 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001279 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 }
1281 }
1282 }
1283}
1284
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001285namespace {
1286
1287bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1288 // detect pixel disalignment
1289 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1290 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1291 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1292 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1293 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1294 COLOR_BLEED_TOLERANCE &&
1295 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1296 COLOR_BLEED_TOLERANCE) {
1297 return true;
1298 }
1299 return false;
1300}
1301
1302bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1303 const SkMatrix& m) {
1304 // Only gets called if hasAlignedSamples returned false.
1305 // So we can assume that sampling is axis aligned but not texel aligned.
1306 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1307 SkRect innerSrcRect(srcRect), innerTransformedRect,
1308 outerTransformedRect(transformedRect);
1309 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1310 m.mapRect(&innerTransformedRect, innerSrcRect);
1311
1312 // The gap between outerTransformedRect and innerTransformedRect
1313 // represents the projection of the source border area, which is
1314 // problematic for color bleeding. We must check whether any
1315 // destination pixels sample the border area.
1316 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1317 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1318 SkIRect outer, inner;
1319 outerTransformedRect.round(&outer);
1320 innerTransformedRect.round(&inner);
1321 // If the inner and outer rects round to the same result, it means the
1322 // border does not overlap any pixel centers. Yay!
1323 return inner != outer;
1324}
1325
1326} // unnamed namespace
1327
reed@google.comac10a2d2010-12-22 21:39:39 +00001328/*
1329 * This is called by drawBitmap(), which has to handle images that may be too
1330 * large to be represented by a single texture.
1331 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001332 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1333 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 */
1335void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1336 const SkBitmap& bitmap,
1337 const SkIRect& srcRect,
1338 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001339 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001340 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1341 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001342
reed@google.com9c49bc32011-07-07 13:42:37 +00001343 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001344 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001345 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 return;
1347 }
1348
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001349 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001350
1351 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1352 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1353 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001354 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001355
1356 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001357 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001358 if (NULL == texture) {
1359 return;
1360 }
1361
bsalomon@google.com452943d2011-10-31 17:37:14 +00001362 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001363
reed@google.com20efde72011-05-09 17:00:02 +00001364 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1365 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001366 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001367 float wInv = 1.f / bitmap.width();
1368 float hInv = 1.f / bitmap.height();
1369 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1370 SkFloatToScalar(srcRect.fTop * hInv),
1371 SkFloatToScalar(srcRect.fRight * wInv),
1372 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001373
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001374 bool needsTextureDomain = false;
1375 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1376 {
1377 // Need texture domain if drawing a sub rect.
1378 needsTextureDomain = srcRect.width() < bitmap.width() ||
1379 srcRect.height() < bitmap.height();
1380 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1381 // sampling is axis-aligned
1382 GrRect floatSrcRect, transformedRect;
1383 floatSrcRect.set(srcRect);
1384 SkMatrix srcToDeviceMatrix(m);
1385 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1386 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1387
1388 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1389 // Samples are texel-aligned, so filtering is futile
1390 sampler->setFilter(GrSamplerState::kNearest_Filter);
1391 needsTextureDomain = false;
1392 } else {
1393 needsTextureDomain = needsTextureDomain &&
1394 mayColorBleed(floatSrcRect, transformedRect, m);
1395 }
1396 }
1397 }
1398
1399 GrRect textureDomain = GrRect::MakeEmpty();
1400
1401 if (needsTextureDomain) {
1402 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001403 GrScalar left, top, right, bottom;
1404 if (srcRect.width() > 1) {
1405 GrScalar border = GR_ScalarHalf / bitmap.width();
1406 left = paintRect.left() + border;
1407 right = paintRect.right() - border;
1408 } else {
1409 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1410 }
1411 if (srcRect.height() > 1) {
1412 GrScalar border = GR_ScalarHalf / bitmap.height();
1413 top = paintRect.top() + border;
1414 bottom = paintRect.bottom() - border;
1415 } else {
1416 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1417 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001418 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001419 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001420 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001421
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001422 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001423}
1424
reed@google.com8926b162012-03-23 15:36:36 +00001425static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1426 SkImageFilter* filter, const GrRect& rect) {
1427 GrAssert(filter);
1428
1429 SkSize blurSize;
1430 SkISize radius;
1431
1432 const GrTextureDesc desc = {
1433 kRenderTarget_GrTextureFlagBit,
1434 rect.width(),
1435 rect.height(),
1436 kRGBA_8888_PM_GrPixelConfig,
bsalomon@google.comb9014f42012-03-30 14:22:41 +00001437 0 // samples
reed@google.com8926b162012-03-23 15:36:36 +00001438 };
1439
1440 if (filter->asABlur(&blurSize)) {
1441 GrAutoScratchTexture temp1, temp2;
1442 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1443 blurSize.width(),
1444 blurSize.height());
1445 texture->ref();
1446 } else if (filter->asADilate(&radius)) {
1447 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1448 texture = context->applyMorphology(texture, rect,
1449 temp1.texture(), temp2.texture(),
1450 GrSamplerState::kDilate_Filter,
1451 radius);
1452 texture->ref();
1453 } else if (filter->asAnErode(&radius)) {
1454 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1455 texture = context->applyMorphology(texture, rect,
1456 temp1.texture(), temp2.texture(),
1457 GrSamplerState::kErode_Filter,
1458 radius);
1459 texture->ref();
1460 }
1461 return texture;
1462}
1463
reed@google.comac10a2d2010-12-22 21:39:39 +00001464void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1465 int left, int top, const SkPaint& paint) {
1466 CHECK_SHOULD_DRAW(draw);
1467
reed@google.com8926b162012-03-23 15:36:36 +00001468 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001469 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1470 return;
1471 }
1472
reed@google.com76dd2772012-01-05 21:15:07 +00001473 int w = bitmap.width();
1474 int h = bitmap.height();
1475
bsalomon@google.com5782d712011-01-21 21:03:59 +00001476 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001477 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001478 return;
1479 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001480
bsalomon@google.com5782d712011-01-21 21:03:59 +00001481 GrAutoMatrix avm(fContext, GrMatrix::I());
1482
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001483 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001484
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001485 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001486 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001487 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001488 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001489
reed@google.com8926b162012-03-23 15:36:36 +00001490 SkImageFilter* filter = paint.getImageFilter();
1491 if (NULL != filter) {
1492 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
1493 GrRect::MakeWH(w, h));
1494 if (filteredTexture) {
1495 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1496 texture = filteredTexture;
1497 filteredTexture->unref();
1498 }
reed@google.com76dd2772012-01-05 21:15:07 +00001499 }
reed@google.com8926b162012-03-23 15:36:36 +00001500
bsalomon@google.com5782d712011-01-21 21:03:59 +00001501 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001502 GrRect::MakeXYWH(GrIntToScalar(left),
1503 GrIntToScalar(top),
1504 GrIntToScalar(w),
1505 GrIntToScalar(h)),
1506 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1507 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001508}
1509
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001510void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001511 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001512 // clear of the source device must occur before CHECK_SHOULD_DRAW
1513 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1514 if (dev->fNeedClear) {
1515 // TODO: could check here whether we really need to draw at all
1516 dev->clear(0x0);
1517 }
1518
reed@google.comac10a2d2010-12-22 21:39:39 +00001519 CHECK_SHOULD_DRAW(draw);
1520
bsalomon@google.com5782d712011-01-21 21:03:59 +00001521 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001522 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001523 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001524 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001525 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001526
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001527 GrTexture* devTex = grPaint.getTexture(0);
1528 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001529
reed@google.com8926b162012-03-23 15:36:36 +00001530 SkImageFilter* filter = paint.getImageFilter();
1531 if (NULL != filter) {
1532 GrRect rect = GrRect::MakeWH(devTex->width(), devTex->height());
1533 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1534 rect);
1535 if (filteredTexture) {
1536 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1537 devTex = filteredTexture;
1538 filteredTexture->unref();
1539 }
1540 }
1541
bsalomon@google.com5782d712011-01-21 21:03:59 +00001542 const SkBitmap& bm = dev->accessBitmap(false);
1543 int w = bm.width();
1544 int h = bm.height();
1545
1546 GrAutoMatrix avm(fContext, GrMatrix::I());
1547
bsalomon@google.com97912912011-12-06 16:30:36 +00001548 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001549
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001550 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1551 GrIntToScalar(y),
1552 GrIntToScalar(w),
1553 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001554
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001555 // The device being drawn may not fill up its texture (saveLayer uses
1556 // the approximate ).
1557 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1558 GR_Scalar1 * h / devTex->height());
1559
1560 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001561}
1562
reed@google.com8926b162012-03-23 15:36:36 +00001563bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001564 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001565 SkISize radius;
1566 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001567 return false;
1568 }
reed@google.com8926b162012-03-23 15:36:36 +00001569 return true;
1570}
1571
1572bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1573 const SkMatrix& ctm,
1574 SkBitmap* result, SkIPoint* offset) {
1575 // want explicitly our impl, so guard against a subclass of us overriding it
1576 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001577 return false;
1578 }
reed@google.com8926b162012-03-23 15:36:36 +00001579
1580 SkAutoLockPixels alp(src, !src.getTexture());
1581 if (!src.getTexture() && !src.readyToDraw()) {
1582 return false;
1583 }
1584
1585 GrPaint paint;
1586 paint.reset();
1587
1588 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1589
1590 GrTexture* texture;
1591 SkAutoCachedTexture act(this, src, sampler, &texture);
1592
1593 result->setConfig(src.config(), src.width(), src.height());
1594 GrRect rect = GrRect::MakeWH(src.width(), src.height());
1595 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1596 if (resultTexture) {
1597 result->setPixelRef(new SkGrTexturePixelRef(resultTexture))->unref();
1598 resultTexture->unref();
1599 }
reed@google.com76dd2772012-01-05 21:15:07 +00001600 return true;
1601}
1602
reed@google.comac10a2d2010-12-22 21:39:39 +00001603///////////////////////////////////////////////////////////////////////////////
1604
1605// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001606static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1607 kTriangles_PrimitiveType,
1608 kTriangleStrip_PrimitiveType,
1609 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001610};
1611
1612void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1613 int vertexCount, const SkPoint vertices[],
1614 const SkPoint texs[], const SkColor colors[],
1615 SkXfermode* xmode,
1616 const uint16_t indices[], int indexCount,
1617 const SkPaint& paint) {
1618 CHECK_SHOULD_DRAW(draw);
1619
bsalomon@google.com5782d712011-01-21 21:03:59 +00001620 GrPaint grPaint;
1621 SkAutoCachedTexture act;
1622 // we ignore the shader if texs is null.
1623 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001624 if (!skPaint2GrPaintNoShader(paint,
1625 false,
1626 NULL == colors,
1627 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001628 return;
1629 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001630 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001631 if (!skPaint2GrPaintShader(this,
1632 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001633 NULL == colors,
1634 &act,
1635 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001636 return;
1637 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001638 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001639
1640 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001641 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001642 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1643#if 0
1644 return
1645#endif
1646 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001647 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001648
bsalomon@google.com498776a2011-08-16 19:20:44 +00001649 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1650 if (NULL != colors) {
1651 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001652 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001653 for (int i = 0; i < vertexCount; ++i) {
1654 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1655 }
1656 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001657 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001658 fContext->drawVertices(grPaint,
1659 gVertexMode2PrimitiveType[vmode],
1660 vertexCount,
1661 (GrPoint*) vertices,
1662 (GrPoint*) texs,
1663 colors,
1664 indices,
1665 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001666}
1667
1668///////////////////////////////////////////////////////////////////////////////
1669
1670static void GlyphCacheAuxProc(void* data) {
1671 delete (GrFontScaler*)data;
1672}
1673
1674static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1675 void* auxData;
1676 GrFontScaler* scaler = NULL;
1677 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1678 scaler = (GrFontScaler*)auxData;
1679 }
1680 if (NULL == scaler) {
1681 scaler = new SkGrFontScaler(cache);
1682 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1683 }
1684 return scaler;
1685}
1686
1687static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1688 SkFixed fx, SkFixed fy,
1689 const SkGlyph& glyph) {
1690 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1691
bungeman@google.com15865a72012-01-11 16:28:04 +00001692 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001693
1694 if (NULL == procs->fFontScaler) {
1695 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1696 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001697
bungeman@google.com15865a72012-01-11 16:28:04 +00001698 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1699 glyph.getSubXFixed(),
1700 glyph.getSubYFixed()),
1701 SkFixedFloorToFixed(fx),
1702 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001703 procs->fFontScaler);
1704}
1705
bsalomon@google.com5782d712011-01-21 21:03:59 +00001706SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001707
1708 // deferred allocation
1709 if (NULL == fDrawProcs) {
1710 fDrawProcs = new GrSkDrawProcs;
1711 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1712 fDrawProcs->fContext = fContext;
1713 }
1714
1715 // init our (and GL's) state
1716 fDrawProcs->fTextContext = context;
1717 fDrawProcs->fFontScaler = NULL;
1718 return fDrawProcs;
1719}
1720
1721void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1722 size_t byteLength, SkScalar x, SkScalar y,
1723 const SkPaint& paint) {
1724 CHECK_SHOULD_DRAW(draw);
1725
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001726 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001727 // this guy will just call our drawPath()
1728 draw.drawText((const char*)text, byteLength, x, y, paint);
1729 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001730 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001731
1732 GrPaint grPaint;
1733 SkAutoCachedTexture act;
1734
bsalomon@google.com84405e02012-03-05 19:57:21 +00001735 if (!skPaint2GrPaintShader(this,
1736 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001737 true,
1738 &act,
1739 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001740 return;
1741 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001742 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1743 grPaint, draw.fExtMatrix);
1744 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001745 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1746 }
1747}
1748
1749void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1750 size_t byteLength, const SkScalar pos[],
1751 SkScalar constY, int scalarsPerPos,
1752 const SkPaint& paint) {
1753 CHECK_SHOULD_DRAW(draw);
1754
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001755 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 // this guy will just call our drawPath()
1757 draw.drawPosText((const char*)text, byteLength, pos, constY,
1758 scalarsPerPos, paint);
1759 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001760 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001761
1762 GrPaint grPaint;
1763 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001764 if (!skPaint2GrPaintShader(this,
1765 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001766 true,
1767 &act,
1768 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001769 return;
1770 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001771 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1772 grPaint, draw.fExtMatrix);
1773 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001774 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1775 scalarsPerPos, paint);
1776 }
1777}
1778
1779void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1780 size_t len, const SkPath& path,
1781 const SkMatrix* m, const SkPaint& paint) {
1782 CHECK_SHOULD_DRAW(draw);
1783
1784 SkASSERT(draw.fDevice == this);
1785 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1786}
1787
1788///////////////////////////////////////////////////////////////////////////////
1789
reed@google.comf67e4cf2011-03-15 20:56:58 +00001790bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1791 if (!paint.isLCDRenderText()) {
1792 // we're cool with the paint as is
1793 return false;
1794 }
1795
1796 if (paint.getShader() ||
1797 paint.getXfermode() || // unless its srcover
1798 paint.getMaskFilter() ||
1799 paint.getRasterizer() ||
1800 paint.getColorFilter() ||
1801 paint.getPathEffect() ||
1802 paint.isFakeBoldText() ||
1803 paint.getStyle() != SkPaint::kFill_Style) {
1804 // turn off lcd
1805 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1806 flags->fHinting = paint.getHinting();
1807 return true;
1808 }
1809 // we're cool with the paint as is
1810 return false;
1811}
1812
reed@google.com75d939b2011-12-07 15:07:23 +00001813void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001814 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001815 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001816}
1817
reed@google.comf67e4cf2011-03-15 20:56:58 +00001818///////////////////////////////////////////////////////////////////////////////
1819
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001820SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1821 const SkBitmap& bitmap,
1822 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001823 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001824 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001825
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001826 if (!bitmap.isVolatile()) {
1827 GrContext::TextureKey key = bitmap.getGenerationID();
1828 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001829
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001830 entry = ctx->findAndLockTexture(key, bitmap.width(),
1831 bitmap.height(), sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001832 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001833 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1834 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001835 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001836 } else {
1837 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY,
1838 sampler, bitmap);
1839 }
1840 if (NULL == entry.texture()) {
1841 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1842 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001843 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001844 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001845}
1846
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001847void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1848 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001849}
1850
bsalomon@google.comfb309512011-11-30 14:13:48 +00001851bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1852 const GrSamplerState& sampler) const {
1853 GrContext::TextureKey key = bitmap.getGenerationID();
1854 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
1855 return this->context()->isTextureInCache(key, bitmap.width(),
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001856 bitmap.height(), &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001857
1858}
1859
1860
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001861SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1862 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001863 bool isOpaque,
1864 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001865 GrTextureDesc desc;
1866 desc.fConfig = fRenderTarget->config();
1867 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1868 desc.fWidth = width;
1869 desc.fHeight = height;
1870 desc.fSampleCnt = fRenderTarget->numSamples();
1871
1872 GrContext::TextureCacheEntry cacheEntry;
1873 GrTexture* texture;
1874 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001875 // Skia's convention is to only clear a device if it is non-opaque.
1876 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001877
1878#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1879 // layers are never draw in repeat modes, so we can request an approx
1880 // match and ignore any padding.
1881 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1882 GrContext::kApprox_ScratchTexMatch :
1883 GrContext::kExact_ScratchTexMatch;
1884 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1885 texture = cacheEntry.texture();
1886#else
1887 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1888 texture = tunref.get();
1889#endif
1890 if (texture) {
1891 return SkNEW_ARGS(SkGpuDevice,(fContext,
1892 texture,
1893 cacheEntry,
1894 needClear));
1895 } else {
1896 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1897 width, height);
1898 return NULL;
1899 }
1900}
1901
1902SkGpuDevice::SkGpuDevice(GrContext* context,
1903 GrTexture* texture,
1904 TexCache cacheEntry,
1905 bool needClear)
1906 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1907 GrAssert(texture && texture->asRenderTarget());
1908 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1909 this->initFromRenderTarget(context, texture->asRenderTarget());
1910 fCache = cacheEntry;
1911 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001912}
1913
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001914GrTextContext* SkGpuDevice::getTextContext() {
1915 if (NULL == fTextContext) {
1916 fTextContext = new GrDefaultTextContext();
1917 }
1918 return fTextContext;
1919}