blob: 031333bc2828af5ee4ab3030330c833fdd866e52 [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;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000119 case kSkia8888_PM_GrPixelConfig:
120 // we don't currently have a way of knowing whether
121 // a 8888 is opaque based on the config.
122 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000123 return SkBitmap::kARGB_8888_Config;
124 default:
125 *isOpaque = false;
126 return SkBitmap::kNo_Config;
127 }
128}
reed@google.comac10a2d2010-12-22 21:39:39 +0000129
reed@google.comaf951c92011-06-16 19:10:39 +0000130static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000131 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000132
133 bool isOpaque;
134 SkBitmap bitmap;
135 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
136 renderTarget->width(), renderTarget->height());
137 bitmap.setIsOpaque(isOpaque);
138 return bitmap;
139}
140
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000141SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
142: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
143 this->initFromRenderTarget(context, texture->asRenderTarget());
144}
145
reed@google.comaf951c92011-06-16 19:10:39 +0000146SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
147: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000148 this->initFromRenderTarget(context, renderTarget);
149}
150
151void SkGpuDevice::initFromRenderTarget(GrContext* context,
152 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000153 fNeedPrepareRenderTarget = false;
154 fDrawProcs = NULL;
155
156 fContext = context;
157 fContext->ref();
158
reed@google.comaf951c92011-06-16 19:10:39 +0000159 fTexture = NULL;
160 fRenderTarget = NULL;
161 fNeedClear = false;
162
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000163 GrAssert(NULL != renderTarget);
164 fRenderTarget = renderTarget;
165 fRenderTarget->ref();
166 // if this RT is also a texture, hold a ref on it
167 fTexture = fRenderTarget->asTexture();
168 SkSafeRef(fTexture);
reed@google.comaf951c92011-06-16 19:10:39 +0000169
170 SkGrRenderTargetPixelRef* pr = new SkGrRenderTargetPixelRef(fRenderTarget);
171 this->setPixelRef(pr, 0)->unref();
172}
173
174SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000175 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000176: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000177 fNeedPrepareRenderTarget = false;
178 fDrawProcs = NULL;
179
reed@google.com7b201d22011-01-11 18:59:23 +0000180 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000181 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000182
reed@google.comac10a2d2010-12-22 21:39:39 +0000183 fTexture = NULL;
184 fRenderTarget = NULL;
185 fNeedClear = false;
186
reed@google.comaf951c92011-06-16 19:10:39 +0000187 if (config != SkBitmap::kRGB_565_Config) {
188 config = SkBitmap::kARGB_8888_Config;
189 }
190 SkBitmap bm;
191 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000192
193#if CACHE_LAYER_TEXTURES
bsalomon@google.come97f0852011-06-17 13:10:25 +0000194 TexType type = (kSaveLayer_Usage == usage) ?
195 kSaveLayerDeviceRenderTarget_TexType :
196 kDeviceRenderTarget_TexType;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000197 fCache = this->lockCachedTexture(bm, GrSamplerState::ClampNoFilter(), type);
198 fTexture = fCache.texture();
199 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000200 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000201 // hold a ref directly on fTexture (even though fCache has one) to match
202 // other constructor paths. Simplifies cleanup.
203 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000204 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000205#else
reed@google.comaf951c92011-06-16 19:10:39 +0000206 const GrTextureDesc desc = {
207 kRenderTarget_GrTextureFlagBit,
208 kNone_GrAALevel,
209 width,
210 height,
211 SkGr::Bitmap2PixelConfig(bm)
212 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000213
reed@google.comaf951c92011-06-16 19:10:39 +0000214 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000215#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000216 if (NULL != fTexture) {
217 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000218 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000219
reed@google.comaf951c92011-06-16 19:10:39 +0000220 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000221
reed@google.comaf951c92011-06-16 19:10:39 +0000222 // we defer the actual clear until our gainFocus()
223 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000224
reed@google.comaf951c92011-06-16 19:10:39 +0000225 // wrap the bitmap with a pixelref to expose our texture
226 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000227 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000228 } else {
229 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
230 width, height);
231 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000232 }
233}
234
235SkGpuDevice::~SkGpuDevice() {
236 if (fDrawProcs) {
237 delete fDrawProcs;
238 }
239
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000240 SkSafeUnref(fTexture);
241 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000242 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000243 GrAssert(NULL != fTexture);
244 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000245 fContext->unlockTexture(fCache);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000246 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000247 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000248}
249
reed@google.comac10a2d2010-12-22 21:39:39 +0000250///////////////////////////////////////////////////////////////////////////////
251
252void SkGpuDevice::makeRenderTargetCurrent() {
253 fContext->setRenderTarget(fRenderTarget);
254 fContext->flush(true);
255 fNeedPrepareRenderTarget = true;
256}
257
258///////////////////////////////////////////////////////////////////////////////
259
bsalomon@google.comc4364992011-11-07 15:54:49 +0000260namespace {
261GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
262 switch (config8888) {
263 case SkCanvas::kNative_Premul_Config8888:
264 return kSkia8888_PM_GrPixelConfig;
265 case SkCanvas::kNative_Unpremul_Config8888:
266 return kSkia8888_UPM_GrPixelConfig;
267 case SkCanvas::kBGRA_Premul_Config8888:
268 return kBGRA_8888_PM_GrPixelConfig;
269 case SkCanvas::kBGRA_Unpremul_Config8888:
270 return kBGRA_8888_UPM_GrPixelConfig;
271 case SkCanvas::kRGBA_Premul_Config8888:
272 return kRGBA_8888_PM_GrPixelConfig;
273 case SkCanvas::kRGBA_Unpremul_Config8888:
274 return kRGBA_8888_UPM_GrPixelConfig;
275 default:
276 GrCrash("Unexpected Config8888.");
277 return kSkia8888_PM_GrPixelConfig;
278 }
279}
280}
281
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000282bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
283 int x, int y,
284 SkCanvas::Config8888 config8888) {
bsalomon@google.com910267d2011-11-02 20:06:25 +0000285 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
286 SkASSERT(!bitmap.isNull());
287 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000288
bsalomon@google.com910267d2011-11-02 20:06:25 +0000289 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000290 GrPixelConfig config;
291 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000292 return fContext->readRenderTargetPixels(fRenderTarget,
293 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000294 bitmap.width(),
295 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000296 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000297 bitmap.getPixels(),
298 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000299}
300
301void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y) {
302 SkAutoLockPixels alp(bitmap);
303 if (!bitmap.readyToDraw()) {
304 return;
305 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000306 GrPixelConfig config = SkGr::BitmapConfig2PixelConfig(bitmap.config(),
307 bitmap.isOpaque());
reed@google.comac10a2d2010-12-22 21:39:39 +0000308 fContext->setRenderTarget(fRenderTarget);
309 // we aren't setting the clip or matrix, so mark as dirty
310 // we don't need to set them for this call and don't have them anyway
311 fNeedPrepareRenderTarget = true;
312
313 fContext->writePixels(x, y, bitmap.width(), bitmap.height(),
314 config, bitmap.getPixels(), bitmap.rowBytes());
315}
316
317///////////////////////////////////////////////////////////////////////////////
318
319static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000320 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000321 const SkRegion& clipRegion,
322 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000323 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000324
325 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000326 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000327 const SkIRect& skBounds = clipRegion.getBounds();
328 GrRect bounds;
329 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
330 GrIntToScalar(skBounds.fTop),
331 GrIntToScalar(skBounds.fRight),
332 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000333 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
334 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000335 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000336}
337
338// call this ever each draw call, to ensure that the context reflects our state,
339// and not the state from some other canvas/device
340void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
341 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000342 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000343
344 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000345 SkASSERT(draw.fClipStack);
346 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000347 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 fNeedPrepareRenderTarget = false;
349 }
350}
351
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000352void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
353 const SkClipStack& clipStack) {
354 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
355 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000356 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000357}
358
359void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000360 const SkRegion& clip, const SkClipStack& clipStack) {
361
reed@google.comac10a2d2010-12-22 21:39:39 +0000362 fContext->setRenderTarget(fRenderTarget);
363
bsalomon@google.comd302f142011-03-03 13:54:13 +0000364 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000365
reed@google.com6f8f2922011-03-04 22:27:10 +0000366 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000367
368 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000369 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000370 fNeedClear = false;
371 }
372}
373
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000374bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000375 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000376 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000377 return true;
378 }
379 return false;
380}
381
382///////////////////////////////////////////////////////////////////////////////
383
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000384SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
385SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
386SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
387SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
388SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
389 shader_type_mismatch);
390SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000391
bsalomon@google.com5782d712011-01-21 21:03:59 +0000392static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
393 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
394 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
395 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
396 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
397 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
398};
399
400bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
401 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000402 GrPaint* grPaint,
403 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000404
405 grPaint->fDither = skPaint.isDither();
406 grPaint->fAntiAlias = skPaint.isAntiAlias();
407
408 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
409 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
410
411 SkXfermode* mode = skPaint.getXfermode();
412 if (mode) {
413 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000414 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000415#if 0
416 return false;
417#endif
418 }
419 }
420 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
421 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
422
423 if (justAlpha) {
424 uint8_t alpha = skPaint.getAlpha();
425 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000426 // justAlpha is currently set to true only if there is a texture,
427 // so constantColor should not also be true.
428 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000429 } else {
430 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000431 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000432 }
Scroggo97c88c22011-05-11 14:05:25 +0000433 SkColorFilter* colorFilter = skPaint.getColorFilter();
434 SkColor color;
435 SkXfermode::Mode filterMode;
436 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000437 if (!constantColor) {
438 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
439 grPaint->fColorFilterXfermode = filterMode;
440 return true;
441 }
442 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
443 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000444 }
Scroggod757df22011-05-16 13:11:16 +0000445 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000446 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000447}
448
bsalomon@google.com5782d712011-01-21 21:03:59 +0000449bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
450 SkAutoCachedTexture* act,
451 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000452 GrPaint* grPaint,
453 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000454
bsalomon@google.com5782d712011-01-21 21:03:59 +0000455 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000456
bsalomon@google.com5782d712011-01-21 21:03:59 +0000457 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000458 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000459 return this->skPaint2GrPaintNoShader(skPaint,
460 false,
461 grPaint,
462 constantColor);
463 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000464 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000465 }
466
reed@google.comac10a2d2010-12-22 21:39:39 +0000467 SkBitmap bitmap;
468 SkMatrix matrix;
469 SkShader::TileMode tileModes[2];
470 SkScalar twoPointParams[3];
471 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
472 tileModes, twoPointParams);
473
bsalomon@google.com5782d712011-01-21 21:03:59 +0000474 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
475 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000476 SkShader::GradientInfo info;
477 SkColor color;
478
479 info.fColors = &color;
480 info.fColorOffsets = NULL;
481 info.fColorCount = 1;
482 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
483 SkPaint copy(skPaint);
484 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000485 // modulate the paint alpha by the shader's solid color alpha
486 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
487 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000488 return this->skPaint2GrPaintNoShader(copy,
489 false,
490 grPaint,
491 constantColor);
492 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000493 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000495 GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx);
496 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000497 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000498 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000499 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000500 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000501 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000502 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
503 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000504 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000505 sampler->setRadial2Params(twoPointParams[0],
506 twoPointParams[1],
507 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000508 }
509
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000510 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000512 SkDebugf("Couldn't convert bitmap to texture.\n");
513 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000514 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000515 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000516
517 // since our texture coords will be in local space, we wack the texture
518 // matrix to map them back into 0...1 before we load it
519 SkMatrix localM;
520 if (shader->getLocalMatrix(&localM)) {
521 SkMatrix inverse;
522 if (localM.invert(&inverse)) {
523 matrix.preConcat(inverse);
524 }
525 }
526 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000527 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
528 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000529 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000530 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000531 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000532 matrix.postScale(s, s);
533 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000534 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000535
536 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000537}
538
539///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000540
bsalomon@google.com398109c2011-04-14 18:40:27 +0000541void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000542 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000543}
544
reed@google.comac10a2d2010-12-22 21:39:39 +0000545void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
546 CHECK_SHOULD_DRAW(draw);
547
bsalomon@google.com5782d712011-01-21 21:03:59 +0000548 GrPaint grPaint;
549 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000550 if (!this->skPaint2GrPaintShader(paint,
551 &act,
552 *draw.fMatrix,
553 &grPaint,
554 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000555 return;
556 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000557
558 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000559}
560
561// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000562static const GrPrimitiveType gPointMode2PrimtiveType[] = {
563 kPoints_PrimitiveType,
564 kLines_PrimitiveType,
565 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000566};
567
568void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000569 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000570 CHECK_SHOULD_DRAW(draw);
571
572 SkScalar width = paint.getStrokeWidth();
573 if (width < 0) {
574 return;
575 }
576
577 // we only handle hairlines here, else we let the SkDraw call our drawPath()
578 if (width > 0) {
579 draw.drawPoints(mode, count, pts, paint, true);
580 return;
581 }
582
bsalomon@google.com5782d712011-01-21 21:03:59 +0000583 GrPaint grPaint;
584 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000585 if (!this->skPaint2GrPaintShader(paint,
586 &act,
587 *draw.fMatrix,
588 &grPaint,
589 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000590 return;
591 }
592
bsalomon@google.com5782d712011-01-21 21:03:59 +0000593 fContext->drawVertices(grPaint,
594 gPointMode2PrimtiveType[mode],
595 count,
596 (GrPoint*)pts,
597 NULL,
598 NULL,
599 NULL,
600 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000601}
602
reed@google.comc9aa5872011-04-05 21:05:37 +0000603///////////////////////////////////////////////////////////////////////////////
604
reed@google.comac10a2d2010-12-22 21:39:39 +0000605void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
606 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000607 CHECK_SHOULD_DRAW(draw);
608
bungeman@google.com79bd8772011-07-18 15:34:08 +0000609 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000610 SkScalar width = paint.getStrokeWidth();
611
612 /*
613 We have special code for hairline strokes, miter-strokes, and fills.
614 Anything else we just call our path code.
615 */
616 bool usePath = doStroke && width > 0 &&
617 paint.getStrokeJoin() != SkPaint::kMiter_Join;
618 // another reason we might need to call drawPath...
619 if (paint.getMaskFilter()) {
620 usePath = true;
621 }
reed@google.com67db6642011-05-26 11:46:35 +0000622 // until we aa rotated rects...
623 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
624 usePath = true;
625 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000626 // small miter limit means right angles show bevel...
627 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
628 paint.getStrokeMiter() < SK_ScalarSqrt2)
629 {
630 usePath = true;
631 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000632 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000633 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
634 usePath = true;
635 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000636
637 if (usePath) {
638 SkPath path;
639 path.addRect(rect);
640 this->drawPath(draw, path, paint, NULL, true);
641 return;
642 }
643
644 GrPaint grPaint;
645 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000646 if (!this->skPaint2GrPaintShader(paint,
647 &act,
648 *draw.fMatrix,
649 &grPaint,
650 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000651 return;
652 }
reed@google.com20efde72011-05-09 17:00:02 +0000653 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000654}
655
reed@google.com69302852011-02-16 18:08:07 +0000656#include "SkMaskFilter.h"
657#include "SkBounder.h"
658
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000659static GrPathFill skToGrFillType(SkPath::FillType fillType) {
660 switch (fillType) {
661 case SkPath::kWinding_FillType:
662 return kWinding_PathFill;
663 case SkPath::kEvenOdd_FillType:
664 return kEvenOdd_PathFill;
665 case SkPath::kInverseWinding_FillType:
666 return kInverseWinding_PathFill;
667 case SkPath::kInverseEvenOdd_FillType:
668 return kInverseEvenOdd_PathFill;
669 default:
670 SkDebugf("Unsupported path fill type\n");
671 return kHairLine_PathFill;
672 }
673}
674
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000675static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000676 int halfWidth = (kernelWidth - 1) / 2;
677 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000678 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000679 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000680 float x = static_cast<float>(i - halfWidth);
681 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
682 // is dropped here, since we renormalize the kernel below.
683 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000684 sum += kernel[i];
685 }
686 // Normalize the kernel
687 float scale = 1.0f / sum;
688 for (int i = 0; i < kernelWidth; ++i)
689 kernel[i] *= scale;
690}
691
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000692static void scaleRect(SkRect* rect, float scale) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000693 rect->fLeft *= scale;
694 rect->fTop *= scale;
695 rect->fRight *= scale;
696 rect->fBottom *= scale;
697}
698
699static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
700 SkMaskFilter* filter, const SkMatrix& matrix,
701 const SkRegion& clip, SkBounder* bounder,
702 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000703#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000704 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000705#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000706 SkMaskFilter::BlurInfo info;
707 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000708 if (SkMaskFilter::kNone_BlurType == blurType ||
709 !context->supportsShaders()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000710 return false;
711 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000712 SkScalar radius = info.fIgnoreTransform ? info.fRadius
713 : matrix.mapRadius(info.fRadius);
714 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000715 if (radius <= 0) {
716 return false;
717 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000718 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000719 SkRect srcRect = path.getBounds();
720
721 int scaleFactor = 1;
722
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000723 while (sigma > MAX_BLUR_SIGMA) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000724 scaleFactor *= 2;
725 sigma *= 0.5f;
726 }
senorblanco@chromium.org4a947d22011-07-18 21:48:35 +0000727 int halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000728 int kernelWidth = halfWidth * 2 + 1;
729
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000730 float invScale = 1.0f / scaleFactor;
731 scaleRect(&srcRect, invScale);
732 srcRect.roundOut();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000733 srcRect.inset(-halfWidth, -halfWidth);
734
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000735 SkRect clipBounds;
736 clipBounds.set(clip.getBounds());
737 scaleRect(&clipBounds, invScale);
738 clipBounds.roundOut();
739 clipBounds.inset(-halfWidth, -halfWidth);
740
741 srcRect.intersect(clipBounds);
742
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000743 scaleRect(&srcRect, scaleFactor);
744 SkRect finalRect = srcRect;
745
746 SkIRect finalIRect;
747 finalRect.roundOut(&finalIRect);
748 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000749 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000750 }
751 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000752 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000753 }
754 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
755 srcRect.offset(-srcRect.fLeft, -srcRect.fTop);
756 const GrTextureDesc desc = {
757 kRenderTarget_GrTextureFlagBit,
758 kNone_GrAALevel,
759 srcRect.width(),
760 srcRect.height(),
761 // We actually only need A8, but it often isn't supported as a
762 // render target
bsalomon@google.comc4364992011-11-07 15:54:49 +0000763 kRGBA_8888_PM_GrPixelConfig
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000764 };
765
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000766 GrAutoScratchTexture srcEntry(context, desc);
767 GrAutoScratchTexture dstEntry(context, desc);
768 if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000769 return false;
770 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000771 GrTexture* srcTexture = srcEntry.texture();
772 GrTexture* dstTexture = dstEntry.texture();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000773 if (NULL == srcTexture || NULL == dstTexture) {
774 return false;
775 }
776 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000777 // Once this code moves into GrContext, this should be changed to use
778 // an AutoClipRestore.
779 GrClip oldClip = context->getClip();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000780 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000781 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000782 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000783 GrPaint tempPaint;
784 tempPaint.reset();
785
786 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000787 tempPaint.fAntiAlias = grp->fAntiAlias;
788 if (tempPaint.fAntiAlias) {
789 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
790 // blend coeff of zero requires dual source blending support in order
791 // to properly blend partially covered pixels. This means the AA
792 // code path may not be taken. So we use a dst blend coeff of ISA. We
793 // could special case AA draws to a dst surface with known alpha=0 to
794 // use a zero dst coeff when dual source blending isn't available.
795 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
796 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
797 }
798 // Draw hard shadow to dstTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000799 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000800 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000801
802 GrMatrix sampleM;
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000803 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000804 GrPaint paint;
805 paint.reset();
806 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
807 paint.getTextureSampler(0)->setMatrix(sampleM);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000808 GrAutoScratchTexture origEntry;
809
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000810 if (blurType != SkMaskFilter::kNormal_BlurType) {
811 // Stash away a copy of the unblurred image.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000812 origEntry.set(context, desc);
813 if (NULL == origEntry.texture()) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000814 return false;
815 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000816 context->setRenderTarget(origEntry.texture()->asRenderTarget());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000817 paint.setTexture(0, srcTexture);
818 context->drawRect(paint, srcRect);
819 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000820 for (int i = 1; i < scaleFactor; i *= 2) {
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000821 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
822 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000823 context->setRenderTarget(dstTexture->asRenderTarget());
824 SkRect dstRect(srcRect);
825 scaleRect(&dstRect, 0.5f);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000826 paint.setTexture(0, srcTexture);
827 context->drawRectToRect(paint, dstRect, srcRect);
828 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000829 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000830 }
831
832 SkAutoTMalloc<float> kernelStorage(kernelWidth);
833 float* kernel = kernelStorage.get();
834 buildKernel(sigma, kernel, kernelWidth);
835
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000836 // Clear out a halfWidth to the right of the srcRect to prevent the
837 // X convolution from reading garbage.
838 SkIRect clearRect = SkIRect::MakeXYWH(
839 srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height());
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->convolveInX(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 out a halfWidth below the srcRect to prevent the Y
847 // convolution from reading garbage.
848 clearRect = SkIRect::MakeXYWH(
849 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth);
850 context->clear(&clearRect, 0x0);
851
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000852 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000853 context->convolveInY(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000854 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000855
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000856 // Clear one pixel to the right and below, to accommodate bilinear
857 // upsampling.
858 clearRect = SkIRect::MakeXYWH(
859 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
860 context->clear(&clearRect, 0x0);
861 clearRect = SkIRect::MakeXYWH(
862 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
863 context->clear(&clearRect, 0x0);
864
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000865 if (scaleFactor > 1) {
866 // FIXME: This should be mitchell, not bilinear.
867 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000868 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000869 paint.getTextureSampler(0)->setMatrix(sampleM);
870 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871 paint.setTexture(0, srcTexture);
872 SkRect dstRect(srcRect);
873 scaleRect(&dstRect, scaleFactor);
874 context->drawRectToRect(paint, dstRect, srcRect);
875 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000876 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000877 }
878
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000879 if (blurType != SkMaskFilter::kNormal_BlurType) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000880 GrTexture* origTexture = origEntry.texture();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000881 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000882 sampleM.setIDiv(origTexture->width(), origTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000883 paint.getTextureSampler(0)->setMatrix(sampleM);
884 // Blend origTexture over srcTexture.
885 context->setRenderTarget(srcTexture->asRenderTarget());
886 paint.setTexture(0, origTexture);
887 if (SkMaskFilter::kInner_BlurType == blurType) {
888 // inner: dst = dst * src
889 paint.fSrcBlendCoeff = kDC_BlendCoeff;
890 paint.fDstBlendCoeff = kZero_BlendCoeff;
891 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
892 // solid: dst = src + dst - src * dst
893 // = (1 - dst) * src + 1 * dst
894 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
895 paint.fDstBlendCoeff = kOne_BlendCoeff;
896 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
897 // outer: dst = dst * (1 - src)
898 // = 0 * src + (1 - src) * dst
899 paint.fSrcBlendCoeff = kZero_BlendCoeff;
900 paint.fDstBlendCoeff = kISC_BlendCoeff;
901 }
902 context->drawRect(paint, srcRect);
903 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000904 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000905 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000906
907 if (grp->hasTextureOrMask()) {
908 GrMatrix inverse;
909 if (!matrix.invert(&inverse)) {
910 return false;
911 }
912 grp->preConcatActiveSamplerMatrices(inverse);
913 }
914
915 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
916 // we assume the last mask index is available for use
917 GrAssert(NULL == grp->getMask(MASK_IDX));
918 grp->setMask(MASK_IDX, srcTexture);
919 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
920
921 GrMatrix m;
922 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
923 m.postIDiv(srcTexture->width(), srcTexture->height());
924 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
925 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000926 return true;
927}
928
reed@google.com69302852011-02-16 18:08:07 +0000929static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
930 SkMaskFilter* filter, const SkMatrix& matrix,
931 const SkRegion& clip, SkBounder* bounder,
932 GrPaint* grp) {
933 SkMask srcM, dstM;
934
935 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
936 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
937 return false;
938 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000939 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000940
941 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
942 return false;
943 }
944 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000945 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000946
947 if (clip.quickReject(dstM.fBounds)) {
948 return false;
949 }
950 if (bounder && !bounder->doIRect(dstM.fBounds)) {
951 return false;
952 }
953
954 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
955 // the current clip (and identity matrix) and grpaint settings
956
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000957 // used to compute inverse view, if necessary
958 GrMatrix ivm = context->getMatrix();
959
reed@google.com0c219b62011-02-16 21:31:18 +0000960 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000961
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000962 const GrTextureDesc desc = {
963 kNone_GrTextureFlags,
964 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +0000965 dstM.fBounds.width(),
966 dstM.fBounds.height(),
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000967 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +0000968 };
969
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000970 GrAutoScratchTexture ast(context, desc);
971 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000972
reed@google.com69302852011-02-16 18:08:07 +0000973 if (NULL == texture) {
974 return false;
975 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000976 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
977 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000978
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000979 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
980 grp->preConcatActiveSamplerMatrices(ivm);
981 }
982
983 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
984 // we assume the last mask index is available for use
985 GrAssert(NULL == grp->getMask(MASK_IDX));
986 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000987 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +0000988
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000989 GrRect d;
990 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000991 GrIntToScalar(dstM.fBounds.fTop),
992 GrIntToScalar(dstM.fBounds.fRight),
993 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000994
995 GrMatrix m;
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000996 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
997 -dstM.fBounds.fTop*SK_Scalar1);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000998 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000999 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1000
1001 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001002 return true;
1003}
reed@google.com69302852011-02-16 18:08:07 +00001004
reed@google.com0c219b62011-02-16 21:31:18 +00001005void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comfe626382011-09-21 13:50:35 +00001006 const SkPaint& origPaint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001007 bool pathIsMutable) {
1008 CHECK_SHOULD_DRAW(draw);
1009
reed@google.comfe626382011-09-21 13:50:35 +00001010 bool doFill = true;
1011 SkTLazy<SkPaint> lazyPaint;
1012 const SkPaint* paint = &origPaint;
1013
1014 // can we cheat, and threat a thin stroke as a hairline (w/ modulated alpha)
1015 // if we can, we draw lots faster (raster device does this same test)
1016 {
1017 SkAlpha newAlpha;
1018 if (SkDrawTreatAsHairline(*paint, *draw.fMatrix, &newAlpha)) {
1019 lazyPaint.set(*paint);
1020 lazyPaint.get()->setAlpha(newAlpha);
1021 lazyPaint.get()->setStrokeWidth(0);
1022 paint = lazyPaint.get();
1023 doFill = false;
1024 }
1025 }
1026 // must reference paint from here down, and not origPaint
1027 // since we may have change the paint (using lazyPaint for storage)
1028
bsalomon@google.com5782d712011-01-21 21:03:59 +00001029 GrPaint grPaint;
1030 SkAutoCachedTexture act;
reed@google.comfe626382011-09-21 13:50:35 +00001031 if (!this->skPaint2GrPaintShader(*paint,
Scroggod757df22011-05-16 13:11:16 +00001032 &act,
1033 *draw.fMatrix,
1034 &grPaint,
1035 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001036 return;
1037 }
1038
reed@google.comfe626382011-09-21 13:50:35 +00001039 // If we have a prematrix, apply it to the path, optimizing for the case
1040 // where the original path can in fact be modified in place (even though
1041 // its parameter type is const).
1042 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1043 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001044
1045 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001046 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001047
reed@google.come3445642011-02-16 23:20:39 +00001048 if (!pathIsMutable) {
1049 result = &tmpPath;
1050 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001051 }
reed@google.come3445642011-02-16 23:20:39 +00001052 // should I push prePathMatrix on our MV stack temporarily, instead
1053 // of applying it here? See SkDraw.cpp
1054 pathPtr->transform(*prePathMatrix, result);
1055 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001056 }
reed@google.com0c219b62011-02-16 21:31:18 +00001057 // at this point we're done with prePathMatrix
1058 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001059
reed@google.comfe626382011-09-21 13:50:35 +00001060 if (doFill && (paint->getPathEffect() ||
1061 paint->getStyle() != SkPaint::kFill_Style)) {
1062 // it is safe to use tmpPath here, even if we already used it for the
1063 // prepathmatrix, since getFillPath can take the same object for its
1064 // input and output safely.
1065 doFill = paint->getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001066 pathPtr = &tmpPath;
1067 }
1068
reed@google.comfe626382011-09-21 13:50:35 +00001069 if (paint->getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001070 // avoid possibly allocating a new path in transform if we can
1071 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1072
1073 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001074 pathPtr->transform(*draw.fMatrix, devPathPtr);
reed@google.comfe626382011-09-21 13:50:35 +00001075 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001076 *draw.fMatrix, *draw.fClip, draw.fBounder,
1077 &grPaint)) {
reed@google.comfe626382011-09-21 13:50:35 +00001078 drawWithMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001079 *draw.fMatrix, *draw.fClip, draw.fBounder,
1080 &grPaint);
1081 }
reed@google.com69302852011-02-16 18:08:07 +00001082 return;
1083 }
reed@google.com69302852011-02-16 18:08:07 +00001084
bsalomon@google.comffca4002011-02-22 20:34:01 +00001085 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001086
reed@google.com0c219b62011-02-16 21:31:18 +00001087 if (doFill) {
1088 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001089 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001090 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 break;
1092 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001093 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001094 break;
1095 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001096 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001097 break;
1098 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001099 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 break;
1101 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001102 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001103 return;
1104 }
1105 }
1106
reed@google.com07f3ee12011-05-16 17:21:57 +00001107 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001108}
1109
reed@google.comac10a2d2010-12-22 21:39:39 +00001110void SkGpuDevice::drawBitmap(const SkDraw& draw,
1111 const SkBitmap& bitmap,
1112 const SkIRect* srcRectPtr,
1113 const SkMatrix& m,
1114 const SkPaint& paint) {
1115 CHECK_SHOULD_DRAW(draw);
1116
1117 SkIRect srcRect;
1118 if (NULL == srcRectPtr) {
1119 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1120 } else {
1121 srcRect = *srcRectPtr;
1122 }
1123
junov@google.comd935cfb2011-06-27 20:48:23 +00001124 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001125 // Convert the bitmap to a shader so that the rect can be drawn
1126 // through drawRect, which supports mask filters.
1127 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001128 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001129 if (srcRectPtr) {
1130 if (!bitmap.extractSubset(&tmp, srcRect)) {
1131 return; // extraction failed
1132 }
1133 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001134 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001135 }
1136 SkPaint paintWithTexture(paint);
1137 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1138 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001139 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001140 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001141
junov@google.com1d329782011-07-28 20:10:09 +00001142 // Transform 'm' needs to be concatenated to the draw matrix,
1143 // rather than transforming the primitive directly, so that 'm' will
1144 // also affect the behavior of the mask filter.
1145 SkMatrix drawMatrix;
1146 drawMatrix.setConcat(*draw.fMatrix, m);
1147 SkDraw transformedDraw(draw);
1148 transformedDraw.fMatrix = &drawMatrix;
1149
1150 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1151
junov@google.comd935cfb2011-06-27 20:48:23 +00001152 return;
1153 }
1154
bsalomon@google.com5782d712011-01-21 21:03:59 +00001155 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001156 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001157 return;
1158 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001159 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001160 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001161 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001162 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001163 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001164 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001165
bsalomon@google.com91958362011-06-13 17:58:13 +00001166 const int maxTextureSize = fContext->getMaxTextureSize();
1167 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1168 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001169 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001170 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001171 return;
1172 }
1173
1174 // undo the translate done by SkCanvas
1175 int DX = SkMax32(0, srcRect.fLeft);
1176 int DY = SkMax32(0, srcRect.fTop);
1177 // compute clip bounds in local coordinates
1178 SkIRect clipRect;
1179 {
1180 SkRect r;
1181 r.set(draw.fClip->getBounds());
1182 SkMatrix matrix, inverse;
1183 matrix.setConcat(*draw.fMatrix, m);
1184 if (!matrix.invert(&inverse)) {
1185 return;
1186 }
1187 inverse.mapRect(&r);
1188 r.roundOut(&clipRect);
1189 // apply the canvas' translate to our local clip
1190 clipRect.offset(DX, DY);
1191 }
1192
bsalomon@google.com91958362011-06-13 17:58:13 +00001193 int nx = bitmap.width() / maxTextureSize;
1194 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001195 for (int x = 0; x <= nx; x++) {
1196 for (int y = 0; y <= ny; y++) {
1197 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001198 tileR.set(x * maxTextureSize, y * maxTextureSize,
1199 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001200 if (!SkIRect::Intersects(tileR, clipRect)) {
1201 continue;
1202 }
1203
1204 SkIRect srcR = tileR;
1205 if (!srcR.intersect(srcRect)) {
1206 continue;
1207 }
1208
1209 SkBitmap tmpB;
1210 if (bitmap.extractSubset(&tmpB, tileR)) {
1211 // now offset it to make it "local" to our tmp bitmap
1212 srcR.offset(-tileR.fLeft, -tileR.fTop);
1213
1214 SkMatrix tmpM(m);
1215 {
1216 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1217 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1218 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1219 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001220 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001221 }
1222 }
1223 }
1224}
1225
1226/*
1227 * This is called by drawBitmap(), which has to handle images that may be too
1228 * large to be represented by a single texture.
1229 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001230 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1231 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001232 */
1233void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1234 const SkBitmap& bitmap,
1235 const SkIRect& srcRect,
1236 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001237 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001238 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1239 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001240
reed@google.com9c49bc32011-07-07 13:42:37 +00001241 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001242 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001243 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001244 return;
1245 }
1246
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001247 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1248
1249 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1250 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1251 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1252 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001253
1254 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001255 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 if (NULL == texture) {
1257 return;
1258 }
1259
bsalomon@google.com452943d2011-10-31 17:37:14 +00001260 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001261
reed@google.com20efde72011-05-09 17:00:02 +00001262 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1263 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001264 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001265 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1266 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1267 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001268 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001269
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001270 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001271 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001272 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001273 // If drawing a subrect of the bitmap and filtering is enabled,
1274 // use a constrained texture domain to avoid color bleeding
1275 GrScalar left, top, right, bottom;
1276 if (srcRect.width() > 1) {
1277 GrScalar border = GR_ScalarHalf / bitmap.width();
1278 left = paintRect.left() + border;
1279 right = paintRect.right() - border;
1280 } else {
1281 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1282 }
1283 if (srcRect.height() > 1) {
1284 GrScalar border = GR_ScalarHalf / bitmap.height();
1285 top = paintRect.top() + border;
1286 bottom = paintRect.bottom() - border;
1287 } else {
1288 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1289 }
1290 GrRect textureDomain;
1291 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001292 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001293 }
1294
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001295 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001296}
1297
1298void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1299 int left, int top, const SkPaint& paint) {
1300 CHECK_SHOULD_DRAW(draw);
1301
1302 SkAutoLockPixels alp(bitmap);
1303 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1304 return;
1305 }
1306
bsalomon@google.com5782d712011-01-21 21:03:59 +00001307 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001308 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001309 return;
1310 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001311
bsalomon@google.com5782d712011-01-21 21:03:59 +00001312 GrAutoMatrix avm(fContext, GrMatrix::I());
1313
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001314 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001315
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001316 GrTexture* texture;
1317 sampler->setClampNoFilter();
1318 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1319
1320 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001321
bsalomon@google.com5782d712011-01-21 21:03:59 +00001322 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001323 GrRect::MakeXYWH(GrIntToScalar(left),
1324 GrIntToScalar(top),
1325 GrIntToScalar(bitmap.width()),
1326 GrIntToScalar(bitmap.height())),
1327 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001328}
1329
1330void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1331 int x, int y, const SkPaint& paint) {
1332 CHECK_SHOULD_DRAW(draw);
1333
bsalomon@google.com5782d712011-01-21 21:03:59 +00001334 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001335 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001336 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001337 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001338 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001339
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001340 GrTexture* devTex = grPaint.getTexture(0);
1341 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001342
1343 const SkBitmap& bm = dev->accessBitmap(false);
1344 int w = bm.width();
1345 int h = bm.height();
1346
1347 GrAutoMatrix avm(fContext, GrMatrix::I());
1348
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001349 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001350
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001351 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1352 GrIntToScalar(y),
1353 GrIntToScalar(w),
1354 GrIntToScalar(h));
1355 // The device being drawn may not fill up its texture (saveLayer uses
1356 // the approximate ).
1357 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1358 GR_Scalar1 * h / devTex->height());
1359
1360 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001361}
1362
1363///////////////////////////////////////////////////////////////////////////////
1364
1365// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001366static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1367 kTriangles_PrimitiveType,
1368 kTriangleStrip_PrimitiveType,
1369 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001370};
1371
1372void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1373 int vertexCount, const SkPoint vertices[],
1374 const SkPoint texs[], const SkColor colors[],
1375 SkXfermode* xmode,
1376 const uint16_t indices[], int indexCount,
1377 const SkPaint& paint) {
1378 CHECK_SHOULD_DRAW(draw);
1379
bsalomon@google.com5782d712011-01-21 21:03:59 +00001380 GrPaint grPaint;
1381 SkAutoCachedTexture act;
1382 // we ignore the shader if texs is null.
1383 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001384 if (!this->skPaint2GrPaintNoShader(paint,
1385 false,
1386 &grPaint,
1387 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001388 return;
1389 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001390 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001391 if (!this->skPaint2GrPaintShader(paint, &act,
1392 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001393 &grPaint,
1394 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001395 return;
1396 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001397 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001398
1399 if (NULL != xmode && NULL != texs && NULL != colors) {
1400 SkXfermode::Mode mode;
1401 if (!SkXfermode::IsMode(xmode, &mode) ||
1402 SkXfermode::kMultiply_Mode != mode) {
1403 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1404#if 0
1405 return
1406#endif
1407 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001408 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001409
bsalomon@google.com498776a2011-08-16 19:20:44 +00001410 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1411 if (NULL != colors) {
1412 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001413 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001414 for (int i = 0; i < vertexCount; ++i) {
1415 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1416 }
1417 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001418 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001419 fContext->drawVertices(grPaint,
1420 gVertexMode2PrimitiveType[vmode],
1421 vertexCount,
1422 (GrPoint*) vertices,
1423 (GrPoint*) texs,
1424 colors,
1425 indices,
1426 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001427}
1428
1429///////////////////////////////////////////////////////////////////////////////
1430
1431static void GlyphCacheAuxProc(void* data) {
1432 delete (GrFontScaler*)data;
1433}
1434
1435static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1436 void* auxData;
1437 GrFontScaler* scaler = NULL;
1438 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1439 scaler = (GrFontScaler*)auxData;
1440 }
1441 if (NULL == scaler) {
1442 scaler = new SkGrFontScaler(cache);
1443 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1444 }
1445 return scaler;
1446}
1447
1448static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1449 SkFixed fx, SkFixed fy,
1450 const SkGlyph& glyph) {
1451 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1452
1453 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1454
1455 if (NULL == procs->fFontScaler) {
1456 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1457 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001458
1459 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001460 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001461 *
reed@google.com3b139f52011-06-07 17:56:25 +00001462 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1463 * It calls that rather than round, because our caller has already added
1464 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1465 *
1466 * Test code between raster and gpu (they should draw the same)
1467 *
1468 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1469 *
1470 * Perhaps we should only perform this integralization if there is no
1471 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001472 */
reed@google.com3b139f52011-06-07 17:56:25 +00001473 fy = SkFixedFloorToFixed(fy);
1474
reed@google.comac10a2d2010-12-22 21:39:39 +00001475 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001476 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001477 procs->fFontScaler);
1478}
1479
bsalomon@google.com5782d712011-01-21 21:03:59 +00001480SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001481
1482 // deferred allocation
1483 if (NULL == fDrawProcs) {
1484 fDrawProcs = new GrSkDrawProcs;
1485 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1486 fDrawProcs->fContext = fContext;
1487 }
1488
1489 // init our (and GL's) state
1490 fDrawProcs->fTextContext = context;
1491 fDrawProcs->fFontScaler = NULL;
1492 return fDrawProcs;
1493}
1494
1495void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1496 size_t byteLength, SkScalar x, SkScalar y,
1497 const SkPaint& paint) {
1498 CHECK_SHOULD_DRAW(draw);
1499
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001500 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001501 // this guy will just call our drawPath()
1502 draw.drawText((const char*)text, byteLength, x, y, paint);
1503 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001504 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001505
1506 GrPaint grPaint;
1507 SkAutoCachedTexture act;
1508
Scroggod757df22011-05-16 13:11:16 +00001509 if (!this->skPaint2GrPaintShader(paint,
1510 &act,
1511 *draw.fMatrix,
1512 &grPaint,
1513 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001514 return;
1515 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001516 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001517 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001518 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1519 }
1520}
1521
1522void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1523 size_t byteLength, const SkScalar pos[],
1524 SkScalar constY, int scalarsPerPos,
1525 const SkPaint& paint) {
1526 CHECK_SHOULD_DRAW(draw);
1527
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001528 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001529 // this guy will just call our drawPath()
1530 draw.drawPosText((const char*)text, byteLength, pos, constY,
1531 scalarsPerPos, paint);
1532 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001533 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001534
1535 GrPaint grPaint;
1536 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001537 if (!this->skPaint2GrPaintShader(paint,
1538 &act,
1539 *draw.fMatrix,
1540 &grPaint,
1541 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001542 return;
1543 }
1544
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001545 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001546 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001547 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1548 scalarsPerPos, paint);
1549 }
1550}
1551
1552void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1553 size_t len, const SkPath& path,
1554 const SkMatrix* m, const SkPaint& paint) {
1555 CHECK_SHOULD_DRAW(draw);
1556
1557 SkASSERT(draw.fDevice == this);
1558 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1559}
1560
1561///////////////////////////////////////////////////////////////////////////////
1562
reed@google.comf67e4cf2011-03-15 20:56:58 +00001563bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1564 if (!paint.isLCDRenderText()) {
1565 // we're cool with the paint as is
1566 return false;
1567 }
1568
1569 if (paint.getShader() ||
1570 paint.getXfermode() || // unless its srcover
1571 paint.getMaskFilter() ||
1572 paint.getRasterizer() ||
1573 paint.getColorFilter() ||
1574 paint.getPathEffect() ||
1575 paint.isFakeBoldText() ||
1576 paint.getStyle() != SkPaint::kFill_Style) {
1577 // turn off lcd
1578 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1579 flags->fHinting = paint.getHinting();
1580 return true;
1581 }
1582 // we're cool with the paint as is
1583 return false;
1584}
1585
1586///////////////////////////////////////////////////////////////////////////////
1587
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001588SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1589 const GrSamplerState& sampler,
1590 TexType type) {
1591 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001592 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001593
bsalomon@google.come97f0852011-06-17 13:10:25 +00001594 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001595 const GrTextureDesc desc = {
1596 kRenderTarget_GrTextureFlagBit,
1597 kNone_GrAALevel,
1598 bitmap.width(),
1599 bitmap.height(),
1600 SkGr::Bitmap2PixelConfig(bitmap)
1601 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001602 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001603 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001604 // we know layers will only be drawn through drawDevice.
1605 // drawDevice has been made to work with content embedded in a
1606 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001607 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001608 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001609 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001610 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001611 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001612 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001613 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001614 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001615 GrContext::TextureKey key = bitmap.getGenerationID();
1616 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001617
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001618 entry = ctx->findAndLockTexture(key, bitmap.width(),
1619 bitmap.height(), sampler);
1620 if (NULL == entry.texture()) {
1621 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001622 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001623 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001624 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001625 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001626 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001627 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001628 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1629 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001630 }
1631 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001632 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001633}
1634
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001635void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1636 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001637}
1638
bsalomon@google.come97f0852011-06-17 13:10:25 +00001639SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1640 int width, int height,
1641 bool isOpaque,
1642 Usage usage) {
1643 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1644 width, height, usage));
1645}
1646