blob: 5f9879e8ec3420bf5878de38b00d3beb472d8b79 [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;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000677 // another two reasons we might need to call drawPath...
678 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000679 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
bsalomon@google.com85003222012-03-28 14:44:37 +0000719///////////////////////////////////////////////////////////////////////////////
720
721// helpers for applying mask filters
722namespace {
723
724GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000725 switch (fillType) {
726 case SkPath::kWinding_FillType:
727 return kWinding_PathFill;
728 case SkPath::kEvenOdd_FillType:
729 return kEvenOdd_PathFill;
730 case SkPath::kInverseWinding_FillType:
731 return kInverseWinding_PathFill;
732 case SkPath::kInverseEvenOdd_FillType:
733 return kInverseEvenOdd_PathFill;
734 default:
735 SkDebugf("Unsupported path fill type\n");
736 return kHairLine_PathFill;
737 }
738}
739
bsalomon@google.com85003222012-03-28 14:44:37 +0000740// We prefer to blur small rect with small radius via CPU.
741#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
742#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
743inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
744 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
745 rect.height() <= MIN_GPU_BLUR_SIZE &&
746 radius <= MIN_GPU_BLUR_RADIUS) {
747 return true;
748 }
749 return false;
750}
751
752bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
753 SkMaskFilter* filter, const SkMatrix& matrix,
754 const SkRegion& clip, SkBounder* bounder,
755 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000756#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000757 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000758#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000759 SkMaskFilter::BlurInfo info;
760 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000761 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000762 return false;
763 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000764 SkScalar radius = info.fIgnoreTransform ? info.fRadius
765 : matrix.mapRadius(info.fRadius);
766 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000767 if (radius <= 0) {
768 return false;
769 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000770
771 SkRect srcRect = path.getBounds();
772 if (shouldDrawBlurWithCPU(srcRect, radius)) {
773 return false;
774 }
775
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000776 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000777 float sigma3 = sigma * 3.0f;
778
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000779 SkRect clipRect;
780 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000781
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000782 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
783 srcRect.inset(-sigma3, -sigma3);
784 clipRect.inset(-sigma3, -sigma3);
785 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000786 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000787 SkIRect finalIRect;
788 finalRect.roundOut(&finalIRect);
789 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000790 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000791 }
792 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000793 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000794 }
795 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000796 srcRect.offset(offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000797 const GrTextureDesc desc = {
798 kRenderTarget_GrTextureFlagBit,
bungeman@google.comf8aa18c2012-03-19 21:04:52 +0000799 SkScalarCeilToInt(srcRect.width()),
800 SkScalarCeilToInt(srcRect.height()),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000801 // We actually only need A8, but it often isn't supported as a
802 // render target
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000803 kRGBA_8888_PM_GrPixelConfig,
804 {0} // samples
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000805 };
806
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000807 GrAutoScratchTexture pathEntry(context, desc);
808 GrTexture* pathTexture = pathEntry.texture();
809 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000810 return false;
811 }
812 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000813 // Once this code moves into GrContext, this should be changed to use
814 // an AutoClipRestore.
815 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000816 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000817 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000819 GrPaint tempPaint;
820 tempPaint.reset();
821
822 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000823 tempPaint.fAntiAlias = grp->fAntiAlias;
824 if (tempPaint.fAntiAlias) {
825 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
826 // blend coeff of zero requires dual source blending support in order
827 // to properly blend partially covered pixels. This means the AA
828 // code path may not be taken. So we use a dst blend coeff of ISA. We
829 // could special case AA draws to a dst surface with known alpha=0 to
830 // use a zero dst coeff when dual source blending isn't available.
831 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
832 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
833 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000834 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000835 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000836
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000837 GrAutoScratchTexture temp1, temp2;
838 // If we're doing a normal blur, we can clobber the pathTexture in the
839 // gaussianBlur. Otherwise, we need to save it for later compositing.
840 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000841 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
842 &temp1,
843 isNormalBlur ? NULL : &temp2,
844 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000845
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000846 if (!isNormalBlur) {
847 GrPaint paint;
848 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000849 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000850 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
851 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000852 // Blend pathTexture over blurTexture.
853 context->setRenderTarget(blurTexture->asRenderTarget());
854 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000855 if (SkMaskFilter::kInner_BlurType == blurType) {
856 // inner: dst = dst * src
857 paint.fSrcBlendCoeff = kDC_BlendCoeff;
858 paint.fDstBlendCoeff = kZero_BlendCoeff;
859 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
860 // solid: dst = src + dst - src * dst
861 // = (1 - dst) * src + 1 * dst
862 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
863 paint.fDstBlendCoeff = kOne_BlendCoeff;
864 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
865 // outer: dst = dst * (1 - src)
866 // = 0 * src + (1 - src) * dst
867 paint.fSrcBlendCoeff = kZero_BlendCoeff;
868 paint.fDstBlendCoeff = kISC_BlendCoeff;
869 }
870 context->drawRect(paint, srcRect);
871 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000872 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000873 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000874
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000875 if (grp->hasTextureOrMask()) {
876 GrMatrix inverse;
877 if (!matrix.invert(&inverse)) {
878 return false;
879 }
880 grp->preConcatActiveSamplerMatrices(inverse);
881 }
882
883 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
884 // we assume the last mask index is available for use
885 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000886 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000887 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000888
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000889 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
890 -finalRect.fTop);
891 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
892 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000893 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000894 return true;
895}
896
bsalomon@google.com85003222012-03-28 14:44:37 +0000897bool drawWithMaskFilter(GrContext* context, const SkPath& path,
898 SkMaskFilter* filter, const SkMatrix& matrix,
899 const SkRegion& clip, SkBounder* bounder,
900 GrPaint* grp) {
reed@google.com69302852011-02-16 18:08:07 +0000901 SkMask srcM, dstM;
902
903 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
904 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
905 return false;
906 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000907 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000908
909 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
910 return false;
911 }
912 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000913 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000914
915 if (clip.quickReject(dstM.fBounds)) {
916 return false;
917 }
918 if (bounder && !bounder->doIRect(dstM.fBounds)) {
919 return false;
920 }
921
922 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
923 // the current clip (and identity matrix) and grpaint settings
924
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000925 // used to compute inverse view, if necessary
926 GrMatrix ivm = context->getMatrix();
927
reed@google.com0c219b62011-02-16 21:31:18 +0000928 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000929
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000930 const GrTextureDesc desc = {
931 kNone_GrTextureFlags,
reed@google.com69302852011-02-16 18:08:07 +0000932 dstM.fBounds.width(),
933 dstM.fBounds.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000934 kAlpha_8_GrPixelConfig,
935 {0}, // samples
reed@google.com69302852011-02-16 18:08:07 +0000936 };
937
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000938 GrAutoScratchTexture ast(context, desc);
939 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000940
reed@google.com69302852011-02-16 18:08:07 +0000941 if (NULL == texture) {
942 return false;
943 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000944 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000945 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000946
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000947 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
948 grp->preConcatActiveSamplerMatrices(ivm);
949 }
950
951 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
952 // we assume the last mask index is available for use
953 GrAssert(NULL == grp->getMask(MASK_IDX));
954 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000955 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000956
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000957 GrRect d;
958 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000959 GrIntToScalar(dstM.fBounds.fTop),
960 GrIntToScalar(dstM.fBounds.fRight),
961 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000962
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000963 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
964 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
965 -dstM.fBounds.fTop*SK_Scalar1);
966 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000967 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000968 return true;
969}
reed@google.com69302852011-02-16 18:08:07 +0000970
bsalomon@google.com85003222012-03-28 14:44:37 +0000971}
972
973///////////////////////////////////////////////////////////////////////////////
974
reed@google.com0c219b62011-02-16 21:31:18 +0000975void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000976 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000977 bool pathIsMutable) {
978 CHECK_SHOULD_DRAW(draw);
979
reed@google.comfe626382011-09-21 13:50:35 +0000980 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000981
bsalomon@google.com5782d712011-01-21 21:03:59 +0000982 GrPaint grPaint;
983 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000984 if (!skPaint2GrPaintShader(this,
985 paint,
986 *draw.fMatrix,
987 true,
988 &act,
989 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000990 return;
991 }
992
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000993 // can we cheat, and threat a thin stroke as a hairline w/ coverage
994 // if we can, we draw lots faster (raster device does this same test)
995 SkScalar hairlineCoverage;
996 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
997 doFill = false;
998 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
999 grPaint.fCoverage);
1000 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001001
reed@google.comfe626382011-09-21 13:50:35 +00001002 // If we have a prematrix, apply it to the path, optimizing for the case
1003 // where the original path can in fact be modified in place (even though
1004 // its parameter type is const).
1005 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1006 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001007
1008 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001009 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001010
reed@google.come3445642011-02-16 23:20:39 +00001011 if (!pathIsMutable) {
1012 result = &tmpPath;
1013 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001014 }
reed@google.come3445642011-02-16 23:20:39 +00001015 // should I push prePathMatrix on our MV stack temporarily, instead
1016 // of applying it here? See SkDraw.cpp
1017 pathPtr->transform(*prePathMatrix, result);
1018 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001019 }
reed@google.com0c219b62011-02-16 21:31:18 +00001020 // at this point we're done with prePathMatrix
1021 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001022
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001023 if (paint.getPathEffect() ||
1024 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001025 // it is safe to use tmpPath here, even if we already used it for the
1026 // prepathmatrix, since getFillPath can take the same object for its
1027 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001028 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001029 pathPtr = &tmpPath;
1030 }
1031
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001032 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001033 // avoid possibly allocating a new path in transform if we can
1034 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1035
1036 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001037 pathPtr->transform(*draw.fMatrix, devPathPtr);
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001038 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001039 *draw.fMatrix, *draw.fClip, draw.fBounder,
1040 &grPaint)) {
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001041 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001042 *draw.fMatrix, *draw.fClip, draw.fBounder,
1043 &grPaint);
1044 }
reed@google.com69302852011-02-16 18:08:07 +00001045 return;
1046 }
reed@google.com69302852011-02-16 18:08:07 +00001047
bsalomon@google.comffca4002011-02-22 20:34:01 +00001048 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001049
reed@google.com0c219b62011-02-16 21:31:18 +00001050 if (doFill) {
1051 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001053 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001054 break;
1055 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001056 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001057 break;
1058 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001059 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001060 break;
1061 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001062 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001063 break;
1064 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001065 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001066 return;
1067 }
1068 }
1069
reed@google.com07f3ee12011-05-16 17:21:57 +00001070 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001071}
1072
bsalomon@google.comfb309512011-11-30 14:13:48 +00001073namespace {
1074
1075inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1076 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1077 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1078 return tilesX * tilesY;
1079}
1080
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001081inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001082 const SkIRect* srcRectPtr,
1083 int maxTextureSize) {
1084 static const int kSmallTileSize = 1 << 10;
1085 if (maxTextureSize <= kSmallTileSize) {
1086 return maxTextureSize;
1087 }
1088
1089 size_t maxTexTotalTileSize;
1090 size_t smallTotalTileSize;
1091
1092 if (NULL == srcRectPtr) {
1093 int w = bitmap.width();
1094 int h = bitmap.height();
1095 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1096 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1097 } else {
1098 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1099 srcRectPtr->fTop,
1100 srcRectPtr->fRight,
1101 srcRectPtr->fBottom,
1102 maxTextureSize);
1103 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1104 srcRectPtr->fTop,
1105 srcRectPtr->fRight,
1106 srcRectPtr->fBottom,
1107 kSmallTileSize);
1108 }
1109 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1110 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1111
1112 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1113 return kSmallTileSize;
1114 } else {
1115 return maxTextureSize;
1116 }
1117}
1118}
1119
1120bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1121 const GrSamplerState& sampler,
1122 const SkIRect* srcRectPtr,
1123 int* tileSize) const {
1124 SkASSERT(NULL != tileSize);
1125
1126 // if bitmap is explictly texture backed then just use the texture
1127 if (NULL != bitmap.getTexture()) {
1128 return false;
1129 }
1130 // if it's larger than the max texture size, then we have no choice but
1131 // tiling
1132 const int maxTextureSize = fContext->getMaxTextureSize();
1133 if (bitmap.width() > maxTextureSize ||
1134 bitmap.height() > maxTextureSize) {
1135 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1136 return true;
1137 }
1138 // if we are going to have to draw the whole thing, then don't tile
1139 if (NULL == srcRectPtr) {
1140 return false;
1141 }
1142 // if the entire texture is already in our cache then no reason to tile it
1143 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1144 return false;
1145 }
1146
1147 // At this point we know we could do the draw by uploading the entire bitmap
1148 // as a texture. However, if the texture would be large compared to the
1149 // cache size and we don't require most of it for this draw then tile to
1150 // reduce the amount of upload and cache spill.
1151
1152 // assumption here is that sw bitmap size is a good proxy for its size as
1153 // a texture
1154 size_t bmpSize = bitmap.getSize();
1155 size_t cacheSize;
1156 fContext->getTextureCacheLimits(NULL, &cacheSize);
1157 if (bmpSize < cacheSize / 2) {
1158 return false;
1159 }
1160
1161 SkFixed fracUsed =
1162 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1163 (srcRectPtr->height() << 16) / bitmap.height());
1164 if (fracUsed <= SK_FixedHalf) {
1165 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1166 return true;
1167 } else {
1168 return false;
1169 }
1170}
1171
reed@google.comac10a2d2010-12-22 21:39:39 +00001172void SkGpuDevice::drawBitmap(const SkDraw& draw,
1173 const SkBitmap& bitmap,
1174 const SkIRect* srcRectPtr,
1175 const SkMatrix& m,
1176 const SkPaint& paint) {
1177 CHECK_SHOULD_DRAW(draw);
1178
1179 SkIRect srcRect;
1180 if (NULL == srcRectPtr) {
1181 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1182 } else {
1183 srcRect = *srcRectPtr;
1184 }
1185
junov@google.comd935cfb2011-06-27 20:48:23 +00001186 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001187 // Convert the bitmap to a shader so that the rect can be drawn
1188 // through drawRect, which supports mask filters.
1189 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001190 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001191 if (srcRectPtr) {
1192 if (!bitmap.extractSubset(&tmp, srcRect)) {
1193 return; // extraction failed
1194 }
1195 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001196 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001197 }
1198 SkPaint paintWithTexture(paint);
1199 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1200 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001201 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001202 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001203
junov@google.com1d329782011-07-28 20:10:09 +00001204 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001205 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001206 // also affect the behavior of the mask filter.
1207 SkMatrix drawMatrix;
1208 drawMatrix.setConcat(*draw.fMatrix, m);
1209 SkDraw transformedDraw(draw);
1210 transformedDraw.fMatrix = &drawMatrix;
1211
1212 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1213
junov@google.comd935cfb2011-06-27 20:48:23 +00001214 return;
1215 }
1216
bsalomon@google.com5782d712011-01-21 21:03:59 +00001217 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001218 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001219 return;
1220 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001221 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001222 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001223 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001224 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001225 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001226 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001227
bsalomon@google.comfb309512011-11-30 14:13:48 +00001228 int tileSize;
1229 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1230 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001231 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001232 return;
1233 }
1234
1235 // undo the translate done by SkCanvas
1236 int DX = SkMax32(0, srcRect.fLeft);
1237 int DY = SkMax32(0, srcRect.fTop);
1238 // compute clip bounds in local coordinates
1239 SkIRect clipRect;
1240 {
1241 SkRect r;
1242 r.set(draw.fClip->getBounds());
1243 SkMatrix matrix, inverse;
1244 matrix.setConcat(*draw.fMatrix, m);
1245 if (!matrix.invert(&inverse)) {
1246 return;
1247 }
1248 inverse.mapRect(&r);
1249 r.roundOut(&clipRect);
1250 // apply the canvas' translate to our local clip
1251 clipRect.offset(DX, DY);
1252 }
1253
bsalomon@google.comfb309512011-11-30 14:13:48 +00001254 int nx = bitmap.width() / tileSize;
1255 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 for (int x = 0; x <= nx; x++) {
1257 for (int y = 0; y <= ny; y++) {
1258 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001259 tileR.set(x * tileSize, y * tileSize,
1260 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001261 if (!SkIRect::Intersects(tileR, clipRect)) {
1262 continue;
1263 }
1264
1265 SkIRect srcR = tileR;
1266 if (!srcR.intersect(srcRect)) {
1267 continue;
1268 }
1269
1270 SkBitmap tmpB;
1271 if (bitmap.extractSubset(&tmpB, tileR)) {
1272 // now offset it to make it "local" to our tmp bitmap
1273 srcR.offset(-tileR.fLeft, -tileR.fTop);
1274
1275 SkMatrix tmpM(m);
1276 {
1277 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1278 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1279 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1280 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001281 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 }
1283 }
1284 }
1285}
1286
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001287namespace {
1288
1289bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1290 // detect pixel disalignment
1291 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1292 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1293 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1294 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1295 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1296 COLOR_BLEED_TOLERANCE &&
1297 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1298 COLOR_BLEED_TOLERANCE) {
1299 return true;
1300 }
1301 return false;
1302}
1303
1304bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1305 const SkMatrix& m) {
1306 // Only gets called if hasAlignedSamples returned false.
1307 // So we can assume that sampling is axis aligned but not texel aligned.
1308 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1309 SkRect innerSrcRect(srcRect), innerTransformedRect,
1310 outerTransformedRect(transformedRect);
1311 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1312 m.mapRect(&innerTransformedRect, innerSrcRect);
1313
1314 // The gap between outerTransformedRect and innerTransformedRect
1315 // represents the projection of the source border area, which is
1316 // problematic for color bleeding. We must check whether any
1317 // destination pixels sample the border area.
1318 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1319 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1320 SkIRect outer, inner;
1321 outerTransformedRect.round(&outer);
1322 innerTransformedRect.round(&inner);
1323 // If the inner and outer rects round to the same result, it means the
1324 // border does not overlap any pixel centers. Yay!
1325 return inner != outer;
1326}
1327
1328} // unnamed namespace
1329
reed@google.comac10a2d2010-12-22 21:39:39 +00001330/*
1331 * This is called by drawBitmap(), which has to handle images that may be too
1332 * large to be represented by a single texture.
1333 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001334 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1335 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001336 */
1337void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1338 const SkBitmap& bitmap,
1339 const SkIRect& srcRect,
1340 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001341 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001342 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1343 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001344
reed@google.com9c49bc32011-07-07 13:42:37 +00001345 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001347 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001348 return;
1349 }
1350
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001351 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001352
1353 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1354 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1355 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001356 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001357
1358 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001359 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001360 if (NULL == texture) {
1361 return;
1362 }
1363
bsalomon@google.com452943d2011-10-31 17:37:14 +00001364 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001365
reed@google.com20efde72011-05-09 17:00:02 +00001366 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1367 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001368 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001369 float wInv = 1.f / bitmap.width();
1370 float hInv = 1.f / bitmap.height();
1371 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1372 SkFloatToScalar(srcRect.fTop * hInv),
1373 SkFloatToScalar(srcRect.fRight * wInv),
1374 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001375
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001376 bool needsTextureDomain = false;
1377 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1378 {
1379 // Need texture domain if drawing a sub rect.
1380 needsTextureDomain = srcRect.width() < bitmap.width() ||
1381 srcRect.height() < bitmap.height();
1382 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1383 // sampling is axis-aligned
1384 GrRect floatSrcRect, transformedRect;
1385 floatSrcRect.set(srcRect);
1386 SkMatrix srcToDeviceMatrix(m);
1387 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1388 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1389
1390 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1391 // Samples are texel-aligned, so filtering is futile
1392 sampler->setFilter(GrSamplerState::kNearest_Filter);
1393 needsTextureDomain = false;
1394 } else {
1395 needsTextureDomain = needsTextureDomain &&
1396 mayColorBleed(floatSrcRect, transformedRect, m);
1397 }
1398 }
1399 }
1400
1401 GrRect textureDomain = GrRect::MakeEmpty();
1402
1403 if (needsTextureDomain) {
1404 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001405 GrScalar left, top, right, bottom;
1406 if (srcRect.width() > 1) {
1407 GrScalar border = GR_ScalarHalf / bitmap.width();
1408 left = paintRect.left() + border;
1409 right = paintRect.right() - border;
1410 } else {
1411 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1412 }
1413 if (srcRect.height() > 1) {
1414 GrScalar border = GR_ScalarHalf / bitmap.height();
1415 top = paintRect.top() + border;
1416 bottom = paintRect.bottom() - border;
1417 } else {
1418 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1419 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001420 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001421 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001422 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001423
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001424 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001425}
1426
reed@google.com8926b162012-03-23 15:36:36 +00001427static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1428 SkImageFilter* filter, const GrRect& rect) {
1429 GrAssert(filter);
1430
1431 SkSize blurSize;
1432 SkISize radius;
1433
1434 const GrTextureDesc desc = {
1435 kRenderTarget_GrTextureFlagBit,
1436 rect.width(),
1437 rect.height(),
1438 kRGBA_8888_PM_GrPixelConfig,
1439 {0} // samples
1440 };
1441
1442 if (filter->asABlur(&blurSize)) {
1443 GrAutoScratchTexture temp1, temp2;
1444 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1445 blurSize.width(),
1446 blurSize.height());
1447 texture->ref();
1448 } else if (filter->asADilate(&radius)) {
1449 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1450 texture = context->applyMorphology(texture, rect,
1451 temp1.texture(), temp2.texture(),
1452 GrSamplerState::kDilate_Filter,
1453 radius);
1454 texture->ref();
1455 } else if (filter->asAnErode(&radius)) {
1456 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1457 texture = context->applyMorphology(texture, rect,
1458 temp1.texture(), temp2.texture(),
1459 GrSamplerState::kErode_Filter,
1460 radius);
1461 texture->ref();
1462 }
1463 return texture;
1464}
1465
reed@google.comac10a2d2010-12-22 21:39:39 +00001466void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1467 int left, int top, const SkPaint& paint) {
1468 CHECK_SHOULD_DRAW(draw);
1469
reed@google.com8926b162012-03-23 15:36:36 +00001470 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001471 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1472 return;
1473 }
1474
reed@google.com76dd2772012-01-05 21:15:07 +00001475 int w = bitmap.width();
1476 int h = bitmap.height();
1477
bsalomon@google.com5782d712011-01-21 21:03:59 +00001478 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001479 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001480 return;
1481 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001482
bsalomon@google.com5782d712011-01-21 21:03:59 +00001483 GrAutoMatrix avm(fContext, GrMatrix::I());
1484
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001485 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001486
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001487 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001488 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001489 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001490 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001491
reed@google.com8926b162012-03-23 15:36:36 +00001492 SkImageFilter* filter = paint.getImageFilter();
1493 if (NULL != filter) {
1494 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
1495 GrRect::MakeWH(w, h));
1496 if (filteredTexture) {
1497 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1498 texture = filteredTexture;
1499 filteredTexture->unref();
1500 }
reed@google.com76dd2772012-01-05 21:15:07 +00001501 }
reed@google.com8926b162012-03-23 15:36:36 +00001502
bsalomon@google.com5782d712011-01-21 21:03:59 +00001503 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001504 GrRect::MakeXYWH(GrIntToScalar(left),
1505 GrIntToScalar(top),
1506 GrIntToScalar(w),
1507 GrIntToScalar(h)),
1508 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1509 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001510}
1511
1512void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1513 int x, int y, const SkPaint& paint) {
1514 CHECK_SHOULD_DRAW(draw);
1515
bsalomon@google.com5782d712011-01-21 21:03:59 +00001516 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001517 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001518 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001519 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001520 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001521
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001522 GrTexture* devTex = grPaint.getTexture(0);
1523 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001524
reed@google.com8926b162012-03-23 15:36:36 +00001525 SkImageFilter* filter = paint.getImageFilter();
1526 if (NULL != filter) {
1527 GrRect rect = GrRect::MakeWH(devTex->width(), devTex->height());
1528 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1529 rect);
1530 if (filteredTexture) {
1531 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1532 devTex = filteredTexture;
1533 filteredTexture->unref();
1534 }
1535 }
1536
bsalomon@google.com5782d712011-01-21 21:03:59 +00001537 const SkBitmap& bm = dev->accessBitmap(false);
1538 int w = bm.width();
1539 int h = bm.height();
1540
1541 GrAutoMatrix avm(fContext, GrMatrix::I());
1542
bsalomon@google.com97912912011-12-06 16:30:36 +00001543 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001544
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001545 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1546 GrIntToScalar(y),
1547 GrIntToScalar(w),
1548 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001549
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001550 // The device being drawn may not fill up its texture (saveLayer uses
1551 // the approximate ).
1552 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1553 GR_Scalar1 * h / devTex->height());
1554
1555 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001556}
1557
reed@google.com8926b162012-03-23 15:36:36 +00001558bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001559 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001560 SkISize radius;
1561 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001562 return false;
1563 }
reed@google.com8926b162012-03-23 15:36:36 +00001564 return true;
1565}
1566
1567bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1568 const SkMatrix& ctm,
1569 SkBitmap* result, SkIPoint* offset) {
1570 // want explicitly our impl, so guard against a subclass of us overriding it
1571 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001572 return false;
1573 }
reed@google.com8926b162012-03-23 15:36:36 +00001574
1575 SkAutoLockPixels alp(src, !src.getTexture());
1576 if (!src.getTexture() && !src.readyToDraw()) {
1577 return false;
1578 }
1579
1580 GrPaint paint;
1581 paint.reset();
1582
1583 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1584
1585 GrTexture* texture;
1586 SkAutoCachedTexture act(this, src, sampler, &texture);
1587
1588 result->setConfig(src.config(), src.width(), src.height());
1589 GrRect rect = GrRect::MakeWH(src.width(), src.height());
1590 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1591 if (resultTexture) {
1592 result->setPixelRef(new SkGrTexturePixelRef(resultTexture))->unref();
1593 resultTexture->unref();
1594 }
reed@google.com76dd2772012-01-05 21:15:07 +00001595 return true;
1596}
1597
reed@google.comac10a2d2010-12-22 21:39:39 +00001598///////////////////////////////////////////////////////////////////////////////
1599
1600// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001601static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1602 kTriangles_PrimitiveType,
1603 kTriangleStrip_PrimitiveType,
1604 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001605};
1606
1607void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1608 int vertexCount, const SkPoint vertices[],
1609 const SkPoint texs[], const SkColor colors[],
1610 SkXfermode* xmode,
1611 const uint16_t indices[], int indexCount,
1612 const SkPaint& paint) {
1613 CHECK_SHOULD_DRAW(draw);
1614
bsalomon@google.com5782d712011-01-21 21:03:59 +00001615 GrPaint grPaint;
1616 SkAutoCachedTexture act;
1617 // we ignore the shader if texs is null.
1618 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001619 if (!skPaint2GrPaintNoShader(paint,
1620 false,
1621 NULL == colors,
1622 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001623 return;
1624 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001625 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001626 if (!skPaint2GrPaintShader(this,
1627 paint,
1628 *draw.fMatrix,
1629 NULL == colors,
1630 &act,
1631 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001632 return;
1633 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001634 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001635
1636 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001637 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001638 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1639#if 0
1640 return
1641#endif
1642 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001643 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001644
bsalomon@google.com498776a2011-08-16 19:20:44 +00001645 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1646 if (NULL != colors) {
1647 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001648 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001649 for (int i = 0; i < vertexCount; ++i) {
1650 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1651 }
1652 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001653 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001654 fContext->drawVertices(grPaint,
1655 gVertexMode2PrimitiveType[vmode],
1656 vertexCount,
1657 (GrPoint*) vertices,
1658 (GrPoint*) texs,
1659 colors,
1660 indices,
1661 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001662}
1663
1664///////////////////////////////////////////////////////////////////////////////
1665
1666static void GlyphCacheAuxProc(void* data) {
1667 delete (GrFontScaler*)data;
1668}
1669
1670static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1671 void* auxData;
1672 GrFontScaler* scaler = NULL;
1673 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1674 scaler = (GrFontScaler*)auxData;
1675 }
1676 if (NULL == scaler) {
1677 scaler = new SkGrFontScaler(cache);
1678 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1679 }
1680 return scaler;
1681}
1682
1683static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1684 SkFixed fx, SkFixed fy,
1685 const SkGlyph& glyph) {
1686 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1687
bungeman@google.com15865a72012-01-11 16:28:04 +00001688 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001689
1690 if (NULL == procs->fFontScaler) {
1691 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1692 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001693
bungeman@google.com15865a72012-01-11 16:28:04 +00001694 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1695 glyph.getSubXFixed(),
1696 glyph.getSubYFixed()),
1697 SkFixedFloorToFixed(fx),
1698 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001699 procs->fFontScaler);
1700}
1701
bsalomon@google.com5782d712011-01-21 21:03:59 +00001702SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001703
1704 // deferred allocation
1705 if (NULL == fDrawProcs) {
1706 fDrawProcs = new GrSkDrawProcs;
1707 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1708 fDrawProcs->fContext = fContext;
1709 }
1710
1711 // init our (and GL's) state
1712 fDrawProcs->fTextContext = context;
1713 fDrawProcs->fFontScaler = NULL;
1714 return fDrawProcs;
1715}
1716
1717void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1718 size_t byteLength, SkScalar x, SkScalar y,
1719 const SkPaint& paint) {
1720 CHECK_SHOULD_DRAW(draw);
1721
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001722 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001723 // this guy will just call our drawPath()
1724 draw.drawText((const char*)text, byteLength, x, y, paint);
1725 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001726 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001727
1728 GrPaint grPaint;
1729 SkAutoCachedTexture act;
1730
bsalomon@google.com84405e02012-03-05 19:57:21 +00001731 if (!skPaint2GrPaintShader(this,
1732 paint,
1733 *draw.fMatrix,
1734 true,
1735 &act,
1736 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001737 return;
1738 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001739 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1740 grPaint, draw.fExtMatrix);
1741 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001742 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1743 }
1744}
1745
1746void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1747 size_t byteLength, const SkScalar pos[],
1748 SkScalar constY, int scalarsPerPos,
1749 const SkPaint& paint) {
1750 CHECK_SHOULD_DRAW(draw);
1751
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001752 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001753 // this guy will just call our drawPath()
1754 draw.drawPosText((const char*)text, byteLength, pos, constY,
1755 scalarsPerPos, paint);
1756 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001757 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001758
1759 GrPaint grPaint;
1760 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001761 if (!skPaint2GrPaintShader(this,
1762 paint,
1763 *draw.fMatrix,
1764 true,
1765 &act,
1766 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001767 return;
1768 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001769 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1770 grPaint, draw.fExtMatrix);
1771 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001772 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1773 scalarsPerPos, paint);
1774 }
1775}
1776
1777void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1778 size_t len, const SkPath& path,
1779 const SkMatrix* m, const SkPaint& paint) {
1780 CHECK_SHOULD_DRAW(draw);
1781
1782 SkASSERT(draw.fDevice == this);
1783 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1784}
1785
1786///////////////////////////////////////////////////////////////////////////////
1787
reed@google.comf67e4cf2011-03-15 20:56:58 +00001788bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1789 if (!paint.isLCDRenderText()) {
1790 // we're cool with the paint as is
1791 return false;
1792 }
1793
1794 if (paint.getShader() ||
1795 paint.getXfermode() || // unless its srcover
1796 paint.getMaskFilter() ||
1797 paint.getRasterizer() ||
1798 paint.getColorFilter() ||
1799 paint.getPathEffect() ||
1800 paint.isFakeBoldText() ||
1801 paint.getStyle() != SkPaint::kFill_Style) {
1802 // turn off lcd
1803 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1804 flags->fHinting = paint.getHinting();
1805 return true;
1806 }
1807 // we're cool with the paint as is
1808 return false;
1809}
1810
reed@google.com75d939b2011-12-07 15:07:23 +00001811void SkGpuDevice::flush() {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001812 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001813}
1814
reed@google.comf67e4cf2011-03-15 20:56:58 +00001815///////////////////////////////////////////////////////////////////////////////
1816
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001817SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001818 const GrSamplerState* sampler,
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001819 TexType type) {
1820 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001821 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001822
bsalomon@google.come97f0852011-06-17 13:10:25 +00001823 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001824 const GrTextureDesc desc = {
1825 kRenderTarget_GrTextureFlagBit,
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001826 bitmap.width(),
1827 bitmap.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001828 SkGr::Bitmap2PixelConfig(bitmap),
1829 {0} // samples
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001830 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001831 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001832 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001833 // we know layers will only be drawn through drawDevice.
1834 // drawDevice has been made to work with content embedded in a
1835 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001836 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001837 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001838 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001839 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001840 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001841 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001842 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001843 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001844 GrContext::TextureKey key = bitmap.getGenerationID();
1845 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001846
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001847 entry = ctx->findAndLockTexture(key, bitmap.width(),
1848 bitmap.height(), sampler);
1849 if (NULL == entry.texture()) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001850 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001851 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001852 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001853 } else {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001854 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY,
1855 sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001856 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001857 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001858 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1859 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001860 }
1861 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001862 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001863}
1864
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001865void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1866 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001867}
1868
bsalomon@google.comfb309512011-11-30 14:13:48 +00001869bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1870 const GrSamplerState& sampler) const {
1871 GrContext::TextureKey key = bitmap.getGenerationID();
1872 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
1873 return this->context()->isTextureInCache(key, bitmap.width(),
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001874 bitmap.height(), &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001875
1876}
1877
1878
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001879SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1880 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001881 bool isOpaque,
1882 Usage usage) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001883 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001884 width, height, usage));
1885}
1886
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001887GrTextContext* SkGpuDevice::getTextContext() {
1888 if (NULL == fTextContext) {
1889 fTextContext = new GrDefaultTextContext();
1890 }
1891 return fTextContext;
1892}