blob: 5283dc07fe8bef48f77b982c9d7107dc152f4477 [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.com6850eab2011-11-03 20:29:47 +0000259bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
260 int x, int y,
261 SkCanvas::Config8888 config8888) {
262 // support for non-native configs coming soon
263 if (config8888 != SkCanvas::kNative_Premul_Config8888) {
264 return false;
265 }
bsalomon@google.com910267d2011-11-02 20:06:25 +0000266 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
267 SkASSERT(!bitmap.isNull());
268 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000269
bsalomon@google.com910267d2011-11-02 20:06:25 +0000270 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000271 return fContext->readRenderTargetPixels(fRenderTarget,
272 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000273 bitmap.width(),
274 bitmap.height(),
bsalomon@google.comc6980972011-11-02 19:57:21 +0000275 kRGBA_8888_GrPixelConfig,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000276 bitmap.getPixels(),
277 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000278}
279
280void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y) {
281 SkAutoLockPixels alp(bitmap);
282 if (!bitmap.readyToDraw()) {
283 return;
284 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000285 GrPixelConfig config = SkGr::BitmapConfig2PixelConfig(bitmap.config(),
286 bitmap.isOpaque());
reed@google.comac10a2d2010-12-22 21:39:39 +0000287 fContext->setRenderTarget(fRenderTarget);
288 // we aren't setting the clip or matrix, so mark as dirty
289 // we don't need to set them for this call and don't have them anyway
290 fNeedPrepareRenderTarget = true;
291
292 fContext->writePixels(x, y, bitmap.width(), bitmap.height(),
293 config, bitmap.getPixels(), bitmap.rowBytes());
294}
295
296///////////////////////////////////////////////////////////////////////////////
297
298static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000299 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000300 const SkRegion& clipRegion,
301 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000302 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000303
304 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000305 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000306 const SkIRect& skBounds = clipRegion.getBounds();
307 GrRect bounds;
308 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
309 GrIntToScalar(skBounds.fTop),
310 GrIntToScalar(skBounds.fRight),
311 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000312 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
313 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000314 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000315}
316
317// call this ever each draw call, to ensure that the context reflects our state,
318// and not the state from some other canvas/device
319void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
320 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000321 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000322
323 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000324 SkASSERT(draw.fClipStack);
325 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000326 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000327 fNeedPrepareRenderTarget = false;
328 }
329}
330
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000331void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
332 const SkClipStack& clipStack) {
333 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
334 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000335 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000336}
337
338void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000339 const SkRegion& clip, const SkClipStack& clipStack) {
340
reed@google.comac10a2d2010-12-22 21:39:39 +0000341 fContext->setRenderTarget(fRenderTarget);
342
bsalomon@google.comd302f142011-03-03 13:54:13 +0000343 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000344
reed@google.com6f8f2922011-03-04 22:27:10 +0000345 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000346
347 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000348 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000349 fNeedClear = false;
350 }
351}
352
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000353bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000354 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000355 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000356 return true;
357 }
358 return false;
359}
360
361///////////////////////////////////////////////////////////////////////////////
362
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000363SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
364SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
365SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
366SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
367SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
368 shader_type_mismatch);
369SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000370
bsalomon@google.com5782d712011-01-21 21:03:59 +0000371static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
372 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
373 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
374 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
375 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
376 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
377};
378
379bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
380 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000381 GrPaint* grPaint,
382 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000383
384 grPaint->fDither = skPaint.isDither();
385 grPaint->fAntiAlias = skPaint.isAntiAlias();
386
387 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
388 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
389
390 SkXfermode* mode = skPaint.getXfermode();
391 if (mode) {
392 if (!mode->asCoeff(&sm, &dm)) {
reed@google.com1a2e8d22011-01-21 22:08:29 +0000393 SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000394#if 0
395 return false;
396#endif
397 }
398 }
399 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
400 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
401
402 if (justAlpha) {
403 uint8_t alpha = skPaint.getAlpha();
404 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000405 // justAlpha is currently set to true only if there is a texture,
406 // so constantColor should not also be true.
407 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000408 } else {
409 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000410 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000411 }
Scroggo97c88c22011-05-11 14:05:25 +0000412 SkColorFilter* colorFilter = skPaint.getColorFilter();
413 SkColor color;
414 SkXfermode::Mode filterMode;
415 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000416 if (!constantColor) {
417 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
418 grPaint->fColorFilterXfermode = filterMode;
419 return true;
420 }
421 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
422 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000423 }
Scroggod757df22011-05-16 13:11:16 +0000424 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000425 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000426}
427
bsalomon@google.com5782d712011-01-21 21:03:59 +0000428bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
429 SkAutoCachedTexture* act,
430 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000431 GrPaint* grPaint,
432 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000433
bsalomon@google.com5782d712011-01-21 21:03:59 +0000434 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000435
bsalomon@google.com5782d712011-01-21 21:03:59 +0000436 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000437 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000438 return this->skPaint2GrPaintNoShader(skPaint,
439 false,
440 grPaint,
441 constantColor);
442 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000443 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000444 }
445
reed@google.comac10a2d2010-12-22 21:39:39 +0000446 SkBitmap bitmap;
447 SkMatrix matrix;
448 SkShader::TileMode tileModes[2];
449 SkScalar twoPointParams[3];
450 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
451 tileModes, twoPointParams);
452
bsalomon@google.com5782d712011-01-21 21:03:59 +0000453 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
454 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000455 SkShader::GradientInfo info;
456 SkColor color;
457
458 info.fColors = &color;
459 info.fColorOffsets = NULL;
460 info.fColorCount = 1;
461 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
462 SkPaint copy(skPaint);
463 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000464 // modulate the paint alpha by the shader's solid color alpha
465 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
466 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000467 return this->skPaint2GrPaintNoShader(copy,
468 false,
469 grPaint,
470 constantColor);
471 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000472 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000473 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000474 GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx);
475 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000476 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000477 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000478 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000479 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000480 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000481 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
482 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000483 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000484 sampler->setRadial2Params(twoPointParams[0],
485 twoPointParams[1],
486 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000487 }
488
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000489 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000490 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000491 SkDebugf("Couldn't convert bitmap to texture.\n");
492 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000494 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
496 // since our texture coords will be in local space, we wack the texture
497 // matrix to map them back into 0...1 before we load it
498 SkMatrix localM;
499 if (shader->getLocalMatrix(&localM)) {
500 SkMatrix inverse;
501 if (localM.invert(&inverse)) {
502 matrix.preConcat(inverse);
503 }
504 }
505 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000506 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
507 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000508 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000509 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000510 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 matrix.postScale(s, s);
512 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000513 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000514
515 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000516}
517
518///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000519
bsalomon@google.com398109c2011-04-14 18:40:27 +0000520void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000521 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000522}
523
reed@google.comac10a2d2010-12-22 21:39:39 +0000524void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
525 CHECK_SHOULD_DRAW(draw);
526
bsalomon@google.com5782d712011-01-21 21:03:59 +0000527 GrPaint grPaint;
528 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000529 if (!this->skPaint2GrPaintShader(paint,
530 &act,
531 *draw.fMatrix,
532 &grPaint,
533 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000534 return;
535 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000536
537 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000538}
539
540// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000541static const GrPrimitiveType gPointMode2PrimtiveType[] = {
542 kPoints_PrimitiveType,
543 kLines_PrimitiveType,
544 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000545};
546
547void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000548 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000549 CHECK_SHOULD_DRAW(draw);
550
551 SkScalar width = paint.getStrokeWidth();
552 if (width < 0) {
553 return;
554 }
555
556 // we only handle hairlines here, else we let the SkDraw call our drawPath()
557 if (width > 0) {
558 draw.drawPoints(mode, count, pts, paint, true);
559 return;
560 }
561
bsalomon@google.com5782d712011-01-21 21:03:59 +0000562 GrPaint grPaint;
563 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000564 if (!this->skPaint2GrPaintShader(paint,
565 &act,
566 *draw.fMatrix,
567 &grPaint,
568 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000569 return;
570 }
571
bsalomon@google.com5782d712011-01-21 21:03:59 +0000572 fContext->drawVertices(grPaint,
573 gPointMode2PrimtiveType[mode],
574 count,
575 (GrPoint*)pts,
576 NULL,
577 NULL,
578 NULL,
579 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000580}
581
reed@google.comc9aa5872011-04-05 21:05:37 +0000582///////////////////////////////////////////////////////////////////////////////
583
reed@google.comac10a2d2010-12-22 21:39:39 +0000584void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
585 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000586 CHECK_SHOULD_DRAW(draw);
587
bungeman@google.com79bd8772011-07-18 15:34:08 +0000588 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000589 SkScalar width = paint.getStrokeWidth();
590
591 /*
592 We have special code for hairline strokes, miter-strokes, and fills.
593 Anything else we just call our path code.
594 */
595 bool usePath = doStroke && width > 0 &&
596 paint.getStrokeJoin() != SkPaint::kMiter_Join;
597 // another reason we might need to call drawPath...
598 if (paint.getMaskFilter()) {
599 usePath = true;
600 }
reed@google.com67db6642011-05-26 11:46:35 +0000601 // until we aa rotated rects...
602 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
603 usePath = true;
604 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000605 // small miter limit means right angles show bevel...
606 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
607 paint.getStrokeMiter() < SK_ScalarSqrt2)
608 {
609 usePath = true;
610 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000611 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000612 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
613 usePath = true;
614 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000615
616 if (usePath) {
617 SkPath path;
618 path.addRect(rect);
619 this->drawPath(draw, path, paint, NULL, true);
620 return;
621 }
622
623 GrPaint grPaint;
624 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000625 if (!this->skPaint2GrPaintShader(paint,
626 &act,
627 *draw.fMatrix,
628 &grPaint,
629 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000630 return;
631 }
reed@google.com20efde72011-05-09 17:00:02 +0000632 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000633}
634
reed@google.com69302852011-02-16 18:08:07 +0000635#include "SkMaskFilter.h"
636#include "SkBounder.h"
637
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000638static GrPathFill skToGrFillType(SkPath::FillType fillType) {
639 switch (fillType) {
640 case SkPath::kWinding_FillType:
641 return kWinding_PathFill;
642 case SkPath::kEvenOdd_FillType:
643 return kEvenOdd_PathFill;
644 case SkPath::kInverseWinding_FillType:
645 return kInverseWinding_PathFill;
646 case SkPath::kInverseEvenOdd_FillType:
647 return kInverseEvenOdd_PathFill;
648 default:
649 SkDebugf("Unsupported path fill type\n");
650 return kHairLine_PathFill;
651 }
652}
653
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000654static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000655 int halfWidth = (kernelWidth - 1) / 2;
656 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000657 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000658 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000659 float x = static_cast<float>(i - halfWidth);
660 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
661 // is dropped here, since we renormalize the kernel below.
662 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000663 sum += kernel[i];
664 }
665 // Normalize the kernel
666 float scale = 1.0f / sum;
667 for (int i = 0; i < kernelWidth; ++i)
668 kernel[i] *= scale;
669}
670
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000671static void scaleRect(SkRect* rect, float scale) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000672 rect->fLeft *= scale;
673 rect->fTop *= scale;
674 rect->fRight *= scale;
675 rect->fBottom *= scale;
676}
677
678static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
679 SkMaskFilter* filter, const SkMatrix& matrix,
680 const SkRegion& clip, SkBounder* bounder,
681 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000682#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000683 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000684#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000685 SkMaskFilter::BlurInfo info;
686 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000687 if (SkMaskFilter::kNone_BlurType == blurType ||
688 !context->supportsShaders()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000689 return false;
690 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000691 SkScalar radius = info.fIgnoreTransform ? info.fRadius
692 : matrix.mapRadius(info.fRadius);
693 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000694 if (radius <= 0) {
695 return false;
696 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000697 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000698 SkRect srcRect = path.getBounds();
699
700 int scaleFactor = 1;
701
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000702 while (sigma > MAX_BLUR_SIGMA) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000703 scaleFactor *= 2;
704 sigma *= 0.5f;
705 }
senorblanco@chromium.org4a947d22011-07-18 21:48:35 +0000706 int halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000707 int kernelWidth = halfWidth * 2 + 1;
708
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000709 float invScale = 1.0f / scaleFactor;
710 scaleRect(&srcRect, invScale);
711 srcRect.roundOut();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000712 srcRect.inset(-halfWidth, -halfWidth);
713
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000714 SkRect clipBounds;
715 clipBounds.set(clip.getBounds());
716 scaleRect(&clipBounds, invScale);
717 clipBounds.roundOut();
718 clipBounds.inset(-halfWidth, -halfWidth);
719
720 srcRect.intersect(clipBounds);
721
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000722 scaleRect(&srcRect, scaleFactor);
723 SkRect finalRect = srcRect;
724
725 SkIRect finalIRect;
726 finalRect.roundOut(&finalIRect);
727 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000728 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000729 }
730 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000731 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000732 }
733 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
734 srcRect.offset(-srcRect.fLeft, -srcRect.fTop);
735 const GrTextureDesc desc = {
736 kRenderTarget_GrTextureFlagBit,
737 kNone_GrAALevel,
738 srcRect.width(),
739 srcRect.height(),
740 // We actually only need A8, but it often isn't supported as a
741 // render target
742 kRGBA_8888_GrPixelConfig
743 };
744
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000745 GrAutoScratchTexture srcEntry(context, desc);
746 GrAutoScratchTexture dstEntry(context, desc);
747 if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000748 return false;
749 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000750 GrTexture* srcTexture = srcEntry.texture();
751 GrTexture* dstTexture = dstEntry.texture();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000752 if (NULL == srcTexture || NULL == dstTexture) {
753 return false;
754 }
755 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000756 // Once this code moves into GrContext, this should be changed to use
757 // an AutoClipRestore.
758 GrClip oldClip = context->getClip();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000759 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000760 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000761 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000762 GrPaint tempPaint;
763 tempPaint.reset();
764
765 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000766 tempPaint.fAntiAlias = grp->fAntiAlias;
767 if (tempPaint.fAntiAlias) {
768 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
769 // blend coeff of zero requires dual source blending support in order
770 // to properly blend partially covered pixels. This means the AA
771 // code path may not be taken. So we use a dst blend coeff of ISA. We
772 // could special case AA draws to a dst surface with known alpha=0 to
773 // use a zero dst coeff when dual source blending isn't available.
774 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
775 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
776 }
777 // Draw hard shadow to dstTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000778 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000779 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000780
781 GrMatrix sampleM;
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000782 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000783 GrPaint paint;
784 paint.reset();
785 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
786 paint.getTextureSampler(0)->setMatrix(sampleM);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000787 GrAutoScratchTexture origEntry;
788
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000789 if (blurType != SkMaskFilter::kNormal_BlurType) {
790 // Stash away a copy of the unblurred image.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000791 origEntry.set(context, desc);
792 if (NULL == origEntry.texture()) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000793 return false;
794 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000795 context->setRenderTarget(origEntry.texture()->asRenderTarget());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000796 paint.setTexture(0, srcTexture);
797 context->drawRect(paint, srcRect);
798 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000799 for (int i = 1; i < scaleFactor; i *= 2) {
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000800 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
801 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000802 context->setRenderTarget(dstTexture->asRenderTarget());
803 SkRect dstRect(srcRect);
804 scaleRect(&dstRect, 0.5f);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000805 paint.setTexture(0, srcTexture);
806 context->drawRectToRect(paint, dstRect, srcRect);
807 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000808 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809 }
810
811 SkAutoTMalloc<float> kernelStorage(kernelWidth);
812 float* kernel = kernelStorage.get();
813 buildKernel(sigma, kernel, kernelWidth);
814
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000815 // Clear out a halfWidth to the right of the srcRect to prevent the
816 // X convolution from reading garbage.
817 SkIRect clearRect = SkIRect::MakeXYWH(
818 srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height());
819 context->clear(&clearRect, 0x0);
820
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000821 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000822 context->convolveInX(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000823 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000824
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000825 // Clear out a halfWidth below the srcRect to prevent the Y
826 // convolution from reading garbage.
827 clearRect = SkIRect::MakeXYWH(
828 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth);
829 context->clear(&clearRect, 0x0);
830
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000831 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000832 context->convolveInY(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000833 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000834
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000835 // Clear one pixel to the right and below, to accommodate bilinear
836 // upsampling.
837 clearRect = SkIRect::MakeXYWH(
838 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
839 context->clear(&clearRect, 0x0);
840 clearRect = SkIRect::MakeXYWH(
841 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
842 context->clear(&clearRect, 0x0);
843
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000844 if (scaleFactor > 1) {
845 // FIXME: This should be mitchell, not bilinear.
846 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000847 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000848 paint.getTextureSampler(0)->setMatrix(sampleM);
849 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000850 paint.setTexture(0, srcTexture);
851 SkRect dstRect(srcRect);
852 scaleRect(&dstRect, scaleFactor);
853 context->drawRectToRect(paint, dstRect, srcRect);
854 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000855 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000856 }
857
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000858 if (blurType != SkMaskFilter::kNormal_BlurType) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000859 GrTexture* origTexture = origEntry.texture();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000860 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000861 sampleM.setIDiv(origTexture->width(), origTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000862 paint.getTextureSampler(0)->setMatrix(sampleM);
863 // Blend origTexture over srcTexture.
864 context->setRenderTarget(srcTexture->asRenderTarget());
865 paint.setTexture(0, origTexture);
866 if (SkMaskFilter::kInner_BlurType == blurType) {
867 // inner: dst = dst * src
868 paint.fSrcBlendCoeff = kDC_BlendCoeff;
869 paint.fDstBlendCoeff = kZero_BlendCoeff;
870 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
871 // solid: dst = src + dst - src * dst
872 // = (1 - dst) * src + 1 * dst
873 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
874 paint.fDstBlendCoeff = kOne_BlendCoeff;
875 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
876 // outer: dst = dst * (1 - src)
877 // = 0 * src + (1 - src) * dst
878 paint.fSrcBlendCoeff = kZero_BlendCoeff;
879 paint.fDstBlendCoeff = kISC_BlendCoeff;
880 }
881 context->drawRect(paint, srcRect);
882 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000883 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000884 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000885
886 if (grp->hasTextureOrMask()) {
887 GrMatrix inverse;
888 if (!matrix.invert(&inverse)) {
889 return false;
890 }
891 grp->preConcatActiveSamplerMatrices(inverse);
892 }
893
894 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
895 // we assume the last mask index is available for use
896 GrAssert(NULL == grp->getMask(MASK_IDX));
897 grp->setMask(MASK_IDX, srcTexture);
898 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
899
900 GrMatrix m;
901 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
902 m.postIDiv(srcTexture->width(), srcTexture->height());
903 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
904 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000905 return true;
906}
907
reed@google.com69302852011-02-16 18:08:07 +0000908static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
909 SkMaskFilter* filter, const SkMatrix& matrix,
910 const SkRegion& clip, SkBounder* bounder,
911 GrPaint* grp) {
912 SkMask srcM, dstM;
913
914 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
915 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
916 return false;
917 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000918 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000919
920 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
921 return false;
922 }
923 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000924 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000925
926 if (clip.quickReject(dstM.fBounds)) {
927 return false;
928 }
929 if (bounder && !bounder->doIRect(dstM.fBounds)) {
930 return false;
931 }
932
933 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
934 // the current clip (and identity matrix) and grpaint settings
935
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000936 // used to compute inverse view, if necessary
937 GrMatrix ivm = context->getMatrix();
938
reed@google.com0c219b62011-02-16 21:31:18 +0000939 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000940
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000941 const GrTextureDesc desc = {
942 kNone_GrTextureFlags,
943 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +0000944 dstM.fBounds.width(),
945 dstM.fBounds.height(),
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000946 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +0000947 };
948
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000949 GrAutoScratchTexture ast(context, desc);
950 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000951
reed@google.com69302852011-02-16 18:08:07 +0000952 if (NULL == texture) {
953 return false;
954 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000955 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
956 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000957
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000958 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
959 grp->preConcatActiveSamplerMatrices(ivm);
960 }
961
962 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
963 // we assume the last mask index is available for use
964 GrAssert(NULL == grp->getMask(MASK_IDX));
965 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000966 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +0000967
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000968 GrRect d;
969 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000970 GrIntToScalar(dstM.fBounds.fTop),
971 GrIntToScalar(dstM.fBounds.fRight),
972 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000973
974 GrMatrix m;
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000975 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
976 -dstM.fBounds.fTop*SK_Scalar1);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000977 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000978 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
979
980 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000981 return true;
982}
reed@google.com69302852011-02-16 18:08:07 +0000983
reed@google.com0c219b62011-02-16 21:31:18 +0000984void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comfe626382011-09-21 13:50:35 +0000985 const SkPaint& origPaint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000986 bool pathIsMutable) {
987 CHECK_SHOULD_DRAW(draw);
988
reed@google.comfe626382011-09-21 13:50:35 +0000989 bool doFill = true;
990 SkTLazy<SkPaint> lazyPaint;
991 const SkPaint* paint = &origPaint;
992
993 // can we cheat, and threat a thin stroke as a hairline (w/ modulated alpha)
994 // if we can, we draw lots faster (raster device does this same test)
995 {
996 SkAlpha newAlpha;
997 if (SkDrawTreatAsHairline(*paint, *draw.fMatrix, &newAlpha)) {
998 lazyPaint.set(*paint);
999 lazyPaint.get()->setAlpha(newAlpha);
1000 lazyPaint.get()->setStrokeWidth(0);
1001 paint = lazyPaint.get();
1002 doFill = false;
1003 }
1004 }
1005 // must reference paint from here down, and not origPaint
1006 // since we may have change the paint (using lazyPaint for storage)
1007
bsalomon@google.com5782d712011-01-21 21:03:59 +00001008 GrPaint grPaint;
1009 SkAutoCachedTexture act;
reed@google.comfe626382011-09-21 13:50:35 +00001010 if (!this->skPaint2GrPaintShader(*paint,
Scroggod757df22011-05-16 13:11:16 +00001011 &act,
1012 *draw.fMatrix,
1013 &grPaint,
1014 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001015 return;
1016 }
1017
reed@google.comfe626382011-09-21 13:50:35 +00001018 // If we have a prematrix, apply it to the path, optimizing for the case
1019 // where the original path can in fact be modified in place (even though
1020 // its parameter type is const).
1021 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1022 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001023
1024 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001025 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001026
reed@google.come3445642011-02-16 23:20:39 +00001027 if (!pathIsMutable) {
1028 result = &tmpPath;
1029 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001030 }
reed@google.come3445642011-02-16 23:20:39 +00001031 // should I push prePathMatrix on our MV stack temporarily, instead
1032 // of applying it here? See SkDraw.cpp
1033 pathPtr->transform(*prePathMatrix, result);
1034 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001035 }
reed@google.com0c219b62011-02-16 21:31:18 +00001036 // at this point we're done with prePathMatrix
1037 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001038
reed@google.comfe626382011-09-21 13:50:35 +00001039 if (doFill && (paint->getPathEffect() ||
1040 paint->getStyle() != SkPaint::kFill_Style)) {
1041 // it is safe to use tmpPath here, even if we already used it for the
1042 // prepathmatrix, since getFillPath can take the same object for its
1043 // input and output safely.
1044 doFill = paint->getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001045 pathPtr = &tmpPath;
1046 }
1047
reed@google.comfe626382011-09-21 13:50:35 +00001048 if (paint->getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001049 // avoid possibly allocating a new path in transform if we can
1050 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1051
1052 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001053 pathPtr->transform(*draw.fMatrix, devPathPtr);
reed@google.comfe626382011-09-21 13:50:35 +00001054 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001055 *draw.fMatrix, *draw.fClip, draw.fBounder,
1056 &grPaint)) {
reed@google.comfe626382011-09-21 13:50:35 +00001057 drawWithMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001058 *draw.fMatrix, *draw.fClip, draw.fBounder,
1059 &grPaint);
1060 }
reed@google.com69302852011-02-16 18:08:07 +00001061 return;
1062 }
reed@google.com69302852011-02-16 18:08:07 +00001063
bsalomon@google.comffca4002011-02-22 20:34:01 +00001064 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001065
reed@google.com0c219b62011-02-16 21:31:18 +00001066 if (doFill) {
1067 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001068 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001069 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001070 break;
1071 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001072 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001073 break;
1074 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001075 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001076 break;
1077 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001078 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001079 break;
1080 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001081 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001082 return;
1083 }
1084 }
1085
reed@google.com07f3ee12011-05-16 17:21:57 +00001086 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001087}
1088
reed@google.comac10a2d2010-12-22 21:39:39 +00001089void SkGpuDevice::drawBitmap(const SkDraw& draw,
1090 const SkBitmap& bitmap,
1091 const SkIRect* srcRectPtr,
1092 const SkMatrix& m,
1093 const SkPaint& paint) {
1094 CHECK_SHOULD_DRAW(draw);
1095
1096 SkIRect srcRect;
1097 if (NULL == srcRectPtr) {
1098 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1099 } else {
1100 srcRect = *srcRectPtr;
1101 }
1102
junov@google.comd935cfb2011-06-27 20:48:23 +00001103 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001104 // Convert the bitmap to a shader so that the rect can be drawn
1105 // through drawRect, which supports mask filters.
1106 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001107 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001108 if (srcRectPtr) {
1109 if (!bitmap.extractSubset(&tmp, srcRect)) {
1110 return; // extraction failed
1111 }
1112 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001113 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001114 }
1115 SkPaint paintWithTexture(paint);
1116 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1117 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001118 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001119 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001120
junov@google.com1d329782011-07-28 20:10:09 +00001121 // Transform 'm' needs to be concatenated to the draw matrix,
1122 // rather than transforming the primitive directly, so that 'm' will
1123 // also affect the behavior of the mask filter.
1124 SkMatrix drawMatrix;
1125 drawMatrix.setConcat(*draw.fMatrix, m);
1126 SkDraw transformedDraw(draw);
1127 transformedDraw.fMatrix = &drawMatrix;
1128
1129 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1130
junov@google.comd935cfb2011-06-27 20:48:23 +00001131 return;
1132 }
1133
bsalomon@google.com5782d712011-01-21 21:03:59 +00001134 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001135 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001136 return;
1137 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001138 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001139 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001140 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001141 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001142 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001143 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001144
bsalomon@google.com91958362011-06-13 17:58:13 +00001145 const int maxTextureSize = fContext->getMaxTextureSize();
1146 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1147 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001148 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001149 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001150 return;
1151 }
1152
1153 // undo the translate done by SkCanvas
1154 int DX = SkMax32(0, srcRect.fLeft);
1155 int DY = SkMax32(0, srcRect.fTop);
1156 // compute clip bounds in local coordinates
1157 SkIRect clipRect;
1158 {
1159 SkRect r;
1160 r.set(draw.fClip->getBounds());
1161 SkMatrix matrix, inverse;
1162 matrix.setConcat(*draw.fMatrix, m);
1163 if (!matrix.invert(&inverse)) {
1164 return;
1165 }
1166 inverse.mapRect(&r);
1167 r.roundOut(&clipRect);
1168 // apply the canvas' translate to our local clip
1169 clipRect.offset(DX, DY);
1170 }
1171
bsalomon@google.com91958362011-06-13 17:58:13 +00001172 int nx = bitmap.width() / maxTextureSize;
1173 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001174 for (int x = 0; x <= nx; x++) {
1175 for (int y = 0; y <= ny; y++) {
1176 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001177 tileR.set(x * maxTextureSize, y * maxTextureSize,
1178 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001179 if (!SkIRect::Intersects(tileR, clipRect)) {
1180 continue;
1181 }
1182
1183 SkIRect srcR = tileR;
1184 if (!srcR.intersect(srcRect)) {
1185 continue;
1186 }
1187
1188 SkBitmap tmpB;
1189 if (bitmap.extractSubset(&tmpB, tileR)) {
1190 // now offset it to make it "local" to our tmp bitmap
1191 srcR.offset(-tileR.fLeft, -tileR.fTop);
1192
1193 SkMatrix tmpM(m);
1194 {
1195 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1196 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1197 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1198 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001199 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001200 }
1201 }
1202 }
1203}
1204
1205/*
1206 * This is called by drawBitmap(), which has to handle images that may be too
1207 * large to be represented by a single texture.
1208 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001209 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1210 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001211 */
1212void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1213 const SkBitmap& bitmap,
1214 const SkIRect& srcRect,
1215 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001216 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001217 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1218 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001219
reed@google.com9c49bc32011-07-07 13:42:37 +00001220 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001221 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001222 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001223 return;
1224 }
1225
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001226 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1227
1228 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1229 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1230 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1231 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001232
1233 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001234 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001235 if (NULL == texture) {
1236 return;
1237 }
1238
bsalomon@google.com452943d2011-10-31 17:37:14 +00001239 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001240
reed@google.com20efde72011-05-09 17:00:02 +00001241 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1242 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001243 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001244 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1245 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1246 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001247 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001248
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001249 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001250 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001251 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001252 // If drawing a subrect of the bitmap and filtering is enabled,
1253 // use a constrained texture domain to avoid color bleeding
1254 GrScalar left, top, right, bottom;
1255 if (srcRect.width() > 1) {
1256 GrScalar border = GR_ScalarHalf / bitmap.width();
1257 left = paintRect.left() + border;
1258 right = paintRect.right() - border;
1259 } else {
1260 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1261 }
1262 if (srcRect.height() > 1) {
1263 GrScalar border = GR_ScalarHalf / bitmap.height();
1264 top = paintRect.top() + border;
1265 bottom = paintRect.bottom() - border;
1266 } else {
1267 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1268 }
1269 GrRect textureDomain;
1270 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001271 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001272 }
1273
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001274 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001275}
1276
1277void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1278 int left, int top, const SkPaint& paint) {
1279 CHECK_SHOULD_DRAW(draw);
1280
1281 SkAutoLockPixels alp(bitmap);
1282 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1283 return;
1284 }
1285
bsalomon@google.com5782d712011-01-21 21:03:59 +00001286 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001287 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001288 return;
1289 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001290
bsalomon@google.com5782d712011-01-21 21:03:59 +00001291 GrAutoMatrix avm(fContext, GrMatrix::I());
1292
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001293 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001294
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001295 GrTexture* texture;
1296 sampler->setClampNoFilter();
1297 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1298
1299 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001300
bsalomon@google.com5782d712011-01-21 21:03:59 +00001301 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001302 GrRect::MakeXYWH(GrIntToScalar(left),
1303 GrIntToScalar(top),
1304 GrIntToScalar(bitmap.width()),
1305 GrIntToScalar(bitmap.height())),
1306 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001307}
1308
1309void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1310 int x, int y, const SkPaint& paint) {
1311 CHECK_SHOULD_DRAW(draw);
1312
bsalomon@google.com5782d712011-01-21 21:03:59 +00001313 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001314 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001315 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001316 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001317 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001318
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001319 GrTexture* devTex = grPaint.getTexture(0);
1320 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001321
1322 const SkBitmap& bm = dev->accessBitmap(false);
1323 int w = bm.width();
1324 int h = bm.height();
1325
1326 GrAutoMatrix avm(fContext, GrMatrix::I());
1327
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001328 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001329
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001330 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1331 GrIntToScalar(y),
1332 GrIntToScalar(w),
1333 GrIntToScalar(h));
1334 // The device being drawn may not fill up its texture (saveLayer uses
1335 // the approximate ).
1336 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1337 GR_Scalar1 * h / devTex->height());
1338
1339 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001340}
1341
1342///////////////////////////////////////////////////////////////////////////////
1343
1344// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001345static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1346 kTriangles_PrimitiveType,
1347 kTriangleStrip_PrimitiveType,
1348 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001349};
1350
1351void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1352 int vertexCount, const SkPoint vertices[],
1353 const SkPoint texs[], const SkColor colors[],
1354 SkXfermode* xmode,
1355 const uint16_t indices[], int indexCount,
1356 const SkPaint& paint) {
1357 CHECK_SHOULD_DRAW(draw);
1358
bsalomon@google.com5782d712011-01-21 21:03:59 +00001359 GrPaint grPaint;
1360 SkAutoCachedTexture act;
1361 // we ignore the shader if texs is null.
1362 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001363 if (!this->skPaint2GrPaintNoShader(paint,
1364 false,
1365 &grPaint,
1366 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001367 return;
1368 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001369 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001370 if (!this->skPaint2GrPaintShader(paint, &act,
1371 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001372 &grPaint,
1373 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001374 return;
1375 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001376 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001377
1378 if (NULL != xmode && NULL != texs && NULL != colors) {
1379 SkXfermode::Mode mode;
1380 if (!SkXfermode::IsMode(xmode, &mode) ||
1381 SkXfermode::kMultiply_Mode != mode) {
1382 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1383#if 0
1384 return
1385#endif
1386 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001387 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001388
bsalomon@google.com498776a2011-08-16 19:20:44 +00001389 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1390 if (NULL != colors) {
1391 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001392 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001393 for (int i = 0; i < vertexCount; ++i) {
1394 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1395 }
1396 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001397 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001398 fContext->drawVertices(grPaint,
1399 gVertexMode2PrimitiveType[vmode],
1400 vertexCount,
1401 (GrPoint*) vertices,
1402 (GrPoint*) texs,
1403 colors,
1404 indices,
1405 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001406}
1407
1408///////////////////////////////////////////////////////////////////////////////
1409
1410static void GlyphCacheAuxProc(void* data) {
1411 delete (GrFontScaler*)data;
1412}
1413
1414static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1415 void* auxData;
1416 GrFontScaler* scaler = NULL;
1417 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1418 scaler = (GrFontScaler*)auxData;
1419 }
1420 if (NULL == scaler) {
1421 scaler = new SkGrFontScaler(cache);
1422 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1423 }
1424 return scaler;
1425}
1426
1427static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1428 SkFixed fx, SkFixed fy,
1429 const SkGlyph& glyph) {
1430 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1431
1432 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1433
1434 if (NULL == procs->fFontScaler) {
1435 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1436 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001437
1438 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001439 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001440 *
reed@google.com3b139f52011-06-07 17:56:25 +00001441 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1442 * It calls that rather than round, because our caller has already added
1443 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1444 *
1445 * Test code between raster and gpu (they should draw the same)
1446 *
1447 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1448 *
1449 * Perhaps we should only perform this integralization if there is no
1450 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001451 */
reed@google.com3b139f52011-06-07 17:56:25 +00001452 fy = SkFixedFloorToFixed(fy);
1453
reed@google.comac10a2d2010-12-22 21:39:39 +00001454 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001455 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001456 procs->fFontScaler);
1457}
1458
bsalomon@google.com5782d712011-01-21 21:03:59 +00001459SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001460
1461 // deferred allocation
1462 if (NULL == fDrawProcs) {
1463 fDrawProcs = new GrSkDrawProcs;
1464 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1465 fDrawProcs->fContext = fContext;
1466 }
1467
1468 // init our (and GL's) state
1469 fDrawProcs->fTextContext = context;
1470 fDrawProcs->fFontScaler = NULL;
1471 return fDrawProcs;
1472}
1473
1474void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1475 size_t byteLength, SkScalar x, SkScalar y,
1476 const SkPaint& paint) {
1477 CHECK_SHOULD_DRAW(draw);
1478
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001479 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001480 // this guy will just call our drawPath()
1481 draw.drawText((const char*)text, byteLength, x, y, paint);
1482 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001483 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001484
1485 GrPaint grPaint;
1486 SkAutoCachedTexture act;
1487
Scroggod757df22011-05-16 13:11:16 +00001488 if (!this->skPaint2GrPaintShader(paint,
1489 &act,
1490 *draw.fMatrix,
1491 &grPaint,
1492 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001493 return;
1494 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001495 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001496 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001497 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1498 }
1499}
1500
1501void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1502 size_t byteLength, const SkScalar pos[],
1503 SkScalar constY, int scalarsPerPos,
1504 const SkPaint& paint) {
1505 CHECK_SHOULD_DRAW(draw);
1506
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001507 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001508 // this guy will just call our drawPath()
1509 draw.drawPosText((const char*)text, byteLength, pos, constY,
1510 scalarsPerPos, paint);
1511 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001512 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001513
1514 GrPaint grPaint;
1515 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001516 if (!this->skPaint2GrPaintShader(paint,
1517 &act,
1518 *draw.fMatrix,
1519 &grPaint,
1520 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001521 return;
1522 }
1523
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001524 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001525 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001526 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1527 scalarsPerPos, paint);
1528 }
1529}
1530
1531void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1532 size_t len, const SkPath& path,
1533 const SkMatrix* m, const SkPaint& paint) {
1534 CHECK_SHOULD_DRAW(draw);
1535
1536 SkASSERT(draw.fDevice == this);
1537 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1538}
1539
1540///////////////////////////////////////////////////////////////////////////////
1541
reed@google.comf67e4cf2011-03-15 20:56:58 +00001542bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1543 if (!paint.isLCDRenderText()) {
1544 // we're cool with the paint as is
1545 return false;
1546 }
1547
1548 if (paint.getShader() ||
1549 paint.getXfermode() || // unless its srcover
1550 paint.getMaskFilter() ||
1551 paint.getRasterizer() ||
1552 paint.getColorFilter() ||
1553 paint.getPathEffect() ||
1554 paint.isFakeBoldText() ||
1555 paint.getStyle() != SkPaint::kFill_Style) {
1556 // turn off lcd
1557 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1558 flags->fHinting = paint.getHinting();
1559 return true;
1560 }
1561 // we're cool with the paint as is
1562 return false;
1563}
1564
1565///////////////////////////////////////////////////////////////////////////////
1566
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001567SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1568 const GrSamplerState& sampler,
1569 TexType type) {
1570 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001571 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001572
bsalomon@google.come97f0852011-06-17 13:10:25 +00001573 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001574 const GrTextureDesc desc = {
1575 kRenderTarget_GrTextureFlagBit,
1576 kNone_GrAALevel,
1577 bitmap.width(),
1578 bitmap.height(),
1579 SkGr::Bitmap2PixelConfig(bitmap)
1580 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001581 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001582 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001583 // we know layers will only be drawn through drawDevice.
1584 // drawDevice has been made to work with content embedded in a
1585 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001586 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001587 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001588 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001589 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001590 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001591 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001592 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001593 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001594 GrContext::TextureKey key = bitmap.getGenerationID();
1595 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001596
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001597 entry = ctx->findAndLockTexture(key, bitmap.width(),
1598 bitmap.height(), sampler);
1599 if (NULL == entry.texture()) {
1600 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001601 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001602 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001603 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001604 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001605 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001606 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001607 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1608 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001609 }
1610 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001611 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001612}
1613
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001614void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1615 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001616}
1617
bsalomon@google.come97f0852011-06-17 13:10:25 +00001618SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1619 int width, int height,
1620 bool isOpaque,
1621 Usage usage) {
1622 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1623 width, height, usage));
1624}
1625