blob: 105c4562100692fd2e5b5ade3d7b61cd09a3abf7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrContext.h"
12#include "GrTextContext.h"
13
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "SkGpuDevice.h"
15#include "SkGrTexturePixelRef.h"
16
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"
reed@google.comc9aa5872011-04-05 21:05:37 +000020#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
22#define CACHE_LAYER_TEXTURES 1
23
24#if 0
25 extern bool (*gShouldDrawProc)();
26 #define CHECK_SHOULD_DRAW(draw) \
27 do { \
28 if (gShouldDrawProc && !gShouldDrawProc()) return; \
29 this->prepareRenderTarget(draw); \
30 } while (0)
31#else
32 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw)
33#endif
34
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000035// we use the same texture slot on GrPaint for bitmaps and shaders
36// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
37enum {
38 kBitmapTextureIdx = 0,
39 kShaderTextureIdx = 0
40};
41
reed@google.comcde92112011-07-06 20:00:52 +000042
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000043#define MAX_BLUR_SIGMA 4.0f
44// FIXME: This value comes from from SkBlurMaskFilter.cpp.
45// Should probably be put in a common header someplace.
46#define MAX_BLUR_RADIUS SkIntToScalar(128)
47// This constant approximates the scaling done in the software path's
48// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
49// IMHO, it actually should be 1: we blur "less" than we should do
50// according to the CSS and canvas specs, simply because Safari does the same.
51// Firefox used to do the same too, until 4.0 where they fixed it. So at some
52// point we should probably get rid of these scaling constants and rebaseline
53// all the blur tests.
54#define BLUR_SIGMA_SCALE 0.6f
reed@google.comac10a2d2010-12-22 21:39:39 +000055///////////////////////////////////////////////////////////////////////////////
56
57SkGpuDevice::SkAutoCachedTexture::
58 SkAutoCachedTexture(SkGpuDevice* device,
59 const SkBitmap& bitmap,
60 const GrSamplerState& sampler,
61 GrTexture** texture) {
62 GrAssert(texture);
reed@google.comac10a2d2010-12-22 21:39:39 +000063 *texture = this->set(device, bitmap, sampler);
64}
65
66SkGpuDevice::SkAutoCachedTexture::SkAutoCachedTexture() {
reed@google.comac10a2d2010-12-22 21:39:39 +000067}
68
69GrTexture* SkGpuDevice::SkAutoCachedTexture::set(SkGpuDevice* device,
70 const SkBitmap& bitmap,
71 const GrSamplerState& sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000072 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000073 fDevice->unlockCachedTexture(fTex);
74 }
75 fDevice = device;
76 GrTexture* texture = (GrTexture*)bitmap.getTexture();
77 if (texture) {
78 // return the native texture
bsalomon@google.com50398bf2011-07-26 20:45:30 +000079 fTex.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000080 } else {
81 // look it up in our cache
bsalomon@google.com50398bf2011-07-26 20:45:30 +000082 fTex = device->lockCachedTexture(bitmap, sampler);
83 texture = fTex.texture();
reed@google.comac10a2d2010-12-22 21:39:39 +000084 }
85 return texture;
86}
87
88SkGpuDevice::SkAutoCachedTexture::~SkAutoCachedTexture() {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000089 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000090 fDevice->unlockCachedTexture(fTex);
91 }
92}
93
94///////////////////////////////////////////////////////////////////////////////
95
96bool gDoTraceDraw;
97
98struct GrSkDrawProcs : public SkDrawProcs {
99public:
100 GrContext* fContext;
101 GrTextContext* fTextContext;
102 GrFontScaler* fFontScaler; // cached in the skia glyphcache
103};
104
105///////////////////////////////////////////////////////////////////////////////
106
reed@google.comaf951c92011-06-16 19:10:39 +0000107static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
108 switch (config) {
109 case kAlpha_8_GrPixelConfig:
110 *isOpaque = false;
111 return SkBitmap::kA8_Config;
112 case kRGB_565_GrPixelConfig:
113 *isOpaque = true;
114 return SkBitmap::kRGB_565_Config;
115 case kRGBA_4444_GrPixelConfig:
116 *isOpaque = false;
117 return SkBitmap::kARGB_4444_Config;
118 case kRGBA_8888_GrPixelConfig:
119 case kRGBX_8888_GrPixelConfig:
120 *isOpaque = (kRGBX_8888_GrPixelConfig == config);
121 return SkBitmap::kARGB_8888_Config;
122 default:
123 *isOpaque = false;
124 return SkBitmap::kNo_Config;
125 }
126}
reed@google.comac10a2d2010-12-22 21:39:39 +0000127
reed@google.comaf951c92011-06-16 19:10:39 +0000128static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000129 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000130
131 bool isOpaque;
132 SkBitmap bitmap;
133 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
134 renderTarget->width(), renderTarget->height());
135 bitmap.setIsOpaque(isOpaque);
136 return bitmap;
137}
138
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000139SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
140: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
141 this->initFromRenderTarget(context, texture->asRenderTarget());
142}
143
reed@google.comaf951c92011-06-16 19:10:39 +0000144SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
145: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000146 this->initFromRenderTarget(context, renderTarget);
147}
148
149void SkGpuDevice::initFromRenderTarget(GrContext* context,
150 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000151 fNeedPrepareRenderTarget = false;
152 fDrawProcs = NULL;
153
154 fContext = context;
155 fContext->ref();
156
reed@google.comaf951c92011-06-16 19:10:39 +0000157 fTexture = NULL;
158 fRenderTarget = NULL;
159 fNeedClear = false;
160
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000161 GrAssert(NULL != renderTarget);
162 fRenderTarget = renderTarget;
163 fRenderTarget->ref();
164 // if this RT is also a texture, hold a ref on it
165 fTexture = fRenderTarget->asTexture();
166 SkSafeRef(fTexture);
reed@google.comaf951c92011-06-16 19:10:39 +0000167
168 SkGrRenderTargetPixelRef* pr = new SkGrRenderTargetPixelRef(fRenderTarget);
169 this->setPixelRef(pr, 0)->unref();
170}
171
172SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000173 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000174: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000175 fNeedPrepareRenderTarget = false;
176 fDrawProcs = NULL;
177
reed@google.com7b201d22011-01-11 18:59:23 +0000178 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000179 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000180
reed@google.comac10a2d2010-12-22 21:39:39 +0000181 fTexture = NULL;
182 fRenderTarget = NULL;
183 fNeedClear = false;
184
reed@google.comaf951c92011-06-16 19:10:39 +0000185 if (config != SkBitmap::kRGB_565_Config) {
186 config = SkBitmap::kARGB_8888_Config;
187 }
188 SkBitmap bm;
189 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000190
191#if CACHE_LAYER_TEXTURES
bsalomon@google.come97f0852011-06-17 13:10:25 +0000192 TexType type = (kSaveLayer_Usage == usage) ?
193 kSaveLayerDeviceRenderTarget_TexType :
194 kDeviceRenderTarget_TexType;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000195 fCache = this->lockCachedTexture(bm, GrSamplerState::ClampNoFilter(), type);
196 fTexture = fCache.texture();
197 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000198 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000199 // hold a ref directly on fTexture (even though fCache has one) to match
200 // other constructor paths. Simplifies cleanup.
201 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000202 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000203#else
reed@google.comaf951c92011-06-16 19:10:39 +0000204 const GrTextureDesc desc = {
205 kRenderTarget_GrTextureFlagBit,
206 kNone_GrAALevel,
207 width,
208 height,
209 SkGr::Bitmap2PixelConfig(bm)
210 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000211
reed@google.comaf951c92011-06-16 19:10:39 +0000212 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000213#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000214 if (NULL != fTexture) {
215 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000216 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000217
reed@google.comaf951c92011-06-16 19:10:39 +0000218 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000219
reed@google.comaf951c92011-06-16 19:10:39 +0000220 // we defer the actual clear until our gainFocus()
221 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000222
reed@google.comaf951c92011-06-16 19:10:39 +0000223 // wrap the bitmap with a pixelref to expose our texture
224 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000225 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000226 } else {
227 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
228 width, height);
229 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000230 }
231}
232
233SkGpuDevice::~SkGpuDevice() {
234 if (fDrawProcs) {
235 delete fDrawProcs;
236 }
237
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000238 SkSafeUnref(fTexture);
239 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000240 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000241 GrAssert(NULL != fTexture);
242 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000243 fContext->unlockTexture(fCache);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000244 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000245 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000246}
247
reed@google.comac10a2d2010-12-22 21:39:39 +0000248///////////////////////////////////////////////////////////////////////////////
249
250void SkGpuDevice::makeRenderTargetCurrent() {
251 fContext->setRenderTarget(fRenderTarget);
252 fContext->flush(true);
253 fNeedPrepareRenderTarget = true;
254}
255
256///////////////////////////////////////////////////////////////////////////////
257
258bool SkGpuDevice::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
259 SkIRect bounds;
260 bounds.set(0, 0, this->width(), this->height());
261 if (!bounds.intersect(srcRect)) {
262 return false;
263 }
264
265 const int w = bounds.width();
266 const int h = bounds.height();
267 SkBitmap tmp;
268 // note we explicitly specify our rowBytes to be snug (no gap between rows)
269 tmp.setConfig(SkBitmap::kARGB_8888_Config, w, h, w * 4);
270 if (!tmp.allocPixels()) {
271 return false;
272 }
273
Scroggo813c33c2011-04-07 20:56:21 +0000274 tmp.lockPixels();
reed@google.comac10a2d2010-12-22 21:39:39 +0000275
Scroggoeb176032011-04-07 21:11:49 +0000276 bool read = fContext->readRenderTargetPixels(fRenderTarget,
277 bounds.fLeft, bounds.fTop,
278 bounds.width(), bounds.height(),
279 kRGBA_8888_GrPixelConfig,
280 tmp.getPixels());
Scroggo813c33c2011-04-07 20:56:21 +0000281 tmp.unlockPixels();
282 if (!read) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000283 return false;
284 }
285
286 tmp.swap(*bitmap);
287 return true;
288}
289
290void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y) {
291 SkAutoLockPixels alp(bitmap);
292 if (!bitmap.readyToDraw()) {
293 return;
294 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000295 GrPixelConfig config = SkGr::BitmapConfig2PixelConfig(bitmap.config(),
296 bitmap.isOpaque());
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 fContext->setRenderTarget(fRenderTarget);
298 // we aren't setting the clip or matrix, so mark as dirty
299 // we don't need to set them for this call and don't have them anyway
300 fNeedPrepareRenderTarget = true;
301
302 fContext->writePixels(x, y, bitmap.width(), bitmap.height(),
303 config, bitmap.getPixels(), bitmap.rowBytes());
304}
305
306///////////////////////////////////////////////////////////////////////////////
307
308static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000309 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000310 const SkRegion& clipRegion,
311 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000312 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000313
314 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000315 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000316 const SkIRect& skBounds = clipRegion.getBounds();
317 GrRect bounds;
318 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
319 GrIntToScalar(skBounds.fTop),
320 GrIntToScalar(skBounds.fRight),
321 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000322 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
323 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000324 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000325}
326
327// call this ever each draw call, to ensure that the context reflects our state,
328// and not the state from some other canvas/device
329void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
330 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000331 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000332
333 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000334 SkASSERT(draw.fClipStack);
335 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000336 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000337 fNeedPrepareRenderTarget = false;
338 }
339}
340
reed@google.com46799cd2011-02-22 20:56:26 +0000341void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
342 const SkClipStack& clipStack) {
343 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000344 // We don't need to set them now because the context may not reflect this device.
345 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000346}
347
348void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000349 const SkRegion& clip, const SkClipStack& clipStack) {
350
reed@google.comac10a2d2010-12-22 21:39:39 +0000351 fContext->setRenderTarget(fRenderTarget);
352
bsalomon@google.comd302f142011-03-03 13:54:13 +0000353 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000354
reed@google.com6f8f2922011-03-04 22:27:10 +0000355 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000356
357 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000358 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000359 fNeedClear = false;
360 }
361}
362
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000363bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000364 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000365 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000366 return true;
367 }
368 return false;
369}
370
371///////////////////////////////////////////////////////////////////////////////
372
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000373SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
374SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
375SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
376SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
377SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
378 shader_type_mismatch);
379SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000380
bsalomon@google.com5782d712011-01-21 21:03:59 +0000381static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
382 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
383 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
384 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
385 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
386 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
387};
388
389bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
390 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000391 GrPaint* grPaint,
392 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000393
394 grPaint->fDither = skPaint.isDither();
395 grPaint->fAntiAlias = skPaint.isAntiAlias();
396
397 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
398 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
399
400 SkXfermode* mode = skPaint.getXfermode();
401 if (mode) {
402 if (!mode->asCoeff(&sm, &dm)) {
reed@google.com1a2e8d22011-01-21 22:08:29 +0000403 SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000404#if 0
405 return false;
406#endif
407 }
408 }
409 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
410 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
411
412 if (justAlpha) {
413 uint8_t alpha = skPaint.getAlpha();
414 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000415 // justAlpha is currently set to true only if there is a texture,
416 // so constantColor should not also be true.
417 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000418 } else {
419 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000420 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000421 }
Scroggo97c88c22011-05-11 14:05:25 +0000422 SkColorFilter* colorFilter = skPaint.getColorFilter();
423 SkColor color;
424 SkXfermode::Mode filterMode;
425 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000426 if (!constantColor) {
427 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
428 grPaint->fColorFilterXfermode = filterMode;
429 return true;
430 }
431 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
432 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000433 }
Scroggod757df22011-05-16 13:11:16 +0000434 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000435 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000436}
437
bsalomon@google.com5782d712011-01-21 21:03:59 +0000438bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
439 SkAutoCachedTexture* act,
440 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000441 GrPaint* grPaint,
442 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000443
bsalomon@google.com5782d712011-01-21 21:03:59 +0000444 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000445
bsalomon@google.com5782d712011-01-21 21:03:59 +0000446 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000447 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000448 return this->skPaint2GrPaintNoShader(skPaint,
449 false,
450 grPaint,
451 constantColor);
452 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000453 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000454 }
455
reed@google.comac10a2d2010-12-22 21:39:39 +0000456 SkBitmap bitmap;
457 SkMatrix matrix;
458 SkShader::TileMode tileModes[2];
459 SkScalar twoPointParams[3];
460 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
461 tileModes, twoPointParams);
462
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
464 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000465 SkShader::GradientInfo info;
466 SkColor color;
467
468 info.fColors = &color;
469 info.fColorOffsets = NULL;
470 info.fColorCount = 1;
471 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
472 SkPaint copy(skPaint);
473 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000474 // modulate the paint alpha by the shader's solid color alpha
475 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
476 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000477 return this->skPaint2GrPaintNoShader(copy,
478 false,
479 grPaint,
480 constantColor);
481 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000482 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000483 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000484 GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx);
485 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000486 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000487 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000488 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000489 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000490 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000491 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
492 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000494 sampler->setRadial2Params(twoPointParams[0],
495 twoPointParams[1],
496 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000497 }
498
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000499 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000500 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000501 SkDebugf("Couldn't convert bitmap to texture.\n");
502 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000503 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000504 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000505
506 // since our texture coords will be in local space, we wack the texture
507 // matrix to map them back into 0...1 before we load it
508 SkMatrix localM;
509 if (shader->getLocalMatrix(&localM)) {
510 SkMatrix inverse;
511 if (localM.invert(&inverse)) {
512 matrix.preConcat(inverse);
513 }
514 }
515 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000516 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
517 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000518 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000519 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000520 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000521 matrix.postScale(s, s);
522 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000523 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000524
525 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000526}
527
528///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000529
bsalomon@google.com398109c2011-04-14 18:40:27 +0000530void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000531 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000532}
533
reed@google.comac10a2d2010-12-22 21:39:39 +0000534void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
535 CHECK_SHOULD_DRAW(draw);
536
bsalomon@google.com5782d712011-01-21 21:03:59 +0000537 GrPaint grPaint;
538 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000539 if (!this->skPaint2GrPaintShader(paint,
540 &act,
541 *draw.fMatrix,
542 &grPaint,
543 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000544 return;
545 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000546
547 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000548}
549
550// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000551static const GrPrimitiveType gPointMode2PrimtiveType[] = {
552 kPoints_PrimitiveType,
553 kLines_PrimitiveType,
554 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000555};
556
557void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000558 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000559 CHECK_SHOULD_DRAW(draw);
560
561 SkScalar width = paint.getStrokeWidth();
562 if (width < 0) {
563 return;
564 }
565
566 // we only handle hairlines here, else we let the SkDraw call our drawPath()
567 if (width > 0) {
568 draw.drawPoints(mode, count, pts, paint, true);
569 return;
570 }
571
bsalomon@google.com5782d712011-01-21 21:03:59 +0000572 GrPaint grPaint;
573 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000574 if (!this->skPaint2GrPaintShader(paint,
575 &act,
576 *draw.fMatrix,
577 &grPaint,
578 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000579 return;
580 }
581
bsalomon@google.com5782d712011-01-21 21:03:59 +0000582 fContext->drawVertices(grPaint,
583 gPointMode2PrimtiveType[mode],
584 count,
585 (GrPoint*)pts,
586 NULL,
587 NULL,
588 NULL,
589 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000590}
591
reed@google.comc9aa5872011-04-05 21:05:37 +0000592///////////////////////////////////////////////////////////////////////////////
593
reed@google.comac10a2d2010-12-22 21:39:39 +0000594void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
595 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000596 CHECK_SHOULD_DRAW(draw);
597
bungeman@google.com79bd8772011-07-18 15:34:08 +0000598 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000599 SkScalar width = paint.getStrokeWidth();
600
601 /*
602 We have special code for hairline strokes, miter-strokes, and fills.
603 Anything else we just call our path code.
604 */
605 bool usePath = doStroke && width > 0 &&
606 paint.getStrokeJoin() != SkPaint::kMiter_Join;
607 // another reason we might need to call drawPath...
608 if (paint.getMaskFilter()) {
609 usePath = true;
610 }
reed@google.com67db6642011-05-26 11:46:35 +0000611 // until we aa rotated rects...
612 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
613 usePath = true;
614 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000615 // small miter limit means right angles show bevel...
616 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
617 paint.getStrokeMiter() < SK_ScalarSqrt2)
618 {
619 usePath = true;
620 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000621 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000622 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
623 usePath = true;
624 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000625
626 if (usePath) {
627 SkPath path;
628 path.addRect(rect);
629 this->drawPath(draw, path, paint, NULL, true);
630 return;
631 }
632
633 GrPaint grPaint;
634 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000635 if (!this->skPaint2GrPaintShader(paint,
636 &act,
637 *draw.fMatrix,
638 &grPaint,
639 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000640 return;
641 }
reed@google.com20efde72011-05-09 17:00:02 +0000642 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000643}
644
reed@google.com69302852011-02-16 18:08:07 +0000645#include "SkMaskFilter.h"
646#include "SkBounder.h"
647
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000648static GrPathFill skToGrFillType(SkPath::FillType fillType) {
649 switch (fillType) {
650 case SkPath::kWinding_FillType:
651 return kWinding_PathFill;
652 case SkPath::kEvenOdd_FillType:
653 return kEvenOdd_PathFill;
654 case SkPath::kInverseWinding_FillType:
655 return kInverseWinding_PathFill;
656 case SkPath::kInverseEvenOdd_FillType:
657 return kInverseEvenOdd_PathFill;
658 default:
659 SkDebugf("Unsupported path fill type\n");
660 return kHairLine_PathFill;
661 }
662}
663
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000664static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000665 int halfWidth = (kernelWidth - 1) / 2;
666 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000667 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000668 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000669 float x = static_cast<float>(i - halfWidth);
670 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
671 // is dropped here, since we renormalize the kernel below.
672 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000673 sum += kernel[i];
674 }
675 // Normalize the kernel
676 float scale = 1.0f / sum;
677 for (int i = 0; i < kernelWidth; ++i)
678 kernel[i] *= scale;
679}
680
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000681static void scaleRect(SkRect* rect, float scale) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000682 rect->fLeft *= scale;
683 rect->fTop *= scale;
684 rect->fRight *= scale;
685 rect->fBottom *= scale;
686}
687
688static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
689 SkMaskFilter* filter, const SkMatrix& matrix,
690 const SkRegion& clip, SkBounder* bounder,
691 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000692#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000693 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000694#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000695 SkMaskFilter::BlurInfo info;
696 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000697 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000698 return false;
699 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000700 SkScalar radius = info.fIgnoreTransform ? info.fRadius
701 : matrix.mapRadius(info.fRadius);
702 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000703 if (radius <= 0) {
704 return false;
705 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000706 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000707 SkRect srcRect = path.getBounds();
708
709 int scaleFactor = 1;
710
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000711 while (sigma > MAX_BLUR_SIGMA) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000712 scaleFactor *= 2;
713 sigma *= 0.5f;
714 }
senorblanco@chromium.org4a947d22011-07-18 21:48:35 +0000715 int halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000716 int kernelWidth = halfWidth * 2 + 1;
717
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000718 float invScale = 1.0f / scaleFactor;
719 scaleRect(&srcRect, invScale);
720 srcRect.roundOut();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000721 srcRect.inset(-halfWidth, -halfWidth);
722
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000723 SkRect clipBounds;
724 clipBounds.set(clip.getBounds());
725 scaleRect(&clipBounds, invScale);
726 clipBounds.roundOut();
727 clipBounds.inset(-halfWidth, -halfWidth);
728
729 srcRect.intersect(clipBounds);
730
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000731 scaleRect(&srcRect, scaleFactor);
732 SkRect finalRect = srcRect;
733
734 SkIRect finalIRect;
735 finalRect.roundOut(&finalIRect);
736 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000737 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000738 }
739 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000740 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000741 }
742 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
743 srcRect.offset(-srcRect.fLeft, -srcRect.fTop);
744 const GrTextureDesc desc = {
745 kRenderTarget_GrTextureFlagBit,
746 kNone_GrAALevel,
747 srcRect.width(),
748 srcRect.height(),
749 // We actually only need A8, but it often isn't supported as a
750 // render target
751 kRGBA_8888_GrPixelConfig
752 };
753
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000754 GrAutoScratchTexture srcEntry(context, desc);
755 GrAutoScratchTexture dstEntry(context, desc);
756 if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000757 return false;
758 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000759 GrTexture* srcTexture = srcEntry.texture();
760 GrTexture* dstTexture = dstEntry.texture();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000761 if (NULL == srcTexture || NULL == dstTexture) {
762 return false;
763 }
764 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000765 // Once this code moves into GrContext, this should be changed to use
766 // an AutoClipRestore.
767 GrClip oldClip = context->getClip();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000768 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000769 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000770 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000771 GrPaint tempPaint;
772 tempPaint.reset();
773
774 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000775 tempPaint.fAntiAlias = grp->fAntiAlias;
776 if (tempPaint.fAntiAlias) {
777 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
778 // blend coeff of zero requires dual source blending support in order
779 // to properly blend partially covered pixels. This means the AA
780 // code path may not be taken. So we use a dst blend coeff of ISA. We
781 // could special case AA draws to a dst surface with known alpha=0 to
782 // use a zero dst coeff when dual source blending isn't available.
783 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
784 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
785 }
786 // Draw hard shadow to dstTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000787 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000788 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000789
790 GrMatrix sampleM;
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000791 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000792 GrPaint paint;
793 paint.reset();
794 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
795 paint.getTextureSampler(0)->setMatrix(sampleM);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000796 GrAutoScratchTexture origEntry;
797
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000798 if (blurType != SkMaskFilter::kNormal_BlurType) {
799 // Stash away a copy of the unblurred image.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000800 origEntry.set(context, desc);
801 if (NULL == origEntry.texture()) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000802 return false;
803 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000804 context->setRenderTarget(origEntry.texture()->asRenderTarget());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000805 paint.setTexture(0, srcTexture);
806 context->drawRect(paint, srcRect);
807 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000808 for (int i = 1; i < scaleFactor; i *= 2) {
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000809 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
810 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000811 context->setRenderTarget(dstTexture->asRenderTarget());
812 SkRect dstRect(srcRect);
813 scaleRect(&dstRect, 0.5f);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000814 paint.setTexture(0, srcTexture);
815 context->drawRectToRect(paint, dstRect, srcRect);
816 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000817 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 }
819
820 SkAutoTMalloc<float> kernelStorage(kernelWidth);
821 float* kernel = kernelStorage.get();
822 buildKernel(sigma, kernel, kernelWidth);
823
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000824 // Clear out a halfWidth to the right of the srcRect to prevent the
825 // X convolution from reading garbage.
826 SkIRect clearRect = SkIRect::MakeXYWH(
827 srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height());
828 context->clear(&clearRect, 0x0);
829
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000830 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000831 context->convolveInX(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000832 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000833
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000834 // Clear out a halfWidth below the srcRect to prevent the Y
835 // convolution from reading garbage.
836 clearRect = SkIRect::MakeXYWH(
837 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth);
838 context->clear(&clearRect, 0x0);
839
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000840 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000841 context->convolveInY(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000842 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000843
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000844 // Clear one pixel to the right and below, to accommodate bilinear
845 // upsampling.
846 clearRect = SkIRect::MakeXYWH(
847 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
848 context->clear(&clearRect, 0x0);
849 clearRect = SkIRect::MakeXYWH(
850 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
851 context->clear(&clearRect, 0x0);
852
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000853 if (scaleFactor > 1) {
854 // FIXME: This should be mitchell, not bilinear.
855 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000856 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000857 paint.getTextureSampler(0)->setMatrix(sampleM);
858 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000859 paint.setTexture(0, srcTexture);
860 SkRect dstRect(srcRect);
861 scaleRect(&dstRect, scaleFactor);
862 context->drawRectToRect(paint, dstRect, srcRect);
863 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000864 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000865 }
866
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000867 if (blurType != SkMaskFilter::kNormal_BlurType) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000868 GrTexture* origTexture = origEntry.texture();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000869 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000870 sampleM.setIDiv(origTexture->width(), origTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000871 paint.getTextureSampler(0)->setMatrix(sampleM);
872 // Blend origTexture over srcTexture.
873 context->setRenderTarget(srcTexture->asRenderTarget());
874 paint.setTexture(0, origTexture);
875 if (SkMaskFilter::kInner_BlurType == blurType) {
876 // inner: dst = dst * src
877 paint.fSrcBlendCoeff = kDC_BlendCoeff;
878 paint.fDstBlendCoeff = kZero_BlendCoeff;
879 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
880 // solid: dst = src + dst - src * dst
881 // = (1 - dst) * src + 1 * dst
882 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
883 paint.fDstBlendCoeff = kOne_BlendCoeff;
884 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
885 // outer: dst = dst * (1 - src)
886 // = 0 * src + (1 - src) * dst
887 paint.fSrcBlendCoeff = kZero_BlendCoeff;
888 paint.fDstBlendCoeff = kISC_BlendCoeff;
889 }
890 context->drawRect(paint, srcRect);
891 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000892 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000893 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000894
895 if (grp->hasTextureOrMask()) {
896 GrMatrix inverse;
897 if (!matrix.invert(&inverse)) {
898 return false;
899 }
900 grp->preConcatActiveSamplerMatrices(inverse);
901 }
902
903 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
904 // we assume the last mask index is available for use
905 GrAssert(NULL == grp->getMask(MASK_IDX));
906 grp->setMask(MASK_IDX, srcTexture);
907 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
908
909 GrMatrix m;
910 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
911 m.postIDiv(srcTexture->width(), srcTexture->height());
912 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
913 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000914 return true;
915}
916
reed@google.com69302852011-02-16 18:08:07 +0000917static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
918 SkMaskFilter* filter, const SkMatrix& matrix,
919 const SkRegion& clip, SkBounder* bounder,
920 GrPaint* grp) {
921 SkMask srcM, dstM;
922
923 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
924 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
925 return false;
926 }
927
928 SkAutoMaskImage autoSrc(&srcM, false);
929
930 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
931 return false;
932 }
933 // this will free-up dstM when we're done (allocated in filterMask())
934 SkAutoMaskImage autoDst(&dstM, false);
935
936 if (clip.quickReject(dstM.fBounds)) {
937 return false;
938 }
939 if (bounder && !bounder->doIRect(dstM.fBounds)) {
940 return false;
941 }
942
943 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
944 // the current clip (and identity matrix) and grpaint settings
945
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000946 // used to compute inverse view, if necessary
947 GrMatrix ivm = context->getMatrix();
948
reed@google.com0c219b62011-02-16 21:31:18 +0000949 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000950
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000951 const GrTextureDesc desc = {
952 kNone_GrTextureFlags,
953 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +0000954 dstM.fBounds.width(),
955 dstM.fBounds.height(),
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000956 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +0000957 };
958
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000959 GrAutoScratchTexture ast(context, desc);
960 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000961
reed@google.com69302852011-02-16 18:08:07 +0000962 if (NULL == texture) {
963 return false;
964 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000965 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
966 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000967
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000968 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
969 grp->preConcatActiveSamplerMatrices(ivm);
970 }
971
972 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
973 // we assume the last mask index is available for use
974 GrAssert(NULL == grp->getMask(MASK_IDX));
975 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000976 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +0000977
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000978 GrRect d;
979 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000980 GrIntToScalar(dstM.fBounds.fTop),
981 GrIntToScalar(dstM.fBounds.fRight),
982 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000983
984 GrMatrix m;
985 m.setTranslate(-dstM.fBounds.fLeft, -dstM.fBounds.fTop);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000986 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000987 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
988
989 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000990 return true;
991}
reed@google.com69302852011-02-16 18:08:07 +0000992
reed@google.com0c219b62011-02-16 21:31:18 +0000993void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comac10a2d2010-12-22 21:39:39 +0000994 const SkPaint& paint, const SkMatrix* prePathMatrix,
995 bool pathIsMutable) {
996 CHECK_SHOULD_DRAW(draw);
997
bsalomon@google.com5782d712011-01-21 21:03:59 +0000998 GrPaint grPaint;
999 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001000 if (!this->skPaint2GrPaintShader(paint,
1001 &act,
1002 *draw.fMatrix,
1003 &grPaint,
1004 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001005 return;
1006 }
1007
reed@google.com0c219b62011-02-16 21:31:18 +00001008 // BEGIN lift from SkDraw::drawPath()
1009
1010 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1011 bool doFill = true;
1012 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001013
1014 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001015 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001016
reed@google.come3445642011-02-16 23:20:39 +00001017 if (!pathIsMutable) {
1018 result = &tmpPath;
1019 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001020 }
reed@google.come3445642011-02-16 23:20:39 +00001021 // should I push prePathMatrix on our MV stack temporarily, instead
1022 // of applying it here? See SkDraw.cpp
1023 pathPtr->transform(*prePathMatrix, result);
1024 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001025 }
reed@google.com0c219b62011-02-16 21:31:18 +00001026 // at this point we're done with prePathMatrix
1027 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001028
bsalomon@google.com04de7822011-03-25 18:04:43 +00001029 // This "if" is not part of the SkDraw::drawPath() lift.
1030 // When we get a 1.0 wide stroke we hairline stroke it instead of creating
1031 // a new stroked-path. This is motivated by canvas2D sites that draw
1032 // lines as 1.0 wide stroked paths. We can consider doing an alpha-modulated-
1033 // hairline for width < 1.0 when AA is enabled.
1034 static const int gMatrixMask = ~(SkMatrix::kIdentity_Mask |
1035 SkMatrix::kTranslate_Mask);
1036 if (!paint.getPathEffect() &&
1037 SkPaint::kStroke_Style == paint.getStyle() &&
1038 !(draw.fMatrix->getType() & gMatrixMask) &&
1039 SK_Scalar1 == paint.getStrokeWidth()) {
1040 doFill = false;
1041 }
1042
1043 if (doFill && (paint.getPathEffect() ||
1044 paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.com0c219b62011-02-16 21:31:18 +00001045 doFill = paint.getFillPath(*pathPtr, &tmpPath);
1046 pathPtr = &tmpPath;
1047 }
1048
1049 // END lift from SkDraw::drawPath()
1050
reed@google.com69302852011-02-16 18:08:07 +00001051 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001052 // avoid possibly allocating a new path in transform if we can
1053 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1054
1055 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001056 pathPtr->transform(*draw.fMatrix, devPathPtr);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001057 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1058 *draw.fMatrix, *draw.fClip, draw.fBounder,
1059 &grPaint)) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001060 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1061 *draw.fMatrix, *draw.fClip, draw.fBounder,
1062 &grPaint);
1063 }
reed@google.com69302852011-02-16 18:08:07 +00001064 return;
1065 }
reed@google.com69302852011-02-16 18:08:07 +00001066
bsalomon@google.comffca4002011-02-22 20:34:01 +00001067 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001068
reed@google.com0c219b62011-02-16 21:31:18 +00001069 if (doFill) {
1070 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001071 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001072 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001073 break;
1074 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001075 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001076 break;
1077 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001078 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001079 break;
1080 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001081 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001082 break;
1083 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001084 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001085 return;
1086 }
1087 }
1088
reed@google.com07f3ee12011-05-16 17:21:57 +00001089 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001090}
1091
reed@google.comac10a2d2010-12-22 21:39:39 +00001092void SkGpuDevice::drawBitmap(const SkDraw& draw,
1093 const SkBitmap& bitmap,
1094 const SkIRect* srcRectPtr,
1095 const SkMatrix& m,
1096 const SkPaint& paint) {
1097 CHECK_SHOULD_DRAW(draw);
1098
1099 SkIRect srcRect;
1100 if (NULL == srcRectPtr) {
1101 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1102 } else {
1103 srcRect = *srcRectPtr;
1104 }
1105
junov@google.comd935cfb2011-06-27 20:48:23 +00001106 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001107 // Convert the bitmap to a shader so that the rect can be drawn
1108 // through drawRect, which supports mask filters.
1109 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001110 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001111 if (srcRectPtr) {
1112 if (!bitmap.extractSubset(&tmp, srcRect)) {
1113 return; // extraction failed
1114 }
1115 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001116 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001117 }
1118 SkPaint paintWithTexture(paint);
1119 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1120 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001121 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001122 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001123
junov@google.com1d329782011-07-28 20:10:09 +00001124 // Transform 'm' needs to be concatenated to the draw matrix,
1125 // rather than transforming the primitive directly, so that 'm' will
1126 // also affect the behavior of the mask filter.
1127 SkMatrix drawMatrix;
1128 drawMatrix.setConcat(*draw.fMatrix, m);
1129 SkDraw transformedDraw(draw);
1130 transformedDraw.fMatrix = &drawMatrix;
1131
1132 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1133
junov@google.comd935cfb2011-06-27 20:48:23 +00001134 return;
1135 }
1136
bsalomon@google.com5782d712011-01-21 21:03:59 +00001137 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001138 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001139 return;
1140 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001141 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001142 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001143 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001144 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001145 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001146 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001147
bsalomon@google.com91958362011-06-13 17:58:13 +00001148 const int maxTextureSize = fContext->getMaxTextureSize();
1149 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1150 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001151 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001152 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001153 return;
1154 }
1155
1156 // undo the translate done by SkCanvas
1157 int DX = SkMax32(0, srcRect.fLeft);
1158 int DY = SkMax32(0, srcRect.fTop);
1159 // compute clip bounds in local coordinates
1160 SkIRect clipRect;
1161 {
1162 SkRect r;
1163 r.set(draw.fClip->getBounds());
1164 SkMatrix matrix, inverse;
1165 matrix.setConcat(*draw.fMatrix, m);
1166 if (!matrix.invert(&inverse)) {
1167 return;
1168 }
1169 inverse.mapRect(&r);
1170 r.roundOut(&clipRect);
1171 // apply the canvas' translate to our local clip
1172 clipRect.offset(DX, DY);
1173 }
1174
bsalomon@google.com91958362011-06-13 17:58:13 +00001175 int nx = bitmap.width() / maxTextureSize;
1176 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001177 for (int x = 0; x <= nx; x++) {
1178 for (int y = 0; y <= ny; y++) {
1179 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001180 tileR.set(x * maxTextureSize, y * maxTextureSize,
1181 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001182 if (!SkIRect::Intersects(tileR, clipRect)) {
1183 continue;
1184 }
1185
1186 SkIRect srcR = tileR;
1187 if (!srcR.intersect(srcRect)) {
1188 continue;
1189 }
1190
1191 SkBitmap tmpB;
1192 if (bitmap.extractSubset(&tmpB, tileR)) {
1193 // now offset it to make it "local" to our tmp bitmap
1194 srcR.offset(-tileR.fLeft, -tileR.fTop);
1195
1196 SkMatrix tmpM(m);
1197 {
1198 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1199 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1200 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1201 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001202 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001203 }
1204 }
1205 }
1206}
1207
1208/*
1209 * This is called by drawBitmap(), which has to handle images that may be too
1210 * large to be represented by a single texture.
1211 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001212 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1213 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001214 */
1215void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1216 const SkBitmap& bitmap,
1217 const SkIRect& srcRect,
1218 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001219 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001220 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1221 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001222
reed@google.com9c49bc32011-07-07 13:42:37 +00001223 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001224 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001225 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001226 return;
1227 }
1228
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001229 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1230
1231 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1232 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1233 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1234 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001235
1236 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001237 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001238 if (NULL == texture) {
1239 return;
1240 }
1241
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001242 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001243
reed@google.com20efde72011-05-09 17:00:02 +00001244 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1245 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001246 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001247 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1248 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1249 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001250 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001251
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001252 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001253 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001254 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001255 // If drawing a subrect of the bitmap and filtering is enabled,
1256 // use a constrained texture domain to avoid color bleeding
1257 GrScalar left, top, right, bottom;
1258 if (srcRect.width() > 1) {
1259 GrScalar border = GR_ScalarHalf / bitmap.width();
1260 left = paintRect.left() + border;
1261 right = paintRect.right() - border;
1262 } else {
1263 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1264 }
1265 if (srcRect.height() > 1) {
1266 GrScalar border = GR_ScalarHalf / bitmap.height();
1267 top = paintRect.top() + border;
1268 bottom = paintRect.bottom() - border;
1269 } else {
1270 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1271 }
1272 GrRect textureDomain;
1273 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001274 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001275 }
1276
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001277 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001278}
1279
1280void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1281 int left, int top, const SkPaint& paint) {
1282 CHECK_SHOULD_DRAW(draw);
1283
1284 SkAutoLockPixels alp(bitmap);
1285 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1286 return;
1287 }
1288
bsalomon@google.com5782d712011-01-21 21:03:59 +00001289 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001290 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001291 return;
1292 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001293
bsalomon@google.com5782d712011-01-21 21:03:59 +00001294 GrAutoMatrix avm(fContext, GrMatrix::I());
1295
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001296 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001297
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001298 GrTexture* texture;
1299 sampler->setClampNoFilter();
1300 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1301
1302 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001303
bsalomon@google.com5782d712011-01-21 21:03:59 +00001304 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001305 GrRect::MakeXYWH(GrIntToScalar(left),
1306 GrIntToScalar(top),
1307 GrIntToScalar(bitmap.width()),
1308 GrIntToScalar(bitmap.height())),
1309 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001310}
1311
1312void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1313 int x, int y, const SkPaint& paint) {
1314 CHECK_SHOULD_DRAW(draw);
1315
bsalomon@google.com5782d712011-01-21 21:03:59 +00001316 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001317 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001318 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001319 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001321
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001322 GrTexture* devTex = grPaint.getTexture(0);
1323 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001324
1325 const SkBitmap& bm = dev->accessBitmap(false);
1326 int w = bm.width();
1327 int h = bm.height();
1328
1329 GrAutoMatrix avm(fContext, GrMatrix::I());
1330
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001331 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001332
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001333 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1334 GrIntToScalar(y),
1335 GrIntToScalar(w),
1336 GrIntToScalar(h));
1337 // The device being drawn may not fill up its texture (saveLayer uses
1338 // the approximate ).
1339 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1340 GR_Scalar1 * h / devTex->height());
1341
1342 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001343}
1344
1345///////////////////////////////////////////////////////////////////////////////
1346
1347// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001348static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1349 kTriangles_PrimitiveType,
1350 kTriangleStrip_PrimitiveType,
1351 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001352};
1353
1354void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1355 int vertexCount, const SkPoint vertices[],
1356 const SkPoint texs[], const SkColor colors[],
1357 SkXfermode* xmode,
1358 const uint16_t indices[], int indexCount,
1359 const SkPaint& paint) {
1360 CHECK_SHOULD_DRAW(draw);
1361
bsalomon@google.com5782d712011-01-21 21:03:59 +00001362 GrPaint grPaint;
1363 SkAutoCachedTexture act;
1364 // we ignore the shader if texs is null.
1365 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001366 if (!this->skPaint2GrPaintNoShader(paint,
1367 false,
1368 &grPaint,
1369 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001370 return;
1371 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001372 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001373 if (!this->skPaint2GrPaintShader(paint, &act,
1374 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001375 &grPaint,
1376 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001377 return;
1378 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001379 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001380
1381 if (NULL != xmode && NULL != texs && NULL != colors) {
1382 SkXfermode::Mode mode;
1383 if (!SkXfermode::IsMode(xmode, &mode) ||
1384 SkXfermode::kMultiply_Mode != mode) {
1385 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1386#if 0
1387 return
1388#endif
1389 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001390 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001391
bsalomon@google.com498776a2011-08-16 19:20:44 +00001392 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1393 if (NULL != colors) {
1394 // need to convert byte order and from non-PM to PM
1395 convertedColors.realloc(vertexCount);
1396 for (int i = 0; i < vertexCount; ++i) {
1397 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1398 }
1399 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001400 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001401 fContext->drawVertices(grPaint,
1402 gVertexMode2PrimitiveType[vmode],
1403 vertexCount,
1404 (GrPoint*) vertices,
1405 (GrPoint*) texs,
1406 colors,
1407 indices,
1408 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001409}
1410
1411///////////////////////////////////////////////////////////////////////////////
1412
1413static void GlyphCacheAuxProc(void* data) {
1414 delete (GrFontScaler*)data;
1415}
1416
1417static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1418 void* auxData;
1419 GrFontScaler* scaler = NULL;
1420 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1421 scaler = (GrFontScaler*)auxData;
1422 }
1423 if (NULL == scaler) {
1424 scaler = new SkGrFontScaler(cache);
1425 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1426 }
1427 return scaler;
1428}
1429
1430static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1431 SkFixed fx, SkFixed fy,
1432 const SkGlyph& glyph) {
1433 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1434
1435 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1436
1437 if (NULL == procs->fFontScaler) {
1438 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1439 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001440
1441 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001442 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001443 *
reed@google.com3b139f52011-06-07 17:56:25 +00001444 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1445 * It calls that rather than round, because our caller has already added
1446 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1447 *
1448 * Test code between raster and gpu (they should draw the same)
1449 *
1450 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1451 *
1452 * Perhaps we should only perform this integralization if there is no
1453 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001454 */
reed@google.com3b139f52011-06-07 17:56:25 +00001455 fy = SkFixedFloorToFixed(fy);
1456
reed@google.comac10a2d2010-12-22 21:39:39 +00001457 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001458 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001459 procs->fFontScaler);
1460}
1461
bsalomon@google.com5782d712011-01-21 21:03:59 +00001462SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001463
1464 // deferred allocation
1465 if (NULL == fDrawProcs) {
1466 fDrawProcs = new GrSkDrawProcs;
1467 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1468 fDrawProcs->fContext = fContext;
1469 }
1470
1471 // init our (and GL's) state
1472 fDrawProcs->fTextContext = context;
1473 fDrawProcs->fFontScaler = NULL;
1474 return fDrawProcs;
1475}
1476
1477void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1478 size_t byteLength, SkScalar x, SkScalar y,
1479 const SkPaint& paint) {
1480 CHECK_SHOULD_DRAW(draw);
1481
1482 if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) {
1483 // this guy will just call our drawPath()
1484 draw.drawText((const char*)text, byteLength, x, y, paint);
1485 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001486 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001487
1488 GrPaint grPaint;
1489 SkAutoCachedTexture act;
1490
Scroggod757df22011-05-16 13:11:16 +00001491 if (!this->skPaint2GrPaintShader(paint,
1492 &act,
1493 *draw.fMatrix,
1494 &grPaint,
1495 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001496 return;
1497 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001498 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001499 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001500 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1501 }
1502}
1503
1504void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1505 size_t byteLength, const SkScalar pos[],
1506 SkScalar constY, int scalarsPerPos,
1507 const SkPaint& paint) {
1508 CHECK_SHOULD_DRAW(draw);
1509
1510 if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) {
1511 // this guy will just call our drawPath()
1512 draw.drawPosText((const char*)text, byteLength, pos, constY,
1513 scalarsPerPos, paint);
1514 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001515 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001516
1517 GrPaint grPaint;
1518 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001519 if (!this->skPaint2GrPaintShader(paint,
1520 &act,
1521 *draw.fMatrix,
1522 &grPaint,
1523 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001524 return;
1525 }
1526
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001527 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001528 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001529 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1530 scalarsPerPos, paint);
1531 }
1532}
1533
1534void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1535 size_t len, const SkPath& path,
1536 const SkMatrix* m, const SkPaint& paint) {
1537 CHECK_SHOULD_DRAW(draw);
1538
1539 SkASSERT(draw.fDevice == this);
1540 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1541}
1542
1543///////////////////////////////////////////////////////////////////////////////
1544
reed@google.comf67e4cf2011-03-15 20:56:58 +00001545bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1546 if (!paint.isLCDRenderText()) {
1547 // we're cool with the paint as is
1548 return false;
1549 }
1550
1551 if (paint.getShader() ||
1552 paint.getXfermode() || // unless its srcover
1553 paint.getMaskFilter() ||
1554 paint.getRasterizer() ||
1555 paint.getColorFilter() ||
1556 paint.getPathEffect() ||
1557 paint.isFakeBoldText() ||
1558 paint.getStyle() != SkPaint::kFill_Style) {
1559 // turn off lcd
1560 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1561 flags->fHinting = paint.getHinting();
1562 return true;
1563 }
1564 // we're cool with the paint as is
1565 return false;
1566}
1567
1568///////////////////////////////////////////////////////////////////////////////
1569
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001570SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1571 const GrSamplerState& sampler,
1572 TexType type) {
1573 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001574 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001575
bsalomon@google.come97f0852011-06-17 13:10:25 +00001576 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001577 const GrTextureDesc desc = {
1578 kRenderTarget_GrTextureFlagBit,
1579 kNone_GrAALevel,
1580 bitmap.width(),
1581 bitmap.height(),
1582 SkGr::Bitmap2PixelConfig(bitmap)
1583 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001584 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001585 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001586 // we know layers will only be drawn through drawDevice.
1587 // drawDevice has been made to work with content embedded in a
1588 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001589 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001590 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001591 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001592 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001593 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001594 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001595 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001596 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001597 GrContext::TextureKey key = bitmap.getGenerationID();
1598 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001599
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001600 entry = ctx->findAndLockTexture(key, bitmap.width(),
1601 bitmap.height(), sampler);
1602 if (NULL == entry.texture()) {
1603 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001604 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001605 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001606 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001607 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001608 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001609 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001610 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1611 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001612 }
1613 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001614 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001615}
1616
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001617void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1618 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001619}
1620
bsalomon@google.come97f0852011-06-17 13:10:25 +00001621SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1622 int width, int height,
1623 bool isOpaque,
1624 Usage usage) {
1625 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1626 width, height, usage));
1627}
1628