blob: 2e82ad9fcd0c3113b3c315abc106618db0005e65 [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
25#define CACHE_LAYER_TEXTURES 1
26
27#if 0
28 extern bool (*gShouldDrawProc)();
29 #define CHECK_SHOULD_DRAW(draw) \
30 do { \
31 if (gShouldDrawProc && !gShouldDrawProc()) return; \
32 this->prepareRenderTarget(draw); \
33 } while (0)
34#else
35 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw)
36#endif
37
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000038// we use the same texture slot on GrPaint for bitmaps and shaders
39// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
40enum {
41 kBitmapTextureIdx = 0,
42 kShaderTextureIdx = 0
43};
44
reed@google.comcde92112011-07-06 20:00:52 +000045
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000046#define MAX_BLUR_SIGMA 4.0f
47// FIXME: This value comes from from SkBlurMaskFilter.cpp.
48// Should probably be put in a common header someplace.
49#define MAX_BLUR_RADIUS SkIntToScalar(128)
50// This constant approximates the scaling done in the software path's
51// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
52// IMHO, it actually should be 1: we blur "less" than we should do
53// according to the CSS and canvas specs, simply because Safari does the same.
54// Firefox used to do the same too, until 4.0 where they fixed it. So at some
55// point we should probably get rid of these scaling constants and rebaseline
56// all the blur tests.
57#define BLUR_SIGMA_SCALE 0.6f
reed@google.comac10a2d2010-12-22 21:39:39 +000058///////////////////////////////////////////////////////////////////////////////
59
bsalomon@google.com84405e02012-03-05 19:57:21 +000060class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
61public:
62 SkAutoCachedTexture() { }
63 SkAutoCachedTexture(SkGpuDevice* device,
64 const SkBitmap& bitmap,
65 const GrSamplerState* sampler,
66 GrTexture** texture) {
67 GrAssert(texture);
68 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000069 }
reed@google.comac10a2d2010-12-22 21:39:39 +000070
bsalomon@google.com84405e02012-03-05 19:57:21 +000071 ~SkAutoCachedTexture() {
72 if (fTex.texture()) {
73 fDevice->unlockCachedTexture(fTex);
74 }
reed@google.comac10a2d2010-12-22 21:39:39 +000075 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000076
77 GrTexture* set(SkGpuDevice* device,
78 const SkBitmap& bitmap,
79 const GrSamplerState* sampler) {
80 if (fTex.texture()) {
81 fDevice->unlockCachedTexture(fTex);
82 }
83 fDevice = device;
84 GrTexture* texture = (GrTexture*)bitmap.getTexture();
85 if (texture) {
86 // return the native texture
87 fTex.reset();
88 } else {
89 // look it up in our cache
90 fTex = device->lockCachedTexture(bitmap, sampler);
91 texture = fTex.texture();
92 }
93 return texture;
94 }
95
96private:
97 SkGpuDevice* fDevice;
98 GrContext::TextureCacheEntry fTex;
99};
reed@google.comac10a2d2010-12-22 21:39:39 +0000100
101///////////////////////////////////////////////////////////////////////////////
102
103bool gDoTraceDraw;
104
105struct GrSkDrawProcs : public SkDrawProcs {
106public:
107 GrContext* fContext;
108 GrTextContext* fTextContext;
109 GrFontScaler* fFontScaler; // cached in the skia glyphcache
110};
111
112///////////////////////////////////////////////////////////////////////////////
113
reed@google.comaf951c92011-06-16 19:10:39 +0000114static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
115 switch (config) {
116 case kAlpha_8_GrPixelConfig:
117 *isOpaque = false;
118 return SkBitmap::kA8_Config;
119 case kRGB_565_GrPixelConfig:
120 *isOpaque = true;
121 return SkBitmap::kRGB_565_Config;
122 case kRGBA_4444_GrPixelConfig:
123 *isOpaque = false;
124 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000125 case kSkia8888_PM_GrPixelConfig:
126 // we don't currently have a way of knowing whether
127 // a 8888 is opaque based on the config.
128 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000129 return SkBitmap::kARGB_8888_Config;
130 default:
131 *isOpaque = false;
132 return SkBitmap::kNo_Config;
133 }
134}
reed@google.comac10a2d2010-12-22 21:39:39 +0000135
reed@google.comaf951c92011-06-16 19:10:39 +0000136static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000137 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000138
139 bool isOpaque;
140 SkBitmap bitmap;
141 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
142 renderTarget->width(), renderTarget->height());
143 bitmap.setIsOpaque(isOpaque);
144 return bitmap;
145}
146
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000147SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
148: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
149 this->initFromRenderTarget(context, texture->asRenderTarget());
150}
151
reed@google.comaf951c92011-06-16 19:10:39 +0000152SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
153: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000154 this->initFromRenderTarget(context, renderTarget);
155}
156
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000157void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000158 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000159 fNeedPrepareRenderTarget = false;
160 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000161
reed@google.comaf951c92011-06-16 19:10:39 +0000162 fContext = context;
163 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000164
reed@google.comaf951c92011-06-16 19:10:39 +0000165 fTexture = NULL;
166 fRenderTarget = NULL;
167 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000168
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000169 GrAssert(NULL != renderTarget);
170 fRenderTarget = renderTarget;
171 fRenderTarget->ref();
172 // if this RT is also a texture, hold a ref on it
173 fTexture = fRenderTarget->asTexture();
174 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000175
176 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
177 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
178 // the device via accessBitmap. This external ref may outlive the device.
179 // Since textures own their render targets (but not vice-versa) we
180 // are ensuring that both objects will live as long as the pixel ref.
181 SkPixelRef* pr;
182 if (fTexture) {
183 pr = new SkGrTexturePixelRef(fTexture);
184 } else {
185 pr = new SkGrRenderTargetPixelRef(fRenderTarget);
186 }
reed@google.comaf951c92011-06-16 19:10:39 +0000187 this->setPixelRef(pr, 0)->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000188
189 fTextContext = NULL;
reed@google.comaf951c92011-06-16 19:10:39 +0000190}
191
192SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000193 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000194: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000195 fNeedPrepareRenderTarget = false;
196 fDrawProcs = NULL;
197
reed@google.com7b201d22011-01-11 18:59:23 +0000198 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000199 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000200
reed@google.comac10a2d2010-12-22 21:39:39 +0000201 fTexture = NULL;
202 fRenderTarget = NULL;
203 fNeedClear = false;
204
reed@google.comaf951c92011-06-16 19:10:39 +0000205 if (config != SkBitmap::kRGB_565_Config) {
206 config = SkBitmap::kARGB_8888_Config;
207 }
208 SkBitmap bm;
209 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000210
211#if CACHE_LAYER_TEXTURES
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000212 TexType type = (kSaveLayer_Usage == usage) ?
bsalomon@google.come97f0852011-06-17 13:10:25 +0000213 kSaveLayerDeviceRenderTarget_TexType :
214 kDeviceRenderTarget_TexType;
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000215 fCache = this->lockCachedTexture(bm, NULL, type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000216 fTexture = fCache.texture();
217 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000218 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000219 // hold a ref directly on fTexture (even though fCache has one) to match
220 // other constructor paths. Simplifies cleanup.
221 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000222 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000223#else
reed@google.comaf951c92011-06-16 19:10:39 +0000224 const GrTextureDesc desc = {
225 kRenderTarget_GrTextureFlagBit,
reed@google.comaf951c92011-06-16 19:10:39 +0000226 width,
227 height,
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000228 SkGr::Bitmap2PixelConfig(bm),
229 {0} // samples
reed@google.comaf951c92011-06-16 19:10:39 +0000230 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
reed@google.comaf951c92011-06-16 19:10:39 +0000232 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000234 if (NULL != fTexture) {
235 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000236 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000237
reed@google.comaf951c92011-06-16 19:10:39 +0000238 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
reed@google.comaf951c92011-06-16 19:10:39 +0000240 // we defer the actual clear until our gainFocus()
241 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
reed@google.comaf951c92011-06-16 19:10:39 +0000243 // wrap the bitmap with a pixelref to expose our texture
244 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000245 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000246 } else {
247 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
248 width, height);
249 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000250 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000251
252 fTextContext = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000253}
254
255SkGpuDevice::~SkGpuDevice() {
256 if (fDrawProcs) {
257 delete fDrawProcs;
258 }
259
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000260 SkSafeUnref(fTexture);
261 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000262 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000263 GrAssert(NULL != fTexture);
264 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000265 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000266 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000267 fContext->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000268
269 if (NULL != fTextContext) {
270 fTextContext->unref();
271 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000272}
273
reed@google.comac10a2d2010-12-22 21:39:39 +0000274///////////////////////////////////////////////////////////////////////////////
275
276void SkGpuDevice::makeRenderTargetCurrent() {
277 fContext->setRenderTarget(fRenderTarget);
278 fContext->flush(true);
279 fNeedPrepareRenderTarget = true;
280}
281
282///////////////////////////////////////////////////////////////////////////////
283
bsalomon@google.comc4364992011-11-07 15:54:49 +0000284namespace {
285GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
286 switch (config8888) {
287 case SkCanvas::kNative_Premul_Config8888:
288 return kSkia8888_PM_GrPixelConfig;
289 case SkCanvas::kNative_Unpremul_Config8888:
290 return kSkia8888_UPM_GrPixelConfig;
291 case SkCanvas::kBGRA_Premul_Config8888:
292 return kBGRA_8888_PM_GrPixelConfig;
293 case SkCanvas::kBGRA_Unpremul_Config8888:
294 return kBGRA_8888_UPM_GrPixelConfig;
295 case SkCanvas::kRGBA_Premul_Config8888:
296 return kRGBA_8888_PM_GrPixelConfig;
297 case SkCanvas::kRGBA_Unpremul_Config8888:
298 return kRGBA_8888_UPM_GrPixelConfig;
299 default:
300 GrCrash("Unexpected Config8888.");
301 return kSkia8888_PM_GrPixelConfig;
302 }
303}
304}
305
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000306bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
307 int x, int y,
308 SkCanvas::Config8888 config8888) {
bsalomon@google.com910267d2011-11-02 20:06:25 +0000309 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
310 SkASSERT(!bitmap.isNull());
311 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000312
bsalomon@google.com910267d2011-11-02 20:06:25 +0000313 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000314 GrPixelConfig config;
315 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000316 return fContext->readRenderTargetPixels(fRenderTarget,
317 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000318 bitmap.width(),
319 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000320 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000321 bitmap.getPixels(),
322 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000323}
324
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000325void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
326 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000327 SkAutoLockPixels alp(bitmap);
328 if (!bitmap.readyToDraw()) {
329 return;
330 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000331
332 GrPixelConfig config;
333 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
334 config = config8888_to_gr_config(config8888);
335 } else {
336 config= SkGr::BitmapConfig2PixelConfig(bitmap.config(),
337 bitmap.isOpaque());
338 }
339
bsalomon@google.com6f379512011-11-16 20:36:03 +0000340 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
341 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000342}
343
344///////////////////////////////////////////////////////////////////////////////
345
346static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000347 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000348 const SkRegion& clipRegion,
349 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000350 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000351
352 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000353 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000354 const SkIRect& skBounds = clipRegion.getBounds();
355 GrRect bounds;
356 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
357 GrIntToScalar(skBounds.fTop),
358 GrIntToScalar(skBounds.fRight),
359 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000360 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
361 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000362 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000363}
364
365// call this ever each draw call, to ensure that the context reflects our state,
366// and not the state from some other canvas/device
367void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
368 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000369 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000370
371 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000372 SkASSERT(draw.fClipStack);
373 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000374 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000375 fNeedPrepareRenderTarget = false;
376 }
377}
378
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000379void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
380 const SkClipStack& clipStack) {
381 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
382 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000383 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000384}
385
386void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000387 const SkRegion& clip, const SkClipStack& clipStack) {
388
reed@google.comac10a2d2010-12-22 21:39:39 +0000389 fContext->setRenderTarget(fRenderTarget);
390
bsalomon@google.comd302f142011-03-03 13:54:13 +0000391 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000392
reed@google.com6f8f2922011-03-04 22:27:10 +0000393 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000394
395 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000396 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000397 fNeedClear = false;
398 }
399}
400
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000401SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
402 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000403}
404
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000405bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000406 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000407 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000408 return true;
409 }
410 return false;
411}
412
413///////////////////////////////////////////////////////////////////////////////
414
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000415SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
416SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
417SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
418SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
419SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
420 shader_type_mismatch);
421SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000422
bsalomon@google.com5782d712011-01-21 21:03:59 +0000423static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
424 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
425 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
426 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
427 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
428 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
429};
430
bsalomon@google.com84405e02012-03-05 19:57:21 +0000431namespace {
432
433// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
434// justAlpha indicates that skPaint's alpha should be used rather than the color
435// Callers may subsequently modify the GrPaint. Setting constantColor indicates
436// that the final paint will draw the same color at every pixel. This allows
437// an optimization where the the color filter can be applied to the skPaint's
438// color once while converting to GrPain and then ignored.
439inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
440 bool justAlpha,
441 bool constantColor,
442 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000443
444 grPaint->fDither = skPaint.isDither();
445 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000446 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000447
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000448 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
449 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000450
451 SkXfermode* mode = skPaint.getXfermode();
452 if (mode) {
453 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000454 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000455#if 0
456 return false;
457#endif
458 }
459 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000460 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
461 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
462
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463 if (justAlpha) {
464 uint8_t alpha = skPaint.getAlpha();
465 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000466 // justAlpha is currently set to true only if there is a texture,
467 // so constantColor should not also be true.
468 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000469 } else {
470 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000471 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000472 }
Scroggo97c88c22011-05-11 14:05:25 +0000473 SkColorFilter* colorFilter = skPaint.getColorFilter();
474 SkColor color;
475 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000476 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000477 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000478 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000479 if (!constantColor) {
480 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
481 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000482 } else {
483 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
484 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000485 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000486 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000487 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
488 grPaint->fColorMatrixEnabled = true;
489 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
490 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
491 } else {
492 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000493 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000494 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000495}
496
bsalomon@google.com84405e02012-03-05 19:57:21 +0000497// This function is similar to skPaint2GrPaintNoShader but also converts
498// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
499// be used is set on grPaint and returned in param act. constantColor has the
500// same meaning as in skPaint2GrPaintNoShader.
501inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
502 const SkPaint& skPaint,
503 const SkMatrix& ctm,
504 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.com31a58402011-04-27 21:00:02 +0000595 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000596}
597
reed@google.comac10a2d2010-12-22 21:39:39 +0000598void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
599 CHECK_SHOULD_DRAW(draw);
600
bsalomon@google.com5782d712011-01-21 21:03:59 +0000601 GrPaint grPaint;
602 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000603 if (!skPaint2GrPaintShader(this,
604 paint,
605 *draw.fMatrix,
606 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,
641 *draw.fMatrix,
642 true,
643 &act,
644 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000645 return;
646 }
647
bsalomon@google.com5782d712011-01-21 21:03:59 +0000648 fContext->drawVertices(grPaint,
649 gPointMode2PrimtiveType[mode],
650 count,
651 (GrPoint*)pts,
652 NULL,
653 NULL,
654 NULL,
655 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000656}
657
reed@google.comc9aa5872011-04-05 21:05:37 +0000658///////////////////////////////////////////////////////////////////////////////
659
reed@google.comac10a2d2010-12-22 21:39:39 +0000660void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
661 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000662 CHECK_SHOULD_DRAW(draw);
663
bungeman@google.com79bd8772011-07-18 15:34:08 +0000664 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000665 SkScalar width = paint.getStrokeWidth();
666
667 /*
668 We have special code for hairline strokes, miter-strokes, and fills.
669 Anything else we just call our path code.
670 */
671 bool usePath = doStroke && width > 0 &&
672 paint.getStrokeJoin() != SkPaint::kMiter_Join;
673 // another reason we might need to call drawPath...
674 if (paint.getMaskFilter()) {
675 usePath = true;
676 }
reed@google.com67db6642011-05-26 11:46:35 +0000677 // until we aa rotated rects...
678 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
679 usePath = true;
680 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000681 // small miter limit means right angles show bevel...
682 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
683 paint.getStrokeMiter() < SK_ScalarSqrt2)
684 {
685 usePath = true;
686 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000687 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000688 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
689 usePath = true;
690 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000691
692 if (usePath) {
693 SkPath path;
694 path.addRect(rect);
695 this->drawPath(draw, path, paint, NULL, true);
696 return;
697 }
698
699 GrPaint grPaint;
700 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000701 if (!skPaint2GrPaintShader(this,
702 paint,
703 *draw.fMatrix,
704 true,
705 &act,
706 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000707 return;
708 }
reed@google.com20efde72011-05-09 17:00:02 +0000709 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000710}
711
reed@google.com69302852011-02-16 18:08:07 +0000712#include "SkMaskFilter.h"
713#include "SkBounder.h"
714
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000715static GrPathFill skToGrFillType(SkPath::FillType fillType) {
716 switch (fillType) {
717 case SkPath::kWinding_FillType:
718 return kWinding_PathFill;
719 case SkPath::kEvenOdd_FillType:
720 return kEvenOdd_PathFill;
721 case SkPath::kInverseWinding_FillType:
722 return kInverseWinding_PathFill;
723 case SkPath::kInverseEvenOdd_FillType:
724 return kInverseEvenOdd_PathFill;
725 default:
726 SkDebugf("Unsupported path fill type\n");
727 return kHairLine_PathFill;
728 }
729}
730
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000731static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
732 SkMaskFilter* filter, const SkMatrix& matrix,
733 const SkRegion& clip, SkBounder* bounder,
734 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000735#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000736 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000737#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000738 SkMaskFilter::BlurInfo info;
739 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000740 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000741 return false;
742 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000743 SkScalar radius = info.fIgnoreTransform ? info.fRadius
744 : matrix.mapRadius(info.fRadius);
745 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000746 if (radius <= 0) {
747 return false;
748 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000749 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000750 float sigma3 = sigma * 3.0f;
751
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000752 SkRect srcRect = path.getBounds();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000753 SkRect clipRect;
754 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000755
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000756 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
757 srcRect.inset(-sigma3, -sigma3);
758 clipRect.inset(-sigma3, -sigma3);
759 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000760 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000761 SkIRect finalIRect;
762 finalRect.roundOut(&finalIRect);
763 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000764 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000765 }
766 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000767 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000768 }
769 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000770 srcRect.offset(offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000771 const GrTextureDesc desc = {
772 kRenderTarget_GrTextureFlagBit,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000773 srcRect.width(),
774 srcRect.height(),
775 // We actually only need A8, but it often isn't supported as a
776 // render target
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000777 kRGBA_8888_PM_GrPixelConfig,
778 {0} // samples
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000779 };
780
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000781 GrAutoScratchTexture pathEntry(context, desc);
782 GrTexture* pathTexture = pathEntry.texture();
783 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000784 return false;
785 }
786 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000787 // Once this code moves into GrContext, this should be changed to use
788 // an AutoClipRestore.
789 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000790 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000791 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000792 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000793 GrPaint tempPaint;
794 tempPaint.reset();
795
796 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000797 tempPaint.fAntiAlias = grp->fAntiAlias;
798 if (tempPaint.fAntiAlias) {
799 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
800 // blend coeff of zero requires dual source blending support in order
801 // to properly blend partially covered pixels. This means the AA
802 // code path may not be taken. So we use a dst blend coeff of ISA. We
803 // could special case AA draws to a dst surface with known alpha=0 to
804 // use a zero dst coeff when dual source blending isn't available.
805 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
806 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
807 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000808 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000810
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000811 GrAutoScratchTexture temp1, temp2;
812 // If we're doing a normal blur, we can clobber the pathTexture in the
813 // gaussianBlur. Otherwise, we need to save it for later compositing.
814 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000815 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
816 &temp1,
817 isNormalBlur ? NULL : &temp2,
818 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000819
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000820 if (!isNormalBlur) {
821 GrPaint paint;
822 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000823 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000824 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
825 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000826 // Blend pathTexture over blurTexture.
827 context->setRenderTarget(blurTexture->asRenderTarget());
828 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000829 if (SkMaskFilter::kInner_BlurType == blurType) {
830 // inner: dst = dst * src
831 paint.fSrcBlendCoeff = kDC_BlendCoeff;
832 paint.fDstBlendCoeff = kZero_BlendCoeff;
833 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
834 // solid: dst = src + dst - src * dst
835 // = (1 - dst) * src + 1 * dst
836 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
837 paint.fDstBlendCoeff = kOne_BlendCoeff;
838 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
839 // outer: dst = dst * (1 - src)
840 // = 0 * src + (1 - src) * dst
841 paint.fSrcBlendCoeff = kZero_BlendCoeff;
842 paint.fDstBlendCoeff = kISC_BlendCoeff;
843 }
844 context->drawRect(paint, srcRect);
845 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000846 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000847 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000848
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000849 if (grp->hasTextureOrMask()) {
850 GrMatrix inverse;
851 if (!matrix.invert(&inverse)) {
852 return false;
853 }
854 grp->preConcatActiveSamplerMatrices(inverse);
855 }
856
857 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
858 // we assume the last mask index is available for use
859 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000860 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000861 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000862
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000863 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
864 -finalRect.fTop);
865 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
866 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000867 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000868 return true;
869}
870
reed@google.com69302852011-02-16 18:08:07 +0000871static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
872 SkMaskFilter* filter, const SkMatrix& matrix,
873 const SkRegion& clip, SkBounder* bounder,
874 GrPaint* grp) {
875 SkMask srcM, dstM;
876
877 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
878 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
879 return false;
880 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000881 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000882
883 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
884 return false;
885 }
886 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000887 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000888
889 if (clip.quickReject(dstM.fBounds)) {
890 return false;
891 }
892 if (bounder && !bounder->doIRect(dstM.fBounds)) {
893 return false;
894 }
895
896 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
897 // the current clip (and identity matrix) and grpaint settings
898
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000899 // used to compute inverse view, if necessary
900 GrMatrix ivm = context->getMatrix();
901
reed@google.com0c219b62011-02-16 21:31:18 +0000902 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000903
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000904 const GrTextureDesc desc = {
905 kNone_GrTextureFlags,
reed@google.com69302852011-02-16 18:08:07 +0000906 dstM.fBounds.width(),
907 dstM.fBounds.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000908 kAlpha_8_GrPixelConfig,
909 {0}, // samples
reed@google.com69302852011-02-16 18:08:07 +0000910 };
911
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000912 GrAutoScratchTexture ast(context, desc);
913 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000914
reed@google.com69302852011-02-16 18:08:07 +0000915 if (NULL == texture) {
916 return false;
917 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000918 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000919 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000920
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000921 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
922 grp->preConcatActiveSamplerMatrices(ivm);
923 }
924
925 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
926 // we assume the last mask index is available for use
927 GrAssert(NULL == grp->getMask(MASK_IDX));
928 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000929 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000930
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000931 GrRect d;
932 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000933 GrIntToScalar(dstM.fBounds.fTop),
934 GrIntToScalar(dstM.fBounds.fRight),
935 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000936
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000937 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
938 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
939 -dstM.fBounds.fTop*SK_Scalar1);
940 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000941 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000942 return true;
943}
reed@google.com69302852011-02-16 18:08:07 +0000944
reed@google.com0c219b62011-02-16 21:31:18 +0000945void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000946 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000947 bool pathIsMutable) {
948 CHECK_SHOULD_DRAW(draw);
949
reed@google.comfe626382011-09-21 13:50:35 +0000950 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000951
bsalomon@google.com5782d712011-01-21 21:03:59 +0000952 GrPaint grPaint;
953 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000954 if (!skPaint2GrPaintShader(this,
955 paint,
956 *draw.fMatrix,
957 true,
958 &act,
959 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000960 return;
961 }
962
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000963 // can we cheat, and threat a thin stroke as a hairline w/ coverage
964 // if we can, we draw lots faster (raster device does this same test)
965 SkScalar hairlineCoverage;
966 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
967 doFill = false;
968 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
969 grPaint.fCoverage);
970 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000971
reed@google.comfe626382011-09-21 13:50:35 +0000972 // If we have a prematrix, apply it to the path, optimizing for the case
973 // where the original path can in fact be modified in place (even though
974 // its parameter type is const).
975 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
976 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000977
978 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +0000979 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +0000980
reed@google.come3445642011-02-16 23:20:39 +0000981 if (!pathIsMutable) {
982 result = &tmpPath;
983 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000984 }
reed@google.come3445642011-02-16 23:20:39 +0000985 // should I push prePathMatrix on our MV stack temporarily, instead
986 // of applying it here? See SkDraw.cpp
987 pathPtr->transform(*prePathMatrix, result);
988 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +0000989 }
reed@google.com0c219b62011-02-16 21:31:18 +0000990 // at this point we're done with prePathMatrix
991 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +0000992
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +0000993 if (paint.getPathEffect() ||
994 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +0000995 // it is safe to use tmpPath here, even if we already used it for the
996 // prepathmatrix, since getFillPath can take the same object for its
997 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000998 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +0000999 pathPtr = &tmpPath;
1000 }
1001
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001002 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001003 // avoid possibly allocating a new path in transform if we can
1004 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1005
1006 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001007 pathPtr->transform(*draw.fMatrix, devPathPtr);
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001008 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001009 *draw.fMatrix, *draw.fClip, draw.fBounder,
1010 &grPaint)) {
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001011 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001012 *draw.fMatrix, *draw.fClip, draw.fBounder,
1013 &grPaint);
1014 }
reed@google.com69302852011-02-16 18:08:07 +00001015 return;
1016 }
reed@google.com69302852011-02-16 18:08:07 +00001017
bsalomon@google.comffca4002011-02-22 20:34:01 +00001018 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001019
reed@google.com0c219b62011-02-16 21:31:18 +00001020 if (doFill) {
1021 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001022 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001023 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001024 break;
1025 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001026 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001027 break;
1028 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001029 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001030 break;
1031 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001032 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001033 break;
1034 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001035 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001036 return;
1037 }
1038 }
1039
reed@google.com07f3ee12011-05-16 17:21:57 +00001040 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001041}
1042
bsalomon@google.comfb309512011-11-30 14:13:48 +00001043namespace {
1044
1045inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1046 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1047 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1048 return tilesX * tilesY;
1049}
1050
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001051inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001052 const SkIRect* srcRectPtr,
1053 int maxTextureSize) {
1054 static const int kSmallTileSize = 1 << 10;
1055 if (maxTextureSize <= kSmallTileSize) {
1056 return maxTextureSize;
1057 }
1058
1059 size_t maxTexTotalTileSize;
1060 size_t smallTotalTileSize;
1061
1062 if (NULL == srcRectPtr) {
1063 int w = bitmap.width();
1064 int h = bitmap.height();
1065 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1066 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1067 } else {
1068 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1069 srcRectPtr->fTop,
1070 srcRectPtr->fRight,
1071 srcRectPtr->fBottom,
1072 maxTextureSize);
1073 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1074 srcRectPtr->fTop,
1075 srcRectPtr->fRight,
1076 srcRectPtr->fBottom,
1077 kSmallTileSize);
1078 }
1079 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1080 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1081
1082 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1083 return kSmallTileSize;
1084 } else {
1085 return maxTextureSize;
1086 }
1087}
1088}
1089
1090bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1091 const GrSamplerState& sampler,
1092 const SkIRect* srcRectPtr,
1093 int* tileSize) const {
1094 SkASSERT(NULL != tileSize);
1095
1096 // if bitmap is explictly texture backed then just use the texture
1097 if (NULL != bitmap.getTexture()) {
1098 return false;
1099 }
1100 // if it's larger than the max texture size, then we have no choice but
1101 // tiling
1102 const int maxTextureSize = fContext->getMaxTextureSize();
1103 if (bitmap.width() > maxTextureSize ||
1104 bitmap.height() > maxTextureSize) {
1105 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1106 return true;
1107 }
1108 // if we are going to have to draw the whole thing, then don't tile
1109 if (NULL == srcRectPtr) {
1110 return false;
1111 }
1112 // if the entire texture is already in our cache then no reason to tile it
1113 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1114 return false;
1115 }
1116
1117 // At this point we know we could do the draw by uploading the entire bitmap
1118 // as a texture. However, if the texture would be large compared to the
1119 // cache size and we don't require most of it for this draw then tile to
1120 // reduce the amount of upload and cache spill.
1121
1122 // assumption here is that sw bitmap size is a good proxy for its size as
1123 // a texture
1124 size_t bmpSize = bitmap.getSize();
1125 size_t cacheSize;
1126 fContext->getTextureCacheLimits(NULL, &cacheSize);
1127 if (bmpSize < cacheSize / 2) {
1128 return false;
1129 }
1130
1131 SkFixed fracUsed =
1132 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1133 (srcRectPtr->height() << 16) / bitmap.height());
1134 if (fracUsed <= SK_FixedHalf) {
1135 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1136 return true;
1137 } else {
1138 return false;
1139 }
1140}
1141
reed@google.comac10a2d2010-12-22 21:39:39 +00001142void SkGpuDevice::drawBitmap(const SkDraw& draw,
1143 const SkBitmap& bitmap,
1144 const SkIRect* srcRectPtr,
1145 const SkMatrix& m,
1146 const SkPaint& paint) {
1147 CHECK_SHOULD_DRAW(draw);
1148
1149 SkIRect srcRect;
1150 if (NULL == srcRectPtr) {
1151 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1152 } else {
1153 srcRect = *srcRectPtr;
1154 }
1155
junov@google.comd935cfb2011-06-27 20:48:23 +00001156 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001157 // Convert the bitmap to a shader so that the rect can be drawn
1158 // through drawRect, which supports mask filters.
1159 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001160 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001161 if (srcRectPtr) {
1162 if (!bitmap.extractSubset(&tmp, srcRect)) {
1163 return; // extraction failed
1164 }
1165 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001166 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001167 }
1168 SkPaint paintWithTexture(paint);
1169 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1170 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001171 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001172 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001173
junov@google.com1d329782011-07-28 20:10:09 +00001174 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001175 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001176 // also affect the behavior of the mask filter.
1177 SkMatrix drawMatrix;
1178 drawMatrix.setConcat(*draw.fMatrix, m);
1179 SkDraw transformedDraw(draw);
1180 transformedDraw.fMatrix = &drawMatrix;
1181
1182 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1183
junov@google.comd935cfb2011-06-27 20:48:23 +00001184 return;
1185 }
1186
bsalomon@google.com5782d712011-01-21 21:03:59 +00001187 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001188 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001189 return;
1190 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001191 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001192 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001193 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001194 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001195 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001196 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001197
bsalomon@google.comfb309512011-11-30 14:13:48 +00001198 int tileSize;
1199 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1200 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001201 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001202 return;
1203 }
1204
1205 // undo the translate done by SkCanvas
1206 int DX = SkMax32(0, srcRect.fLeft);
1207 int DY = SkMax32(0, srcRect.fTop);
1208 // compute clip bounds in local coordinates
1209 SkIRect clipRect;
1210 {
1211 SkRect r;
1212 r.set(draw.fClip->getBounds());
1213 SkMatrix matrix, inverse;
1214 matrix.setConcat(*draw.fMatrix, m);
1215 if (!matrix.invert(&inverse)) {
1216 return;
1217 }
1218 inverse.mapRect(&r);
1219 r.roundOut(&clipRect);
1220 // apply the canvas' translate to our local clip
1221 clipRect.offset(DX, DY);
1222 }
1223
bsalomon@google.comfb309512011-11-30 14:13:48 +00001224 int nx = bitmap.width() / tileSize;
1225 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001226 for (int x = 0; x <= nx; x++) {
1227 for (int y = 0; y <= ny; y++) {
1228 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001229 tileR.set(x * tileSize, y * tileSize,
1230 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001231 if (!SkIRect::Intersects(tileR, clipRect)) {
1232 continue;
1233 }
1234
1235 SkIRect srcR = tileR;
1236 if (!srcR.intersect(srcRect)) {
1237 continue;
1238 }
1239
1240 SkBitmap tmpB;
1241 if (bitmap.extractSubset(&tmpB, tileR)) {
1242 // now offset it to make it "local" to our tmp bitmap
1243 srcR.offset(-tileR.fLeft, -tileR.fTop);
1244
1245 SkMatrix tmpM(m);
1246 {
1247 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1248 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1249 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1250 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001251 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001252 }
1253 }
1254 }
1255}
1256
1257/*
1258 * This is called by drawBitmap(), which has to handle images that may be too
1259 * large to be represented by a single texture.
1260 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001261 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1262 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001263 */
1264void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1265 const SkBitmap& bitmap,
1266 const SkIRect& srcRect,
1267 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001268 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001269 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1270 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001271
reed@google.com9c49bc32011-07-07 13:42:37 +00001272 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001273 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001274 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001275 return;
1276 }
1277
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001278 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001279
1280 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1281 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1282 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001283 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001284
1285 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001286 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001287 if (NULL == texture) {
1288 return;
1289 }
1290
bsalomon@google.com452943d2011-10-31 17:37:14 +00001291 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001292
reed@google.com20efde72011-05-09 17:00:02 +00001293 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1294 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001295 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001296 float wInv = 1.f / bitmap.width();
1297 float hInv = 1.f / bitmap.height();
1298 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1299 SkFloatToScalar(srcRect.fTop * hInv),
1300 SkFloatToScalar(srcRect.fRight * wInv),
1301 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001302
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001303 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001304 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001305 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001306 // If drawing a subrect of the bitmap and filtering is enabled,
1307 // use a constrained texture domain to avoid color bleeding
1308 GrScalar left, top, right, bottom;
1309 if (srcRect.width() > 1) {
1310 GrScalar border = GR_ScalarHalf / bitmap.width();
1311 left = paintRect.left() + border;
1312 right = paintRect.right() - border;
1313 } else {
1314 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1315 }
1316 if (srcRect.height() > 1) {
1317 GrScalar border = GR_ScalarHalf / bitmap.height();
1318 top = paintRect.top() + border;
1319 bottom = paintRect.bottom() - border;
1320 } else {
1321 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1322 }
1323 GrRect textureDomain;
1324 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001325 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001326 }
1327
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001328 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001329}
1330
1331void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1332 int left, int top, const SkPaint& paint) {
1333 CHECK_SHOULD_DRAW(draw);
1334
1335 SkAutoLockPixels alp(bitmap);
1336 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1337 return;
1338 }
1339
reed@google.com76dd2772012-01-05 21:15:07 +00001340 int w = bitmap.width();
1341 int h = bitmap.height();
1342
bsalomon@google.com5782d712011-01-21 21:03:59 +00001343 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001344 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001345 return;
1346 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001347
bsalomon@google.com5782d712011-01-21 21:03:59 +00001348 GrAutoMatrix avm(fContext, GrMatrix::I());
1349
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001350 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001351
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001352 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001353 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001354 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001355
reed@google.com76dd2772012-01-05 21:15:07 +00001356 SkImageFilter* imageFilter = paint.getImageFilter();
1357 SkSize blurSize;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001358 SkISize radius;
reed@google.com76dd2772012-01-05 21:15:07 +00001359 if (NULL != imageFilter && imageFilter->asABlur(&blurSize)) {
1360 GrAutoScratchTexture temp1, temp2;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001361 GrTexture* blurTexture = fContext->gaussianBlur(texture, &temp1, &temp2,
1362 GrRect::MakeWH(w, h),
1363 blurSize.width(),
1364 blurSize.height());
reed@google.com76dd2772012-01-05 21:15:07 +00001365 texture = blurTexture;
1366 grPaint.setTexture(kBitmapTextureIdx, texture);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001367 } else if (NULL != imageFilter && imageFilter->asADilate(&radius)) {
1368 const GrTextureDesc desc = {
1369 kRenderTarget_GrTextureFlagBit,
1370 w,
1371 h,
1372 kRGBA_8888_PM_GrPixelConfig,
1373 {0} // samples
1374 };
1375 GrAutoScratchTexture temp1(fContext, desc), temp2(fContext, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001376 texture = fContext->applyMorphology(texture, GrRect::MakeWH(w, h),
1377 temp1.texture(), temp2.texture(),
1378 GrSamplerState::kDilate_Filter,
1379 radius);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001380 grPaint.setTexture(kBitmapTextureIdx, texture);
1381 } else if (NULL != imageFilter && imageFilter->asAnErode(&radius)) {
1382 const GrTextureDesc desc = {
1383 kRenderTarget_GrTextureFlagBit,
1384 w,
1385 h,
1386 kRGBA_8888_PM_GrPixelConfig,
1387 {0} // samples
1388 };
1389 GrAutoScratchTexture temp1(fContext, desc), temp2(fContext, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001390 texture = fContext->applyMorphology(texture, GrRect::MakeWH(w, h),
1391 temp1.texture(), temp2.texture(),
1392 GrSamplerState::kErode_Filter,
1393 radius);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001394 grPaint.setTexture(kBitmapTextureIdx, texture);
reed@google.com76dd2772012-01-05 21:15:07 +00001395 } else {
1396 grPaint.setTexture(kBitmapTextureIdx, texture);
1397 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001398
bsalomon@google.com5782d712011-01-21 21:03:59 +00001399 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001400 GrRect::MakeXYWH(GrIntToScalar(left),
1401 GrIntToScalar(top),
1402 GrIntToScalar(w),
1403 GrIntToScalar(h)),
1404 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1405 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001406}
1407
1408void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1409 int x, int y, const SkPaint& paint) {
1410 CHECK_SHOULD_DRAW(draw);
1411
bsalomon@google.com5782d712011-01-21 21:03:59 +00001412 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001413 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001414 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001415 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001416 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001417
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001418 GrTexture* devTex = grPaint.getTexture(0);
1419 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001420
1421 const SkBitmap& bm = dev->accessBitmap(false);
1422 int w = bm.width();
1423 int h = bm.height();
1424
1425 GrAutoMatrix avm(fContext, GrMatrix::I());
1426
bsalomon@google.com97912912011-12-06 16:30:36 +00001427 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001428
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001429 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1430 GrIntToScalar(y),
1431 GrIntToScalar(w),
1432 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001433
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001434 // The device being drawn may not fill up its texture (saveLayer uses
1435 // the approximate ).
1436 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1437 GR_Scalar1 * h / devTex->height());
1438
1439 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001440}
1441
reed@google.com76dd2772012-01-05 21:15:07 +00001442bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1443 const SkMatrix& ctm,
1444 SkBitmap* result, SkIPoint* offset) {
1445 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001446 SkISize radius;
1447 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001448 return false;
1449 }
1450 SkDevice* dev = this->createCompatibleDevice(SkBitmap::kARGB_8888_Config,
1451 src.width(),
1452 src.height(),
1453 false);
1454 if (NULL == dev) {
1455 return false;
1456 }
1457 SkAutoUnref aur(dev);
1458 SkCanvas canvas(dev);
1459 SkPaint paint;
1460 paint.setImageFilter(filter);
1461 canvas.drawSprite(src, 0, 0, &paint);
1462 *result = dev->accessBitmap(false);
1463 return true;
1464}
1465
reed@google.comac10a2d2010-12-22 21:39:39 +00001466///////////////////////////////////////////////////////////////////////////////
1467
1468// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001469static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1470 kTriangles_PrimitiveType,
1471 kTriangleStrip_PrimitiveType,
1472 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001473};
1474
1475void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1476 int vertexCount, const SkPoint vertices[],
1477 const SkPoint texs[], const SkColor colors[],
1478 SkXfermode* xmode,
1479 const uint16_t indices[], int indexCount,
1480 const SkPaint& paint) {
1481 CHECK_SHOULD_DRAW(draw);
1482
bsalomon@google.com5782d712011-01-21 21:03:59 +00001483 GrPaint grPaint;
1484 SkAutoCachedTexture act;
1485 // we ignore the shader if texs is null.
1486 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001487 if (!skPaint2GrPaintNoShader(paint,
1488 false,
1489 NULL == colors,
1490 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001491 return;
1492 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001493 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001494 if (!skPaint2GrPaintShader(this,
1495 paint,
1496 *draw.fMatrix,
1497 NULL == colors,
1498 &act,
1499 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001500 return;
1501 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001502 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001503
1504 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001505 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001506 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1507#if 0
1508 return
1509#endif
1510 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001511 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001512
bsalomon@google.com498776a2011-08-16 19:20:44 +00001513 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1514 if (NULL != colors) {
1515 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001516 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001517 for (int i = 0; i < vertexCount; ++i) {
1518 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1519 }
1520 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001521 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001522 fContext->drawVertices(grPaint,
1523 gVertexMode2PrimitiveType[vmode],
1524 vertexCount,
1525 (GrPoint*) vertices,
1526 (GrPoint*) texs,
1527 colors,
1528 indices,
1529 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001530}
1531
1532///////////////////////////////////////////////////////////////////////////////
1533
1534static void GlyphCacheAuxProc(void* data) {
1535 delete (GrFontScaler*)data;
1536}
1537
1538static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1539 void* auxData;
1540 GrFontScaler* scaler = NULL;
1541 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1542 scaler = (GrFontScaler*)auxData;
1543 }
1544 if (NULL == scaler) {
1545 scaler = new SkGrFontScaler(cache);
1546 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1547 }
1548 return scaler;
1549}
1550
1551static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1552 SkFixed fx, SkFixed fy,
1553 const SkGlyph& glyph) {
1554 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1555
bungeman@google.com15865a72012-01-11 16:28:04 +00001556 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001557
1558 if (NULL == procs->fFontScaler) {
1559 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1560 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001561
bungeman@google.com15865a72012-01-11 16:28:04 +00001562 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1563 glyph.getSubXFixed(),
1564 glyph.getSubYFixed()),
1565 SkFixedFloorToFixed(fx),
1566 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001567 procs->fFontScaler);
1568}
1569
bsalomon@google.com5782d712011-01-21 21:03:59 +00001570SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001571
1572 // deferred allocation
1573 if (NULL == fDrawProcs) {
1574 fDrawProcs = new GrSkDrawProcs;
1575 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1576 fDrawProcs->fContext = fContext;
1577 }
1578
1579 // init our (and GL's) state
1580 fDrawProcs->fTextContext = context;
1581 fDrawProcs->fFontScaler = NULL;
1582 return fDrawProcs;
1583}
1584
1585void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1586 size_t byteLength, SkScalar x, SkScalar y,
1587 const SkPaint& paint) {
1588 CHECK_SHOULD_DRAW(draw);
1589
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001590 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001591 // this guy will just call our drawPath()
1592 draw.drawText((const char*)text, byteLength, x, y, paint);
1593 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001594 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001595
1596 GrPaint grPaint;
1597 SkAutoCachedTexture act;
1598
bsalomon@google.com84405e02012-03-05 19:57:21 +00001599 if (!skPaint2GrPaintShader(this,
1600 paint,
1601 *draw.fMatrix,
1602 true,
1603 &act,
1604 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001605 return;
1606 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001607 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1608 grPaint, draw.fExtMatrix);
1609 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001610 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1611 }
1612}
1613
1614void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1615 size_t byteLength, const SkScalar pos[],
1616 SkScalar constY, int scalarsPerPos,
1617 const SkPaint& paint) {
1618 CHECK_SHOULD_DRAW(draw);
1619
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001620 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001621 // this guy will just call our drawPath()
1622 draw.drawPosText((const char*)text, byteLength, pos, constY,
1623 scalarsPerPos, paint);
1624 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001625 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001626
1627 GrPaint grPaint;
1628 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001629 if (!skPaint2GrPaintShader(this,
1630 paint,
1631 *draw.fMatrix,
1632 true,
1633 &act,
1634 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001635 return;
1636 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001637 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1638 grPaint, draw.fExtMatrix);
1639 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001640 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1641 scalarsPerPos, paint);
1642 }
1643}
1644
1645void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1646 size_t len, const SkPath& path,
1647 const SkMatrix* m, const SkPaint& paint) {
1648 CHECK_SHOULD_DRAW(draw);
1649
1650 SkASSERT(draw.fDevice == this);
1651 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1652}
1653
1654///////////////////////////////////////////////////////////////////////////////
1655
reed@google.comf67e4cf2011-03-15 20:56:58 +00001656bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1657 if (!paint.isLCDRenderText()) {
1658 // we're cool with the paint as is
1659 return false;
1660 }
1661
1662 if (paint.getShader() ||
1663 paint.getXfermode() || // unless its srcover
1664 paint.getMaskFilter() ||
1665 paint.getRasterizer() ||
1666 paint.getColorFilter() ||
1667 paint.getPathEffect() ||
1668 paint.isFakeBoldText() ||
1669 paint.getStyle() != SkPaint::kFill_Style) {
1670 // turn off lcd
1671 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1672 flags->fHinting = paint.getHinting();
1673 return true;
1674 }
1675 // we're cool with the paint as is
1676 return false;
1677}
1678
reed@google.com75d939b2011-12-07 15:07:23 +00001679void SkGpuDevice::flush() {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001680 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001681}
1682
reed@google.comf67e4cf2011-03-15 20:56:58 +00001683///////////////////////////////////////////////////////////////////////////////
1684
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001685SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001686 const GrSamplerState* sampler,
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001687 TexType type) {
1688 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001689 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001690
bsalomon@google.come97f0852011-06-17 13:10:25 +00001691 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001692 const GrTextureDesc desc = {
1693 kRenderTarget_GrTextureFlagBit,
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001694 bitmap.width(),
1695 bitmap.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001696 SkGr::Bitmap2PixelConfig(bitmap),
1697 {0} // samples
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001698 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001699 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001700 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001701 // we know layers will only be drawn through drawDevice.
1702 // drawDevice has been made to work with content embedded in a
1703 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001704 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001705 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001706 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001707 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001708 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001709 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001710 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001711 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001712 GrContext::TextureKey key = bitmap.getGenerationID();
1713 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001714
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001715 entry = ctx->findAndLockTexture(key, bitmap.width(),
1716 bitmap.height(), sampler);
1717 if (NULL == entry.texture()) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001718 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001719 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001720 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001721 } else {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001722 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY,
1723 sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001724 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001725 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001726 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1727 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001728 }
1729 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001730 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001731}
1732
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001733void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1734 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001735}
1736
bsalomon@google.comfb309512011-11-30 14:13:48 +00001737bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1738 const GrSamplerState& sampler) const {
1739 GrContext::TextureKey key = bitmap.getGenerationID();
1740 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
1741 return this->context()->isTextureInCache(key, bitmap.width(),
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001742 bitmap.height(), &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001743
1744}
1745
1746
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001747SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1748 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001749 bool isOpaque,
1750 Usage usage) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001751 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001752 width, height, usage));
1753}
1754
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001755GrTextContext* SkGpuDevice::getTextContext() {
1756 if (NULL == fTextContext) {
1757 fTextContext = new GrDefaultTextContext();
1758 }
1759 return fTextContext;
1760}