blob: 11543136a6b55ab2efd8d69921ba30a09cae47a9 [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
reed@google.comb0a34d82012-07-11 19:57:55 +000074#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
75 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
76
77///////////////////////////////////////////////////////////////////////////////
78
79
bsalomon@google.com84405e02012-03-05 19:57:21 +000080class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
81public:
82 SkAutoCachedTexture() { }
83 SkAutoCachedTexture(SkGpuDevice* device,
84 const SkBitmap& bitmap,
85 const GrSamplerState* sampler,
86 GrTexture** texture) {
87 GrAssert(texture);
88 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000089 }
reed@google.comac10a2d2010-12-22 21:39:39 +000090
bsalomon@google.com84405e02012-03-05 19:57:21 +000091 ~SkAutoCachedTexture() {
92 if (fTex.texture()) {
93 fDevice->unlockCachedTexture(fTex);
94 }
reed@google.comac10a2d2010-12-22 21:39:39 +000095 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000096
97 GrTexture* set(SkGpuDevice* device,
98 const SkBitmap& bitmap,
99 const GrSamplerState* sampler) {
100 if (fTex.texture()) {
101 fDevice->unlockCachedTexture(fTex);
102 }
103 fDevice = device;
104 GrTexture* texture = (GrTexture*)bitmap.getTexture();
105 if (texture) {
106 // return the native texture
107 fTex.reset();
108 } else {
109 // look it up in our cache
110 fTex = device->lockCachedTexture(bitmap, sampler);
111 texture = fTex.texture();
112 }
113 return texture;
114 }
115
116private:
117 SkGpuDevice* fDevice;
118 GrContext::TextureCacheEntry fTex;
119};
reed@google.comac10a2d2010-12-22 21:39:39 +0000120
121///////////////////////////////////////////////////////////////////////////////
122
123bool gDoTraceDraw;
124
125struct GrSkDrawProcs : public SkDrawProcs {
126public:
127 GrContext* fContext;
128 GrTextContext* fTextContext;
129 GrFontScaler* fFontScaler; // cached in the skia glyphcache
130};
131
132///////////////////////////////////////////////////////////////////////////////
133
reed@google.comaf951c92011-06-16 19:10:39 +0000134static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
135 switch (config) {
136 case kAlpha_8_GrPixelConfig:
137 *isOpaque = false;
138 return SkBitmap::kA8_Config;
139 case kRGB_565_GrPixelConfig:
140 *isOpaque = true;
141 return SkBitmap::kRGB_565_Config;
142 case kRGBA_4444_GrPixelConfig:
143 *isOpaque = false;
144 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000145 case kSkia8888_PM_GrPixelConfig:
146 // we don't currently have a way of knowing whether
147 // a 8888 is opaque based on the config.
148 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000149 return SkBitmap::kARGB_8888_Config;
150 default:
151 *isOpaque = false;
152 return SkBitmap::kNo_Config;
153 }
154}
reed@google.comac10a2d2010-12-22 21:39:39 +0000155
reed@google.comaf951c92011-06-16 19:10:39 +0000156static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000157 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000158
159 bool isOpaque;
160 SkBitmap bitmap;
161 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
162 renderTarget->width(), renderTarget->height());
163 bitmap.setIsOpaque(isOpaque);
164 return bitmap;
165}
166
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000167SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
168: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
169 this->initFromRenderTarget(context, texture->asRenderTarget());
170}
171
reed@google.comaf951c92011-06-16 19:10:39 +0000172SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
173: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000174 this->initFromRenderTarget(context, renderTarget);
175}
176
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000177void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000178 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000179 fNeedPrepareRenderTarget = false;
180 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000181
reed@google.comaf951c92011-06-16 19:10:39 +0000182 fContext = context;
183 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000184
reed@google.comaf951c92011-06-16 19:10:39 +0000185 fTexture = NULL;
186 fRenderTarget = NULL;
187 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000188
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000189 GrAssert(NULL != renderTarget);
190 fRenderTarget = renderTarget;
191 fRenderTarget->ref();
192 // if this RT is also a texture, hold a ref on it
193 fTexture = fRenderTarget->asTexture();
194 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000195
196 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
197 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
198 // the device via accessBitmap. This external ref may outlive the device.
199 // Since textures own their render targets (but not vice-versa) we
200 // are ensuring that both objects will live as long as the pixel ref.
201 SkPixelRef* pr;
202 if (fTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000203 pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000204 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000205 pr = SkNEW_ARGS(SkGrRenderTargetPixelRef, (fRenderTarget));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000206 }
reed@google.comaf951c92011-06-16 19:10:39 +0000207 this->setPixelRef(pr, 0)->unref();
208}
209
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000210SkGpuDevice::SkGpuDevice(GrContext* context,
211 SkBitmap::Config config,
212 int width,
213 int height)
214 : SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 fNeedPrepareRenderTarget = false;
216 fDrawProcs = NULL;
217
reed@google.com7b201d22011-01-11 18:59:23 +0000218 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000219 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000220
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 fTexture = NULL;
222 fRenderTarget = NULL;
223 fNeedClear = false;
224
reed@google.comaf951c92011-06-16 19:10:39 +0000225 if (config != SkBitmap::kRGB_565_Config) {
226 config = SkBitmap::kARGB_8888_Config;
227 }
228 SkBitmap bm;
229 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000230
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000231 GrTextureDesc desc;
232 desc.fFlags = kRenderTarget_GrTextureFlagBit;
233 desc.fWidth = width;
234 desc.fHeight = height;
235 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
reed@google.comaf951c92011-06-16 19:10:39 +0000237 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000238
reed@google.comaf951c92011-06-16 19:10:39 +0000239 if (NULL != fTexture) {
240 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000241 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
reed@google.comaf951c92011-06-16 19:10:39 +0000243 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000244
reed@google.comaf951c92011-06-16 19:10:39 +0000245 // wrap the bitmap with a pixelref to expose our texture
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000246 SkGrTexturePixelRef* pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000247 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000248 } else {
249 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
250 width, height);
251 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000252 }
253}
254
255SkGpuDevice::~SkGpuDevice() {
256 if (fDrawProcs) {
257 delete fDrawProcs;
258 }
259
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000260 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
261 // This call gives the context a chance to relinquish it
262 fContext->setRenderTarget(NULL);
263
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000264 SkSafeUnref(fTexture);
265 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000266 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000267 GrAssert(NULL != fTexture);
268 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000269 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000270 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000271 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000272}
273
reed@google.comac10a2d2010-12-22 21:39:39 +0000274///////////////////////////////////////////////////////////////////////////////
275
276void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000277 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000278 fContext->setRenderTarget(fRenderTarget);
279 fContext->flush(true);
280 fNeedPrepareRenderTarget = true;
281}
282
283///////////////////////////////////////////////////////////////////////////////
284
bsalomon@google.comc4364992011-11-07 15:54:49 +0000285namespace {
286GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
287 switch (config8888) {
288 case SkCanvas::kNative_Premul_Config8888:
289 return kSkia8888_PM_GrPixelConfig;
290 case SkCanvas::kNative_Unpremul_Config8888:
291 return kSkia8888_UPM_GrPixelConfig;
292 case SkCanvas::kBGRA_Premul_Config8888:
293 return kBGRA_8888_PM_GrPixelConfig;
294 case SkCanvas::kBGRA_Unpremul_Config8888:
295 return kBGRA_8888_UPM_GrPixelConfig;
296 case SkCanvas::kRGBA_Premul_Config8888:
297 return kRGBA_8888_PM_GrPixelConfig;
298 case SkCanvas::kRGBA_Unpremul_Config8888:
299 return kRGBA_8888_UPM_GrPixelConfig;
300 default:
301 GrCrash("Unexpected Config8888.");
302 return kSkia8888_PM_GrPixelConfig;
303 }
304}
305}
306
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000307bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
308 int x, int y,
309 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000310 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000311 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
312 SkASSERT(!bitmap.isNull());
313 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000314
bsalomon@google.com910267d2011-11-02 20:06:25 +0000315 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000316 GrPixelConfig config;
317 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000318 return fContext->readRenderTargetPixels(fRenderTarget,
319 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000320 bitmap.width(),
321 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000322 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000323 bitmap.getPixels(),
324 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000325}
326
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000327void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
328 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000329 SkAutoLockPixels alp(bitmap);
330 if (!bitmap.readyToDraw()) {
331 return;
332 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000333
334 GrPixelConfig config;
335 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
336 config = config8888_to_gr_config(config8888);
337 } else {
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000338 config= SkGr::BitmapConfig2PixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000339 }
340
bsalomon@google.com6f379512011-11-16 20:36:03 +0000341 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
342 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000343}
344
345///////////////////////////////////////////////////////////////////////////////
346
347static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000348 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000349 const SkRegion& clipRegion,
350 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000351 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000352
353 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000354 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000355 const SkIRect& skBounds = clipRegion.getBounds();
356 GrRect bounds;
357 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
358 GrIntToScalar(skBounds.fTop),
359 GrIntToScalar(skBounds.fRight),
360 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000361 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000362 bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000363 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000364}
365
366// call this ever each draw call, to ensure that the context reflects our state,
367// and not the state from some other canvas/device
368void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
369 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000370 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000371
372 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000373 SkASSERT(draw.fClipStack);
374 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000375 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 fNeedPrepareRenderTarget = false;
377 }
378}
379
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000380void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
381 const SkClipStack& clipStack) {
382 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
383 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000384 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000385}
386
387void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000388 const SkRegion& clip, const SkClipStack& clipStack) {
389
reed@google.comac10a2d2010-12-22 21:39:39 +0000390 fContext->setRenderTarget(fRenderTarget);
391
bsalomon@google.comd302f142011-03-03 13:54:13 +0000392 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000393
reed@google.com6f8f2922011-03-04 22:27:10 +0000394 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000395
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000396 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000397}
398
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000399SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000400 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000401 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000402}
403
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000404bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000405 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000406 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000407 return true;
408 }
409 return false;
410}
411
412///////////////////////////////////////////////////////////////////////////////
413
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000414SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
415SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
416SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
417SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
418SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
419 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000420SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
421 shader_type_mismatch);
422SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 5, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000423
bsalomon@google.com84405e02012-03-05 19:57:21 +0000424namespace {
425
426// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
427// justAlpha indicates that skPaint's alpha should be used rather than the color
428// Callers may subsequently modify the GrPaint. Setting constantColor indicates
429// that the final paint will draw the same color at every pixel. This allows
430// an optimization where the the color filter can be applied to the skPaint's
431// color once while converting to GrPain and then ignored.
432inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
433 bool justAlpha,
434 bool constantColor,
435 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000436
437 grPaint->fDither = skPaint.isDither();
438 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000439 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000440
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000441 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
442 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000443
444 SkXfermode* mode = skPaint.getXfermode();
445 if (mode) {
446 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000447 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000448#if 0
449 return false;
450#endif
451 }
452 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000453 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
454 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
455
bsalomon@google.com5782d712011-01-21 21:03:59 +0000456 if (justAlpha) {
457 uint8_t alpha = skPaint.getAlpha();
458 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000459 // justAlpha is currently set to true only if there is a texture,
460 // so constantColor should not also be true.
461 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000462 } else {
463 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000464 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000465 }
Scroggo97c88c22011-05-11 14:05:25 +0000466 SkColorFilter* colorFilter = skPaint.getColorFilter();
467 SkColor color;
468 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000469 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000470 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000471 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000472 if (!constantColor) {
473 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
474 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000475 } else {
476 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
477 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000478 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000479 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000480 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
481 grPaint->fColorMatrixEnabled = true;
482 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
483 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
484 } else {
485 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000486 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000487 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000488}
489
bsalomon@google.com84405e02012-03-05 19:57:21 +0000490// This function is similar to skPaint2GrPaintNoShader but also converts
491// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
492// be used is set on grPaint and returned in param act. constantColor has the
493// same meaning as in skPaint2GrPaintNoShader.
494inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
495 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000496 bool constantColor,
497 SkGpuDevice::SkAutoCachedTexture* act,
498 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000499
bsalomon@google.com5782d712011-01-21 21:03:59 +0000500 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000501
bsalomon@google.com5782d712011-01-21 21:03:59 +0000502 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000503 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000504 return skPaint2GrPaintNoShader(skPaint,
505 false,
506 constantColor,
507 grPaint);
508 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000509 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000510 }
511
reed@google.comac10a2d2010-12-22 21:39:39 +0000512 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000513 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000514 SkShader::TileMode tileModes[2];
515 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000516 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000517 tileModes, twoPointParams);
518
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000519 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000520 SkShader::GradientInfo info;
521 SkColor color;
522
523 info.fColors = &color;
524 info.fColorOffsets = NULL;
525 info.fColorCount = 1;
526 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
527 SkPaint copy(skPaint);
528 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000529 // modulate the paint alpha by the shader's solid color alpha
530 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
531 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000532 return skPaint2GrPaintNoShader(copy,
533 false,
534 constantColor,
535 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000536 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000537 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000538 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000539
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000540 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000541 GrTexture* texture = act->set(dev, bitmap, sampler);
542 if (NULL == texture) {
543 SkDebugf("Couldn't convert bitmap to texture.\n");
544 return false;
545 }
546 grPaint->setTexture(kShaderTextureIdx, texture);
547
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000548 switch (bmptype) {
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000549 case SkShader::kRadial_BitmapType:
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000550 sampler->setCustomStage(SkNEW_ARGS(GrRadialGradient, (texture)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000551 sampler->setFilter(GrSamplerState::kBilinear_Filter);
552 break;
553 case SkShader::kSweep_BitmapType:
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000554 sampler->setCustomStage(SkNEW_ARGS(GrSweepGradient, (texture)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000555 sampler->setFilter(GrSamplerState::kBilinear_Filter);
556 break;
557 case SkShader::kTwoPointRadial_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000558 sampler->setCustomStage(SkNEW_ARGS(GrRadial2Gradient,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000559 (texture,
560 twoPointParams[0],
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000561 twoPointParams[1],
562 twoPointParams[2] < 0)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000563 sampler->setFilter(GrSamplerState::kBilinear_Filter);
564 break;
rileya@google.com3e332582012-07-03 13:43:35 +0000565 case SkShader::kTwoPointConical_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000566 sampler->setCustomStage(SkNEW_ARGS(GrConical2Gradient,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000567 (texture,
568 twoPointParams[0],
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000569 twoPointParams[1],
570 twoPointParams[2])))->unref();
rileya@google.com3e332582012-07-03 13:43:35 +0000571 sampler->setFilter(GrSamplerState::kBilinear_Filter);
572 break;
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000573 default:
574 if (skPaint.isFilterBitmap()) {
575 sampler->setFilter(GrSamplerState::kBilinear_Filter);
576 } else {
577 sampler->setFilter(GrSamplerState::kNearest_Filter);
578 }
579 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000580 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000581 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
582 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000583
reed@google.comac10a2d2010-12-22 21:39:39 +0000584 // since our texture coords will be in local space, we wack the texture
585 // matrix to map them back into 0...1 before we load it
586 SkMatrix localM;
587 if (shader->getLocalMatrix(&localM)) {
588 SkMatrix inverse;
589 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000590 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000591 }
592 }
593 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000594 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
595 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000596 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000597 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000598 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000599 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000600 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000601
602 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000603}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000604}
reed@google.comac10a2d2010-12-22 21:39:39 +0000605
606///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000607void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000608 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000609}
610
reed@google.comac10a2d2010-12-22 21:39:39 +0000611void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
612 CHECK_SHOULD_DRAW(draw);
613
bsalomon@google.com5782d712011-01-21 21:03:59 +0000614 GrPaint grPaint;
615 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000616 if (!skPaint2GrPaintShader(this,
617 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000618 true,
619 &act,
620 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000621 return;
622 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000623
624 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000625}
626
627// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000628static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000629 kPoints_GrPrimitiveType,
630 kLines_GrPrimitiveType,
631 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000632};
633
634void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000635 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000636 CHECK_SHOULD_DRAW(draw);
637
638 SkScalar width = paint.getStrokeWidth();
639 if (width < 0) {
640 return;
641 }
642
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000643 // we only handle hairlines and paints without path effects or mask filters,
644 // else we let the SkDraw call our drawPath()
645 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000646 draw.drawPoints(mode, count, pts, paint, true);
647 return;
648 }
649
bsalomon@google.com5782d712011-01-21 21:03:59 +0000650 GrPaint grPaint;
651 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000652 if (!skPaint2GrPaintShader(this,
653 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000654 true,
655 &act,
656 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000657 return;
658 }
659
bsalomon@google.com5782d712011-01-21 21:03:59 +0000660 fContext->drawVertices(grPaint,
661 gPointMode2PrimtiveType[mode],
662 count,
663 (GrPoint*)pts,
664 NULL,
665 NULL,
666 NULL,
667 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000668}
669
reed@google.comc9aa5872011-04-05 21:05:37 +0000670///////////////////////////////////////////////////////////////////////////////
671
reed@google.comac10a2d2010-12-22 21:39:39 +0000672void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
673 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000674 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000675 CHECK_SHOULD_DRAW(draw);
676
bungeman@google.com79bd8772011-07-18 15:34:08 +0000677 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000678 SkScalar width = paint.getStrokeWidth();
679
680 /*
681 We have special code for hairline strokes, miter-strokes, and fills.
682 Anything else we just call our path code.
683 */
684 bool usePath = doStroke && width > 0 &&
685 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000686 // another two reasons we might need to call drawPath...
687 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000688 usePath = true;
689 }
reed@google.com67db6642011-05-26 11:46:35 +0000690 // until we aa rotated rects...
691 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
692 usePath = true;
693 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000694 // small miter limit means right angles show bevel...
695 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
696 paint.getStrokeMiter() < SK_ScalarSqrt2)
697 {
698 usePath = true;
699 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000700 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000701 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
702 usePath = true;
703 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000704
705 if (usePath) {
706 SkPath path;
707 path.addRect(rect);
708 this->drawPath(draw, path, paint, NULL, true);
709 return;
710 }
711
712 GrPaint grPaint;
713 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000714 if (!skPaint2GrPaintShader(this,
715 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000716 true,
717 &act,
718 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000719 return;
720 }
reed@google.com20efde72011-05-09 17:00:02 +0000721 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000722}
723
reed@google.com69302852011-02-16 18:08:07 +0000724#include "SkMaskFilter.h"
725#include "SkBounder.h"
726
bsalomon@google.com85003222012-03-28 14:44:37 +0000727///////////////////////////////////////////////////////////////////////////////
728
729// helpers for applying mask filters
730namespace {
731
732GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000733 switch (fillType) {
734 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000735 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000736 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000737 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000738 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000739 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000740 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000741 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000742 default:
743 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000744 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000745 }
746}
747
bsalomon@google.com85003222012-03-28 14:44:37 +0000748// We prefer to blur small rect with small radius via CPU.
749#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
750#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
751inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
752 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
753 rect.height() <= MIN_GPU_BLUR_SIZE &&
754 radius <= MIN_GPU_BLUR_RADIUS) {
755 return true;
756 }
757 return false;
758}
759
760bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
761 SkMaskFilter* filter, const SkMatrix& matrix,
762 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000763 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000764#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000765 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000766#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000767 SkMaskFilter::BlurInfo info;
768 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000769 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000770 return false;
771 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000772 SkScalar radius = info.fIgnoreTransform ? info.fRadius
773 : matrix.mapRadius(info.fRadius);
774 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000775 if (radius <= 0) {
776 return false;
777 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000778
779 SkRect srcRect = path.getBounds();
780 if (shouldDrawBlurWithCPU(srcRect, radius)) {
781 return false;
782 }
783
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000784 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000785 float sigma3 = sigma * 3.0f;
786
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000787 SkRect clipRect;
788 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000789
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000790 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000791 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
792 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000793 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000794 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000795 SkIRect finalIRect;
796 finalRect.roundOut(&finalIRect);
797 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000798 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000799 }
800 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000801 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000802 }
803 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000804 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000805 GrTextureDesc desc;
806 desc.fFlags = kRenderTarget_GrTextureFlagBit;
807 desc.fWidth = SkScalarCeilToInt(srcRect.width());
808 desc.fHeight = SkScalarCeilToInt(srcRect.height());
809 // We actually only need A8, but it often isn't supported as a
810 // render target so default to RGBA_8888
811 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000812
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000813 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
814 desc.fConfig = kAlpha_8_GrPixelConfig;
815 }
816
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000817 GrAutoScratchTexture pathEntry(context, desc);
818 GrTexture* pathTexture = pathEntry.texture();
819 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000820 return false;
821 }
822 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000823 // Once this code moves into GrContext, this should be changed to use
824 // an AutoClipRestore.
825 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000826 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000827
828 GrClip newClip(srcRect);
829 context->setClip(newClip);
830
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000831 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000832 GrPaint tempPaint;
833 tempPaint.reset();
834
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000835 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000836 tempPaint.fAntiAlias = grp->fAntiAlias;
837 if (tempPaint.fAntiAlias) {
838 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
839 // blend coeff of zero requires dual source blending support in order
840 // to properly blend partially covered pixels. This means the AA
841 // code path may not be taken. So we use a dst blend coeff of ISA. We
842 // could special case AA draws to a dst surface with known alpha=0 to
843 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000844 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
845 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000846 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000847 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000848 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000849
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000850 GrAutoScratchTexture temp1, temp2;
851 // If we're doing a normal blur, we can clobber the pathTexture in the
852 // gaussianBlur. Otherwise, we need to save it for later compositing.
853 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000854 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
855 &temp1,
856 isNormalBlur ? NULL : &temp2,
857 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000858
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000859 if (!isNormalBlur) {
860 GrPaint paint;
861 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000862 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000863 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
864 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000865 // Blend pathTexture over blurTexture.
866 context->setRenderTarget(blurTexture->asRenderTarget());
867 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000868 if (SkMaskFilter::kInner_BlurType == blurType) {
869 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000870 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
871 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000872 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
873 // solid: dst = src + dst - src * dst
874 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000875 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
876 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000877 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
878 // outer: dst = dst * (1 - src)
879 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000880 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
881 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000882 }
883 context->drawRect(paint, srcRect);
884 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000885 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000886 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000887
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000888 if (grp->hasTextureOrMask()) {
889 GrMatrix inverse;
890 if (!matrix.invert(&inverse)) {
891 return false;
892 }
893 grp->preConcatActiveSamplerMatrices(inverse);
894 }
895
896 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
897 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000898 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000899 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000900 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000901
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000902 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
903 -finalRect.fTop);
904 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
905 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000906 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000907 return true;
908}
909
bsalomon@google.com85003222012-03-28 14:44:37 +0000910bool drawWithMaskFilter(GrContext* context, const SkPath& path,
911 SkMaskFilter* filter, const SkMatrix& matrix,
912 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000913 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000914 SkMask srcM, dstM;
915
916 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000917 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000918 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000919 return false;
920 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000921 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000922
923 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
924 return false;
925 }
926 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000927 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000928
929 if (clip.quickReject(dstM.fBounds)) {
930 return false;
931 }
932 if (bounder && !bounder->doIRect(dstM.fBounds)) {
933 return false;
934 }
935
936 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
937 // the current clip (and identity matrix) and grpaint settings
938
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000939 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000940 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000941
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000942 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000943
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000944 GrTextureDesc desc;
945 desc.fWidth = dstM.fBounds.width();
946 desc.fHeight = dstM.fBounds.height();
947 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000948
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000949 GrAutoScratchTexture ast(context, desc);
950 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000951
reed@google.com69302852011-02-16 18:08:07 +0000952 if (NULL == texture) {
953 return false;
954 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000955 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000956 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000957
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000958 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
959 grp->preConcatActiveSamplerMatrices(ivm);
960 }
961
962 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
963 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000964 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000965 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000966 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000967
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000968 GrRect d;
969 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000970 GrIntToScalar(dstM.fBounds.fTop),
971 GrIntToScalar(dstM.fBounds.fRight),
972 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000973
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000974 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
975 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
976 -dstM.fBounds.fTop*SK_Scalar1);
977 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000978 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000979 return true;
980}
reed@google.com69302852011-02-16 18:08:07 +0000981
bsalomon@google.com85003222012-03-28 14:44:37 +0000982}
983
984///////////////////////////////////////////////////////////////////////////////
985
reed@google.com0c219b62011-02-16 21:31:18 +0000986void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000987 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000988 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000989 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000990 CHECK_SHOULD_DRAW(draw);
991
reed@google.comfe626382011-09-21 13:50:35 +0000992 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000993
bsalomon@google.com5782d712011-01-21 21:03:59 +0000994 GrPaint grPaint;
995 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000996 if (!skPaint2GrPaintShader(this,
997 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000998 true,
999 &act,
1000 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001001 return;
1002 }
1003
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001004 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1005 // if we can, we draw lots faster (raster device does this same test)
1006 SkScalar hairlineCoverage;
1007 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1008 doFill = false;
1009 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1010 grPaint.fCoverage);
1011 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001012
reed@google.comfe626382011-09-21 13:50:35 +00001013 // If we have a prematrix, apply it to the path, optimizing for the case
1014 // where the original path can in fact be modified in place (even though
1015 // its parameter type is const).
1016 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1017 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001018
1019 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001020 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001021
reed@google.come3445642011-02-16 23:20:39 +00001022 if (!pathIsMutable) {
1023 result = &tmpPath;
1024 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001025 }
reed@google.come3445642011-02-16 23:20:39 +00001026 // should I push prePathMatrix on our MV stack temporarily, instead
1027 // of applying it here? See SkDraw.cpp
1028 pathPtr->transform(*prePathMatrix, result);
1029 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001030 }
reed@google.com0c219b62011-02-16 21:31:18 +00001031 // at this point we're done with prePathMatrix
1032 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001033
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001034 if (paint.getPathEffect() ||
1035 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001036 // it is safe to use tmpPath here, even if we already used it for the
1037 // prepathmatrix, since getFillPath can take the same object for its
1038 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001039 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001040 pathPtr = &tmpPath;
1041 }
1042
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001043 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001044 // avoid possibly allocating a new path in transform if we can
1045 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1046
1047 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001048 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001049 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001050 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001051 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001052 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001053 &grPaint, pathFillType)) {
1054 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1055 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001056 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001057 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001058 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001059 }
reed@google.com69302852011-02-16 18:08:07 +00001060 return;
1061 }
reed@google.com69302852011-02-16 18:08:07 +00001062
bsalomon@google.com47059542012-06-06 20:51:20 +00001063 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001064
reed@google.com0c219b62011-02-16 21:31:18 +00001065 if (doFill) {
1066 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001067 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001068 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001069 break;
1070 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001071 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001072 break;
1073 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001074 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001075 break;
1076 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001077 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001078 break;
1079 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001080 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001081 return;
1082 }
1083 }
1084
reed@google.com07f3ee12011-05-16 17:21:57 +00001085 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001086}
1087
bsalomon@google.comfb309512011-11-30 14:13:48 +00001088namespace {
1089
1090inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1091 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1092 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1093 return tilesX * tilesY;
1094}
1095
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001096inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001097 const SkIRect* srcRectPtr,
1098 int maxTextureSize) {
1099 static const int kSmallTileSize = 1 << 10;
1100 if (maxTextureSize <= kSmallTileSize) {
1101 return maxTextureSize;
1102 }
1103
1104 size_t maxTexTotalTileSize;
1105 size_t smallTotalTileSize;
1106
1107 if (NULL == srcRectPtr) {
1108 int w = bitmap.width();
1109 int h = bitmap.height();
1110 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1111 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1112 } else {
1113 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1114 srcRectPtr->fTop,
1115 srcRectPtr->fRight,
1116 srcRectPtr->fBottom,
1117 maxTextureSize);
1118 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1119 srcRectPtr->fTop,
1120 srcRectPtr->fRight,
1121 srcRectPtr->fBottom,
1122 kSmallTileSize);
1123 }
1124 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1125 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1126
1127 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1128 return kSmallTileSize;
1129 } else {
1130 return maxTextureSize;
1131 }
1132}
1133}
1134
1135bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1136 const GrSamplerState& sampler,
1137 const SkIRect* srcRectPtr,
1138 int* tileSize) const {
1139 SkASSERT(NULL != tileSize);
1140
1141 // if bitmap is explictly texture backed then just use the texture
1142 if (NULL != bitmap.getTexture()) {
1143 return false;
1144 }
1145 // if it's larger than the max texture size, then we have no choice but
1146 // tiling
1147 const int maxTextureSize = fContext->getMaxTextureSize();
1148 if (bitmap.width() > maxTextureSize ||
1149 bitmap.height() > maxTextureSize) {
1150 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1151 return true;
1152 }
1153 // if we are going to have to draw the whole thing, then don't tile
1154 if (NULL == srcRectPtr) {
1155 return false;
1156 }
1157 // if the entire texture is already in our cache then no reason to tile it
1158 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1159 return false;
1160 }
1161
1162 // At this point we know we could do the draw by uploading the entire bitmap
1163 // as a texture. However, if the texture would be large compared to the
1164 // cache size and we don't require most of it for this draw then tile to
1165 // reduce the amount of upload and cache spill.
1166
1167 // assumption here is that sw bitmap size is a good proxy for its size as
1168 // a texture
1169 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001170 size_t cacheSize;
1171 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001172 if (bmpSize < cacheSize / 2) {
1173 return false;
1174 }
1175
1176 SkFixed fracUsed =
1177 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1178 (srcRectPtr->height() << 16) / bitmap.height());
1179 if (fracUsed <= SK_FixedHalf) {
1180 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1181 return true;
1182 } else {
1183 return false;
1184 }
1185}
1186
reed@google.comac10a2d2010-12-22 21:39:39 +00001187void SkGpuDevice::drawBitmap(const SkDraw& draw,
1188 const SkBitmap& bitmap,
1189 const SkIRect* srcRectPtr,
1190 const SkMatrix& m,
1191 const SkPaint& paint) {
1192 CHECK_SHOULD_DRAW(draw);
1193
1194 SkIRect srcRect;
1195 if (NULL == srcRectPtr) {
1196 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1197 } else {
1198 srcRect = *srcRectPtr;
1199 }
1200
junov@google.comd935cfb2011-06-27 20:48:23 +00001201 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001202 // Convert the bitmap to a shader so that the rect can be drawn
1203 // through drawRect, which supports mask filters.
1204 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001205 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001206 if (srcRectPtr) {
1207 if (!bitmap.extractSubset(&tmp, srcRect)) {
1208 return; // extraction failed
1209 }
1210 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001211 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001212 }
1213 SkPaint paintWithTexture(paint);
1214 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1215 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001216 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001217 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001218
junov@google.com1d329782011-07-28 20:10:09 +00001219 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001220 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001221 // also affect the behavior of the mask filter.
1222 SkMatrix drawMatrix;
1223 drawMatrix.setConcat(*draw.fMatrix, m);
1224 SkDraw transformedDraw(draw);
1225 transformedDraw.fMatrix = &drawMatrix;
1226
1227 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1228
junov@google.comd935cfb2011-06-27 20:48:23 +00001229 return;
1230 }
1231
bsalomon@google.com5782d712011-01-21 21:03:59 +00001232 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001233 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001234 return;
1235 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001236 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001237 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001238 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001239 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001240 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001241 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001242
bsalomon@google.comfb309512011-11-30 14:13:48 +00001243 int tileSize;
1244 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1245 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001246 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001247 return;
1248 }
1249
1250 // undo the translate done by SkCanvas
1251 int DX = SkMax32(0, srcRect.fLeft);
1252 int DY = SkMax32(0, srcRect.fTop);
1253 // compute clip bounds in local coordinates
1254 SkIRect clipRect;
1255 {
1256 SkRect r;
1257 r.set(draw.fClip->getBounds());
1258 SkMatrix matrix, inverse;
1259 matrix.setConcat(*draw.fMatrix, m);
1260 if (!matrix.invert(&inverse)) {
1261 return;
1262 }
1263 inverse.mapRect(&r);
1264 r.roundOut(&clipRect);
1265 // apply the canvas' translate to our local clip
1266 clipRect.offset(DX, DY);
1267 }
1268
bsalomon@google.comfb309512011-11-30 14:13:48 +00001269 int nx = bitmap.width() / tileSize;
1270 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001271 for (int x = 0; x <= nx; x++) {
1272 for (int y = 0; y <= ny; y++) {
1273 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001274 tileR.set(x * tileSize, y * tileSize,
1275 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001276 if (!SkIRect::Intersects(tileR, clipRect)) {
1277 continue;
1278 }
1279
1280 SkIRect srcR = tileR;
1281 if (!srcR.intersect(srcRect)) {
1282 continue;
1283 }
1284
1285 SkBitmap tmpB;
1286 if (bitmap.extractSubset(&tmpB, tileR)) {
1287 // now offset it to make it "local" to our tmp bitmap
1288 srcR.offset(-tileR.fLeft, -tileR.fTop);
1289
1290 SkMatrix tmpM(m);
1291 {
1292 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1293 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1294 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1295 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001296 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001297 }
1298 }
1299 }
1300}
1301
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001302namespace {
1303
1304bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1305 // detect pixel disalignment
1306 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1307 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1308 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1309 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1310 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1311 COLOR_BLEED_TOLERANCE &&
1312 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1313 COLOR_BLEED_TOLERANCE) {
1314 return true;
1315 }
1316 return false;
1317}
1318
1319bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1320 const SkMatrix& m) {
1321 // Only gets called if hasAlignedSamples returned false.
1322 // So we can assume that sampling is axis aligned but not texel aligned.
1323 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1324 SkRect innerSrcRect(srcRect), innerTransformedRect,
1325 outerTransformedRect(transformedRect);
1326 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1327 m.mapRect(&innerTransformedRect, innerSrcRect);
1328
1329 // The gap between outerTransformedRect and innerTransformedRect
1330 // represents the projection of the source border area, which is
1331 // problematic for color bleeding. We must check whether any
1332 // destination pixels sample the border area.
1333 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1334 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1335 SkIRect outer, inner;
1336 outerTransformedRect.round(&outer);
1337 innerTransformedRect.round(&inner);
1338 // If the inner and outer rects round to the same result, it means the
1339 // border does not overlap any pixel centers. Yay!
1340 return inner != outer;
1341}
1342
1343} // unnamed namespace
1344
reed@google.comac10a2d2010-12-22 21:39:39 +00001345/*
1346 * This is called by drawBitmap(), which has to handle images that may be too
1347 * large to be represented by a single texture.
1348 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001349 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1350 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001351 */
1352void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1353 const SkBitmap& bitmap,
1354 const SkIRect& srcRect,
1355 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001356 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001357 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1358 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001359
reed@google.com9c49bc32011-07-07 13:42:37 +00001360 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001361 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001362 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001363 return;
1364 }
1365
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001366 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001367
1368 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1369 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001370 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001371
1372 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001373 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001374 if (NULL == texture) {
1375 return;
1376 }
1377
bsalomon@google.com452943d2011-10-31 17:37:14 +00001378 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001379
reed@google.com20efde72011-05-09 17:00:02 +00001380 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1381 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001382 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001383 float wInv = 1.f / bitmap.width();
1384 float hInv = 1.f / bitmap.height();
1385 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1386 SkFloatToScalar(srcRect.fTop * hInv),
1387 SkFloatToScalar(srcRect.fRight * wInv),
1388 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001389
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001390 bool needsTextureDomain = false;
1391 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1392 {
1393 // Need texture domain if drawing a sub rect.
1394 needsTextureDomain = srcRect.width() < bitmap.width() ||
1395 srcRect.height() < bitmap.height();
1396 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1397 // sampling is axis-aligned
1398 GrRect floatSrcRect, transformedRect;
1399 floatSrcRect.set(srcRect);
1400 SkMatrix srcToDeviceMatrix(m);
1401 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1402 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1403
1404 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1405 // Samples are texel-aligned, so filtering is futile
1406 sampler->setFilter(GrSamplerState::kNearest_Filter);
1407 needsTextureDomain = false;
1408 } else {
1409 needsTextureDomain = needsTextureDomain &&
1410 mayColorBleed(floatSrcRect, transformedRect, m);
1411 }
1412 }
1413 }
1414
1415 GrRect textureDomain = GrRect::MakeEmpty();
1416
1417 if (needsTextureDomain) {
1418 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001419 GrScalar left, top, right, bottom;
1420 if (srcRect.width() > 1) {
1421 GrScalar border = GR_ScalarHalf / bitmap.width();
1422 left = paintRect.left() + border;
1423 right = paintRect.right() - border;
1424 } else {
1425 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1426 }
1427 if (srcRect.height() > 1) {
1428 GrScalar border = GR_ScalarHalf / bitmap.height();
1429 top = paintRect.top() + border;
1430 bottom = paintRect.bottom() - border;
1431 } else {
1432 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1433 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001434 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001435 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001436 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001437
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001438 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001439}
1440
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001441namespace {
1442
1443void apply_custom_stage(GrContext* context,
1444 GrTexture* srcTexture,
1445 GrTexture* dstTexture,
1446 const GrRect& rect,
1447 GrCustomStage* stage) {
1448 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001449 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001450 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001451 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001452
1453 GrMatrix sampleM;
1454 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1455 GrPaint paint;
1456 paint.reset();
1457 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1458 paint.textureSampler(0)->reset(sampleM);
1459 paint.textureSampler(0)->setCustomStage(stage);
1460 paint.setTexture(0, srcTexture);
1461 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001462}
1463
1464};
1465
reed@google.com8926b162012-03-23 15:36:36 +00001466static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1467 SkImageFilter* filter, const GrRect& rect) {
1468 GrAssert(filter);
1469
1470 SkSize blurSize;
1471 SkISize radius;
1472
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001473 GrTextureDesc desc;
1474 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1475 desc.fWidth = SkScalarCeilToInt(rect.width());
1476 desc.fHeight = SkScalarCeilToInt(rect.height());
1477 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001478 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001479
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001480 if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001481 GrAutoScratchTexture dst(context, desc);
1482 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1483 texture = dst.detach();
1484 stage->unref();
1485 } else if (filter->asABlur(&blurSize)) {
reed@google.com8926b162012-03-23 15:36:36 +00001486 GrAutoScratchTexture temp1, temp2;
1487 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1488 blurSize.width(),
1489 blurSize.height());
1490 texture->ref();
1491 } else if (filter->asADilate(&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::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001496 radius);
1497 texture->ref();
1498 } else if (filter->asAnErode(&radius)) {
1499 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1500 texture = context->applyMorphology(texture, rect,
1501 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001502 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001503 radius);
1504 texture->ref();
1505 }
1506 return texture;
1507}
1508
reed@google.comac10a2d2010-12-22 21:39:39 +00001509void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1510 int left, int top, const SkPaint& paint) {
1511 CHECK_SHOULD_DRAW(draw);
1512
reed@google.com8926b162012-03-23 15:36:36 +00001513 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001514 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1515 return;
1516 }
1517
reed@google.com76dd2772012-01-05 21:15:07 +00001518 int w = bitmap.width();
1519 int h = bitmap.height();
1520
bsalomon@google.com5782d712011-01-21 21:03:59 +00001521 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001522 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001523 return;
1524 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001525
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001526 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001527
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001528 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001529
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001530 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001531 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001532 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001533 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001534
reed@google.com8926b162012-03-23 15:36:36 +00001535 SkImageFilter* filter = paint.getImageFilter();
1536 if (NULL != filter) {
1537 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001538 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001539 if (filteredTexture) {
1540 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1541 texture = filteredTexture;
1542 filteredTexture->unref();
1543 }
reed@google.com76dd2772012-01-05 21:15:07 +00001544 }
reed@google.com8926b162012-03-23 15:36:36 +00001545
bsalomon@google.com5782d712011-01-21 21:03:59 +00001546 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001547 GrRect::MakeXYWH(GrIntToScalar(left),
1548 GrIntToScalar(top),
1549 GrIntToScalar(w),
1550 GrIntToScalar(h)),
1551 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1552 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001553}
1554
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001555void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001556 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001557 // clear of the source device must occur before CHECK_SHOULD_DRAW
1558 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1559 if (dev->fNeedClear) {
1560 // TODO: could check here whether we really need to draw at all
1561 dev->clear(0x0);
1562 }
1563
reed@google.comac10a2d2010-12-22 21:39:39 +00001564 CHECK_SHOULD_DRAW(draw);
1565
bsalomon@google.com5782d712011-01-21 21:03:59 +00001566 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001567 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001568 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001569 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001570 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001571
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001572 GrTexture* devTex = grPaint.getTexture(0);
1573 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001574
reed@google.com8926b162012-03-23 15:36:36 +00001575 SkImageFilter* filter = paint.getImageFilter();
1576 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001577 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1578 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001579 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1580 rect);
1581 if (filteredTexture) {
1582 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1583 devTex = filteredTexture;
1584 filteredTexture->unref();
1585 }
1586 }
1587
bsalomon@google.com5782d712011-01-21 21:03:59 +00001588 const SkBitmap& bm = dev->accessBitmap(false);
1589 int w = bm.width();
1590 int h = bm.height();
1591
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001592 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001593
bsalomon@google.com97912912011-12-06 16:30:36 +00001594 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001595
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001596 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1597 GrIntToScalar(y),
1598 GrIntToScalar(w),
1599 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001600
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001601 // The device being drawn may not fill up its texture (saveLayer uses
1602 // the approximate ).
1603 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1604 GR_Scalar1 * h / devTex->height());
1605
1606 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001607}
1608
reed@google.com8926b162012-03-23 15:36:36 +00001609bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001610 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001611 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001612
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001613 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001614 !filter->asABlur(&size) &&
1615 !filter->asADilate(&radius) &&
1616 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001617 return false;
1618 }
reed@google.com8926b162012-03-23 15:36:36 +00001619 return true;
1620}
1621
1622bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1623 const SkMatrix& ctm,
1624 SkBitmap* result, SkIPoint* offset) {
1625 // want explicitly our impl, so guard against a subclass of us overriding it
1626 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001627 return false;
1628 }
reed@google.com8926b162012-03-23 15:36:36 +00001629
1630 SkAutoLockPixels alp(src, !src.getTexture());
1631 if (!src.getTexture() && !src.readyToDraw()) {
1632 return false;
1633 }
1634
1635 GrPaint paint;
1636 paint.reset();
1637
1638 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1639
1640 GrTexture* texture;
1641 SkAutoCachedTexture act(this, src, sampler, &texture);
1642
1643 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001644 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1645 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001646 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1647 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001648 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1649 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001650 resultTexture->unref();
1651 }
reed@google.com76dd2772012-01-05 21:15:07 +00001652 return true;
1653}
1654
reed@google.comac10a2d2010-12-22 21:39:39 +00001655///////////////////////////////////////////////////////////////////////////////
1656
1657// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001658static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001659 kTriangles_GrPrimitiveType,
1660 kTriangleStrip_GrPrimitiveType,
1661 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001662};
1663
1664void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1665 int vertexCount, const SkPoint vertices[],
1666 const SkPoint texs[], const SkColor colors[],
1667 SkXfermode* xmode,
1668 const uint16_t indices[], int indexCount,
1669 const SkPaint& paint) {
1670 CHECK_SHOULD_DRAW(draw);
1671
bsalomon@google.com5782d712011-01-21 21:03:59 +00001672 GrPaint grPaint;
1673 SkAutoCachedTexture act;
1674 // we ignore the shader if texs is null.
1675 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001676 if (!skPaint2GrPaintNoShader(paint,
1677 false,
1678 NULL == colors,
1679 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001680 return;
1681 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001682 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001683 if (!skPaint2GrPaintShader(this,
1684 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001685 NULL == colors,
1686 &act,
1687 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001688 return;
1689 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001690 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001691
1692 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001693 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001694 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1695#if 0
1696 return
1697#endif
1698 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001699 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001700
bsalomon@google.com498776a2011-08-16 19:20:44 +00001701 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1702 if (NULL != colors) {
1703 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001704 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001705 for (int i = 0; i < vertexCount; ++i) {
1706 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1707 }
1708 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001709 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001710 fContext->drawVertices(grPaint,
1711 gVertexMode2PrimitiveType[vmode],
1712 vertexCount,
1713 (GrPoint*) vertices,
1714 (GrPoint*) texs,
1715 colors,
1716 indices,
1717 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001718}
1719
1720///////////////////////////////////////////////////////////////////////////////
1721
1722static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001723 GrFontScaler* scaler = (GrFontScaler*)data;
1724 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001725}
1726
1727static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1728 void* auxData;
1729 GrFontScaler* scaler = NULL;
1730 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1731 scaler = (GrFontScaler*)auxData;
1732 }
1733 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001734 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001735 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1736 }
1737 return scaler;
1738}
1739
1740static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1741 SkFixed fx, SkFixed fy,
1742 const SkGlyph& glyph) {
1743 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1744
bungeman@google.com15865a72012-01-11 16:28:04 +00001745 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001746
1747 if (NULL == procs->fFontScaler) {
1748 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1749 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001750
bungeman@google.com15865a72012-01-11 16:28:04 +00001751 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1752 glyph.getSubXFixed(),
1753 glyph.getSubYFixed()),
1754 SkFixedFloorToFixed(fx),
1755 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 procs->fFontScaler);
1757}
1758
bsalomon@google.com5782d712011-01-21 21:03:59 +00001759SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001760
1761 // deferred allocation
1762 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001763 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001764 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1765 fDrawProcs->fContext = fContext;
1766 }
1767
1768 // init our (and GL's) state
1769 fDrawProcs->fTextContext = context;
1770 fDrawProcs->fFontScaler = NULL;
1771 return fDrawProcs;
1772}
1773
1774void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1775 size_t byteLength, SkScalar x, SkScalar y,
1776 const SkPaint& paint) {
1777 CHECK_SHOULD_DRAW(draw);
1778
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001779 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001780 // this guy will just call our drawPath()
1781 draw.drawText((const char*)text, byteLength, x, y, paint);
1782 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001783 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001784
1785 GrPaint grPaint;
1786 SkAutoCachedTexture act;
1787
bsalomon@google.com84405e02012-03-05 19:57:21 +00001788 if (!skPaint2GrPaintShader(this,
1789 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001790 true,
1791 &act,
1792 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001793 return;
1794 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001795 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1796 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001797 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1798 }
1799}
1800
1801void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1802 size_t byteLength, const SkScalar pos[],
1803 SkScalar constY, int scalarsPerPos,
1804 const SkPaint& paint) {
1805 CHECK_SHOULD_DRAW(draw);
1806
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001807 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001808 // this guy will just call our drawPath()
1809 draw.drawPosText((const char*)text, byteLength, pos, constY,
1810 scalarsPerPos, paint);
1811 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001812 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001813
1814 GrPaint grPaint;
1815 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001816 if (!skPaint2GrPaintShader(this,
1817 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001818 true,
1819 &act,
1820 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001821 return;
1822 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001823 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1824 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001825 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1826 scalarsPerPos, paint);
1827 }
1828}
1829
1830void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1831 size_t len, const SkPath& path,
1832 const SkMatrix* m, const SkPaint& paint) {
1833 CHECK_SHOULD_DRAW(draw);
1834
1835 SkASSERT(draw.fDevice == this);
1836 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1837}
1838
1839///////////////////////////////////////////////////////////////////////////////
1840
reed@google.comf67e4cf2011-03-15 20:56:58 +00001841bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1842 if (!paint.isLCDRenderText()) {
1843 // we're cool with the paint as is
1844 return false;
1845 }
1846
1847 if (paint.getShader() ||
1848 paint.getXfermode() || // unless its srcover
1849 paint.getMaskFilter() ||
1850 paint.getRasterizer() ||
1851 paint.getColorFilter() ||
1852 paint.getPathEffect() ||
1853 paint.isFakeBoldText() ||
1854 paint.getStyle() != SkPaint::kFill_Style) {
1855 // turn off lcd
1856 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1857 flags->fHinting = paint.getHinting();
1858 return true;
1859 }
1860 // we're cool with the paint as is
1861 return false;
1862}
1863
reed@google.com75d939b2011-12-07 15:07:23 +00001864void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001865 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001866 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001867}
1868
reed@google.comf67e4cf2011-03-15 20:56:58 +00001869///////////////////////////////////////////////////////////////////////////////
1870
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001871SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1872 const SkBitmap& bitmap,
1873 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001874 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001875 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001876
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001877 if (!bitmap.isVolatile()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001878 uint64_t key = bitmap.getGenerationID();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001879 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001880
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001881 GrTextureDesc desc;
1882 desc.fWidth = bitmap.width();
1883 desc.fHeight = bitmap.height();
1884 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
robertphillips@google.com56f22442012-06-08 14:21:26 +00001885 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001886
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001887 entry = ctx->findAndLockTexture(desc, sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001888 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001889 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1890 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001891 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001892 } else {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001893 entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001894 sampler, bitmap);
1895 }
1896 if (NULL == entry.texture()) {
1897 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1898 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001899 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001900 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001901}
1902
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001903void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1904 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001905}
1906
bsalomon@google.comfb309512011-11-30 14:13:48 +00001907bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1908 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001909 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001910 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001911
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001912 GrTextureDesc desc;
1913 desc.fWidth = bitmap.width();
1914 desc.fHeight = bitmap.height();
1915 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
1916 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001917
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001918 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001919}
1920
1921
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001922SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1923 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001924 bool isOpaque,
1925 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001926 GrTextureDesc desc;
1927 desc.fConfig = fRenderTarget->config();
1928 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1929 desc.fWidth = width;
1930 desc.fHeight = height;
1931 desc.fSampleCnt = fRenderTarget->numSamples();
1932
1933 GrContext::TextureCacheEntry cacheEntry;
1934 GrTexture* texture;
1935 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001936 // Skia's convention is to only clear a device if it is non-opaque.
1937 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001938
1939#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1940 // layers are never draw in repeat modes, so we can request an approx
1941 // match and ignore any padding.
1942 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1943 GrContext::kApprox_ScratchTexMatch :
1944 GrContext::kExact_ScratchTexMatch;
1945 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1946 texture = cacheEntry.texture();
1947#else
1948 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1949 texture = tunref.get();
1950#endif
1951 if (texture) {
1952 return SkNEW_ARGS(SkGpuDevice,(fContext,
1953 texture,
1954 cacheEntry,
1955 needClear));
1956 } else {
1957 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1958 width, height);
1959 return NULL;
1960 }
1961}
1962
1963SkGpuDevice::SkGpuDevice(GrContext* context,
1964 GrTexture* texture,
1965 TexCache cacheEntry,
1966 bool needClear)
1967 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1968 GrAssert(texture && texture->asRenderTarget());
1969 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1970 this->initFromRenderTarget(context, texture->asRenderTarget());
1971 fCache = cacheEntry;
1972 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001973}
1974