blob: e5227a275bf645d83cf1af6db93c5e204200d68b [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
tomhudson@google.com898e7b52012-06-01 20:42:15 +00008#include "SkGpuDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00009
tomhudson@google.com898e7b52012-06-01 20:42:15 +000010#include "effects/GrGradientEffects.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000011
reed@google.comac10a2d2010-12-22 21:39:39 +000012#include "GrContext.h"
13#include "GrTextContext.h"
14
robertphillips@google.come9c04692012-06-29 00:30:13 +000015#include "SkGrTexturePixelRef.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
Scroggo97c88c22011-05-11 14:05:25 +000017#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018#include "SkDrawProcs.h"
19#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000020#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000021#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000022#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
bsalomon@google.com06cd7322012-03-30 18:45:35 +000024#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000025
26#if 0
27 extern bool (*gShouldDrawProc)();
28 #define CHECK_SHOULD_DRAW(draw) \
29 do { \
30 if (gShouldDrawProc && !gShouldDrawProc()) return; \
31 this->prepareRenderTarget(draw); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000032 GrAssert(!fNeedClear) \
reed@google.comac10a2d2010-12-22 21:39:39 +000033 } while (0)
34#else
bsalomon@google.com06cd7322012-03-30 18:45:35 +000035 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \
36 GrAssert(!fNeedClear)
reed@google.comac10a2d2010-12-22 21:39:39 +000037#endif
38
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000039// we use the same texture slot on GrPaint for bitmaps and shaders
40// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
41enum {
42 kBitmapTextureIdx = 0,
43 kShaderTextureIdx = 0
44};
45
reed@google.comcde92112011-07-06 20:00:52 +000046
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000047#define MAX_BLUR_SIGMA 4.0f
48// FIXME: This value comes from from SkBlurMaskFilter.cpp.
49// Should probably be put in a common header someplace.
50#define MAX_BLUR_RADIUS SkIntToScalar(128)
51// This constant approximates the scaling done in the software path's
52// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
53// IMHO, it actually should be 1: we blur "less" than we should do
54// according to the CSS and canvas specs, simply because Safari does the same.
55// Firefox used to do the same too, until 4.0 where they fixed it. So at some
56// point we should probably get rid of these scaling constants and rebaseline
57// all the blur tests.
58#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000059// This constant represents the screen alignment criterion in texels for
60// requiring texture domain clamping to prevent color bleeding when drawing
61// a sub region of a larger source image.
62#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000063
64#define DO_DEFERRED_CLEAR \
65 do { \
66 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000067 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000068 fNeedClear = false; \
69 } \
70 } while (false) \
71
reed@google.comac10a2d2010-12-22 21:39:39 +000072///////////////////////////////////////////////////////////////////////////////
73
bsalomon@google.com84405e02012-03-05 19:57:21 +000074class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
75public:
76 SkAutoCachedTexture() { }
77 SkAutoCachedTexture(SkGpuDevice* device,
78 const SkBitmap& bitmap,
79 const GrSamplerState* sampler,
80 GrTexture** texture) {
81 GrAssert(texture);
82 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000083 }
reed@google.comac10a2d2010-12-22 21:39:39 +000084
bsalomon@google.com84405e02012-03-05 19:57:21 +000085 ~SkAutoCachedTexture() {
86 if (fTex.texture()) {
87 fDevice->unlockCachedTexture(fTex);
88 }
reed@google.comac10a2d2010-12-22 21:39:39 +000089 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000090
91 GrTexture* set(SkGpuDevice* device,
92 const SkBitmap& bitmap,
93 const GrSamplerState* sampler) {
94 if (fTex.texture()) {
95 fDevice->unlockCachedTexture(fTex);
96 }
97 fDevice = device;
98 GrTexture* texture = (GrTexture*)bitmap.getTexture();
99 if (texture) {
100 // return the native texture
101 fTex.reset();
102 } else {
103 // look it up in our cache
104 fTex = device->lockCachedTexture(bitmap, sampler);
105 texture = fTex.texture();
106 }
107 return texture;
108 }
109
110private:
111 SkGpuDevice* fDevice;
112 GrContext::TextureCacheEntry fTex;
113};
reed@google.comac10a2d2010-12-22 21:39:39 +0000114
115///////////////////////////////////////////////////////////////////////////////
116
117bool gDoTraceDraw;
118
119struct GrSkDrawProcs : public SkDrawProcs {
120public:
121 GrContext* fContext;
122 GrTextContext* fTextContext;
123 GrFontScaler* fFontScaler; // cached in the skia glyphcache
124};
125
126///////////////////////////////////////////////////////////////////////////////
127
reed@google.comaf951c92011-06-16 19:10:39 +0000128static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
129 switch (config) {
130 case kAlpha_8_GrPixelConfig:
131 *isOpaque = false;
132 return SkBitmap::kA8_Config;
133 case kRGB_565_GrPixelConfig:
134 *isOpaque = true;
135 return SkBitmap::kRGB_565_Config;
136 case kRGBA_4444_GrPixelConfig:
137 *isOpaque = false;
138 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000139 case kSkia8888_PM_GrPixelConfig:
140 // we don't currently have a way of knowing whether
141 // a 8888 is opaque based on the config.
142 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000143 return SkBitmap::kARGB_8888_Config;
144 default:
145 *isOpaque = false;
146 return SkBitmap::kNo_Config;
147 }
148}
reed@google.comac10a2d2010-12-22 21:39:39 +0000149
reed@google.comaf951c92011-06-16 19:10:39 +0000150static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000151 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000152
153 bool isOpaque;
154 SkBitmap bitmap;
155 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
156 renderTarget->width(), renderTarget->height());
157 bitmap.setIsOpaque(isOpaque);
158 return bitmap;
159}
160
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000161SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
162: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
163 this->initFromRenderTarget(context, texture->asRenderTarget());
164}
165
reed@google.comaf951c92011-06-16 19:10:39 +0000166SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
167: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000168 this->initFromRenderTarget(context, renderTarget);
169}
170
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000171void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000172 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000173 fNeedPrepareRenderTarget = false;
174 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000175
reed@google.comaf951c92011-06-16 19:10:39 +0000176 fContext = context;
177 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000178
reed@google.comaf951c92011-06-16 19:10:39 +0000179 fTexture = NULL;
180 fRenderTarget = NULL;
181 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000182
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000183 GrAssert(NULL != renderTarget);
184 fRenderTarget = renderTarget;
185 fRenderTarget->ref();
186 // if this RT is also a texture, hold a ref on it
187 fTexture = fRenderTarget->asTexture();
188 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000189
190 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
191 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
192 // the device via accessBitmap. This external ref may outlive the device.
193 // Since textures own their render targets (but not vice-versa) we
194 // are ensuring that both objects will live as long as the pixel ref.
195 SkPixelRef* pr;
196 if (fTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000197 pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000198 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000199 pr = SkNEW_ARGS(SkGrRenderTargetPixelRef, (fRenderTarget));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000200 }
reed@google.comaf951c92011-06-16 19:10:39 +0000201 this->setPixelRef(pr, 0)->unref();
202}
203
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000204SkGpuDevice::SkGpuDevice(GrContext* context,
205 SkBitmap::Config config,
206 int width,
207 int height)
208 : SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000209 fNeedPrepareRenderTarget = false;
210 fDrawProcs = NULL;
211
reed@google.com7b201d22011-01-11 18:59:23 +0000212 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000213 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000214
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 fTexture = NULL;
216 fRenderTarget = NULL;
217 fNeedClear = false;
218
reed@google.comaf951c92011-06-16 19:10:39 +0000219 if (config != SkBitmap::kRGB_565_Config) {
220 config = SkBitmap::kARGB_8888_Config;
221 }
222 SkBitmap bm;
223 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000224
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000225 GrTextureDesc desc;
226 desc.fFlags = kRenderTarget_GrTextureFlagBit;
227 desc.fWidth = width;
228 desc.fHeight = height;
229 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000230
reed@google.comaf951c92011-06-16 19:10:39 +0000231 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000232
reed@google.comaf951c92011-06-16 19:10:39 +0000233 if (NULL != fTexture) {
234 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000235 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
reed@google.comaf951c92011-06-16 19:10:39 +0000237 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000238
reed@google.comaf951c92011-06-16 19:10:39 +0000239 // wrap the bitmap with a pixelref to expose our texture
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000240 SkGrTexturePixelRef* pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000241 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000242 } else {
243 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
244 width, height);
245 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000246 }
247}
248
249SkGpuDevice::~SkGpuDevice() {
250 if (fDrawProcs) {
251 delete fDrawProcs;
252 }
253
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000254 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
255 // This call gives the context a chance to relinquish it
256 fContext->setRenderTarget(NULL);
257
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000258 SkSafeUnref(fTexture);
259 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000260 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000261 GrAssert(NULL != fTexture);
262 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000263 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000264 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000265 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000266}
267
reed@google.comac10a2d2010-12-22 21:39:39 +0000268///////////////////////////////////////////////////////////////////////////////
269
270void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000271 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000272 fContext->setRenderTarget(fRenderTarget);
273 fContext->flush(true);
274 fNeedPrepareRenderTarget = true;
275}
276
277///////////////////////////////////////////////////////////////////////////////
278
bsalomon@google.comc4364992011-11-07 15:54:49 +0000279namespace {
280GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
281 switch (config8888) {
282 case SkCanvas::kNative_Premul_Config8888:
283 return kSkia8888_PM_GrPixelConfig;
284 case SkCanvas::kNative_Unpremul_Config8888:
285 return kSkia8888_UPM_GrPixelConfig;
286 case SkCanvas::kBGRA_Premul_Config8888:
287 return kBGRA_8888_PM_GrPixelConfig;
288 case SkCanvas::kBGRA_Unpremul_Config8888:
289 return kBGRA_8888_UPM_GrPixelConfig;
290 case SkCanvas::kRGBA_Premul_Config8888:
291 return kRGBA_8888_PM_GrPixelConfig;
292 case SkCanvas::kRGBA_Unpremul_Config8888:
293 return kRGBA_8888_UPM_GrPixelConfig;
294 default:
295 GrCrash("Unexpected Config8888.");
296 return kSkia8888_PM_GrPixelConfig;
297 }
298}
299}
300
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000301bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
302 int x, int y,
303 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000304 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000305 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
306 SkASSERT(!bitmap.isNull());
307 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000308
bsalomon@google.com910267d2011-11-02 20:06:25 +0000309 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000310 GrPixelConfig config;
311 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000312 return fContext->readRenderTargetPixels(fRenderTarget,
313 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000314 bitmap.width(),
315 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000316 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000317 bitmap.getPixels(),
318 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000319}
320
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000321void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
322 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000323 SkAutoLockPixels alp(bitmap);
324 if (!bitmap.readyToDraw()) {
325 return;
326 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000327
328 GrPixelConfig config;
329 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
330 config = config8888_to_gr_config(config8888);
331 } else {
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000332 config= SkGr::BitmapConfig2PixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000333 }
334
bsalomon@google.com6f379512011-11-16 20:36:03 +0000335 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
336 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000337}
338
339///////////////////////////////////////////////////////////////////////////////
340
341static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000342 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000343 const SkRegion& clipRegion,
344 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000345 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000346
347 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000348 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000349 const SkIRect& skBounds = clipRegion.getBounds();
350 GrRect bounds;
351 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
352 GrIntToScalar(skBounds.fTop),
353 GrIntToScalar(skBounds.fRight),
354 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000355 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
356 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000357 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000358}
359
360// call this ever each draw call, to ensure that the context reflects our state,
361// and not the state from some other canvas/device
362void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
363 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000364 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000365
366 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000367 SkASSERT(draw.fClipStack);
368 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000369 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000370 fNeedPrepareRenderTarget = false;
371 }
372}
373
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000374void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
375 const SkClipStack& clipStack) {
376 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
377 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000378 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000379}
380
381void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000382 const SkRegion& clip, const SkClipStack& clipStack) {
383
reed@google.comac10a2d2010-12-22 21:39:39 +0000384 fContext->setRenderTarget(fRenderTarget);
385
bsalomon@google.comd302f142011-03-03 13:54:13 +0000386 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000387
reed@google.com6f8f2922011-03-04 22:27:10 +0000388 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000389
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000390 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000391}
392
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000393SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000394 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000395 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000396}
397
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000398bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000399 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000400 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000401 return true;
402 }
403 return false;
404}
405
406///////////////////////////////////////////////////////////////////////////////
407
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000408SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
409SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
410SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
411SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
412SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
413 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000414SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
415 shader_type_mismatch);
416SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 5, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000417
bsalomon@google.com84405e02012-03-05 19:57:21 +0000418namespace {
419
420// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
421// justAlpha indicates that skPaint's alpha should be used rather than the color
422// Callers may subsequently modify the GrPaint. Setting constantColor indicates
423// that the final paint will draw the same color at every pixel. This allows
424// an optimization where the the color filter can be applied to the skPaint's
425// color once while converting to GrPain and then ignored.
426inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
427 bool justAlpha,
428 bool constantColor,
429 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000430
431 grPaint->fDither = skPaint.isDither();
432 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000433 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000434
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000435 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
436 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000437
438 SkXfermode* mode = skPaint.getXfermode();
439 if (mode) {
440 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000441 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000442#if 0
443 return false;
444#endif
445 }
446 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000447 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
448 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
449
bsalomon@google.com5782d712011-01-21 21:03:59 +0000450 if (justAlpha) {
451 uint8_t alpha = skPaint.getAlpha();
452 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000453 // justAlpha is currently set to true only if there is a texture,
454 // so constantColor should not also be true.
455 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000456 } else {
457 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000458 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000459 }
Scroggo97c88c22011-05-11 14:05:25 +0000460 SkColorFilter* colorFilter = skPaint.getColorFilter();
461 SkColor color;
462 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000463 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000464 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000465 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000466 if (!constantColor) {
467 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
468 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000469 } else {
470 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
471 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000472 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000473 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000474 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
475 grPaint->fColorMatrixEnabled = true;
476 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
477 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
478 } else {
479 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000480 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000481 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000482}
483
bsalomon@google.com84405e02012-03-05 19:57:21 +0000484// This function is similar to skPaint2GrPaintNoShader but also converts
485// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
486// be used is set on grPaint and returned in param act. constantColor has the
487// same meaning as in skPaint2GrPaintNoShader.
488inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
489 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000490 bool constantColor,
491 SkGpuDevice::SkAutoCachedTexture* act,
492 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000493
bsalomon@google.com5782d712011-01-21 21:03:59 +0000494 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
bsalomon@google.com5782d712011-01-21 21:03:59 +0000496 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000497 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000498 return skPaint2GrPaintNoShader(skPaint,
499 false,
500 constantColor,
501 grPaint);
502 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000503 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000504 }
505
reed@google.comac10a2d2010-12-22 21:39:39 +0000506 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000507 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000508 SkShader::TileMode tileModes[2];
509 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000510 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 tileModes, twoPointParams);
512
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000513 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000514 SkShader::GradientInfo info;
515 SkColor color;
516
517 info.fColors = &color;
518 info.fColorOffsets = NULL;
519 info.fColorCount = 1;
520 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
521 SkPaint copy(skPaint);
522 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000523 // modulate the paint alpha by the shader's solid color alpha
524 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
525 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000526 return skPaint2GrPaintNoShader(copy,
527 false,
528 constantColor,
529 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000530 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000531 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000532 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000533 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000534 switch (bmptype) {
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000535 case SkShader::kRadial_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000536 sampler->setCustomStage(SkNEW(GrRadialGradient))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000537 sampler->setFilter(GrSamplerState::kBilinear_Filter);
538 break;
539 case SkShader::kSweep_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000540 sampler->setCustomStage(SkNEW(GrSweepGradient))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000541 sampler->setFilter(GrSamplerState::kBilinear_Filter);
542 break;
543 case SkShader::kTwoPointRadial_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000544 sampler->setCustomStage(SkNEW_ARGS(GrRadial2Gradient,
545 (twoPointParams[0],
546 twoPointParams[1],
547 twoPointParams[2] < 0)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000548 sampler->setFilter(GrSamplerState::kBilinear_Filter);
549 break;
rileya@google.com3e332582012-07-03 13:43:35 +0000550 case SkShader::kTwoPointConical_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000551 sampler->setCustomStage(SkNEW_ARGS(GrConical2Gradient,
552 (twoPointParams[0],
553 twoPointParams[1],
554 twoPointParams[2])))->unref();
rileya@google.com3e332582012-07-03 13:43:35 +0000555 sampler->setFilter(GrSamplerState::kBilinear_Filter);
556 break;
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000557 default:
558 if (skPaint.isFilterBitmap()) {
559 sampler->setFilter(GrSamplerState::kBilinear_Filter);
560 } else {
561 sampler->setFilter(GrSamplerState::kNearest_Filter);
562 }
563 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000564 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000565 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
566 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000567
bsalomon@google.com84405e02012-03-05 19:57:21 +0000568 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000569 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000570 SkDebugf("Couldn't convert bitmap to texture.\n");
571 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000572 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000573 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000574
575 // since our texture coords will be in local space, we wack the texture
576 // matrix to map them back into 0...1 before we load it
577 SkMatrix localM;
578 if (shader->getLocalMatrix(&localM)) {
579 SkMatrix inverse;
580 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000581 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000582 }
583 }
584 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000585 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
586 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000587 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000588 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000589 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000590 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000591 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000592
593 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000594}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000595}
reed@google.comac10a2d2010-12-22 21:39:39 +0000596
597///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000598void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000599 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000600}
601
reed@google.comac10a2d2010-12-22 21:39:39 +0000602void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
603 CHECK_SHOULD_DRAW(draw);
604
bsalomon@google.com5782d712011-01-21 21:03:59 +0000605 GrPaint grPaint;
606 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000607 if (!skPaint2GrPaintShader(this,
608 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000609 true,
610 &act,
611 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000612 return;
613 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000614
615 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000616}
617
618// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000619static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000620 kPoints_GrPrimitiveType,
621 kLines_GrPrimitiveType,
622 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000623};
624
625void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000626 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000627 CHECK_SHOULD_DRAW(draw);
628
629 SkScalar width = paint.getStrokeWidth();
630 if (width < 0) {
631 return;
632 }
633
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000634 // we only handle hairlines and paints without path effects or mask filters,
635 // else we let the SkDraw call our drawPath()
636 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000637 draw.drawPoints(mode, count, pts, paint, true);
638 return;
639 }
640
bsalomon@google.com5782d712011-01-21 21:03:59 +0000641 GrPaint grPaint;
642 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000643 if (!skPaint2GrPaintShader(this,
644 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000645 true,
646 &act,
647 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000648 return;
649 }
650
bsalomon@google.com5782d712011-01-21 21:03:59 +0000651 fContext->drawVertices(grPaint,
652 gPointMode2PrimtiveType[mode],
653 count,
654 (GrPoint*)pts,
655 NULL,
656 NULL,
657 NULL,
658 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000659}
660
reed@google.comc9aa5872011-04-05 21:05:37 +0000661///////////////////////////////////////////////////////////////////////////////
662
reed@google.comac10a2d2010-12-22 21:39:39 +0000663void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
664 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000665 CHECK_SHOULD_DRAW(draw);
666
bungeman@google.com79bd8772011-07-18 15:34:08 +0000667 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000668 SkScalar width = paint.getStrokeWidth();
669
670 /*
671 We have special code for hairline strokes, miter-strokes, and fills.
672 Anything else we just call our path code.
673 */
674 bool usePath = doStroke && width > 0 &&
675 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000676 // another two reasons we might need to call drawPath...
677 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000678 usePath = true;
679 }
reed@google.com67db6642011-05-26 11:46:35 +0000680 // until we aa rotated rects...
681 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
682 usePath = true;
683 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000684 // small miter limit means right angles show bevel...
685 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
686 paint.getStrokeMiter() < SK_ScalarSqrt2)
687 {
688 usePath = true;
689 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000690 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000691 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
692 usePath = true;
693 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000694
695 if (usePath) {
696 SkPath path;
697 path.addRect(rect);
698 this->drawPath(draw, path, paint, NULL, true);
699 return;
700 }
701
702 GrPaint grPaint;
703 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000704 if (!skPaint2GrPaintShader(this,
705 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000706 true,
707 &act,
708 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000709 return;
710 }
reed@google.com20efde72011-05-09 17:00:02 +0000711 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000712}
713
reed@google.com69302852011-02-16 18:08:07 +0000714#include "SkMaskFilter.h"
715#include "SkBounder.h"
716
bsalomon@google.com85003222012-03-28 14:44:37 +0000717///////////////////////////////////////////////////////////////////////////////
718
719// helpers for applying mask filters
720namespace {
721
722GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000723 switch (fillType) {
724 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000725 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000726 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000727 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000728 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000729 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000730 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000731 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000732 default:
733 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000734 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000735 }
736}
737
bsalomon@google.com85003222012-03-28 14:44:37 +0000738// We prefer to blur small rect with small radius via CPU.
739#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
740#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
741inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
742 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
743 rect.height() <= MIN_GPU_BLUR_SIZE &&
744 radius <= MIN_GPU_BLUR_RADIUS) {
745 return true;
746 }
747 return false;
748}
749
750bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
751 SkMaskFilter* filter, const SkMatrix& matrix,
752 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000753 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000754#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000755 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000756#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000757 SkMaskFilter::BlurInfo info;
758 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000759 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000760 return false;
761 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000762 SkScalar radius = info.fIgnoreTransform ? info.fRadius
763 : matrix.mapRadius(info.fRadius);
764 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000765 if (radius <= 0) {
766 return false;
767 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000768
769 SkRect srcRect = path.getBounds();
770 if (shouldDrawBlurWithCPU(srcRect, radius)) {
771 return false;
772 }
773
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000774 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000775 float sigma3 = sigma * 3.0f;
776
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000777 SkRect clipRect;
778 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000779
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000780 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000781 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
782 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000783 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000784 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000785 SkIRect finalIRect;
786 finalRect.roundOut(&finalIRect);
787 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000788 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000789 }
790 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000791 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000792 }
793 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000794 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000795 GrTextureDesc desc;
796 desc.fFlags = kRenderTarget_GrTextureFlagBit;
797 desc.fWidth = SkScalarCeilToInt(srcRect.width());
798 desc.fHeight = SkScalarCeilToInt(srcRect.height());
799 // We actually only need A8, but it often isn't supported as a
800 // render target so default to RGBA_8888
801 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000802
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000803 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
804 desc.fConfig = kAlpha_8_GrPixelConfig;
805 }
806
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000807 GrAutoScratchTexture pathEntry(context, desc);
808 GrTexture* pathTexture = pathEntry.texture();
809 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000810 return false;
811 }
812 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000813 // Once this code moves into GrContext, this should be changed to use
814 // an AutoClipRestore.
815 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000816 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000817 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000819 GrPaint tempPaint;
820 tempPaint.reset();
821
822 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000823 tempPaint.fAntiAlias = grp->fAntiAlias;
824 if (tempPaint.fAntiAlias) {
825 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
826 // blend coeff of zero requires dual source blending support in order
827 // to properly blend partially covered pixels. This means the AA
828 // code path may not be taken. So we use a dst blend coeff of ISA. We
829 // could special case AA draws to a dst surface with known alpha=0 to
830 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000831 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
832 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000833 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000834 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000835 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000836
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000837 GrAutoScratchTexture temp1, temp2;
838 // If we're doing a normal blur, we can clobber the pathTexture in the
839 // gaussianBlur. Otherwise, we need to save it for later compositing.
840 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000841 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
842 &temp1,
843 isNormalBlur ? NULL : &temp2,
844 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000845
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000846 if (!isNormalBlur) {
847 GrPaint paint;
848 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000849 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000850 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
851 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000852 // Blend pathTexture over blurTexture.
853 context->setRenderTarget(blurTexture->asRenderTarget());
854 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000855 if (SkMaskFilter::kInner_BlurType == blurType) {
856 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000857 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
858 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000859 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
860 // solid: dst = src + dst - src * dst
861 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000862 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
863 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000864 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
865 // outer: dst = dst * (1 - src)
866 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000867 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
868 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000869 }
870 context->drawRect(paint, srcRect);
871 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000872 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000873 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000874
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000875 if (grp->hasTextureOrMask()) {
876 GrMatrix inverse;
877 if (!matrix.invert(&inverse)) {
878 return false;
879 }
880 grp->preConcatActiveSamplerMatrices(inverse);
881 }
882
883 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
884 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000885 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000886 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000887 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000888
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000889 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
890 -finalRect.fTop);
891 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
892 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000893 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000894 return true;
895}
896
bsalomon@google.com85003222012-03-28 14:44:37 +0000897bool drawWithMaskFilter(GrContext* context, const SkPath& path,
898 SkMaskFilter* filter, const SkMatrix& matrix,
899 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000900 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000901 SkMask srcM, dstM;
902
903 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000904 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000905 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000906 return false;
907 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000908 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000909
910 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
911 return false;
912 }
913 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000914 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000915
916 if (clip.quickReject(dstM.fBounds)) {
917 return false;
918 }
919 if (bounder && !bounder->doIRect(dstM.fBounds)) {
920 return false;
921 }
922
923 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
924 // the current clip (and identity matrix) and grpaint settings
925
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000926 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000927 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000928
reed@google.com0c219b62011-02-16 21:31:18 +0000929 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000930
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000931 GrTextureDesc desc;
932 desc.fWidth = dstM.fBounds.width();
933 desc.fHeight = dstM.fBounds.height();
934 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000935
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000936 GrAutoScratchTexture ast(context, desc);
937 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000938
reed@google.com69302852011-02-16 18:08:07 +0000939 if (NULL == texture) {
940 return false;
941 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000942 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000943 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000944
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000945 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
946 grp->preConcatActiveSamplerMatrices(ivm);
947 }
948
949 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
950 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000951 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000952 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000953 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000954
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000955 GrRect d;
956 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000957 GrIntToScalar(dstM.fBounds.fTop),
958 GrIntToScalar(dstM.fBounds.fRight),
959 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000960
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000961 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
962 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
963 -dstM.fBounds.fTop*SK_Scalar1);
964 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000965 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000966 return true;
967}
reed@google.com69302852011-02-16 18:08:07 +0000968
bsalomon@google.com85003222012-03-28 14:44:37 +0000969}
970
971///////////////////////////////////////////////////////////////////////////////
972
reed@google.com0c219b62011-02-16 21:31:18 +0000973void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000974 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000975 bool pathIsMutable) {
976 CHECK_SHOULD_DRAW(draw);
977
reed@google.comfe626382011-09-21 13:50:35 +0000978 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000979
bsalomon@google.com5782d712011-01-21 21:03:59 +0000980 GrPaint grPaint;
981 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000982 if (!skPaint2GrPaintShader(this,
983 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000984 true,
985 &act,
986 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000987 return;
988 }
989
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000990 // can we cheat, and threat a thin stroke as a hairline w/ coverage
991 // if we can, we draw lots faster (raster device does this same test)
992 SkScalar hairlineCoverage;
993 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
994 doFill = false;
995 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
996 grPaint.fCoverage);
997 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000998
reed@google.comfe626382011-09-21 13:50:35 +0000999 // If we have a prematrix, apply it to the path, optimizing for the case
1000 // where the original path can in fact be modified in place (even though
1001 // its parameter type is const).
1002 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1003 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001004
1005 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001006 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001007
reed@google.come3445642011-02-16 23:20:39 +00001008 if (!pathIsMutable) {
1009 result = &tmpPath;
1010 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001011 }
reed@google.come3445642011-02-16 23:20:39 +00001012 // should I push prePathMatrix on our MV stack temporarily, instead
1013 // of applying it here? See SkDraw.cpp
1014 pathPtr->transform(*prePathMatrix, result);
1015 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001016 }
reed@google.com0c219b62011-02-16 21:31:18 +00001017 // at this point we're done with prePathMatrix
1018 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001019
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001020 if (paint.getPathEffect() ||
1021 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001022 // it is safe to use tmpPath here, even if we already used it for the
1023 // prepathmatrix, since getFillPath can take the same object for its
1024 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001025 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001026 pathPtr = &tmpPath;
1027 }
1028
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001029 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001030 // avoid possibly allocating a new path in transform if we can
1031 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1032
1033 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001034 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001035 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001036 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001037 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001038 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001039 &grPaint, pathFillType)) {
1040 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1041 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001042 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001043 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001044 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001045 }
reed@google.com69302852011-02-16 18:08:07 +00001046 return;
1047 }
reed@google.com69302852011-02-16 18:08:07 +00001048
bsalomon@google.com47059542012-06-06 20:51:20 +00001049 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001050
reed@google.com0c219b62011-02-16 21:31:18 +00001051 if (doFill) {
1052 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001053 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001054 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001055 break;
1056 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001057 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 break;
1059 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001060 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001061 break;
1062 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001063 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 break;
1065 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001066 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001067 return;
1068 }
1069 }
1070
reed@google.com07f3ee12011-05-16 17:21:57 +00001071 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001072}
1073
bsalomon@google.comfb309512011-11-30 14:13:48 +00001074namespace {
1075
1076inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1077 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1078 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1079 return tilesX * tilesY;
1080}
1081
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001082inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001083 const SkIRect* srcRectPtr,
1084 int maxTextureSize) {
1085 static const int kSmallTileSize = 1 << 10;
1086 if (maxTextureSize <= kSmallTileSize) {
1087 return maxTextureSize;
1088 }
1089
1090 size_t maxTexTotalTileSize;
1091 size_t smallTotalTileSize;
1092
1093 if (NULL == srcRectPtr) {
1094 int w = bitmap.width();
1095 int h = bitmap.height();
1096 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1097 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1098 } else {
1099 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1100 srcRectPtr->fTop,
1101 srcRectPtr->fRight,
1102 srcRectPtr->fBottom,
1103 maxTextureSize);
1104 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1105 srcRectPtr->fTop,
1106 srcRectPtr->fRight,
1107 srcRectPtr->fBottom,
1108 kSmallTileSize);
1109 }
1110 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1111 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1112
1113 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1114 return kSmallTileSize;
1115 } else {
1116 return maxTextureSize;
1117 }
1118}
1119}
1120
1121bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1122 const GrSamplerState& sampler,
1123 const SkIRect* srcRectPtr,
1124 int* tileSize) const {
1125 SkASSERT(NULL != tileSize);
1126
1127 // if bitmap is explictly texture backed then just use the texture
1128 if (NULL != bitmap.getTexture()) {
1129 return false;
1130 }
1131 // if it's larger than the max texture size, then we have no choice but
1132 // tiling
1133 const int maxTextureSize = fContext->getMaxTextureSize();
1134 if (bitmap.width() > maxTextureSize ||
1135 bitmap.height() > maxTextureSize) {
1136 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1137 return true;
1138 }
1139 // if we are going to have to draw the whole thing, then don't tile
1140 if (NULL == srcRectPtr) {
1141 return false;
1142 }
1143 // if the entire texture is already in our cache then no reason to tile it
1144 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1145 return false;
1146 }
1147
1148 // At this point we know we could do the draw by uploading the entire bitmap
1149 // as a texture. However, if the texture would be large compared to the
1150 // cache size and we don't require most of it for this draw then tile to
1151 // reduce the amount of upload and cache spill.
1152
1153 // assumption here is that sw bitmap size is a good proxy for its size as
1154 // a texture
1155 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001156 size_t cacheSize;
1157 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001158 if (bmpSize < cacheSize / 2) {
1159 return false;
1160 }
1161
1162 SkFixed fracUsed =
1163 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1164 (srcRectPtr->height() << 16) / bitmap.height());
1165 if (fracUsed <= SK_FixedHalf) {
1166 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1167 return true;
1168 } else {
1169 return false;
1170 }
1171}
1172
reed@google.comac10a2d2010-12-22 21:39:39 +00001173void SkGpuDevice::drawBitmap(const SkDraw& draw,
1174 const SkBitmap& bitmap,
1175 const SkIRect* srcRectPtr,
1176 const SkMatrix& m,
1177 const SkPaint& paint) {
1178 CHECK_SHOULD_DRAW(draw);
1179
1180 SkIRect srcRect;
1181 if (NULL == srcRectPtr) {
1182 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1183 } else {
1184 srcRect = *srcRectPtr;
1185 }
1186
junov@google.comd935cfb2011-06-27 20:48:23 +00001187 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001188 // Convert the bitmap to a shader so that the rect can be drawn
1189 // through drawRect, which supports mask filters.
1190 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001191 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001192 if (srcRectPtr) {
1193 if (!bitmap.extractSubset(&tmp, srcRect)) {
1194 return; // extraction failed
1195 }
1196 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001197 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001198 }
1199 SkPaint paintWithTexture(paint);
1200 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1201 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001202 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001203 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001204
junov@google.com1d329782011-07-28 20:10:09 +00001205 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001206 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001207 // also affect the behavior of the mask filter.
1208 SkMatrix drawMatrix;
1209 drawMatrix.setConcat(*draw.fMatrix, m);
1210 SkDraw transformedDraw(draw);
1211 transformedDraw.fMatrix = &drawMatrix;
1212
1213 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1214
junov@google.comd935cfb2011-06-27 20:48:23 +00001215 return;
1216 }
1217
bsalomon@google.com5782d712011-01-21 21:03:59 +00001218 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001219 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001220 return;
1221 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001222 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001223 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001224 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001225 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001226 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001227 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001228
bsalomon@google.comfb309512011-11-30 14:13:48 +00001229 int tileSize;
1230 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1231 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001232 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001233 return;
1234 }
1235
1236 // undo the translate done by SkCanvas
1237 int DX = SkMax32(0, srcRect.fLeft);
1238 int DY = SkMax32(0, srcRect.fTop);
1239 // compute clip bounds in local coordinates
1240 SkIRect clipRect;
1241 {
1242 SkRect r;
1243 r.set(draw.fClip->getBounds());
1244 SkMatrix matrix, inverse;
1245 matrix.setConcat(*draw.fMatrix, m);
1246 if (!matrix.invert(&inverse)) {
1247 return;
1248 }
1249 inverse.mapRect(&r);
1250 r.roundOut(&clipRect);
1251 // apply the canvas' translate to our local clip
1252 clipRect.offset(DX, DY);
1253 }
1254
bsalomon@google.comfb309512011-11-30 14:13:48 +00001255 int nx = bitmap.width() / tileSize;
1256 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001257 for (int x = 0; x <= nx; x++) {
1258 for (int y = 0; y <= ny; y++) {
1259 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001260 tileR.set(x * tileSize, y * tileSize,
1261 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001262 if (!SkIRect::Intersects(tileR, clipRect)) {
1263 continue;
1264 }
1265
1266 SkIRect srcR = tileR;
1267 if (!srcR.intersect(srcRect)) {
1268 continue;
1269 }
1270
1271 SkBitmap tmpB;
1272 if (bitmap.extractSubset(&tmpB, tileR)) {
1273 // now offset it to make it "local" to our tmp bitmap
1274 srcR.offset(-tileR.fLeft, -tileR.fTop);
1275
1276 SkMatrix tmpM(m);
1277 {
1278 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1279 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1280 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1281 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001282 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001283 }
1284 }
1285 }
1286}
1287
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001288namespace {
1289
1290bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1291 // detect pixel disalignment
1292 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1293 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1294 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1295 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1296 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1297 COLOR_BLEED_TOLERANCE &&
1298 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1299 COLOR_BLEED_TOLERANCE) {
1300 return true;
1301 }
1302 return false;
1303}
1304
1305bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1306 const SkMatrix& m) {
1307 // Only gets called if hasAlignedSamples returned false.
1308 // So we can assume that sampling is axis aligned but not texel aligned.
1309 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1310 SkRect innerSrcRect(srcRect), innerTransformedRect,
1311 outerTransformedRect(transformedRect);
1312 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1313 m.mapRect(&innerTransformedRect, innerSrcRect);
1314
1315 // The gap between outerTransformedRect and innerTransformedRect
1316 // represents the projection of the source border area, which is
1317 // problematic for color bleeding. We must check whether any
1318 // destination pixels sample the border area.
1319 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1320 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1321 SkIRect outer, inner;
1322 outerTransformedRect.round(&outer);
1323 innerTransformedRect.round(&inner);
1324 // If the inner and outer rects round to the same result, it means the
1325 // border does not overlap any pixel centers. Yay!
1326 return inner != outer;
1327}
1328
1329} // unnamed namespace
1330
reed@google.comac10a2d2010-12-22 21:39:39 +00001331/*
1332 * This is called by drawBitmap(), which has to handle images that may be too
1333 * large to be represented by a single texture.
1334 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001335 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1336 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 */
1338void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1339 const SkBitmap& bitmap,
1340 const SkIRect& srcRect,
1341 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001342 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001343 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1344 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001345
reed@google.com9c49bc32011-07-07 13:42:37 +00001346 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001347 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001348 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001349 return;
1350 }
1351
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001352 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001353
1354 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1355 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001356 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001357
1358 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001359 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001360 if (NULL == texture) {
1361 return;
1362 }
1363
bsalomon@google.com452943d2011-10-31 17:37:14 +00001364 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001365
reed@google.com20efde72011-05-09 17:00:02 +00001366 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1367 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001368 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001369 float wInv = 1.f / bitmap.width();
1370 float hInv = 1.f / bitmap.height();
1371 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1372 SkFloatToScalar(srcRect.fTop * hInv),
1373 SkFloatToScalar(srcRect.fRight * wInv),
1374 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001375
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001376 bool needsTextureDomain = false;
1377 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1378 {
1379 // Need texture domain if drawing a sub rect.
1380 needsTextureDomain = srcRect.width() < bitmap.width() ||
1381 srcRect.height() < bitmap.height();
1382 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1383 // sampling is axis-aligned
1384 GrRect floatSrcRect, transformedRect;
1385 floatSrcRect.set(srcRect);
1386 SkMatrix srcToDeviceMatrix(m);
1387 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1388 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1389
1390 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1391 // Samples are texel-aligned, so filtering is futile
1392 sampler->setFilter(GrSamplerState::kNearest_Filter);
1393 needsTextureDomain = false;
1394 } else {
1395 needsTextureDomain = needsTextureDomain &&
1396 mayColorBleed(floatSrcRect, transformedRect, m);
1397 }
1398 }
1399 }
1400
1401 GrRect textureDomain = GrRect::MakeEmpty();
1402
1403 if (needsTextureDomain) {
1404 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001405 GrScalar left, top, right, bottom;
1406 if (srcRect.width() > 1) {
1407 GrScalar border = GR_ScalarHalf / bitmap.width();
1408 left = paintRect.left() + border;
1409 right = paintRect.right() - border;
1410 } else {
1411 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1412 }
1413 if (srcRect.height() > 1) {
1414 GrScalar border = GR_ScalarHalf / bitmap.height();
1415 top = paintRect.top() + border;
1416 bottom = paintRect.bottom() - border;
1417 } else {
1418 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1419 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001420 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001421 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001422 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001423
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001424 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001425}
1426
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001427namespace {
1428
1429void apply_custom_stage(GrContext* context,
1430 GrTexture* srcTexture,
1431 GrTexture* dstTexture,
1432 const GrRect& rect,
1433 GrCustomStage* stage) {
1434 SkASSERT(srcTexture && srcTexture->getContext() == context);
1435 GrAutoMatrix avm(context, GrMatrix::I());
1436 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
1437 GrClip oldClip = context->getClip();
1438 context->setClip(rect);
1439
1440 GrMatrix sampleM;
1441 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1442 GrPaint paint;
1443 paint.reset();
1444 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1445 paint.textureSampler(0)->reset(sampleM);
1446 paint.textureSampler(0)->setCustomStage(stage);
1447 paint.setTexture(0, srcTexture);
1448 context->drawRect(paint, rect);
1449 context->setClip(oldClip);
1450}
1451
1452};
1453
reed@google.com8926b162012-03-23 15:36:36 +00001454static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1455 SkImageFilter* filter, const GrRect& rect) {
1456 GrAssert(filter);
1457
1458 SkSize blurSize;
1459 SkISize radius;
1460
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001461 GrTextureDesc desc;
1462 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1463 desc.fWidth = SkScalarCeilToInt(rect.width());
1464 desc.fHeight = SkScalarCeilToInt(rect.height());
1465 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001466 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001467
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001468 if (filter->asNewCustomStage(&stage)) {
1469 GrAutoScratchTexture dst(context, desc);
1470 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1471 texture = dst.detach();
1472 stage->unref();
1473 } else if (filter->asABlur(&blurSize)) {
reed@google.com8926b162012-03-23 15:36:36 +00001474 GrAutoScratchTexture temp1, temp2;
1475 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1476 blurSize.width(),
1477 blurSize.height());
1478 texture->ref();
1479 } else if (filter->asADilate(&radius)) {
1480 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1481 texture = context->applyMorphology(texture, rect,
1482 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001483 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001484 radius);
1485 texture->ref();
1486 } else if (filter->asAnErode(&radius)) {
1487 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1488 texture = context->applyMorphology(texture, rect,
1489 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001490 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001491 radius);
1492 texture->ref();
1493 }
1494 return texture;
1495}
1496
reed@google.comac10a2d2010-12-22 21:39:39 +00001497void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1498 int left, int top, const SkPaint& paint) {
1499 CHECK_SHOULD_DRAW(draw);
1500
reed@google.com8926b162012-03-23 15:36:36 +00001501 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001502 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1503 return;
1504 }
1505
reed@google.com76dd2772012-01-05 21:15:07 +00001506 int w = bitmap.width();
1507 int h = bitmap.height();
1508
bsalomon@google.com5782d712011-01-21 21:03:59 +00001509 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001510 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001511 return;
1512 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001513
bsalomon@google.com5782d712011-01-21 21:03:59 +00001514 GrAutoMatrix avm(fContext, GrMatrix::I());
1515
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001516 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001517
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001518 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001519 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001520 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001521 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001522
reed@google.com8926b162012-03-23 15:36:36 +00001523 SkImageFilter* filter = paint.getImageFilter();
1524 if (NULL != filter) {
1525 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001526 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001527 if (filteredTexture) {
1528 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1529 texture = filteredTexture;
1530 filteredTexture->unref();
1531 }
reed@google.com76dd2772012-01-05 21:15:07 +00001532 }
reed@google.com8926b162012-03-23 15:36:36 +00001533
bsalomon@google.com5782d712011-01-21 21:03:59 +00001534 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001535 GrRect::MakeXYWH(GrIntToScalar(left),
1536 GrIntToScalar(top),
1537 GrIntToScalar(w),
1538 GrIntToScalar(h)),
1539 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1540 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001541}
1542
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001543void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001544 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001545 // clear of the source device must occur before CHECK_SHOULD_DRAW
1546 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1547 if (dev->fNeedClear) {
1548 // TODO: could check here whether we really need to draw at all
1549 dev->clear(0x0);
1550 }
1551
reed@google.comac10a2d2010-12-22 21:39:39 +00001552 CHECK_SHOULD_DRAW(draw);
1553
bsalomon@google.com5782d712011-01-21 21:03:59 +00001554 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001555 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001556 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001557 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001558 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001559
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001560 GrTexture* devTex = grPaint.getTexture(0);
1561 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001562
reed@google.com8926b162012-03-23 15:36:36 +00001563 SkImageFilter* filter = paint.getImageFilter();
1564 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001565 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1566 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001567 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1568 rect);
1569 if (filteredTexture) {
1570 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1571 devTex = filteredTexture;
1572 filteredTexture->unref();
1573 }
1574 }
1575
bsalomon@google.com5782d712011-01-21 21:03:59 +00001576 const SkBitmap& bm = dev->accessBitmap(false);
1577 int w = bm.width();
1578 int h = bm.height();
1579
1580 GrAutoMatrix avm(fContext, GrMatrix::I());
1581
bsalomon@google.com97912912011-12-06 16:30:36 +00001582 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001583
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001584 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1585 GrIntToScalar(y),
1586 GrIntToScalar(w),
1587 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001588
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001589 // The device being drawn may not fill up its texture (saveLayer uses
1590 // the approximate ).
1591 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1592 GR_Scalar1 * h / devTex->height());
1593
1594 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001595}
1596
reed@google.com8926b162012-03-23 15:36:36 +00001597bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001598 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001599 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001600
1601 if (!filter->asNewCustomStage(NULL) &&
1602 !filter->asABlur(&size) &&
1603 !filter->asADilate(&radius) &&
1604 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001605 return false;
1606 }
reed@google.com8926b162012-03-23 15:36:36 +00001607 return true;
1608}
1609
1610bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1611 const SkMatrix& ctm,
1612 SkBitmap* result, SkIPoint* offset) {
1613 // want explicitly our impl, so guard against a subclass of us overriding it
1614 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001615 return false;
1616 }
reed@google.com8926b162012-03-23 15:36:36 +00001617
1618 SkAutoLockPixels alp(src, !src.getTexture());
1619 if (!src.getTexture() && !src.readyToDraw()) {
1620 return false;
1621 }
1622
1623 GrPaint paint;
1624 paint.reset();
1625
1626 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1627
1628 GrTexture* texture;
1629 SkAutoCachedTexture act(this, src, sampler, &texture);
1630
1631 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001632 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1633 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001634 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1635 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001636 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1637 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001638 resultTexture->unref();
1639 }
reed@google.com76dd2772012-01-05 21:15:07 +00001640 return true;
1641}
1642
reed@google.comac10a2d2010-12-22 21:39:39 +00001643///////////////////////////////////////////////////////////////////////////////
1644
1645// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001646static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001647 kTriangles_GrPrimitiveType,
1648 kTriangleStrip_GrPrimitiveType,
1649 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001650};
1651
1652void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1653 int vertexCount, const SkPoint vertices[],
1654 const SkPoint texs[], const SkColor colors[],
1655 SkXfermode* xmode,
1656 const uint16_t indices[], int indexCount,
1657 const SkPaint& paint) {
1658 CHECK_SHOULD_DRAW(draw);
1659
bsalomon@google.com5782d712011-01-21 21:03:59 +00001660 GrPaint grPaint;
1661 SkAutoCachedTexture act;
1662 // we ignore the shader if texs is null.
1663 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001664 if (!skPaint2GrPaintNoShader(paint,
1665 false,
1666 NULL == colors,
1667 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001668 return;
1669 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001670 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001671 if (!skPaint2GrPaintShader(this,
1672 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001673 NULL == colors,
1674 &act,
1675 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001676 return;
1677 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001678 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001679
1680 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001681 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001682 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1683#if 0
1684 return
1685#endif
1686 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001687 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001688
bsalomon@google.com498776a2011-08-16 19:20:44 +00001689 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1690 if (NULL != colors) {
1691 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001692 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001693 for (int i = 0; i < vertexCount; ++i) {
1694 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1695 }
1696 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001697 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001698 fContext->drawVertices(grPaint,
1699 gVertexMode2PrimitiveType[vmode],
1700 vertexCount,
1701 (GrPoint*) vertices,
1702 (GrPoint*) texs,
1703 colors,
1704 indices,
1705 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001706}
1707
1708///////////////////////////////////////////////////////////////////////////////
1709
1710static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001711 GrFontScaler* scaler = (GrFontScaler*)data;
1712 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001713}
1714
1715static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1716 void* auxData;
1717 GrFontScaler* scaler = NULL;
1718 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1719 scaler = (GrFontScaler*)auxData;
1720 }
1721 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001722 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001723 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1724 }
1725 return scaler;
1726}
1727
1728static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1729 SkFixed fx, SkFixed fy,
1730 const SkGlyph& glyph) {
1731 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1732
bungeman@google.com15865a72012-01-11 16:28:04 +00001733 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001734
1735 if (NULL == procs->fFontScaler) {
1736 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1737 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001738
bungeman@google.com15865a72012-01-11 16:28:04 +00001739 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1740 glyph.getSubXFixed(),
1741 glyph.getSubYFixed()),
1742 SkFixedFloorToFixed(fx),
1743 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001744 procs->fFontScaler);
1745}
1746
bsalomon@google.com5782d712011-01-21 21:03:59 +00001747SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001748
1749 // deferred allocation
1750 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001751 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001752 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1753 fDrawProcs->fContext = fContext;
1754 }
1755
1756 // init our (and GL's) state
1757 fDrawProcs->fTextContext = context;
1758 fDrawProcs->fFontScaler = NULL;
1759 return fDrawProcs;
1760}
1761
1762void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1763 size_t byteLength, SkScalar x, SkScalar y,
1764 const SkPaint& paint) {
1765 CHECK_SHOULD_DRAW(draw);
1766
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001767 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001768 // this guy will just call our drawPath()
1769 draw.drawText((const char*)text, byteLength, x, y, paint);
1770 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001771 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001772
1773 GrPaint grPaint;
1774 SkAutoCachedTexture act;
1775
bsalomon@google.com84405e02012-03-05 19:57:21 +00001776 if (!skPaint2GrPaintShader(this,
1777 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001778 true,
1779 &act,
1780 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001781 return;
1782 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001783 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1784 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001785 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1786 }
1787}
1788
1789void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1790 size_t byteLength, const SkScalar pos[],
1791 SkScalar constY, int scalarsPerPos,
1792 const SkPaint& paint) {
1793 CHECK_SHOULD_DRAW(draw);
1794
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001795 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001796 // this guy will just call our drawPath()
1797 draw.drawPosText((const char*)text, byteLength, pos, constY,
1798 scalarsPerPos, paint);
1799 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001800 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001801
1802 GrPaint grPaint;
1803 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001804 if (!skPaint2GrPaintShader(this,
1805 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001806 true,
1807 &act,
1808 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001809 return;
1810 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001811 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1812 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001813 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1814 scalarsPerPos, paint);
1815 }
1816}
1817
1818void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1819 size_t len, const SkPath& path,
1820 const SkMatrix* m, const SkPaint& paint) {
1821 CHECK_SHOULD_DRAW(draw);
1822
1823 SkASSERT(draw.fDevice == this);
1824 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1825}
1826
1827///////////////////////////////////////////////////////////////////////////////
1828
reed@google.comf67e4cf2011-03-15 20:56:58 +00001829bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1830 if (!paint.isLCDRenderText()) {
1831 // we're cool with the paint as is
1832 return false;
1833 }
1834
1835 if (paint.getShader() ||
1836 paint.getXfermode() || // unless its srcover
1837 paint.getMaskFilter() ||
1838 paint.getRasterizer() ||
1839 paint.getColorFilter() ||
1840 paint.getPathEffect() ||
1841 paint.isFakeBoldText() ||
1842 paint.getStyle() != SkPaint::kFill_Style) {
1843 // turn off lcd
1844 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1845 flags->fHinting = paint.getHinting();
1846 return true;
1847 }
1848 // we're cool with the paint as is
1849 return false;
1850}
1851
reed@google.com75d939b2011-12-07 15:07:23 +00001852void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001853 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001854 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001855}
1856
reed@google.comf67e4cf2011-03-15 20:56:58 +00001857///////////////////////////////////////////////////////////////////////////////
1858
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001859SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1860 const SkBitmap& bitmap,
1861 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001862 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001863 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001864
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001865 if (!bitmap.isVolatile()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001866 uint64_t key = bitmap.getGenerationID();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001867 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001868
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001869 GrTextureDesc desc;
1870 desc.fWidth = bitmap.width();
1871 desc.fHeight = bitmap.height();
1872 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
robertphillips@google.com56f22442012-06-08 14:21:26 +00001873 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001874
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001875 entry = ctx->findAndLockTexture(desc, sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001876 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001877 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1878 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001879 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001880 } else {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001881 entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001882 sampler, bitmap);
1883 }
1884 if (NULL == entry.texture()) {
1885 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1886 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001887 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001888 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001889}
1890
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001891void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1892 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001893}
1894
bsalomon@google.comfb309512011-11-30 14:13:48 +00001895bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1896 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001897 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001898 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001899
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001900 GrTextureDesc desc;
1901 desc.fWidth = bitmap.width();
1902 desc.fHeight = bitmap.height();
1903 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
1904 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001905
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001906 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001907}
1908
1909
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001910SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1911 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001912 bool isOpaque,
1913 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001914 GrTextureDesc desc;
1915 desc.fConfig = fRenderTarget->config();
1916 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1917 desc.fWidth = width;
1918 desc.fHeight = height;
1919 desc.fSampleCnt = fRenderTarget->numSamples();
1920
1921 GrContext::TextureCacheEntry cacheEntry;
1922 GrTexture* texture;
1923 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001924 // Skia's convention is to only clear a device if it is non-opaque.
1925 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001926
1927#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1928 // layers are never draw in repeat modes, so we can request an approx
1929 // match and ignore any padding.
1930 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1931 GrContext::kApprox_ScratchTexMatch :
1932 GrContext::kExact_ScratchTexMatch;
1933 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1934 texture = cacheEntry.texture();
1935#else
1936 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1937 texture = tunref.get();
1938#endif
1939 if (texture) {
1940 return SkNEW_ARGS(SkGpuDevice,(fContext,
1941 texture,
1942 cacheEntry,
1943 needClear));
1944 } else {
1945 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1946 width, height);
1947 return NULL;
1948 }
1949}
1950
1951SkGpuDevice::SkGpuDevice(GrContext* context,
1952 GrTexture* texture,
1953 TexCache cacheEntry,
1954 bool needClear)
1955 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1956 GrAssert(texture && texture->asRenderTarget());
1957 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1958 this->initFromRenderTarget(context, texture->asRenderTarget());
1959 fCache = cacheEntry;
1960 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001961}
1962