blob: 9fe54e5eebcc7dd479fb60d8ac957b0ffb56de70 [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
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000107GrRenderTarget* SkGpuDevice::Current3DApiRenderTarget() {
108 return (GrRenderTarget*) -1;
109}
110
reed@google.comaf951c92011-06-16 19:10:39 +0000111static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
112 switch (config) {
113 case kAlpha_8_GrPixelConfig:
114 *isOpaque = false;
115 return SkBitmap::kA8_Config;
116 case kRGB_565_GrPixelConfig:
117 *isOpaque = true;
118 return SkBitmap::kRGB_565_Config;
119 case kRGBA_4444_GrPixelConfig:
120 *isOpaque = false;
121 return SkBitmap::kARGB_4444_Config;
122 case kRGBA_8888_GrPixelConfig:
123 case kRGBX_8888_GrPixelConfig:
124 *isOpaque = (kRGBX_8888_GrPixelConfig == config);
125 return SkBitmap::kARGB_8888_Config;
126 default:
127 *isOpaque = false;
128 return SkBitmap::kNo_Config;
129 }
130}
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
reed@google.comaf951c92011-06-16 19:10:39 +0000132static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000133 SkAutoTUnref<GrRenderTarget> rtunref;
reed@google.comaf951c92011-06-16 19:10:39 +0000134 if (SkGpuDevice::Current3DApiRenderTarget() == renderTarget) {
135 renderTarget = context->createRenderTargetFrom3DApiState();
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000136 rtunref.reset(renderTarget);
reed@google.comaf951c92011-06-16 19:10:39 +0000137 }
138 GrTexture* texture = renderTarget->asTexture();
139 GrPixelConfig config = texture ? texture->config() : kRGBA_8888_GrPixelConfig;
140
141 bool isOpaque;
142 SkBitmap bitmap;
143 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
144 renderTarget->width(), renderTarget->height());
145 bitmap.setIsOpaque(isOpaque);
146 return bitmap;
147}
148
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000149SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
150: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
151 this->initFromRenderTarget(context, texture->asRenderTarget());
152}
153
reed@google.comaf951c92011-06-16 19:10:39 +0000154SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
155: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000156 this->initFromRenderTarget(context, renderTarget);
157}
158
159void SkGpuDevice::initFromRenderTarget(GrContext* context,
160 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000161 fNeedPrepareRenderTarget = false;
162 fDrawProcs = NULL;
163
164 fContext = context;
165 fContext->ref();
166
reed@google.comaf951c92011-06-16 19:10:39 +0000167 fTexture = NULL;
168 fRenderTarget = NULL;
169 fNeedClear = false;
170
171 if (Current3DApiRenderTarget() == renderTarget) {
172 fRenderTarget = fContext->createRenderTargetFrom3DApiState();
173 } else {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000174 GrAssert(NULL != renderTarget);
reed@google.comaf951c92011-06-16 19:10:39 +0000175 fRenderTarget = renderTarget;
176 fRenderTarget->ref();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000177 // if this RT is also a texture, hold a ref on it
178 fTexture = fRenderTarget->asTexture();
179 SkSafeRef(fTexture);
reed@google.comaf951c92011-06-16 19:10:39 +0000180 }
181
182 SkGrRenderTargetPixelRef* pr = new SkGrRenderTargetPixelRef(fRenderTarget);
183 this->setPixelRef(pr, 0)->unref();
184}
185
186SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000187 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000188: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000189 fNeedPrepareRenderTarget = false;
190 fDrawProcs = NULL;
191
reed@google.com7b201d22011-01-11 18:59:23 +0000192 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000193 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000194
reed@google.comac10a2d2010-12-22 21:39:39 +0000195 fTexture = NULL;
196 fRenderTarget = NULL;
197 fNeedClear = false;
198
reed@google.comaf951c92011-06-16 19:10:39 +0000199 if (config != SkBitmap::kRGB_565_Config) {
200 config = SkBitmap::kARGB_8888_Config;
201 }
202 SkBitmap bm;
203 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000204
205#if CACHE_LAYER_TEXTURES
bsalomon@google.come97f0852011-06-17 13:10:25 +0000206 TexType type = (kSaveLayer_Usage == usage) ?
207 kSaveLayerDeviceRenderTarget_TexType :
208 kDeviceRenderTarget_TexType;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000209 fCache = this->lockCachedTexture(bm, GrSamplerState::ClampNoFilter(), type);
210 fTexture = fCache.texture();
211 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000212 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000213 // hold a ref directly on fTexture (even though fCache has one) to match
214 // other constructor paths. Simplifies cleanup.
215 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000216 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000217#else
reed@google.comaf951c92011-06-16 19:10:39 +0000218 const GrTextureDesc desc = {
219 kRenderTarget_GrTextureFlagBit,
220 kNone_GrAALevel,
221 width,
222 height,
223 SkGr::Bitmap2PixelConfig(bm)
224 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000225
reed@google.comaf951c92011-06-16 19:10:39 +0000226 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000227#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000228 if (NULL != fTexture) {
229 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000230 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
reed@google.comaf951c92011-06-16 19:10:39 +0000232 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
reed@google.comaf951c92011-06-16 19:10:39 +0000234 // we defer the actual clear until our gainFocus()
235 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
reed@google.comaf951c92011-06-16 19:10:39 +0000237 // wrap the bitmap with a pixelref to expose our texture
238 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000239 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000240 } else {
241 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
242 width, height);
243 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000244 }
245}
246
247SkGpuDevice::~SkGpuDevice() {
248 if (fDrawProcs) {
249 delete fDrawProcs;
250 }
251
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000252 SkSafeUnref(fTexture);
253 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000254 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000255 GrAssert(NULL != fTexture);
256 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000257 fContext->unlockTexture(fCache);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000258 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000259 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000260}
261
reed@google.comac10a2d2010-12-22 21:39:39 +0000262///////////////////////////////////////////////////////////////////////////////
263
264void SkGpuDevice::makeRenderTargetCurrent() {
265 fContext->setRenderTarget(fRenderTarget);
266 fContext->flush(true);
267 fNeedPrepareRenderTarget = true;
268}
269
270///////////////////////////////////////////////////////////////////////////////
271
272bool SkGpuDevice::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
273 SkIRect bounds;
274 bounds.set(0, 0, this->width(), this->height());
275 if (!bounds.intersect(srcRect)) {
276 return false;
277 }
278
279 const int w = bounds.width();
280 const int h = bounds.height();
281 SkBitmap tmp;
282 // note we explicitly specify our rowBytes to be snug (no gap between rows)
283 tmp.setConfig(SkBitmap::kARGB_8888_Config, w, h, w * 4);
284 if (!tmp.allocPixels()) {
285 return false;
286 }
287
Scroggo813c33c2011-04-07 20:56:21 +0000288 tmp.lockPixels();
reed@google.comac10a2d2010-12-22 21:39:39 +0000289
Scroggoeb176032011-04-07 21:11:49 +0000290 bool read = fContext->readRenderTargetPixels(fRenderTarget,
291 bounds.fLeft, bounds.fTop,
292 bounds.width(), bounds.height(),
293 kRGBA_8888_GrPixelConfig,
294 tmp.getPixels());
Scroggo813c33c2011-04-07 20:56:21 +0000295 tmp.unlockPixels();
296 if (!read) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 return false;
298 }
299
300 tmp.swap(*bitmap);
301 return true;
302}
303
304void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y) {
305 SkAutoLockPixels alp(bitmap);
306 if (!bitmap.readyToDraw()) {
307 return;
308 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000309 GrPixelConfig config = SkGr::BitmapConfig2PixelConfig(bitmap.config(),
310 bitmap.isOpaque());
reed@google.comac10a2d2010-12-22 21:39:39 +0000311 fContext->setRenderTarget(fRenderTarget);
312 // we aren't setting the clip or matrix, so mark as dirty
313 // we don't need to set them for this call and don't have them anyway
314 fNeedPrepareRenderTarget = true;
315
316 fContext->writePixels(x, y, bitmap.width(), bitmap.height(),
317 config, bitmap.getPixels(), bitmap.rowBytes());
318}
319
320///////////////////////////////////////////////////////////////////////////////
321
322static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000323 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000324 const SkRegion& clipRegion,
325 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000326 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000327
328 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000329 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000330 const SkIRect& skBounds = clipRegion.getBounds();
331 GrRect bounds;
332 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
333 GrIntToScalar(skBounds.fTop),
334 GrIntToScalar(skBounds.fRight),
335 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000336 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
337 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000338 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000339}
340
341// call this ever each draw call, to ensure that the context reflects our state,
342// and not the state from some other canvas/device
343void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
344 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000345 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000346
347 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000348 SkASSERT(draw.fClipStack);
349 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000350 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000351 fNeedPrepareRenderTarget = false;
352 }
353}
354
reed@google.com46799cd2011-02-22 20:56:26 +0000355void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
356 const SkClipStack& clipStack) {
357 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000358 // We don't need to set them now because the context may not reflect this device.
359 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000360}
361
362void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000363 const SkRegion& clip, const SkClipStack& clipStack) {
364
reed@google.comac10a2d2010-12-22 21:39:39 +0000365 fContext->setRenderTarget(fRenderTarget);
366
bsalomon@google.comd302f142011-03-03 13:54:13 +0000367 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000368
reed@google.com6f8f2922011-03-04 22:27:10 +0000369 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000370
371 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000372 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000373 fNeedClear = false;
374 }
375}
376
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000377bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000379 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000380 return true;
381 }
382 return false;
383}
384
385///////////////////////////////////////////////////////////////////////////////
386
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000387SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
388SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
389SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
390SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
391SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
392 shader_type_mismatch);
393SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000394
bsalomon@google.com5782d712011-01-21 21:03:59 +0000395static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
396 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
397 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
398 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
399 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
400 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
401};
402
403bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
404 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000405 GrPaint* grPaint,
406 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000407
408 grPaint->fDither = skPaint.isDither();
409 grPaint->fAntiAlias = skPaint.isAntiAlias();
410
411 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
412 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
413
414 SkXfermode* mode = skPaint.getXfermode();
415 if (mode) {
416 if (!mode->asCoeff(&sm, &dm)) {
reed@google.com1a2e8d22011-01-21 22:08:29 +0000417 SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000418#if 0
419 return false;
420#endif
421 }
422 }
423 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
424 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
425
426 if (justAlpha) {
427 uint8_t alpha = skPaint.getAlpha();
428 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000429 // justAlpha is currently set to true only if there is a texture,
430 // so constantColor should not also be true.
431 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000432 } else {
433 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000434 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000435 }
Scroggo97c88c22011-05-11 14:05:25 +0000436 SkColorFilter* colorFilter = skPaint.getColorFilter();
437 SkColor color;
438 SkXfermode::Mode filterMode;
439 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000440 if (!constantColor) {
441 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
442 grPaint->fColorFilterXfermode = filterMode;
443 return true;
444 }
445 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
446 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000447 }
Scroggod757df22011-05-16 13:11:16 +0000448 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000449 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000450}
451
bsalomon@google.com5782d712011-01-21 21:03:59 +0000452bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
453 SkAutoCachedTexture* act,
454 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000455 GrPaint* grPaint,
456 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000457
bsalomon@google.com5782d712011-01-21 21:03:59 +0000458 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000459
bsalomon@google.com5782d712011-01-21 21:03:59 +0000460 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000461 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000462 return this->skPaint2GrPaintNoShader(skPaint,
463 false,
464 grPaint,
465 constantColor);
466 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000467 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000468 }
469
reed@google.comac10a2d2010-12-22 21:39:39 +0000470 SkBitmap bitmap;
471 SkMatrix matrix;
472 SkShader::TileMode tileModes[2];
473 SkScalar twoPointParams[3];
474 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
475 tileModes, twoPointParams);
476
bsalomon@google.com5782d712011-01-21 21:03:59 +0000477 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
478 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000479 SkShader::GradientInfo info;
480 SkColor color;
481
482 info.fColors = &color;
483 info.fColorOffsets = NULL;
484 info.fColorCount = 1;
485 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
486 SkPaint copy(skPaint);
487 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000488 // modulate the paint alpha by the shader's solid color alpha
489 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
490 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000491 return this->skPaint2GrPaintNoShader(copy,
492 false,
493 grPaint,
494 constantColor);
495 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000496 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000497 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000498 GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx);
499 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000500 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000501 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000502 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000503 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000504 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000505 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
506 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000507 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000508 sampler->setRadial2Params(twoPointParams[0],
509 twoPointParams[1],
510 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 }
512
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000513 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000514 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000515 SkDebugf("Couldn't convert bitmap to texture.\n");
516 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000517 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000518 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000519
520 // since our texture coords will be in local space, we wack the texture
521 // matrix to map them back into 0...1 before we load it
522 SkMatrix localM;
523 if (shader->getLocalMatrix(&localM)) {
524 SkMatrix inverse;
525 if (localM.invert(&inverse)) {
526 matrix.preConcat(inverse);
527 }
528 }
529 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000530 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
531 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000532 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000533 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000534 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000535 matrix.postScale(s, s);
536 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000537 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000538
539 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000540}
541
542///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000543
bsalomon@google.com398109c2011-04-14 18:40:27 +0000544void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000545 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000546}
547
reed@google.comac10a2d2010-12-22 21:39:39 +0000548void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
549 CHECK_SHOULD_DRAW(draw);
550
bsalomon@google.com5782d712011-01-21 21:03:59 +0000551 GrPaint grPaint;
552 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000553 if (!this->skPaint2GrPaintShader(paint,
554 &act,
555 *draw.fMatrix,
556 &grPaint,
557 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000558 return;
559 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000560
561 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000562}
563
564// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000565static const GrPrimitiveType gPointMode2PrimtiveType[] = {
566 kPoints_PrimitiveType,
567 kLines_PrimitiveType,
568 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000569};
570
571void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000572 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000573 CHECK_SHOULD_DRAW(draw);
574
575 SkScalar width = paint.getStrokeWidth();
576 if (width < 0) {
577 return;
578 }
579
580 // we only handle hairlines here, else we let the SkDraw call our drawPath()
581 if (width > 0) {
582 draw.drawPoints(mode, count, pts, paint, true);
583 return;
584 }
585
bsalomon@google.com5782d712011-01-21 21:03:59 +0000586 GrPaint grPaint;
587 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000588 if (!this->skPaint2GrPaintShader(paint,
589 &act,
590 *draw.fMatrix,
591 &grPaint,
592 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000593 return;
594 }
595
bsalomon@google.com5782d712011-01-21 21:03:59 +0000596 fContext->drawVertices(grPaint,
597 gPointMode2PrimtiveType[mode],
598 count,
599 (GrPoint*)pts,
600 NULL,
601 NULL,
602 NULL,
603 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000604}
605
reed@google.comc9aa5872011-04-05 21:05:37 +0000606///////////////////////////////////////////////////////////////////////////////
607
reed@google.comac10a2d2010-12-22 21:39:39 +0000608void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
609 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000610 CHECK_SHOULD_DRAW(draw);
611
bungeman@google.com79bd8772011-07-18 15:34:08 +0000612 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000613 SkScalar width = paint.getStrokeWidth();
614
615 /*
616 We have special code for hairline strokes, miter-strokes, and fills.
617 Anything else we just call our path code.
618 */
619 bool usePath = doStroke && width > 0 &&
620 paint.getStrokeJoin() != SkPaint::kMiter_Join;
621 // another reason we might need to call drawPath...
622 if (paint.getMaskFilter()) {
623 usePath = true;
624 }
reed@google.com67db6642011-05-26 11:46:35 +0000625 // until we aa rotated rects...
626 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
627 usePath = true;
628 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000629 // small miter limit means right angles show bevel...
630 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
631 paint.getStrokeMiter() < SK_ScalarSqrt2)
632 {
633 usePath = true;
634 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000635 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000636 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
637 usePath = true;
638 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000639
640 if (usePath) {
641 SkPath path;
642 path.addRect(rect);
643 this->drawPath(draw, path, paint, NULL, true);
644 return;
645 }
646
647 GrPaint grPaint;
648 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000649 if (!this->skPaint2GrPaintShader(paint,
650 &act,
651 *draw.fMatrix,
652 &grPaint,
653 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000654 return;
655 }
reed@google.com20efde72011-05-09 17:00:02 +0000656 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000657}
658
reed@google.com69302852011-02-16 18:08:07 +0000659#include "SkMaskFilter.h"
660#include "SkBounder.h"
661
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000662static GrPathFill skToGrFillType(SkPath::FillType fillType) {
663 switch (fillType) {
664 case SkPath::kWinding_FillType:
665 return kWinding_PathFill;
666 case SkPath::kEvenOdd_FillType:
667 return kEvenOdd_PathFill;
668 case SkPath::kInverseWinding_FillType:
669 return kInverseWinding_PathFill;
670 case SkPath::kInverseEvenOdd_FillType:
671 return kInverseEvenOdd_PathFill;
672 default:
673 SkDebugf("Unsupported path fill type\n");
674 return kHairLine_PathFill;
675 }
676}
677
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000678static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000679 int halfWidth = (kernelWidth - 1) / 2;
680 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000681 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000682 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000683 float x = static_cast<float>(i - halfWidth);
684 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
685 // is dropped here, since we renormalize the kernel below.
686 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000687 sum += kernel[i];
688 }
689 // Normalize the kernel
690 float scale = 1.0f / sum;
691 for (int i = 0; i < kernelWidth; ++i)
692 kernel[i] *= scale;
693}
694
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000695static void scaleRect(SkRect* rect, float scale) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000696 rect->fLeft *= scale;
697 rect->fTop *= scale;
698 rect->fRight *= scale;
699 rect->fBottom *= scale;
700}
701
702static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
703 SkMaskFilter* filter, const SkMatrix& matrix,
704 const SkRegion& clip, SkBounder* bounder,
705 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000706#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000707 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000708#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000709 SkMaskFilter::BlurInfo info;
710 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000711 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000712 return false;
713 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000714 SkScalar radius = info.fIgnoreTransform ? info.fRadius
715 : matrix.mapRadius(info.fRadius);
716 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000717 if (radius <= 0) {
718 return false;
719 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000720 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000721 SkRect srcRect = path.getBounds();
722
723 int scaleFactor = 1;
724
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000725 while (sigma > MAX_BLUR_SIGMA) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000726 scaleFactor *= 2;
727 sigma *= 0.5f;
728 }
senorblanco@chromium.org4a947d22011-07-18 21:48:35 +0000729 int halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000730 int kernelWidth = halfWidth * 2 + 1;
731
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000732 float invScale = 1.0f / scaleFactor;
733 scaleRect(&srcRect, invScale);
734 srcRect.roundOut();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000735 srcRect.inset(-halfWidth, -halfWidth);
736
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000737 SkRect clipBounds;
738 clipBounds.set(clip.getBounds());
739 scaleRect(&clipBounds, invScale);
740 clipBounds.roundOut();
741 clipBounds.inset(-halfWidth, -halfWidth);
742
743 srcRect.intersect(clipBounds);
744
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000745 scaleRect(&srcRect, scaleFactor);
746 SkRect finalRect = srcRect;
747
748 SkIRect finalIRect;
749 finalRect.roundOut(&finalIRect);
750 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000751 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000752 }
753 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000754 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000755 }
756 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
757 srcRect.offset(-srcRect.fLeft, -srcRect.fTop);
758 const GrTextureDesc desc = {
759 kRenderTarget_GrTextureFlagBit,
760 kNone_GrAALevel,
761 srcRect.width(),
762 srcRect.height(),
763 // We actually only need A8, but it often isn't supported as a
764 // render target
765 kRGBA_8888_GrPixelConfig
766 };
767
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000768 GrAutoScratchTexture srcEntry(context, desc);
769 GrAutoScratchTexture dstEntry(context, desc);
770 if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000771 return false;
772 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000773 GrTexture* srcTexture = srcEntry.texture();
774 GrTexture* dstTexture = dstEntry.texture();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000775 if (NULL == srcTexture || NULL == dstTexture) {
776 return false;
777 }
778 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000779 // Once this code moves into GrContext, this should be changed to use
780 // an AutoClipRestore.
781 GrClip oldClip = context->getClip();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000782 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000783 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000784 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000785 GrPaint tempPaint;
786 tempPaint.reset();
787
788 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000789 tempPaint.fAntiAlias = grp->fAntiAlias;
790 if (tempPaint.fAntiAlias) {
791 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
792 // blend coeff of zero requires dual source blending support in order
793 // to properly blend partially covered pixels. This means the AA
794 // code path may not be taken. So we use a dst blend coeff of ISA. We
795 // could special case AA draws to a dst surface with known alpha=0 to
796 // use a zero dst coeff when dual source blending isn't available.
797 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
798 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
799 }
800 // Draw hard shadow to dstTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000801 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000802 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000803
804 GrMatrix sampleM;
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000805 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000806 GrPaint paint;
807 paint.reset();
808 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
809 paint.getTextureSampler(0)->setMatrix(sampleM);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000810 GrAutoScratchTexture origEntry;
811
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000812 if (blurType != SkMaskFilter::kNormal_BlurType) {
813 // Stash away a copy of the unblurred image.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000814 origEntry.set(context, desc);
815 if (NULL == origEntry.texture()) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000816 return false;
817 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000818 context->setRenderTarget(origEntry.texture()->asRenderTarget());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000819 paint.setTexture(0, srcTexture);
820 context->drawRect(paint, srcRect);
821 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000822 for (int i = 1; i < scaleFactor; i *= 2) {
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000823 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
824 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000825 context->setRenderTarget(dstTexture->asRenderTarget());
826 SkRect dstRect(srcRect);
827 scaleRect(&dstRect, 0.5f);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000828 paint.setTexture(0, srcTexture);
829 context->drawRectToRect(paint, dstRect, srcRect);
830 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000831 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000832 }
833
834 SkAutoTMalloc<float> kernelStorage(kernelWidth);
835 float* kernel = kernelStorage.get();
836 buildKernel(sigma, kernel, kernelWidth);
837
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000838 // Clear out a halfWidth to the right of the srcRect to prevent the
839 // X convolution from reading garbage.
840 SkIRect clearRect = SkIRect::MakeXYWH(
841 srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height());
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->convolveInX(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 out a halfWidth below the srcRect to prevent the Y
849 // convolution from reading garbage.
850 clearRect = SkIRect::MakeXYWH(
851 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth);
852 context->clear(&clearRect, 0x0);
853
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000854 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000855 context->convolveInY(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000856 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000857
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000858 // Clear one pixel to the right and below, to accommodate bilinear
859 // upsampling.
860 clearRect = SkIRect::MakeXYWH(
861 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
862 context->clear(&clearRect, 0x0);
863 clearRect = SkIRect::MakeXYWH(
864 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
865 context->clear(&clearRect, 0x0);
866
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000867 if (scaleFactor > 1) {
868 // FIXME: This should be mitchell, not bilinear.
869 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000870 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871 paint.getTextureSampler(0)->setMatrix(sampleM);
872 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000873 paint.setTexture(0, srcTexture);
874 SkRect dstRect(srcRect);
875 scaleRect(&dstRect, scaleFactor);
876 context->drawRectToRect(paint, dstRect, srcRect);
877 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000878 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000879 }
880
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000881 if (blurType != SkMaskFilter::kNormal_BlurType) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000882 GrTexture* origTexture = origEntry.texture();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000883 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000884 sampleM.setIDiv(origTexture->width(), origTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000885 paint.getTextureSampler(0)->setMatrix(sampleM);
886 // Blend origTexture over srcTexture.
887 context->setRenderTarget(srcTexture->asRenderTarget());
888 paint.setTexture(0, origTexture);
889 if (SkMaskFilter::kInner_BlurType == blurType) {
890 // inner: dst = dst * src
891 paint.fSrcBlendCoeff = kDC_BlendCoeff;
892 paint.fDstBlendCoeff = kZero_BlendCoeff;
893 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
894 // solid: dst = src + dst - src * dst
895 // = (1 - dst) * src + 1 * dst
896 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
897 paint.fDstBlendCoeff = kOne_BlendCoeff;
898 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
899 // outer: dst = dst * (1 - src)
900 // = 0 * src + (1 - src) * dst
901 paint.fSrcBlendCoeff = kZero_BlendCoeff;
902 paint.fDstBlendCoeff = kISC_BlendCoeff;
903 }
904 context->drawRect(paint, srcRect);
905 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000906 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000907 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000908
909 if (grp->hasTextureOrMask()) {
910 GrMatrix inverse;
911 if (!matrix.invert(&inverse)) {
912 return false;
913 }
914 grp->preConcatActiveSamplerMatrices(inverse);
915 }
916
917 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
918 // we assume the last mask index is available for use
919 GrAssert(NULL == grp->getMask(MASK_IDX));
920 grp->setMask(MASK_IDX, srcTexture);
921 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
922
923 GrMatrix m;
924 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
925 m.postIDiv(srcTexture->width(), srcTexture->height());
926 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
927 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000928 return true;
929}
930
reed@google.com69302852011-02-16 18:08:07 +0000931static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
932 SkMaskFilter* filter, const SkMatrix& matrix,
933 const SkRegion& clip, SkBounder* bounder,
934 GrPaint* grp) {
935 SkMask srcM, dstM;
936
937 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
938 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
939 return false;
940 }
941
942 SkAutoMaskImage autoSrc(&srcM, false);
943
944 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
945 return false;
946 }
947 // this will free-up dstM when we're done (allocated in filterMask())
948 SkAutoMaskImage autoDst(&dstM, false);
949
950 if (clip.quickReject(dstM.fBounds)) {
951 return false;
952 }
953 if (bounder && !bounder->doIRect(dstM.fBounds)) {
954 return false;
955 }
956
957 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
958 // the current clip (and identity matrix) and grpaint settings
959
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000960 // used to compute inverse view, if necessary
961 GrMatrix ivm = context->getMatrix();
962
reed@google.com0c219b62011-02-16 21:31:18 +0000963 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000964
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000965 const GrTextureDesc desc = {
966 kNone_GrTextureFlags,
967 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +0000968 dstM.fBounds.width(),
969 dstM.fBounds.height(),
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000970 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +0000971 };
972
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000973 GrAutoScratchTexture ast(context, desc);
974 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000975
reed@google.com69302852011-02-16 18:08:07 +0000976 if (NULL == texture) {
977 return false;
978 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000979 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
980 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000981
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000982 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
983 grp->preConcatActiveSamplerMatrices(ivm);
984 }
985
986 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
987 // we assume the last mask index is available for use
988 GrAssert(NULL == grp->getMask(MASK_IDX));
989 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000990 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +0000991
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000992 GrRect d;
993 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000994 GrIntToScalar(dstM.fBounds.fTop),
995 GrIntToScalar(dstM.fBounds.fRight),
996 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000997
998 GrMatrix m;
999 m.setTranslate(-dstM.fBounds.fLeft, -dstM.fBounds.fTop);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001000 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001001 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1002
1003 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001004 return true;
1005}
reed@google.com69302852011-02-16 18:08:07 +00001006
reed@google.com0c219b62011-02-16 21:31:18 +00001007void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comac10a2d2010-12-22 21:39:39 +00001008 const SkPaint& paint, const SkMatrix* prePathMatrix,
1009 bool pathIsMutable) {
1010 CHECK_SHOULD_DRAW(draw);
1011
bsalomon@google.com5782d712011-01-21 21:03:59 +00001012 GrPaint grPaint;
1013 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001014 if (!this->skPaint2GrPaintShader(paint,
1015 &act,
1016 *draw.fMatrix,
1017 &grPaint,
1018 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001019 return;
1020 }
1021
reed@google.com0c219b62011-02-16 21:31:18 +00001022 // BEGIN lift from SkDraw::drawPath()
1023
1024 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1025 bool doFill = true;
1026 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001027
1028 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001029 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001030
reed@google.come3445642011-02-16 23:20:39 +00001031 if (!pathIsMutable) {
1032 result = &tmpPath;
1033 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001034 }
reed@google.come3445642011-02-16 23:20:39 +00001035 // should I push prePathMatrix on our MV stack temporarily, instead
1036 // of applying it here? See SkDraw.cpp
1037 pathPtr->transform(*prePathMatrix, result);
1038 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001039 }
reed@google.com0c219b62011-02-16 21:31:18 +00001040 // at this point we're done with prePathMatrix
1041 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001042
bsalomon@google.com04de7822011-03-25 18:04:43 +00001043 // This "if" is not part of the SkDraw::drawPath() lift.
1044 // When we get a 1.0 wide stroke we hairline stroke it instead of creating
1045 // a new stroked-path. This is motivated by canvas2D sites that draw
1046 // lines as 1.0 wide stroked paths. We can consider doing an alpha-modulated-
1047 // hairline for width < 1.0 when AA is enabled.
1048 static const int gMatrixMask = ~(SkMatrix::kIdentity_Mask |
1049 SkMatrix::kTranslate_Mask);
1050 if (!paint.getPathEffect() &&
1051 SkPaint::kStroke_Style == paint.getStyle() &&
1052 !(draw.fMatrix->getType() & gMatrixMask) &&
1053 SK_Scalar1 == paint.getStrokeWidth()) {
1054 doFill = false;
1055 }
1056
1057 if (doFill && (paint.getPathEffect() ||
1058 paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.com0c219b62011-02-16 21:31:18 +00001059 doFill = paint.getFillPath(*pathPtr, &tmpPath);
1060 pathPtr = &tmpPath;
1061 }
1062
1063 // END lift from SkDraw::drawPath()
1064
reed@google.com69302852011-02-16 18:08:07 +00001065 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001066 // avoid possibly allocating a new path in transform if we can
1067 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1068
1069 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001070 pathPtr->transform(*draw.fMatrix, devPathPtr);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001071 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1072 *draw.fMatrix, *draw.fClip, draw.fBounder,
1073 &grPaint)) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001074 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1075 *draw.fMatrix, *draw.fClip, draw.fBounder,
1076 &grPaint);
1077 }
reed@google.com69302852011-02-16 18:08:07 +00001078 return;
1079 }
reed@google.com69302852011-02-16 18:08:07 +00001080
bsalomon@google.comffca4002011-02-22 20:34:01 +00001081 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001082
reed@google.com0c219b62011-02-16 21:31:18 +00001083 if (doFill) {
1084 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001085 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001086 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001087 break;
1088 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001089 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001090 break;
1091 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001092 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001093 break;
1094 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001095 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001096 break;
1097 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001098 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001099 return;
1100 }
1101 }
1102
reed@google.com07f3ee12011-05-16 17:21:57 +00001103 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001104}
1105
reed@google.comac10a2d2010-12-22 21:39:39 +00001106void SkGpuDevice::drawBitmap(const SkDraw& draw,
1107 const SkBitmap& bitmap,
1108 const SkIRect* srcRectPtr,
1109 const SkMatrix& m,
1110 const SkPaint& paint) {
1111 CHECK_SHOULD_DRAW(draw);
1112
1113 SkIRect srcRect;
1114 if (NULL == srcRectPtr) {
1115 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1116 } else {
1117 srcRect = *srcRectPtr;
1118 }
1119
junov@google.comd935cfb2011-06-27 20:48:23 +00001120 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001121 // Convert the bitmap to a shader so that the rect can be drawn
1122 // through drawRect, which supports mask filters.
1123 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001124 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001125 if (srcRectPtr) {
1126 if (!bitmap.extractSubset(&tmp, srcRect)) {
1127 return; // extraction failed
1128 }
1129 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001130 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001131 }
1132 SkPaint paintWithTexture(paint);
1133 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1134 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001135 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001136 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001137
junov@google.com1d329782011-07-28 20:10:09 +00001138 // Transform 'm' needs to be concatenated to the draw matrix,
1139 // rather than transforming the primitive directly, so that 'm' will
1140 // also affect the behavior of the mask filter.
1141 SkMatrix drawMatrix;
1142 drawMatrix.setConcat(*draw.fMatrix, m);
1143 SkDraw transformedDraw(draw);
1144 transformedDraw.fMatrix = &drawMatrix;
1145
1146 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1147
junov@google.comd935cfb2011-06-27 20:48:23 +00001148 return;
1149 }
1150
bsalomon@google.com5782d712011-01-21 21:03:59 +00001151 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001152 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001153 return;
1154 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001155 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001156 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001157 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001158 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001159 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001160 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001161
bsalomon@google.com91958362011-06-13 17:58:13 +00001162 const int maxTextureSize = fContext->getMaxTextureSize();
1163 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1164 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001165 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001166 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001167 return;
1168 }
1169
1170 // undo the translate done by SkCanvas
1171 int DX = SkMax32(0, srcRect.fLeft);
1172 int DY = SkMax32(0, srcRect.fTop);
1173 // compute clip bounds in local coordinates
1174 SkIRect clipRect;
1175 {
1176 SkRect r;
1177 r.set(draw.fClip->getBounds());
1178 SkMatrix matrix, inverse;
1179 matrix.setConcat(*draw.fMatrix, m);
1180 if (!matrix.invert(&inverse)) {
1181 return;
1182 }
1183 inverse.mapRect(&r);
1184 r.roundOut(&clipRect);
1185 // apply the canvas' translate to our local clip
1186 clipRect.offset(DX, DY);
1187 }
1188
bsalomon@google.com91958362011-06-13 17:58:13 +00001189 int nx = bitmap.width() / maxTextureSize;
1190 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001191 for (int x = 0; x <= nx; x++) {
1192 for (int y = 0; y <= ny; y++) {
1193 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001194 tileR.set(x * maxTextureSize, y * maxTextureSize,
1195 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001196 if (!SkIRect::Intersects(tileR, clipRect)) {
1197 continue;
1198 }
1199
1200 SkIRect srcR = tileR;
1201 if (!srcR.intersect(srcRect)) {
1202 continue;
1203 }
1204
1205 SkBitmap tmpB;
1206 if (bitmap.extractSubset(&tmpB, tileR)) {
1207 // now offset it to make it "local" to our tmp bitmap
1208 srcR.offset(-tileR.fLeft, -tileR.fTop);
1209
1210 SkMatrix tmpM(m);
1211 {
1212 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1213 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1214 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1215 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001216 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001217 }
1218 }
1219 }
1220}
1221
1222/*
1223 * This is called by drawBitmap(), which has to handle images that may be too
1224 * large to be represented by a single texture.
1225 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001226 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1227 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001228 */
1229void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1230 const SkBitmap& bitmap,
1231 const SkIRect& srcRect,
1232 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001233 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001234 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1235 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001236
reed@google.com9c49bc32011-07-07 13:42:37 +00001237 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001238 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001239 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001240 return;
1241 }
1242
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001243 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1244
1245 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1246 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1247 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1248 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001249
1250 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001251 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001252 if (NULL == texture) {
1253 return;
1254 }
1255
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001256 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001257
reed@google.com20efde72011-05-09 17:00:02 +00001258 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1259 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001260 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001261 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1262 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1263 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001264 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001265
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001266 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001267 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001268 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001269 // If drawing a subrect of the bitmap and filtering is enabled,
1270 // use a constrained texture domain to avoid color bleeding
1271 GrScalar left, top, right, bottom;
1272 if (srcRect.width() > 1) {
1273 GrScalar border = GR_ScalarHalf / bitmap.width();
1274 left = paintRect.left() + border;
1275 right = paintRect.right() - border;
1276 } else {
1277 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1278 }
1279 if (srcRect.height() > 1) {
1280 GrScalar border = GR_ScalarHalf / bitmap.height();
1281 top = paintRect.top() + border;
1282 bottom = paintRect.bottom() - border;
1283 } else {
1284 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1285 }
1286 GrRect textureDomain;
1287 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001288 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001289 }
1290
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001291 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001292}
1293
1294void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1295 int left, int top, const SkPaint& paint) {
1296 CHECK_SHOULD_DRAW(draw);
1297
1298 SkAutoLockPixels alp(bitmap);
1299 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1300 return;
1301 }
1302
bsalomon@google.com5782d712011-01-21 21:03:59 +00001303 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001304 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001305 return;
1306 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001307
bsalomon@google.com5782d712011-01-21 21:03:59 +00001308 GrAutoMatrix avm(fContext, GrMatrix::I());
1309
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001310 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001311
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001312 GrTexture* texture;
1313 sampler->setClampNoFilter();
1314 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1315
1316 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001317
bsalomon@google.com5782d712011-01-21 21:03:59 +00001318 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001319 GrRect::MakeXYWH(GrIntToScalar(left),
1320 GrIntToScalar(top),
1321 GrIntToScalar(bitmap.width()),
1322 GrIntToScalar(bitmap.height())),
1323 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001324}
1325
1326void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1327 int x, int y, const SkPaint& paint) {
1328 CHECK_SHOULD_DRAW(draw);
1329
bsalomon@google.com5782d712011-01-21 21:03:59 +00001330 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001331 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001332 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001333 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001335
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001336 GrTexture* devTex = grPaint.getTexture(0);
1337 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001338
1339 const SkBitmap& bm = dev->accessBitmap(false);
1340 int w = bm.width();
1341 int h = bm.height();
1342
1343 GrAutoMatrix avm(fContext, GrMatrix::I());
1344
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001345 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001346
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001347 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1348 GrIntToScalar(y),
1349 GrIntToScalar(w),
1350 GrIntToScalar(h));
1351 // The device being drawn may not fill up its texture (saveLayer uses
1352 // the approximate ).
1353 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1354 GR_Scalar1 * h / devTex->height());
1355
1356 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001357}
1358
1359///////////////////////////////////////////////////////////////////////////////
1360
1361// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001362static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1363 kTriangles_PrimitiveType,
1364 kTriangleStrip_PrimitiveType,
1365 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001366};
1367
1368void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1369 int vertexCount, const SkPoint vertices[],
1370 const SkPoint texs[], const SkColor colors[],
1371 SkXfermode* xmode,
1372 const uint16_t indices[], int indexCount,
1373 const SkPaint& paint) {
1374 CHECK_SHOULD_DRAW(draw);
1375
bsalomon@google.com5782d712011-01-21 21:03:59 +00001376 GrPaint grPaint;
1377 SkAutoCachedTexture act;
1378 // we ignore the shader if texs is null.
1379 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001380 if (!this->skPaint2GrPaintNoShader(paint,
1381 false,
1382 &grPaint,
1383 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001384 return;
1385 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001386 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001387 if (!this->skPaint2GrPaintShader(paint, &act,
1388 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001389 &grPaint,
1390 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001391 return;
1392 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001393 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001394
1395 if (NULL != xmode && NULL != texs && NULL != colors) {
1396 SkXfermode::Mode mode;
1397 if (!SkXfermode::IsMode(xmode, &mode) ||
1398 SkXfermode::kMultiply_Mode != mode) {
1399 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1400#if 0
1401 return
1402#endif
1403 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001404 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001405
bsalomon@google.com498776a2011-08-16 19:20:44 +00001406 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1407 if (NULL != colors) {
1408 // need to convert byte order and from non-PM to PM
1409 convertedColors.realloc(vertexCount);
1410 for (int i = 0; i < vertexCount; ++i) {
1411 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1412 }
1413 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001414 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001415 fContext->drawVertices(grPaint,
1416 gVertexMode2PrimitiveType[vmode],
1417 vertexCount,
1418 (GrPoint*) vertices,
1419 (GrPoint*) texs,
1420 colors,
1421 indices,
1422 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001423}
1424
1425///////////////////////////////////////////////////////////////////////////////
1426
1427static void GlyphCacheAuxProc(void* data) {
1428 delete (GrFontScaler*)data;
1429}
1430
1431static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1432 void* auxData;
1433 GrFontScaler* scaler = NULL;
1434 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1435 scaler = (GrFontScaler*)auxData;
1436 }
1437 if (NULL == scaler) {
1438 scaler = new SkGrFontScaler(cache);
1439 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1440 }
1441 return scaler;
1442}
1443
1444static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1445 SkFixed fx, SkFixed fy,
1446 const SkGlyph& glyph) {
1447 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1448
1449 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1450
1451 if (NULL == procs->fFontScaler) {
1452 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1453 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001454
1455 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001456 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001457 *
reed@google.com3b139f52011-06-07 17:56:25 +00001458 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1459 * It calls that rather than round, because our caller has already added
1460 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1461 *
1462 * Test code between raster and gpu (they should draw the same)
1463 *
1464 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1465 *
1466 * Perhaps we should only perform this integralization if there is no
1467 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001468 */
reed@google.com3b139f52011-06-07 17:56:25 +00001469 fy = SkFixedFloorToFixed(fy);
1470
reed@google.comac10a2d2010-12-22 21:39:39 +00001471 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001472 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001473 procs->fFontScaler);
1474}
1475
bsalomon@google.com5782d712011-01-21 21:03:59 +00001476SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001477
1478 // deferred allocation
1479 if (NULL == fDrawProcs) {
1480 fDrawProcs = new GrSkDrawProcs;
1481 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1482 fDrawProcs->fContext = fContext;
1483 }
1484
1485 // init our (and GL's) state
1486 fDrawProcs->fTextContext = context;
1487 fDrawProcs->fFontScaler = NULL;
1488 return fDrawProcs;
1489}
1490
1491void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1492 size_t byteLength, SkScalar x, SkScalar y,
1493 const SkPaint& paint) {
1494 CHECK_SHOULD_DRAW(draw);
1495
1496 if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) {
1497 // this guy will just call our drawPath()
1498 draw.drawText((const char*)text, byteLength, x, y, paint);
1499 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001500 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001501
1502 GrPaint grPaint;
1503 SkAutoCachedTexture act;
1504
Scroggod757df22011-05-16 13:11:16 +00001505 if (!this->skPaint2GrPaintShader(paint,
1506 &act,
1507 *draw.fMatrix,
1508 &grPaint,
1509 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001510 return;
1511 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001512 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001513 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001514 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1515 }
1516}
1517
1518void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1519 size_t byteLength, const SkScalar pos[],
1520 SkScalar constY, int scalarsPerPos,
1521 const SkPaint& paint) {
1522 CHECK_SHOULD_DRAW(draw);
1523
1524 if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) {
1525 // this guy will just call our drawPath()
1526 draw.drawPosText((const char*)text, byteLength, pos, constY,
1527 scalarsPerPos, paint);
1528 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001529 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001530
1531 GrPaint grPaint;
1532 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001533 if (!this->skPaint2GrPaintShader(paint,
1534 &act,
1535 *draw.fMatrix,
1536 &grPaint,
1537 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001538 return;
1539 }
1540
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001541 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001542 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001543 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1544 scalarsPerPos, paint);
1545 }
1546}
1547
1548void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1549 size_t len, const SkPath& path,
1550 const SkMatrix* m, const SkPaint& paint) {
1551 CHECK_SHOULD_DRAW(draw);
1552
1553 SkASSERT(draw.fDevice == this);
1554 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1555}
1556
1557///////////////////////////////////////////////////////////////////////////////
1558
reed@google.comf67e4cf2011-03-15 20:56:58 +00001559bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1560 if (!paint.isLCDRenderText()) {
1561 // we're cool with the paint as is
1562 return false;
1563 }
1564
1565 if (paint.getShader() ||
1566 paint.getXfermode() || // unless its srcover
1567 paint.getMaskFilter() ||
1568 paint.getRasterizer() ||
1569 paint.getColorFilter() ||
1570 paint.getPathEffect() ||
1571 paint.isFakeBoldText() ||
1572 paint.getStyle() != SkPaint::kFill_Style) {
1573 // turn off lcd
1574 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1575 flags->fHinting = paint.getHinting();
1576 return true;
1577 }
1578 // we're cool with the paint as is
1579 return false;
1580}
1581
1582///////////////////////////////////////////////////////////////////////////////
1583
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001584SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1585 const GrSamplerState& sampler,
1586 TexType type) {
1587 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001588 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001589
bsalomon@google.come97f0852011-06-17 13:10:25 +00001590 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001591 const GrTextureDesc desc = {
1592 kRenderTarget_GrTextureFlagBit,
1593 kNone_GrAALevel,
1594 bitmap.width(),
1595 bitmap.height(),
1596 SkGr::Bitmap2PixelConfig(bitmap)
1597 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001598 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001599 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001600 // we know layers will only be drawn through drawDevice.
1601 // drawDevice has been made to work with content embedded in a
1602 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001603 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001604 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001605 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001606 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001607 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001608 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001609 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001610 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001611 GrContext::TextureKey key = bitmap.getGenerationID();
1612 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001613
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001614 entry = ctx->findAndLockTexture(key, bitmap.width(),
1615 bitmap.height(), sampler);
1616 if (NULL == entry.texture()) {
1617 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001618 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001619 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001620 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001621 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001622 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001623 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001624 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1625 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001626 }
1627 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001628 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001629}
1630
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001631void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1632 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001633}
1634
bsalomon@google.come97f0852011-06-17 13:10:25 +00001635SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1636 int width, int height,
1637 bool isOpaque,
1638 Usage usage) {
1639 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1640 width, height, usage));
1641}
1642