blob: 32ac5af39c1ed8bcf0b82433f00aa27306240f1c [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
tomhudson@google.com66d57252011-09-12 19:52:44 +0000341void SkGpuDevice::markMatrixDirty() {
342 INHERITED::markMatrixDirty();
343 fNeedPrepareRenderTarget = true;
344}
345
346void SkGpuDevice::markClipDirty() {
347 INHERITED::markClipDirty();
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000348 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000349}
350
351void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000352 const SkRegion& clip, const SkClipStack& clipStack) {
353
reed@google.comac10a2d2010-12-22 21:39:39 +0000354 fContext->setRenderTarget(fRenderTarget);
355
bsalomon@google.comd302f142011-03-03 13:54:13 +0000356 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000357
reed@google.com6f8f2922011-03-04 22:27:10 +0000358 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000359
360 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000361 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000362 fNeedClear = false;
363 }
364}
365
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000366bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000367 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000368 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000369 return true;
370 }
371 return false;
372}
373
374///////////////////////////////////////////////////////////////////////////////
375
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000376SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
377SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
378SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
379SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
380SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
381 shader_type_mismatch);
382SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000383
bsalomon@google.com5782d712011-01-21 21:03:59 +0000384static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
385 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
386 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
387 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
388 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
389 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
390};
391
392bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
393 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000394 GrPaint* grPaint,
395 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000396
397 grPaint->fDither = skPaint.isDither();
398 grPaint->fAntiAlias = skPaint.isAntiAlias();
399
400 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
401 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
402
403 SkXfermode* mode = skPaint.getXfermode();
404 if (mode) {
405 if (!mode->asCoeff(&sm, &dm)) {
reed@google.com1a2e8d22011-01-21 22:08:29 +0000406 SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000407#if 0
408 return false;
409#endif
410 }
411 }
412 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
413 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
414
415 if (justAlpha) {
416 uint8_t alpha = skPaint.getAlpha();
417 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000418 // justAlpha is currently set to true only if there is a texture,
419 // so constantColor should not also be true.
420 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000421 } else {
422 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000423 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000424 }
Scroggo97c88c22011-05-11 14:05:25 +0000425 SkColorFilter* colorFilter = skPaint.getColorFilter();
426 SkColor color;
427 SkXfermode::Mode filterMode;
428 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000429 if (!constantColor) {
430 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
431 grPaint->fColorFilterXfermode = filterMode;
432 return true;
433 }
434 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
435 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000436 }
Scroggod757df22011-05-16 13:11:16 +0000437 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000438 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000439}
440
bsalomon@google.com5782d712011-01-21 21:03:59 +0000441bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
442 SkAutoCachedTexture* act,
443 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000444 GrPaint* grPaint,
445 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000446
bsalomon@google.com5782d712011-01-21 21:03:59 +0000447 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000448
bsalomon@google.com5782d712011-01-21 21:03:59 +0000449 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000450 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000451 return this->skPaint2GrPaintNoShader(skPaint,
452 false,
453 grPaint,
454 constantColor);
455 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000456 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000457 }
458
reed@google.comac10a2d2010-12-22 21:39:39 +0000459 SkBitmap bitmap;
460 SkMatrix matrix;
461 SkShader::TileMode tileModes[2];
462 SkScalar twoPointParams[3];
463 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
464 tileModes, twoPointParams);
465
bsalomon@google.com5782d712011-01-21 21:03:59 +0000466 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
467 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000468 SkShader::GradientInfo info;
469 SkColor color;
470
471 info.fColors = &color;
472 info.fColorOffsets = NULL;
473 info.fColorCount = 1;
474 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
475 SkPaint copy(skPaint);
476 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000477 // modulate the paint alpha by the shader's solid color alpha
478 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
479 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000480 return this->skPaint2GrPaintNoShader(copy,
481 false,
482 grPaint,
483 constantColor);
484 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000485 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000486 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000487 GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx);
488 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000489 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000490 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000491 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000492 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000493 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000494 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
495 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000496 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000497 sampler->setRadial2Params(twoPointParams[0],
498 twoPointParams[1],
499 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000500 }
501
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000502 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000503 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000504 SkDebugf("Couldn't convert bitmap to texture.\n");
505 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000506 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000507 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000508
509 // since our texture coords will be in local space, we wack the texture
510 // matrix to map them back into 0...1 before we load it
511 SkMatrix localM;
512 if (shader->getLocalMatrix(&localM)) {
513 SkMatrix inverse;
514 if (localM.invert(&inverse)) {
515 matrix.preConcat(inverse);
516 }
517 }
518 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000519 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
520 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000521 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000522 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000523 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000524 matrix.postScale(s, s);
525 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000526 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000527
528 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000529}
530
531///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000532
bsalomon@google.com398109c2011-04-14 18:40:27 +0000533void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000534 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000535}
536
reed@google.comac10a2d2010-12-22 21:39:39 +0000537void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
538 CHECK_SHOULD_DRAW(draw);
539
bsalomon@google.com5782d712011-01-21 21:03:59 +0000540 GrPaint grPaint;
541 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000542 if (!this->skPaint2GrPaintShader(paint,
543 &act,
544 *draw.fMatrix,
545 &grPaint,
546 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000547 return;
548 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000549
550 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000551}
552
553// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000554static const GrPrimitiveType gPointMode2PrimtiveType[] = {
555 kPoints_PrimitiveType,
556 kLines_PrimitiveType,
557 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000558};
559
560void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000561 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000562 CHECK_SHOULD_DRAW(draw);
563
564 SkScalar width = paint.getStrokeWidth();
565 if (width < 0) {
566 return;
567 }
568
569 // we only handle hairlines here, else we let the SkDraw call our drawPath()
570 if (width > 0) {
571 draw.drawPoints(mode, count, pts, paint, true);
572 return;
573 }
574
bsalomon@google.com5782d712011-01-21 21:03:59 +0000575 GrPaint grPaint;
576 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000577 if (!this->skPaint2GrPaintShader(paint,
578 &act,
579 *draw.fMatrix,
580 &grPaint,
581 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000582 return;
583 }
584
bsalomon@google.com5782d712011-01-21 21:03:59 +0000585 fContext->drawVertices(grPaint,
586 gPointMode2PrimtiveType[mode],
587 count,
588 (GrPoint*)pts,
589 NULL,
590 NULL,
591 NULL,
592 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000593}
594
reed@google.comc9aa5872011-04-05 21:05:37 +0000595///////////////////////////////////////////////////////////////////////////////
596
reed@google.comac10a2d2010-12-22 21:39:39 +0000597void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
598 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000599 CHECK_SHOULD_DRAW(draw);
600
bungeman@google.com79bd8772011-07-18 15:34:08 +0000601 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000602 SkScalar width = paint.getStrokeWidth();
603
604 /*
605 We have special code for hairline strokes, miter-strokes, and fills.
606 Anything else we just call our path code.
607 */
608 bool usePath = doStroke && width > 0 &&
609 paint.getStrokeJoin() != SkPaint::kMiter_Join;
610 // another reason we might need to call drawPath...
611 if (paint.getMaskFilter()) {
612 usePath = true;
613 }
reed@google.com67db6642011-05-26 11:46:35 +0000614 // until we aa rotated rects...
615 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
616 usePath = true;
617 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000618 // small miter limit means right angles show bevel...
619 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
620 paint.getStrokeMiter() < SK_ScalarSqrt2)
621 {
622 usePath = true;
623 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000624 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000625 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
626 usePath = true;
627 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000628
629 if (usePath) {
630 SkPath path;
631 path.addRect(rect);
632 this->drawPath(draw, path, paint, NULL, true);
633 return;
634 }
635
636 GrPaint grPaint;
637 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000638 if (!this->skPaint2GrPaintShader(paint,
639 &act,
640 *draw.fMatrix,
641 &grPaint,
642 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000643 return;
644 }
reed@google.com20efde72011-05-09 17:00:02 +0000645 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000646}
647
reed@google.com69302852011-02-16 18:08:07 +0000648#include "SkMaskFilter.h"
649#include "SkBounder.h"
650
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000651static GrPathFill skToGrFillType(SkPath::FillType fillType) {
652 switch (fillType) {
653 case SkPath::kWinding_FillType:
654 return kWinding_PathFill;
655 case SkPath::kEvenOdd_FillType:
656 return kEvenOdd_PathFill;
657 case SkPath::kInverseWinding_FillType:
658 return kInverseWinding_PathFill;
659 case SkPath::kInverseEvenOdd_FillType:
660 return kInverseEvenOdd_PathFill;
661 default:
662 SkDebugf("Unsupported path fill type\n");
663 return kHairLine_PathFill;
664 }
665}
666
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000667static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000668 int halfWidth = (kernelWidth - 1) / 2;
669 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000670 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000671 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000672 float x = static_cast<float>(i - halfWidth);
673 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
674 // is dropped here, since we renormalize the kernel below.
675 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000676 sum += kernel[i];
677 }
678 // Normalize the kernel
679 float scale = 1.0f / sum;
680 for (int i = 0; i < kernelWidth; ++i)
681 kernel[i] *= scale;
682}
683
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000684static void scaleRect(SkRect* rect, float scale) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000685 rect->fLeft *= scale;
686 rect->fTop *= scale;
687 rect->fRight *= scale;
688 rect->fBottom *= scale;
689}
690
691static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
692 SkMaskFilter* filter, const SkMatrix& matrix,
693 const SkRegion& clip, SkBounder* bounder,
694 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000695#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000696 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000697#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000698 SkMaskFilter::BlurInfo info;
699 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000700 if (SkMaskFilter::kNone_BlurType == blurType ||
701 !context->supportsShaders()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000702 return false;
703 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000704 SkScalar radius = info.fIgnoreTransform ? info.fRadius
705 : matrix.mapRadius(info.fRadius);
706 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000707 if (radius <= 0) {
708 return false;
709 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000710 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000711 SkRect srcRect = path.getBounds();
712
713 int scaleFactor = 1;
714
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000715 while (sigma > MAX_BLUR_SIGMA) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000716 scaleFactor *= 2;
717 sigma *= 0.5f;
718 }
senorblanco@chromium.org4a947d22011-07-18 21:48:35 +0000719 int halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000720 int kernelWidth = halfWidth * 2 + 1;
721
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000722 float invScale = 1.0f / scaleFactor;
723 scaleRect(&srcRect, invScale);
724 srcRect.roundOut();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000725 srcRect.inset(-halfWidth, -halfWidth);
726
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000727 SkRect clipBounds;
728 clipBounds.set(clip.getBounds());
729 scaleRect(&clipBounds, invScale);
730 clipBounds.roundOut();
731 clipBounds.inset(-halfWidth, -halfWidth);
732
733 srcRect.intersect(clipBounds);
734
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000735 scaleRect(&srcRect, scaleFactor);
736 SkRect finalRect = srcRect;
737
738 SkIRect finalIRect;
739 finalRect.roundOut(&finalIRect);
740 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000741 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000742 }
743 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000744 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000745 }
746 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
747 srcRect.offset(-srcRect.fLeft, -srcRect.fTop);
748 const GrTextureDesc desc = {
749 kRenderTarget_GrTextureFlagBit,
750 kNone_GrAALevel,
751 srcRect.width(),
752 srcRect.height(),
753 // We actually only need A8, but it often isn't supported as a
754 // render target
755 kRGBA_8888_GrPixelConfig
756 };
757
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000758 GrAutoScratchTexture srcEntry(context, desc);
759 GrAutoScratchTexture dstEntry(context, desc);
760 if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000761 return false;
762 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000763 GrTexture* srcTexture = srcEntry.texture();
764 GrTexture* dstTexture = dstEntry.texture();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000765 if (NULL == srcTexture || NULL == dstTexture) {
766 return false;
767 }
768 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000769 // Once this code moves into GrContext, this should be changed to use
770 // an AutoClipRestore.
771 GrClip oldClip = context->getClip();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000772 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000773 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000774 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000775 GrPaint tempPaint;
776 tempPaint.reset();
777
778 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000779 tempPaint.fAntiAlias = grp->fAntiAlias;
780 if (tempPaint.fAntiAlias) {
781 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
782 // blend coeff of zero requires dual source blending support in order
783 // to properly blend partially covered pixels. This means the AA
784 // code path may not be taken. So we use a dst blend coeff of ISA. We
785 // could special case AA draws to a dst surface with known alpha=0 to
786 // use a zero dst coeff when dual source blending isn't available.
787 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
788 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
789 }
790 // Draw hard shadow to dstTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000791 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000792 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000793
794 GrMatrix sampleM;
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000795 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000796 GrPaint paint;
797 paint.reset();
798 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
799 paint.getTextureSampler(0)->setMatrix(sampleM);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000800 GrAutoScratchTexture origEntry;
801
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000802 if (blurType != SkMaskFilter::kNormal_BlurType) {
803 // Stash away a copy of the unblurred image.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000804 origEntry.set(context, desc);
805 if (NULL == origEntry.texture()) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000806 return false;
807 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000808 context->setRenderTarget(origEntry.texture()->asRenderTarget());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000809 paint.setTexture(0, srcTexture);
810 context->drawRect(paint, srcRect);
811 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000812 for (int i = 1; i < scaleFactor; i *= 2) {
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000813 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
814 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000815 context->setRenderTarget(dstTexture->asRenderTarget());
816 SkRect dstRect(srcRect);
817 scaleRect(&dstRect, 0.5f);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 paint.setTexture(0, srcTexture);
819 context->drawRectToRect(paint, dstRect, srcRect);
820 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000821 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000822 }
823
824 SkAutoTMalloc<float> kernelStorage(kernelWidth);
825 float* kernel = kernelStorage.get();
826 buildKernel(sigma, kernel, kernelWidth);
827
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000828 // Clear out a halfWidth to the right of the srcRect to prevent the
829 // X convolution from reading garbage.
830 SkIRect clearRect = SkIRect::MakeXYWH(
831 srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height());
832 context->clear(&clearRect, 0x0);
833
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000834 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000835 context->convolveInX(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000836 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000837
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000838 // Clear out a halfWidth below the srcRect to prevent the Y
839 // convolution from reading garbage.
840 clearRect = SkIRect::MakeXYWH(
841 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth);
842 context->clear(&clearRect, 0x0);
843
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000844 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000845 context->convolveInY(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000846 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000847
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000848 // Clear one pixel to the right and below, to accommodate bilinear
849 // upsampling.
850 clearRect = SkIRect::MakeXYWH(
851 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
852 context->clear(&clearRect, 0x0);
853 clearRect = SkIRect::MakeXYWH(
854 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
855 context->clear(&clearRect, 0x0);
856
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000857 if (scaleFactor > 1) {
858 // FIXME: This should be mitchell, not bilinear.
859 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000860 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000861 paint.getTextureSampler(0)->setMatrix(sampleM);
862 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000863 paint.setTexture(0, srcTexture);
864 SkRect dstRect(srcRect);
865 scaleRect(&dstRect, scaleFactor);
866 context->drawRectToRect(paint, dstRect, srcRect);
867 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000868 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000869 }
870
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000871 if (blurType != SkMaskFilter::kNormal_BlurType) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000872 GrTexture* origTexture = origEntry.texture();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000873 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000874 sampleM.setIDiv(origTexture->width(), origTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000875 paint.getTextureSampler(0)->setMatrix(sampleM);
876 // Blend origTexture over srcTexture.
877 context->setRenderTarget(srcTexture->asRenderTarget());
878 paint.setTexture(0, origTexture);
879 if (SkMaskFilter::kInner_BlurType == blurType) {
880 // inner: dst = dst * src
881 paint.fSrcBlendCoeff = kDC_BlendCoeff;
882 paint.fDstBlendCoeff = kZero_BlendCoeff;
883 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
884 // solid: dst = src + dst - src * dst
885 // = (1 - dst) * src + 1 * dst
886 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
887 paint.fDstBlendCoeff = kOne_BlendCoeff;
888 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
889 // outer: dst = dst * (1 - src)
890 // = 0 * src + (1 - src) * dst
891 paint.fSrcBlendCoeff = kZero_BlendCoeff;
892 paint.fDstBlendCoeff = kISC_BlendCoeff;
893 }
894 context->drawRect(paint, srcRect);
895 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000896 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000897 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000898
899 if (grp->hasTextureOrMask()) {
900 GrMatrix inverse;
901 if (!matrix.invert(&inverse)) {
902 return false;
903 }
904 grp->preConcatActiveSamplerMatrices(inverse);
905 }
906
907 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
908 // we assume the last mask index is available for use
909 GrAssert(NULL == grp->getMask(MASK_IDX));
910 grp->setMask(MASK_IDX, srcTexture);
911 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
912
913 GrMatrix m;
914 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
915 m.postIDiv(srcTexture->width(), srcTexture->height());
916 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
917 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000918 return true;
919}
920
reed@google.com69302852011-02-16 18:08:07 +0000921static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
922 SkMaskFilter* filter, const SkMatrix& matrix,
923 const SkRegion& clip, SkBounder* bounder,
924 GrPaint* grp) {
925 SkMask srcM, dstM;
926
927 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
928 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
929 return false;
930 }
931
932 SkAutoMaskImage autoSrc(&srcM, false);
933
934 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
935 return false;
936 }
937 // this will free-up dstM when we're done (allocated in filterMask())
938 SkAutoMaskImage autoDst(&dstM, false);
939
940 if (clip.quickReject(dstM.fBounds)) {
941 return false;
942 }
943 if (bounder && !bounder->doIRect(dstM.fBounds)) {
944 return false;
945 }
946
947 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
948 // the current clip (and identity matrix) and grpaint settings
949
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000950 // used to compute inverse view, if necessary
951 GrMatrix ivm = context->getMatrix();
952
reed@google.com0c219b62011-02-16 21:31:18 +0000953 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000954
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000955 const GrTextureDesc desc = {
956 kNone_GrTextureFlags,
957 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +0000958 dstM.fBounds.width(),
959 dstM.fBounds.height(),
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000960 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +0000961 };
962
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000963 GrAutoScratchTexture ast(context, desc);
964 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000965
reed@google.com69302852011-02-16 18:08:07 +0000966 if (NULL == texture) {
967 return false;
968 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000969 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
970 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000971
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000972 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
973 grp->preConcatActiveSamplerMatrices(ivm);
974 }
975
976 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
977 // we assume the last mask index is available for use
978 GrAssert(NULL == grp->getMask(MASK_IDX));
979 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000980 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +0000981
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000982 GrRect d;
983 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000984 GrIntToScalar(dstM.fBounds.fTop),
985 GrIntToScalar(dstM.fBounds.fRight),
986 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000987
988 GrMatrix m;
989 m.setTranslate(-dstM.fBounds.fLeft, -dstM.fBounds.fTop);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000990 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000991 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
992
993 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000994 return true;
995}
reed@google.com69302852011-02-16 18:08:07 +0000996
reed@google.com0c219b62011-02-16 21:31:18 +0000997void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comac10a2d2010-12-22 21:39:39 +0000998 const SkPaint& paint, const SkMatrix* prePathMatrix,
999 bool pathIsMutable) {
1000 CHECK_SHOULD_DRAW(draw);
1001
bsalomon@google.com5782d712011-01-21 21:03:59 +00001002 GrPaint grPaint;
1003 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001004 if (!this->skPaint2GrPaintShader(paint,
1005 &act,
1006 *draw.fMatrix,
1007 &grPaint,
1008 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001009 return;
1010 }
1011
reed@google.com0c219b62011-02-16 21:31:18 +00001012 // BEGIN lift from SkDraw::drawPath()
1013
1014 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1015 bool doFill = true;
1016 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001017
1018 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001019 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001020
reed@google.come3445642011-02-16 23:20:39 +00001021 if (!pathIsMutable) {
1022 result = &tmpPath;
1023 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001024 }
reed@google.come3445642011-02-16 23:20:39 +00001025 // should I push prePathMatrix on our MV stack temporarily, instead
1026 // of applying it here? See SkDraw.cpp
1027 pathPtr->transform(*prePathMatrix, result);
1028 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001029 }
reed@google.com0c219b62011-02-16 21:31:18 +00001030 // at this point we're done with prePathMatrix
1031 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001032
bsalomon@google.com04de7822011-03-25 18:04:43 +00001033 // This "if" is not part of the SkDraw::drawPath() lift.
1034 // When we get a 1.0 wide stroke we hairline stroke it instead of creating
1035 // a new stroked-path. This is motivated by canvas2D sites that draw
1036 // lines as 1.0 wide stroked paths. We can consider doing an alpha-modulated-
1037 // hairline for width < 1.0 when AA is enabled.
1038 static const int gMatrixMask = ~(SkMatrix::kIdentity_Mask |
1039 SkMatrix::kTranslate_Mask);
1040 if (!paint.getPathEffect() &&
1041 SkPaint::kStroke_Style == paint.getStyle() &&
1042 !(draw.fMatrix->getType() & gMatrixMask) &&
1043 SK_Scalar1 == paint.getStrokeWidth()) {
1044 doFill = false;
1045 }
1046
1047 if (doFill && (paint.getPathEffect() ||
1048 paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.com0c219b62011-02-16 21:31:18 +00001049 doFill = paint.getFillPath(*pathPtr, &tmpPath);
1050 pathPtr = &tmpPath;
1051 }
1052
1053 // END lift from SkDraw::drawPath()
1054
reed@google.com69302852011-02-16 18:08:07 +00001055 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001056 // avoid possibly allocating a new path in transform if we can
1057 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1058
1059 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001060 pathPtr->transform(*draw.fMatrix, devPathPtr);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001061 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1062 *draw.fMatrix, *draw.fClip, draw.fBounder,
1063 &grPaint)) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001064 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1065 *draw.fMatrix, *draw.fClip, draw.fBounder,
1066 &grPaint);
1067 }
reed@google.com69302852011-02-16 18:08:07 +00001068 return;
1069 }
reed@google.com69302852011-02-16 18:08:07 +00001070
bsalomon@google.comffca4002011-02-22 20:34:01 +00001071 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001072
reed@google.com0c219b62011-02-16 21:31:18 +00001073 if (doFill) {
1074 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001075 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001076 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001077 break;
1078 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001079 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001080 break;
1081 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001082 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001083 break;
1084 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001085 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001086 break;
1087 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001088 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001089 return;
1090 }
1091 }
1092
reed@google.com07f3ee12011-05-16 17:21:57 +00001093 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001094}
1095
reed@google.comac10a2d2010-12-22 21:39:39 +00001096void SkGpuDevice::drawBitmap(const SkDraw& draw,
1097 const SkBitmap& bitmap,
1098 const SkIRect* srcRectPtr,
1099 const SkMatrix& m,
1100 const SkPaint& paint) {
1101 CHECK_SHOULD_DRAW(draw);
1102
1103 SkIRect srcRect;
1104 if (NULL == srcRectPtr) {
1105 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1106 } else {
1107 srcRect = *srcRectPtr;
1108 }
1109
junov@google.comd935cfb2011-06-27 20:48:23 +00001110 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001111 // Convert the bitmap to a shader so that the rect can be drawn
1112 // through drawRect, which supports mask filters.
1113 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001114 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001115 if (srcRectPtr) {
1116 if (!bitmap.extractSubset(&tmp, srcRect)) {
1117 return; // extraction failed
1118 }
1119 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001120 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001121 }
1122 SkPaint paintWithTexture(paint);
1123 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1124 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001125 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001126 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001127
junov@google.com1d329782011-07-28 20:10:09 +00001128 // Transform 'm' needs to be concatenated to the draw matrix,
1129 // rather than transforming the primitive directly, so that 'm' will
1130 // also affect the behavior of the mask filter.
1131 SkMatrix drawMatrix;
1132 drawMatrix.setConcat(*draw.fMatrix, m);
1133 SkDraw transformedDraw(draw);
1134 transformedDraw.fMatrix = &drawMatrix;
1135
1136 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1137
junov@google.comd935cfb2011-06-27 20:48:23 +00001138 return;
1139 }
1140
bsalomon@google.com5782d712011-01-21 21:03:59 +00001141 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001142 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001143 return;
1144 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001145 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001146 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001147 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001148 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001149 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001150 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001151
bsalomon@google.com91958362011-06-13 17:58:13 +00001152 const int maxTextureSize = fContext->getMaxTextureSize();
1153 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1154 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001155 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001156 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001157 return;
1158 }
1159
1160 // undo the translate done by SkCanvas
1161 int DX = SkMax32(0, srcRect.fLeft);
1162 int DY = SkMax32(0, srcRect.fTop);
1163 // compute clip bounds in local coordinates
1164 SkIRect clipRect;
1165 {
1166 SkRect r;
1167 r.set(draw.fClip->getBounds());
1168 SkMatrix matrix, inverse;
1169 matrix.setConcat(*draw.fMatrix, m);
1170 if (!matrix.invert(&inverse)) {
1171 return;
1172 }
1173 inverse.mapRect(&r);
1174 r.roundOut(&clipRect);
1175 // apply the canvas' translate to our local clip
1176 clipRect.offset(DX, DY);
1177 }
1178
bsalomon@google.com91958362011-06-13 17:58:13 +00001179 int nx = bitmap.width() / maxTextureSize;
1180 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001181 for (int x = 0; x <= nx; x++) {
1182 for (int y = 0; y <= ny; y++) {
1183 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001184 tileR.set(x * maxTextureSize, y * maxTextureSize,
1185 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001186 if (!SkIRect::Intersects(tileR, clipRect)) {
1187 continue;
1188 }
1189
1190 SkIRect srcR = tileR;
1191 if (!srcR.intersect(srcRect)) {
1192 continue;
1193 }
1194
1195 SkBitmap tmpB;
1196 if (bitmap.extractSubset(&tmpB, tileR)) {
1197 // now offset it to make it "local" to our tmp bitmap
1198 srcR.offset(-tileR.fLeft, -tileR.fTop);
1199
1200 SkMatrix tmpM(m);
1201 {
1202 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1203 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1204 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1205 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001206 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001207 }
1208 }
1209 }
1210}
1211
1212/*
1213 * This is called by drawBitmap(), which has to handle images that may be too
1214 * large to be represented by a single texture.
1215 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001216 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1217 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001218 */
1219void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1220 const SkBitmap& bitmap,
1221 const SkIRect& srcRect,
1222 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001223 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001224 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1225 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001226
reed@google.com9c49bc32011-07-07 13:42:37 +00001227 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001228 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001229 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001230 return;
1231 }
1232
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001233 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1234
1235 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1236 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1237 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1238 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001239
1240 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001241 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001242 if (NULL == texture) {
1243 return;
1244 }
1245
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001246 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001247
reed@google.com20efde72011-05-09 17:00:02 +00001248 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1249 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001250 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001251 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1252 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1253 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001254 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001255
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001256 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001257 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001258 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001259 // If drawing a subrect of the bitmap and filtering is enabled,
1260 // use a constrained texture domain to avoid color bleeding
1261 GrScalar left, top, right, bottom;
1262 if (srcRect.width() > 1) {
1263 GrScalar border = GR_ScalarHalf / bitmap.width();
1264 left = paintRect.left() + border;
1265 right = paintRect.right() - border;
1266 } else {
1267 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1268 }
1269 if (srcRect.height() > 1) {
1270 GrScalar border = GR_ScalarHalf / bitmap.height();
1271 top = paintRect.top() + border;
1272 bottom = paintRect.bottom() - border;
1273 } else {
1274 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1275 }
1276 GrRect textureDomain;
1277 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001278 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001279 }
1280
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001281 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001282}
1283
1284void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1285 int left, int top, const SkPaint& paint) {
1286 CHECK_SHOULD_DRAW(draw);
1287
1288 SkAutoLockPixels alp(bitmap);
1289 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1290 return;
1291 }
1292
bsalomon@google.com5782d712011-01-21 21:03:59 +00001293 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001294 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001295 return;
1296 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001297
bsalomon@google.com5782d712011-01-21 21:03:59 +00001298 GrAutoMatrix avm(fContext, GrMatrix::I());
1299
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001300 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001301
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001302 GrTexture* texture;
1303 sampler->setClampNoFilter();
1304 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1305
1306 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001307
bsalomon@google.com5782d712011-01-21 21:03:59 +00001308 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001309 GrRect::MakeXYWH(GrIntToScalar(left),
1310 GrIntToScalar(top),
1311 GrIntToScalar(bitmap.width()),
1312 GrIntToScalar(bitmap.height())),
1313 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001314}
1315
1316void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1317 int x, int y, const SkPaint& paint) {
1318 CHECK_SHOULD_DRAW(draw);
1319
bsalomon@google.com5782d712011-01-21 21:03:59 +00001320 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001321 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001322 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001323 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001324 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001325
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001326 GrTexture* devTex = grPaint.getTexture(0);
1327 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001328
1329 const SkBitmap& bm = dev->accessBitmap(false);
1330 int w = bm.width();
1331 int h = bm.height();
1332
1333 GrAutoMatrix avm(fContext, GrMatrix::I());
1334
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001335 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001336
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001337 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1338 GrIntToScalar(y),
1339 GrIntToScalar(w),
1340 GrIntToScalar(h));
1341 // The device being drawn may not fill up its texture (saveLayer uses
1342 // the approximate ).
1343 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1344 GR_Scalar1 * h / devTex->height());
1345
1346 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001347}
1348
1349///////////////////////////////////////////////////////////////////////////////
1350
1351// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001352static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1353 kTriangles_PrimitiveType,
1354 kTriangleStrip_PrimitiveType,
1355 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001356};
1357
1358void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1359 int vertexCount, const SkPoint vertices[],
1360 const SkPoint texs[], const SkColor colors[],
1361 SkXfermode* xmode,
1362 const uint16_t indices[], int indexCount,
1363 const SkPaint& paint) {
1364 CHECK_SHOULD_DRAW(draw);
1365
bsalomon@google.com5782d712011-01-21 21:03:59 +00001366 GrPaint grPaint;
1367 SkAutoCachedTexture act;
1368 // we ignore the shader if texs is null.
1369 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001370 if (!this->skPaint2GrPaintNoShader(paint,
1371 false,
1372 &grPaint,
1373 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001374 return;
1375 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001376 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001377 if (!this->skPaint2GrPaintShader(paint, &act,
1378 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001379 &grPaint,
1380 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001381 return;
1382 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001383 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001384
1385 if (NULL != xmode && NULL != texs && NULL != colors) {
1386 SkXfermode::Mode mode;
1387 if (!SkXfermode::IsMode(xmode, &mode) ||
1388 SkXfermode::kMultiply_Mode != mode) {
1389 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1390#if 0
1391 return
1392#endif
1393 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001394 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001395
bsalomon@google.com498776a2011-08-16 19:20:44 +00001396 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1397 if (NULL != colors) {
1398 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001399 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001400 for (int i = 0; i < vertexCount; ++i) {
1401 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1402 }
1403 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001404 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001405 fContext->drawVertices(grPaint,
1406 gVertexMode2PrimitiveType[vmode],
1407 vertexCount,
1408 (GrPoint*) vertices,
1409 (GrPoint*) texs,
1410 colors,
1411 indices,
1412 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001413}
1414
1415///////////////////////////////////////////////////////////////////////////////
1416
1417static void GlyphCacheAuxProc(void* data) {
1418 delete (GrFontScaler*)data;
1419}
1420
1421static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1422 void* auxData;
1423 GrFontScaler* scaler = NULL;
1424 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1425 scaler = (GrFontScaler*)auxData;
1426 }
1427 if (NULL == scaler) {
1428 scaler = new SkGrFontScaler(cache);
1429 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1430 }
1431 return scaler;
1432}
1433
1434static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1435 SkFixed fx, SkFixed fy,
1436 const SkGlyph& glyph) {
1437 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1438
1439 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1440
1441 if (NULL == procs->fFontScaler) {
1442 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1443 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001444
1445 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001446 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001447 *
reed@google.com3b139f52011-06-07 17:56:25 +00001448 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1449 * It calls that rather than round, because our caller has already added
1450 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1451 *
1452 * Test code between raster and gpu (they should draw the same)
1453 *
1454 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1455 *
1456 * Perhaps we should only perform this integralization if there is no
1457 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001458 */
reed@google.com3b139f52011-06-07 17:56:25 +00001459 fy = SkFixedFloorToFixed(fy);
1460
reed@google.comac10a2d2010-12-22 21:39:39 +00001461 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001462 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001463 procs->fFontScaler);
1464}
1465
bsalomon@google.com5782d712011-01-21 21:03:59 +00001466SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001467
1468 // deferred allocation
1469 if (NULL == fDrawProcs) {
1470 fDrawProcs = new GrSkDrawProcs;
1471 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1472 fDrawProcs->fContext = fContext;
1473 }
1474
1475 // init our (and GL's) state
1476 fDrawProcs->fTextContext = context;
1477 fDrawProcs->fFontScaler = NULL;
1478 return fDrawProcs;
1479}
1480
1481void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1482 size_t byteLength, SkScalar x, SkScalar y,
1483 const SkPaint& paint) {
1484 CHECK_SHOULD_DRAW(draw);
1485
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001486 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001487 // this guy will just call our drawPath()
1488 draw.drawText((const char*)text, byteLength, x, y, paint);
1489 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001490 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001491
1492 GrPaint grPaint;
1493 SkAutoCachedTexture act;
1494
Scroggod757df22011-05-16 13:11:16 +00001495 if (!this->skPaint2GrPaintShader(paint,
1496 &act,
1497 *draw.fMatrix,
1498 &grPaint,
1499 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001500 return;
1501 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001502 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001503 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001504 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1505 }
1506}
1507
1508void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1509 size_t byteLength, const SkScalar pos[],
1510 SkScalar constY, int scalarsPerPos,
1511 const SkPaint& paint) {
1512 CHECK_SHOULD_DRAW(draw);
1513
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001514 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001515 // this guy will just call our drawPath()
1516 draw.drawPosText((const char*)text, byteLength, pos, constY,
1517 scalarsPerPos, paint);
1518 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001519 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001520
1521 GrPaint grPaint;
1522 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001523 if (!this->skPaint2GrPaintShader(paint,
1524 &act,
1525 *draw.fMatrix,
1526 &grPaint,
1527 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001528 return;
1529 }
1530
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001531 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001532 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001533 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1534 scalarsPerPos, paint);
1535 }
1536}
1537
1538void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1539 size_t len, const SkPath& path,
1540 const SkMatrix* m, const SkPaint& paint) {
1541 CHECK_SHOULD_DRAW(draw);
1542
1543 SkASSERT(draw.fDevice == this);
1544 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1545}
1546
1547///////////////////////////////////////////////////////////////////////////////
1548
reed@google.comf67e4cf2011-03-15 20:56:58 +00001549bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1550 if (!paint.isLCDRenderText()) {
1551 // we're cool with the paint as is
1552 return false;
1553 }
1554
1555 if (paint.getShader() ||
1556 paint.getXfermode() || // unless its srcover
1557 paint.getMaskFilter() ||
1558 paint.getRasterizer() ||
1559 paint.getColorFilter() ||
1560 paint.getPathEffect() ||
1561 paint.isFakeBoldText() ||
1562 paint.getStyle() != SkPaint::kFill_Style) {
1563 // turn off lcd
1564 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1565 flags->fHinting = paint.getHinting();
1566 return true;
1567 }
1568 // we're cool with the paint as is
1569 return false;
1570}
1571
1572///////////////////////////////////////////////////////////////////////////////
1573
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001574SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1575 const GrSamplerState& sampler,
1576 TexType type) {
1577 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001578 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001579
bsalomon@google.come97f0852011-06-17 13:10:25 +00001580 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001581 const GrTextureDesc desc = {
1582 kRenderTarget_GrTextureFlagBit,
1583 kNone_GrAALevel,
1584 bitmap.width(),
1585 bitmap.height(),
1586 SkGr::Bitmap2PixelConfig(bitmap)
1587 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001588 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001589 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001590 // we know layers will only be drawn through drawDevice.
1591 // drawDevice has been made to work with content embedded in a
1592 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001593 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001594 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001595 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001596 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001597 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001598 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001599 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001600 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001601 GrContext::TextureKey key = bitmap.getGenerationID();
1602 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001603
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001604 entry = ctx->findAndLockTexture(key, bitmap.width(),
1605 bitmap.height(), sampler);
1606 if (NULL == entry.texture()) {
1607 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001608 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001609 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001610 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001611 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001612 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001613 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001614 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1615 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001616 }
1617 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001618 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001619}
1620
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001621void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1622 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001623}
1624
bsalomon@google.come97f0852011-06-17 13:10:25 +00001625SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1626 int width, int height,
1627 bool isOpaque,
1628 Usage usage) {
1629 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1630 width, height, usage));
1631}
1632