blob: c8e5f48094c2ba823de193d55f0b53e5839c2c3a [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
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000058// This constant represents the screen alignment criterion in texels for
59// requiring texture domain clamping to prevent color bleeding when drawing
60// a sub region of a larger source image.
61#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
reed@google.comac10a2d2010-12-22 21:39:39 +000062///////////////////////////////////////////////////////////////////////////////
63
bsalomon@google.com84405e02012-03-05 19:57:21 +000064class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
65public:
66 SkAutoCachedTexture() { }
67 SkAutoCachedTexture(SkGpuDevice* device,
68 const SkBitmap& bitmap,
69 const GrSamplerState* sampler,
70 GrTexture** texture) {
71 GrAssert(texture);
72 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000073 }
reed@google.comac10a2d2010-12-22 21:39:39 +000074
bsalomon@google.com84405e02012-03-05 19:57:21 +000075 ~SkAutoCachedTexture() {
76 if (fTex.texture()) {
77 fDevice->unlockCachedTexture(fTex);
78 }
reed@google.comac10a2d2010-12-22 21:39:39 +000079 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000080
81 GrTexture* set(SkGpuDevice* device,
82 const SkBitmap& bitmap,
83 const GrSamplerState* sampler) {
84 if (fTex.texture()) {
85 fDevice->unlockCachedTexture(fTex);
86 }
87 fDevice = device;
88 GrTexture* texture = (GrTexture*)bitmap.getTexture();
89 if (texture) {
90 // return the native texture
91 fTex.reset();
92 } else {
93 // look it up in our cache
94 fTex = device->lockCachedTexture(bitmap, sampler);
95 texture = fTex.texture();
96 }
97 return texture;
98 }
99
100private:
101 SkGpuDevice* fDevice;
102 GrContext::TextureCacheEntry fTex;
103};
reed@google.comac10a2d2010-12-22 21:39:39 +0000104
105///////////////////////////////////////////////////////////////////////////////
106
107bool gDoTraceDraw;
108
109struct GrSkDrawProcs : public SkDrawProcs {
110public:
111 GrContext* fContext;
112 GrTextContext* fTextContext;
113 GrFontScaler* fFontScaler; // cached in the skia glyphcache
114};
115
116///////////////////////////////////////////////////////////////////////////////
117
reed@google.comaf951c92011-06-16 19:10:39 +0000118static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
119 switch (config) {
120 case kAlpha_8_GrPixelConfig:
121 *isOpaque = false;
122 return SkBitmap::kA8_Config;
123 case kRGB_565_GrPixelConfig:
124 *isOpaque = true;
125 return SkBitmap::kRGB_565_Config;
126 case kRGBA_4444_GrPixelConfig:
127 *isOpaque = false;
128 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000129 case kSkia8888_PM_GrPixelConfig:
130 // we don't currently have a way of knowing whether
131 // a 8888 is opaque based on the config.
132 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000133 return SkBitmap::kARGB_8888_Config;
134 default:
135 *isOpaque = false;
136 return SkBitmap::kNo_Config;
137 }
138}
reed@google.comac10a2d2010-12-22 21:39:39 +0000139
reed@google.comaf951c92011-06-16 19:10:39 +0000140static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000141 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000142
143 bool isOpaque;
144 SkBitmap bitmap;
145 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
146 renderTarget->width(), renderTarget->height());
147 bitmap.setIsOpaque(isOpaque);
148 return bitmap;
149}
150
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000151SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
152: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
153 this->initFromRenderTarget(context, texture->asRenderTarget());
154}
155
reed@google.comaf951c92011-06-16 19:10:39 +0000156SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
157: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000158 this->initFromRenderTarget(context, renderTarget);
159}
160
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000161void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000162 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000163 fNeedPrepareRenderTarget = false;
164 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000165
reed@google.comaf951c92011-06-16 19:10:39 +0000166 fContext = context;
167 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000168
reed@google.comaf951c92011-06-16 19:10:39 +0000169 fTexture = NULL;
170 fRenderTarget = NULL;
171 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000172
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000173 GrAssert(NULL != renderTarget);
174 fRenderTarget = renderTarget;
175 fRenderTarget->ref();
176 // if this RT is also a texture, hold a ref on it
177 fTexture = fRenderTarget->asTexture();
178 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000179
180 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
181 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
182 // the device via accessBitmap. This external ref may outlive the device.
183 // Since textures own their render targets (but not vice-versa) we
184 // are ensuring that both objects will live as long as the pixel ref.
185 SkPixelRef* pr;
186 if (fTexture) {
187 pr = new SkGrTexturePixelRef(fTexture);
188 } else {
189 pr = new SkGrRenderTargetPixelRef(fRenderTarget);
190 }
reed@google.comaf951c92011-06-16 19:10:39 +0000191 this->setPixelRef(pr, 0)->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000192
193 fTextContext = NULL;
reed@google.comaf951c92011-06-16 19:10:39 +0000194}
195
196SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000197 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000198: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000199 fNeedPrepareRenderTarget = false;
200 fDrawProcs = NULL;
201
reed@google.com7b201d22011-01-11 18:59:23 +0000202 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000203 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000204
reed@google.comac10a2d2010-12-22 21:39:39 +0000205 fTexture = NULL;
206 fRenderTarget = NULL;
207 fNeedClear = false;
208
reed@google.comaf951c92011-06-16 19:10:39 +0000209 if (config != SkBitmap::kRGB_565_Config) {
210 config = SkBitmap::kARGB_8888_Config;
211 }
212 SkBitmap bm;
213 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000214
215#if CACHE_LAYER_TEXTURES
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000216 TexType type = (kSaveLayer_Usage == usage) ?
bsalomon@google.come97f0852011-06-17 13:10:25 +0000217 kSaveLayerDeviceRenderTarget_TexType :
218 kDeviceRenderTarget_TexType;
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000219 fCache = this->lockCachedTexture(bm, NULL, type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000220 fTexture = fCache.texture();
221 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000222 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000223 // hold a ref directly on fTexture (even though fCache has one) to match
224 // other constructor paths. Simplifies cleanup.
225 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000226 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000227#else
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),
233 {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);
reed@google.comac10a2d2010-12-22 21:39:39 +0000237#endif
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 // we defer the actual clear until our gainFocus()
245 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000246
reed@google.comaf951c92011-06-16 19:10:39 +0000247 // wrap the bitmap with a pixelref to expose our texture
248 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000249 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000250 } else {
251 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
252 width, height);
253 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000254 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000255
256 fTextContext = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000257}
258
259SkGpuDevice::~SkGpuDevice() {
260 if (fDrawProcs) {
261 delete fDrawProcs;
262 }
263
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000264 SkSafeUnref(fTexture);
265 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000266 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000267 GrAssert(NULL != fTexture);
268 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000269 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000270 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000271 fContext->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000272
273 if (NULL != fTextContext) {
274 fTextContext->unref();
275 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000276}
277
reed@google.comac10a2d2010-12-22 21:39:39 +0000278///////////////////////////////////////////////////////////////////////////////
279
280void SkGpuDevice::makeRenderTargetCurrent() {
281 fContext->setRenderTarget(fRenderTarget);
282 fContext->flush(true);
283 fNeedPrepareRenderTarget = true;
284}
285
286///////////////////////////////////////////////////////////////////////////////
287
bsalomon@google.comc4364992011-11-07 15:54:49 +0000288namespace {
289GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
290 switch (config8888) {
291 case SkCanvas::kNative_Premul_Config8888:
292 return kSkia8888_PM_GrPixelConfig;
293 case SkCanvas::kNative_Unpremul_Config8888:
294 return kSkia8888_UPM_GrPixelConfig;
295 case SkCanvas::kBGRA_Premul_Config8888:
296 return kBGRA_8888_PM_GrPixelConfig;
297 case SkCanvas::kBGRA_Unpremul_Config8888:
298 return kBGRA_8888_UPM_GrPixelConfig;
299 case SkCanvas::kRGBA_Premul_Config8888:
300 return kRGBA_8888_PM_GrPixelConfig;
301 case SkCanvas::kRGBA_Unpremul_Config8888:
302 return kRGBA_8888_UPM_GrPixelConfig;
303 default:
304 GrCrash("Unexpected Config8888.");
305 return kSkia8888_PM_GrPixelConfig;
306 }
307}
308}
309
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000310bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
311 int x, int y,
312 SkCanvas::Config8888 config8888) {
bsalomon@google.com910267d2011-11-02 20:06:25 +0000313 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
314 SkASSERT(!bitmap.isNull());
315 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000316
bsalomon@google.com910267d2011-11-02 20:06:25 +0000317 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000318 GrPixelConfig config;
319 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000320 return fContext->readRenderTargetPixels(fRenderTarget,
321 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000322 bitmap.width(),
323 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000324 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000325 bitmap.getPixels(),
326 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000327}
328
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000329void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
330 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000331 SkAutoLockPixels alp(bitmap);
332 if (!bitmap.readyToDraw()) {
333 return;
334 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000335
336 GrPixelConfig config;
337 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
338 config = config8888_to_gr_config(config8888);
339 } else {
340 config= SkGr::BitmapConfig2PixelConfig(bitmap.config(),
341 bitmap.isOpaque());
342 }
343
bsalomon@google.com6f379512011-11-16 20:36:03 +0000344 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
345 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000346}
347
348///////////////////////////////////////////////////////////////////////////////
349
350static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000351 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000352 const SkRegion& clipRegion,
353 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000354 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000355
356 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000357 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000358 const SkIRect& skBounds = clipRegion.getBounds();
359 GrRect bounds;
360 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
361 GrIntToScalar(skBounds.fTop),
362 GrIntToScalar(skBounds.fRight),
363 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000364 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
365 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000366 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000367}
368
369// call this ever each draw call, to ensure that the context reflects our state,
370// and not the state from some other canvas/device
371void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
372 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000373 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000374
375 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000376 SkASSERT(draw.fClipStack);
377 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000378 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000379 fNeedPrepareRenderTarget = false;
380 }
381}
382
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000383void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
384 const SkClipStack& clipStack) {
385 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
386 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000387 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000388}
389
390void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000391 const SkRegion& clip, const SkClipStack& clipStack) {
392
reed@google.comac10a2d2010-12-22 21:39:39 +0000393 fContext->setRenderTarget(fRenderTarget);
394
bsalomon@google.comd302f142011-03-03 13:54:13 +0000395 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000396
reed@google.com6f8f2922011-03-04 22:27:10 +0000397 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000398
399 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000400 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000401 fNeedClear = false;
402 }
403}
404
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000405SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
406 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000407}
408
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000409bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000410 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000411 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000412 return true;
413 }
414 return false;
415}
416
417///////////////////////////////////////////////////////////////////////////////
418
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000419SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
420SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
421SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
422SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
423SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
424 shader_type_mismatch);
425SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000426
bsalomon@google.com5782d712011-01-21 21:03:59 +0000427static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
428 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
429 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
430 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
431 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
432 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
433};
434
bsalomon@google.com84405e02012-03-05 19:57:21 +0000435namespace {
436
437// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
438// justAlpha indicates that skPaint's alpha should be used rather than the color
439// Callers may subsequently modify the GrPaint. Setting constantColor indicates
440// that the final paint will draw the same color at every pixel. This allows
441// an optimization where the the color filter can be applied to the skPaint's
442// color once while converting to GrPain and then ignored.
443inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
444 bool justAlpha,
445 bool constantColor,
446 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000447
448 grPaint->fDither = skPaint.isDither();
449 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000450 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000451
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000452 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
453 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000454
455 SkXfermode* mode = skPaint.getXfermode();
456 if (mode) {
457 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000458 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000459#if 0
460 return false;
461#endif
462 }
463 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000464 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
465 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
466
bsalomon@google.com5782d712011-01-21 21:03:59 +0000467 if (justAlpha) {
468 uint8_t alpha = skPaint.getAlpha();
469 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000470 // justAlpha is currently set to true only if there is a texture,
471 // so constantColor should not also be true.
472 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000473 } else {
474 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000475 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000476 }
Scroggo97c88c22011-05-11 14:05:25 +0000477 SkColorFilter* colorFilter = skPaint.getColorFilter();
478 SkColor color;
479 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000480 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000481 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000482 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000483 if (!constantColor) {
484 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
485 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000486 } else {
487 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
488 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000489 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000490 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000491 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
492 grPaint->fColorMatrixEnabled = true;
493 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
494 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
495 } else {
496 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000497 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000498 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000499}
500
bsalomon@google.com84405e02012-03-05 19:57:21 +0000501// This function is similar to skPaint2GrPaintNoShader but also converts
502// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
503// be used is set on grPaint and returned in param act. constantColor has the
504// same meaning as in skPaint2GrPaintNoShader.
505inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
506 const SkPaint& skPaint,
507 const SkMatrix& ctm,
508 bool constantColor,
509 SkGpuDevice::SkAutoCachedTexture* act,
510 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000511
bsalomon@google.com5782d712011-01-21 21:03:59 +0000512 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000513
bsalomon@google.com5782d712011-01-21 21:03:59 +0000514 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000515 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000516 return skPaint2GrPaintNoShader(skPaint,
517 false,
518 constantColor,
519 grPaint);
520 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000521 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000522 }
523
reed@google.comac10a2d2010-12-22 21:39:39 +0000524 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000525 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000526 SkShader::TileMode tileModes[2];
527 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000528 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000529 tileModes, twoPointParams);
530
bsalomon@google.com5782d712011-01-21 21:03:59 +0000531 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
532 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000533 SkShader::GradientInfo info;
534 SkColor color;
535
536 info.fColors = &color;
537 info.fColorOffsets = NULL;
538 info.fColorCount = 1;
539 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
540 SkPaint copy(skPaint);
541 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000542 // modulate the paint alpha by the shader's solid color alpha
543 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
544 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000545 return skPaint2GrPaintNoShader(copy,
546 false,
547 constantColor,
548 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000549 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000550 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000551 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000552 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000553 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000554 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000555 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000556 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000557 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000558 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000559 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
560 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000561 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000562 sampler->setRadial2Params(twoPointParams[0],
563 twoPointParams[1],
564 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000565 }
566
bsalomon@google.com84405e02012-03-05 19:57:21 +0000567 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000568 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000569 SkDebugf("Couldn't convert bitmap to texture.\n");
570 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000571 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000572 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000573
574 // since our texture coords will be in local space, we wack the texture
575 // matrix to map them back into 0...1 before we load it
576 SkMatrix localM;
577 if (shader->getLocalMatrix(&localM)) {
578 SkMatrix inverse;
579 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000580 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000581 }
582 }
583 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000584 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
585 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000586 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000587 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000588 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000589 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000590 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000591
592 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000593}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000594}
reed@google.comac10a2d2010-12-22 21:39:39 +0000595
596///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000597
bsalomon@google.com398109c2011-04-14 18:40:27 +0000598void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000599 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000600}
601
reed@google.comac10a2d2010-12-22 21:39:39 +0000602void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
603 CHECK_SHOULD_DRAW(draw);
604
bsalomon@google.com5782d712011-01-21 21:03:59 +0000605 GrPaint grPaint;
606 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000607 if (!skPaint2GrPaintShader(this,
608 paint,
609 *draw.fMatrix,
610 true,
611 &act,
612 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000613 return;
614 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000615
616 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000617}
618
619// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000620static const GrPrimitiveType gPointMode2PrimtiveType[] = {
621 kPoints_PrimitiveType,
622 kLines_PrimitiveType,
623 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000624};
625
626void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000627 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000628 CHECK_SHOULD_DRAW(draw);
629
630 SkScalar width = paint.getStrokeWidth();
631 if (width < 0) {
632 return;
633 }
634
635 // we only handle hairlines here, else we let the SkDraw call our drawPath()
636 if (width > 0) {
637 draw.drawPoints(mode, count, pts, paint, true);
638 return;
639 }
640
bsalomon@google.com5782d712011-01-21 21:03:59 +0000641 GrPaint grPaint;
642 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000643 if (!skPaint2GrPaintShader(this,
644 paint,
645 *draw.fMatrix,
646 true,
647 &act,
648 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000649 return;
650 }
651
bsalomon@google.com5782d712011-01-21 21:03:59 +0000652 fContext->drawVertices(grPaint,
653 gPointMode2PrimtiveType[mode],
654 count,
655 (GrPoint*)pts,
656 NULL,
657 NULL,
658 NULL,
659 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000660}
661
reed@google.comc9aa5872011-04-05 21:05:37 +0000662///////////////////////////////////////////////////////////////////////////////
663
reed@google.comac10a2d2010-12-22 21:39:39 +0000664void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
665 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000666 CHECK_SHOULD_DRAW(draw);
667
bungeman@google.com79bd8772011-07-18 15:34:08 +0000668 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000669 SkScalar width = paint.getStrokeWidth();
670
671 /*
672 We have special code for hairline strokes, miter-strokes, and fills.
673 Anything else we just call our path code.
674 */
675 bool usePath = doStroke && width > 0 &&
676 paint.getStrokeJoin() != SkPaint::kMiter_Join;
677 // another reason we might need to call drawPath...
678 if (paint.getMaskFilter()) {
679 usePath = true;
680 }
reed@google.com67db6642011-05-26 11:46:35 +0000681 // until we aa rotated rects...
682 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
683 usePath = true;
684 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000685 // small miter limit means right angles show bevel...
686 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
687 paint.getStrokeMiter() < SK_ScalarSqrt2)
688 {
689 usePath = true;
690 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000691 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000692 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
693 usePath = true;
694 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000695
696 if (usePath) {
697 SkPath path;
698 path.addRect(rect);
699 this->drawPath(draw, path, paint, NULL, true);
700 return;
701 }
702
703 GrPaint grPaint;
704 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000705 if (!skPaint2GrPaintShader(this,
706 paint,
707 *draw.fMatrix,
708 true,
709 &act,
710 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000711 return;
712 }
reed@google.com20efde72011-05-09 17:00:02 +0000713 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000714}
715
reed@google.com69302852011-02-16 18:08:07 +0000716#include "SkMaskFilter.h"
717#include "SkBounder.h"
718
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000719static GrPathFill skToGrFillType(SkPath::FillType fillType) {
720 switch (fillType) {
721 case SkPath::kWinding_FillType:
722 return kWinding_PathFill;
723 case SkPath::kEvenOdd_FillType:
724 return kEvenOdd_PathFill;
725 case SkPath::kInverseWinding_FillType:
726 return kInverseWinding_PathFill;
727 case SkPath::kInverseEvenOdd_FillType:
728 return kInverseEvenOdd_PathFill;
729 default:
730 SkDebugf("Unsupported path fill type\n");
731 return kHairLine_PathFill;
732 }
733}
734
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000735static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
736 SkMaskFilter* filter, const SkMatrix& matrix,
737 const SkRegion& clip, SkBounder* bounder,
738 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000739#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000740 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000741#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000742 SkMaskFilter::BlurInfo info;
743 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000744 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000745 return false;
746 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000747 SkScalar radius = info.fIgnoreTransform ? info.fRadius
748 : matrix.mapRadius(info.fRadius);
749 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000750 if (radius <= 0) {
751 return false;
752 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000753 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000754 float sigma3 = sigma * 3.0f;
755
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000756 SkRect srcRect = path.getBounds();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000757 SkRect clipRect;
758 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000759
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000760 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
761 srcRect.inset(-sigma3, -sigma3);
762 clipRect.inset(-sigma3, -sigma3);
763 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000764 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000765 SkIRect finalIRect;
766 finalRect.roundOut(&finalIRect);
767 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000768 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000769 }
770 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000771 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000772 }
773 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000774 srcRect.offset(offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000775 const GrTextureDesc desc = {
776 kRenderTarget_GrTextureFlagBit,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000777 srcRect.width(),
778 srcRect.height(),
779 // We actually only need A8, but it often isn't supported as a
780 // render target
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000781 kRGBA_8888_PM_GrPixelConfig,
782 {0} // samples
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000783 };
784
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000785 GrAutoScratchTexture pathEntry(context, desc);
786 GrTexture* pathTexture = pathEntry.texture();
787 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000788 return false;
789 }
790 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000791 // Once this code moves into GrContext, this should be changed to use
792 // an AutoClipRestore.
793 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000794 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000795 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000796 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000797 GrPaint tempPaint;
798 tempPaint.reset();
799
800 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000801 tempPaint.fAntiAlias = grp->fAntiAlias;
802 if (tempPaint.fAntiAlias) {
803 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
804 // blend coeff of zero requires dual source blending support in order
805 // to properly blend partially covered pixels. This means the AA
806 // code path may not be taken. So we use a dst blend coeff of ISA. We
807 // could special case AA draws to a dst surface with known alpha=0 to
808 // use a zero dst coeff when dual source blending isn't available.
809 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
810 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
811 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000812 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000813 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000814
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000815 GrAutoScratchTexture temp1, temp2;
816 // If we're doing a normal blur, we can clobber the pathTexture in the
817 // gaussianBlur. Otherwise, we need to save it for later compositing.
818 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000819 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
820 &temp1,
821 isNormalBlur ? NULL : &temp2,
822 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000823
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000824 if (!isNormalBlur) {
825 GrPaint paint;
826 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000827 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000828 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
829 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000830 // Blend pathTexture over blurTexture.
831 context->setRenderTarget(blurTexture->asRenderTarget());
832 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000833 if (SkMaskFilter::kInner_BlurType == blurType) {
834 // inner: dst = dst * src
835 paint.fSrcBlendCoeff = kDC_BlendCoeff;
836 paint.fDstBlendCoeff = kZero_BlendCoeff;
837 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
838 // solid: dst = src + dst - src * dst
839 // = (1 - dst) * src + 1 * dst
840 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
841 paint.fDstBlendCoeff = kOne_BlendCoeff;
842 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
843 // outer: dst = dst * (1 - src)
844 // = 0 * src + (1 - src) * dst
845 paint.fSrcBlendCoeff = kZero_BlendCoeff;
846 paint.fDstBlendCoeff = kISC_BlendCoeff;
847 }
848 context->drawRect(paint, srcRect);
849 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000850 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000851 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000852
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000853 if (grp->hasTextureOrMask()) {
854 GrMatrix inverse;
855 if (!matrix.invert(&inverse)) {
856 return false;
857 }
858 grp->preConcatActiveSamplerMatrices(inverse);
859 }
860
861 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
862 // we assume the last mask index is available for use
863 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000864 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000865 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000866
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000867 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
868 -finalRect.fTop);
869 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
870 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000872 return true;
873}
874
reed@google.com69302852011-02-16 18:08:07 +0000875static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
876 SkMaskFilter* filter, const SkMatrix& matrix,
877 const SkRegion& clip, SkBounder* bounder,
878 GrPaint* grp) {
879 SkMask srcM, dstM;
880
881 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
882 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
883 return false;
884 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000885 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000886
887 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
888 return false;
889 }
890 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000891 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000892
893 if (clip.quickReject(dstM.fBounds)) {
894 return false;
895 }
896 if (bounder && !bounder->doIRect(dstM.fBounds)) {
897 return false;
898 }
899
900 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
901 // the current clip (and identity matrix) and grpaint settings
902
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000903 // used to compute inverse view, if necessary
904 GrMatrix ivm = context->getMatrix();
905
reed@google.com0c219b62011-02-16 21:31:18 +0000906 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000907
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000908 const GrTextureDesc desc = {
909 kNone_GrTextureFlags,
reed@google.com69302852011-02-16 18:08:07 +0000910 dstM.fBounds.width(),
911 dstM.fBounds.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000912 kAlpha_8_GrPixelConfig,
913 {0}, // samples
reed@google.com69302852011-02-16 18:08:07 +0000914 };
915
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000916 GrAutoScratchTexture ast(context, desc);
917 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000918
reed@google.com69302852011-02-16 18:08:07 +0000919 if (NULL == texture) {
920 return false;
921 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000922 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000923 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000924
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000925 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
926 grp->preConcatActiveSamplerMatrices(ivm);
927 }
928
929 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
930 // we assume the last mask index is available for use
931 GrAssert(NULL == grp->getMask(MASK_IDX));
932 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000933 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000934
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000935 GrRect d;
936 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000937 GrIntToScalar(dstM.fBounds.fTop),
938 GrIntToScalar(dstM.fBounds.fRight),
939 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000940
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000941 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
942 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
943 -dstM.fBounds.fTop*SK_Scalar1);
944 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000945 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000946 return true;
947}
reed@google.com69302852011-02-16 18:08:07 +0000948
reed@google.com0c219b62011-02-16 21:31:18 +0000949void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000950 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000951 bool pathIsMutable) {
952 CHECK_SHOULD_DRAW(draw);
953
reed@google.comfe626382011-09-21 13:50:35 +0000954 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000955
bsalomon@google.com5782d712011-01-21 21:03:59 +0000956 GrPaint grPaint;
957 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000958 if (!skPaint2GrPaintShader(this,
959 paint,
960 *draw.fMatrix,
961 true,
962 &act,
963 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000964 return;
965 }
966
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000967 // can we cheat, and threat a thin stroke as a hairline w/ coverage
968 // if we can, we draw lots faster (raster device does this same test)
969 SkScalar hairlineCoverage;
970 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
971 doFill = false;
972 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
973 grPaint.fCoverage);
974 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000975
reed@google.comfe626382011-09-21 13:50:35 +0000976 // If we have a prematrix, apply it to the path, optimizing for the case
977 // where the original path can in fact be modified in place (even though
978 // its parameter type is const).
979 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
980 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000981
982 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +0000983 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +0000984
reed@google.come3445642011-02-16 23:20:39 +0000985 if (!pathIsMutable) {
986 result = &tmpPath;
987 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000988 }
reed@google.come3445642011-02-16 23:20:39 +0000989 // should I push prePathMatrix on our MV stack temporarily, instead
990 // of applying it here? See SkDraw.cpp
991 pathPtr->transform(*prePathMatrix, result);
992 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +0000993 }
reed@google.com0c219b62011-02-16 21:31:18 +0000994 // at this point we're done with prePathMatrix
995 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +0000996
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +0000997 if (paint.getPathEffect() ||
998 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +0000999 // it is safe to use tmpPath here, even if we already used it for the
1000 // prepathmatrix, since getFillPath can take the same object for its
1001 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001002 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001003 pathPtr = &tmpPath;
1004 }
1005
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001006 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001007 // avoid possibly allocating a new path in transform if we can
1008 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1009
1010 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001011 pathPtr->transform(*draw.fMatrix, devPathPtr);
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001012 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001013 *draw.fMatrix, *draw.fClip, draw.fBounder,
1014 &grPaint)) {
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001015 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001016 *draw.fMatrix, *draw.fClip, draw.fBounder,
1017 &grPaint);
1018 }
reed@google.com69302852011-02-16 18:08:07 +00001019 return;
1020 }
reed@google.com69302852011-02-16 18:08:07 +00001021
bsalomon@google.comffca4002011-02-22 20:34:01 +00001022 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001023
reed@google.com0c219b62011-02-16 21:31:18 +00001024 if (doFill) {
1025 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001026 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001027 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001028 break;
1029 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001030 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001031 break;
1032 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001033 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001034 break;
1035 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001036 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001037 break;
1038 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001039 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001040 return;
1041 }
1042 }
1043
reed@google.com07f3ee12011-05-16 17:21:57 +00001044 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001045}
1046
bsalomon@google.comfb309512011-11-30 14:13:48 +00001047namespace {
1048
1049inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1050 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1051 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1052 return tilesX * tilesY;
1053}
1054
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001055inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001056 const SkIRect* srcRectPtr,
1057 int maxTextureSize) {
1058 static const int kSmallTileSize = 1 << 10;
1059 if (maxTextureSize <= kSmallTileSize) {
1060 return maxTextureSize;
1061 }
1062
1063 size_t maxTexTotalTileSize;
1064 size_t smallTotalTileSize;
1065
1066 if (NULL == srcRectPtr) {
1067 int w = bitmap.width();
1068 int h = bitmap.height();
1069 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1070 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1071 } else {
1072 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1073 srcRectPtr->fTop,
1074 srcRectPtr->fRight,
1075 srcRectPtr->fBottom,
1076 maxTextureSize);
1077 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1078 srcRectPtr->fTop,
1079 srcRectPtr->fRight,
1080 srcRectPtr->fBottom,
1081 kSmallTileSize);
1082 }
1083 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1084 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1085
1086 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1087 return kSmallTileSize;
1088 } else {
1089 return maxTextureSize;
1090 }
1091}
1092}
1093
1094bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1095 const GrSamplerState& sampler,
1096 const SkIRect* srcRectPtr,
1097 int* tileSize) const {
1098 SkASSERT(NULL != tileSize);
1099
1100 // if bitmap is explictly texture backed then just use the texture
1101 if (NULL != bitmap.getTexture()) {
1102 return false;
1103 }
1104 // if it's larger than the max texture size, then we have no choice but
1105 // tiling
1106 const int maxTextureSize = fContext->getMaxTextureSize();
1107 if (bitmap.width() > maxTextureSize ||
1108 bitmap.height() > maxTextureSize) {
1109 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1110 return true;
1111 }
1112 // if we are going to have to draw the whole thing, then don't tile
1113 if (NULL == srcRectPtr) {
1114 return false;
1115 }
1116 // if the entire texture is already in our cache then no reason to tile it
1117 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1118 return false;
1119 }
1120
1121 // At this point we know we could do the draw by uploading the entire bitmap
1122 // as a texture. However, if the texture would be large compared to the
1123 // cache size and we don't require most of it for this draw then tile to
1124 // reduce the amount of upload and cache spill.
1125
1126 // assumption here is that sw bitmap size is a good proxy for its size as
1127 // a texture
1128 size_t bmpSize = bitmap.getSize();
1129 size_t cacheSize;
1130 fContext->getTextureCacheLimits(NULL, &cacheSize);
1131 if (bmpSize < cacheSize / 2) {
1132 return false;
1133 }
1134
1135 SkFixed fracUsed =
1136 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1137 (srcRectPtr->height() << 16) / bitmap.height());
1138 if (fracUsed <= SK_FixedHalf) {
1139 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1140 return true;
1141 } else {
1142 return false;
1143 }
1144}
1145
reed@google.comac10a2d2010-12-22 21:39:39 +00001146void SkGpuDevice::drawBitmap(const SkDraw& draw,
1147 const SkBitmap& bitmap,
1148 const SkIRect* srcRectPtr,
1149 const SkMatrix& m,
1150 const SkPaint& paint) {
1151 CHECK_SHOULD_DRAW(draw);
1152
1153 SkIRect srcRect;
1154 if (NULL == srcRectPtr) {
1155 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1156 } else {
1157 srcRect = *srcRectPtr;
1158 }
1159
junov@google.comd935cfb2011-06-27 20:48:23 +00001160 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001161 // Convert the bitmap to a shader so that the rect can be drawn
1162 // through drawRect, which supports mask filters.
1163 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001164 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001165 if (srcRectPtr) {
1166 if (!bitmap.extractSubset(&tmp, srcRect)) {
1167 return; // extraction failed
1168 }
1169 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001170 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001171 }
1172 SkPaint paintWithTexture(paint);
1173 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1174 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001175 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001176 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001177
junov@google.com1d329782011-07-28 20:10:09 +00001178 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001179 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001180 // also affect the behavior of the mask filter.
1181 SkMatrix drawMatrix;
1182 drawMatrix.setConcat(*draw.fMatrix, m);
1183 SkDraw transformedDraw(draw);
1184 transformedDraw.fMatrix = &drawMatrix;
1185
1186 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1187
junov@google.comd935cfb2011-06-27 20:48:23 +00001188 return;
1189 }
1190
bsalomon@google.com5782d712011-01-21 21:03:59 +00001191 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001192 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001193 return;
1194 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001195 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001196 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001197 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001198 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001199 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001200 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001201
bsalomon@google.comfb309512011-11-30 14:13:48 +00001202 int tileSize;
1203 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1204 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001205 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001206 return;
1207 }
1208
1209 // undo the translate done by SkCanvas
1210 int DX = SkMax32(0, srcRect.fLeft);
1211 int DY = SkMax32(0, srcRect.fTop);
1212 // compute clip bounds in local coordinates
1213 SkIRect clipRect;
1214 {
1215 SkRect r;
1216 r.set(draw.fClip->getBounds());
1217 SkMatrix matrix, inverse;
1218 matrix.setConcat(*draw.fMatrix, m);
1219 if (!matrix.invert(&inverse)) {
1220 return;
1221 }
1222 inverse.mapRect(&r);
1223 r.roundOut(&clipRect);
1224 // apply the canvas' translate to our local clip
1225 clipRect.offset(DX, DY);
1226 }
1227
bsalomon@google.comfb309512011-11-30 14:13:48 +00001228 int nx = bitmap.width() / tileSize;
1229 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001230 for (int x = 0; x <= nx; x++) {
1231 for (int y = 0; y <= ny; y++) {
1232 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001233 tileR.set(x * tileSize, y * tileSize,
1234 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001235 if (!SkIRect::Intersects(tileR, clipRect)) {
1236 continue;
1237 }
1238
1239 SkIRect srcR = tileR;
1240 if (!srcR.intersect(srcRect)) {
1241 continue;
1242 }
1243
1244 SkBitmap tmpB;
1245 if (bitmap.extractSubset(&tmpB, tileR)) {
1246 // now offset it to make it "local" to our tmp bitmap
1247 srcR.offset(-tileR.fLeft, -tileR.fTop);
1248
1249 SkMatrix tmpM(m);
1250 {
1251 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1252 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1253 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1254 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001255 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 }
1257 }
1258 }
1259}
1260
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001261namespace {
1262
1263bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1264 // detect pixel disalignment
1265 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1266 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1267 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1268 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1269 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1270 COLOR_BLEED_TOLERANCE &&
1271 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1272 COLOR_BLEED_TOLERANCE) {
1273 return true;
1274 }
1275 return false;
1276}
1277
1278bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1279 const SkMatrix& m) {
1280 // Only gets called if hasAlignedSamples returned false.
1281 // So we can assume that sampling is axis aligned but not texel aligned.
1282 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1283 SkRect innerSrcRect(srcRect), innerTransformedRect,
1284 outerTransformedRect(transformedRect);
1285 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1286 m.mapRect(&innerTransformedRect, innerSrcRect);
1287
1288 // The gap between outerTransformedRect and innerTransformedRect
1289 // represents the projection of the source border area, which is
1290 // problematic for color bleeding. We must check whether any
1291 // destination pixels sample the border area.
1292 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1293 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1294 SkIRect outer, inner;
1295 outerTransformedRect.round(&outer);
1296 innerTransformedRect.round(&inner);
1297 // If the inner and outer rects round to the same result, it means the
1298 // border does not overlap any pixel centers. Yay!
1299 return inner != outer;
1300}
1301
1302} // unnamed namespace
1303
reed@google.comac10a2d2010-12-22 21:39:39 +00001304/*
1305 * This is called by drawBitmap(), which has to handle images that may be too
1306 * large to be represented by a single texture.
1307 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001308 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1309 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001310 */
1311void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1312 const SkBitmap& bitmap,
1313 const SkIRect& srcRect,
1314 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001315 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001316 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1317 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001318
reed@google.com9c49bc32011-07-07 13:42:37 +00001319 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001321 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001322 return;
1323 }
1324
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001325 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001326
1327 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1328 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1329 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001330 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001331
1332 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001333 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 if (NULL == texture) {
1335 return;
1336 }
1337
bsalomon@google.com452943d2011-10-31 17:37:14 +00001338 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001339
reed@google.com20efde72011-05-09 17:00:02 +00001340 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1341 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001342 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001343 float wInv = 1.f / bitmap.width();
1344 float hInv = 1.f / bitmap.height();
1345 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1346 SkFloatToScalar(srcRect.fTop * hInv),
1347 SkFloatToScalar(srcRect.fRight * wInv),
1348 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001349
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001350 bool needsTextureDomain = false;
1351 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1352 {
1353 // Need texture domain if drawing a sub rect.
1354 needsTextureDomain = srcRect.width() < bitmap.width() ||
1355 srcRect.height() < bitmap.height();
1356 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1357 // sampling is axis-aligned
1358 GrRect floatSrcRect, transformedRect;
1359 floatSrcRect.set(srcRect);
1360 SkMatrix srcToDeviceMatrix(m);
1361 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1362 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1363
1364 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1365 // Samples are texel-aligned, so filtering is futile
1366 sampler->setFilter(GrSamplerState::kNearest_Filter);
1367 needsTextureDomain = false;
1368 } else {
1369 needsTextureDomain = needsTextureDomain &&
1370 mayColorBleed(floatSrcRect, transformedRect, m);
1371 }
1372 }
1373 }
1374
1375 GrRect textureDomain = GrRect::MakeEmpty();
1376
1377 if (needsTextureDomain) {
1378 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001379 GrScalar left, top, right, bottom;
1380 if (srcRect.width() > 1) {
1381 GrScalar border = GR_ScalarHalf / bitmap.width();
1382 left = paintRect.left() + border;
1383 right = paintRect.right() - border;
1384 } else {
1385 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1386 }
1387 if (srcRect.height() > 1) {
1388 GrScalar border = GR_ScalarHalf / bitmap.height();
1389 top = paintRect.top() + border;
1390 bottom = paintRect.bottom() - border;
1391 } else {
1392 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1393 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001394 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001395 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001396 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001397
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001398 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001399}
1400
1401void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1402 int left, int top, const SkPaint& paint) {
1403 CHECK_SHOULD_DRAW(draw);
1404
1405 SkAutoLockPixels alp(bitmap);
1406 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1407 return;
1408 }
1409
reed@google.com76dd2772012-01-05 21:15:07 +00001410 int w = bitmap.width();
1411 int h = bitmap.height();
1412
bsalomon@google.com5782d712011-01-21 21:03:59 +00001413 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001414 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001415 return;
1416 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001417
bsalomon@google.com5782d712011-01-21 21:03:59 +00001418 GrAutoMatrix avm(fContext, GrMatrix::I());
1419
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001420 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001421
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001422 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001423 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001424 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001425
reed@google.com76dd2772012-01-05 21:15:07 +00001426 SkImageFilter* imageFilter = paint.getImageFilter();
1427 SkSize blurSize;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001428 SkISize radius;
reed@google.com76dd2772012-01-05 21:15:07 +00001429 if (NULL != imageFilter && imageFilter->asABlur(&blurSize)) {
1430 GrAutoScratchTexture temp1, temp2;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001431 GrTexture* blurTexture = fContext->gaussianBlur(texture, &temp1, &temp2,
1432 GrRect::MakeWH(w, h),
1433 blurSize.width(),
1434 blurSize.height());
reed@google.com76dd2772012-01-05 21:15:07 +00001435 texture = blurTexture;
1436 grPaint.setTexture(kBitmapTextureIdx, texture);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001437 } else if (NULL != imageFilter && imageFilter->asADilate(&radius)) {
1438 const GrTextureDesc desc = {
1439 kRenderTarget_GrTextureFlagBit,
1440 w,
1441 h,
1442 kRGBA_8888_PM_GrPixelConfig,
1443 {0} // samples
1444 };
1445 GrAutoScratchTexture temp1(fContext, desc), temp2(fContext, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001446 texture = fContext->applyMorphology(texture, GrRect::MakeWH(w, h),
1447 temp1.texture(), temp2.texture(),
1448 GrSamplerState::kDilate_Filter,
1449 radius);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001450 grPaint.setTexture(kBitmapTextureIdx, texture);
1451 } else if (NULL != imageFilter && imageFilter->asAnErode(&radius)) {
1452 const GrTextureDesc desc = {
1453 kRenderTarget_GrTextureFlagBit,
1454 w,
1455 h,
1456 kRGBA_8888_PM_GrPixelConfig,
1457 {0} // samples
1458 };
1459 GrAutoScratchTexture temp1(fContext, desc), temp2(fContext, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001460 texture = fContext->applyMorphology(texture, GrRect::MakeWH(w, h),
1461 temp1.texture(), temp2.texture(),
1462 GrSamplerState::kErode_Filter,
1463 radius);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001464 grPaint.setTexture(kBitmapTextureIdx, texture);
reed@google.com76dd2772012-01-05 21:15:07 +00001465 } else {
1466 grPaint.setTexture(kBitmapTextureIdx, texture);
1467 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001468
bsalomon@google.com5782d712011-01-21 21:03:59 +00001469 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001470 GrRect::MakeXYWH(GrIntToScalar(left),
1471 GrIntToScalar(top),
1472 GrIntToScalar(w),
1473 GrIntToScalar(h)),
1474 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1475 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001476}
1477
1478void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1479 int x, int y, const SkPaint& paint) {
1480 CHECK_SHOULD_DRAW(draw);
1481
bsalomon@google.com5782d712011-01-21 21:03:59 +00001482 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001483 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001484 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001485 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001486 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001487
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001488 GrTexture* devTex = grPaint.getTexture(0);
1489 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001490
1491 const SkBitmap& bm = dev->accessBitmap(false);
1492 int w = bm.width();
1493 int h = bm.height();
1494
1495 GrAutoMatrix avm(fContext, GrMatrix::I());
1496
bsalomon@google.com97912912011-12-06 16:30:36 +00001497 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001498
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001499 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1500 GrIntToScalar(y),
1501 GrIntToScalar(w),
1502 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001503
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001504 // The device being drawn may not fill up its texture (saveLayer uses
1505 // the approximate ).
1506 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1507 GR_Scalar1 * h / devTex->height());
1508
1509 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001510}
1511
reed@google.com76dd2772012-01-05 21:15:07 +00001512bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1513 const SkMatrix& ctm,
1514 SkBitmap* result, SkIPoint* offset) {
1515 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001516 SkISize radius;
1517 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001518 return false;
1519 }
1520 SkDevice* dev = this->createCompatibleDevice(SkBitmap::kARGB_8888_Config,
1521 src.width(),
1522 src.height(),
1523 false);
1524 if (NULL == dev) {
1525 return false;
1526 }
1527 SkAutoUnref aur(dev);
1528 SkCanvas canvas(dev);
1529 SkPaint paint;
1530 paint.setImageFilter(filter);
1531 canvas.drawSprite(src, 0, 0, &paint);
1532 *result = dev->accessBitmap(false);
1533 return true;
1534}
1535
reed@google.comac10a2d2010-12-22 21:39:39 +00001536///////////////////////////////////////////////////////////////////////////////
1537
1538// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001539static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1540 kTriangles_PrimitiveType,
1541 kTriangleStrip_PrimitiveType,
1542 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001543};
1544
1545void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1546 int vertexCount, const SkPoint vertices[],
1547 const SkPoint texs[], const SkColor colors[],
1548 SkXfermode* xmode,
1549 const uint16_t indices[], int indexCount,
1550 const SkPaint& paint) {
1551 CHECK_SHOULD_DRAW(draw);
1552
bsalomon@google.com5782d712011-01-21 21:03:59 +00001553 GrPaint grPaint;
1554 SkAutoCachedTexture act;
1555 // we ignore the shader if texs is null.
1556 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001557 if (!skPaint2GrPaintNoShader(paint,
1558 false,
1559 NULL == colors,
1560 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001561 return;
1562 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001563 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001564 if (!skPaint2GrPaintShader(this,
1565 paint,
1566 *draw.fMatrix,
1567 NULL == colors,
1568 &act,
1569 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001570 return;
1571 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001572 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001573
1574 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001575 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001576 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1577#if 0
1578 return
1579#endif
1580 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001581 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001582
bsalomon@google.com498776a2011-08-16 19:20:44 +00001583 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1584 if (NULL != colors) {
1585 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001586 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001587 for (int i = 0; i < vertexCount; ++i) {
1588 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1589 }
1590 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001591 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001592 fContext->drawVertices(grPaint,
1593 gVertexMode2PrimitiveType[vmode],
1594 vertexCount,
1595 (GrPoint*) vertices,
1596 (GrPoint*) texs,
1597 colors,
1598 indices,
1599 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001600}
1601
1602///////////////////////////////////////////////////////////////////////////////
1603
1604static void GlyphCacheAuxProc(void* data) {
1605 delete (GrFontScaler*)data;
1606}
1607
1608static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1609 void* auxData;
1610 GrFontScaler* scaler = NULL;
1611 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1612 scaler = (GrFontScaler*)auxData;
1613 }
1614 if (NULL == scaler) {
1615 scaler = new SkGrFontScaler(cache);
1616 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1617 }
1618 return scaler;
1619}
1620
1621static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1622 SkFixed fx, SkFixed fy,
1623 const SkGlyph& glyph) {
1624 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1625
bungeman@google.com15865a72012-01-11 16:28:04 +00001626 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001627
1628 if (NULL == procs->fFontScaler) {
1629 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1630 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001631
bungeman@google.com15865a72012-01-11 16:28:04 +00001632 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1633 glyph.getSubXFixed(),
1634 glyph.getSubYFixed()),
1635 SkFixedFloorToFixed(fx),
1636 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001637 procs->fFontScaler);
1638}
1639
bsalomon@google.com5782d712011-01-21 21:03:59 +00001640SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001641
1642 // deferred allocation
1643 if (NULL == fDrawProcs) {
1644 fDrawProcs = new GrSkDrawProcs;
1645 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1646 fDrawProcs->fContext = fContext;
1647 }
1648
1649 // init our (and GL's) state
1650 fDrawProcs->fTextContext = context;
1651 fDrawProcs->fFontScaler = NULL;
1652 return fDrawProcs;
1653}
1654
1655void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1656 size_t byteLength, SkScalar x, SkScalar y,
1657 const SkPaint& paint) {
1658 CHECK_SHOULD_DRAW(draw);
1659
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001660 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001661 // this guy will just call our drawPath()
1662 draw.drawText((const char*)text, byteLength, x, y, paint);
1663 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001664 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001665
1666 GrPaint grPaint;
1667 SkAutoCachedTexture act;
1668
bsalomon@google.com84405e02012-03-05 19:57:21 +00001669 if (!skPaint2GrPaintShader(this,
1670 paint,
1671 *draw.fMatrix,
1672 true,
1673 &act,
1674 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001675 return;
1676 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001677 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1678 grPaint, draw.fExtMatrix);
1679 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001680 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1681 }
1682}
1683
1684void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1685 size_t byteLength, const SkScalar pos[],
1686 SkScalar constY, int scalarsPerPos,
1687 const SkPaint& paint) {
1688 CHECK_SHOULD_DRAW(draw);
1689
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001690 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001691 // this guy will just call our drawPath()
1692 draw.drawPosText((const char*)text, byteLength, pos, constY,
1693 scalarsPerPos, paint);
1694 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001695 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001696
1697 GrPaint grPaint;
1698 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001699 if (!skPaint2GrPaintShader(this,
1700 paint,
1701 *draw.fMatrix,
1702 true,
1703 &act,
1704 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001705 return;
1706 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001707 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1708 grPaint, draw.fExtMatrix);
1709 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001710 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1711 scalarsPerPos, paint);
1712 }
1713}
1714
1715void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1716 size_t len, const SkPath& path,
1717 const SkMatrix* m, const SkPaint& paint) {
1718 CHECK_SHOULD_DRAW(draw);
1719
1720 SkASSERT(draw.fDevice == this);
1721 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1722}
1723
1724///////////////////////////////////////////////////////////////////////////////
1725
reed@google.comf67e4cf2011-03-15 20:56:58 +00001726bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1727 if (!paint.isLCDRenderText()) {
1728 // we're cool with the paint as is
1729 return false;
1730 }
1731
1732 if (paint.getShader() ||
1733 paint.getXfermode() || // unless its srcover
1734 paint.getMaskFilter() ||
1735 paint.getRasterizer() ||
1736 paint.getColorFilter() ||
1737 paint.getPathEffect() ||
1738 paint.isFakeBoldText() ||
1739 paint.getStyle() != SkPaint::kFill_Style) {
1740 // turn off lcd
1741 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1742 flags->fHinting = paint.getHinting();
1743 return true;
1744 }
1745 // we're cool with the paint as is
1746 return false;
1747}
1748
reed@google.com75d939b2011-12-07 15:07:23 +00001749void SkGpuDevice::flush() {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001750 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001751}
1752
reed@google.comf67e4cf2011-03-15 20:56:58 +00001753///////////////////////////////////////////////////////////////////////////////
1754
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001755SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001756 const GrSamplerState* sampler,
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001757 TexType type) {
1758 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001759 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001760
bsalomon@google.come97f0852011-06-17 13:10:25 +00001761 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001762 const GrTextureDesc desc = {
1763 kRenderTarget_GrTextureFlagBit,
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001764 bitmap.width(),
1765 bitmap.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001766 SkGr::Bitmap2PixelConfig(bitmap),
1767 {0} // samples
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001768 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001769 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001770 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001771 // we know layers will only be drawn through drawDevice.
1772 // drawDevice has been made to work with content embedded in a
1773 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001774 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001775 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001776 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001777 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001778 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001779 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001780 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001781 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001782 GrContext::TextureKey key = bitmap.getGenerationID();
1783 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001784
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001785 entry = ctx->findAndLockTexture(key, bitmap.width(),
1786 bitmap.height(), sampler);
1787 if (NULL == entry.texture()) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001788 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001789 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001790 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001791 } else {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001792 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY,
1793 sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001794 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001795 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001796 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1797 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001798 }
1799 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001800 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001801}
1802
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001803void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1804 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001805}
1806
bsalomon@google.comfb309512011-11-30 14:13:48 +00001807bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1808 const GrSamplerState& sampler) const {
1809 GrContext::TextureKey key = bitmap.getGenerationID();
1810 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
1811 return this->context()->isTextureInCache(key, bitmap.width(),
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001812 bitmap.height(), &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001813
1814}
1815
1816
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001817SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1818 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001819 bool isOpaque,
1820 Usage usage) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001821 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001822 width, height, usage));
1823}
1824
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001825GrTextContext* SkGpuDevice::getTextContext() {
1826 if (NULL == fTextContext) {
1827 fTextContext = new GrDefaultTextContext();
1828 }
1829 return fTextContext;
1830}