blob: fc77c470916c422dcbd2d305db3fd017d01c8479 [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 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000539 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000540 switch (bmptype) {
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000541 case SkShader::kRadial_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000542 sampler->setCustomStage(SkNEW(GrRadialGradient))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000543 sampler->setFilter(GrSamplerState::kBilinear_Filter);
544 break;
545 case SkShader::kSweep_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000546 sampler->setCustomStage(SkNEW(GrSweepGradient))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000547 sampler->setFilter(GrSamplerState::kBilinear_Filter);
548 break;
549 case SkShader::kTwoPointRadial_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000550 sampler->setCustomStage(SkNEW_ARGS(GrRadial2Gradient,
551 (twoPointParams[0],
552 twoPointParams[1],
553 twoPointParams[2] < 0)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000554 sampler->setFilter(GrSamplerState::kBilinear_Filter);
555 break;
rileya@google.com3e332582012-07-03 13:43:35 +0000556 case SkShader::kTwoPointConical_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000557 sampler->setCustomStage(SkNEW_ARGS(GrConical2Gradient,
558 (twoPointParams[0],
559 twoPointParams[1],
560 twoPointParams[2])))->unref();
rileya@google.com3e332582012-07-03 13:43:35 +0000561 sampler->setFilter(GrSamplerState::kBilinear_Filter);
562 break;
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000563 default:
564 if (skPaint.isFilterBitmap()) {
565 sampler->setFilter(GrSamplerState::kBilinear_Filter);
566 } else {
567 sampler->setFilter(GrSamplerState::kNearest_Filter);
568 }
569 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000570 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000571 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
572 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000573
bsalomon@google.com84405e02012-03-05 19:57:21 +0000574 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000575 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000576 SkDebugf("Couldn't convert bitmap to texture.\n");
577 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000578 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000579 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000580
581 // since our texture coords will be in local space, we wack the texture
582 // matrix to map them back into 0...1 before we load it
583 SkMatrix localM;
584 if (shader->getLocalMatrix(&localM)) {
585 SkMatrix inverse;
586 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000587 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000588 }
589 }
590 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000591 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
592 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000593 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000594 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000595 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000596 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000597 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000598
599 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000600}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000601}
reed@google.comac10a2d2010-12-22 21:39:39 +0000602
603///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000604void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000605 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000606}
607
reed@google.comac10a2d2010-12-22 21:39:39 +0000608void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
609 CHECK_SHOULD_DRAW(draw);
610
bsalomon@google.com5782d712011-01-21 21:03:59 +0000611 GrPaint grPaint;
612 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000613 if (!skPaint2GrPaintShader(this,
614 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000615 true,
616 &act,
617 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000618 return;
619 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000620
621 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000622}
623
624// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000625static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000626 kPoints_GrPrimitiveType,
627 kLines_GrPrimitiveType,
628 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000629};
630
631void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000632 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000633 CHECK_SHOULD_DRAW(draw);
634
635 SkScalar width = paint.getStrokeWidth();
636 if (width < 0) {
637 return;
638 }
639
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000640 // we only handle hairlines and paints without path effects or mask filters,
641 // else we let the SkDraw call our drawPath()
642 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000643 draw.drawPoints(mode, count, pts, paint, true);
644 return;
645 }
646
bsalomon@google.com5782d712011-01-21 21:03:59 +0000647 GrPaint grPaint;
648 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000649 if (!skPaint2GrPaintShader(this,
650 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000651 true,
652 &act,
653 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000654 return;
655 }
656
bsalomon@google.com5782d712011-01-21 21:03:59 +0000657 fContext->drawVertices(grPaint,
658 gPointMode2PrimtiveType[mode],
659 count,
660 (GrPoint*)pts,
661 NULL,
662 NULL,
663 NULL,
664 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000665}
666
reed@google.comc9aa5872011-04-05 21:05:37 +0000667///////////////////////////////////////////////////////////////////////////////
668
reed@google.comac10a2d2010-12-22 21:39:39 +0000669void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
670 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000671 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000672 CHECK_SHOULD_DRAW(draw);
673
bungeman@google.com79bd8772011-07-18 15:34:08 +0000674 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000675 SkScalar width = paint.getStrokeWidth();
676
677 /*
678 We have special code for hairline strokes, miter-strokes, and fills.
679 Anything else we just call our path code.
680 */
681 bool usePath = doStroke && width > 0 &&
682 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000683 // another two reasons we might need to call drawPath...
684 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000685 usePath = true;
686 }
reed@google.com67db6642011-05-26 11:46:35 +0000687 // until we aa rotated rects...
688 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
689 usePath = true;
690 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000691 // small miter limit means right angles show bevel...
692 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
693 paint.getStrokeMiter() < SK_ScalarSqrt2)
694 {
695 usePath = true;
696 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000697 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000698 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
699 usePath = true;
700 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000701
702 if (usePath) {
703 SkPath path;
704 path.addRect(rect);
705 this->drawPath(draw, path, paint, NULL, true);
706 return;
707 }
708
709 GrPaint grPaint;
710 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000711 if (!skPaint2GrPaintShader(this,
712 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000713 true,
714 &act,
715 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000716 return;
717 }
reed@google.com20efde72011-05-09 17:00:02 +0000718 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000719}
720
reed@google.com69302852011-02-16 18:08:07 +0000721#include "SkMaskFilter.h"
722#include "SkBounder.h"
723
bsalomon@google.com85003222012-03-28 14:44:37 +0000724///////////////////////////////////////////////////////////////////////////////
725
726// helpers for applying mask filters
727namespace {
728
729GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000730 switch (fillType) {
731 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000732 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000733 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000734 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000735 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000736 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000737 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000738 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000739 default:
740 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000741 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000742 }
743}
744
bsalomon@google.com85003222012-03-28 14:44:37 +0000745// We prefer to blur small rect with small radius via CPU.
746#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
747#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
748inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
749 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
750 rect.height() <= MIN_GPU_BLUR_SIZE &&
751 radius <= MIN_GPU_BLUR_RADIUS) {
752 return true;
753 }
754 return false;
755}
756
757bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
758 SkMaskFilter* filter, const SkMatrix& matrix,
759 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000760 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000761#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000762 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000763#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000764 SkMaskFilter::BlurInfo info;
765 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000766 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000767 return false;
768 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000769 SkScalar radius = info.fIgnoreTransform ? info.fRadius
770 : matrix.mapRadius(info.fRadius);
771 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000772 if (radius <= 0) {
773 return false;
774 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000775
776 SkRect srcRect = path.getBounds();
777 if (shouldDrawBlurWithCPU(srcRect, radius)) {
778 return false;
779 }
780
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000781 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000782 float sigma3 = sigma * 3.0f;
783
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000784 SkRect clipRect;
785 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000786
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000787 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000788 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
789 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000790 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000791 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000792 SkIRect finalIRect;
793 finalRect.roundOut(&finalIRect);
794 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000795 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000796 }
797 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000798 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000799 }
800 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000801 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000802 GrTextureDesc desc;
803 desc.fFlags = kRenderTarget_GrTextureFlagBit;
804 desc.fWidth = SkScalarCeilToInt(srcRect.width());
805 desc.fHeight = SkScalarCeilToInt(srcRect.height());
806 // We actually only need A8, but it often isn't supported as a
807 // render target so default to RGBA_8888
808 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000810 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
811 desc.fConfig = kAlpha_8_GrPixelConfig;
812 }
813
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000814 GrAutoScratchTexture pathEntry(context, desc);
815 GrTexture* pathTexture = pathEntry.texture();
816 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000817 return false;
818 }
819 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000820 // Once this code moves into GrContext, this should be changed to use
821 // an AutoClipRestore.
822 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000823 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000824
825 GrClip newClip(srcRect);
826 context->setClip(newClip);
827
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000828 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000829 GrPaint tempPaint;
830 tempPaint.reset();
831
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000832 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000833 tempPaint.fAntiAlias = grp->fAntiAlias;
834 if (tempPaint.fAntiAlias) {
835 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
836 // blend coeff of zero requires dual source blending support in order
837 // to properly blend partially covered pixels. This means the AA
838 // code path may not be taken. So we use a dst blend coeff of ISA. We
839 // could special case AA draws to a dst surface with known alpha=0 to
840 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000841 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
842 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000843 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000844 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000845 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000846
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000847 GrAutoScratchTexture temp1, temp2;
848 // If we're doing a normal blur, we can clobber the pathTexture in the
849 // gaussianBlur. Otherwise, we need to save it for later compositing.
850 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000851 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
852 &temp1,
853 isNormalBlur ? NULL : &temp2,
854 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000855
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000856 if (!isNormalBlur) {
857 GrPaint paint;
858 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000859 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000860 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
861 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000862 // Blend pathTexture over blurTexture.
863 context->setRenderTarget(blurTexture->asRenderTarget());
864 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000865 if (SkMaskFilter::kInner_BlurType == blurType) {
866 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000867 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
868 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000869 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
870 // solid: dst = src + dst - src * dst
871 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000872 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
873 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000874 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
875 // outer: dst = dst * (1 - src)
876 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000877 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
878 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000879 }
880 context->drawRect(paint, srcRect);
881 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000882 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000883 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000884
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000885 if (grp->hasTextureOrMask()) {
886 GrMatrix inverse;
887 if (!matrix.invert(&inverse)) {
888 return false;
889 }
890 grp->preConcatActiveSamplerMatrices(inverse);
891 }
892
893 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
894 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000895 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000896 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000897 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000898
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000899 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
900 -finalRect.fTop);
901 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
902 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000903 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000904 return true;
905}
906
bsalomon@google.com85003222012-03-28 14:44:37 +0000907bool drawWithMaskFilter(GrContext* context, const SkPath& path,
908 SkMaskFilter* filter, const SkMatrix& matrix,
909 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000910 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000911 SkMask srcM, dstM;
912
913 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000914 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000915 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000916 return false;
917 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000918 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000919
920 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
921 return false;
922 }
923 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000924 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000925
926 if (clip.quickReject(dstM.fBounds)) {
927 return false;
928 }
929 if (bounder && !bounder->doIRect(dstM.fBounds)) {
930 return false;
931 }
932
933 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
934 // the current clip (and identity matrix) and grpaint settings
935
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000936 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000937 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000938
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000939 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000940
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000941 GrTextureDesc desc;
942 desc.fWidth = dstM.fBounds.width();
943 desc.fHeight = dstM.fBounds.height();
944 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000945
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000946 GrAutoScratchTexture ast(context, desc);
947 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000948
reed@google.com69302852011-02-16 18:08:07 +0000949 if (NULL == texture) {
950 return false;
951 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000952 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000953 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000954
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000955 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
956 grp->preConcatActiveSamplerMatrices(ivm);
957 }
958
959 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
960 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000961 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000962 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000963 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000964
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000965 GrRect d;
966 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000967 GrIntToScalar(dstM.fBounds.fTop),
968 GrIntToScalar(dstM.fBounds.fRight),
969 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000970
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000971 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
972 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
973 -dstM.fBounds.fTop*SK_Scalar1);
974 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000975 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000976 return true;
977}
reed@google.com69302852011-02-16 18:08:07 +0000978
bsalomon@google.com85003222012-03-28 14:44:37 +0000979}
980
981///////////////////////////////////////////////////////////////////////////////
982
reed@google.com0c219b62011-02-16 21:31:18 +0000983void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000984 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000985 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000986 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000987 CHECK_SHOULD_DRAW(draw);
988
reed@google.comfe626382011-09-21 13:50:35 +0000989 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000990
bsalomon@google.com5782d712011-01-21 21:03:59 +0000991 GrPaint grPaint;
992 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000993 if (!skPaint2GrPaintShader(this,
994 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000995 true,
996 &act,
997 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000998 return;
999 }
1000
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001001 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1002 // if we can, we draw lots faster (raster device does this same test)
1003 SkScalar hairlineCoverage;
1004 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1005 doFill = false;
1006 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1007 grPaint.fCoverage);
1008 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001009
reed@google.comfe626382011-09-21 13:50:35 +00001010 // If we have a prematrix, apply it to the path, optimizing for the case
1011 // where the original path can in fact be modified in place (even though
1012 // its parameter type is const).
1013 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1014 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001015
1016 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001017 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001018
reed@google.come3445642011-02-16 23:20:39 +00001019 if (!pathIsMutable) {
1020 result = &tmpPath;
1021 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001022 }
reed@google.come3445642011-02-16 23:20:39 +00001023 // should I push prePathMatrix on our MV stack temporarily, instead
1024 // of applying it here? See SkDraw.cpp
1025 pathPtr->transform(*prePathMatrix, result);
1026 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001027 }
reed@google.com0c219b62011-02-16 21:31:18 +00001028 // at this point we're done with prePathMatrix
1029 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001030
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001031 if (paint.getPathEffect() ||
1032 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001033 // it is safe to use tmpPath here, even if we already used it for the
1034 // prepathmatrix, since getFillPath can take the same object for its
1035 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001036 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001037 pathPtr = &tmpPath;
1038 }
1039
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001040 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001041 // avoid possibly allocating a new path in transform if we can
1042 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1043
1044 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001045 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001046 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001047 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001048 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001049 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001050 &grPaint, pathFillType)) {
1051 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1052 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001053 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001054 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001055 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001056 }
reed@google.com69302852011-02-16 18:08:07 +00001057 return;
1058 }
reed@google.com69302852011-02-16 18:08:07 +00001059
bsalomon@google.com47059542012-06-06 20:51:20 +00001060 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001061
reed@google.com0c219b62011-02-16 21:31:18 +00001062 if (doFill) {
1063 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001065 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001066 break;
1067 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001068 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001069 break;
1070 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001071 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001072 break;
1073 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001074 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001075 break;
1076 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001077 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001078 return;
1079 }
1080 }
1081
reed@google.com07f3ee12011-05-16 17:21:57 +00001082 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001083}
1084
bsalomon@google.comfb309512011-11-30 14:13:48 +00001085namespace {
1086
1087inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1088 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1089 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1090 return tilesX * tilesY;
1091}
1092
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001093inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001094 const SkIRect* srcRectPtr,
1095 int maxTextureSize) {
1096 static const int kSmallTileSize = 1 << 10;
1097 if (maxTextureSize <= kSmallTileSize) {
1098 return maxTextureSize;
1099 }
1100
1101 size_t maxTexTotalTileSize;
1102 size_t smallTotalTileSize;
1103
1104 if (NULL == srcRectPtr) {
1105 int w = bitmap.width();
1106 int h = bitmap.height();
1107 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1108 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1109 } else {
1110 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1111 srcRectPtr->fTop,
1112 srcRectPtr->fRight,
1113 srcRectPtr->fBottom,
1114 maxTextureSize);
1115 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1116 srcRectPtr->fTop,
1117 srcRectPtr->fRight,
1118 srcRectPtr->fBottom,
1119 kSmallTileSize);
1120 }
1121 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1122 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1123
1124 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1125 return kSmallTileSize;
1126 } else {
1127 return maxTextureSize;
1128 }
1129}
1130}
1131
1132bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1133 const GrSamplerState& sampler,
1134 const SkIRect* srcRectPtr,
1135 int* tileSize) const {
1136 SkASSERT(NULL != tileSize);
1137
1138 // if bitmap is explictly texture backed then just use the texture
1139 if (NULL != bitmap.getTexture()) {
1140 return false;
1141 }
1142 // if it's larger than the max texture size, then we have no choice but
1143 // tiling
1144 const int maxTextureSize = fContext->getMaxTextureSize();
1145 if (bitmap.width() > maxTextureSize ||
1146 bitmap.height() > maxTextureSize) {
1147 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1148 return true;
1149 }
1150 // if we are going to have to draw the whole thing, then don't tile
1151 if (NULL == srcRectPtr) {
1152 return false;
1153 }
1154 // if the entire texture is already in our cache then no reason to tile it
1155 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1156 return false;
1157 }
1158
1159 // At this point we know we could do the draw by uploading the entire bitmap
1160 // as a texture. However, if the texture would be large compared to the
1161 // cache size and we don't require most of it for this draw then tile to
1162 // reduce the amount of upload and cache spill.
1163
1164 // assumption here is that sw bitmap size is a good proxy for its size as
1165 // a texture
1166 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001167 size_t cacheSize;
1168 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001169 if (bmpSize < cacheSize / 2) {
1170 return false;
1171 }
1172
1173 SkFixed fracUsed =
1174 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1175 (srcRectPtr->height() << 16) / bitmap.height());
1176 if (fracUsed <= SK_FixedHalf) {
1177 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1178 return true;
1179 } else {
1180 return false;
1181 }
1182}
1183
reed@google.comac10a2d2010-12-22 21:39:39 +00001184void SkGpuDevice::drawBitmap(const SkDraw& draw,
1185 const SkBitmap& bitmap,
1186 const SkIRect* srcRectPtr,
1187 const SkMatrix& m,
1188 const SkPaint& paint) {
1189 CHECK_SHOULD_DRAW(draw);
1190
1191 SkIRect srcRect;
1192 if (NULL == srcRectPtr) {
1193 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1194 } else {
1195 srcRect = *srcRectPtr;
1196 }
1197
junov@google.comd935cfb2011-06-27 20:48:23 +00001198 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001199 // Convert the bitmap to a shader so that the rect can be drawn
1200 // through drawRect, which supports mask filters.
1201 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001202 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001203 if (srcRectPtr) {
1204 if (!bitmap.extractSubset(&tmp, srcRect)) {
1205 return; // extraction failed
1206 }
1207 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001208 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001209 }
1210 SkPaint paintWithTexture(paint);
1211 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1212 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001213 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001214 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001215
junov@google.com1d329782011-07-28 20:10:09 +00001216 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001217 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001218 // also affect the behavior of the mask filter.
1219 SkMatrix drawMatrix;
1220 drawMatrix.setConcat(*draw.fMatrix, m);
1221 SkDraw transformedDraw(draw);
1222 transformedDraw.fMatrix = &drawMatrix;
1223
1224 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1225
junov@google.comd935cfb2011-06-27 20:48:23 +00001226 return;
1227 }
1228
bsalomon@google.com5782d712011-01-21 21:03:59 +00001229 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001230 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001231 return;
1232 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001233 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001234 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001235 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001236 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001237 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001238 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001239
bsalomon@google.comfb309512011-11-30 14:13:48 +00001240 int tileSize;
1241 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1242 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001243 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001244 return;
1245 }
1246
1247 // undo the translate done by SkCanvas
1248 int DX = SkMax32(0, srcRect.fLeft);
1249 int DY = SkMax32(0, srcRect.fTop);
1250 // compute clip bounds in local coordinates
1251 SkIRect clipRect;
1252 {
1253 SkRect r;
1254 r.set(draw.fClip->getBounds());
1255 SkMatrix matrix, inverse;
1256 matrix.setConcat(*draw.fMatrix, m);
1257 if (!matrix.invert(&inverse)) {
1258 return;
1259 }
1260 inverse.mapRect(&r);
1261 r.roundOut(&clipRect);
1262 // apply the canvas' translate to our local clip
1263 clipRect.offset(DX, DY);
1264 }
1265
bsalomon@google.comfb309512011-11-30 14:13:48 +00001266 int nx = bitmap.width() / tileSize;
1267 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001268 for (int x = 0; x <= nx; x++) {
1269 for (int y = 0; y <= ny; y++) {
1270 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001271 tileR.set(x * tileSize, y * tileSize,
1272 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001273 if (!SkIRect::Intersects(tileR, clipRect)) {
1274 continue;
1275 }
1276
1277 SkIRect srcR = tileR;
1278 if (!srcR.intersect(srcRect)) {
1279 continue;
1280 }
1281
1282 SkBitmap tmpB;
1283 if (bitmap.extractSubset(&tmpB, tileR)) {
1284 // now offset it to make it "local" to our tmp bitmap
1285 srcR.offset(-tileR.fLeft, -tileR.fTop);
1286
1287 SkMatrix tmpM(m);
1288 {
1289 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1290 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1291 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1292 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001293 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 }
1295 }
1296 }
1297}
1298
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001299namespace {
1300
1301bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1302 // detect pixel disalignment
1303 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1304 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1305 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1306 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1307 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1308 COLOR_BLEED_TOLERANCE &&
1309 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1310 COLOR_BLEED_TOLERANCE) {
1311 return true;
1312 }
1313 return false;
1314}
1315
1316bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1317 const SkMatrix& m) {
1318 // Only gets called if hasAlignedSamples returned false.
1319 // So we can assume that sampling is axis aligned but not texel aligned.
1320 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1321 SkRect innerSrcRect(srcRect), innerTransformedRect,
1322 outerTransformedRect(transformedRect);
1323 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1324 m.mapRect(&innerTransformedRect, innerSrcRect);
1325
1326 // The gap between outerTransformedRect and innerTransformedRect
1327 // represents the projection of the source border area, which is
1328 // problematic for color bleeding. We must check whether any
1329 // destination pixels sample the border area.
1330 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1331 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1332 SkIRect outer, inner;
1333 outerTransformedRect.round(&outer);
1334 innerTransformedRect.round(&inner);
1335 // If the inner and outer rects round to the same result, it means the
1336 // border does not overlap any pixel centers. Yay!
1337 return inner != outer;
1338}
1339
1340} // unnamed namespace
1341
reed@google.comac10a2d2010-12-22 21:39:39 +00001342/*
1343 * This is called by drawBitmap(), which has to handle images that may be too
1344 * large to be represented by a single texture.
1345 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001346 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1347 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001348 */
1349void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1350 const SkBitmap& bitmap,
1351 const SkIRect& srcRect,
1352 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001353 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001354 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1355 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001356
reed@google.com9c49bc32011-07-07 13:42:37 +00001357 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001358 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001359 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001360 return;
1361 }
1362
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001363 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001364
1365 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1366 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001367 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001368
1369 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001370 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001371 if (NULL == texture) {
1372 return;
1373 }
1374
bsalomon@google.com452943d2011-10-31 17:37:14 +00001375 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001376
reed@google.com20efde72011-05-09 17:00:02 +00001377 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1378 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001379 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001380 float wInv = 1.f / bitmap.width();
1381 float hInv = 1.f / bitmap.height();
1382 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1383 SkFloatToScalar(srcRect.fTop * hInv),
1384 SkFloatToScalar(srcRect.fRight * wInv),
1385 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001386
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001387 bool needsTextureDomain = false;
1388 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1389 {
1390 // Need texture domain if drawing a sub rect.
1391 needsTextureDomain = srcRect.width() < bitmap.width() ||
1392 srcRect.height() < bitmap.height();
1393 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1394 // sampling is axis-aligned
1395 GrRect floatSrcRect, transformedRect;
1396 floatSrcRect.set(srcRect);
1397 SkMatrix srcToDeviceMatrix(m);
1398 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1399 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1400
1401 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1402 // Samples are texel-aligned, so filtering is futile
1403 sampler->setFilter(GrSamplerState::kNearest_Filter);
1404 needsTextureDomain = false;
1405 } else {
1406 needsTextureDomain = needsTextureDomain &&
1407 mayColorBleed(floatSrcRect, transformedRect, m);
1408 }
1409 }
1410 }
1411
1412 GrRect textureDomain = GrRect::MakeEmpty();
1413
1414 if (needsTextureDomain) {
1415 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001416 GrScalar left, top, right, bottom;
1417 if (srcRect.width() > 1) {
1418 GrScalar border = GR_ScalarHalf / bitmap.width();
1419 left = paintRect.left() + border;
1420 right = paintRect.right() - border;
1421 } else {
1422 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1423 }
1424 if (srcRect.height() > 1) {
1425 GrScalar border = GR_ScalarHalf / bitmap.height();
1426 top = paintRect.top() + border;
1427 bottom = paintRect.bottom() - border;
1428 } else {
1429 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1430 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001431 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001432 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001433 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001434
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001435 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001436}
1437
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001438namespace {
1439
1440void apply_custom_stage(GrContext* context,
1441 GrTexture* srcTexture,
1442 GrTexture* dstTexture,
1443 const GrRect& rect,
1444 GrCustomStage* stage) {
1445 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001446 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001447 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
1448 GrClip oldClip = context->getClip();
robertphillips@google.combca1c5d2012-07-11 18:25:24 +00001449
1450 GrClip newClip(rect);
1451 context->setClip(newClip);
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);
1462 context->setClip(oldClip);
1463}
1464
1465};
1466
reed@google.com8926b162012-03-23 15:36:36 +00001467static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1468 SkImageFilter* filter, const GrRect& rect) {
1469 GrAssert(filter);
1470
1471 SkSize blurSize;
1472 SkISize radius;
1473
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001474 GrTextureDesc desc;
1475 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1476 desc.fWidth = SkScalarCeilToInt(rect.width());
1477 desc.fHeight = SkScalarCeilToInt(rect.height());
1478 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001479 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001480
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001481 if (filter->asNewCustomStage(&stage)) {
1482 GrAutoScratchTexture dst(context, desc);
1483 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1484 texture = dst.detach();
1485 stage->unref();
1486 } else if (filter->asABlur(&blurSize)) {
reed@google.com8926b162012-03-23 15:36:36 +00001487 GrAutoScratchTexture temp1, temp2;
1488 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1489 blurSize.width(),
1490 blurSize.height());
1491 texture->ref();
1492 } else if (filter->asADilate(&radius)) {
1493 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1494 texture = context->applyMorphology(texture, rect,
1495 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001496 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001497 radius);
1498 texture->ref();
1499 } else if (filter->asAnErode(&radius)) {
1500 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1501 texture = context->applyMorphology(texture, rect,
1502 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001503 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001504 radius);
1505 texture->ref();
1506 }
1507 return texture;
1508}
1509
reed@google.comac10a2d2010-12-22 21:39:39 +00001510void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1511 int left, int top, const SkPaint& paint) {
1512 CHECK_SHOULD_DRAW(draw);
1513
reed@google.com8926b162012-03-23 15:36:36 +00001514 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001515 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1516 return;
1517 }
1518
reed@google.com76dd2772012-01-05 21:15:07 +00001519 int w = bitmap.width();
1520 int h = bitmap.height();
1521
bsalomon@google.com5782d712011-01-21 21:03:59 +00001522 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001523 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001524 return;
1525 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001526
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001527 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001528
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001529 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001530
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001531 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001532 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001533 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001534 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001535
reed@google.com8926b162012-03-23 15:36:36 +00001536 SkImageFilter* filter = paint.getImageFilter();
1537 if (NULL != filter) {
1538 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001539 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001540 if (filteredTexture) {
1541 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1542 texture = filteredTexture;
1543 filteredTexture->unref();
1544 }
reed@google.com76dd2772012-01-05 21:15:07 +00001545 }
reed@google.com8926b162012-03-23 15:36:36 +00001546
bsalomon@google.com5782d712011-01-21 21:03:59 +00001547 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001548 GrRect::MakeXYWH(GrIntToScalar(left),
1549 GrIntToScalar(top),
1550 GrIntToScalar(w),
1551 GrIntToScalar(h)),
1552 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1553 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001554}
1555
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001556void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001557 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001558 // clear of the source device must occur before CHECK_SHOULD_DRAW
1559 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1560 if (dev->fNeedClear) {
1561 // TODO: could check here whether we really need to draw at all
1562 dev->clear(0x0);
1563 }
1564
reed@google.comac10a2d2010-12-22 21:39:39 +00001565 CHECK_SHOULD_DRAW(draw);
1566
bsalomon@google.com5782d712011-01-21 21:03:59 +00001567 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001568 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001569 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001570 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001571 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001572
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001573 GrTexture* devTex = grPaint.getTexture(0);
1574 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001575
reed@google.com8926b162012-03-23 15:36:36 +00001576 SkImageFilter* filter = paint.getImageFilter();
1577 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001578 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1579 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001580 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1581 rect);
1582 if (filteredTexture) {
1583 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1584 devTex = filteredTexture;
1585 filteredTexture->unref();
1586 }
1587 }
1588
bsalomon@google.com5782d712011-01-21 21:03:59 +00001589 const SkBitmap& bm = dev->accessBitmap(false);
1590 int w = bm.width();
1591 int h = bm.height();
1592
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001593 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001594
bsalomon@google.com97912912011-12-06 16:30:36 +00001595 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001596
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001597 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1598 GrIntToScalar(y),
1599 GrIntToScalar(w),
1600 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001601
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001602 // The device being drawn may not fill up its texture (saveLayer uses
1603 // the approximate ).
1604 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1605 GR_Scalar1 * h / devTex->height());
1606
1607 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001608}
1609
reed@google.com8926b162012-03-23 15:36:36 +00001610bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001611 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001612 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001613
1614 if (!filter->asNewCustomStage(NULL) &&
1615 !filter->asABlur(&size) &&
1616 !filter->asADilate(&radius) &&
1617 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001618 return false;
1619 }
reed@google.com8926b162012-03-23 15:36:36 +00001620 return true;
1621}
1622
1623bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1624 const SkMatrix& ctm,
1625 SkBitmap* result, SkIPoint* offset) {
1626 // want explicitly our impl, so guard against a subclass of us overriding it
1627 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001628 return false;
1629 }
reed@google.com8926b162012-03-23 15:36:36 +00001630
1631 SkAutoLockPixels alp(src, !src.getTexture());
1632 if (!src.getTexture() && !src.readyToDraw()) {
1633 return false;
1634 }
1635
1636 GrPaint paint;
1637 paint.reset();
1638
1639 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1640
1641 GrTexture* texture;
1642 SkAutoCachedTexture act(this, src, sampler, &texture);
1643
1644 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001645 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1646 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001647 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1648 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001649 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1650 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001651 resultTexture->unref();
1652 }
reed@google.com76dd2772012-01-05 21:15:07 +00001653 return true;
1654}
1655
reed@google.comac10a2d2010-12-22 21:39:39 +00001656///////////////////////////////////////////////////////////////////////////////
1657
1658// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001659static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001660 kTriangles_GrPrimitiveType,
1661 kTriangleStrip_GrPrimitiveType,
1662 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001663};
1664
1665void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1666 int vertexCount, const SkPoint vertices[],
1667 const SkPoint texs[], const SkColor colors[],
1668 SkXfermode* xmode,
1669 const uint16_t indices[], int indexCount,
1670 const SkPaint& paint) {
1671 CHECK_SHOULD_DRAW(draw);
1672
bsalomon@google.com5782d712011-01-21 21:03:59 +00001673 GrPaint grPaint;
1674 SkAutoCachedTexture act;
1675 // we ignore the shader if texs is null.
1676 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001677 if (!skPaint2GrPaintNoShader(paint,
1678 false,
1679 NULL == colors,
1680 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001681 return;
1682 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001683 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001684 if (!skPaint2GrPaintShader(this,
1685 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001686 NULL == colors,
1687 &act,
1688 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001689 return;
1690 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001691 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001692
1693 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001694 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001695 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1696#if 0
1697 return
1698#endif
1699 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001700 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001701
bsalomon@google.com498776a2011-08-16 19:20:44 +00001702 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1703 if (NULL != colors) {
1704 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001705 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001706 for (int i = 0; i < vertexCount; ++i) {
1707 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1708 }
1709 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001710 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001711 fContext->drawVertices(grPaint,
1712 gVertexMode2PrimitiveType[vmode],
1713 vertexCount,
1714 (GrPoint*) vertices,
1715 (GrPoint*) texs,
1716 colors,
1717 indices,
1718 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001719}
1720
1721///////////////////////////////////////////////////////////////////////////////
1722
1723static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001724 GrFontScaler* scaler = (GrFontScaler*)data;
1725 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001726}
1727
1728static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1729 void* auxData;
1730 GrFontScaler* scaler = NULL;
1731 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1732 scaler = (GrFontScaler*)auxData;
1733 }
1734 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001735 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001736 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1737 }
1738 return scaler;
1739}
1740
1741static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1742 SkFixed fx, SkFixed fy,
1743 const SkGlyph& glyph) {
1744 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1745
bungeman@google.com15865a72012-01-11 16:28:04 +00001746 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001747
1748 if (NULL == procs->fFontScaler) {
1749 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1750 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001751
bungeman@google.com15865a72012-01-11 16:28:04 +00001752 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1753 glyph.getSubXFixed(),
1754 glyph.getSubYFixed()),
1755 SkFixedFloorToFixed(fx),
1756 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001757 procs->fFontScaler);
1758}
1759
bsalomon@google.com5782d712011-01-21 21:03:59 +00001760SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001761
1762 // deferred allocation
1763 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001764 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001765 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1766 fDrawProcs->fContext = fContext;
1767 }
1768
1769 // init our (and GL's) state
1770 fDrawProcs->fTextContext = context;
1771 fDrawProcs->fFontScaler = NULL;
1772 return fDrawProcs;
1773}
1774
1775void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1776 size_t byteLength, SkScalar x, SkScalar y,
1777 const SkPaint& paint) {
1778 CHECK_SHOULD_DRAW(draw);
1779
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001780 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001781 // this guy will just call our drawPath()
1782 draw.drawText((const char*)text, byteLength, x, y, paint);
1783 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001784 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001785
1786 GrPaint grPaint;
1787 SkAutoCachedTexture act;
1788
bsalomon@google.com84405e02012-03-05 19:57:21 +00001789 if (!skPaint2GrPaintShader(this,
1790 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001791 true,
1792 &act,
1793 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001794 return;
1795 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001796 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1797 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001798 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1799 }
1800}
1801
1802void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1803 size_t byteLength, const SkScalar pos[],
1804 SkScalar constY, int scalarsPerPos,
1805 const SkPaint& paint) {
1806 CHECK_SHOULD_DRAW(draw);
1807
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001808 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001809 // this guy will just call our drawPath()
1810 draw.drawPosText((const char*)text, byteLength, pos, constY,
1811 scalarsPerPos, paint);
1812 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001813 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001814
1815 GrPaint grPaint;
1816 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001817 if (!skPaint2GrPaintShader(this,
1818 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001819 true,
1820 &act,
1821 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001822 return;
1823 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001824 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1825 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001826 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1827 scalarsPerPos, paint);
1828 }
1829}
1830
1831void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1832 size_t len, const SkPath& path,
1833 const SkMatrix* m, const SkPaint& paint) {
1834 CHECK_SHOULD_DRAW(draw);
1835
1836 SkASSERT(draw.fDevice == this);
1837 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1838}
1839
1840///////////////////////////////////////////////////////////////////////////////
1841
reed@google.comf67e4cf2011-03-15 20:56:58 +00001842bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1843 if (!paint.isLCDRenderText()) {
1844 // we're cool with the paint as is
1845 return false;
1846 }
1847
1848 if (paint.getShader() ||
1849 paint.getXfermode() || // unless its srcover
1850 paint.getMaskFilter() ||
1851 paint.getRasterizer() ||
1852 paint.getColorFilter() ||
1853 paint.getPathEffect() ||
1854 paint.isFakeBoldText() ||
1855 paint.getStyle() != SkPaint::kFill_Style) {
1856 // turn off lcd
1857 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1858 flags->fHinting = paint.getHinting();
1859 return true;
1860 }
1861 // we're cool with the paint as is
1862 return false;
1863}
1864
reed@google.com75d939b2011-12-07 15:07:23 +00001865void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001866 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001867 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001868}
1869
reed@google.comf67e4cf2011-03-15 20:56:58 +00001870///////////////////////////////////////////////////////////////////////////////
1871
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001872SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1873 const SkBitmap& bitmap,
1874 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001875 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001876 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001877
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001878 if (!bitmap.isVolatile()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001879 uint64_t key = bitmap.getGenerationID();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001880 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001881
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001882 GrTextureDesc desc;
1883 desc.fWidth = bitmap.width();
1884 desc.fHeight = bitmap.height();
1885 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
robertphillips@google.com56f22442012-06-08 14:21:26 +00001886 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001887
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001888 entry = ctx->findAndLockTexture(desc, sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001889 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001890 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1891 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001892 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001893 } else {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001894 entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001895 sampler, bitmap);
1896 }
1897 if (NULL == entry.texture()) {
1898 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1899 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001900 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001901 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001902}
1903
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001904void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1905 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001906}
1907
bsalomon@google.comfb309512011-11-30 14:13:48 +00001908bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1909 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001910 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001911 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001912
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001913 GrTextureDesc desc;
1914 desc.fWidth = bitmap.width();
1915 desc.fHeight = bitmap.height();
1916 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
1917 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001918
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001919 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001920}
1921
1922
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001923SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1924 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001925 bool isOpaque,
1926 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001927 GrTextureDesc desc;
1928 desc.fConfig = fRenderTarget->config();
1929 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1930 desc.fWidth = width;
1931 desc.fHeight = height;
1932 desc.fSampleCnt = fRenderTarget->numSamples();
1933
1934 GrContext::TextureCacheEntry cacheEntry;
1935 GrTexture* texture;
1936 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001937 // Skia's convention is to only clear a device if it is non-opaque.
1938 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001939
1940#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1941 // layers are never draw in repeat modes, so we can request an approx
1942 // match and ignore any padding.
1943 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1944 GrContext::kApprox_ScratchTexMatch :
1945 GrContext::kExact_ScratchTexMatch;
1946 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1947 texture = cacheEntry.texture();
1948#else
1949 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1950 texture = tunref.get();
1951#endif
1952 if (texture) {
1953 return SkNEW_ARGS(SkGpuDevice,(fContext,
1954 texture,
1955 cacheEntry,
1956 needClear));
1957 } else {
1958 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1959 width, height);
1960 return NULL;
1961 }
1962}
1963
1964SkGpuDevice::SkGpuDevice(GrContext* context,
1965 GrTexture* texture,
1966 TexCache cacheEntry,
1967 bool needClear)
1968 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1969 GrAssert(texture && texture->asRenderTarget());
1970 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1971 this->initFromRenderTarget(context, texture->asRenderTarget());
1972 fCache = cacheEntry;
1973 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001974}
1975