blob: 9923c8c7b4c3ad18dba83956212a10c1368809c5 [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
259bool SkGpuDevice::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
260 SkIRect bounds;
261 bounds.set(0, 0, this->width(), this->height());
262 if (!bounds.intersect(srcRect)) {
263 return false;
264 }
265
266 const int w = bounds.width();
267 const int h = bounds.height();
268 SkBitmap tmp;
269 // note we explicitly specify our rowBytes to be snug (no gap between rows)
270 tmp.setConfig(SkBitmap::kARGB_8888_Config, w, h, w * 4);
271 if (!tmp.allocPixels()) {
272 return false;
273 }
274
Scroggo813c33c2011-04-07 20:56:21 +0000275 tmp.lockPixels();
reed@google.comac10a2d2010-12-22 21:39:39 +0000276
Scroggoeb176032011-04-07 21:11:49 +0000277 bool read = fContext->readRenderTargetPixels(fRenderTarget,
278 bounds.fLeft, bounds.fTop,
279 bounds.width(), bounds.height(),
280 kRGBA_8888_GrPixelConfig,
281 tmp.getPixels());
Scroggo813c33c2011-04-07 20:56:21 +0000282 tmp.unlockPixels();
283 if (!read) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000284 return false;
285 }
286
287 tmp.swap(*bitmap);
288 return true;
289}
290
291void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y) {
292 SkAutoLockPixels alp(bitmap);
293 if (!bitmap.readyToDraw()) {
294 return;
295 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000296 GrPixelConfig config = SkGr::BitmapConfig2PixelConfig(bitmap.config(),
297 bitmap.isOpaque());
reed@google.comac10a2d2010-12-22 21:39:39 +0000298 fContext->setRenderTarget(fRenderTarget);
299 // we aren't setting the clip or matrix, so mark as dirty
300 // we don't need to set them for this call and don't have them anyway
301 fNeedPrepareRenderTarget = true;
302
303 fContext->writePixels(x, y, bitmap.width(), bitmap.height(),
304 config, bitmap.getPixels(), bitmap.rowBytes());
305}
306
307///////////////////////////////////////////////////////////////////////////////
308
309static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000310 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000311 const SkRegion& clipRegion,
312 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000313 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000314
315 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000316 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000317 const SkIRect& skBounds = clipRegion.getBounds();
318 GrRect bounds;
319 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
320 GrIntToScalar(skBounds.fTop),
321 GrIntToScalar(skBounds.fRight),
322 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000323 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
324 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000325 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000326}
327
328// call this ever each draw call, to ensure that the context reflects our state,
329// and not the state from some other canvas/device
330void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
331 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000332 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000333
334 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000335 SkASSERT(draw.fClipStack);
336 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000337 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000338 fNeedPrepareRenderTarget = false;
339 }
340}
341
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000342void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
343 const SkClipStack& clipStack) {
344 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
345 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000346 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000347}
348
349void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000350 const SkRegion& clip, const SkClipStack& clipStack) {
351
reed@google.comac10a2d2010-12-22 21:39:39 +0000352 fContext->setRenderTarget(fRenderTarget);
353
bsalomon@google.comd302f142011-03-03 13:54:13 +0000354 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000355
reed@google.com6f8f2922011-03-04 22:27:10 +0000356 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000357
358 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000359 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000360 fNeedClear = false;
361 }
362}
363
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000364bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000365 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000366 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000367 return true;
368 }
369 return false;
370}
371
372///////////////////////////////////////////////////////////////////////////////
373
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000374SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
375SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
376SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
377SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
378SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
379 shader_type_mismatch);
380SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000381
bsalomon@google.com5782d712011-01-21 21:03:59 +0000382static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
383 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
384 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
385 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
386 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
387 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
388};
389
390bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
391 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000392 GrPaint* grPaint,
393 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000394
395 grPaint->fDither = skPaint.isDither();
396 grPaint->fAntiAlias = skPaint.isAntiAlias();
397
398 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
399 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
400
401 SkXfermode* mode = skPaint.getXfermode();
402 if (mode) {
403 if (!mode->asCoeff(&sm, &dm)) {
reed@google.com1a2e8d22011-01-21 22:08:29 +0000404 SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000405#if 0
406 return false;
407#endif
408 }
409 }
410 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
411 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
412
413 if (justAlpha) {
414 uint8_t alpha = skPaint.getAlpha();
415 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000416 // justAlpha is currently set to true only if there is a texture,
417 // so constantColor should not also be true.
418 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000419 } else {
420 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000421 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000422 }
Scroggo97c88c22011-05-11 14:05:25 +0000423 SkColorFilter* colorFilter = skPaint.getColorFilter();
424 SkColor color;
425 SkXfermode::Mode filterMode;
426 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000427 if (!constantColor) {
428 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
429 grPaint->fColorFilterXfermode = filterMode;
430 return true;
431 }
432 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
433 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000434 }
Scroggod757df22011-05-16 13:11:16 +0000435 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000436 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000437}
438
bsalomon@google.com5782d712011-01-21 21:03:59 +0000439bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
440 SkAutoCachedTexture* act,
441 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000442 GrPaint* grPaint,
443 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000444
bsalomon@google.com5782d712011-01-21 21:03:59 +0000445 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000446
bsalomon@google.com5782d712011-01-21 21:03:59 +0000447 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000448 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000449 return this->skPaint2GrPaintNoShader(skPaint,
450 false,
451 grPaint,
452 constantColor);
453 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000454 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000455 }
456
reed@google.comac10a2d2010-12-22 21:39:39 +0000457 SkBitmap bitmap;
458 SkMatrix matrix;
459 SkShader::TileMode tileModes[2];
460 SkScalar twoPointParams[3];
461 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
462 tileModes, twoPointParams);
463
bsalomon@google.com5782d712011-01-21 21:03:59 +0000464 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
465 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000466 SkShader::GradientInfo info;
467 SkColor color;
468
469 info.fColors = &color;
470 info.fColorOffsets = NULL;
471 info.fColorCount = 1;
472 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
473 SkPaint copy(skPaint);
474 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000475 // modulate the paint alpha by the shader's solid color alpha
476 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
477 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000478 return this->skPaint2GrPaintNoShader(copy,
479 false,
480 grPaint,
481 constantColor);
482 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000483 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000484 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000485 GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx);
486 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000487 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000488 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000489 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000490 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000491 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000492 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
493 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000495 sampler->setRadial2Params(twoPointParams[0],
496 twoPointParams[1],
497 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000498 }
499
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000500 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000501 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000502 SkDebugf("Couldn't convert bitmap to texture.\n");
503 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000504 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000505 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000506
507 // since our texture coords will be in local space, we wack the texture
508 // matrix to map them back into 0...1 before we load it
509 SkMatrix localM;
510 if (shader->getLocalMatrix(&localM)) {
511 SkMatrix inverse;
512 if (localM.invert(&inverse)) {
513 matrix.preConcat(inverse);
514 }
515 }
516 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000517 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
518 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000519 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000520 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000521 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000522 matrix.postScale(s, s);
523 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000524 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000525
526 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000527}
528
529///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000530
bsalomon@google.com398109c2011-04-14 18:40:27 +0000531void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000532 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000533}
534
reed@google.comac10a2d2010-12-22 21:39:39 +0000535void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
536 CHECK_SHOULD_DRAW(draw);
537
bsalomon@google.com5782d712011-01-21 21:03:59 +0000538 GrPaint grPaint;
539 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000540 if (!this->skPaint2GrPaintShader(paint,
541 &act,
542 *draw.fMatrix,
543 &grPaint,
544 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000545 return;
546 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000547
548 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000549}
550
551// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000552static const GrPrimitiveType gPointMode2PrimtiveType[] = {
553 kPoints_PrimitiveType,
554 kLines_PrimitiveType,
555 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000556};
557
558void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000559 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000560 CHECK_SHOULD_DRAW(draw);
561
562 SkScalar width = paint.getStrokeWidth();
563 if (width < 0) {
564 return;
565 }
566
567 // we only handle hairlines here, else we let the SkDraw call our drawPath()
568 if (width > 0) {
569 draw.drawPoints(mode, count, pts, paint, true);
570 return;
571 }
572
bsalomon@google.com5782d712011-01-21 21:03:59 +0000573 GrPaint grPaint;
574 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000575 if (!this->skPaint2GrPaintShader(paint,
576 &act,
577 *draw.fMatrix,
578 &grPaint,
579 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000580 return;
581 }
582
bsalomon@google.com5782d712011-01-21 21:03:59 +0000583 fContext->drawVertices(grPaint,
584 gPointMode2PrimtiveType[mode],
585 count,
586 (GrPoint*)pts,
587 NULL,
588 NULL,
589 NULL,
590 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000591}
592
reed@google.comc9aa5872011-04-05 21:05:37 +0000593///////////////////////////////////////////////////////////////////////////////
594
reed@google.comac10a2d2010-12-22 21:39:39 +0000595void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
596 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000597 CHECK_SHOULD_DRAW(draw);
598
bungeman@google.com79bd8772011-07-18 15:34:08 +0000599 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000600 SkScalar width = paint.getStrokeWidth();
601
602 /*
603 We have special code for hairline strokes, miter-strokes, and fills.
604 Anything else we just call our path code.
605 */
606 bool usePath = doStroke && width > 0 &&
607 paint.getStrokeJoin() != SkPaint::kMiter_Join;
608 // another reason we might need to call drawPath...
609 if (paint.getMaskFilter()) {
610 usePath = true;
611 }
reed@google.com67db6642011-05-26 11:46:35 +0000612 // until we aa rotated rects...
613 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
614 usePath = true;
615 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000616 // small miter limit means right angles show bevel...
617 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
618 paint.getStrokeMiter() < SK_ScalarSqrt2)
619 {
620 usePath = true;
621 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000622 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000623 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
624 usePath = true;
625 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000626
627 if (usePath) {
628 SkPath path;
629 path.addRect(rect);
630 this->drawPath(draw, path, paint, NULL, true);
631 return;
632 }
633
634 GrPaint grPaint;
635 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000636 if (!this->skPaint2GrPaintShader(paint,
637 &act,
638 *draw.fMatrix,
639 &grPaint,
640 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000641 return;
642 }
reed@google.com20efde72011-05-09 17:00:02 +0000643 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000644}
645
reed@google.com69302852011-02-16 18:08:07 +0000646#include "SkMaskFilter.h"
647#include "SkBounder.h"
648
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000649static GrPathFill skToGrFillType(SkPath::FillType fillType) {
650 switch (fillType) {
651 case SkPath::kWinding_FillType:
652 return kWinding_PathFill;
653 case SkPath::kEvenOdd_FillType:
654 return kEvenOdd_PathFill;
655 case SkPath::kInverseWinding_FillType:
656 return kInverseWinding_PathFill;
657 case SkPath::kInverseEvenOdd_FillType:
658 return kInverseEvenOdd_PathFill;
659 default:
660 SkDebugf("Unsupported path fill type\n");
661 return kHairLine_PathFill;
662 }
663}
664
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000665static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000666 int halfWidth = (kernelWidth - 1) / 2;
667 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000668 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000669 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000670 float x = static_cast<float>(i - halfWidth);
671 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
672 // is dropped here, since we renormalize the kernel below.
673 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000674 sum += kernel[i];
675 }
676 // Normalize the kernel
677 float scale = 1.0f / sum;
678 for (int i = 0; i < kernelWidth; ++i)
679 kernel[i] *= scale;
680}
681
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000682static void scaleRect(SkRect* rect, float scale) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000683 rect->fLeft *= scale;
684 rect->fTop *= scale;
685 rect->fRight *= scale;
686 rect->fBottom *= scale;
687}
688
689static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
690 SkMaskFilter* filter, const SkMatrix& matrix,
691 const SkRegion& clip, SkBounder* bounder,
692 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000693#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000694 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000695#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000696 SkMaskFilter::BlurInfo info;
697 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000698 if (SkMaskFilter::kNone_BlurType == blurType ||
699 !context->supportsShaders()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000700 return false;
701 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000702 SkScalar radius = info.fIgnoreTransform ? info.fRadius
703 : matrix.mapRadius(info.fRadius);
704 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000705 if (radius <= 0) {
706 return false;
707 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000708 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000709 SkRect srcRect = path.getBounds();
710
711 int scaleFactor = 1;
712
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000713 while (sigma > MAX_BLUR_SIGMA) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000714 scaleFactor *= 2;
715 sigma *= 0.5f;
716 }
senorblanco@chromium.org4a947d22011-07-18 21:48:35 +0000717 int halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000718 int kernelWidth = halfWidth * 2 + 1;
719
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000720 float invScale = 1.0f / scaleFactor;
721 scaleRect(&srcRect, invScale);
722 srcRect.roundOut();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000723 srcRect.inset(-halfWidth, -halfWidth);
724
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000725 SkRect clipBounds;
726 clipBounds.set(clip.getBounds());
727 scaleRect(&clipBounds, invScale);
728 clipBounds.roundOut();
729 clipBounds.inset(-halfWidth, -halfWidth);
730
731 srcRect.intersect(clipBounds);
732
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000733 scaleRect(&srcRect, scaleFactor);
734 SkRect finalRect = srcRect;
735
736 SkIRect finalIRect;
737 finalRect.roundOut(&finalIRect);
738 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000739 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000740 }
741 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000742 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000743 }
744 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
745 srcRect.offset(-srcRect.fLeft, -srcRect.fTop);
746 const GrTextureDesc desc = {
747 kRenderTarget_GrTextureFlagBit,
748 kNone_GrAALevel,
749 srcRect.width(),
750 srcRect.height(),
751 // We actually only need A8, but it often isn't supported as a
752 // render target
753 kRGBA_8888_GrPixelConfig
754 };
755
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000756 GrAutoScratchTexture srcEntry(context, desc);
757 GrAutoScratchTexture dstEntry(context, desc);
758 if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000759 return false;
760 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000761 GrTexture* srcTexture = srcEntry.texture();
762 GrTexture* dstTexture = dstEntry.texture();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000763 if (NULL == srcTexture || NULL == dstTexture) {
764 return false;
765 }
766 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000767 // Once this code moves into GrContext, this should be changed to use
768 // an AutoClipRestore.
769 GrClip oldClip = context->getClip();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000770 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000771 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000772 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000773 GrPaint tempPaint;
774 tempPaint.reset();
775
776 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000777 tempPaint.fAntiAlias = grp->fAntiAlias;
778 if (tempPaint.fAntiAlias) {
779 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
780 // blend coeff of zero requires dual source blending support in order
781 // to properly blend partially covered pixels. This means the AA
782 // code path may not be taken. So we use a dst blend coeff of ISA. We
783 // could special case AA draws to a dst surface with known alpha=0 to
784 // use a zero dst coeff when dual source blending isn't available.
785 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
786 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
787 }
788 // Draw hard shadow to dstTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000789 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000790 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000791
792 GrMatrix sampleM;
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000793 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000794 GrPaint paint;
795 paint.reset();
796 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
797 paint.getTextureSampler(0)->setMatrix(sampleM);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000798 GrAutoScratchTexture origEntry;
799
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000800 if (blurType != SkMaskFilter::kNormal_BlurType) {
801 // Stash away a copy of the unblurred image.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000802 origEntry.set(context, desc);
803 if (NULL == origEntry.texture()) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000804 return false;
805 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000806 context->setRenderTarget(origEntry.texture()->asRenderTarget());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000807 paint.setTexture(0, srcTexture);
808 context->drawRect(paint, srcRect);
809 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000810 for (int i = 1; i < scaleFactor; i *= 2) {
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000811 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
812 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000813 context->setRenderTarget(dstTexture->asRenderTarget());
814 SkRect dstRect(srcRect);
815 scaleRect(&dstRect, 0.5f);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000816 paint.setTexture(0, srcTexture);
817 context->drawRectToRect(paint, dstRect, srcRect);
818 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000819 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000820 }
821
822 SkAutoTMalloc<float> kernelStorage(kernelWidth);
823 float* kernel = kernelStorage.get();
824 buildKernel(sigma, kernel, kernelWidth);
825
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000826 // Clear out a halfWidth to the right of the srcRect to prevent the
827 // X convolution from reading garbage.
828 SkIRect clearRect = SkIRect::MakeXYWH(
829 srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height());
830 context->clear(&clearRect, 0x0);
831
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000832 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000833 context->convolveInX(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000834 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000835
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000836 // Clear out a halfWidth below the srcRect to prevent the Y
837 // convolution from reading garbage.
838 clearRect = SkIRect::MakeXYWH(
839 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth);
840 context->clear(&clearRect, 0x0);
841
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000842 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000843 context->convolveInY(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000844 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000845
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000846 // Clear one pixel to the right and below, to accommodate bilinear
847 // upsampling.
848 clearRect = SkIRect::MakeXYWH(
849 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
850 context->clear(&clearRect, 0x0);
851 clearRect = SkIRect::MakeXYWH(
852 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
853 context->clear(&clearRect, 0x0);
854
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000855 if (scaleFactor > 1) {
856 // FIXME: This should be mitchell, not bilinear.
857 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000858 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000859 paint.getTextureSampler(0)->setMatrix(sampleM);
860 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000861 paint.setTexture(0, srcTexture);
862 SkRect dstRect(srcRect);
863 scaleRect(&dstRect, scaleFactor);
864 context->drawRectToRect(paint, dstRect, srcRect);
865 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000866 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000867 }
868
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000869 if (blurType != SkMaskFilter::kNormal_BlurType) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000870 GrTexture* origTexture = origEntry.texture();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000871 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000872 sampleM.setIDiv(origTexture->width(), origTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000873 paint.getTextureSampler(0)->setMatrix(sampleM);
874 // Blend origTexture over srcTexture.
875 context->setRenderTarget(srcTexture->asRenderTarget());
876 paint.setTexture(0, origTexture);
877 if (SkMaskFilter::kInner_BlurType == blurType) {
878 // inner: dst = dst * src
879 paint.fSrcBlendCoeff = kDC_BlendCoeff;
880 paint.fDstBlendCoeff = kZero_BlendCoeff;
881 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
882 // solid: dst = src + dst - src * dst
883 // = (1 - dst) * src + 1 * dst
884 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
885 paint.fDstBlendCoeff = kOne_BlendCoeff;
886 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
887 // outer: dst = dst * (1 - src)
888 // = 0 * src + (1 - src) * dst
889 paint.fSrcBlendCoeff = kZero_BlendCoeff;
890 paint.fDstBlendCoeff = kISC_BlendCoeff;
891 }
892 context->drawRect(paint, srcRect);
893 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000894 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000895 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000896
897 if (grp->hasTextureOrMask()) {
898 GrMatrix inverse;
899 if (!matrix.invert(&inverse)) {
900 return false;
901 }
902 grp->preConcatActiveSamplerMatrices(inverse);
903 }
904
905 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
906 // we assume the last mask index is available for use
907 GrAssert(NULL == grp->getMask(MASK_IDX));
908 grp->setMask(MASK_IDX, srcTexture);
909 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
910
911 GrMatrix m;
912 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
913 m.postIDiv(srcTexture->width(), srcTexture->height());
914 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
915 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000916 return true;
917}
918
reed@google.com69302852011-02-16 18:08:07 +0000919static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
920 SkMaskFilter* filter, const SkMatrix& matrix,
921 const SkRegion& clip, SkBounder* bounder,
922 GrPaint* grp) {
923 SkMask srcM, dstM;
924
925 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
926 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
927 return false;
928 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000929 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000930
931 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
932 return false;
933 }
934 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000935 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000936
937 if (clip.quickReject(dstM.fBounds)) {
938 return false;
939 }
940 if (bounder && !bounder->doIRect(dstM.fBounds)) {
941 return false;
942 }
943
944 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
945 // the current clip (and identity matrix) and grpaint settings
946
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000947 // used to compute inverse view, if necessary
948 GrMatrix ivm = context->getMatrix();
949
reed@google.com0c219b62011-02-16 21:31:18 +0000950 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000951
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000952 const GrTextureDesc desc = {
953 kNone_GrTextureFlags,
954 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +0000955 dstM.fBounds.width(),
956 dstM.fBounds.height(),
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000957 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +0000958 };
959
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000960 GrAutoScratchTexture ast(context, desc);
961 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000962
reed@google.com69302852011-02-16 18:08:07 +0000963 if (NULL == texture) {
964 return false;
965 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000966 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
967 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000968
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000969 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
970 grp->preConcatActiveSamplerMatrices(ivm);
971 }
972
973 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
974 // we assume the last mask index is available for use
975 GrAssert(NULL == grp->getMask(MASK_IDX));
976 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000977 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +0000978
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000979 GrRect d;
980 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000981 GrIntToScalar(dstM.fBounds.fTop),
982 GrIntToScalar(dstM.fBounds.fRight),
983 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000984
985 GrMatrix m;
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000986 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
987 -dstM.fBounds.fTop*SK_Scalar1);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000988 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000989 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
990
991 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000992 return true;
993}
reed@google.com69302852011-02-16 18:08:07 +0000994
reed@google.com0c219b62011-02-16 21:31:18 +0000995void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comfe626382011-09-21 13:50:35 +0000996 const SkPaint& origPaint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000997 bool pathIsMutable) {
998 CHECK_SHOULD_DRAW(draw);
999
reed@google.comfe626382011-09-21 13:50:35 +00001000 bool doFill = true;
1001 SkTLazy<SkPaint> lazyPaint;
1002 const SkPaint* paint = &origPaint;
1003
1004 // can we cheat, and threat a thin stroke as a hairline (w/ modulated alpha)
1005 // if we can, we draw lots faster (raster device does this same test)
1006 {
1007 SkAlpha newAlpha;
1008 if (SkDrawTreatAsHairline(*paint, *draw.fMatrix, &newAlpha)) {
1009 lazyPaint.set(*paint);
1010 lazyPaint.get()->setAlpha(newAlpha);
1011 lazyPaint.get()->setStrokeWidth(0);
1012 paint = lazyPaint.get();
1013 doFill = false;
1014 }
1015 }
1016 // must reference paint from here down, and not origPaint
1017 // since we may have change the paint (using lazyPaint for storage)
1018
bsalomon@google.com5782d712011-01-21 21:03:59 +00001019 GrPaint grPaint;
1020 SkAutoCachedTexture act;
reed@google.comfe626382011-09-21 13:50:35 +00001021 if (!this->skPaint2GrPaintShader(*paint,
Scroggod757df22011-05-16 13:11:16 +00001022 &act,
1023 *draw.fMatrix,
1024 &grPaint,
1025 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001026 return;
1027 }
1028
reed@google.comfe626382011-09-21 13:50:35 +00001029 // If we have a prematrix, apply it to the path, optimizing for the case
1030 // where the original path can in fact be modified in place (even though
1031 // its parameter type is const).
1032 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1033 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001034
1035 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001036 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001037
reed@google.come3445642011-02-16 23:20:39 +00001038 if (!pathIsMutable) {
1039 result = &tmpPath;
1040 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001041 }
reed@google.come3445642011-02-16 23:20:39 +00001042 // should I push prePathMatrix on our MV stack temporarily, instead
1043 // of applying it here? See SkDraw.cpp
1044 pathPtr->transform(*prePathMatrix, result);
1045 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001046 }
reed@google.com0c219b62011-02-16 21:31:18 +00001047 // at this point we're done with prePathMatrix
1048 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001049
reed@google.comfe626382011-09-21 13:50:35 +00001050 if (doFill && (paint->getPathEffect() ||
1051 paint->getStyle() != SkPaint::kFill_Style)) {
1052 // it is safe to use tmpPath here, even if we already used it for the
1053 // prepathmatrix, since getFillPath can take the same object for its
1054 // input and output safely.
1055 doFill = paint->getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001056 pathPtr = &tmpPath;
1057 }
1058
reed@google.comfe626382011-09-21 13:50:35 +00001059 if (paint->getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001060 // avoid possibly allocating a new path in transform if we can
1061 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1062
1063 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001064 pathPtr->transform(*draw.fMatrix, devPathPtr);
reed@google.comfe626382011-09-21 13:50:35 +00001065 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001066 *draw.fMatrix, *draw.fClip, draw.fBounder,
1067 &grPaint)) {
reed@google.comfe626382011-09-21 13:50:35 +00001068 drawWithMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001069 *draw.fMatrix, *draw.fClip, draw.fBounder,
1070 &grPaint);
1071 }
reed@google.com69302852011-02-16 18:08:07 +00001072 return;
1073 }
reed@google.com69302852011-02-16 18:08:07 +00001074
bsalomon@google.comffca4002011-02-22 20:34:01 +00001075 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001076
reed@google.com0c219b62011-02-16 21:31:18 +00001077 if (doFill) {
1078 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001079 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001080 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001081 break;
1082 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001083 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001084 break;
1085 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001086 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001087 break;
1088 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001089 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001090 break;
1091 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001092 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001093 return;
1094 }
1095 }
1096
reed@google.com07f3ee12011-05-16 17:21:57 +00001097 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001098}
1099
reed@google.comac10a2d2010-12-22 21:39:39 +00001100void SkGpuDevice::drawBitmap(const SkDraw& draw,
1101 const SkBitmap& bitmap,
1102 const SkIRect* srcRectPtr,
1103 const SkMatrix& m,
1104 const SkPaint& paint) {
1105 CHECK_SHOULD_DRAW(draw);
1106
1107 SkIRect srcRect;
1108 if (NULL == srcRectPtr) {
1109 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1110 } else {
1111 srcRect = *srcRectPtr;
1112 }
1113
junov@google.comd935cfb2011-06-27 20:48:23 +00001114 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001115 // Convert the bitmap to a shader so that the rect can be drawn
1116 // through drawRect, which supports mask filters.
1117 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001118 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001119 if (srcRectPtr) {
1120 if (!bitmap.extractSubset(&tmp, srcRect)) {
1121 return; // extraction failed
1122 }
1123 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001124 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001125 }
1126 SkPaint paintWithTexture(paint);
1127 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1128 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001129 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001130 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001131
junov@google.com1d329782011-07-28 20:10:09 +00001132 // Transform 'm' needs to be concatenated to the draw matrix,
1133 // rather than transforming the primitive directly, so that 'm' will
1134 // also affect the behavior of the mask filter.
1135 SkMatrix drawMatrix;
1136 drawMatrix.setConcat(*draw.fMatrix, m);
1137 SkDraw transformedDraw(draw);
1138 transformedDraw.fMatrix = &drawMatrix;
1139
1140 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1141
junov@google.comd935cfb2011-06-27 20:48:23 +00001142 return;
1143 }
1144
bsalomon@google.com5782d712011-01-21 21:03:59 +00001145 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001146 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001147 return;
1148 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001149 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001150 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001151 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001152 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001153 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001154 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001155
bsalomon@google.com91958362011-06-13 17:58:13 +00001156 const int maxTextureSize = fContext->getMaxTextureSize();
1157 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1158 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001159 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001160 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001161 return;
1162 }
1163
1164 // undo the translate done by SkCanvas
1165 int DX = SkMax32(0, srcRect.fLeft);
1166 int DY = SkMax32(0, srcRect.fTop);
1167 // compute clip bounds in local coordinates
1168 SkIRect clipRect;
1169 {
1170 SkRect r;
1171 r.set(draw.fClip->getBounds());
1172 SkMatrix matrix, inverse;
1173 matrix.setConcat(*draw.fMatrix, m);
1174 if (!matrix.invert(&inverse)) {
1175 return;
1176 }
1177 inverse.mapRect(&r);
1178 r.roundOut(&clipRect);
1179 // apply the canvas' translate to our local clip
1180 clipRect.offset(DX, DY);
1181 }
1182
bsalomon@google.com91958362011-06-13 17:58:13 +00001183 int nx = bitmap.width() / maxTextureSize;
1184 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001185 for (int x = 0; x <= nx; x++) {
1186 for (int y = 0; y <= ny; y++) {
1187 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001188 tileR.set(x * maxTextureSize, y * maxTextureSize,
1189 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001190 if (!SkIRect::Intersects(tileR, clipRect)) {
1191 continue;
1192 }
1193
1194 SkIRect srcR = tileR;
1195 if (!srcR.intersect(srcRect)) {
1196 continue;
1197 }
1198
1199 SkBitmap tmpB;
1200 if (bitmap.extractSubset(&tmpB, tileR)) {
1201 // now offset it to make it "local" to our tmp bitmap
1202 srcR.offset(-tileR.fLeft, -tileR.fTop);
1203
1204 SkMatrix tmpM(m);
1205 {
1206 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1207 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1208 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1209 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001210 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001211 }
1212 }
1213 }
1214}
1215
1216/*
1217 * This is called by drawBitmap(), which has to handle images that may be too
1218 * large to be represented by a single texture.
1219 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001220 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1221 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001222 */
1223void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1224 const SkBitmap& bitmap,
1225 const SkIRect& srcRect,
1226 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001227 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001228 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1229 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001230
reed@google.com9c49bc32011-07-07 13:42:37 +00001231 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001232 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001233 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001234 return;
1235 }
1236
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001237 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1238
1239 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1240 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1241 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1242 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001243
1244 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001245 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001246 if (NULL == texture) {
1247 return;
1248 }
1249
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001250 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001251
reed@google.com20efde72011-05-09 17:00:02 +00001252 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1253 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001254 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001255 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1256 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1257 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001258 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001259
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001260 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001261 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001262 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001263 // If drawing a subrect of the bitmap and filtering is enabled,
1264 // use a constrained texture domain to avoid color bleeding
1265 GrScalar left, top, right, bottom;
1266 if (srcRect.width() > 1) {
1267 GrScalar border = GR_ScalarHalf / bitmap.width();
1268 left = paintRect.left() + border;
1269 right = paintRect.right() - border;
1270 } else {
1271 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1272 }
1273 if (srcRect.height() > 1) {
1274 GrScalar border = GR_ScalarHalf / bitmap.height();
1275 top = paintRect.top() + border;
1276 bottom = paintRect.bottom() - border;
1277 } else {
1278 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1279 }
1280 GrRect textureDomain;
1281 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001282 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001283 }
1284
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001285 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001286}
1287
1288void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1289 int left, int top, const SkPaint& paint) {
1290 CHECK_SHOULD_DRAW(draw);
1291
1292 SkAutoLockPixels alp(bitmap);
1293 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1294 return;
1295 }
1296
bsalomon@google.com5782d712011-01-21 21:03:59 +00001297 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001298 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001299 return;
1300 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001301
bsalomon@google.com5782d712011-01-21 21:03:59 +00001302 GrAutoMatrix avm(fContext, GrMatrix::I());
1303
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001304 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001305
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001306 GrTexture* texture;
1307 sampler->setClampNoFilter();
1308 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1309
1310 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001311
bsalomon@google.com5782d712011-01-21 21:03:59 +00001312 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001313 GrRect::MakeXYWH(GrIntToScalar(left),
1314 GrIntToScalar(top),
1315 GrIntToScalar(bitmap.width()),
1316 GrIntToScalar(bitmap.height())),
1317 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001318}
1319
1320void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1321 int x, int y, const SkPaint& paint) {
1322 CHECK_SHOULD_DRAW(draw);
1323
bsalomon@google.com5782d712011-01-21 21:03:59 +00001324 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001325 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001326 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001327 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001328 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001329
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001330 GrTexture* devTex = grPaint.getTexture(0);
1331 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001332
1333 const SkBitmap& bm = dev->accessBitmap(false);
1334 int w = bm.width();
1335 int h = bm.height();
1336
1337 GrAutoMatrix avm(fContext, GrMatrix::I());
1338
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001339 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001340
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001341 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1342 GrIntToScalar(y),
1343 GrIntToScalar(w),
1344 GrIntToScalar(h));
1345 // The device being drawn may not fill up its texture (saveLayer uses
1346 // the approximate ).
1347 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1348 GR_Scalar1 * h / devTex->height());
1349
1350 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001351}
1352
1353///////////////////////////////////////////////////////////////////////////////
1354
1355// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001356static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1357 kTriangles_PrimitiveType,
1358 kTriangleStrip_PrimitiveType,
1359 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001360};
1361
1362void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1363 int vertexCount, const SkPoint vertices[],
1364 const SkPoint texs[], const SkColor colors[],
1365 SkXfermode* xmode,
1366 const uint16_t indices[], int indexCount,
1367 const SkPaint& paint) {
1368 CHECK_SHOULD_DRAW(draw);
1369
bsalomon@google.com5782d712011-01-21 21:03:59 +00001370 GrPaint grPaint;
1371 SkAutoCachedTexture act;
1372 // we ignore the shader if texs is null.
1373 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001374 if (!this->skPaint2GrPaintNoShader(paint,
1375 false,
1376 &grPaint,
1377 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001378 return;
1379 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001380 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001381 if (!this->skPaint2GrPaintShader(paint, &act,
1382 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001383 &grPaint,
1384 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001385 return;
1386 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001387 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001388
1389 if (NULL != xmode && NULL != texs && NULL != colors) {
1390 SkXfermode::Mode mode;
1391 if (!SkXfermode::IsMode(xmode, &mode) ||
1392 SkXfermode::kMultiply_Mode != mode) {
1393 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1394#if 0
1395 return
1396#endif
1397 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001398 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001399
bsalomon@google.com498776a2011-08-16 19:20:44 +00001400 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1401 if (NULL != colors) {
1402 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001403 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001404 for (int i = 0; i < vertexCount; ++i) {
1405 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1406 }
1407 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001408 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001409 fContext->drawVertices(grPaint,
1410 gVertexMode2PrimitiveType[vmode],
1411 vertexCount,
1412 (GrPoint*) vertices,
1413 (GrPoint*) texs,
1414 colors,
1415 indices,
1416 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001417}
1418
1419///////////////////////////////////////////////////////////////////////////////
1420
1421static void GlyphCacheAuxProc(void* data) {
1422 delete (GrFontScaler*)data;
1423}
1424
1425static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1426 void* auxData;
1427 GrFontScaler* scaler = NULL;
1428 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1429 scaler = (GrFontScaler*)auxData;
1430 }
1431 if (NULL == scaler) {
1432 scaler = new SkGrFontScaler(cache);
1433 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1434 }
1435 return scaler;
1436}
1437
1438static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1439 SkFixed fx, SkFixed fy,
1440 const SkGlyph& glyph) {
1441 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1442
1443 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1444
1445 if (NULL == procs->fFontScaler) {
1446 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1447 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001448
1449 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001450 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001451 *
reed@google.com3b139f52011-06-07 17:56:25 +00001452 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1453 * It calls that rather than round, because our caller has already added
1454 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1455 *
1456 * Test code between raster and gpu (they should draw the same)
1457 *
1458 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1459 *
1460 * Perhaps we should only perform this integralization if there is no
1461 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001462 */
reed@google.com3b139f52011-06-07 17:56:25 +00001463 fy = SkFixedFloorToFixed(fy);
1464
reed@google.comac10a2d2010-12-22 21:39:39 +00001465 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001466 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001467 procs->fFontScaler);
1468}
1469
bsalomon@google.com5782d712011-01-21 21:03:59 +00001470SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001471
1472 // deferred allocation
1473 if (NULL == fDrawProcs) {
1474 fDrawProcs = new GrSkDrawProcs;
1475 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1476 fDrawProcs->fContext = fContext;
1477 }
1478
1479 // init our (and GL's) state
1480 fDrawProcs->fTextContext = context;
1481 fDrawProcs->fFontScaler = NULL;
1482 return fDrawProcs;
1483}
1484
1485void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1486 size_t byteLength, SkScalar x, SkScalar y,
1487 const SkPaint& paint) {
1488 CHECK_SHOULD_DRAW(draw);
1489
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001490 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001491 // this guy will just call our drawPath()
1492 draw.drawText((const char*)text, byteLength, x, y, paint);
1493 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001494 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001495
1496 GrPaint grPaint;
1497 SkAutoCachedTexture act;
1498
Scroggod757df22011-05-16 13:11:16 +00001499 if (!this->skPaint2GrPaintShader(paint,
1500 &act,
1501 *draw.fMatrix,
1502 &grPaint,
1503 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001504 return;
1505 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001506 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001507 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001508 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1509 }
1510}
1511
1512void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1513 size_t byteLength, const SkScalar pos[],
1514 SkScalar constY, int scalarsPerPos,
1515 const SkPaint& paint) {
1516 CHECK_SHOULD_DRAW(draw);
1517
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001518 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001519 // this guy will just call our drawPath()
1520 draw.drawPosText((const char*)text, byteLength, pos, constY,
1521 scalarsPerPos, paint);
1522 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001523 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001524
1525 GrPaint grPaint;
1526 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001527 if (!this->skPaint2GrPaintShader(paint,
1528 &act,
1529 *draw.fMatrix,
1530 &grPaint,
1531 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001532 return;
1533 }
1534
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001535 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001536 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001537 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1538 scalarsPerPos, paint);
1539 }
1540}
1541
1542void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1543 size_t len, const SkPath& path,
1544 const SkMatrix* m, const SkPaint& paint) {
1545 CHECK_SHOULD_DRAW(draw);
1546
1547 SkASSERT(draw.fDevice == this);
1548 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1549}
1550
1551///////////////////////////////////////////////////////////////////////////////
1552
reed@google.comf67e4cf2011-03-15 20:56:58 +00001553bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1554 if (!paint.isLCDRenderText()) {
1555 // we're cool with the paint as is
1556 return false;
1557 }
1558
1559 if (paint.getShader() ||
1560 paint.getXfermode() || // unless its srcover
1561 paint.getMaskFilter() ||
1562 paint.getRasterizer() ||
1563 paint.getColorFilter() ||
1564 paint.getPathEffect() ||
1565 paint.isFakeBoldText() ||
1566 paint.getStyle() != SkPaint::kFill_Style) {
1567 // turn off lcd
1568 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1569 flags->fHinting = paint.getHinting();
1570 return true;
1571 }
1572 // we're cool with the paint as is
1573 return false;
1574}
1575
1576///////////////////////////////////////////////////////////////////////////////
1577
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001578SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1579 const GrSamplerState& sampler,
1580 TexType type) {
1581 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001582 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001583
bsalomon@google.come97f0852011-06-17 13:10:25 +00001584 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001585 const GrTextureDesc desc = {
1586 kRenderTarget_GrTextureFlagBit,
1587 kNone_GrAALevel,
1588 bitmap.width(),
1589 bitmap.height(),
1590 SkGr::Bitmap2PixelConfig(bitmap)
1591 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001592 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001593 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001594 // we know layers will only be drawn through drawDevice.
1595 // drawDevice has been made to work with content embedded in a
1596 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001597 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001598 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001599 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001600 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001601 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001602 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001603 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001604 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001605 GrContext::TextureKey key = bitmap.getGenerationID();
1606 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001607
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001608 entry = ctx->findAndLockTexture(key, bitmap.width(),
1609 bitmap.height(), sampler);
1610 if (NULL == entry.texture()) {
1611 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001612 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001613 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001614 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001615 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001616 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001617 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001618 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1619 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001620 }
1621 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001622 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001623}
1624
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001625void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1626 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001627}
1628
bsalomon@google.come97f0852011-06-17 13:10:25 +00001629SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1630 int width, int height,
1631 bool isOpaque,
1632 Usage usage) {
1633 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1634 width, height, usage));
1635}
1636