blob: ecdcd7a080daf66001458bce009d556ac5a63cd5 [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);
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000697 if (SkMaskFilter::kNone_BlurType == blurType ||
698 !context->supportsShaders()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000699 return false;
700 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000701 SkScalar radius = info.fIgnoreTransform ? info.fRadius
702 : matrix.mapRadius(info.fRadius);
703 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000704 if (radius <= 0) {
705 return false;
706 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000707 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000708 SkRect srcRect = path.getBounds();
709
710 int scaleFactor = 1;
711
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000712 while (sigma > MAX_BLUR_SIGMA) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000713 scaleFactor *= 2;
714 sigma *= 0.5f;
715 }
senorblanco@chromium.org4a947d22011-07-18 21:48:35 +0000716 int halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000717 int kernelWidth = halfWidth * 2 + 1;
718
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000719 float invScale = 1.0f / scaleFactor;
720 scaleRect(&srcRect, invScale);
721 srcRect.roundOut();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000722 srcRect.inset(-halfWidth, -halfWidth);
723
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000724 SkRect clipBounds;
725 clipBounds.set(clip.getBounds());
726 scaleRect(&clipBounds, invScale);
727 clipBounds.roundOut();
728 clipBounds.inset(-halfWidth, -halfWidth);
729
730 srcRect.intersect(clipBounds);
731
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000732 scaleRect(&srcRect, scaleFactor);
733 SkRect finalRect = srcRect;
734
735 SkIRect finalIRect;
736 finalRect.roundOut(&finalIRect);
737 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000738 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000739 }
740 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000741 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000742 }
743 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
744 srcRect.offset(-srcRect.fLeft, -srcRect.fTop);
745 const GrTextureDesc desc = {
746 kRenderTarget_GrTextureFlagBit,
747 kNone_GrAALevel,
748 srcRect.width(),
749 srcRect.height(),
750 // We actually only need A8, but it often isn't supported as a
751 // render target
752 kRGBA_8888_GrPixelConfig
753 };
754
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000755 GrAutoScratchTexture srcEntry(context, desc);
756 GrAutoScratchTexture dstEntry(context, desc);
757 if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000758 return false;
759 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000760 GrTexture* srcTexture = srcEntry.texture();
761 GrTexture* dstTexture = dstEntry.texture();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000762 if (NULL == srcTexture || NULL == dstTexture) {
763 return false;
764 }
765 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000766 // Once this code moves into GrContext, this should be changed to use
767 // an AutoClipRestore.
768 GrClip oldClip = context->getClip();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000769 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000770 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000771 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000772 GrPaint tempPaint;
773 tempPaint.reset();
774
775 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000776 tempPaint.fAntiAlias = grp->fAntiAlias;
777 if (tempPaint.fAntiAlias) {
778 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
779 // blend coeff of zero requires dual source blending support in order
780 // to properly blend partially covered pixels. This means the AA
781 // code path may not be taken. So we use a dst blend coeff of ISA. We
782 // could special case AA draws to a dst surface with known alpha=0 to
783 // use a zero dst coeff when dual source blending isn't available.
784 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
785 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
786 }
787 // Draw hard shadow to dstTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000788 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000789 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000790
791 GrMatrix sampleM;
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000792 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000793 GrPaint paint;
794 paint.reset();
795 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
796 paint.getTextureSampler(0)->setMatrix(sampleM);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000797 GrAutoScratchTexture origEntry;
798
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000799 if (blurType != SkMaskFilter::kNormal_BlurType) {
800 // Stash away a copy of the unblurred image.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000801 origEntry.set(context, desc);
802 if (NULL == origEntry.texture()) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000803 return false;
804 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000805 context->setRenderTarget(origEntry.texture()->asRenderTarget());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000806 paint.setTexture(0, srcTexture);
807 context->drawRect(paint, srcRect);
808 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809 for (int i = 1; i < scaleFactor; i *= 2) {
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000810 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
811 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000812 context->setRenderTarget(dstTexture->asRenderTarget());
813 SkRect dstRect(srcRect);
814 scaleRect(&dstRect, 0.5f);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000815 paint.setTexture(0, srcTexture);
816 context->drawRectToRect(paint, dstRect, srcRect);
817 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000818 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000819 }
820
821 SkAutoTMalloc<float> kernelStorage(kernelWidth);
822 float* kernel = kernelStorage.get();
823 buildKernel(sigma, kernel, kernelWidth);
824
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000825 // Clear out a halfWidth to the right of the srcRect to prevent the
826 // X convolution from reading garbage.
827 SkIRect clearRect = SkIRect::MakeXYWH(
828 srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height());
829 context->clear(&clearRect, 0x0);
830
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000831 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000832 context->convolveInX(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000833 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000834
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000835 // Clear out a halfWidth below the srcRect to prevent the Y
836 // convolution from reading garbage.
837 clearRect = SkIRect::MakeXYWH(
838 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth);
839 context->clear(&clearRect, 0x0);
840
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000841 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000842 context->convolveInY(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000843 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000844
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000845 // Clear one pixel to the right and below, to accommodate bilinear
846 // upsampling.
847 clearRect = SkIRect::MakeXYWH(
848 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
849 context->clear(&clearRect, 0x0);
850 clearRect = SkIRect::MakeXYWH(
851 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
852 context->clear(&clearRect, 0x0);
853
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000854 if (scaleFactor > 1) {
855 // FIXME: This should be mitchell, not bilinear.
856 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000857 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000858 paint.getTextureSampler(0)->setMatrix(sampleM);
859 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000860 paint.setTexture(0, srcTexture);
861 SkRect dstRect(srcRect);
862 scaleRect(&dstRect, scaleFactor);
863 context->drawRectToRect(paint, dstRect, srcRect);
864 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000865 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000866 }
867
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000868 if (blurType != SkMaskFilter::kNormal_BlurType) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000869 GrTexture* origTexture = origEntry.texture();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000870 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000871 sampleM.setIDiv(origTexture->width(), origTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000872 paint.getTextureSampler(0)->setMatrix(sampleM);
873 // Blend origTexture over srcTexture.
874 context->setRenderTarget(srcTexture->asRenderTarget());
875 paint.setTexture(0, origTexture);
876 if (SkMaskFilter::kInner_BlurType == blurType) {
877 // inner: dst = dst * src
878 paint.fSrcBlendCoeff = kDC_BlendCoeff;
879 paint.fDstBlendCoeff = kZero_BlendCoeff;
880 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
881 // solid: dst = src + dst - src * dst
882 // = (1 - dst) * src + 1 * dst
883 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
884 paint.fDstBlendCoeff = kOne_BlendCoeff;
885 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
886 // outer: dst = dst * (1 - src)
887 // = 0 * src + (1 - src) * dst
888 paint.fSrcBlendCoeff = kZero_BlendCoeff;
889 paint.fDstBlendCoeff = kISC_BlendCoeff;
890 }
891 context->drawRect(paint, srcRect);
892 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000893 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000894 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000895
896 if (grp->hasTextureOrMask()) {
897 GrMatrix inverse;
898 if (!matrix.invert(&inverse)) {
899 return false;
900 }
901 grp->preConcatActiveSamplerMatrices(inverse);
902 }
903
904 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
905 // we assume the last mask index is available for use
906 GrAssert(NULL == grp->getMask(MASK_IDX));
907 grp->setMask(MASK_IDX, srcTexture);
908 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
909
910 GrMatrix m;
911 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
912 m.postIDiv(srcTexture->width(), srcTexture->height());
913 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
914 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000915 return true;
916}
917
reed@google.com69302852011-02-16 18:08:07 +0000918static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
919 SkMaskFilter* filter, const SkMatrix& matrix,
920 const SkRegion& clip, SkBounder* bounder,
921 GrPaint* grp) {
922 SkMask srcM, dstM;
923
924 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
925 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
926 return false;
927 }
928
929 SkAutoMaskImage autoSrc(&srcM, false);
930
931 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
932 return false;
933 }
934 // this will free-up dstM when we're done (allocated in filterMask())
935 SkAutoMaskImage autoDst(&dstM, false);
936
937 if (clip.quickReject(dstM.fBounds)) {
938 return false;
939 }
940 if (bounder && !bounder->doIRect(dstM.fBounds)) {
941 return false;
942 }
943
944 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
945 // the current clip (and identity matrix) and grpaint settings
946
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000947 // used to compute inverse view, if necessary
948 GrMatrix ivm = context->getMatrix();
949
reed@google.com0c219b62011-02-16 21:31:18 +0000950 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000951
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000952 const GrTextureDesc desc = {
953 kNone_GrTextureFlags,
954 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +0000955 dstM.fBounds.width(),
956 dstM.fBounds.height(),
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000957 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +0000958 };
959
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000960 GrAutoScratchTexture ast(context, desc);
961 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000962
reed@google.com69302852011-02-16 18:08:07 +0000963 if (NULL == texture) {
964 return false;
965 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000966 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
967 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000968
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000969 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
970 grp->preConcatActiveSamplerMatrices(ivm);
971 }
972
973 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
974 // we assume the last mask index is available for use
975 GrAssert(NULL == grp->getMask(MASK_IDX));
976 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000977 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +0000978
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000979 GrRect d;
980 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000981 GrIntToScalar(dstM.fBounds.fTop),
982 GrIntToScalar(dstM.fBounds.fRight),
983 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000984
985 GrMatrix m;
986 m.setTranslate(-dstM.fBounds.fLeft, -dstM.fBounds.fTop);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000987 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000988 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
989
990 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000991 return true;
992}
reed@google.com69302852011-02-16 18:08:07 +0000993
reed@google.com0c219b62011-02-16 21:31:18 +0000994void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comac10a2d2010-12-22 21:39:39 +0000995 const SkPaint& paint, const SkMatrix* prePathMatrix,
996 bool pathIsMutable) {
997 CHECK_SHOULD_DRAW(draw);
998
bsalomon@google.com5782d712011-01-21 21:03:59 +0000999 GrPaint grPaint;
1000 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001001 if (!this->skPaint2GrPaintShader(paint,
1002 &act,
1003 *draw.fMatrix,
1004 &grPaint,
1005 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001006 return;
1007 }
1008
reed@google.com0c219b62011-02-16 21:31:18 +00001009 // BEGIN lift from SkDraw::drawPath()
1010
1011 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1012 bool doFill = true;
1013 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001014
1015 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001016 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001017
reed@google.come3445642011-02-16 23:20:39 +00001018 if (!pathIsMutable) {
1019 result = &tmpPath;
1020 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001021 }
reed@google.come3445642011-02-16 23:20:39 +00001022 // should I push prePathMatrix on our MV stack temporarily, instead
1023 // of applying it here? See SkDraw.cpp
1024 pathPtr->transform(*prePathMatrix, result);
1025 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001026 }
reed@google.com0c219b62011-02-16 21:31:18 +00001027 // at this point we're done with prePathMatrix
1028 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001029
bsalomon@google.com04de7822011-03-25 18:04:43 +00001030 // This "if" is not part of the SkDraw::drawPath() lift.
1031 // When we get a 1.0 wide stroke we hairline stroke it instead of creating
1032 // a new stroked-path. This is motivated by canvas2D sites that draw
1033 // lines as 1.0 wide stroked paths. We can consider doing an alpha-modulated-
1034 // hairline for width < 1.0 when AA is enabled.
1035 static const int gMatrixMask = ~(SkMatrix::kIdentity_Mask |
1036 SkMatrix::kTranslate_Mask);
1037 if (!paint.getPathEffect() &&
1038 SkPaint::kStroke_Style == paint.getStyle() &&
1039 !(draw.fMatrix->getType() & gMatrixMask) &&
1040 SK_Scalar1 == paint.getStrokeWidth()) {
1041 doFill = false;
1042 }
1043
1044 if (doFill && (paint.getPathEffect() ||
1045 paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.com0c219b62011-02-16 21:31:18 +00001046 doFill = paint.getFillPath(*pathPtr, &tmpPath);
1047 pathPtr = &tmpPath;
1048 }
1049
1050 // END lift from SkDraw::drawPath()
1051
reed@google.com69302852011-02-16 18:08:07 +00001052 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001053 // avoid possibly allocating a new path in transform if we can
1054 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1055
1056 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001057 pathPtr->transform(*draw.fMatrix, devPathPtr);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001058 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1059 *draw.fMatrix, *draw.fClip, draw.fBounder,
1060 &grPaint)) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001061 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1062 *draw.fMatrix, *draw.fClip, draw.fBounder,
1063 &grPaint);
1064 }
reed@google.com69302852011-02-16 18:08:07 +00001065 return;
1066 }
reed@google.com69302852011-02-16 18:08:07 +00001067
bsalomon@google.comffca4002011-02-22 20:34:01 +00001068 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001069
reed@google.com0c219b62011-02-16 21:31:18 +00001070 if (doFill) {
1071 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001072 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001073 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001074 break;
1075 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001076 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001077 break;
1078 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001079 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001080 break;
1081 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001082 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001083 break;
1084 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001085 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001086 return;
1087 }
1088 }
1089
reed@google.com07f3ee12011-05-16 17:21:57 +00001090 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001091}
1092
reed@google.comac10a2d2010-12-22 21:39:39 +00001093void SkGpuDevice::drawBitmap(const SkDraw& draw,
1094 const SkBitmap& bitmap,
1095 const SkIRect* srcRectPtr,
1096 const SkMatrix& m,
1097 const SkPaint& paint) {
1098 CHECK_SHOULD_DRAW(draw);
1099
1100 SkIRect srcRect;
1101 if (NULL == srcRectPtr) {
1102 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1103 } else {
1104 srcRect = *srcRectPtr;
1105 }
1106
junov@google.comd935cfb2011-06-27 20:48:23 +00001107 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001108 // Convert the bitmap to a shader so that the rect can be drawn
1109 // through drawRect, which supports mask filters.
1110 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001111 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001112 if (srcRectPtr) {
1113 if (!bitmap.extractSubset(&tmp, srcRect)) {
1114 return; // extraction failed
1115 }
1116 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001117 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001118 }
1119 SkPaint paintWithTexture(paint);
1120 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1121 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001122 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001123 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001124
junov@google.com1d329782011-07-28 20:10:09 +00001125 // Transform 'm' needs to be concatenated to the draw matrix,
1126 // rather than transforming the primitive directly, so that 'm' will
1127 // also affect the behavior of the mask filter.
1128 SkMatrix drawMatrix;
1129 drawMatrix.setConcat(*draw.fMatrix, m);
1130 SkDraw transformedDraw(draw);
1131 transformedDraw.fMatrix = &drawMatrix;
1132
1133 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1134
junov@google.comd935cfb2011-06-27 20:48:23 +00001135 return;
1136 }
1137
bsalomon@google.com5782d712011-01-21 21:03:59 +00001138 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001139 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001140 return;
1141 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001142 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001143 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001144 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001145 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001146 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001147 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001148
bsalomon@google.com91958362011-06-13 17:58:13 +00001149 const int maxTextureSize = fContext->getMaxTextureSize();
1150 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1151 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001152 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001153 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001154 return;
1155 }
1156
1157 // undo the translate done by SkCanvas
1158 int DX = SkMax32(0, srcRect.fLeft);
1159 int DY = SkMax32(0, srcRect.fTop);
1160 // compute clip bounds in local coordinates
1161 SkIRect clipRect;
1162 {
1163 SkRect r;
1164 r.set(draw.fClip->getBounds());
1165 SkMatrix matrix, inverse;
1166 matrix.setConcat(*draw.fMatrix, m);
1167 if (!matrix.invert(&inverse)) {
1168 return;
1169 }
1170 inverse.mapRect(&r);
1171 r.roundOut(&clipRect);
1172 // apply the canvas' translate to our local clip
1173 clipRect.offset(DX, DY);
1174 }
1175
bsalomon@google.com91958362011-06-13 17:58:13 +00001176 int nx = bitmap.width() / maxTextureSize;
1177 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001178 for (int x = 0; x <= nx; x++) {
1179 for (int y = 0; y <= ny; y++) {
1180 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001181 tileR.set(x * maxTextureSize, y * maxTextureSize,
1182 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001183 if (!SkIRect::Intersects(tileR, clipRect)) {
1184 continue;
1185 }
1186
1187 SkIRect srcR = tileR;
1188 if (!srcR.intersect(srcRect)) {
1189 continue;
1190 }
1191
1192 SkBitmap tmpB;
1193 if (bitmap.extractSubset(&tmpB, tileR)) {
1194 // now offset it to make it "local" to our tmp bitmap
1195 srcR.offset(-tileR.fLeft, -tileR.fTop);
1196
1197 SkMatrix tmpM(m);
1198 {
1199 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1200 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1201 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1202 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001203 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001204 }
1205 }
1206 }
1207}
1208
1209/*
1210 * This is called by drawBitmap(), which has to handle images that may be too
1211 * large to be represented by a single texture.
1212 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001213 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1214 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001215 */
1216void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1217 const SkBitmap& bitmap,
1218 const SkIRect& srcRect,
1219 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001220 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001221 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1222 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001223
reed@google.com9c49bc32011-07-07 13:42:37 +00001224 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001225 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001226 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001227 return;
1228 }
1229
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001230 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1231
1232 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1233 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1234 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1235 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001236
1237 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001238 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001239 if (NULL == texture) {
1240 return;
1241 }
1242
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001243 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001244
reed@google.com20efde72011-05-09 17:00:02 +00001245 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1246 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001247 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001248 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1249 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1250 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001251 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001252
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001253 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001254 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001255 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001256 // If drawing a subrect of the bitmap and filtering is enabled,
1257 // use a constrained texture domain to avoid color bleeding
1258 GrScalar left, top, right, bottom;
1259 if (srcRect.width() > 1) {
1260 GrScalar border = GR_ScalarHalf / bitmap.width();
1261 left = paintRect.left() + border;
1262 right = paintRect.right() - border;
1263 } else {
1264 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1265 }
1266 if (srcRect.height() > 1) {
1267 GrScalar border = GR_ScalarHalf / bitmap.height();
1268 top = paintRect.top() + border;
1269 bottom = paintRect.bottom() - border;
1270 } else {
1271 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1272 }
1273 GrRect textureDomain;
1274 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001275 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001276 }
1277
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001278 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001279}
1280
1281void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1282 int left, int top, const SkPaint& paint) {
1283 CHECK_SHOULD_DRAW(draw);
1284
1285 SkAutoLockPixels alp(bitmap);
1286 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1287 return;
1288 }
1289
bsalomon@google.com5782d712011-01-21 21:03:59 +00001290 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001291 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001292 return;
1293 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001294
bsalomon@google.com5782d712011-01-21 21:03:59 +00001295 GrAutoMatrix avm(fContext, GrMatrix::I());
1296
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001297 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001298
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001299 GrTexture* texture;
1300 sampler->setClampNoFilter();
1301 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1302
1303 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001304
bsalomon@google.com5782d712011-01-21 21:03:59 +00001305 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001306 GrRect::MakeXYWH(GrIntToScalar(left),
1307 GrIntToScalar(top),
1308 GrIntToScalar(bitmap.width()),
1309 GrIntToScalar(bitmap.height())),
1310 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001311}
1312
1313void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1314 int x, int y, const SkPaint& paint) {
1315 CHECK_SHOULD_DRAW(draw);
1316
bsalomon@google.com5782d712011-01-21 21:03:59 +00001317 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001318 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001319 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001320 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001321 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001322
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001323 GrTexture* devTex = grPaint.getTexture(0);
1324 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001325
1326 const SkBitmap& bm = dev->accessBitmap(false);
1327 int w = bm.width();
1328 int h = bm.height();
1329
1330 GrAutoMatrix avm(fContext, GrMatrix::I());
1331
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001332 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001333
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001334 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1335 GrIntToScalar(y),
1336 GrIntToScalar(w),
1337 GrIntToScalar(h));
1338 // The device being drawn may not fill up its texture (saveLayer uses
1339 // the approximate ).
1340 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1341 GR_Scalar1 * h / devTex->height());
1342
1343 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001344}
1345
1346///////////////////////////////////////////////////////////////////////////////
1347
1348// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001349static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1350 kTriangles_PrimitiveType,
1351 kTriangleStrip_PrimitiveType,
1352 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001353};
1354
1355void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1356 int vertexCount, const SkPoint vertices[],
1357 const SkPoint texs[], const SkColor colors[],
1358 SkXfermode* xmode,
1359 const uint16_t indices[], int indexCount,
1360 const SkPaint& paint) {
1361 CHECK_SHOULD_DRAW(draw);
1362
bsalomon@google.com5782d712011-01-21 21:03:59 +00001363 GrPaint grPaint;
1364 SkAutoCachedTexture act;
1365 // we ignore the shader if texs is null.
1366 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001367 if (!this->skPaint2GrPaintNoShader(paint,
1368 false,
1369 &grPaint,
1370 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001371 return;
1372 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001373 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001374 if (!this->skPaint2GrPaintShader(paint, &act,
1375 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001376 &grPaint,
1377 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001378 return;
1379 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001380 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001381
1382 if (NULL != xmode && NULL != texs && NULL != colors) {
1383 SkXfermode::Mode mode;
1384 if (!SkXfermode::IsMode(xmode, &mode) ||
1385 SkXfermode::kMultiply_Mode != mode) {
1386 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1387#if 0
1388 return
1389#endif
1390 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001391 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001392
bsalomon@google.com498776a2011-08-16 19:20:44 +00001393 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1394 if (NULL != colors) {
1395 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001396 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001397 for (int i = 0; i < vertexCount; ++i) {
1398 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1399 }
1400 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001401 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001402 fContext->drawVertices(grPaint,
1403 gVertexMode2PrimitiveType[vmode],
1404 vertexCount,
1405 (GrPoint*) vertices,
1406 (GrPoint*) texs,
1407 colors,
1408 indices,
1409 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001410}
1411
1412///////////////////////////////////////////////////////////////////////////////
1413
1414static void GlyphCacheAuxProc(void* data) {
1415 delete (GrFontScaler*)data;
1416}
1417
1418static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1419 void* auxData;
1420 GrFontScaler* scaler = NULL;
1421 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1422 scaler = (GrFontScaler*)auxData;
1423 }
1424 if (NULL == scaler) {
1425 scaler = new SkGrFontScaler(cache);
1426 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1427 }
1428 return scaler;
1429}
1430
1431static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1432 SkFixed fx, SkFixed fy,
1433 const SkGlyph& glyph) {
1434 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1435
1436 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1437
1438 if (NULL == procs->fFontScaler) {
1439 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1440 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001441
1442 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001443 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001444 *
reed@google.com3b139f52011-06-07 17:56:25 +00001445 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1446 * It calls that rather than round, because our caller has already added
1447 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1448 *
1449 * Test code between raster and gpu (they should draw the same)
1450 *
1451 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1452 *
1453 * Perhaps we should only perform this integralization if there is no
1454 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001455 */
reed@google.com3b139f52011-06-07 17:56:25 +00001456 fy = SkFixedFloorToFixed(fy);
1457
reed@google.comac10a2d2010-12-22 21:39:39 +00001458 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001459 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001460 procs->fFontScaler);
1461}
1462
bsalomon@google.com5782d712011-01-21 21:03:59 +00001463SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001464
1465 // deferred allocation
1466 if (NULL == fDrawProcs) {
1467 fDrawProcs = new GrSkDrawProcs;
1468 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1469 fDrawProcs->fContext = fContext;
1470 }
1471
1472 // init our (and GL's) state
1473 fDrawProcs->fTextContext = context;
1474 fDrawProcs->fFontScaler = NULL;
1475 return fDrawProcs;
1476}
1477
1478void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1479 size_t byteLength, SkScalar x, SkScalar y,
1480 const SkPaint& paint) {
1481 CHECK_SHOULD_DRAW(draw);
1482
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001483 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001484 // this guy will just call our drawPath()
1485 draw.drawText((const char*)text, byteLength, x, y, paint);
1486 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001487 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001488
1489 GrPaint grPaint;
1490 SkAutoCachedTexture act;
1491
Scroggod757df22011-05-16 13:11:16 +00001492 if (!this->skPaint2GrPaintShader(paint,
1493 &act,
1494 *draw.fMatrix,
1495 &grPaint,
1496 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001497 return;
1498 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001499 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001500 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001501 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1502 }
1503}
1504
1505void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1506 size_t byteLength, const SkScalar pos[],
1507 SkScalar constY, int scalarsPerPos,
1508 const SkPaint& paint) {
1509 CHECK_SHOULD_DRAW(draw);
1510
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001511 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001512 // this guy will just call our drawPath()
1513 draw.drawPosText((const char*)text, byteLength, pos, constY,
1514 scalarsPerPos, paint);
1515 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001516 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001517
1518 GrPaint grPaint;
1519 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001520 if (!this->skPaint2GrPaintShader(paint,
1521 &act,
1522 *draw.fMatrix,
1523 &grPaint,
1524 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001525 return;
1526 }
1527
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001528 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001529 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001530 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1531 scalarsPerPos, paint);
1532 }
1533}
1534
1535void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1536 size_t len, const SkPath& path,
1537 const SkMatrix* m, const SkPaint& paint) {
1538 CHECK_SHOULD_DRAW(draw);
1539
1540 SkASSERT(draw.fDevice == this);
1541 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1542}
1543
1544///////////////////////////////////////////////////////////////////////////////
1545
reed@google.comf67e4cf2011-03-15 20:56:58 +00001546bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1547 if (!paint.isLCDRenderText()) {
1548 // we're cool with the paint as is
1549 return false;
1550 }
1551
1552 if (paint.getShader() ||
1553 paint.getXfermode() || // unless its srcover
1554 paint.getMaskFilter() ||
1555 paint.getRasterizer() ||
1556 paint.getColorFilter() ||
1557 paint.getPathEffect() ||
1558 paint.isFakeBoldText() ||
1559 paint.getStyle() != SkPaint::kFill_Style) {
1560 // turn off lcd
1561 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1562 flags->fHinting = paint.getHinting();
1563 return true;
1564 }
1565 // we're cool with the paint as is
1566 return false;
1567}
1568
1569///////////////////////////////////////////////////////////////////////////////
1570
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001571SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1572 const GrSamplerState& sampler,
1573 TexType type) {
1574 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001575 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001576
bsalomon@google.come97f0852011-06-17 13:10:25 +00001577 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001578 const GrTextureDesc desc = {
1579 kRenderTarget_GrTextureFlagBit,
1580 kNone_GrAALevel,
1581 bitmap.width(),
1582 bitmap.height(),
1583 SkGr::Bitmap2PixelConfig(bitmap)
1584 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001585 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001586 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001587 // we know layers will only be drawn through drawDevice.
1588 // drawDevice has been made to work with content embedded in a
1589 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001590 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001591 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001592 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001593 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001594 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001595 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001596 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001597 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001598 GrContext::TextureKey key = bitmap.getGenerationID();
1599 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001600
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001601 entry = ctx->findAndLockTexture(key, bitmap.width(),
1602 bitmap.height(), sampler);
1603 if (NULL == entry.texture()) {
1604 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001605 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001606 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001607 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001608 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001609 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001610 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001611 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1612 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001613 }
1614 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001615 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001616}
1617
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001618void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1619 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001620}
1621
bsalomon@google.come97f0852011-06-17 13:10:25 +00001622SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1623 int width, int height,
1624 bool isOpaque,
1625 Usage usage) {
1626 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1627 width, height, usage));
1628}
1629