blob: c731999dbacb4d000724072646573c6fbe64d984 [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) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000197 pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000198 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000199 pr = SkNEW_ARGS(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
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000240 SkGrTexturePixelRef* pr = SkNEW_ARGS(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()),
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000356 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);
rileya@google.com3e332582012-07-03 13:43:35 +0000414SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
415 shader_type_mismatch);
416SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 5, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000417
bsalomon@google.com84405e02012-03-05 19:57:21 +0000418namespace {
419
420// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
421// justAlpha indicates that skPaint's alpha should be used rather than the color
422// Callers may subsequently modify the GrPaint. Setting constantColor indicates
423// that the final paint will draw the same color at every pixel. This allows
424// an optimization where the the color filter can be applied to the skPaint's
425// color once while converting to GrPain and then ignored.
426inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
427 bool justAlpha,
428 bool constantColor,
429 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000430
431 grPaint->fDither = skPaint.isDither();
432 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000433 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000434
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000435 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
436 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000437
438 SkXfermode* mode = skPaint.getXfermode();
439 if (mode) {
440 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000441 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000442#if 0
443 return false;
444#endif
445 }
446 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000447 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
448 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
449
bsalomon@google.com5782d712011-01-21 21:03:59 +0000450 if (justAlpha) {
451 uint8_t alpha = skPaint.getAlpha();
452 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000453 // justAlpha is currently set to true only if there is a texture,
454 // so constantColor should not also be true.
455 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000456 } else {
457 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000458 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000459 }
Scroggo97c88c22011-05-11 14:05:25 +0000460 SkColorFilter* colorFilter = skPaint.getColorFilter();
461 SkColor color;
462 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000463 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000464 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000465 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000466 if (!constantColor) {
467 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
468 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000469 } else {
470 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
471 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000472 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000473 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000474 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
475 grPaint->fColorMatrixEnabled = true;
476 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
477 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
478 } else {
479 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000480 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000481 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000482}
483
bsalomon@google.com84405e02012-03-05 19:57:21 +0000484// This function is similar to skPaint2GrPaintNoShader but also converts
485// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
486// be used is set on grPaint and returned in param act. constantColor has the
487// same meaning as in skPaint2GrPaintNoShader.
488inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
489 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000490 bool constantColor,
491 SkGpuDevice::SkAutoCachedTexture* act,
492 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000493
bsalomon@google.com5782d712011-01-21 21:03:59 +0000494 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
bsalomon@google.com5782d712011-01-21 21:03:59 +0000496 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000497 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000498 return skPaint2GrPaintNoShader(skPaint,
499 false,
500 constantColor,
501 grPaint);
502 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000503 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000504 }
505
reed@google.comac10a2d2010-12-22 21:39:39 +0000506 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000507 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000508 SkShader::TileMode tileModes[2];
509 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000510 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 tileModes, twoPointParams);
512
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000513 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000514 SkShader::GradientInfo info;
515 SkColor color;
516
517 info.fColors = &color;
518 info.fColorOffsets = NULL;
519 info.fColorCount = 1;
520 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
521 SkPaint copy(skPaint);
522 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000523 // modulate the paint alpha by the shader's solid color alpha
524 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
525 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000526 return skPaint2GrPaintNoShader(copy,
527 false,
528 constantColor,
529 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000530 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000531 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000532 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000533 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000534 switch (bmptype) {
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000535 case SkShader::kRadial_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000536 sampler->setCustomStage(SkNEW(GrRadialGradient))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000537 sampler->setFilter(GrSamplerState::kBilinear_Filter);
538 break;
539 case SkShader::kSweep_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000540 sampler->setCustomStage(SkNEW(GrSweepGradient))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000541 sampler->setFilter(GrSamplerState::kBilinear_Filter);
542 break;
543 case SkShader::kTwoPointRadial_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000544 sampler->setCustomStage(SkNEW_ARGS(GrRadial2Gradient,
545 (twoPointParams[0],
546 twoPointParams[1],
547 twoPointParams[2] < 0)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000548 sampler->setFilter(GrSamplerState::kBilinear_Filter);
549 break;
rileya@google.com3e332582012-07-03 13:43:35 +0000550 case SkShader::kTwoPointConical_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000551 sampler->setCustomStage(SkNEW_ARGS(GrConical2Gradient,
552 (twoPointParams[0],
553 twoPointParams[1],
554 twoPointParams[2])))->unref();
rileya@google.com3e332582012-07-03 13:43:35 +0000555 sampler->setFilter(GrSamplerState::kBilinear_Filter);
556 break;
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000557 default:
558 if (skPaint.isFilterBitmap()) {
559 sampler->setFilter(GrSamplerState::kBilinear_Filter);
560 } else {
561 sampler->setFilter(GrSamplerState::kNearest_Filter);
562 }
563 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000564 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000565 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
566 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000567
bsalomon@google.com84405e02012-03-05 19:57:21 +0000568 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000569 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000570 SkDebugf("Couldn't convert bitmap to texture.\n");
571 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000572 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000573 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000574
575 // since our texture coords will be in local space, we wack the texture
576 // matrix to map them back into 0...1 before we load it
577 SkMatrix localM;
578 if (shader->getLocalMatrix(&localM)) {
579 SkMatrix inverse;
580 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000581 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000582 }
583 }
584 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000585 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
586 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000587 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000588 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000589 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000590 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000591 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000592
593 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000594}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000595}
reed@google.comac10a2d2010-12-22 21:39:39 +0000596
597///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000598void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000599 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000600}
601
reed@google.comac10a2d2010-12-22 21:39:39 +0000602void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
603 CHECK_SHOULD_DRAW(draw);
604
bsalomon@google.com5782d712011-01-21 21:03:59 +0000605 GrPaint grPaint;
606 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000607 if (!skPaint2GrPaintShader(this,
608 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000609 true,
610 &act,
611 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000612 return;
613 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000614
615 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000616}
617
618// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000619static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000620 kPoints_GrPrimitiveType,
621 kLines_GrPrimitiveType,
622 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000623};
624
625void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000626 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000627 CHECK_SHOULD_DRAW(draw);
628
629 SkScalar width = paint.getStrokeWidth();
630 if (width < 0) {
631 return;
632 }
633
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000634 // we only handle hairlines and paints without path effects or mask filters,
635 // else we let the SkDraw call our drawPath()
636 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000637 draw.drawPoints(mode, count, pts, paint, true);
638 return;
639 }
640
bsalomon@google.com5782d712011-01-21 21:03:59 +0000641 GrPaint grPaint;
642 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000643 if (!skPaint2GrPaintShader(this,
644 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000645 true,
646 &act,
647 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000648 return;
649 }
650
bsalomon@google.com5782d712011-01-21 21:03:59 +0000651 fContext->drawVertices(grPaint,
652 gPointMode2PrimtiveType[mode],
653 count,
654 (GrPoint*)pts,
655 NULL,
656 NULL,
657 NULL,
658 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000659}
660
reed@google.comc9aa5872011-04-05 21:05:37 +0000661///////////////////////////////////////////////////////////////////////////////
662
reed@google.comac10a2d2010-12-22 21:39:39 +0000663void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
664 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000665 CHECK_SHOULD_DRAW(draw);
666
bungeman@google.com79bd8772011-07-18 15:34:08 +0000667 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000668 SkScalar width = paint.getStrokeWidth();
669
670 /*
671 We have special code for hairline strokes, miter-strokes, and fills.
672 Anything else we just call our path code.
673 */
674 bool usePath = doStroke && width > 0 &&
675 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000676 // another two reasons we might need to call drawPath...
677 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000678 usePath = true;
679 }
reed@google.com67db6642011-05-26 11:46:35 +0000680 // until we aa rotated rects...
681 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
682 usePath = true;
683 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000684 // small miter limit means right angles show bevel...
685 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
686 paint.getStrokeMiter() < SK_ScalarSqrt2)
687 {
688 usePath = true;
689 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000690 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000691 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
692 usePath = true;
693 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000694
695 if (usePath) {
696 SkPath path;
697 path.addRect(rect);
698 this->drawPath(draw, path, paint, NULL, true);
699 return;
700 }
701
702 GrPaint grPaint;
703 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000704 if (!skPaint2GrPaintShader(this,
705 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000706 true,
707 &act,
708 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000709 return;
710 }
reed@google.com20efde72011-05-09 17:00:02 +0000711 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000712}
713
reed@google.com69302852011-02-16 18:08:07 +0000714#include "SkMaskFilter.h"
715#include "SkBounder.h"
716
bsalomon@google.com85003222012-03-28 14:44:37 +0000717///////////////////////////////////////////////////////////////////////////////
718
719// helpers for applying mask filters
720namespace {
721
722GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000723 switch (fillType) {
724 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000725 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000726 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000727 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000728 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000729 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000730 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000731 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000732 default:
733 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000734 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000735 }
736}
737
bsalomon@google.com85003222012-03-28 14:44:37 +0000738// We prefer to blur small rect with small radius via CPU.
739#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
740#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
741inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
742 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
743 rect.height() <= MIN_GPU_BLUR_SIZE &&
744 radius <= MIN_GPU_BLUR_RADIUS) {
745 return true;
746 }
747 return false;
748}
749
750bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
751 SkMaskFilter* filter, const SkMatrix& matrix,
752 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000753 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000754#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000755 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000756#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000757 SkMaskFilter::BlurInfo info;
758 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000759 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000760 return false;
761 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000762 SkScalar radius = info.fIgnoreTransform ? info.fRadius
763 : matrix.mapRadius(info.fRadius);
764 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000765 if (radius <= 0) {
766 return false;
767 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000768
769 SkRect srcRect = path.getBounds();
770 if (shouldDrawBlurWithCPU(srcRect, radius)) {
771 return false;
772 }
773
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000774 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000775 float sigma3 = sigma * 3.0f;
776
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000777 SkRect clipRect;
778 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000779
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000780 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000781 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
782 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000783 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000784 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000785 SkIRect finalIRect;
786 finalRect.roundOut(&finalIRect);
787 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000788 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000789 }
790 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000791 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000792 }
793 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000794 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000795 GrTextureDesc desc;
796 desc.fFlags = kRenderTarget_GrTextureFlagBit;
797 desc.fWidth = SkScalarCeilToInt(srcRect.width());
798 desc.fHeight = SkScalarCeilToInt(srcRect.height());
799 // We actually only need A8, but it often isn't supported as a
800 // render target so default to RGBA_8888
801 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000802
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000803 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
804 desc.fConfig = kAlpha_8_GrPixelConfig;
805 }
806
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000807 GrAutoScratchTexture pathEntry(context, desc);
808 GrTexture* pathTexture = pathEntry.texture();
809 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000810 return false;
811 }
812 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000813 // Once this code moves into GrContext, this should be changed to use
814 // an AutoClipRestore.
815 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000816 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000817
818 GrClip newClip(srcRect);
819 context->setClip(newClip);
820
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000821 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000822 GrPaint tempPaint;
823 tempPaint.reset();
824
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000825 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000826 tempPaint.fAntiAlias = grp->fAntiAlias;
827 if (tempPaint.fAntiAlias) {
828 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
829 // blend coeff of zero requires dual source blending support in order
830 // to properly blend partially covered pixels. This means the AA
831 // code path may not be taken. So we use a dst blend coeff of ISA. We
832 // could special case AA draws to a dst surface with known alpha=0 to
833 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000834 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
835 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000836 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000837 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000838 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000839
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000840 GrAutoScratchTexture temp1, temp2;
841 // If we're doing a normal blur, we can clobber the pathTexture in the
842 // gaussianBlur. Otherwise, we need to save it for later compositing.
843 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000844 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
845 &temp1,
846 isNormalBlur ? NULL : &temp2,
847 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000848
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000849 if (!isNormalBlur) {
850 GrPaint paint;
851 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000852 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000853 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
854 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000855 // Blend pathTexture over blurTexture.
856 context->setRenderTarget(blurTexture->asRenderTarget());
857 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000858 if (SkMaskFilter::kInner_BlurType == blurType) {
859 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000860 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
861 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000862 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
863 // solid: dst = src + dst - src * dst
864 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000865 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
866 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000867 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
868 // outer: dst = dst * (1 - src)
869 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000870 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
871 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000872 }
873 context->drawRect(paint, srcRect);
874 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000875 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000876 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000877
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000878 if (grp->hasTextureOrMask()) {
879 GrMatrix inverse;
880 if (!matrix.invert(&inverse)) {
881 return false;
882 }
883 grp->preConcatActiveSamplerMatrices(inverse);
884 }
885
886 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
887 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000888 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000889 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000890 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000891
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000892 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
893 -finalRect.fTop);
894 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
895 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000896 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000897 return true;
898}
899
bsalomon@google.com85003222012-03-28 14:44:37 +0000900bool drawWithMaskFilter(GrContext* context, const SkPath& path,
901 SkMaskFilter* filter, const SkMatrix& matrix,
902 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000903 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000904 SkMask srcM, dstM;
905
906 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000907 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000908 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000909 return false;
910 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000911 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000912
913 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
914 return false;
915 }
916 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000917 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000918
919 if (clip.quickReject(dstM.fBounds)) {
920 return false;
921 }
922 if (bounder && !bounder->doIRect(dstM.fBounds)) {
923 return false;
924 }
925
926 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
927 // the current clip (and identity matrix) and grpaint settings
928
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000929 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000930 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000931
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000932 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000933
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000934 GrTextureDesc desc;
935 desc.fWidth = dstM.fBounds.width();
936 desc.fHeight = dstM.fBounds.height();
937 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000938
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000939 GrAutoScratchTexture ast(context, desc);
940 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000941
reed@google.com69302852011-02-16 18:08:07 +0000942 if (NULL == texture) {
943 return false;
944 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000945 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000946 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000947
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000948 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
949 grp->preConcatActiveSamplerMatrices(ivm);
950 }
951
952 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
953 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000954 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000955 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000956 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000957
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000958 GrRect d;
959 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000960 GrIntToScalar(dstM.fBounds.fTop),
961 GrIntToScalar(dstM.fBounds.fRight),
962 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000963
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000964 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
965 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
966 -dstM.fBounds.fTop*SK_Scalar1);
967 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000968 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000969 return true;
970}
reed@google.com69302852011-02-16 18:08:07 +0000971
bsalomon@google.com85003222012-03-28 14:44:37 +0000972}
973
974///////////////////////////////////////////////////////////////////////////////
975
reed@google.com0c219b62011-02-16 21:31:18 +0000976void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000977 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000978 bool pathIsMutable) {
979 CHECK_SHOULD_DRAW(draw);
980
reed@google.comfe626382011-09-21 13:50:35 +0000981 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000982
bsalomon@google.com5782d712011-01-21 21:03:59 +0000983 GrPaint grPaint;
984 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000985 if (!skPaint2GrPaintShader(this,
986 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000987 true,
988 &act,
989 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000990 return;
991 }
992
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000993 // can we cheat, and threat a thin stroke as a hairline w/ coverage
994 // if we can, we draw lots faster (raster device does this same test)
995 SkScalar hairlineCoverage;
996 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
997 doFill = false;
998 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
999 grPaint.fCoverage);
1000 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001001
reed@google.comfe626382011-09-21 13:50:35 +00001002 // If we have a prematrix, apply it to the path, optimizing for the case
1003 // where the original path can in fact be modified in place (even though
1004 // its parameter type is const).
1005 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1006 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001007
1008 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001009 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001010
reed@google.come3445642011-02-16 23:20:39 +00001011 if (!pathIsMutable) {
1012 result = &tmpPath;
1013 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001014 }
reed@google.come3445642011-02-16 23:20:39 +00001015 // should I push prePathMatrix on our MV stack temporarily, instead
1016 // of applying it here? See SkDraw.cpp
1017 pathPtr->transform(*prePathMatrix, result);
1018 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001019 }
reed@google.com0c219b62011-02-16 21:31:18 +00001020 // at this point we're done with prePathMatrix
1021 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001022
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001023 if (paint.getPathEffect() ||
1024 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001025 // it is safe to use tmpPath here, even if we already used it for the
1026 // prepathmatrix, since getFillPath can take the same object for its
1027 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001028 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001029 pathPtr = &tmpPath;
1030 }
1031
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001032 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001033 // avoid possibly allocating a new path in transform if we can
1034 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1035
1036 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001037 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001038 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001039 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001040 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001041 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001042 &grPaint, pathFillType)) {
1043 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1044 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001045 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001046 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001047 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001048 }
reed@google.com69302852011-02-16 18:08:07 +00001049 return;
1050 }
reed@google.com69302852011-02-16 18:08:07 +00001051
bsalomon@google.com47059542012-06-06 20:51:20 +00001052 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001053
reed@google.com0c219b62011-02-16 21:31:18 +00001054 if (doFill) {
1055 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001056 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001057 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 break;
1059 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001060 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001061 break;
1062 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001063 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 break;
1065 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001066 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001067 break;
1068 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001069 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001070 return;
1071 }
1072 }
1073
reed@google.com07f3ee12011-05-16 17:21:57 +00001074 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001075}
1076
bsalomon@google.comfb309512011-11-30 14:13:48 +00001077namespace {
1078
1079inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1080 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1081 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1082 return tilesX * tilesY;
1083}
1084
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001085inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001086 const SkIRect* srcRectPtr,
1087 int maxTextureSize) {
1088 static const int kSmallTileSize = 1 << 10;
1089 if (maxTextureSize <= kSmallTileSize) {
1090 return maxTextureSize;
1091 }
1092
1093 size_t maxTexTotalTileSize;
1094 size_t smallTotalTileSize;
1095
1096 if (NULL == srcRectPtr) {
1097 int w = bitmap.width();
1098 int h = bitmap.height();
1099 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1100 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1101 } else {
1102 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1103 srcRectPtr->fTop,
1104 srcRectPtr->fRight,
1105 srcRectPtr->fBottom,
1106 maxTextureSize);
1107 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1108 srcRectPtr->fTop,
1109 srcRectPtr->fRight,
1110 srcRectPtr->fBottom,
1111 kSmallTileSize);
1112 }
1113 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1114 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1115
1116 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1117 return kSmallTileSize;
1118 } else {
1119 return maxTextureSize;
1120 }
1121}
1122}
1123
1124bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1125 const GrSamplerState& sampler,
1126 const SkIRect* srcRectPtr,
1127 int* tileSize) const {
1128 SkASSERT(NULL != tileSize);
1129
1130 // if bitmap is explictly texture backed then just use the texture
1131 if (NULL != bitmap.getTexture()) {
1132 return false;
1133 }
1134 // if it's larger than the max texture size, then we have no choice but
1135 // tiling
1136 const int maxTextureSize = fContext->getMaxTextureSize();
1137 if (bitmap.width() > maxTextureSize ||
1138 bitmap.height() > maxTextureSize) {
1139 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1140 return true;
1141 }
1142 // if we are going to have to draw the whole thing, then don't tile
1143 if (NULL == srcRectPtr) {
1144 return false;
1145 }
1146 // if the entire texture is already in our cache then no reason to tile it
1147 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1148 return false;
1149 }
1150
1151 // At this point we know we could do the draw by uploading the entire bitmap
1152 // as a texture. However, if the texture would be large compared to the
1153 // cache size and we don't require most of it for this draw then tile to
1154 // reduce the amount of upload and cache spill.
1155
1156 // assumption here is that sw bitmap size is a good proxy for its size as
1157 // a texture
1158 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001159 size_t cacheSize;
1160 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001161 if (bmpSize < cacheSize / 2) {
1162 return false;
1163 }
1164
1165 SkFixed fracUsed =
1166 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1167 (srcRectPtr->height() << 16) / bitmap.height());
1168 if (fracUsed <= SK_FixedHalf) {
1169 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1170 return true;
1171 } else {
1172 return false;
1173 }
1174}
1175
reed@google.comac10a2d2010-12-22 21:39:39 +00001176void SkGpuDevice::drawBitmap(const SkDraw& draw,
1177 const SkBitmap& bitmap,
1178 const SkIRect* srcRectPtr,
1179 const SkMatrix& m,
1180 const SkPaint& paint) {
1181 CHECK_SHOULD_DRAW(draw);
1182
1183 SkIRect srcRect;
1184 if (NULL == srcRectPtr) {
1185 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1186 } else {
1187 srcRect = *srcRectPtr;
1188 }
1189
junov@google.comd935cfb2011-06-27 20:48:23 +00001190 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001191 // Convert the bitmap to a shader so that the rect can be drawn
1192 // through drawRect, which supports mask filters.
1193 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001194 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001195 if (srcRectPtr) {
1196 if (!bitmap.extractSubset(&tmp, srcRect)) {
1197 return; // extraction failed
1198 }
1199 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001200 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001201 }
1202 SkPaint paintWithTexture(paint);
1203 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1204 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001205 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001206 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001207
junov@google.com1d329782011-07-28 20:10:09 +00001208 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001209 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001210 // also affect the behavior of the mask filter.
1211 SkMatrix drawMatrix;
1212 drawMatrix.setConcat(*draw.fMatrix, m);
1213 SkDraw transformedDraw(draw);
1214 transformedDraw.fMatrix = &drawMatrix;
1215
1216 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1217
junov@google.comd935cfb2011-06-27 20:48:23 +00001218 return;
1219 }
1220
bsalomon@google.com5782d712011-01-21 21:03:59 +00001221 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001222 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001223 return;
1224 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001225 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001226 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001227 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001228 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001229 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001230 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001231
bsalomon@google.comfb309512011-11-30 14:13:48 +00001232 int tileSize;
1233 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1234 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001235 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001236 return;
1237 }
1238
1239 // undo the translate done by SkCanvas
1240 int DX = SkMax32(0, srcRect.fLeft);
1241 int DY = SkMax32(0, srcRect.fTop);
1242 // compute clip bounds in local coordinates
1243 SkIRect clipRect;
1244 {
1245 SkRect r;
1246 r.set(draw.fClip->getBounds());
1247 SkMatrix matrix, inverse;
1248 matrix.setConcat(*draw.fMatrix, m);
1249 if (!matrix.invert(&inverse)) {
1250 return;
1251 }
1252 inverse.mapRect(&r);
1253 r.roundOut(&clipRect);
1254 // apply the canvas' translate to our local clip
1255 clipRect.offset(DX, DY);
1256 }
1257
bsalomon@google.comfb309512011-11-30 14:13:48 +00001258 int nx = bitmap.width() / tileSize;
1259 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001260 for (int x = 0; x <= nx; x++) {
1261 for (int y = 0; y <= ny; y++) {
1262 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001263 tileR.set(x * tileSize, y * tileSize,
1264 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 if (!SkIRect::Intersects(tileR, clipRect)) {
1266 continue;
1267 }
1268
1269 SkIRect srcR = tileR;
1270 if (!srcR.intersect(srcRect)) {
1271 continue;
1272 }
1273
1274 SkBitmap tmpB;
1275 if (bitmap.extractSubset(&tmpB, tileR)) {
1276 // now offset it to make it "local" to our tmp bitmap
1277 srcR.offset(-tileR.fLeft, -tileR.fTop);
1278
1279 SkMatrix tmpM(m);
1280 {
1281 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1282 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1283 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1284 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001285 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 }
1287 }
1288 }
1289}
1290
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001291namespace {
1292
1293bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1294 // detect pixel disalignment
1295 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1296 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1297 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1298 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1299 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1300 COLOR_BLEED_TOLERANCE &&
1301 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1302 COLOR_BLEED_TOLERANCE) {
1303 return true;
1304 }
1305 return false;
1306}
1307
1308bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1309 const SkMatrix& m) {
1310 // Only gets called if hasAlignedSamples returned false.
1311 // So we can assume that sampling is axis aligned but not texel aligned.
1312 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1313 SkRect innerSrcRect(srcRect), innerTransformedRect,
1314 outerTransformedRect(transformedRect);
1315 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1316 m.mapRect(&innerTransformedRect, innerSrcRect);
1317
1318 // The gap between outerTransformedRect and innerTransformedRect
1319 // represents the projection of the source border area, which is
1320 // problematic for color bleeding. We must check whether any
1321 // destination pixels sample the border area.
1322 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1323 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1324 SkIRect outer, inner;
1325 outerTransformedRect.round(&outer);
1326 innerTransformedRect.round(&inner);
1327 // If the inner and outer rects round to the same result, it means the
1328 // border does not overlap any pixel centers. Yay!
1329 return inner != outer;
1330}
1331
1332} // unnamed namespace
1333
reed@google.comac10a2d2010-12-22 21:39:39 +00001334/*
1335 * This is called by drawBitmap(), which has to handle images that may be too
1336 * large to be represented by a single texture.
1337 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001338 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1339 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001340 */
1341void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1342 const SkBitmap& bitmap,
1343 const SkIRect& srcRect,
1344 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001345 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001346 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1347 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001348
reed@google.com9c49bc32011-07-07 13:42:37 +00001349 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001350 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001351 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001352 return;
1353 }
1354
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001355 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001356
1357 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1358 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001359 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001360
1361 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001362 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001363 if (NULL == texture) {
1364 return;
1365 }
1366
bsalomon@google.com452943d2011-10-31 17:37:14 +00001367 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001368
reed@google.com20efde72011-05-09 17:00:02 +00001369 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1370 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001371 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001372 float wInv = 1.f / bitmap.width();
1373 float hInv = 1.f / bitmap.height();
1374 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1375 SkFloatToScalar(srcRect.fTop * hInv),
1376 SkFloatToScalar(srcRect.fRight * wInv),
1377 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001378
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001379 bool needsTextureDomain = false;
1380 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1381 {
1382 // Need texture domain if drawing a sub rect.
1383 needsTextureDomain = srcRect.width() < bitmap.width() ||
1384 srcRect.height() < bitmap.height();
1385 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1386 // sampling is axis-aligned
1387 GrRect floatSrcRect, transformedRect;
1388 floatSrcRect.set(srcRect);
1389 SkMatrix srcToDeviceMatrix(m);
1390 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1391 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1392
1393 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1394 // Samples are texel-aligned, so filtering is futile
1395 sampler->setFilter(GrSamplerState::kNearest_Filter);
1396 needsTextureDomain = false;
1397 } else {
1398 needsTextureDomain = needsTextureDomain &&
1399 mayColorBleed(floatSrcRect, transformedRect, m);
1400 }
1401 }
1402 }
1403
1404 GrRect textureDomain = GrRect::MakeEmpty();
1405
1406 if (needsTextureDomain) {
1407 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001408 GrScalar left, top, right, bottom;
1409 if (srcRect.width() > 1) {
1410 GrScalar border = GR_ScalarHalf / bitmap.width();
1411 left = paintRect.left() + border;
1412 right = paintRect.right() - border;
1413 } else {
1414 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1415 }
1416 if (srcRect.height() > 1) {
1417 GrScalar border = GR_ScalarHalf / bitmap.height();
1418 top = paintRect.top() + border;
1419 bottom = paintRect.bottom() - border;
1420 } else {
1421 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1422 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001423 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001424 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001425 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001426
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001427 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001428}
1429
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001430namespace {
1431
1432void apply_custom_stage(GrContext* context,
1433 GrTexture* srcTexture,
1434 GrTexture* dstTexture,
1435 const GrRect& rect,
1436 GrCustomStage* stage) {
1437 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001438 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001439 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
1440 GrClip oldClip = context->getClip();
robertphillips@google.combca1c5d2012-07-11 18:25:24 +00001441
1442 GrClip newClip(rect);
1443 context->setClip(newClip);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001444
1445 GrMatrix sampleM;
1446 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1447 GrPaint paint;
1448 paint.reset();
1449 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1450 paint.textureSampler(0)->reset(sampleM);
1451 paint.textureSampler(0)->setCustomStage(stage);
1452 paint.setTexture(0, srcTexture);
1453 context->drawRect(paint, rect);
1454 context->setClip(oldClip);
1455}
1456
1457};
1458
reed@google.com8926b162012-03-23 15:36:36 +00001459static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1460 SkImageFilter* filter, const GrRect& rect) {
1461 GrAssert(filter);
1462
1463 SkSize blurSize;
1464 SkISize radius;
1465
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001466 GrTextureDesc desc;
1467 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1468 desc.fWidth = SkScalarCeilToInt(rect.width());
1469 desc.fHeight = SkScalarCeilToInt(rect.height());
1470 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001471 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001472
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001473 if (filter->asNewCustomStage(&stage)) {
1474 GrAutoScratchTexture dst(context, desc);
1475 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1476 texture = dst.detach();
1477 stage->unref();
1478 } else if (filter->asABlur(&blurSize)) {
reed@google.com8926b162012-03-23 15:36:36 +00001479 GrAutoScratchTexture temp1, temp2;
1480 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1481 blurSize.width(),
1482 blurSize.height());
1483 texture->ref();
1484 } else if (filter->asADilate(&radius)) {
1485 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1486 texture = context->applyMorphology(texture, rect,
1487 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001488 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001489 radius);
1490 texture->ref();
1491 } else if (filter->asAnErode(&radius)) {
1492 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1493 texture = context->applyMorphology(texture, rect,
1494 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001495 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001496 radius);
1497 texture->ref();
1498 }
1499 return texture;
1500}
1501
reed@google.comac10a2d2010-12-22 21:39:39 +00001502void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1503 int left, int top, const SkPaint& paint) {
1504 CHECK_SHOULD_DRAW(draw);
1505
reed@google.com8926b162012-03-23 15:36:36 +00001506 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001507 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1508 return;
1509 }
1510
reed@google.com76dd2772012-01-05 21:15:07 +00001511 int w = bitmap.width();
1512 int h = bitmap.height();
1513
bsalomon@google.com5782d712011-01-21 21:03:59 +00001514 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001515 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001516 return;
1517 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001518
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001519 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001520
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001521 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001522
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001523 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001524 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001525 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001526 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001527
reed@google.com8926b162012-03-23 15:36:36 +00001528 SkImageFilter* filter = paint.getImageFilter();
1529 if (NULL != filter) {
1530 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001531 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001532 if (filteredTexture) {
1533 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1534 texture = filteredTexture;
1535 filteredTexture->unref();
1536 }
reed@google.com76dd2772012-01-05 21:15:07 +00001537 }
reed@google.com8926b162012-03-23 15:36:36 +00001538
bsalomon@google.com5782d712011-01-21 21:03:59 +00001539 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001540 GrRect::MakeXYWH(GrIntToScalar(left),
1541 GrIntToScalar(top),
1542 GrIntToScalar(w),
1543 GrIntToScalar(h)),
1544 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1545 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001546}
1547
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001548void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001549 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001550 // clear of the source device must occur before CHECK_SHOULD_DRAW
1551 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1552 if (dev->fNeedClear) {
1553 // TODO: could check here whether we really need to draw at all
1554 dev->clear(0x0);
1555 }
1556
reed@google.comac10a2d2010-12-22 21:39:39 +00001557 CHECK_SHOULD_DRAW(draw);
1558
bsalomon@google.com5782d712011-01-21 21:03:59 +00001559 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001560 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001561 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001562 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001563 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001564
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001565 GrTexture* devTex = grPaint.getTexture(0);
1566 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001567
reed@google.com8926b162012-03-23 15:36:36 +00001568 SkImageFilter* filter = paint.getImageFilter();
1569 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001570 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1571 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001572 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1573 rect);
1574 if (filteredTexture) {
1575 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1576 devTex = filteredTexture;
1577 filteredTexture->unref();
1578 }
1579 }
1580
bsalomon@google.com5782d712011-01-21 21:03:59 +00001581 const SkBitmap& bm = dev->accessBitmap(false);
1582 int w = bm.width();
1583 int h = bm.height();
1584
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001585 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001586
bsalomon@google.com97912912011-12-06 16:30:36 +00001587 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001588
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001589 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1590 GrIntToScalar(y),
1591 GrIntToScalar(w),
1592 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001593
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001594 // The device being drawn may not fill up its texture (saveLayer uses
1595 // the approximate ).
1596 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1597 GR_Scalar1 * h / devTex->height());
1598
1599 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001600}
1601
reed@google.com8926b162012-03-23 15:36:36 +00001602bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001603 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001604 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001605
1606 if (!filter->asNewCustomStage(NULL) &&
1607 !filter->asABlur(&size) &&
1608 !filter->asADilate(&radius) &&
1609 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001610 return false;
1611 }
reed@google.com8926b162012-03-23 15:36:36 +00001612 return true;
1613}
1614
1615bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1616 const SkMatrix& ctm,
1617 SkBitmap* result, SkIPoint* offset) {
1618 // want explicitly our impl, so guard against a subclass of us overriding it
1619 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001620 return false;
1621 }
reed@google.com8926b162012-03-23 15:36:36 +00001622
1623 SkAutoLockPixels alp(src, !src.getTexture());
1624 if (!src.getTexture() && !src.readyToDraw()) {
1625 return false;
1626 }
1627
1628 GrPaint paint;
1629 paint.reset();
1630
1631 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1632
1633 GrTexture* texture;
1634 SkAutoCachedTexture act(this, src, sampler, &texture);
1635
1636 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001637 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1638 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001639 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1640 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001641 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1642 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001643 resultTexture->unref();
1644 }
reed@google.com76dd2772012-01-05 21:15:07 +00001645 return true;
1646}
1647
reed@google.comac10a2d2010-12-22 21:39:39 +00001648///////////////////////////////////////////////////////////////////////////////
1649
1650// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001651static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001652 kTriangles_GrPrimitiveType,
1653 kTriangleStrip_GrPrimitiveType,
1654 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001655};
1656
1657void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1658 int vertexCount, const SkPoint vertices[],
1659 const SkPoint texs[], const SkColor colors[],
1660 SkXfermode* xmode,
1661 const uint16_t indices[], int indexCount,
1662 const SkPaint& paint) {
1663 CHECK_SHOULD_DRAW(draw);
1664
bsalomon@google.com5782d712011-01-21 21:03:59 +00001665 GrPaint grPaint;
1666 SkAutoCachedTexture act;
1667 // we ignore the shader if texs is null.
1668 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001669 if (!skPaint2GrPaintNoShader(paint,
1670 false,
1671 NULL == colors,
1672 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001673 return;
1674 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001675 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001676 if (!skPaint2GrPaintShader(this,
1677 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001678 NULL == colors,
1679 &act,
1680 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001681 return;
1682 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001683 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001684
1685 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001686 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001687 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1688#if 0
1689 return
1690#endif
1691 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001692 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001693
bsalomon@google.com498776a2011-08-16 19:20:44 +00001694 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1695 if (NULL != colors) {
1696 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001697 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001698 for (int i = 0; i < vertexCount; ++i) {
1699 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1700 }
1701 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001702 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001703 fContext->drawVertices(grPaint,
1704 gVertexMode2PrimitiveType[vmode],
1705 vertexCount,
1706 (GrPoint*) vertices,
1707 (GrPoint*) texs,
1708 colors,
1709 indices,
1710 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001711}
1712
1713///////////////////////////////////////////////////////////////////////////////
1714
1715static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001716 GrFontScaler* scaler = (GrFontScaler*)data;
1717 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001718}
1719
1720static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1721 void* auxData;
1722 GrFontScaler* scaler = NULL;
1723 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1724 scaler = (GrFontScaler*)auxData;
1725 }
1726 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001727 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001728 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1729 }
1730 return scaler;
1731}
1732
1733static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1734 SkFixed fx, SkFixed fy,
1735 const SkGlyph& glyph) {
1736 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1737
bungeman@google.com15865a72012-01-11 16:28:04 +00001738 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001739
1740 if (NULL == procs->fFontScaler) {
1741 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1742 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001743
bungeman@google.com15865a72012-01-11 16:28:04 +00001744 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1745 glyph.getSubXFixed(),
1746 glyph.getSubYFixed()),
1747 SkFixedFloorToFixed(fx),
1748 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001749 procs->fFontScaler);
1750}
1751
bsalomon@google.com5782d712011-01-21 21:03:59 +00001752SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001753
1754 // deferred allocation
1755 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001756 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001757 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1758 fDrawProcs->fContext = fContext;
1759 }
1760
1761 // init our (and GL's) state
1762 fDrawProcs->fTextContext = context;
1763 fDrawProcs->fFontScaler = NULL;
1764 return fDrawProcs;
1765}
1766
1767void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1768 size_t byteLength, SkScalar x, SkScalar y,
1769 const SkPaint& paint) {
1770 CHECK_SHOULD_DRAW(draw);
1771
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001772 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001773 // this guy will just call our drawPath()
1774 draw.drawText((const char*)text, byteLength, x, y, paint);
1775 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001776 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001777
1778 GrPaint grPaint;
1779 SkAutoCachedTexture act;
1780
bsalomon@google.com84405e02012-03-05 19:57:21 +00001781 if (!skPaint2GrPaintShader(this,
1782 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001783 true,
1784 &act,
1785 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001786 return;
1787 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001788 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1789 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001790 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1791 }
1792}
1793
1794void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1795 size_t byteLength, const SkScalar pos[],
1796 SkScalar constY, int scalarsPerPos,
1797 const SkPaint& paint) {
1798 CHECK_SHOULD_DRAW(draw);
1799
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001800 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001801 // this guy will just call our drawPath()
1802 draw.drawPosText((const char*)text, byteLength, pos, constY,
1803 scalarsPerPos, paint);
1804 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001805 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001806
1807 GrPaint grPaint;
1808 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001809 if (!skPaint2GrPaintShader(this,
1810 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001811 true,
1812 &act,
1813 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001814 return;
1815 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001816 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1817 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001818 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1819 scalarsPerPos, paint);
1820 }
1821}
1822
1823void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1824 size_t len, const SkPath& path,
1825 const SkMatrix* m, const SkPaint& paint) {
1826 CHECK_SHOULD_DRAW(draw);
1827
1828 SkASSERT(draw.fDevice == this);
1829 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1830}
1831
1832///////////////////////////////////////////////////////////////////////////////
1833
reed@google.comf67e4cf2011-03-15 20:56:58 +00001834bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1835 if (!paint.isLCDRenderText()) {
1836 // we're cool with the paint as is
1837 return false;
1838 }
1839
1840 if (paint.getShader() ||
1841 paint.getXfermode() || // unless its srcover
1842 paint.getMaskFilter() ||
1843 paint.getRasterizer() ||
1844 paint.getColorFilter() ||
1845 paint.getPathEffect() ||
1846 paint.isFakeBoldText() ||
1847 paint.getStyle() != SkPaint::kFill_Style) {
1848 // turn off lcd
1849 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1850 flags->fHinting = paint.getHinting();
1851 return true;
1852 }
1853 // we're cool with the paint as is
1854 return false;
1855}
1856
reed@google.com75d939b2011-12-07 15:07:23 +00001857void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001858 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001859 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001860}
1861
reed@google.comf67e4cf2011-03-15 20:56:58 +00001862///////////////////////////////////////////////////////////////////////////////
1863
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001864SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1865 const SkBitmap& bitmap,
1866 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001867 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001868 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001869
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001870 if (!bitmap.isVolatile()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001871 uint64_t key = bitmap.getGenerationID();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001872 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001873
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001874 GrTextureDesc desc;
1875 desc.fWidth = bitmap.width();
1876 desc.fHeight = bitmap.height();
1877 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
robertphillips@google.com56f22442012-06-08 14:21:26 +00001878 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001879
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001880 entry = ctx->findAndLockTexture(desc, sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001881 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001882 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1883 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001884 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001885 } else {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001886 entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001887 sampler, bitmap);
1888 }
1889 if (NULL == entry.texture()) {
1890 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1891 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001892 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001893 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001894}
1895
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001896void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1897 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001898}
1899
bsalomon@google.comfb309512011-11-30 14:13:48 +00001900bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1901 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001902 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001903 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001904
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001905 GrTextureDesc desc;
1906 desc.fWidth = bitmap.width();
1907 desc.fHeight = bitmap.height();
1908 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
1909 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001910
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001911 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001912}
1913
1914
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001915SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1916 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001917 bool isOpaque,
1918 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001919 GrTextureDesc desc;
1920 desc.fConfig = fRenderTarget->config();
1921 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1922 desc.fWidth = width;
1923 desc.fHeight = height;
1924 desc.fSampleCnt = fRenderTarget->numSamples();
1925
1926 GrContext::TextureCacheEntry cacheEntry;
1927 GrTexture* texture;
1928 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001929 // Skia's convention is to only clear a device if it is non-opaque.
1930 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001931
1932#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1933 // layers are never draw in repeat modes, so we can request an approx
1934 // match and ignore any padding.
1935 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1936 GrContext::kApprox_ScratchTexMatch :
1937 GrContext::kExact_ScratchTexMatch;
1938 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1939 texture = cacheEntry.texture();
1940#else
1941 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1942 texture = tunref.get();
1943#endif
1944 if (texture) {
1945 return SkNEW_ARGS(SkGpuDevice,(fContext,
1946 texture,
1947 cacheEntry,
1948 needClear));
1949 } else {
1950 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1951 width, height);
1952 return NULL;
1953 }
1954}
1955
1956SkGpuDevice::SkGpuDevice(GrContext* context,
1957 GrTexture* texture,
1958 TexCache cacheEntry,
1959 bool needClear)
1960 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1961 GrAssert(texture && texture->asRenderTarget());
1962 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1963 this->initFromRenderTarget(context, texture->asRenderTarget());
1964 fCache = cacheEntry;
1965 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001966}
1967