blob: 761d6cc42946257a7b7b5b663f88a675626c526a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrContext.h"
12#include "GrTextContext.h"
13
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "SkGpuDevice.h"
15#include "SkGrTexturePixelRef.h"
16
Scroggo97c88c22011-05-11 14:05:25 +000017#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018#include "SkDrawProcs.h"
19#include "SkGlyphCache.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000020#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
22#define CACHE_LAYER_TEXTURES 1
23
24#if 0
25 extern bool (*gShouldDrawProc)();
26 #define CHECK_SHOULD_DRAW(draw) \
27 do { \
28 if (gShouldDrawProc && !gShouldDrawProc()) return; \
29 this->prepareRenderTarget(draw); \
30 } while (0)
31#else
32 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw)
33#endif
34
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000035// we use the same texture slot on GrPaint for bitmaps and shaders
36// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
37enum {
38 kBitmapTextureIdx = 0,
39 kShaderTextureIdx = 0
40};
41
reed@google.comcde92112011-07-06 20:00:52 +000042
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000043#define MAX_BLUR_SIGMA 4.0f
44// FIXME: This value comes from from SkBlurMaskFilter.cpp.
45// Should probably be put in a common header someplace.
46#define MAX_BLUR_RADIUS SkIntToScalar(128)
47// This constant approximates the scaling done in the software path's
48// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
49// IMHO, it actually should be 1: we blur "less" than we should do
50// according to the CSS and canvas specs, simply because Safari does the same.
51// Firefox used to do the same too, until 4.0 where they fixed it. So at some
52// point we should probably get rid of these scaling constants and rebaseline
53// all the blur tests.
54#define BLUR_SIGMA_SCALE 0.6f
reed@google.comac10a2d2010-12-22 21:39:39 +000055///////////////////////////////////////////////////////////////////////////////
56
57SkGpuDevice::SkAutoCachedTexture::
58 SkAutoCachedTexture(SkGpuDevice* device,
59 const SkBitmap& bitmap,
60 const GrSamplerState& sampler,
61 GrTexture** texture) {
62 GrAssert(texture);
reed@google.comac10a2d2010-12-22 21:39:39 +000063 *texture = this->set(device, bitmap, sampler);
64}
65
66SkGpuDevice::SkAutoCachedTexture::SkAutoCachedTexture() {
reed@google.comac10a2d2010-12-22 21:39:39 +000067}
68
69GrTexture* SkGpuDevice::SkAutoCachedTexture::set(SkGpuDevice* device,
70 const SkBitmap& bitmap,
71 const GrSamplerState& sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000072 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000073 fDevice->unlockCachedTexture(fTex);
74 }
75 fDevice = device;
76 GrTexture* texture = (GrTexture*)bitmap.getTexture();
77 if (texture) {
78 // return the native texture
bsalomon@google.com50398bf2011-07-26 20:45:30 +000079 fTex.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000080 } else {
81 // look it up in our cache
bsalomon@google.com50398bf2011-07-26 20:45:30 +000082 fTex = device->lockCachedTexture(bitmap, sampler);
83 texture = fTex.texture();
reed@google.comac10a2d2010-12-22 21:39:39 +000084 }
85 return texture;
86}
87
88SkGpuDevice::SkAutoCachedTexture::~SkAutoCachedTexture() {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000089 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000090 fDevice->unlockCachedTexture(fTex);
91 }
92}
93
94///////////////////////////////////////////////////////////////////////////////
95
96bool gDoTraceDraw;
97
98struct GrSkDrawProcs : public SkDrawProcs {
99public:
100 GrContext* fContext;
101 GrTextContext* fTextContext;
102 GrFontScaler* fFontScaler; // cached in the skia glyphcache
103};
104
105///////////////////////////////////////////////////////////////////////////////
106
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000107GrRenderTarget* SkGpuDevice::Current3DApiRenderTarget() {
108 return (GrRenderTarget*) -1;
109}
110
reed@google.comaf951c92011-06-16 19:10:39 +0000111static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
112 switch (config) {
113 case kAlpha_8_GrPixelConfig:
114 *isOpaque = false;
115 return SkBitmap::kA8_Config;
116 case kRGB_565_GrPixelConfig:
117 *isOpaque = true;
118 return SkBitmap::kRGB_565_Config;
119 case kRGBA_4444_GrPixelConfig:
120 *isOpaque = false;
121 return SkBitmap::kARGB_4444_Config;
122 case kRGBA_8888_GrPixelConfig:
123 case kRGBX_8888_GrPixelConfig:
124 *isOpaque = (kRGBX_8888_GrPixelConfig == config);
125 return SkBitmap::kARGB_8888_Config;
126 default:
127 *isOpaque = false;
128 return SkBitmap::kNo_Config;
129 }
130}
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
reed@google.comaf951c92011-06-16 19:10:39 +0000132static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000133 SkAutoTUnref<GrRenderTarget> rtunref;
reed@google.comaf951c92011-06-16 19:10:39 +0000134 if (SkGpuDevice::Current3DApiRenderTarget() == renderTarget) {
135 renderTarget = context->createRenderTargetFrom3DApiState();
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000136 rtunref.reset(renderTarget);
reed@google.comaf951c92011-06-16 19:10:39 +0000137 }
138 GrTexture* texture = renderTarget->asTexture();
139 GrPixelConfig config = texture ? texture->config() : kRGBA_8888_GrPixelConfig;
140
141 bool isOpaque;
142 SkBitmap bitmap;
143 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
144 renderTarget->width(), renderTarget->height());
145 bitmap.setIsOpaque(isOpaque);
146 return bitmap;
147}
148
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000149SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
150: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
151 this->initFromRenderTarget(context, texture->asRenderTarget());
152}
153
reed@google.comaf951c92011-06-16 19:10:39 +0000154SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
155: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000156 this->initFromRenderTarget(context, renderTarget);
157}
158
159void SkGpuDevice::initFromRenderTarget(GrContext* context,
160 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000161 fNeedPrepareRenderTarget = false;
162 fDrawProcs = NULL;
163
164 fContext = context;
165 fContext->ref();
166
reed@google.comaf951c92011-06-16 19:10:39 +0000167 fTexture = NULL;
168 fRenderTarget = NULL;
169 fNeedClear = false;
170
171 if (Current3DApiRenderTarget() == renderTarget) {
172 fRenderTarget = fContext->createRenderTargetFrom3DApiState();
173 } else {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000174 GrAssert(NULL != renderTarget);
reed@google.comaf951c92011-06-16 19:10:39 +0000175 fRenderTarget = renderTarget;
176 fRenderTarget->ref();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000177 // if this RT is also a texture, hold a ref on it
178 fTexture = fRenderTarget->asTexture();
179 SkSafeRef(fTexture);
reed@google.comaf951c92011-06-16 19:10:39 +0000180 }
181
182 SkGrRenderTargetPixelRef* pr = new SkGrRenderTargetPixelRef(fRenderTarget);
183 this->setPixelRef(pr, 0)->unref();
184}
185
186SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000187 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000188: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000189 fNeedPrepareRenderTarget = false;
190 fDrawProcs = NULL;
191
reed@google.com7b201d22011-01-11 18:59:23 +0000192 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000193 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000194
reed@google.comac10a2d2010-12-22 21:39:39 +0000195 fTexture = NULL;
196 fRenderTarget = NULL;
197 fNeedClear = false;
198
reed@google.comaf951c92011-06-16 19:10:39 +0000199 if (config != SkBitmap::kRGB_565_Config) {
200 config = SkBitmap::kARGB_8888_Config;
201 }
202 SkBitmap bm;
203 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000204
205#if CACHE_LAYER_TEXTURES
bsalomon@google.come97f0852011-06-17 13:10:25 +0000206 TexType type = (kSaveLayer_Usage == usage) ?
207 kSaveLayerDeviceRenderTarget_TexType :
208 kDeviceRenderTarget_TexType;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000209 fCache = this->lockCachedTexture(bm, GrSamplerState::ClampNoFilter(), type);
210 fTexture = fCache.texture();
211 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000212 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000213 // hold a ref directly on fTexture (even though fCache has one) to match
214 // other constructor paths. Simplifies cleanup.
215 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000216 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000217#else
reed@google.comaf951c92011-06-16 19:10:39 +0000218 const GrTextureDesc desc = {
219 kRenderTarget_GrTextureFlagBit,
220 kNone_GrAALevel,
221 width,
222 height,
223 SkGr::Bitmap2PixelConfig(bm)
224 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000225
reed@google.comaf951c92011-06-16 19:10:39 +0000226 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000227#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000228 if (NULL != fTexture) {
229 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000230 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
reed@google.comaf951c92011-06-16 19:10:39 +0000232 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
reed@google.comaf951c92011-06-16 19:10:39 +0000234 // we defer the actual clear until our gainFocus()
235 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
reed@google.comaf951c92011-06-16 19:10:39 +0000237 // wrap the bitmap with a pixelref to expose our texture
238 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000239 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000240 } else {
241 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
242 width, height);
243 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000244 }
245}
246
247SkGpuDevice::~SkGpuDevice() {
248 if (fDrawProcs) {
249 delete fDrawProcs;
250 }
251
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000252 SkSafeUnref(fTexture);
253 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000254 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000255 GrAssert(NULL != fTexture);
256 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000257 fContext->unlockTexture(fCache);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000258 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000259 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000260}
261
reed@google.comac10a2d2010-12-22 21:39:39 +0000262///////////////////////////////////////////////////////////////////////////////
263
264void SkGpuDevice::makeRenderTargetCurrent() {
265 fContext->setRenderTarget(fRenderTarget);
266 fContext->flush(true);
267 fNeedPrepareRenderTarget = true;
268}
269
270///////////////////////////////////////////////////////////////////////////////
271
272bool SkGpuDevice::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
273 SkIRect bounds;
274 bounds.set(0, 0, this->width(), this->height());
275 if (!bounds.intersect(srcRect)) {
276 return false;
277 }
278
279 const int w = bounds.width();
280 const int h = bounds.height();
281 SkBitmap tmp;
282 // note we explicitly specify our rowBytes to be snug (no gap between rows)
283 tmp.setConfig(SkBitmap::kARGB_8888_Config, w, h, w * 4);
284 if (!tmp.allocPixels()) {
285 return false;
286 }
287
Scroggo813c33c2011-04-07 20:56:21 +0000288 tmp.lockPixels();
reed@google.comac10a2d2010-12-22 21:39:39 +0000289
Scroggoeb176032011-04-07 21:11:49 +0000290 bool read = fContext->readRenderTargetPixels(fRenderTarget,
291 bounds.fLeft, bounds.fTop,
292 bounds.width(), bounds.height(),
293 kRGBA_8888_GrPixelConfig,
294 tmp.getPixels());
Scroggo813c33c2011-04-07 20:56:21 +0000295 tmp.unlockPixels();
296 if (!read) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 return false;
298 }
299
300 tmp.swap(*bitmap);
301 return true;
302}
303
304void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y) {
305 SkAutoLockPixels alp(bitmap);
306 if (!bitmap.readyToDraw()) {
307 return;
308 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000309 GrPixelConfig config = SkGr::BitmapConfig2PixelConfig(bitmap.config(),
310 bitmap.isOpaque());
reed@google.comac10a2d2010-12-22 21:39:39 +0000311 fContext->setRenderTarget(fRenderTarget);
312 // we aren't setting the clip or matrix, so mark as dirty
313 // we don't need to set them for this call and don't have them anyway
314 fNeedPrepareRenderTarget = true;
315
316 fContext->writePixels(x, y, bitmap.width(), bitmap.height(),
317 config, bitmap.getPixels(), bitmap.rowBytes());
318}
319
320///////////////////////////////////////////////////////////////////////////////
321
322static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000323 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000324 const SkRegion& clipRegion,
325 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000326 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000327
328 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000329 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000330 const SkIRect& skBounds = clipRegion.getBounds();
331 GrRect bounds;
332 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
333 GrIntToScalar(skBounds.fTop),
334 GrIntToScalar(skBounds.fRight),
335 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000336 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
337 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000338 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000339}
340
341// call this ever each draw call, to ensure that the context reflects our state,
342// and not the state from some other canvas/device
343void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
344 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000345 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000346
347 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000348 SkASSERT(draw.fClipStack);
349 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000350 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000351 fNeedPrepareRenderTarget = false;
352 }
353}
354
reed@google.com46799cd2011-02-22 20:56:26 +0000355void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
356 const SkClipStack& clipStack) {
357 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000358 // We don't need to set them now because the context may not reflect this device.
359 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000360}
361
362void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000363 const SkRegion& clip, const SkClipStack& clipStack) {
364
reed@google.comac10a2d2010-12-22 21:39:39 +0000365 fContext->setRenderTarget(fRenderTarget);
366
bsalomon@google.comd302f142011-03-03 13:54:13 +0000367 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000368
reed@google.com6f8f2922011-03-04 22:27:10 +0000369 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000370
371 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000372 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000373 fNeedClear = false;
374 }
375}
376
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000377bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000379 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000380 return true;
381 }
382 return false;
383}
384
385///////////////////////////////////////////////////////////////////////////////
386
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000387SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
388SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
389SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
390SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
391SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
392 shader_type_mismatch);
393SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000394
bsalomon@google.com5782d712011-01-21 21:03:59 +0000395static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
396 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
397 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
398 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
399 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
400 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
401};
402
403bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
404 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000405 GrPaint* grPaint,
406 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000407
408 grPaint->fDither = skPaint.isDither();
409 grPaint->fAntiAlias = skPaint.isAntiAlias();
410
411 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
412 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
413
414 SkXfermode* mode = skPaint.getXfermode();
415 if (mode) {
416 if (!mode->asCoeff(&sm, &dm)) {
reed@google.com1a2e8d22011-01-21 22:08:29 +0000417 SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000418#if 0
419 return false;
420#endif
421 }
422 }
423 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
424 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
425
426 if (justAlpha) {
427 uint8_t alpha = skPaint.getAlpha();
428 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000429 // justAlpha is currently set to true only if there is a texture,
430 // so constantColor should not also be true.
431 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000432 } else {
433 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000434 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000435 }
Scroggo97c88c22011-05-11 14:05:25 +0000436 SkColorFilter* colorFilter = skPaint.getColorFilter();
437 SkColor color;
438 SkXfermode::Mode filterMode;
439 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000440 if (!constantColor) {
441 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
442 grPaint->fColorFilterXfermode = filterMode;
443 return true;
444 }
445 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
446 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000447 }
Scroggod757df22011-05-16 13:11:16 +0000448 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000449 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000450}
451
bsalomon@google.com5782d712011-01-21 21:03:59 +0000452bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
453 SkAutoCachedTexture* act,
454 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000455 GrPaint* grPaint,
456 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000457
bsalomon@google.com5782d712011-01-21 21:03:59 +0000458 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000459
bsalomon@google.com5782d712011-01-21 21:03:59 +0000460 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000461 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000462 return this->skPaint2GrPaintNoShader(skPaint,
463 false,
464 grPaint,
465 constantColor);
466 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000467 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000468 }
469
reed@google.comac10a2d2010-12-22 21:39:39 +0000470 SkBitmap bitmap;
471 SkMatrix matrix;
472 SkShader::TileMode tileModes[2];
473 SkScalar twoPointParams[3];
474 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
475 tileModes, twoPointParams);
476
bsalomon@google.com5782d712011-01-21 21:03:59 +0000477 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
478 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000479 SkShader::GradientInfo info;
480 SkColor color;
481
482 info.fColors = &color;
483 info.fColorOffsets = NULL;
484 info.fColorCount = 1;
485 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
486 SkPaint copy(skPaint);
487 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000488 // modulate the paint alpha by the shader's solid color alpha
489 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
490 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000491 return this->skPaint2GrPaintNoShader(copy,
492 false,
493 grPaint,
494 constantColor);
495 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000496 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000497 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000498 GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx);
499 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000500 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000501 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000502 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000503 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000504 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000505 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
506 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000507 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000508 sampler->setRadial2Params(twoPointParams[0],
509 twoPointParams[1],
510 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 }
512
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000513 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000514 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000515 SkDebugf("Couldn't convert bitmap to texture.\n");
516 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000517 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000518 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000519
520 // since our texture coords will be in local space, we wack the texture
521 // matrix to map them back into 0...1 before we load it
522 SkMatrix localM;
523 if (shader->getLocalMatrix(&localM)) {
524 SkMatrix inverse;
525 if (localM.invert(&inverse)) {
526 matrix.preConcat(inverse);
527 }
528 }
529 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000530 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
531 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000532 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000533 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000534 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000535 matrix.postScale(s, s);
536 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000537 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000538
539 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000540}
541
542///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000543
544class SkPositionSource {
545public:
546 SkPositionSource(const SkPoint* points, int count)
547 : fPoints(points), fCount(count) {}
548
549 int count() const { return fCount; }
550
551 void writeValue(int i, GrPoint* dstPosition) const {
552 SkASSERT(i < fCount);
bsalomon@google.com178d41e2011-08-16 18:48:48 +0000553 dstPosition->fX = fPoints[i].fX;
554 dstPosition->fY = fPoints[i].fY;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000555 }
556private:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000557 const SkPoint* fPoints;
bsalomon@google.com19628322011-02-03 21:30:17 +0000558 int fCount;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000559};
560
561class SkTexCoordSource {
562public:
563 SkTexCoordSource(const SkPoint* coords)
564 : fCoords(coords) {}
565
566 void writeValue(int i, GrPoint* dstCoord) const {
bsalomon@google.com178d41e2011-08-16 18:48:48 +0000567 dstCoord->fX = fCoords[i].fX;
568 dstCoord->fY = fCoords[i].fY;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000569 }
570private:
571 const SkPoint* fCoords;
572};
573
574class SkColorSource {
575public:
576 SkColorSource(const SkColor* colors) : fColors(colors) {}
577
578 void writeValue(int i, GrColor* dstColor) const {
579 *dstColor = SkGr::SkColor2GrColor(fColors[i]);
580 }
581private:
582 const SkColor* fColors;
583};
584
585class SkIndexSource {
586public:
587 SkIndexSource(const uint16_t* indices, int count)
588 : fIndices(indices), fCount(count) {
589 }
590
591 int count() const { return fCount; }
592
593 void writeValue(int i, uint16_t* dstIndex) const {
594 *dstIndex = fIndices[i];
595 }
596
597private:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000598 const uint16_t* fIndices;
bsalomon@google.com19628322011-02-03 21:30:17 +0000599 int fCount;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000600};
601
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000602
reed@google.comac10a2d2010-12-22 21:39:39 +0000603///////////////////////////////////////////////////////////////////////////////
604
bsalomon@google.com398109c2011-04-14 18:40:27 +0000605void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000606 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000607}
608
reed@google.comac10a2d2010-12-22 21:39:39 +0000609void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
610 CHECK_SHOULD_DRAW(draw);
611
bsalomon@google.com5782d712011-01-21 21:03:59 +0000612 GrPaint grPaint;
613 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000614 if (!this->skPaint2GrPaintShader(paint,
615 &act,
616 *draw.fMatrix,
617 &grPaint,
618 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000619 return;
620 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000621
622 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000623}
624
625// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000626static const GrPrimitiveType gPointMode2PrimtiveType[] = {
627 kPoints_PrimitiveType,
628 kLines_PrimitiveType,
629 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000630};
631
632void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000633 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000634 CHECK_SHOULD_DRAW(draw);
635
636 SkScalar width = paint.getStrokeWidth();
637 if (width < 0) {
638 return;
639 }
640
641 // we only handle hairlines here, else we let the SkDraw call our drawPath()
642 if (width > 0) {
643 draw.drawPoints(mode, count, pts, paint, true);
644 return;
645 }
646
bsalomon@google.com5782d712011-01-21 21:03:59 +0000647 GrPaint grPaint;
648 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000649 if (!this->skPaint2GrPaintShader(paint,
650 &act,
651 *draw.fMatrix,
652 &grPaint,
653 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000654 return;
655 }
656
bsalomon@google.com5782d712011-01-21 21:03:59 +0000657 fContext->drawVertices(grPaint,
658 gPointMode2PrimtiveType[mode],
659 count,
660 (GrPoint*)pts,
661 NULL,
662 NULL,
663 NULL,
664 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000665}
666
reed@google.comc9aa5872011-04-05 21:05:37 +0000667///////////////////////////////////////////////////////////////////////////////
668
reed@google.comac10a2d2010-12-22 21:39:39 +0000669void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
670 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000671 CHECK_SHOULD_DRAW(draw);
672
bungeman@google.com79bd8772011-07-18 15:34:08 +0000673 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000674 SkScalar width = paint.getStrokeWidth();
675
676 /*
677 We have special code for hairline strokes, miter-strokes, and fills.
678 Anything else we just call our path code.
679 */
680 bool usePath = doStroke && width > 0 &&
681 paint.getStrokeJoin() != SkPaint::kMiter_Join;
682 // another reason we might need to call drawPath...
683 if (paint.getMaskFilter()) {
684 usePath = true;
685 }
reed@google.com67db6642011-05-26 11:46:35 +0000686 // until we aa rotated rects...
687 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
688 usePath = true;
689 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000690 // small miter limit means right angles show bevel...
691 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
692 paint.getStrokeMiter() < SK_ScalarSqrt2)
693 {
694 usePath = true;
695 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000696 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000697 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
698 usePath = true;
699 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000700
701 if (usePath) {
702 SkPath path;
703 path.addRect(rect);
704 this->drawPath(draw, path, paint, NULL, true);
705 return;
706 }
707
708 GrPaint grPaint;
709 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000710 if (!this->skPaint2GrPaintShader(paint,
711 &act,
712 *draw.fMatrix,
713 &grPaint,
714 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000715 return;
716 }
reed@google.com20efde72011-05-09 17:00:02 +0000717 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000718}
719
reed@google.com69302852011-02-16 18:08:07 +0000720#include "SkMaskFilter.h"
721#include "SkBounder.h"
722
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000723static GrPathFill skToGrFillType(SkPath::FillType fillType) {
724 switch (fillType) {
725 case SkPath::kWinding_FillType:
726 return kWinding_PathFill;
727 case SkPath::kEvenOdd_FillType:
728 return kEvenOdd_PathFill;
729 case SkPath::kInverseWinding_FillType:
730 return kInverseWinding_PathFill;
731 case SkPath::kInverseEvenOdd_FillType:
732 return kInverseEvenOdd_PathFill;
733 default:
734 SkDebugf("Unsupported path fill type\n");
735 return kHairLine_PathFill;
736 }
737}
738
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000739static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000740 int halfWidth = (kernelWidth - 1) / 2;
741 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000742 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000743 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000744 float x = static_cast<float>(i - halfWidth);
745 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
746 // is dropped here, since we renormalize the kernel below.
747 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000748 sum += kernel[i];
749 }
750 // Normalize the kernel
751 float scale = 1.0f / sum;
752 for (int i = 0; i < kernelWidth; ++i)
753 kernel[i] *= scale;
754}
755
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000756static void scaleRect(SkRect* rect, float scale) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000757 rect->fLeft *= scale;
758 rect->fTop *= scale;
759 rect->fRight *= scale;
760 rect->fBottom *= scale;
761}
762
763static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
764 SkMaskFilter* filter, const SkMatrix& matrix,
765 const SkRegion& clip, SkBounder* bounder,
766 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000767#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000768 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000769#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000770 SkMaskFilter::BlurInfo info;
771 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000772 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000773 return false;
774 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000775 SkScalar radius = info.fIgnoreTransform ? info.fRadius
776 : matrix.mapRadius(info.fRadius);
777 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000778 if (radius <= 0) {
779 return false;
780 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000781 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000782 SkRect srcRect = path.getBounds();
783
784 int scaleFactor = 1;
785
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000786 while (sigma > MAX_BLUR_SIGMA) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000787 scaleFactor *= 2;
788 sigma *= 0.5f;
789 }
senorblanco@chromium.org4a947d22011-07-18 21:48:35 +0000790 int halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000791 int kernelWidth = halfWidth * 2 + 1;
792
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000793 float invScale = 1.0f / scaleFactor;
794 scaleRect(&srcRect, invScale);
795 srcRect.roundOut();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000796 srcRect.inset(-halfWidth, -halfWidth);
797
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000798 SkRect clipBounds;
799 clipBounds.set(clip.getBounds());
800 scaleRect(&clipBounds, invScale);
801 clipBounds.roundOut();
802 clipBounds.inset(-halfWidth, -halfWidth);
803
804 srcRect.intersect(clipBounds);
805
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000806 scaleRect(&srcRect, scaleFactor);
807 SkRect finalRect = srcRect;
808
809 SkIRect finalIRect;
810 finalRect.roundOut(&finalIRect);
811 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000812 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000813 }
814 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000815 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000816 }
817 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
818 srcRect.offset(-srcRect.fLeft, -srcRect.fTop);
819 const GrTextureDesc desc = {
820 kRenderTarget_GrTextureFlagBit,
821 kNone_GrAALevel,
822 srcRect.width(),
823 srcRect.height(),
824 // We actually only need A8, but it often isn't supported as a
825 // render target
826 kRGBA_8888_GrPixelConfig
827 };
828
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000829 GrAutoScratchTexture srcEntry(context, desc);
830 GrAutoScratchTexture dstEntry(context, desc);
831 if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000832 return false;
833 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000834 GrTexture* srcTexture = srcEntry.texture();
835 GrTexture* dstTexture = dstEntry.texture();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000836 if (NULL == srcTexture || NULL == dstTexture) {
837 return false;
838 }
839 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000840 // Once this code moves into GrContext, this should be changed to use
841 // an AutoClipRestore.
842 GrClip oldClip = context->getClip();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000843 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000844 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000845 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000846 GrPaint tempPaint;
847 tempPaint.reset();
848
849 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000850 tempPaint.fAntiAlias = grp->fAntiAlias;
851 if (tempPaint.fAntiAlias) {
852 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
853 // blend coeff of zero requires dual source blending support in order
854 // to properly blend partially covered pixels. This means the AA
855 // code path may not be taken. So we use a dst blend coeff of ISA. We
856 // could special case AA draws to a dst surface with known alpha=0 to
857 // use a zero dst coeff when dual source blending isn't available.
858 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
859 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
860 }
861 // Draw hard shadow to dstTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000862 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000863 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000864
865 GrMatrix sampleM;
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000866 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000867 GrPaint paint;
868 paint.reset();
869 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
870 paint.getTextureSampler(0)->setMatrix(sampleM);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000871 GrAutoScratchTexture origEntry;
872
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000873 if (blurType != SkMaskFilter::kNormal_BlurType) {
874 // Stash away a copy of the unblurred image.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000875 origEntry.set(context, desc);
876 if (NULL == origEntry.texture()) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000877 return false;
878 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000879 context->setRenderTarget(origEntry.texture()->asRenderTarget());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000880 paint.setTexture(0, srcTexture);
881 context->drawRect(paint, srcRect);
882 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000883 for (int i = 1; i < scaleFactor; i *= 2) {
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000884 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
885 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000886 context->setRenderTarget(dstTexture->asRenderTarget());
887 SkRect dstRect(srcRect);
888 scaleRect(&dstRect, 0.5f);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000889 paint.setTexture(0, srcTexture);
890 context->drawRectToRect(paint, dstRect, srcRect);
891 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000892 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000893 }
894
895 SkAutoTMalloc<float> kernelStorage(kernelWidth);
896 float* kernel = kernelStorage.get();
897 buildKernel(sigma, kernel, kernelWidth);
898
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000899 // Clear out a halfWidth to the right of the srcRect to prevent the
900 // X convolution from reading garbage.
901 SkIRect clearRect = SkIRect::MakeXYWH(
902 srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height());
903 context->clear(&clearRect, 0x0);
904
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000905 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000906 context->convolveInX(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000907 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000908
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000909 // Clear out a halfWidth below the srcRect to prevent the Y
910 // convolution from reading garbage.
911 clearRect = SkIRect::MakeXYWH(
912 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth);
913 context->clear(&clearRect, 0x0);
914
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000915 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000916 context->convolveInY(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000917 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000918
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000919 // Clear one pixel to the right and below, to accommodate bilinear
920 // upsampling.
921 clearRect = SkIRect::MakeXYWH(
922 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
923 context->clear(&clearRect, 0x0);
924 clearRect = SkIRect::MakeXYWH(
925 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
926 context->clear(&clearRect, 0x0);
927
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000928 if (scaleFactor > 1) {
929 // FIXME: This should be mitchell, not bilinear.
930 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000931 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000932 paint.getTextureSampler(0)->setMatrix(sampleM);
933 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000934 paint.setTexture(0, srcTexture);
935 SkRect dstRect(srcRect);
936 scaleRect(&dstRect, scaleFactor);
937 context->drawRectToRect(paint, dstRect, srcRect);
938 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000939 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000940 }
941
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000942 if (blurType != SkMaskFilter::kNormal_BlurType) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000943 GrTexture* origTexture = origEntry.texture();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000944 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000945 sampleM.setIDiv(origTexture->width(), origTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000946 paint.getTextureSampler(0)->setMatrix(sampleM);
947 // Blend origTexture over srcTexture.
948 context->setRenderTarget(srcTexture->asRenderTarget());
949 paint.setTexture(0, origTexture);
950 if (SkMaskFilter::kInner_BlurType == blurType) {
951 // inner: dst = dst * src
952 paint.fSrcBlendCoeff = kDC_BlendCoeff;
953 paint.fDstBlendCoeff = kZero_BlendCoeff;
954 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
955 // solid: dst = src + dst - src * dst
956 // = (1 - dst) * src + 1 * dst
957 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
958 paint.fDstBlendCoeff = kOne_BlendCoeff;
959 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
960 // outer: dst = dst * (1 - src)
961 // = 0 * src + (1 - src) * dst
962 paint.fSrcBlendCoeff = kZero_BlendCoeff;
963 paint.fDstBlendCoeff = kISC_BlendCoeff;
964 }
965 context->drawRect(paint, srcRect);
966 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000967 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000968 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000969
970 if (grp->hasTextureOrMask()) {
971 GrMatrix inverse;
972 if (!matrix.invert(&inverse)) {
973 return false;
974 }
975 grp->preConcatActiveSamplerMatrices(inverse);
976 }
977
978 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
979 // we assume the last mask index is available for use
980 GrAssert(NULL == grp->getMask(MASK_IDX));
981 grp->setMask(MASK_IDX, srcTexture);
982 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
983
984 GrMatrix m;
985 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
986 m.postIDiv(srcTexture->width(), srcTexture->height());
987 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
988 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000989 return true;
990}
991
reed@google.com69302852011-02-16 18:08:07 +0000992static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
993 SkMaskFilter* filter, const SkMatrix& matrix,
994 const SkRegion& clip, SkBounder* bounder,
995 GrPaint* grp) {
996 SkMask srcM, dstM;
997
998 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
999 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
1000 return false;
1001 }
1002
1003 SkAutoMaskImage autoSrc(&srcM, false);
1004
1005 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
1006 return false;
1007 }
1008 // this will free-up dstM when we're done (allocated in filterMask())
1009 SkAutoMaskImage autoDst(&dstM, false);
1010
1011 if (clip.quickReject(dstM.fBounds)) {
1012 return false;
1013 }
1014 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1015 return false;
1016 }
1017
1018 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1019 // the current clip (and identity matrix) and grpaint settings
1020
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001021 // used to compute inverse view, if necessary
1022 GrMatrix ivm = context->getMatrix();
1023
reed@google.com0c219b62011-02-16 21:31:18 +00001024 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001025
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001026 const GrTextureDesc desc = {
1027 kNone_GrTextureFlags,
1028 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +00001029 dstM.fBounds.width(),
1030 dstM.fBounds.height(),
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001031 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +00001032 };
1033
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001034 GrAutoScratchTexture ast(context, desc);
1035 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001036
reed@google.com69302852011-02-16 18:08:07 +00001037 if (NULL == texture) {
1038 return false;
1039 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001040 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
1041 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001042
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001043 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
1044 grp->preConcatActiveSamplerMatrices(ivm);
1045 }
1046
1047 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1048 // we assume the last mask index is available for use
1049 GrAssert(NULL == grp->getMask(MASK_IDX));
1050 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001051 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +00001052
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001053 GrRect d;
1054 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001055 GrIntToScalar(dstM.fBounds.fTop),
1056 GrIntToScalar(dstM.fBounds.fRight),
1057 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001058
1059 GrMatrix m;
1060 m.setTranslate(-dstM.fBounds.fLeft, -dstM.fBounds.fTop);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001061 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001062 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1063
1064 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001065 return true;
1066}
reed@google.com69302852011-02-16 18:08:07 +00001067
reed@google.com0c219b62011-02-16 21:31:18 +00001068void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comac10a2d2010-12-22 21:39:39 +00001069 const SkPaint& paint, const SkMatrix* prePathMatrix,
1070 bool pathIsMutable) {
1071 CHECK_SHOULD_DRAW(draw);
1072
bsalomon@google.com5782d712011-01-21 21:03:59 +00001073 GrPaint grPaint;
1074 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001075 if (!this->skPaint2GrPaintShader(paint,
1076 &act,
1077 *draw.fMatrix,
1078 &grPaint,
1079 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001080 return;
1081 }
1082
reed@google.com0c219b62011-02-16 21:31:18 +00001083 // BEGIN lift from SkDraw::drawPath()
1084
1085 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1086 bool doFill = true;
1087 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001088
1089 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001090 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001091
reed@google.come3445642011-02-16 23:20:39 +00001092 if (!pathIsMutable) {
1093 result = &tmpPath;
1094 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001095 }
reed@google.come3445642011-02-16 23:20:39 +00001096 // should I push prePathMatrix on our MV stack temporarily, instead
1097 // of applying it here? See SkDraw.cpp
1098 pathPtr->transform(*prePathMatrix, result);
1099 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 }
reed@google.com0c219b62011-02-16 21:31:18 +00001101 // at this point we're done with prePathMatrix
1102 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001103
bsalomon@google.com04de7822011-03-25 18:04:43 +00001104 // This "if" is not part of the SkDraw::drawPath() lift.
1105 // When we get a 1.0 wide stroke we hairline stroke it instead of creating
1106 // a new stroked-path. This is motivated by canvas2D sites that draw
1107 // lines as 1.0 wide stroked paths. We can consider doing an alpha-modulated-
1108 // hairline for width < 1.0 when AA is enabled.
1109 static const int gMatrixMask = ~(SkMatrix::kIdentity_Mask |
1110 SkMatrix::kTranslate_Mask);
1111 if (!paint.getPathEffect() &&
1112 SkPaint::kStroke_Style == paint.getStyle() &&
1113 !(draw.fMatrix->getType() & gMatrixMask) &&
1114 SK_Scalar1 == paint.getStrokeWidth()) {
1115 doFill = false;
1116 }
1117
1118 if (doFill && (paint.getPathEffect() ||
1119 paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.com0c219b62011-02-16 21:31:18 +00001120 doFill = paint.getFillPath(*pathPtr, &tmpPath);
1121 pathPtr = &tmpPath;
1122 }
1123
1124 // END lift from SkDraw::drawPath()
1125
reed@google.com69302852011-02-16 18:08:07 +00001126 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001127 // avoid possibly allocating a new path in transform if we can
1128 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1129
1130 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001131 pathPtr->transform(*draw.fMatrix, devPathPtr);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001132 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1133 *draw.fMatrix, *draw.fClip, draw.fBounder,
1134 &grPaint)) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001135 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1136 *draw.fMatrix, *draw.fClip, draw.fBounder,
1137 &grPaint);
1138 }
reed@google.com69302852011-02-16 18:08:07 +00001139 return;
1140 }
reed@google.com69302852011-02-16 18:08:07 +00001141
bsalomon@google.comffca4002011-02-22 20:34:01 +00001142 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001143
reed@google.com0c219b62011-02-16 21:31:18 +00001144 if (doFill) {
1145 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001146 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001147 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001148 break;
1149 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001150 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001151 break;
1152 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001153 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001154 break;
1155 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001156 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001157 break;
1158 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001159 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001160 return;
1161 }
1162 }
1163
reed@google.com07f3ee12011-05-16 17:21:57 +00001164 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001165}
1166
reed@google.comac10a2d2010-12-22 21:39:39 +00001167void SkGpuDevice::drawBitmap(const SkDraw& draw,
1168 const SkBitmap& bitmap,
1169 const SkIRect* srcRectPtr,
1170 const SkMatrix& m,
1171 const SkPaint& paint) {
1172 CHECK_SHOULD_DRAW(draw);
1173
1174 SkIRect srcRect;
1175 if (NULL == srcRectPtr) {
1176 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1177 } else {
1178 srcRect = *srcRectPtr;
1179 }
1180
junov@google.comd935cfb2011-06-27 20:48:23 +00001181 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001182 // Convert the bitmap to a shader so that the rect can be drawn
1183 // through drawRect, which supports mask filters.
1184 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001185 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001186 if (srcRectPtr) {
1187 if (!bitmap.extractSubset(&tmp, srcRect)) {
1188 return; // extraction failed
1189 }
1190 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001191 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001192 }
1193 SkPaint paintWithTexture(paint);
1194 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1195 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001196 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001197 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001198
junov@google.com1d329782011-07-28 20:10:09 +00001199 // Transform 'm' needs to be concatenated to the draw matrix,
1200 // rather than transforming the primitive directly, so that 'm' will
1201 // also affect the behavior of the mask filter.
1202 SkMatrix drawMatrix;
1203 drawMatrix.setConcat(*draw.fMatrix, m);
1204 SkDraw transformedDraw(draw);
1205 transformedDraw.fMatrix = &drawMatrix;
1206
1207 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1208
junov@google.comd935cfb2011-06-27 20:48:23 +00001209 return;
1210 }
1211
bsalomon@google.com5782d712011-01-21 21:03:59 +00001212 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001213 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001214 return;
1215 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001216 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001217 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001218 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001219 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001220 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001221 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001222
bsalomon@google.com91958362011-06-13 17:58:13 +00001223 const int maxTextureSize = fContext->getMaxTextureSize();
1224 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1225 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001226 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001227 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001228 return;
1229 }
1230
1231 // undo the translate done by SkCanvas
1232 int DX = SkMax32(0, srcRect.fLeft);
1233 int DY = SkMax32(0, srcRect.fTop);
1234 // compute clip bounds in local coordinates
1235 SkIRect clipRect;
1236 {
1237 SkRect r;
1238 r.set(draw.fClip->getBounds());
1239 SkMatrix matrix, inverse;
1240 matrix.setConcat(*draw.fMatrix, m);
1241 if (!matrix.invert(&inverse)) {
1242 return;
1243 }
1244 inverse.mapRect(&r);
1245 r.roundOut(&clipRect);
1246 // apply the canvas' translate to our local clip
1247 clipRect.offset(DX, DY);
1248 }
1249
bsalomon@google.com91958362011-06-13 17:58:13 +00001250 int nx = bitmap.width() / maxTextureSize;
1251 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001252 for (int x = 0; x <= nx; x++) {
1253 for (int y = 0; y <= ny; y++) {
1254 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001255 tileR.set(x * maxTextureSize, y * maxTextureSize,
1256 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001257 if (!SkIRect::Intersects(tileR, clipRect)) {
1258 continue;
1259 }
1260
1261 SkIRect srcR = tileR;
1262 if (!srcR.intersect(srcRect)) {
1263 continue;
1264 }
1265
1266 SkBitmap tmpB;
1267 if (bitmap.extractSubset(&tmpB, tileR)) {
1268 // now offset it to make it "local" to our tmp bitmap
1269 srcR.offset(-tileR.fLeft, -tileR.fTop);
1270
1271 SkMatrix tmpM(m);
1272 {
1273 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1274 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1275 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1276 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001277 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 }
1279 }
1280 }
1281}
1282
1283/*
1284 * This is called by drawBitmap(), which has to handle images that may be too
1285 * large to be represented by a single texture.
1286 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001287 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1288 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001289 */
1290void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1291 const SkBitmap& bitmap,
1292 const SkIRect& srcRect,
1293 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001294 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001295 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1296 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001297
reed@google.com9c49bc32011-07-07 13:42:37 +00001298 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001299 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001300 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001301 return;
1302 }
1303
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001304 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1305
1306 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1307 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1308 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1309 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001310
1311 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001312 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001313 if (NULL == texture) {
1314 return;
1315 }
1316
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001317 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001318
reed@google.com20efde72011-05-09 17:00:02 +00001319 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1320 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001321 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001322 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1323 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1324 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001325 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001326
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001327 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001328 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001329 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001330 // If drawing a subrect of the bitmap and filtering is enabled,
1331 // use a constrained texture domain to avoid color bleeding
1332 GrScalar left, top, right, bottom;
1333 if (srcRect.width() > 1) {
1334 GrScalar border = GR_ScalarHalf / bitmap.width();
1335 left = paintRect.left() + border;
1336 right = paintRect.right() - border;
1337 } else {
1338 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1339 }
1340 if (srcRect.height() > 1) {
1341 GrScalar border = GR_ScalarHalf / bitmap.height();
1342 top = paintRect.top() + border;
1343 bottom = paintRect.bottom() - border;
1344 } else {
1345 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1346 }
1347 GrRect textureDomain;
1348 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001349 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001350 }
1351
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001352 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001353}
1354
1355void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1356 int left, int top, const SkPaint& paint) {
1357 CHECK_SHOULD_DRAW(draw);
1358
1359 SkAutoLockPixels alp(bitmap);
1360 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1361 return;
1362 }
1363
bsalomon@google.com5782d712011-01-21 21:03:59 +00001364 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001365 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001366 return;
1367 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001368
bsalomon@google.com5782d712011-01-21 21:03:59 +00001369 GrAutoMatrix avm(fContext, GrMatrix::I());
1370
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001371 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001372
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001373 GrTexture* texture;
1374 sampler->setClampNoFilter();
1375 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1376
1377 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001378
bsalomon@google.com5782d712011-01-21 21:03:59 +00001379 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001380 GrRect::MakeXYWH(GrIntToScalar(left),
1381 GrIntToScalar(top),
1382 GrIntToScalar(bitmap.width()),
1383 GrIntToScalar(bitmap.height())),
1384 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001385}
1386
1387void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1388 int x, int y, const SkPaint& paint) {
1389 CHECK_SHOULD_DRAW(draw);
1390
bsalomon@google.com5782d712011-01-21 21:03:59 +00001391 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001392 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001393 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001394 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001395 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001396
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001397 GrTexture* devTex = grPaint.getTexture(0);
1398 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001399
1400 const SkBitmap& bm = dev->accessBitmap(false);
1401 int w = bm.width();
1402 int h = bm.height();
1403
1404 GrAutoMatrix avm(fContext, GrMatrix::I());
1405
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001406 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001407
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001408 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1409 GrIntToScalar(y),
1410 GrIntToScalar(w),
1411 GrIntToScalar(h));
1412 // The device being drawn may not fill up its texture (saveLayer uses
1413 // the approximate ).
1414 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1415 GR_Scalar1 * h / devTex->height());
1416
1417 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001418}
1419
1420///////////////////////////////////////////////////////////////////////////////
1421
1422// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001423static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1424 kTriangles_PrimitiveType,
1425 kTriangleStrip_PrimitiveType,
1426 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001427};
1428
1429void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1430 int vertexCount, const SkPoint vertices[],
1431 const SkPoint texs[], const SkColor colors[],
1432 SkXfermode* xmode,
1433 const uint16_t indices[], int indexCount,
1434 const SkPaint& paint) {
1435 CHECK_SHOULD_DRAW(draw);
1436
bsalomon@google.com5782d712011-01-21 21:03:59 +00001437 GrPaint grPaint;
1438 SkAutoCachedTexture act;
1439 // we ignore the shader if texs is null.
1440 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001441 if (!this->skPaint2GrPaintNoShader(paint,
1442 false,
1443 &grPaint,
1444 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001445 return;
1446 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001447 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001448 if (!this->skPaint2GrPaintShader(paint, &act,
1449 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001450 &grPaint,
1451 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001452 return;
1453 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001454 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001455
1456 if (NULL != xmode && NULL != texs && NULL != colors) {
1457 SkXfermode::Mode mode;
1458 if (!SkXfermode::IsMode(xmode, &mode) ||
1459 SkXfermode::kMultiply_Mode != mode) {
1460 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1461#if 0
1462 return
1463#endif
1464 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001465 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001466
bsalomon@google.com5782d712011-01-21 21:03:59 +00001467 // even if GrColor and SkColor byte offsets match we need
1468 // to perform pre-multiply.
1469 if (NULL == colors) {
1470 fContext->drawVertices(grPaint,
1471 gVertexMode2PrimitiveType[vmode],
1472 vertexCount,
1473 (GrPoint*) vertices,
1474 (GrPoint*) texs,
1475 NULL,
1476 indices,
1477 indexCount);
bsalomon@google.com178d41e2011-08-16 18:48:48 +00001478 } else {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001479 SkTexCoordSource texSrc(texs);
1480 SkColorSource colSrc(colors);
1481 SkIndexSource idxSrc(indices, indexCount);
1482
1483 fContext->drawCustomVertices(grPaint,
1484 gVertexMode2PrimitiveType[vmode],
1485 SkPositionSource(vertices, vertexCount),
1486 (NULL == texs) ? NULL : &texSrc,
1487 (NULL == colors) ? NULL : &colSrc,
1488 (NULL == indices) ? NULL : &idxSrc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001489 }
1490}
1491
1492///////////////////////////////////////////////////////////////////////////////
1493
1494static void GlyphCacheAuxProc(void* data) {
1495 delete (GrFontScaler*)data;
1496}
1497
1498static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1499 void* auxData;
1500 GrFontScaler* scaler = NULL;
1501 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1502 scaler = (GrFontScaler*)auxData;
1503 }
1504 if (NULL == scaler) {
1505 scaler = new SkGrFontScaler(cache);
1506 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1507 }
1508 return scaler;
1509}
1510
1511static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1512 SkFixed fx, SkFixed fy,
1513 const SkGlyph& glyph) {
1514 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1515
1516 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1517
1518 if (NULL == procs->fFontScaler) {
1519 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1520 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001521
1522 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001523 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001524 *
reed@google.com3b139f52011-06-07 17:56:25 +00001525 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1526 * It calls that rather than round, because our caller has already added
1527 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1528 *
1529 * Test code between raster and gpu (they should draw the same)
1530 *
1531 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1532 *
1533 * Perhaps we should only perform this integralization if there is no
1534 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001535 */
reed@google.com3b139f52011-06-07 17:56:25 +00001536 fy = SkFixedFloorToFixed(fy);
1537
reed@google.comac10a2d2010-12-22 21:39:39 +00001538 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001539 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001540 procs->fFontScaler);
1541}
1542
bsalomon@google.com5782d712011-01-21 21:03:59 +00001543SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001544
1545 // deferred allocation
1546 if (NULL == fDrawProcs) {
1547 fDrawProcs = new GrSkDrawProcs;
1548 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1549 fDrawProcs->fContext = fContext;
1550 }
1551
1552 // init our (and GL's) state
1553 fDrawProcs->fTextContext = context;
1554 fDrawProcs->fFontScaler = NULL;
1555 return fDrawProcs;
1556}
1557
1558void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1559 size_t byteLength, SkScalar x, SkScalar y,
1560 const SkPaint& paint) {
1561 CHECK_SHOULD_DRAW(draw);
1562
1563 if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) {
1564 // this guy will just call our drawPath()
1565 draw.drawText((const char*)text, byteLength, x, y, paint);
1566 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001567 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001568
1569 GrPaint grPaint;
1570 SkAutoCachedTexture act;
1571
Scroggod757df22011-05-16 13:11:16 +00001572 if (!this->skPaint2GrPaintShader(paint,
1573 &act,
1574 *draw.fMatrix,
1575 &grPaint,
1576 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001577 return;
1578 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001579 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001580 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001581 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1582 }
1583}
1584
1585void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1586 size_t byteLength, const SkScalar pos[],
1587 SkScalar constY, int scalarsPerPos,
1588 const SkPaint& paint) {
1589 CHECK_SHOULD_DRAW(draw);
1590
1591 if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) {
1592 // this guy will just call our drawPath()
1593 draw.drawPosText((const char*)text, byteLength, pos, constY,
1594 scalarsPerPos, paint);
1595 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001596 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001597
1598 GrPaint grPaint;
1599 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001600 if (!this->skPaint2GrPaintShader(paint,
1601 &act,
1602 *draw.fMatrix,
1603 &grPaint,
1604 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001605 return;
1606 }
1607
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001608 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001609 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001610 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1611 scalarsPerPos, paint);
1612 }
1613}
1614
1615void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1616 size_t len, const SkPath& path,
1617 const SkMatrix* m, const SkPaint& paint) {
1618 CHECK_SHOULD_DRAW(draw);
1619
1620 SkASSERT(draw.fDevice == this);
1621 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1622}
1623
1624///////////////////////////////////////////////////////////////////////////////
1625
reed@google.comf67e4cf2011-03-15 20:56:58 +00001626bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1627 if (!paint.isLCDRenderText()) {
1628 // we're cool with the paint as is
1629 return false;
1630 }
1631
1632 if (paint.getShader() ||
1633 paint.getXfermode() || // unless its srcover
1634 paint.getMaskFilter() ||
1635 paint.getRasterizer() ||
1636 paint.getColorFilter() ||
1637 paint.getPathEffect() ||
1638 paint.isFakeBoldText() ||
1639 paint.getStyle() != SkPaint::kFill_Style) {
1640 // turn off lcd
1641 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1642 flags->fHinting = paint.getHinting();
1643 return true;
1644 }
1645 // we're cool with the paint as is
1646 return false;
1647}
1648
1649///////////////////////////////////////////////////////////////////////////////
1650
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001651SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1652 const GrSamplerState& sampler,
1653 TexType type) {
1654 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001655 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001656
bsalomon@google.come97f0852011-06-17 13:10:25 +00001657 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001658 const GrTextureDesc desc = {
1659 kRenderTarget_GrTextureFlagBit,
1660 kNone_GrAALevel,
1661 bitmap.width(),
1662 bitmap.height(),
1663 SkGr::Bitmap2PixelConfig(bitmap)
1664 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001665 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001666 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001667 // we know layers will only be drawn through drawDevice.
1668 // drawDevice has been made to work with content embedded in a
1669 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001670 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001671 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001672 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001673 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001674 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001675 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001676 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001677 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001678 GrContext::TextureKey key = bitmap.getGenerationID();
1679 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001680
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001681 entry = ctx->findAndLockTexture(key, bitmap.width(),
1682 bitmap.height(), sampler);
1683 if (NULL == entry.texture()) {
1684 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001685 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001686 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001687 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001688 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001689 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001690 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001691 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1692 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001693 }
1694 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001695 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001696}
1697
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001698void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1699 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001700}
1701
bsalomon@google.come97f0852011-06-17 13:10:25 +00001702SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1703 int width, int height,
1704 bool isOpaque,
1705 Usage usage) {
1706 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1707 width, height, usage));
1708}
1709