blob: c3e495ae28dc6f0db94f926fe3a62ca94dbdea51 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
tomhudson@google.com898e7b52012-06-01 20:42:15 +00008#include "SkGpuDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00009
tomhudson@google.com898e7b52012-06-01 20:42:15 +000010#include "effects/GrGradientEffects.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000011
reed@google.comac10a2d2010-12-22 21:39:39 +000012#include "GrContext.h"
13#include "GrTextContext.h"
14
robertphillips@google.come9c04692012-06-29 00:30:13 +000015#include "SkGrTexturePixelRef.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
Scroggo97c88c22011-05-11 14:05:25 +000017#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018#include "SkDrawProcs.h"
19#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000020#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000021#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000022#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
bsalomon@google.com06cd7322012-03-30 18:45:35 +000024#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000025
26#if 0
27 extern bool (*gShouldDrawProc)();
28 #define CHECK_SHOULD_DRAW(draw) \
29 do { \
30 if (gShouldDrawProc && !gShouldDrawProc()) return; \
31 this->prepareRenderTarget(draw); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000032 GrAssert(!fNeedClear) \
reed@google.comac10a2d2010-12-22 21:39:39 +000033 } while (0)
34#else
bsalomon@google.com06cd7322012-03-30 18:45:35 +000035 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \
36 GrAssert(!fNeedClear)
reed@google.comac10a2d2010-12-22 21:39:39 +000037#endif
38
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000039// we use the same texture slot on GrPaint for bitmaps and shaders
40// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
41enum {
42 kBitmapTextureIdx = 0,
43 kShaderTextureIdx = 0
44};
45
reed@google.comcde92112011-07-06 20:00:52 +000046
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000047#define MAX_BLUR_SIGMA 4.0f
48// FIXME: This value comes from from SkBlurMaskFilter.cpp.
49// Should probably be put in a common header someplace.
50#define MAX_BLUR_RADIUS SkIntToScalar(128)
51// This constant approximates the scaling done in the software path's
52// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
53// IMHO, it actually should be 1: we blur "less" than we should do
54// according to the CSS and canvas specs, simply because Safari does the same.
55// Firefox used to do the same too, until 4.0 where they fixed it. So at some
56// point we should probably get rid of these scaling constants and rebaseline
57// all the blur tests.
58#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000059// This constant represents the screen alignment criterion in texels for
60// requiring texture domain clamping to prevent color bleeding when drawing
61// a sub region of a larger source image.
62#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000063
64#define DO_DEFERRED_CLEAR \
65 do { \
66 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000067 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000068 fNeedClear = false; \
69 } \
70 } while (false) \
71
reed@google.comac10a2d2010-12-22 21:39:39 +000072///////////////////////////////////////////////////////////////////////////////
73
bsalomon@google.com84405e02012-03-05 19:57:21 +000074class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
75public:
76 SkAutoCachedTexture() { }
77 SkAutoCachedTexture(SkGpuDevice* device,
78 const SkBitmap& bitmap,
79 const GrSamplerState* sampler,
80 GrTexture** texture) {
81 GrAssert(texture);
82 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000083 }
reed@google.comac10a2d2010-12-22 21:39:39 +000084
bsalomon@google.com84405e02012-03-05 19:57:21 +000085 ~SkAutoCachedTexture() {
86 if (fTex.texture()) {
87 fDevice->unlockCachedTexture(fTex);
88 }
reed@google.comac10a2d2010-12-22 21:39:39 +000089 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000090
91 GrTexture* set(SkGpuDevice* device,
92 const SkBitmap& bitmap,
93 const GrSamplerState* sampler) {
94 if (fTex.texture()) {
95 fDevice->unlockCachedTexture(fTex);
96 }
97 fDevice = device;
98 GrTexture* texture = (GrTexture*)bitmap.getTexture();
99 if (texture) {
100 // return the native texture
101 fTex.reset();
102 } else {
103 // look it up in our cache
104 fTex = device->lockCachedTexture(bitmap, sampler);
105 texture = fTex.texture();
106 }
107 return texture;
108 }
109
110private:
111 SkGpuDevice* fDevice;
112 GrContext::TextureCacheEntry fTex;
113};
reed@google.comac10a2d2010-12-22 21:39:39 +0000114
115///////////////////////////////////////////////////////////////////////////////
116
117bool gDoTraceDraw;
118
119struct GrSkDrawProcs : public SkDrawProcs {
120public:
121 GrContext* fContext;
122 GrTextContext* fTextContext;
123 GrFontScaler* fFontScaler; // cached in the skia glyphcache
124};
125
126///////////////////////////////////////////////////////////////////////////////
127
reed@google.comaf951c92011-06-16 19:10:39 +0000128static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
129 switch (config) {
130 case kAlpha_8_GrPixelConfig:
131 *isOpaque = false;
132 return SkBitmap::kA8_Config;
133 case kRGB_565_GrPixelConfig:
134 *isOpaque = true;
135 return SkBitmap::kRGB_565_Config;
136 case kRGBA_4444_GrPixelConfig:
137 *isOpaque = false;
138 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000139 case kSkia8888_PM_GrPixelConfig:
140 // we don't currently have a way of knowing whether
141 // a 8888 is opaque based on the config.
142 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000143 return SkBitmap::kARGB_8888_Config;
144 default:
145 *isOpaque = false;
146 return SkBitmap::kNo_Config;
147 }
148}
reed@google.comac10a2d2010-12-22 21:39:39 +0000149
reed@google.comaf951c92011-06-16 19:10:39 +0000150static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000151 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000152
153 bool isOpaque;
154 SkBitmap bitmap;
155 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
156 renderTarget->width(), renderTarget->height());
157 bitmap.setIsOpaque(isOpaque);
158 return bitmap;
159}
160
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000161SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
162: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
163 this->initFromRenderTarget(context, texture->asRenderTarget());
164}
165
reed@google.comaf951c92011-06-16 19:10:39 +0000166SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
167: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000168 this->initFromRenderTarget(context, renderTarget);
169}
170
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000171void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000172 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000173 fNeedPrepareRenderTarget = false;
174 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000175
reed@google.comaf951c92011-06-16 19:10:39 +0000176 fContext = context;
177 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000178
reed@google.comaf951c92011-06-16 19:10:39 +0000179 fTexture = NULL;
180 fRenderTarget = NULL;
181 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000182
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000183 GrAssert(NULL != renderTarget);
184 fRenderTarget = renderTarget;
185 fRenderTarget->ref();
186 // if this RT is also a texture, hold a ref on it
187 fTexture = fRenderTarget->asTexture();
188 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000189
190 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
191 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
192 // the device via accessBitmap. This external ref may outlive the device.
193 // Since textures own their render targets (but not vice-versa) we
194 // are ensuring that both objects will live as long as the pixel ref.
195 SkPixelRef* pr;
196 if (fTexture) {
robertphillips@google.come9c04692012-06-29 00:30:13 +0000197 pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000198 } else {
robertphillips@google.come9c04692012-06-29 00:30:13 +0000199 pr = new SkGrRenderTargetPixelRef(fRenderTarget);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000200 }
reed@google.comaf951c92011-06-16 19:10:39 +0000201 this->setPixelRef(pr, 0)->unref();
202}
203
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000204SkGpuDevice::SkGpuDevice(GrContext* context,
205 SkBitmap::Config config,
206 int width,
207 int height)
208 : SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000209 fNeedPrepareRenderTarget = false;
210 fDrawProcs = NULL;
211
reed@google.com7b201d22011-01-11 18:59:23 +0000212 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000213 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000214
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 fTexture = NULL;
216 fRenderTarget = NULL;
217 fNeedClear = false;
218
reed@google.comaf951c92011-06-16 19:10:39 +0000219 if (config != SkBitmap::kRGB_565_Config) {
220 config = SkBitmap::kARGB_8888_Config;
221 }
222 SkBitmap bm;
223 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000224
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000225 GrTextureDesc desc;
226 desc.fFlags = kRenderTarget_GrTextureFlagBit;
227 desc.fWidth = width;
228 desc.fHeight = height;
229 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000230
reed@google.comaf951c92011-06-16 19:10:39 +0000231 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000232
reed@google.comaf951c92011-06-16 19:10:39 +0000233 if (NULL != fTexture) {
234 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000235 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
reed@google.comaf951c92011-06-16 19:10:39 +0000237 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000238
reed@google.comaf951c92011-06-16 19:10:39 +0000239 // wrap the bitmap with a pixelref to expose our texture
robertphillips@google.come9c04692012-06-29 00:30:13 +0000240 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000241 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000242 } else {
243 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
244 width, height);
245 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000246 }
247}
248
249SkGpuDevice::~SkGpuDevice() {
250 if (fDrawProcs) {
251 delete fDrawProcs;
252 }
253
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000254 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
255 // This call gives the context a chance to relinquish it
256 fContext->setRenderTarget(NULL);
257
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000258 SkSafeUnref(fTexture);
259 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000260 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000261 GrAssert(NULL != fTexture);
262 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000263 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000264 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000265 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000266}
267
reed@google.comac10a2d2010-12-22 21:39:39 +0000268///////////////////////////////////////////////////////////////////////////////
269
270void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000271 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000272 fContext->setRenderTarget(fRenderTarget);
273 fContext->flush(true);
274 fNeedPrepareRenderTarget = true;
275}
276
277///////////////////////////////////////////////////////////////////////////////
278
bsalomon@google.comc4364992011-11-07 15:54:49 +0000279namespace {
280GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
281 switch (config8888) {
282 case SkCanvas::kNative_Premul_Config8888:
283 return kSkia8888_PM_GrPixelConfig;
284 case SkCanvas::kNative_Unpremul_Config8888:
285 return kSkia8888_UPM_GrPixelConfig;
286 case SkCanvas::kBGRA_Premul_Config8888:
287 return kBGRA_8888_PM_GrPixelConfig;
288 case SkCanvas::kBGRA_Unpremul_Config8888:
289 return kBGRA_8888_UPM_GrPixelConfig;
290 case SkCanvas::kRGBA_Premul_Config8888:
291 return kRGBA_8888_PM_GrPixelConfig;
292 case SkCanvas::kRGBA_Unpremul_Config8888:
293 return kRGBA_8888_UPM_GrPixelConfig;
294 default:
295 GrCrash("Unexpected Config8888.");
296 return kSkia8888_PM_GrPixelConfig;
297 }
298}
299}
300
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000301bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
302 int x, int y,
303 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000304 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000305 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
306 SkASSERT(!bitmap.isNull());
307 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000308
bsalomon@google.com910267d2011-11-02 20:06:25 +0000309 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000310 GrPixelConfig config;
311 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000312 return fContext->readRenderTargetPixels(fRenderTarget,
313 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000314 bitmap.width(),
315 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000316 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000317 bitmap.getPixels(),
318 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000319}
320
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000321void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
322 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000323 SkAutoLockPixels alp(bitmap);
324 if (!bitmap.readyToDraw()) {
325 return;
326 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000327
328 GrPixelConfig config;
329 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
330 config = config8888_to_gr_config(config8888);
331 } else {
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000332 config= SkGr::BitmapConfig2PixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000333 }
334
bsalomon@google.com6f379512011-11-16 20:36:03 +0000335 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
336 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000337}
338
339///////////////////////////////////////////////////////////////////////////////
340
341static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000342 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000343 const SkRegion& clipRegion,
344 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000345 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000346
347 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000348 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000349 const SkIRect& skBounds = clipRegion.getBounds();
350 GrRect bounds;
351 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
352 GrIntToScalar(skBounds.fTop),
353 GrIntToScalar(skBounds.fRight),
354 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000355 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
356 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000357 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000358}
359
360// call this ever each draw call, to ensure that the context reflects our state,
361// and not the state from some other canvas/device
362void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
363 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000364 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000365
366 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000367 SkASSERT(draw.fClipStack);
368 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000369 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000370 fNeedPrepareRenderTarget = false;
371 }
372}
373
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000374void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
375 const SkClipStack& clipStack) {
376 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
377 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000378 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000379}
380
381void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000382 const SkRegion& clip, const SkClipStack& clipStack) {
383
reed@google.comac10a2d2010-12-22 21:39:39 +0000384 fContext->setRenderTarget(fRenderTarget);
385
bsalomon@google.comd302f142011-03-03 13:54:13 +0000386 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000387
reed@google.com6f8f2922011-03-04 22:27:10 +0000388 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000389
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000390 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000391}
392
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000393SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000394 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000395 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000396}
397
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000398bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000399 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000400 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000401 return true;
402 }
403 return false;
404}
405
406///////////////////////////////////////////////////////////////////////////////
407
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000408SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
409SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
410SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
411SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
412SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
413 shader_type_mismatch);
414SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000415
bsalomon@google.com84405e02012-03-05 19:57:21 +0000416namespace {
417
418// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
419// justAlpha indicates that skPaint's alpha should be used rather than the color
420// Callers may subsequently modify the GrPaint. Setting constantColor indicates
421// that the final paint will draw the same color at every pixel. This allows
422// an optimization where the the color filter can be applied to the skPaint's
423// color once while converting to GrPain and then ignored.
424inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
425 bool justAlpha,
426 bool constantColor,
427 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000428
429 grPaint->fDither = skPaint.isDither();
430 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000431 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000432
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000433 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
434 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000435
436 SkXfermode* mode = skPaint.getXfermode();
437 if (mode) {
438 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000439 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000440#if 0
441 return false;
442#endif
443 }
444 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000445 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
446 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
447
bsalomon@google.com5782d712011-01-21 21:03:59 +0000448 if (justAlpha) {
449 uint8_t alpha = skPaint.getAlpha();
450 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000451 // justAlpha is currently set to true only if there is a texture,
452 // so constantColor should not also be true.
453 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000454 } else {
455 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000456 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000457 }
Scroggo97c88c22011-05-11 14:05:25 +0000458 SkColorFilter* colorFilter = skPaint.getColorFilter();
459 SkColor color;
460 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000461 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000462 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000463 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000464 if (!constantColor) {
465 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
466 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000467 } else {
468 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
469 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000470 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000471 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000472 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
473 grPaint->fColorMatrixEnabled = true;
474 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
475 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
476 } else {
477 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000478 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000479 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000480}
481
bsalomon@google.com84405e02012-03-05 19:57:21 +0000482// This function is similar to skPaint2GrPaintNoShader but also converts
483// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
484// be used is set on grPaint and returned in param act. constantColor has the
485// same meaning as in skPaint2GrPaintNoShader.
486inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
487 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000488 bool constantColor,
489 SkGpuDevice::SkAutoCachedTexture* act,
490 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000491
bsalomon@google.com5782d712011-01-21 21:03:59 +0000492 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000493
bsalomon@google.com5782d712011-01-21 21:03:59 +0000494 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000495 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000496 return skPaint2GrPaintNoShader(skPaint,
497 false,
498 constantColor,
499 grPaint);
500 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000501 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000502 }
503
reed@google.comac10a2d2010-12-22 21:39:39 +0000504 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000505 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000506 SkShader::TileMode tileModes[2];
507 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000508 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000509 tileModes, twoPointParams);
510
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000511 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000512 SkShader::GradientInfo info;
513 SkColor color;
514
515 info.fColors = &color;
516 info.fColorOffsets = NULL;
517 info.fColorCount = 1;
518 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
519 SkPaint copy(skPaint);
520 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000521 // modulate the paint alpha by the shader's solid color alpha
522 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
523 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000524 return skPaint2GrPaintNoShader(copy,
525 false,
526 constantColor,
527 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000528 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000529 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000530 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000531 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000532 switch (bmptype) {
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000533 case SkShader::kRadial_BitmapType:
534 sampler->setCustomStage(new GrRadialGradient())->unref();
535 sampler->setFilter(GrSamplerState::kBilinear_Filter);
536 break;
537 case SkShader::kSweep_BitmapType:
538 sampler->setCustomStage(new GrSweepGradient())->unref();
539 sampler->setFilter(GrSamplerState::kBilinear_Filter);
540 break;
541 case SkShader::kTwoPointRadial_BitmapType:
542 sampler->setCustomStage(new
543 GrRadial2Gradient(twoPointParams[0],
544 twoPointParams[1],
545 twoPointParams[2] < 0))->unref();
546 sampler->setFilter(GrSamplerState::kBilinear_Filter);
547 break;
548 default:
549 if (skPaint.isFilterBitmap()) {
550 sampler->setFilter(GrSamplerState::kBilinear_Filter);
551 } else {
552 sampler->setFilter(GrSamplerState::kNearest_Filter);
553 }
554 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000555 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000556 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
557 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000558
bsalomon@google.com84405e02012-03-05 19:57:21 +0000559 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000560 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000561 SkDebugf("Couldn't convert bitmap to texture.\n");
562 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000563 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000564 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000565
566 // since our texture coords will be in local space, we wack the texture
567 // matrix to map them back into 0...1 before we load it
568 SkMatrix localM;
569 if (shader->getLocalMatrix(&localM)) {
570 SkMatrix inverse;
571 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000572 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000573 }
574 }
575 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000576 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
577 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000578 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000579 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000580 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000581 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000582 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000583
584 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000585}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000586}
reed@google.comac10a2d2010-12-22 21:39:39 +0000587
588///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000589void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000590 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000591}
592
reed@google.comac10a2d2010-12-22 21:39:39 +0000593void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
594 CHECK_SHOULD_DRAW(draw);
595
bsalomon@google.com5782d712011-01-21 21:03:59 +0000596 GrPaint grPaint;
597 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000598 if (!skPaint2GrPaintShader(this,
599 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000600 true,
601 &act,
602 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000603 return;
604 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000605
606 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000607}
608
609// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000610static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000611 kPoints_GrPrimitiveType,
612 kLines_GrPrimitiveType,
613 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000614};
615
616void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000617 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000618 CHECK_SHOULD_DRAW(draw);
619
620 SkScalar width = paint.getStrokeWidth();
621 if (width < 0) {
622 return;
623 }
624
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000625 // we only handle hairlines and paints without path effects or mask filters,
626 // else we let the SkDraw call our drawPath()
627 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000628 draw.drawPoints(mode, count, pts, paint, true);
629 return;
630 }
631
bsalomon@google.com5782d712011-01-21 21:03:59 +0000632 GrPaint grPaint;
633 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000634 if (!skPaint2GrPaintShader(this,
635 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000636 true,
637 &act,
638 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000639 return;
640 }
641
bsalomon@google.com5782d712011-01-21 21:03:59 +0000642 fContext->drawVertices(grPaint,
643 gPointMode2PrimtiveType[mode],
644 count,
645 (GrPoint*)pts,
646 NULL,
647 NULL,
648 NULL,
649 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000650}
651
reed@google.comc9aa5872011-04-05 21:05:37 +0000652///////////////////////////////////////////////////////////////////////////////
653
reed@google.comac10a2d2010-12-22 21:39:39 +0000654void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
655 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000656 CHECK_SHOULD_DRAW(draw);
657
bungeman@google.com79bd8772011-07-18 15:34:08 +0000658 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000659 SkScalar width = paint.getStrokeWidth();
660
661 /*
662 We have special code for hairline strokes, miter-strokes, and fills.
663 Anything else we just call our path code.
664 */
665 bool usePath = doStroke && width > 0 &&
666 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000667 // another two reasons we might need to call drawPath...
668 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000669 usePath = true;
670 }
reed@google.com67db6642011-05-26 11:46:35 +0000671 // until we aa rotated rects...
672 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
673 usePath = true;
674 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000675 // small miter limit means right angles show bevel...
676 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
677 paint.getStrokeMiter() < SK_ScalarSqrt2)
678 {
679 usePath = true;
680 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000681 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000682 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
683 usePath = true;
684 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000685
686 if (usePath) {
687 SkPath path;
688 path.addRect(rect);
689 this->drawPath(draw, path, paint, NULL, true);
690 return;
691 }
692
693 GrPaint grPaint;
694 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000695 if (!skPaint2GrPaintShader(this,
696 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000697 true,
698 &act,
699 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000700 return;
701 }
reed@google.com20efde72011-05-09 17:00:02 +0000702 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000703}
704
reed@google.com69302852011-02-16 18:08:07 +0000705#include "SkMaskFilter.h"
706#include "SkBounder.h"
707
bsalomon@google.com85003222012-03-28 14:44:37 +0000708///////////////////////////////////////////////////////////////////////////////
709
710// helpers for applying mask filters
711namespace {
712
713GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000714 switch (fillType) {
715 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000716 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000717 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000718 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000719 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000720 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000721 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000722 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000723 default:
724 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000725 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000726 }
727}
728
bsalomon@google.com85003222012-03-28 14:44:37 +0000729// We prefer to blur small rect with small radius via CPU.
730#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
731#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
732inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
733 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
734 rect.height() <= MIN_GPU_BLUR_SIZE &&
735 radius <= MIN_GPU_BLUR_RADIUS) {
736 return true;
737 }
738 return false;
739}
740
741bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
742 SkMaskFilter* filter, const SkMatrix& matrix,
743 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000744 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000745#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000746 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000747#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000748 SkMaskFilter::BlurInfo info;
749 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000750 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000751 return false;
752 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000753 SkScalar radius = info.fIgnoreTransform ? info.fRadius
754 : matrix.mapRadius(info.fRadius);
755 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000756 if (radius <= 0) {
757 return false;
758 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000759
760 SkRect srcRect = path.getBounds();
761 if (shouldDrawBlurWithCPU(srcRect, radius)) {
762 return false;
763 }
764
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000765 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000766 float sigma3 = sigma * 3.0f;
767
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000768 SkRect clipRect;
769 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000770
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000771 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000772 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
773 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000774 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000775 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000776 SkIRect finalIRect;
777 finalRect.roundOut(&finalIRect);
778 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000779 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000780 }
781 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000782 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000783 }
784 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000785 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000786 GrTextureDesc desc;
787 desc.fFlags = kRenderTarget_GrTextureFlagBit;
788 desc.fWidth = SkScalarCeilToInt(srcRect.width());
789 desc.fHeight = SkScalarCeilToInt(srcRect.height());
790 // We actually only need A8, but it often isn't supported as a
791 // render target so default to RGBA_8888
792 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000793
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000794 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
795 desc.fConfig = kAlpha_8_GrPixelConfig;
796 }
797
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000798 GrAutoScratchTexture pathEntry(context, desc);
799 GrTexture* pathTexture = pathEntry.texture();
800 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000801 return false;
802 }
803 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000804 // Once this code moves into GrContext, this should be changed to use
805 // an AutoClipRestore.
806 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000807 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000808 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000810 GrPaint tempPaint;
811 tempPaint.reset();
812
813 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000814 tempPaint.fAntiAlias = grp->fAntiAlias;
815 if (tempPaint.fAntiAlias) {
816 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
817 // blend coeff of zero requires dual source blending support in order
818 // to properly blend partially covered pixels. This means the AA
819 // code path may not be taken. So we use a dst blend coeff of ISA. We
820 // could special case AA draws to a dst surface with known alpha=0 to
821 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000822 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
823 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000824 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000825 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000826 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000827
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000828 GrAutoScratchTexture temp1, temp2;
829 // If we're doing a normal blur, we can clobber the pathTexture in the
830 // gaussianBlur. Otherwise, we need to save it for later compositing.
831 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000832 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
833 &temp1,
834 isNormalBlur ? NULL : &temp2,
835 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000836
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000837 if (!isNormalBlur) {
838 GrPaint paint;
839 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000840 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000841 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
842 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000843 // Blend pathTexture over blurTexture.
844 context->setRenderTarget(blurTexture->asRenderTarget());
845 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000846 if (SkMaskFilter::kInner_BlurType == blurType) {
847 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000848 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
849 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000850 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
851 // solid: dst = src + dst - src * dst
852 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000853 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
854 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000855 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
856 // outer: dst = dst * (1 - src)
857 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000858 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
859 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000860 }
861 context->drawRect(paint, srcRect);
862 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000863 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000864 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000865
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000866 if (grp->hasTextureOrMask()) {
867 GrMatrix inverse;
868 if (!matrix.invert(&inverse)) {
869 return false;
870 }
871 grp->preConcatActiveSamplerMatrices(inverse);
872 }
873
874 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
875 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000876 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000877 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000878 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000879
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000880 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
881 -finalRect.fTop);
882 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
883 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000884 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000885 return true;
886}
887
bsalomon@google.com85003222012-03-28 14:44:37 +0000888bool drawWithMaskFilter(GrContext* context, const SkPath& path,
889 SkMaskFilter* filter, const SkMatrix& matrix,
890 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000891 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000892 SkMask srcM, dstM;
893
894 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000895 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000896 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000897 return false;
898 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000899 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000900
901 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
902 return false;
903 }
904 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000905 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000906
907 if (clip.quickReject(dstM.fBounds)) {
908 return false;
909 }
910 if (bounder && !bounder->doIRect(dstM.fBounds)) {
911 return false;
912 }
913
914 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
915 // the current clip (and identity matrix) and grpaint settings
916
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000917 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000918 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000919
reed@google.com0c219b62011-02-16 21:31:18 +0000920 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000921
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000922 GrTextureDesc desc;
923 desc.fWidth = dstM.fBounds.width();
924 desc.fHeight = dstM.fBounds.height();
925 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000926
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000927 GrAutoScratchTexture ast(context, desc);
928 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000929
reed@google.com69302852011-02-16 18:08:07 +0000930 if (NULL == texture) {
931 return false;
932 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000933 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000934 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000935
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000936 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
937 grp->preConcatActiveSamplerMatrices(ivm);
938 }
939
940 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
941 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000942 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000943 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000944 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000945
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000946 GrRect d;
947 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000948 GrIntToScalar(dstM.fBounds.fTop),
949 GrIntToScalar(dstM.fBounds.fRight),
950 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000951
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000952 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
953 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
954 -dstM.fBounds.fTop*SK_Scalar1);
955 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000956 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000957 return true;
958}
reed@google.com69302852011-02-16 18:08:07 +0000959
bsalomon@google.com85003222012-03-28 14:44:37 +0000960}
961
962///////////////////////////////////////////////////////////////////////////////
963
reed@google.com0c219b62011-02-16 21:31:18 +0000964void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000965 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000966 bool pathIsMutable) {
967 CHECK_SHOULD_DRAW(draw);
968
reed@google.comfe626382011-09-21 13:50:35 +0000969 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000970
bsalomon@google.com5782d712011-01-21 21:03:59 +0000971 GrPaint grPaint;
972 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000973 if (!skPaint2GrPaintShader(this,
974 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000975 true,
976 &act,
977 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000978 return;
979 }
980
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000981 // can we cheat, and threat a thin stroke as a hairline w/ coverage
982 // if we can, we draw lots faster (raster device does this same test)
983 SkScalar hairlineCoverage;
984 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
985 doFill = false;
986 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
987 grPaint.fCoverage);
988 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000989
reed@google.comfe626382011-09-21 13:50:35 +0000990 // If we have a prematrix, apply it to the path, optimizing for the case
991 // where the original path can in fact be modified in place (even though
992 // its parameter type is const).
993 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
994 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000995
996 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +0000997 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +0000998
reed@google.come3445642011-02-16 23:20:39 +0000999 if (!pathIsMutable) {
1000 result = &tmpPath;
1001 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001002 }
reed@google.come3445642011-02-16 23:20:39 +00001003 // should I push prePathMatrix on our MV stack temporarily, instead
1004 // of applying it here? See SkDraw.cpp
1005 pathPtr->transform(*prePathMatrix, result);
1006 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001007 }
reed@google.com0c219b62011-02-16 21:31:18 +00001008 // at this point we're done with prePathMatrix
1009 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001010
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001011 if (paint.getPathEffect() ||
1012 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001013 // it is safe to use tmpPath here, even if we already used it for the
1014 // prepathmatrix, since getFillPath can take the same object for its
1015 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001016 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001017 pathPtr = &tmpPath;
1018 }
1019
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001020 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001021 // avoid possibly allocating a new path in transform if we can
1022 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1023
1024 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001025 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001026 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001027 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001028 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001029 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001030 &grPaint, pathFillType)) {
1031 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1032 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001033 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001034 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001035 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001036 }
reed@google.com69302852011-02-16 18:08:07 +00001037 return;
1038 }
reed@google.com69302852011-02-16 18:08:07 +00001039
bsalomon@google.com47059542012-06-06 20:51:20 +00001040 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001041
reed@google.com0c219b62011-02-16 21:31:18 +00001042 if (doFill) {
1043 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001044 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001045 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001046 break;
1047 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001048 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001049 break;
1050 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001051 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 break;
1053 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001054 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001055 break;
1056 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001057 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 return;
1059 }
1060 }
1061
reed@google.com07f3ee12011-05-16 17:21:57 +00001062 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001063}
1064
bsalomon@google.comfb309512011-11-30 14:13:48 +00001065namespace {
1066
1067inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1068 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1069 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1070 return tilesX * tilesY;
1071}
1072
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001073inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001074 const SkIRect* srcRectPtr,
1075 int maxTextureSize) {
1076 static const int kSmallTileSize = 1 << 10;
1077 if (maxTextureSize <= kSmallTileSize) {
1078 return maxTextureSize;
1079 }
1080
1081 size_t maxTexTotalTileSize;
1082 size_t smallTotalTileSize;
1083
1084 if (NULL == srcRectPtr) {
1085 int w = bitmap.width();
1086 int h = bitmap.height();
1087 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1088 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1089 } else {
1090 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1091 srcRectPtr->fTop,
1092 srcRectPtr->fRight,
1093 srcRectPtr->fBottom,
1094 maxTextureSize);
1095 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1096 srcRectPtr->fTop,
1097 srcRectPtr->fRight,
1098 srcRectPtr->fBottom,
1099 kSmallTileSize);
1100 }
1101 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1102 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1103
1104 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1105 return kSmallTileSize;
1106 } else {
1107 return maxTextureSize;
1108 }
1109}
1110}
1111
1112bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1113 const GrSamplerState& sampler,
1114 const SkIRect* srcRectPtr,
1115 int* tileSize) const {
1116 SkASSERT(NULL != tileSize);
1117
1118 // if bitmap is explictly texture backed then just use the texture
1119 if (NULL != bitmap.getTexture()) {
1120 return false;
1121 }
1122 // if it's larger than the max texture size, then we have no choice but
1123 // tiling
1124 const int maxTextureSize = fContext->getMaxTextureSize();
1125 if (bitmap.width() > maxTextureSize ||
1126 bitmap.height() > maxTextureSize) {
1127 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1128 return true;
1129 }
1130 // if we are going to have to draw the whole thing, then don't tile
1131 if (NULL == srcRectPtr) {
1132 return false;
1133 }
1134 // if the entire texture is already in our cache then no reason to tile it
1135 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1136 return false;
1137 }
1138
1139 // At this point we know we could do the draw by uploading the entire bitmap
1140 // as a texture. However, if the texture would be large compared to the
1141 // cache size and we don't require most of it for this draw then tile to
1142 // reduce the amount of upload and cache spill.
1143
1144 // assumption here is that sw bitmap size is a good proxy for its size as
1145 // a texture
1146 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001147 size_t cacheSize;
1148 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001149 if (bmpSize < cacheSize / 2) {
1150 return false;
1151 }
1152
1153 SkFixed fracUsed =
1154 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1155 (srcRectPtr->height() << 16) / bitmap.height());
1156 if (fracUsed <= SK_FixedHalf) {
1157 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1158 return true;
1159 } else {
1160 return false;
1161 }
1162}
1163
reed@google.comac10a2d2010-12-22 21:39:39 +00001164void SkGpuDevice::drawBitmap(const SkDraw& draw,
1165 const SkBitmap& bitmap,
1166 const SkIRect* srcRectPtr,
1167 const SkMatrix& m,
1168 const SkPaint& paint) {
1169 CHECK_SHOULD_DRAW(draw);
1170
1171 SkIRect srcRect;
1172 if (NULL == srcRectPtr) {
1173 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1174 } else {
1175 srcRect = *srcRectPtr;
1176 }
1177
junov@google.comd935cfb2011-06-27 20:48:23 +00001178 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001179 // Convert the bitmap to a shader so that the rect can be drawn
1180 // through drawRect, which supports mask filters.
1181 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001182 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001183 if (srcRectPtr) {
1184 if (!bitmap.extractSubset(&tmp, srcRect)) {
1185 return; // extraction failed
1186 }
1187 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001188 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001189 }
1190 SkPaint paintWithTexture(paint);
1191 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1192 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001193 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001194 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001195
junov@google.com1d329782011-07-28 20:10:09 +00001196 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001197 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001198 // also affect the behavior of the mask filter.
1199 SkMatrix drawMatrix;
1200 drawMatrix.setConcat(*draw.fMatrix, m);
1201 SkDraw transformedDraw(draw);
1202 transformedDraw.fMatrix = &drawMatrix;
1203
1204 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1205
junov@google.comd935cfb2011-06-27 20:48:23 +00001206 return;
1207 }
1208
bsalomon@google.com5782d712011-01-21 21:03:59 +00001209 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001210 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001211 return;
1212 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001213 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001214 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001215 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001216 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001217 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001218 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001219
bsalomon@google.comfb309512011-11-30 14:13:48 +00001220 int tileSize;
1221 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1222 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001223 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001224 return;
1225 }
1226
1227 // undo the translate done by SkCanvas
1228 int DX = SkMax32(0, srcRect.fLeft);
1229 int DY = SkMax32(0, srcRect.fTop);
1230 // compute clip bounds in local coordinates
1231 SkIRect clipRect;
1232 {
1233 SkRect r;
1234 r.set(draw.fClip->getBounds());
1235 SkMatrix matrix, inverse;
1236 matrix.setConcat(*draw.fMatrix, m);
1237 if (!matrix.invert(&inverse)) {
1238 return;
1239 }
1240 inverse.mapRect(&r);
1241 r.roundOut(&clipRect);
1242 // apply the canvas' translate to our local clip
1243 clipRect.offset(DX, DY);
1244 }
1245
bsalomon@google.comfb309512011-11-30 14:13:48 +00001246 int nx = bitmap.width() / tileSize;
1247 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001248 for (int x = 0; x <= nx; x++) {
1249 for (int y = 0; y <= ny; y++) {
1250 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001251 tileR.set(x * tileSize, y * tileSize,
1252 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001253 if (!SkIRect::Intersects(tileR, clipRect)) {
1254 continue;
1255 }
1256
1257 SkIRect srcR = tileR;
1258 if (!srcR.intersect(srcRect)) {
1259 continue;
1260 }
1261
1262 SkBitmap tmpB;
1263 if (bitmap.extractSubset(&tmpB, tileR)) {
1264 // now offset it to make it "local" to our tmp bitmap
1265 srcR.offset(-tileR.fLeft, -tileR.fTop);
1266
1267 SkMatrix tmpM(m);
1268 {
1269 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1270 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1271 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1272 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001273 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001274 }
1275 }
1276 }
1277}
1278
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001279namespace {
1280
1281bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1282 // detect pixel disalignment
1283 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1284 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1285 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1286 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1287 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1288 COLOR_BLEED_TOLERANCE &&
1289 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1290 COLOR_BLEED_TOLERANCE) {
1291 return true;
1292 }
1293 return false;
1294}
1295
1296bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1297 const SkMatrix& m) {
1298 // Only gets called if hasAlignedSamples returned false.
1299 // So we can assume that sampling is axis aligned but not texel aligned.
1300 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1301 SkRect innerSrcRect(srcRect), innerTransformedRect,
1302 outerTransformedRect(transformedRect);
1303 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1304 m.mapRect(&innerTransformedRect, innerSrcRect);
1305
1306 // The gap between outerTransformedRect and innerTransformedRect
1307 // represents the projection of the source border area, which is
1308 // problematic for color bleeding. We must check whether any
1309 // destination pixels sample the border area.
1310 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1311 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1312 SkIRect outer, inner;
1313 outerTransformedRect.round(&outer);
1314 innerTransformedRect.round(&inner);
1315 // If the inner and outer rects round to the same result, it means the
1316 // border does not overlap any pixel centers. Yay!
1317 return inner != outer;
1318}
1319
1320} // unnamed namespace
1321
reed@google.comac10a2d2010-12-22 21:39:39 +00001322/*
1323 * This is called by drawBitmap(), which has to handle images that may be too
1324 * large to be represented by a single texture.
1325 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001326 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1327 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001328 */
1329void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1330 const SkBitmap& bitmap,
1331 const SkIRect& srcRect,
1332 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001333 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001334 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1335 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001336
reed@google.com9c49bc32011-07-07 13:42:37 +00001337 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001338 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001339 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001340 return;
1341 }
1342
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001343 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001344
1345 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1346 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001347 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001348
1349 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001350 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001351 if (NULL == texture) {
1352 return;
1353 }
1354
bsalomon@google.com452943d2011-10-31 17:37:14 +00001355 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001356
reed@google.com20efde72011-05-09 17:00:02 +00001357 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1358 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001359 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001360 float wInv = 1.f / bitmap.width();
1361 float hInv = 1.f / bitmap.height();
1362 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1363 SkFloatToScalar(srcRect.fTop * hInv),
1364 SkFloatToScalar(srcRect.fRight * wInv),
1365 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001366
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001367 bool needsTextureDomain = false;
1368 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1369 {
1370 // Need texture domain if drawing a sub rect.
1371 needsTextureDomain = srcRect.width() < bitmap.width() ||
1372 srcRect.height() < bitmap.height();
1373 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1374 // sampling is axis-aligned
1375 GrRect floatSrcRect, transformedRect;
1376 floatSrcRect.set(srcRect);
1377 SkMatrix srcToDeviceMatrix(m);
1378 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1379 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1380
1381 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1382 // Samples are texel-aligned, so filtering is futile
1383 sampler->setFilter(GrSamplerState::kNearest_Filter);
1384 needsTextureDomain = false;
1385 } else {
1386 needsTextureDomain = needsTextureDomain &&
1387 mayColorBleed(floatSrcRect, transformedRect, m);
1388 }
1389 }
1390 }
1391
1392 GrRect textureDomain = GrRect::MakeEmpty();
1393
1394 if (needsTextureDomain) {
1395 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001396 GrScalar left, top, right, bottom;
1397 if (srcRect.width() > 1) {
1398 GrScalar border = GR_ScalarHalf / bitmap.width();
1399 left = paintRect.left() + border;
1400 right = paintRect.right() - border;
1401 } else {
1402 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1403 }
1404 if (srcRect.height() > 1) {
1405 GrScalar border = GR_ScalarHalf / bitmap.height();
1406 top = paintRect.top() + border;
1407 bottom = paintRect.bottom() - border;
1408 } else {
1409 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1410 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001411 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001412 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001413 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001414
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001415 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001416}
1417
reed@google.com8926b162012-03-23 15:36:36 +00001418static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1419 SkImageFilter* filter, const GrRect& rect) {
1420 GrAssert(filter);
1421
1422 SkSize blurSize;
1423 SkISize radius;
1424
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001425 GrTextureDesc desc;
1426 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1427 desc.fWidth = SkScalarCeilToInt(rect.width());
1428 desc.fHeight = SkScalarCeilToInt(rect.height());
1429 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
reed@google.com8926b162012-03-23 15:36:36 +00001430
1431 if (filter->asABlur(&blurSize)) {
1432 GrAutoScratchTexture temp1, temp2;
1433 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1434 blurSize.width(),
1435 blurSize.height());
1436 texture->ref();
1437 } else if (filter->asADilate(&radius)) {
1438 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1439 texture = context->applyMorphology(texture, rect,
1440 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001441 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001442 radius);
1443 texture->ref();
1444 } else if (filter->asAnErode(&radius)) {
1445 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1446 texture = context->applyMorphology(texture, rect,
1447 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001448 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001449 radius);
1450 texture->ref();
1451 }
1452 return texture;
1453}
1454
reed@google.comac10a2d2010-12-22 21:39:39 +00001455void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1456 int left, int top, const SkPaint& paint) {
1457 CHECK_SHOULD_DRAW(draw);
1458
reed@google.com8926b162012-03-23 15:36:36 +00001459 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001460 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1461 return;
1462 }
1463
reed@google.com76dd2772012-01-05 21:15:07 +00001464 int w = bitmap.width();
1465 int h = bitmap.height();
1466
bsalomon@google.com5782d712011-01-21 21:03:59 +00001467 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001468 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001469 return;
1470 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001471
bsalomon@google.com5782d712011-01-21 21:03:59 +00001472 GrAutoMatrix avm(fContext, GrMatrix::I());
1473
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001474 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001475
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001476 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001477 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001478 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001479 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001480
reed@google.com8926b162012-03-23 15:36:36 +00001481 SkImageFilter* filter = paint.getImageFilter();
1482 if (NULL != filter) {
1483 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001484 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001485 if (filteredTexture) {
1486 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1487 texture = filteredTexture;
1488 filteredTexture->unref();
1489 }
reed@google.com76dd2772012-01-05 21:15:07 +00001490 }
reed@google.com8926b162012-03-23 15:36:36 +00001491
bsalomon@google.com5782d712011-01-21 21:03:59 +00001492 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001493 GrRect::MakeXYWH(GrIntToScalar(left),
1494 GrIntToScalar(top),
1495 GrIntToScalar(w),
1496 GrIntToScalar(h)),
1497 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1498 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001499}
1500
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001501void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001502 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001503 // clear of the source device must occur before CHECK_SHOULD_DRAW
1504 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1505 if (dev->fNeedClear) {
1506 // TODO: could check here whether we really need to draw at all
1507 dev->clear(0x0);
1508 }
1509
reed@google.comac10a2d2010-12-22 21:39:39 +00001510 CHECK_SHOULD_DRAW(draw);
1511
bsalomon@google.com5782d712011-01-21 21:03:59 +00001512 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001513 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001514 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001515 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001516 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001517
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001518 GrTexture* devTex = grPaint.getTexture(0);
1519 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001520
reed@google.com8926b162012-03-23 15:36:36 +00001521 SkImageFilter* filter = paint.getImageFilter();
1522 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001523 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1524 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001525 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1526 rect);
1527 if (filteredTexture) {
1528 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1529 devTex = filteredTexture;
1530 filteredTexture->unref();
1531 }
1532 }
1533
bsalomon@google.com5782d712011-01-21 21:03:59 +00001534 const SkBitmap& bm = dev->accessBitmap(false);
1535 int w = bm.width();
1536 int h = bm.height();
1537
1538 GrAutoMatrix avm(fContext, GrMatrix::I());
1539
bsalomon@google.com97912912011-12-06 16:30:36 +00001540 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001541
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001542 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1543 GrIntToScalar(y),
1544 GrIntToScalar(w),
1545 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001546
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001547 // The device being drawn may not fill up its texture (saveLayer uses
1548 // the approximate ).
1549 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1550 GR_Scalar1 * h / devTex->height());
1551
1552 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001553}
1554
reed@google.com8926b162012-03-23 15:36:36 +00001555bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001556 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001557 SkISize radius;
1558 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001559 return false;
1560 }
reed@google.com8926b162012-03-23 15:36:36 +00001561 return true;
1562}
1563
1564bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1565 const SkMatrix& ctm,
1566 SkBitmap* result, SkIPoint* offset) {
1567 // want explicitly our impl, so guard against a subclass of us overriding it
1568 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001569 return false;
1570 }
reed@google.com8926b162012-03-23 15:36:36 +00001571
1572 SkAutoLockPixels alp(src, !src.getTexture());
1573 if (!src.getTexture() && !src.readyToDraw()) {
1574 return false;
1575 }
1576
1577 GrPaint paint;
1578 paint.reset();
1579
1580 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1581
1582 GrTexture* texture;
1583 SkAutoCachedTexture act(this, src, sampler, &texture);
1584
1585 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001586 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1587 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001588 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1589 if (resultTexture) {
robertphillips@google.come9c04692012-06-29 00:30:13 +00001590 result->setPixelRef(new SkGrTexturePixelRef(resultTexture))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001591 resultTexture->unref();
1592 }
reed@google.com76dd2772012-01-05 21:15:07 +00001593 return true;
1594}
1595
reed@google.comac10a2d2010-12-22 21:39:39 +00001596///////////////////////////////////////////////////////////////////////////////
1597
1598// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001599static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001600 kTriangles_GrPrimitiveType,
1601 kTriangleStrip_GrPrimitiveType,
1602 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001603};
1604
1605void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1606 int vertexCount, const SkPoint vertices[],
1607 const SkPoint texs[], const SkColor colors[],
1608 SkXfermode* xmode,
1609 const uint16_t indices[], int indexCount,
1610 const SkPaint& paint) {
1611 CHECK_SHOULD_DRAW(draw);
1612
bsalomon@google.com5782d712011-01-21 21:03:59 +00001613 GrPaint grPaint;
1614 SkAutoCachedTexture act;
1615 // we ignore the shader if texs is null.
1616 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001617 if (!skPaint2GrPaintNoShader(paint,
1618 false,
1619 NULL == colors,
1620 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001621 return;
1622 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001623 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001624 if (!skPaint2GrPaintShader(this,
1625 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001626 NULL == colors,
1627 &act,
1628 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001629 return;
1630 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001631 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001632
1633 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001634 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001635 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1636#if 0
1637 return
1638#endif
1639 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001640 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001641
bsalomon@google.com498776a2011-08-16 19:20:44 +00001642 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1643 if (NULL != colors) {
1644 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001645 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001646 for (int i = 0; i < vertexCount; ++i) {
1647 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1648 }
1649 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001650 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001651 fContext->drawVertices(grPaint,
1652 gVertexMode2PrimitiveType[vmode],
1653 vertexCount,
1654 (GrPoint*) vertices,
1655 (GrPoint*) texs,
1656 colors,
1657 indices,
1658 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001659}
1660
1661///////////////////////////////////////////////////////////////////////////////
1662
1663static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001664 GrFontScaler* scaler = (GrFontScaler*)data;
1665 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001666}
1667
1668static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1669 void* auxData;
1670 GrFontScaler* scaler = NULL;
1671 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1672 scaler = (GrFontScaler*)auxData;
1673 }
1674 if (NULL == scaler) {
1675 scaler = new SkGrFontScaler(cache);
1676 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1677 }
1678 return scaler;
1679}
1680
1681static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1682 SkFixed fx, SkFixed fy,
1683 const SkGlyph& glyph) {
1684 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1685
bungeman@google.com15865a72012-01-11 16:28:04 +00001686 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001687
1688 if (NULL == procs->fFontScaler) {
1689 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1690 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001691
bungeman@google.com15865a72012-01-11 16:28:04 +00001692 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1693 glyph.getSubXFixed(),
1694 glyph.getSubYFixed()),
1695 SkFixedFloorToFixed(fx),
1696 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001697 procs->fFontScaler);
1698}
1699
bsalomon@google.com5782d712011-01-21 21:03:59 +00001700SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001701
1702 // deferred allocation
1703 if (NULL == fDrawProcs) {
1704 fDrawProcs = new GrSkDrawProcs;
1705 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1706 fDrawProcs->fContext = fContext;
1707 }
1708
1709 // init our (and GL's) state
1710 fDrawProcs->fTextContext = context;
1711 fDrawProcs->fFontScaler = NULL;
1712 return fDrawProcs;
1713}
1714
1715void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1716 size_t byteLength, SkScalar x, SkScalar y,
1717 const SkPaint& paint) {
1718 CHECK_SHOULD_DRAW(draw);
1719
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001720 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001721 // this guy will just call our drawPath()
1722 draw.drawText((const char*)text, byteLength, x, y, paint);
1723 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001724 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001725
1726 GrPaint grPaint;
1727 SkAutoCachedTexture act;
1728
bsalomon@google.com84405e02012-03-05 19:57:21 +00001729 if (!skPaint2GrPaintShader(this,
1730 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001731 true,
1732 &act,
1733 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001734 return;
1735 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001736 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1737 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001738 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1739 }
1740}
1741
1742void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1743 size_t byteLength, const SkScalar pos[],
1744 SkScalar constY, int scalarsPerPos,
1745 const SkPaint& paint) {
1746 CHECK_SHOULD_DRAW(draw);
1747
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001748 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001749 // this guy will just call our drawPath()
1750 draw.drawPosText((const char*)text, byteLength, pos, constY,
1751 scalarsPerPos, paint);
1752 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001753 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001754
1755 GrPaint grPaint;
1756 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001757 if (!skPaint2GrPaintShader(this,
1758 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001759 true,
1760 &act,
1761 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001762 return;
1763 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001764 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1765 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001766 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1767 scalarsPerPos, paint);
1768 }
1769}
1770
1771void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1772 size_t len, const SkPath& path,
1773 const SkMatrix* m, const SkPaint& paint) {
1774 CHECK_SHOULD_DRAW(draw);
1775
1776 SkASSERT(draw.fDevice == this);
1777 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1778}
1779
1780///////////////////////////////////////////////////////////////////////////////
1781
reed@google.comf67e4cf2011-03-15 20:56:58 +00001782bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1783 if (!paint.isLCDRenderText()) {
1784 // we're cool with the paint as is
1785 return false;
1786 }
1787
1788 if (paint.getShader() ||
1789 paint.getXfermode() || // unless its srcover
1790 paint.getMaskFilter() ||
1791 paint.getRasterizer() ||
1792 paint.getColorFilter() ||
1793 paint.getPathEffect() ||
1794 paint.isFakeBoldText() ||
1795 paint.getStyle() != SkPaint::kFill_Style) {
1796 // turn off lcd
1797 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1798 flags->fHinting = paint.getHinting();
1799 return true;
1800 }
1801 // we're cool with the paint as is
1802 return false;
1803}
1804
reed@google.com75d939b2011-12-07 15:07:23 +00001805void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001806 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001807 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001808}
1809
reed@google.comf67e4cf2011-03-15 20:56:58 +00001810///////////////////////////////////////////////////////////////////////////////
1811
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001812SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1813 const SkBitmap& bitmap,
1814 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001815 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001816 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001817
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001818 if (!bitmap.isVolatile()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001819 uint64_t key = bitmap.getGenerationID();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001820 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001821
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001822 GrTextureDesc desc;
1823 desc.fWidth = bitmap.width();
1824 desc.fHeight = bitmap.height();
1825 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
robertphillips@google.com56f22442012-06-08 14:21:26 +00001826 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001827
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001828 entry = ctx->findAndLockTexture(desc, sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001829 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001830 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1831 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001832 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001833 } else {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001834 entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001835 sampler, bitmap);
1836 }
1837 if (NULL == entry.texture()) {
1838 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1839 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001840 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001841 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001842}
1843
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001844void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1845 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001846}
1847
bsalomon@google.comfb309512011-11-30 14:13:48 +00001848bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1849 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001850 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001851 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001852
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001853 GrTextureDesc desc;
1854 desc.fWidth = bitmap.width();
1855 desc.fHeight = bitmap.height();
1856 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
1857 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001858
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001859 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001860}
1861
1862
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001863SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1864 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001865 bool isOpaque,
1866 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001867 GrTextureDesc desc;
1868 desc.fConfig = fRenderTarget->config();
1869 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1870 desc.fWidth = width;
1871 desc.fHeight = height;
1872 desc.fSampleCnt = fRenderTarget->numSamples();
1873
1874 GrContext::TextureCacheEntry cacheEntry;
1875 GrTexture* texture;
1876 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001877 // Skia's convention is to only clear a device if it is non-opaque.
1878 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001879
1880#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1881 // layers are never draw in repeat modes, so we can request an approx
1882 // match and ignore any padding.
1883 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1884 GrContext::kApprox_ScratchTexMatch :
1885 GrContext::kExact_ScratchTexMatch;
1886 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1887 texture = cacheEntry.texture();
1888#else
1889 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1890 texture = tunref.get();
1891#endif
1892 if (texture) {
1893 return SkNEW_ARGS(SkGpuDevice,(fContext,
1894 texture,
1895 cacheEntry,
1896 needClear));
1897 } else {
1898 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1899 width, height);
1900 return NULL;
1901 }
1902}
1903
1904SkGpuDevice::SkGpuDevice(GrContext* context,
1905 GrTexture* texture,
1906 TexCache cacheEntry,
1907 bool needClear)
1908 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1909 GrAssert(texture && texture->asRenderTarget());
1910 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1911 this->initFromRenderTarget(context, texture->asRenderTarget());
1912 fCache = cacheEntry;
1913 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001914}
1915