blob: f14593908679fb3280d1ae584ab4673716713654 [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.comfe626382011-09-21 13:50:35 +000020#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000021#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
23#define CACHE_LAYER_TEXTURES 1
24
25#if 0
26 extern bool (*gShouldDrawProc)();
27 #define CHECK_SHOULD_DRAW(draw) \
28 do { \
29 if (gShouldDrawProc && !gShouldDrawProc()) return; \
30 this->prepareRenderTarget(draw); \
31 } while (0)
32#else
33 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw)
34#endif
35
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000036// we use the same texture slot on GrPaint for bitmaps and shaders
37// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
38enum {
39 kBitmapTextureIdx = 0,
40 kShaderTextureIdx = 0
41};
42
reed@google.comcde92112011-07-06 20:00:52 +000043
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000044#define MAX_BLUR_SIGMA 4.0f
45// FIXME: This value comes from from SkBlurMaskFilter.cpp.
46// Should probably be put in a common header someplace.
47#define MAX_BLUR_RADIUS SkIntToScalar(128)
48// This constant approximates the scaling done in the software path's
49// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
50// IMHO, it actually should be 1: we blur "less" than we should do
51// according to the CSS and canvas specs, simply because Safari does the same.
52// Firefox used to do the same too, until 4.0 where they fixed it. So at some
53// point we should probably get rid of these scaling constants and rebaseline
54// all the blur tests.
55#define BLUR_SIGMA_SCALE 0.6f
reed@google.comac10a2d2010-12-22 21:39:39 +000056///////////////////////////////////////////////////////////////////////////////
57
58SkGpuDevice::SkAutoCachedTexture::
59 SkAutoCachedTexture(SkGpuDevice* device,
60 const SkBitmap& bitmap,
61 const GrSamplerState& sampler,
62 GrTexture** texture) {
63 GrAssert(texture);
reed@google.comac10a2d2010-12-22 21:39:39 +000064 *texture = this->set(device, bitmap, sampler);
65}
66
67SkGpuDevice::SkAutoCachedTexture::SkAutoCachedTexture() {
reed@google.comac10a2d2010-12-22 21:39:39 +000068}
69
70GrTexture* SkGpuDevice::SkAutoCachedTexture::set(SkGpuDevice* device,
71 const SkBitmap& bitmap,
72 const GrSamplerState& sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000073 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000074 fDevice->unlockCachedTexture(fTex);
75 }
76 fDevice = device;
77 GrTexture* texture = (GrTexture*)bitmap.getTexture();
78 if (texture) {
79 // return the native texture
bsalomon@google.com50398bf2011-07-26 20:45:30 +000080 fTex.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000081 } else {
82 // look it up in our cache
bsalomon@google.com50398bf2011-07-26 20:45:30 +000083 fTex = device->lockCachedTexture(bitmap, sampler);
84 texture = fTex.texture();
reed@google.comac10a2d2010-12-22 21:39:39 +000085 }
86 return texture;
87}
88
89SkGpuDevice::SkAutoCachedTexture::~SkAutoCachedTexture() {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000090 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000091 fDevice->unlockCachedTexture(fTex);
92 }
93}
94
95///////////////////////////////////////////////////////////////////////////////
96
97bool gDoTraceDraw;
98
99struct GrSkDrawProcs : public SkDrawProcs {
100public:
101 GrContext* fContext;
102 GrTextContext* fTextContext;
103 GrFontScaler* fFontScaler; // cached in the skia glyphcache
104};
105
106///////////////////////////////////////////////////////////////////////////////
107
reed@google.comaf951c92011-06-16 19:10:39 +0000108static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
109 switch (config) {
110 case kAlpha_8_GrPixelConfig:
111 *isOpaque = false;
112 return SkBitmap::kA8_Config;
113 case kRGB_565_GrPixelConfig:
114 *isOpaque = true;
115 return SkBitmap::kRGB_565_Config;
116 case kRGBA_4444_GrPixelConfig:
117 *isOpaque = false;
118 return SkBitmap::kARGB_4444_Config;
119 case kRGBA_8888_GrPixelConfig:
120 case kRGBX_8888_GrPixelConfig:
121 *isOpaque = (kRGBX_8888_GrPixelConfig == config);
122 return SkBitmap::kARGB_8888_Config;
123 default:
124 *isOpaque = false;
125 return SkBitmap::kNo_Config;
126 }
127}
reed@google.comac10a2d2010-12-22 21:39:39 +0000128
reed@google.comaf951c92011-06-16 19:10:39 +0000129static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000130 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000131
132 bool isOpaque;
133 SkBitmap bitmap;
134 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
135 renderTarget->width(), renderTarget->height());
136 bitmap.setIsOpaque(isOpaque);
137 return bitmap;
138}
139
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000140SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
141: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
142 this->initFromRenderTarget(context, texture->asRenderTarget());
143}
144
reed@google.comaf951c92011-06-16 19:10:39 +0000145SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
146: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000147 this->initFromRenderTarget(context, renderTarget);
148}
149
150void SkGpuDevice::initFromRenderTarget(GrContext* context,
151 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000152 fNeedPrepareRenderTarget = false;
153 fDrawProcs = NULL;
154
155 fContext = context;
156 fContext->ref();
157
reed@google.comaf951c92011-06-16 19:10:39 +0000158 fTexture = NULL;
159 fRenderTarget = NULL;
160 fNeedClear = false;
161
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000162 GrAssert(NULL != renderTarget);
163 fRenderTarget = renderTarget;
164 fRenderTarget->ref();
165 // if this RT is also a texture, hold a ref on it
166 fTexture = fRenderTarget->asTexture();
167 SkSafeRef(fTexture);
reed@google.comaf951c92011-06-16 19:10:39 +0000168
169 SkGrRenderTargetPixelRef* pr = new SkGrRenderTargetPixelRef(fRenderTarget);
170 this->setPixelRef(pr, 0)->unref();
171}
172
173SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000174 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000175: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000176 fNeedPrepareRenderTarget = false;
177 fDrawProcs = NULL;
178
reed@google.com7b201d22011-01-11 18:59:23 +0000179 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000180 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000181
reed@google.comac10a2d2010-12-22 21:39:39 +0000182 fTexture = NULL;
183 fRenderTarget = NULL;
184 fNeedClear = false;
185
reed@google.comaf951c92011-06-16 19:10:39 +0000186 if (config != SkBitmap::kRGB_565_Config) {
187 config = SkBitmap::kARGB_8888_Config;
188 }
189 SkBitmap bm;
190 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000191
192#if CACHE_LAYER_TEXTURES
bsalomon@google.come97f0852011-06-17 13:10:25 +0000193 TexType type = (kSaveLayer_Usage == usage) ?
194 kSaveLayerDeviceRenderTarget_TexType :
195 kDeviceRenderTarget_TexType;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000196 fCache = this->lockCachedTexture(bm, GrSamplerState::ClampNoFilter(), type);
197 fTexture = fCache.texture();
198 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000199 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000200 // hold a ref directly on fTexture (even though fCache has one) to match
201 // other constructor paths. Simplifies cleanup.
202 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000203 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000204#else
reed@google.comaf951c92011-06-16 19:10:39 +0000205 const GrTextureDesc desc = {
206 kRenderTarget_GrTextureFlagBit,
207 kNone_GrAALevel,
208 width,
209 height,
210 SkGr::Bitmap2PixelConfig(bm)
211 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000212
reed@google.comaf951c92011-06-16 19:10:39 +0000213 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000214#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000215 if (NULL != fTexture) {
216 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000217 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000218
reed@google.comaf951c92011-06-16 19:10:39 +0000219 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000220
reed@google.comaf951c92011-06-16 19:10:39 +0000221 // we defer the actual clear until our gainFocus()
222 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000223
reed@google.comaf951c92011-06-16 19:10:39 +0000224 // wrap the bitmap with a pixelref to expose our texture
225 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000226 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000227 } else {
228 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
229 width, height);
230 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000231 }
232}
233
234SkGpuDevice::~SkGpuDevice() {
235 if (fDrawProcs) {
236 delete fDrawProcs;
237 }
238
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000239 SkSafeUnref(fTexture);
240 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000241 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000242 GrAssert(NULL != fTexture);
243 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000244 fContext->unlockTexture(fCache);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000245 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000246 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000247}
248
reed@google.comac10a2d2010-12-22 21:39:39 +0000249///////////////////////////////////////////////////////////////////////////////
250
251void SkGpuDevice::makeRenderTargetCurrent() {
252 fContext->setRenderTarget(fRenderTarget);
253 fContext->flush(true);
254 fNeedPrepareRenderTarget = true;
255}
256
257///////////////////////////////////////////////////////////////////////////////
258
bsalomon@google.com910267d2011-11-02 20:06:25 +0000259bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap, int x, int y) {
260 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
261 SkASSERT(!bitmap.isNull());
262 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000263
bsalomon@google.com910267d2011-11-02 20:06:25 +0000264 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000265 return fContext->readRenderTargetPixels(fRenderTarget,
266 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000267 bitmap.width(),
268 bitmap.height(),
bsalomon@google.comc6980972011-11-02 19:57:21 +0000269 kRGBA_8888_GrPixelConfig,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000270 bitmap.getPixels(),
271 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000272}
273
274void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y) {
275 SkAutoLockPixels alp(bitmap);
276 if (!bitmap.readyToDraw()) {
277 return;
278 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000279 GrPixelConfig config = SkGr::BitmapConfig2PixelConfig(bitmap.config(),
280 bitmap.isOpaque());
reed@google.comac10a2d2010-12-22 21:39:39 +0000281 fContext->setRenderTarget(fRenderTarget);
282 // we aren't setting the clip or matrix, so mark as dirty
283 // we don't need to set them for this call and don't have them anyway
284 fNeedPrepareRenderTarget = true;
285
286 fContext->writePixels(x, y, bitmap.width(), bitmap.height(),
287 config, bitmap.getPixels(), bitmap.rowBytes());
288}
289
290///////////////////////////////////////////////////////////////////////////////
291
292static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000293 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000294 const SkRegion& clipRegion,
295 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000296 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000297
298 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000299 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000300 const SkIRect& skBounds = clipRegion.getBounds();
301 GrRect bounds;
302 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
303 GrIntToScalar(skBounds.fTop),
304 GrIntToScalar(skBounds.fRight),
305 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000306 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
307 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000308 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000309}
310
311// call this ever each draw call, to ensure that the context reflects our state,
312// and not the state from some other canvas/device
313void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
314 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000315 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000316
317 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000318 SkASSERT(draw.fClipStack);
319 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000320 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000321 fNeedPrepareRenderTarget = false;
322 }
323}
324
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000325void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
326 const SkClipStack& clipStack) {
327 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
328 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000329 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000330}
331
332void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000333 const SkRegion& clip, const SkClipStack& clipStack) {
334
reed@google.comac10a2d2010-12-22 21:39:39 +0000335 fContext->setRenderTarget(fRenderTarget);
336
bsalomon@google.comd302f142011-03-03 13:54:13 +0000337 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000338
reed@google.com6f8f2922011-03-04 22:27:10 +0000339 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000340
341 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000342 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000343 fNeedClear = false;
344 }
345}
346
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000347bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000349 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000350 return true;
351 }
352 return false;
353}
354
355///////////////////////////////////////////////////////////////////////////////
356
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000357SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
358SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
359SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
360SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
361SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
362 shader_type_mismatch);
363SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000364
bsalomon@google.com5782d712011-01-21 21:03:59 +0000365static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
366 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
367 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
368 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
369 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
370 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
371};
372
373bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
374 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000375 GrPaint* grPaint,
376 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000377
378 grPaint->fDither = skPaint.isDither();
379 grPaint->fAntiAlias = skPaint.isAntiAlias();
380
381 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
382 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
383
384 SkXfermode* mode = skPaint.getXfermode();
385 if (mode) {
386 if (!mode->asCoeff(&sm, &dm)) {
reed@google.com1a2e8d22011-01-21 22:08:29 +0000387 SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000388#if 0
389 return false;
390#endif
391 }
392 }
393 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
394 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
395
396 if (justAlpha) {
397 uint8_t alpha = skPaint.getAlpha();
398 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000399 // justAlpha is currently set to true only if there is a texture,
400 // so constantColor should not also be true.
401 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000402 } else {
403 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000404 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000405 }
Scroggo97c88c22011-05-11 14:05:25 +0000406 SkColorFilter* colorFilter = skPaint.getColorFilter();
407 SkColor color;
408 SkXfermode::Mode filterMode;
409 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000410 if (!constantColor) {
411 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
412 grPaint->fColorFilterXfermode = filterMode;
413 return true;
414 }
415 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
416 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000417 }
Scroggod757df22011-05-16 13:11:16 +0000418 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000419 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000420}
421
bsalomon@google.com5782d712011-01-21 21:03:59 +0000422bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
423 SkAutoCachedTexture* act,
424 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000425 GrPaint* grPaint,
426 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000427
bsalomon@google.com5782d712011-01-21 21:03:59 +0000428 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000429
bsalomon@google.com5782d712011-01-21 21:03:59 +0000430 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000431 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000432 return this->skPaint2GrPaintNoShader(skPaint,
433 false,
434 grPaint,
435 constantColor);
436 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000437 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000438 }
439
reed@google.comac10a2d2010-12-22 21:39:39 +0000440 SkBitmap bitmap;
441 SkMatrix matrix;
442 SkShader::TileMode tileModes[2];
443 SkScalar twoPointParams[3];
444 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
445 tileModes, twoPointParams);
446
bsalomon@google.com5782d712011-01-21 21:03:59 +0000447 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
448 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000449 SkShader::GradientInfo info;
450 SkColor color;
451
452 info.fColors = &color;
453 info.fColorOffsets = NULL;
454 info.fColorCount = 1;
455 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
456 SkPaint copy(skPaint);
457 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000458 // modulate the paint alpha by the shader's solid color alpha
459 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
460 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000461 return this->skPaint2GrPaintNoShader(copy,
462 false,
463 grPaint,
464 constantColor);
465 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000466 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000467 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000468 GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx);
469 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000470 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000471 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000472 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000473 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000474 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000475 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
476 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000477 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000478 sampler->setRadial2Params(twoPointParams[0],
479 twoPointParams[1],
480 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000481 }
482
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000483 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000484 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000485 SkDebugf("Couldn't convert bitmap to texture.\n");
486 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000487 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000488 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000489
490 // since our texture coords will be in local space, we wack the texture
491 // matrix to map them back into 0...1 before we load it
492 SkMatrix localM;
493 if (shader->getLocalMatrix(&localM)) {
494 SkMatrix inverse;
495 if (localM.invert(&inverse)) {
496 matrix.preConcat(inverse);
497 }
498 }
499 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000500 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
501 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000502 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000503 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000504 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000505 matrix.postScale(s, s);
506 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000507 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000508
509 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000510}
511
512///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000513
bsalomon@google.com398109c2011-04-14 18:40:27 +0000514void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000515 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000516}
517
reed@google.comac10a2d2010-12-22 21:39:39 +0000518void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
519 CHECK_SHOULD_DRAW(draw);
520
bsalomon@google.com5782d712011-01-21 21:03:59 +0000521 GrPaint grPaint;
522 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000523 if (!this->skPaint2GrPaintShader(paint,
524 &act,
525 *draw.fMatrix,
526 &grPaint,
527 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000528 return;
529 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000530
531 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000532}
533
534// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000535static const GrPrimitiveType gPointMode2PrimtiveType[] = {
536 kPoints_PrimitiveType,
537 kLines_PrimitiveType,
538 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000539};
540
541void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000542 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000543 CHECK_SHOULD_DRAW(draw);
544
545 SkScalar width = paint.getStrokeWidth();
546 if (width < 0) {
547 return;
548 }
549
550 // we only handle hairlines here, else we let the SkDraw call our drawPath()
551 if (width > 0) {
552 draw.drawPoints(mode, count, pts, paint, true);
553 return;
554 }
555
bsalomon@google.com5782d712011-01-21 21:03:59 +0000556 GrPaint grPaint;
557 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000558 if (!this->skPaint2GrPaintShader(paint,
559 &act,
560 *draw.fMatrix,
561 &grPaint,
562 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000563 return;
564 }
565
bsalomon@google.com5782d712011-01-21 21:03:59 +0000566 fContext->drawVertices(grPaint,
567 gPointMode2PrimtiveType[mode],
568 count,
569 (GrPoint*)pts,
570 NULL,
571 NULL,
572 NULL,
573 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000574}
575
reed@google.comc9aa5872011-04-05 21:05:37 +0000576///////////////////////////////////////////////////////////////////////////////
577
reed@google.comac10a2d2010-12-22 21:39:39 +0000578void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
579 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000580 CHECK_SHOULD_DRAW(draw);
581
bungeman@google.com79bd8772011-07-18 15:34:08 +0000582 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000583 SkScalar width = paint.getStrokeWidth();
584
585 /*
586 We have special code for hairline strokes, miter-strokes, and fills.
587 Anything else we just call our path code.
588 */
589 bool usePath = doStroke && width > 0 &&
590 paint.getStrokeJoin() != SkPaint::kMiter_Join;
591 // another reason we might need to call drawPath...
592 if (paint.getMaskFilter()) {
593 usePath = true;
594 }
reed@google.com67db6642011-05-26 11:46:35 +0000595 // until we aa rotated rects...
596 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
597 usePath = true;
598 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000599 // small miter limit means right angles show bevel...
600 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
601 paint.getStrokeMiter() < SK_ScalarSqrt2)
602 {
603 usePath = true;
604 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000605 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000606 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
607 usePath = true;
608 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000609
610 if (usePath) {
611 SkPath path;
612 path.addRect(rect);
613 this->drawPath(draw, path, paint, NULL, true);
614 return;
615 }
616
617 GrPaint grPaint;
618 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000619 if (!this->skPaint2GrPaintShader(paint,
620 &act,
621 *draw.fMatrix,
622 &grPaint,
623 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000624 return;
625 }
reed@google.com20efde72011-05-09 17:00:02 +0000626 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000627}
628
reed@google.com69302852011-02-16 18:08:07 +0000629#include "SkMaskFilter.h"
630#include "SkBounder.h"
631
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000632static GrPathFill skToGrFillType(SkPath::FillType fillType) {
633 switch (fillType) {
634 case SkPath::kWinding_FillType:
635 return kWinding_PathFill;
636 case SkPath::kEvenOdd_FillType:
637 return kEvenOdd_PathFill;
638 case SkPath::kInverseWinding_FillType:
639 return kInverseWinding_PathFill;
640 case SkPath::kInverseEvenOdd_FillType:
641 return kInverseEvenOdd_PathFill;
642 default:
643 SkDebugf("Unsupported path fill type\n");
644 return kHairLine_PathFill;
645 }
646}
647
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000648static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000649 int halfWidth = (kernelWidth - 1) / 2;
650 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000651 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000652 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000653 float x = static_cast<float>(i - halfWidth);
654 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
655 // is dropped here, since we renormalize the kernel below.
656 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000657 sum += kernel[i];
658 }
659 // Normalize the kernel
660 float scale = 1.0f / sum;
661 for (int i = 0; i < kernelWidth; ++i)
662 kernel[i] *= scale;
663}
664
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000665static void scaleRect(SkRect* rect, float scale) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000666 rect->fLeft *= scale;
667 rect->fTop *= scale;
668 rect->fRight *= scale;
669 rect->fBottom *= scale;
670}
671
672static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
673 SkMaskFilter* filter, const SkMatrix& matrix,
674 const SkRegion& clip, SkBounder* bounder,
675 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000676#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000677 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000678#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000679 SkMaskFilter::BlurInfo info;
680 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000681 if (SkMaskFilter::kNone_BlurType == blurType ||
682 !context->supportsShaders()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000683 return false;
684 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000685 SkScalar radius = info.fIgnoreTransform ? info.fRadius
686 : matrix.mapRadius(info.fRadius);
687 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000688 if (radius <= 0) {
689 return false;
690 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000691 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000692 SkRect srcRect = path.getBounds();
693
694 int scaleFactor = 1;
695
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000696 while (sigma > MAX_BLUR_SIGMA) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000697 scaleFactor *= 2;
698 sigma *= 0.5f;
699 }
senorblanco@chromium.org4a947d22011-07-18 21:48:35 +0000700 int halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000701 int kernelWidth = halfWidth * 2 + 1;
702
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000703 float invScale = 1.0f / scaleFactor;
704 scaleRect(&srcRect, invScale);
705 srcRect.roundOut();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000706 srcRect.inset(-halfWidth, -halfWidth);
707
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000708 SkRect clipBounds;
709 clipBounds.set(clip.getBounds());
710 scaleRect(&clipBounds, invScale);
711 clipBounds.roundOut();
712 clipBounds.inset(-halfWidth, -halfWidth);
713
714 srcRect.intersect(clipBounds);
715
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000716 scaleRect(&srcRect, scaleFactor);
717 SkRect finalRect = srcRect;
718
719 SkIRect finalIRect;
720 finalRect.roundOut(&finalIRect);
721 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000722 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000723 }
724 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000725 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000726 }
727 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
728 srcRect.offset(-srcRect.fLeft, -srcRect.fTop);
729 const GrTextureDesc desc = {
730 kRenderTarget_GrTextureFlagBit,
731 kNone_GrAALevel,
732 srcRect.width(),
733 srcRect.height(),
734 // We actually only need A8, but it often isn't supported as a
735 // render target
736 kRGBA_8888_GrPixelConfig
737 };
738
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000739 GrAutoScratchTexture srcEntry(context, desc);
740 GrAutoScratchTexture dstEntry(context, desc);
741 if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000742 return false;
743 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000744 GrTexture* srcTexture = srcEntry.texture();
745 GrTexture* dstTexture = dstEntry.texture();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000746 if (NULL == srcTexture || NULL == dstTexture) {
747 return false;
748 }
749 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000750 // Once this code moves into GrContext, this should be changed to use
751 // an AutoClipRestore.
752 GrClip oldClip = context->getClip();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000753 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000754 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000755 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000756 GrPaint tempPaint;
757 tempPaint.reset();
758
759 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000760 tempPaint.fAntiAlias = grp->fAntiAlias;
761 if (tempPaint.fAntiAlias) {
762 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
763 // blend coeff of zero requires dual source blending support in order
764 // to properly blend partially covered pixels. This means the AA
765 // code path may not be taken. So we use a dst blend coeff of ISA. We
766 // could special case AA draws to a dst surface with known alpha=0 to
767 // use a zero dst coeff when dual source blending isn't available.
768 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
769 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
770 }
771 // Draw hard shadow to dstTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000772 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000773 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000774
775 GrMatrix sampleM;
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000776 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000777 GrPaint paint;
778 paint.reset();
779 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
780 paint.getTextureSampler(0)->setMatrix(sampleM);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000781 GrAutoScratchTexture origEntry;
782
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000783 if (blurType != SkMaskFilter::kNormal_BlurType) {
784 // Stash away a copy of the unblurred image.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000785 origEntry.set(context, desc);
786 if (NULL == origEntry.texture()) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000787 return false;
788 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000789 context->setRenderTarget(origEntry.texture()->asRenderTarget());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000790 paint.setTexture(0, srcTexture);
791 context->drawRect(paint, srcRect);
792 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000793 for (int i = 1; i < scaleFactor; i *= 2) {
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000794 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
795 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000796 context->setRenderTarget(dstTexture->asRenderTarget());
797 SkRect dstRect(srcRect);
798 scaleRect(&dstRect, 0.5f);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000799 paint.setTexture(0, srcTexture);
800 context->drawRectToRect(paint, dstRect, srcRect);
801 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000802 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000803 }
804
805 SkAutoTMalloc<float> kernelStorage(kernelWidth);
806 float* kernel = kernelStorage.get();
807 buildKernel(sigma, kernel, kernelWidth);
808
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000809 // Clear out a halfWidth to the right of the srcRect to prevent the
810 // X convolution from reading garbage.
811 SkIRect clearRect = SkIRect::MakeXYWH(
812 srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height());
813 context->clear(&clearRect, 0x0);
814
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000815 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000816 context->convolveInX(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000817 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000819 // Clear out a halfWidth below the srcRect to prevent the Y
820 // convolution from reading garbage.
821 clearRect = SkIRect::MakeXYWH(
822 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth);
823 context->clear(&clearRect, 0x0);
824
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000825 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000826 context->convolveInY(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000827 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000828
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000829 // Clear one pixel to the right and below, to accommodate bilinear
830 // upsampling.
831 clearRect = SkIRect::MakeXYWH(
832 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
833 context->clear(&clearRect, 0x0);
834 clearRect = SkIRect::MakeXYWH(
835 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
836 context->clear(&clearRect, 0x0);
837
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000838 if (scaleFactor > 1) {
839 // FIXME: This should be mitchell, not bilinear.
840 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000841 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000842 paint.getTextureSampler(0)->setMatrix(sampleM);
843 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000844 paint.setTexture(0, srcTexture);
845 SkRect dstRect(srcRect);
846 scaleRect(&dstRect, scaleFactor);
847 context->drawRectToRect(paint, dstRect, srcRect);
848 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000849 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000850 }
851
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000852 if (blurType != SkMaskFilter::kNormal_BlurType) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000853 GrTexture* origTexture = origEntry.texture();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000854 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000855 sampleM.setIDiv(origTexture->width(), origTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000856 paint.getTextureSampler(0)->setMatrix(sampleM);
857 // Blend origTexture over srcTexture.
858 context->setRenderTarget(srcTexture->asRenderTarget());
859 paint.setTexture(0, origTexture);
860 if (SkMaskFilter::kInner_BlurType == blurType) {
861 // inner: dst = dst * src
862 paint.fSrcBlendCoeff = kDC_BlendCoeff;
863 paint.fDstBlendCoeff = kZero_BlendCoeff;
864 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
865 // solid: dst = src + dst - src * dst
866 // = (1 - dst) * src + 1 * dst
867 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
868 paint.fDstBlendCoeff = kOne_BlendCoeff;
869 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
870 // outer: dst = dst * (1 - src)
871 // = 0 * src + (1 - src) * dst
872 paint.fSrcBlendCoeff = kZero_BlendCoeff;
873 paint.fDstBlendCoeff = kISC_BlendCoeff;
874 }
875 context->drawRect(paint, srcRect);
876 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000877 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000878 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000879
880 if (grp->hasTextureOrMask()) {
881 GrMatrix inverse;
882 if (!matrix.invert(&inverse)) {
883 return false;
884 }
885 grp->preConcatActiveSamplerMatrices(inverse);
886 }
887
888 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
889 // we assume the last mask index is available for use
890 GrAssert(NULL == grp->getMask(MASK_IDX));
891 grp->setMask(MASK_IDX, srcTexture);
892 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
893
894 GrMatrix m;
895 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
896 m.postIDiv(srcTexture->width(), srcTexture->height());
897 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
898 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000899 return true;
900}
901
reed@google.com69302852011-02-16 18:08:07 +0000902static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
903 SkMaskFilter* filter, const SkMatrix& matrix,
904 const SkRegion& clip, SkBounder* bounder,
905 GrPaint* grp) {
906 SkMask srcM, dstM;
907
908 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
909 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
910 return false;
911 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000912 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000913
914 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
915 return false;
916 }
917 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000918 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000919
920 if (clip.quickReject(dstM.fBounds)) {
921 return false;
922 }
923 if (bounder && !bounder->doIRect(dstM.fBounds)) {
924 return false;
925 }
926
927 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
928 // the current clip (and identity matrix) and grpaint settings
929
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000930 // used to compute inverse view, if necessary
931 GrMatrix ivm = context->getMatrix();
932
reed@google.com0c219b62011-02-16 21:31:18 +0000933 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000934
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000935 const GrTextureDesc desc = {
936 kNone_GrTextureFlags,
937 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +0000938 dstM.fBounds.width(),
939 dstM.fBounds.height(),
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000940 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +0000941 };
942
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000943 GrAutoScratchTexture ast(context, desc);
944 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000945
reed@google.com69302852011-02-16 18:08:07 +0000946 if (NULL == texture) {
947 return false;
948 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000949 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
950 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000951
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000952 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
953 grp->preConcatActiveSamplerMatrices(ivm);
954 }
955
956 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
957 // we assume the last mask index is available for use
958 GrAssert(NULL == grp->getMask(MASK_IDX));
959 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000960 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +0000961
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000962 GrRect d;
963 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000964 GrIntToScalar(dstM.fBounds.fTop),
965 GrIntToScalar(dstM.fBounds.fRight),
966 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000967
968 GrMatrix m;
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000969 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
970 -dstM.fBounds.fTop*SK_Scalar1);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000971 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000972 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
973
974 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000975 return true;
976}
reed@google.com69302852011-02-16 18:08:07 +0000977
reed@google.com0c219b62011-02-16 21:31:18 +0000978void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comfe626382011-09-21 13:50:35 +0000979 const SkPaint& origPaint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000980 bool pathIsMutable) {
981 CHECK_SHOULD_DRAW(draw);
982
reed@google.comfe626382011-09-21 13:50:35 +0000983 bool doFill = true;
984 SkTLazy<SkPaint> lazyPaint;
985 const SkPaint* paint = &origPaint;
986
987 // can we cheat, and threat a thin stroke as a hairline (w/ modulated alpha)
988 // if we can, we draw lots faster (raster device does this same test)
989 {
990 SkAlpha newAlpha;
991 if (SkDrawTreatAsHairline(*paint, *draw.fMatrix, &newAlpha)) {
992 lazyPaint.set(*paint);
993 lazyPaint.get()->setAlpha(newAlpha);
994 lazyPaint.get()->setStrokeWidth(0);
995 paint = lazyPaint.get();
996 doFill = false;
997 }
998 }
999 // must reference paint from here down, and not origPaint
1000 // since we may have change the paint (using lazyPaint for storage)
1001
bsalomon@google.com5782d712011-01-21 21:03:59 +00001002 GrPaint grPaint;
1003 SkAutoCachedTexture act;
reed@google.comfe626382011-09-21 13:50:35 +00001004 if (!this->skPaint2GrPaintShader(*paint,
Scroggod757df22011-05-16 13:11:16 +00001005 &act,
1006 *draw.fMatrix,
1007 &grPaint,
1008 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001009 return;
1010 }
1011
reed@google.comfe626382011-09-21 13:50:35 +00001012 // If we have a prematrix, apply it to the path, optimizing for the case
1013 // where the original path can in fact be modified in place (even though
1014 // its parameter type is const).
1015 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1016 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001017
1018 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001019 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001020
reed@google.come3445642011-02-16 23:20:39 +00001021 if (!pathIsMutable) {
1022 result = &tmpPath;
1023 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001024 }
reed@google.come3445642011-02-16 23:20:39 +00001025 // should I push prePathMatrix on our MV stack temporarily, instead
1026 // of applying it here? See SkDraw.cpp
1027 pathPtr->transform(*prePathMatrix, result);
1028 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001029 }
reed@google.com0c219b62011-02-16 21:31:18 +00001030 // at this point we're done with prePathMatrix
1031 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001032
reed@google.comfe626382011-09-21 13:50:35 +00001033 if (doFill && (paint->getPathEffect() ||
1034 paint->getStyle() != SkPaint::kFill_Style)) {
1035 // it is safe to use tmpPath here, even if we already used it for the
1036 // prepathmatrix, since getFillPath can take the same object for its
1037 // input and output safely.
1038 doFill = paint->getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001039 pathPtr = &tmpPath;
1040 }
1041
reed@google.comfe626382011-09-21 13:50:35 +00001042 if (paint->getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001043 // avoid possibly allocating a new path in transform if we can
1044 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1045
1046 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001047 pathPtr->transform(*draw.fMatrix, devPathPtr);
reed@google.comfe626382011-09-21 13:50:35 +00001048 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001049 *draw.fMatrix, *draw.fClip, draw.fBounder,
1050 &grPaint)) {
reed@google.comfe626382011-09-21 13:50:35 +00001051 drawWithMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001052 *draw.fMatrix, *draw.fClip, draw.fBounder,
1053 &grPaint);
1054 }
reed@google.com69302852011-02-16 18:08:07 +00001055 return;
1056 }
reed@google.com69302852011-02-16 18:08:07 +00001057
bsalomon@google.comffca4002011-02-22 20:34:01 +00001058 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001059
reed@google.com0c219b62011-02-16 21:31:18 +00001060 if (doFill) {
1061 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001062 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001063 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 break;
1065 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001066 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001067 break;
1068 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001069 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001070 break;
1071 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001072 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001073 break;
1074 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001075 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001076 return;
1077 }
1078 }
1079
reed@google.com07f3ee12011-05-16 17:21:57 +00001080 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001081}
1082
reed@google.comac10a2d2010-12-22 21:39:39 +00001083void SkGpuDevice::drawBitmap(const SkDraw& draw,
1084 const SkBitmap& bitmap,
1085 const SkIRect* srcRectPtr,
1086 const SkMatrix& m,
1087 const SkPaint& paint) {
1088 CHECK_SHOULD_DRAW(draw);
1089
1090 SkIRect srcRect;
1091 if (NULL == srcRectPtr) {
1092 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1093 } else {
1094 srcRect = *srcRectPtr;
1095 }
1096
junov@google.comd935cfb2011-06-27 20:48:23 +00001097 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001098 // Convert the bitmap to a shader so that the rect can be drawn
1099 // through drawRect, which supports mask filters.
1100 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001101 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001102 if (srcRectPtr) {
1103 if (!bitmap.extractSubset(&tmp, srcRect)) {
1104 return; // extraction failed
1105 }
1106 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001107 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001108 }
1109 SkPaint paintWithTexture(paint);
1110 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1111 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001112 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001113 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001114
junov@google.com1d329782011-07-28 20:10:09 +00001115 // Transform 'm' needs to be concatenated to the draw matrix,
1116 // rather than transforming the primitive directly, so that 'm' will
1117 // also affect the behavior of the mask filter.
1118 SkMatrix drawMatrix;
1119 drawMatrix.setConcat(*draw.fMatrix, m);
1120 SkDraw transformedDraw(draw);
1121 transformedDraw.fMatrix = &drawMatrix;
1122
1123 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1124
junov@google.comd935cfb2011-06-27 20:48:23 +00001125 return;
1126 }
1127
bsalomon@google.com5782d712011-01-21 21:03:59 +00001128 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001129 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001130 return;
1131 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001132 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001133 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001134 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001135 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001136 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001137 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001138
bsalomon@google.com91958362011-06-13 17:58:13 +00001139 const int maxTextureSize = fContext->getMaxTextureSize();
1140 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1141 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001142 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001143 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001144 return;
1145 }
1146
1147 // undo the translate done by SkCanvas
1148 int DX = SkMax32(0, srcRect.fLeft);
1149 int DY = SkMax32(0, srcRect.fTop);
1150 // compute clip bounds in local coordinates
1151 SkIRect clipRect;
1152 {
1153 SkRect r;
1154 r.set(draw.fClip->getBounds());
1155 SkMatrix matrix, inverse;
1156 matrix.setConcat(*draw.fMatrix, m);
1157 if (!matrix.invert(&inverse)) {
1158 return;
1159 }
1160 inverse.mapRect(&r);
1161 r.roundOut(&clipRect);
1162 // apply the canvas' translate to our local clip
1163 clipRect.offset(DX, DY);
1164 }
1165
bsalomon@google.com91958362011-06-13 17:58:13 +00001166 int nx = bitmap.width() / maxTextureSize;
1167 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001168 for (int x = 0; x <= nx; x++) {
1169 for (int y = 0; y <= ny; y++) {
1170 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001171 tileR.set(x * maxTextureSize, y * maxTextureSize,
1172 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001173 if (!SkIRect::Intersects(tileR, clipRect)) {
1174 continue;
1175 }
1176
1177 SkIRect srcR = tileR;
1178 if (!srcR.intersect(srcRect)) {
1179 continue;
1180 }
1181
1182 SkBitmap tmpB;
1183 if (bitmap.extractSubset(&tmpB, tileR)) {
1184 // now offset it to make it "local" to our tmp bitmap
1185 srcR.offset(-tileR.fLeft, -tileR.fTop);
1186
1187 SkMatrix tmpM(m);
1188 {
1189 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1190 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1191 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1192 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001193 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001194 }
1195 }
1196 }
1197}
1198
1199/*
1200 * This is called by drawBitmap(), which has to handle images that may be too
1201 * large to be represented by a single texture.
1202 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001203 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1204 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001205 */
1206void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1207 const SkBitmap& bitmap,
1208 const SkIRect& srcRect,
1209 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001210 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001211 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1212 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001213
reed@google.com9c49bc32011-07-07 13:42:37 +00001214 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001215 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001216 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001217 return;
1218 }
1219
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001220 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1221
1222 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1223 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1224 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1225 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001226
1227 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001228 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001229 if (NULL == texture) {
1230 return;
1231 }
1232
bsalomon@google.com452943d2011-10-31 17:37:14 +00001233 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001234
reed@google.com20efde72011-05-09 17:00:02 +00001235 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1236 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001237 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001238 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1239 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1240 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001241 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001242
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001243 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001244 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001245 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001246 // If drawing a subrect of the bitmap and filtering is enabled,
1247 // use a constrained texture domain to avoid color bleeding
1248 GrScalar left, top, right, bottom;
1249 if (srcRect.width() > 1) {
1250 GrScalar border = GR_ScalarHalf / bitmap.width();
1251 left = paintRect.left() + border;
1252 right = paintRect.right() - border;
1253 } else {
1254 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1255 }
1256 if (srcRect.height() > 1) {
1257 GrScalar border = GR_ScalarHalf / bitmap.height();
1258 top = paintRect.top() + border;
1259 bottom = paintRect.bottom() - border;
1260 } else {
1261 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1262 }
1263 GrRect textureDomain;
1264 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001265 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001266 }
1267
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001268 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001269}
1270
1271void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1272 int left, int top, const SkPaint& paint) {
1273 CHECK_SHOULD_DRAW(draw);
1274
1275 SkAutoLockPixels alp(bitmap);
1276 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1277 return;
1278 }
1279
bsalomon@google.com5782d712011-01-21 21:03:59 +00001280 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001281 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001282 return;
1283 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001284
bsalomon@google.com5782d712011-01-21 21:03:59 +00001285 GrAutoMatrix avm(fContext, GrMatrix::I());
1286
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001287 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001288
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001289 GrTexture* texture;
1290 sampler->setClampNoFilter();
1291 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1292
1293 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001294
bsalomon@google.com5782d712011-01-21 21:03:59 +00001295 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001296 GrRect::MakeXYWH(GrIntToScalar(left),
1297 GrIntToScalar(top),
1298 GrIntToScalar(bitmap.width()),
1299 GrIntToScalar(bitmap.height())),
1300 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001301}
1302
1303void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1304 int x, int y, const SkPaint& paint) {
1305 CHECK_SHOULD_DRAW(draw);
1306
bsalomon@google.com5782d712011-01-21 21:03:59 +00001307 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001308 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001309 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001310 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001311 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001312
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001313 GrTexture* devTex = grPaint.getTexture(0);
1314 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001315
1316 const SkBitmap& bm = dev->accessBitmap(false);
1317 int w = bm.width();
1318 int h = bm.height();
1319
1320 GrAutoMatrix avm(fContext, GrMatrix::I());
1321
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001322 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001323
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001324 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1325 GrIntToScalar(y),
1326 GrIntToScalar(w),
1327 GrIntToScalar(h));
1328 // The device being drawn may not fill up its texture (saveLayer uses
1329 // the approximate ).
1330 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1331 GR_Scalar1 * h / devTex->height());
1332
1333 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001334}
1335
1336///////////////////////////////////////////////////////////////////////////////
1337
1338// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001339static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1340 kTriangles_PrimitiveType,
1341 kTriangleStrip_PrimitiveType,
1342 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001343};
1344
1345void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1346 int vertexCount, const SkPoint vertices[],
1347 const SkPoint texs[], const SkColor colors[],
1348 SkXfermode* xmode,
1349 const uint16_t indices[], int indexCount,
1350 const SkPaint& paint) {
1351 CHECK_SHOULD_DRAW(draw);
1352
bsalomon@google.com5782d712011-01-21 21:03:59 +00001353 GrPaint grPaint;
1354 SkAutoCachedTexture act;
1355 // we ignore the shader if texs is null.
1356 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001357 if (!this->skPaint2GrPaintNoShader(paint,
1358 false,
1359 &grPaint,
1360 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001361 return;
1362 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001363 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001364 if (!this->skPaint2GrPaintShader(paint, &act,
1365 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001366 &grPaint,
1367 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001368 return;
1369 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001370 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001371
1372 if (NULL != xmode && NULL != texs && NULL != colors) {
1373 SkXfermode::Mode mode;
1374 if (!SkXfermode::IsMode(xmode, &mode) ||
1375 SkXfermode::kMultiply_Mode != mode) {
1376 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1377#if 0
1378 return
1379#endif
1380 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001381 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001382
bsalomon@google.com498776a2011-08-16 19:20:44 +00001383 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1384 if (NULL != colors) {
1385 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001386 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001387 for (int i = 0; i < vertexCount; ++i) {
1388 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1389 }
1390 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001391 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001392 fContext->drawVertices(grPaint,
1393 gVertexMode2PrimitiveType[vmode],
1394 vertexCount,
1395 (GrPoint*) vertices,
1396 (GrPoint*) texs,
1397 colors,
1398 indices,
1399 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001400}
1401
1402///////////////////////////////////////////////////////////////////////////////
1403
1404static void GlyphCacheAuxProc(void* data) {
1405 delete (GrFontScaler*)data;
1406}
1407
1408static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1409 void* auxData;
1410 GrFontScaler* scaler = NULL;
1411 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1412 scaler = (GrFontScaler*)auxData;
1413 }
1414 if (NULL == scaler) {
1415 scaler = new SkGrFontScaler(cache);
1416 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1417 }
1418 return scaler;
1419}
1420
1421static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1422 SkFixed fx, SkFixed fy,
1423 const SkGlyph& glyph) {
1424 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1425
1426 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1427
1428 if (NULL == procs->fFontScaler) {
1429 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1430 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001431
1432 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001433 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001434 *
reed@google.com3b139f52011-06-07 17:56:25 +00001435 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1436 * It calls that rather than round, because our caller has already added
1437 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1438 *
1439 * Test code between raster and gpu (they should draw the same)
1440 *
1441 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1442 *
1443 * Perhaps we should only perform this integralization if there is no
1444 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001445 */
reed@google.com3b139f52011-06-07 17:56:25 +00001446 fy = SkFixedFloorToFixed(fy);
1447
reed@google.comac10a2d2010-12-22 21:39:39 +00001448 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001449 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001450 procs->fFontScaler);
1451}
1452
bsalomon@google.com5782d712011-01-21 21:03:59 +00001453SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001454
1455 // deferred allocation
1456 if (NULL == fDrawProcs) {
1457 fDrawProcs = new GrSkDrawProcs;
1458 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1459 fDrawProcs->fContext = fContext;
1460 }
1461
1462 // init our (and GL's) state
1463 fDrawProcs->fTextContext = context;
1464 fDrawProcs->fFontScaler = NULL;
1465 return fDrawProcs;
1466}
1467
1468void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1469 size_t byteLength, SkScalar x, SkScalar y,
1470 const SkPaint& paint) {
1471 CHECK_SHOULD_DRAW(draw);
1472
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001473 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001474 // this guy will just call our drawPath()
1475 draw.drawText((const char*)text, byteLength, x, y, paint);
1476 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001477 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001478
1479 GrPaint grPaint;
1480 SkAutoCachedTexture act;
1481
Scroggod757df22011-05-16 13:11:16 +00001482 if (!this->skPaint2GrPaintShader(paint,
1483 &act,
1484 *draw.fMatrix,
1485 &grPaint,
1486 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001487 return;
1488 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001489 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001490 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001491 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1492 }
1493}
1494
1495void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1496 size_t byteLength, const SkScalar pos[],
1497 SkScalar constY, int scalarsPerPos,
1498 const SkPaint& paint) {
1499 CHECK_SHOULD_DRAW(draw);
1500
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001501 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001502 // this guy will just call our drawPath()
1503 draw.drawPosText((const char*)text, byteLength, pos, constY,
1504 scalarsPerPos, paint);
1505 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001506 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001507
1508 GrPaint grPaint;
1509 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001510 if (!this->skPaint2GrPaintShader(paint,
1511 &act,
1512 *draw.fMatrix,
1513 &grPaint,
1514 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001515 return;
1516 }
1517
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001518 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001519 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001520 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1521 scalarsPerPos, paint);
1522 }
1523}
1524
1525void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1526 size_t len, const SkPath& path,
1527 const SkMatrix* m, const SkPaint& paint) {
1528 CHECK_SHOULD_DRAW(draw);
1529
1530 SkASSERT(draw.fDevice == this);
1531 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1532}
1533
1534///////////////////////////////////////////////////////////////////////////////
1535
reed@google.comf67e4cf2011-03-15 20:56:58 +00001536bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1537 if (!paint.isLCDRenderText()) {
1538 // we're cool with the paint as is
1539 return false;
1540 }
1541
1542 if (paint.getShader() ||
1543 paint.getXfermode() || // unless its srcover
1544 paint.getMaskFilter() ||
1545 paint.getRasterizer() ||
1546 paint.getColorFilter() ||
1547 paint.getPathEffect() ||
1548 paint.isFakeBoldText() ||
1549 paint.getStyle() != SkPaint::kFill_Style) {
1550 // turn off lcd
1551 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1552 flags->fHinting = paint.getHinting();
1553 return true;
1554 }
1555 // we're cool with the paint as is
1556 return false;
1557}
1558
1559///////////////////////////////////////////////////////////////////////////////
1560
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001561SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1562 const GrSamplerState& sampler,
1563 TexType type) {
1564 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001565 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001566
bsalomon@google.come97f0852011-06-17 13:10:25 +00001567 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001568 const GrTextureDesc desc = {
1569 kRenderTarget_GrTextureFlagBit,
1570 kNone_GrAALevel,
1571 bitmap.width(),
1572 bitmap.height(),
1573 SkGr::Bitmap2PixelConfig(bitmap)
1574 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001575 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001576 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001577 // we know layers will only be drawn through drawDevice.
1578 // drawDevice has been made to work with content embedded in a
1579 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001580 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001581 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001582 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001583 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001584 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001585 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001586 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001587 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001588 GrContext::TextureKey key = bitmap.getGenerationID();
1589 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001590
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001591 entry = ctx->findAndLockTexture(key, bitmap.width(),
1592 bitmap.height(), sampler);
1593 if (NULL == entry.texture()) {
1594 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001595 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001596 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001597 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001598 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001599 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001600 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001601 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1602 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001603 }
1604 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001605 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001606}
1607
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001608void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1609 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001610}
1611
bsalomon@google.come97f0852011-06-17 13:10:25 +00001612SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1613 int width, int height,
1614 bool isOpaque,
1615 Usage usage) {
1616 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1617 width, height, usage));
1618}
1619