blob: 64d74ee8ab007a2e6a72e3173722b4adf2d65d8f [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"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000020#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000021#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000022#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
24#define CACHE_LAYER_TEXTURES 1
25
26#if 0
27 extern bool (*gShouldDrawProc)();
28 #define CHECK_SHOULD_DRAW(draw) \
29 do { \
30 if (gShouldDrawProc && !gShouldDrawProc()) return; \
31 this->prepareRenderTarget(draw); \
32 } while (0)
33#else
34 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw)
35#endif
36
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000037// we use the same texture slot on GrPaint for bitmaps and shaders
38// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
39enum {
40 kBitmapTextureIdx = 0,
41 kShaderTextureIdx = 0
42};
43
reed@google.comcde92112011-07-06 20:00:52 +000044
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000045#define MAX_BLUR_SIGMA 4.0f
46// FIXME: This value comes from from SkBlurMaskFilter.cpp.
47// Should probably be put in a common header someplace.
48#define MAX_BLUR_RADIUS SkIntToScalar(128)
49// This constant approximates the scaling done in the software path's
50// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
51// IMHO, it actually should be 1: we blur "less" than we should do
52// according to the CSS and canvas specs, simply because Safari does the same.
53// Firefox used to do the same too, until 4.0 where they fixed it. So at some
54// point we should probably get rid of these scaling constants and rebaseline
55// all the blur tests.
56#define BLUR_SIGMA_SCALE 0.6f
reed@google.comac10a2d2010-12-22 21:39:39 +000057///////////////////////////////////////////////////////////////////////////////
58
59SkGpuDevice::SkAutoCachedTexture::
60 SkAutoCachedTexture(SkGpuDevice* device,
61 const SkBitmap& bitmap,
62 const GrSamplerState& sampler,
63 GrTexture** texture) {
64 GrAssert(texture);
reed@google.comac10a2d2010-12-22 21:39:39 +000065 *texture = this->set(device, bitmap, sampler);
66}
67
68SkGpuDevice::SkAutoCachedTexture::SkAutoCachedTexture() {
reed@google.comac10a2d2010-12-22 21:39:39 +000069}
70
71GrTexture* SkGpuDevice::SkAutoCachedTexture::set(SkGpuDevice* device,
72 const SkBitmap& bitmap,
73 const GrSamplerState& sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000074 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000075 fDevice->unlockCachedTexture(fTex);
76 }
77 fDevice = device;
78 GrTexture* texture = (GrTexture*)bitmap.getTexture();
79 if (texture) {
80 // return the native texture
bsalomon@google.com50398bf2011-07-26 20:45:30 +000081 fTex.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000082 } else {
83 // look it up in our cache
bsalomon@google.com50398bf2011-07-26 20:45:30 +000084 fTex = device->lockCachedTexture(bitmap, sampler);
85 texture = fTex.texture();
reed@google.comac10a2d2010-12-22 21:39:39 +000086 }
87 return texture;
88}
89
90SkGpuDevice::SkAutoCachedTexture::~SkAutoCachedTexture() {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000091 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000092 fDevice->unlockCachedTexture(fTex);
93 }
94}
95
96///////////////////////////////////////////////////////////////////////////////
97
98bool gDoTraceDraw;
99
100struct GrSkDrawProcs : public SkDrawProcs {
101public:
102 GrContext* fContext;
103 GrTextContext* fTextContext;
104 GrFontScaler* fFontScaler; // cached in the skia glyphcache
105};
106
107///////////////////////////////////////////////////////////////////////////////
108
reed@google.comaf951c92011-06-16 19:10:39 +0000109static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
110 switch (config) {
111 case kAlpha_8_GrPixelConfig:
112 *isOpaque = false;
113 return SkBitmap::kA8_Config;
114 case kRGB_565_GrPixelConfig:
115 *isOpaque = true;
116 return SkBitmap::kRGB_565_Config;
117 case kRGBA_4444_GrPixelConfig:
118 *isOpaque = false;
119 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000120 case kSkia8888_PM_GrPixelConfig:
121 // we don't currently have a way of knowing whether
122 // a 8888 is opaque based on the config.
123 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000124 return SkBitmap::kARGB_8888_Config;
125 default:
126 *isOpaque = false;
127 return SkBitmap::kNo_Config;
128 }
129}
reed@google.comac10a2d2010-12-22 21:39:39 +0000130
reed@google.comaf951c92011-06-16 19:10:39 +0000131static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000132 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000133
134 bool isOpaque;
135 SkBitmap bitmap;
136 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
137 renderTarget->width(), renderTarget->height());
138 bitmap.setIsOpaque(isOpaque);
139 return bitmap;
140}
141
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000142SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
143: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
144 this->initFromRenderTarget(context, texture->asRenderTarget());
145}
146
reed@google.comaf951c92011-06-16 19:10:39 +0000147SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
148: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000149 this->initFromRenderTarget(context, renderTarget);
150}
151
152void SkGpuDevice::initFromRenderTarget(GrContext* context,
153 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000154 fNeedPrepareRenderTarget = false;
155 fDrawProcs = NULL;
156
157 fContext = context;
158 fContext->ref();
159
reed@google.comaf951c92011-06-16 19:10:39 +0000160 fTexture = NULL;
161 fRenderTarget = NULL;
162 fNeedClear = false;
163
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000164 GrAssert(NULL != renderTarget);
165 fRenderTarget = renderTarget;
166 fRenderTarget->ref();
167 // if this RT is also a texture, hold a ref on it
168 fTexture = fRenderTarget->asTexture();
169 SkSafeRef(fTexture);
reed@google.comaf951c92011-06-16 19:10:39 +0000170
171 SkGrRenderTargetPixelRef* pr = new SkGrRenderTargetPixelRef(fRenderTarget);
172 this->setPixelRef(pr, 0)->unref();
173}
174
175SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000176 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000177: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 fNeedPrepareRenderTarget = false;
179 fDrawProcs = NULL;
180
reed@google.com7b201d22011-01-11 18:59:23 +0000181 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000182 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000183
reed@google.comac10a2d2010-12-22 21:39:39 +0000184 fTexture = NULL;
185 fRenderTarget = NULL;
186 fNeedClear = false;
187
reed@google.comaf951c92011-06-16 19:10:39 +0000188 if (config != SkBitmap::kRGB_565_Config) {
189 config = SkBitmap::kARGB_8888_Config;
190 }
191 SkBitmap bm;
192 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000193
194#if CACHE_LAYER_TEXTURES
bsalomon@google.come97f0852011-06-17 13:10:25 +0000195 TexType type = (kSaveLayer_Usage == usage) ?
196 kSaveLayerDeviceRenderTarget_TexType :
197 kDeviceRenderTarget_TexType;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000198 fCache = this->lockCachedTexture(bm, GrSamplerState::ClampNoFilter(), type);
199 fTexture = fCache.texture();
200 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000201 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000202 // hold a ref directly on fTexture (even though fCache has one) to match
203 // other constructor paths. Simplifies cleanup.
204 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000205 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000206#else
reed@google.comaf951c92011-06-16 19:10:39 +0000207 const GrTextureDesc desc = {
208 kRenderTarget_GrTextureFlagBit,
209 kNone_GrAALevel,
210 width,
211 height,
212 SkGr::Bitmap2PixelConfig(bm)
213 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000214
reed@google.comaf951c92011-06-16 19:10:39 +0000215 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000216#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000217 if (NULL != fTexture) {
218 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000219 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000220
reed@google.comaf951c92011-06-16 19:10:39 +0000221 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000222
reed@google.comaf951c92011-06-16 19:10:39 +0000223 // we defer the actual clear until our gainFocus()
224 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000225
reed@google.comaf951c92011-06-16 19:10:39 +0000226 // wrap the bitmap with a pixelref to expose our texture
227 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000228 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000229 } else {
230 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
231 width, height);
232 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233 }
234}
235
236SkGpuDevice::~SkGpuDevice() {
237 if (fDrawProcs) {
238 delete fDrawProcs;
239 }
240
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000241 SkSafeUnref(fTexture);
242 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000243 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000244 GrAssert(NULL != fTexture);
245 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000246 fContext->unlockTexture(fCache);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000247 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000248 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000249}
250
reed@google.comac10a2d2010-12-22 21:39:39 +0000251///////////////////////////////////////////////////////////////////////////////
252
253void SkGpuDevice::makeRenderTargetCurrent() {
254 fContext->setRenderTarget(fRenderTarget);
255 fContext->flush(true);
256 fNeedPrepareRenderTarget = true;
257}
258
259///////////////////////////////////////////////////////////////////////////////
260
bsalomon@google.comc4364992011-11-07 15:54:49 +0000261namespace {
262GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
263 switch (config8888) {
264 case SkCanvas::kNative_Premul_Config8888:
265 return kSkia8888_PM_GrPixelConfig;
266 case SkCanvas::kNative_Unpremul_Config8888:
267 return kSkia8888_UPM_GrPixelConfig;
268 case SkCanvas::kBGRA_Premul_Config8888:
269 return kBGRA_8888_PM_GrPixelConfig;
270 case SkCanvas::kBGRA_Unpremul_Config8888:
271 return kBGRA_8888_UPM_GrPixelConfig;
272 case SkCanvas::kRGBA_Premul_Config8888:
273 return kRGBA_8888_PM_GrPixelConfig;
274 case SkCanvas::kRGBA_Unpremul_Config8888:
275 return kRGBA_8888_UPM_GrPixelConfig;
276 default:
277 GrCrash("Unexpected Config8888.");
278 return kSkia8888_PM_GrPixelConfig;
279 }
280}
281}
282
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000283bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
284 int x, int y,
285 SkCanvas::Config8888 config8888) {
bsalomon@google.com910267d2011-11-02 20:06:25 +0000286 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
287 SkASSERT(!bitmap.isNull());
288 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000289
bsalomon@google.com910267d2011-11-02 20:06:25 +0000290 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 GrPixelConfig config;
292 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000293 return fContext->readRenderTargetPixels(fRenderTarget,
294 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000295 bitmap.width(),
296 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000297 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000298 bitmap.getPixels(),
299 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000300}
301
302void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y) {
303 SkAutoLockPixels alp(bitmap);
304 if (!bitmap.readyToDraw()) {
305 return;
306 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000307 GrPixelConfig config = SkGr::BitmapConfig2PixelConfig(bitmap.config(),
308 bitmap.isOpaque());
reed@google.comac10a2d2010-12-22 21:39:39 +0000309 fContext->setRenderTarget(fRenderTarget);
310 // we aren't setting the clip or matrix, so mark as dirty
311 // we don't need to set them for this call and don't have them anyway
312 fNeedPrepareRenderTarget = true;
313
314 fContext->writePixels(x, y, bitmap.width(), bitmap.height(),
315 config, bitmap.getPixels(), bitmap.rowBytes());
316}
317
318///////////////////////////////////////////////////////////////////////////////
319
320static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000321 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000322 const SkRegion& clipRegion,
323 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000324 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000325
326 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000327 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000328 const SkIRect& skBounds = clipRegion.getBounds();
329 GrRect bounds;
330 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
331 GrIntToScalar(skBounds.fTop),
332 GrIntToScalar(skBounds.fRight),
333 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000334 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
335 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000336 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000337}
338
339// call this ever each draw call, to ensure that the context reflects our state,
340// and not the state from some other canvas/device
341void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
342 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000343 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000344
345 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000346 SkASSERT(draw.fClipStack);
347 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000348 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000349 fNeedPrepareRenderTarget = false;
350 }
351}
352
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000353void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
354 const SkClipStack& clipStack) {
355 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
356 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000357 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000358}
359
360void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000361 const SkRegion& clip, const SkClipStack& clipStack) {
362
reed@google.comac10a2d2010-12-22 21:39:39 +0000363 fContext->setRenderTarget(fRenderTarget);
364
bsalomon@google.comd302f142011-03-03 13:54:13 +0000365 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000366
reed@google.com6f8f2922011-03-04 22:27:10 +0000367 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000368
369 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000370 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000371 fNeedClear = false;
372 }
373}
374
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000375bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000377 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 return true;
379 }
380 return false;
381}
382
383///////////////////////////////////////////////////////////////////////////////
384
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000385SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
386SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
387SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
388SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
389SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
390 shader_type_mismatch);
391SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000392
bsalomon@google.com5782d712011-01-21 21:03:59 +0000393static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
394 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
395 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
396 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
397 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
398 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
399};
400
401bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
402 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000403 GrPaint* grPaint,
404 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000405
406 grPaint->fDither = skPaint.isDither();
407 grPaint->fAntiAlias = skPaint.isAntiAlias();
408
409 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
410 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
411
412 SkXfermode* mode = skPaint.getXfermode();
413 if (mode) {
414 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000415 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000416#if 0
417 return false;
418#endif
419 }
420 }
421 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
422 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
423
424 if (justAlpha) {
425 uint8_t alpha = skPaint.getAlpha();
426 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000427 // justAlpha is currently set to true only if there is a texture,
428 // so constantColor should not also be true.
429 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000430 } else {
431 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000432 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000433 }
Scroggo97c88c22011-05-11 14:05:25 +0000434 SkColorFilter* colorFilter = skPaint.getColorFilter();
435 SkColor color;
436 SkXfermode::Mode filterMode;
437 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000438 if (!constantColor) {
439 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
440 grPaint->fColorFilterXfermode = filterMode;
441 return true;
442 }
443 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
444 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000445 }
Scroggod757df22011-05-16 13:11:16 +0000446 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000447 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000448}
449
bsalomon@google.com5782d712011-01-21 21:03:59 +0000450bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
451 SkAutoCachedTexture* act,
452 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000453 GrPaint* grPaint,
454 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000455
bsalomon@google.com5782d712011-01-21 21:03:59 +0000456 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000457
bsalomon@google.com5782d712011-01-21 21:03:59 +0000458 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000459 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000460 return this->skPaint2GrPaintNoShader(skPaint,
461 false,
462 grPaint,
463 constantColor);
464 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000465 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000466 }
467
reed@google.comac10a2d2010-12-22 21:39:39 +0000468 SkBitmap bitmap;
469 SkMatrix matrix;
470 SkShader::TileMode tileModes[2];
471 SkScalar twoPointParams[3];
472 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
473 tileModes, twoPointParams);
474
bsalomon@google.com5782d712011-01-21 21:03:59 +0000475 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
476 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000477 SkShader::GradientInfo info;
478 SkColor color;
479
480 info.fColors = &color;
481 info.fColorOffsets = NULL;
482 info.fColorCount = 1;
483 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
484 SkPaint copy(skPaint);
485 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000486 // modulate the paint alpha by the shader's solid color alpha
487 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
488 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000489 return this->skPaint2GrPaintNoShader(copy,
490 false,
491 grPaint,
492 constantColor);
493 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000494 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000495 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000496 GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx);
497 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000498 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000499 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000500 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000501 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000502 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000503 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
504 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000505 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000506 sampler->setRadial2Params(twoPointParams[0],
507 twoPointParams[1],
508 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000509 }
510
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000511 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000512 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000513 SkDebugf("Couldn't convert bitmap to texture.\n");
514 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000515 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000516 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000517
518 // since our texture coords will be in local space, we wack the texture
519 // matrix to map them back into 0...1 before we load it
520 SkMatrix localM;
521 if (shader->getLocalMatrix(&localM)) {
522 SkMatrix inverse;
523 if (localM.invert(&inverse)) {
524 matrix.preConcat(inverse);
525 }
526 }
527 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000528 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
529 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000530 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000531 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000532 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000533 matrix.postScale(s, s);
534 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000535 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000536
537 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000538}
539
540///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000541
bsalomon@google.com398109c2011-04-14 18:40:27 +0000542void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000543 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000544}
545
reed@google.comac10a2d2010-12-22 21:39:39 +0000546void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
547 CHECK_SHOULD_DRAW(draw);
548
bsalomon@google.com5782d712011-01-21 21:03:59 +0000549 GrPaint grPaint;
550 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000551 if (!this->skPaint2GrPaintShader(paint,
552 &act,
553 *draw.fMatrix,
554 &grPaint,
555 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000556 return;
557 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000558
559 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000560}
561
562// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000563static const GrPrimitiveType gPointMode2PrimtiveType[] = {
564 kPoints_PrimitiveType,
565 kLines_PrimitiveType,
566 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000567};
568
569void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000570 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000571 CHECK_SHOULD_DRAW(draw);
572
573 SkScalar width = paint.getStrokeWidth();
574 if (width < 0) {
575 return;
576 }
577
578 // we only handle hairlines here, else we let the SkDraw call our drawPath()
579 if (width > 0) {
580 draw.drawPoints(mode, count, pts, paint, true);
581 return;
582 }
583
bsalomon@google.com5782d712011-01-21 21:03:59 +0000584 GrPaint grPaint;
585 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000586 if (!this->skPaint2GrPaintShader(paint,
587 &act,
588 *draw.fMatrix,
589 &grPaint,
590 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000591 return;
592 }
593
bsalomon@google.com5782d712011-01-21 21:03:59 +0000594 fContext->drawVertices(grPaint,
595 gPointMode2PrimtiveType[mode],
596 count,
597 (GrPoint*)pts,
598 NULL,
599 NULL,
600 NULL,
601 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000602}
603
reed@google.comc9aa5872011-04-05 21:05:37 +0000604///////////////////////////////////////////////////////////////////////////////
605
reed@google.comac10a2d2010-12-22 21:39:39 +0000606void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
607 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000608 CHECK_SHOULD_DRAW(draw);
609
bungeman@google.com79bd8772011-07-18 15:34:08 +0000610 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000611 SkScalar width = paint.getStrokeWidth();
612
613 /*
614 We have special code for hairline strokes, miter-strokes, and fills.
615 Anything else we just call our path code.
616 */
617 bool usePath = doStroke && width > 0 &&
618 paint.getStrokeJoin() != SkPaint::kMiter_Join;
619 // another reason we might need to call drawPath...
620 if (paint.getMaskFilter()) {
621 usePath = true;
622 }
reed@google.com67db6642011-05-26 11:46:35 +0000623 // until we aa rotated rects...
624 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
625 usePath = true;
626 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000627 // small miter limit means right angles show bevel...
628 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
629 paint.getStrokeMiter() < SK_ScalarSqrt2)
630 {
631 usePath = true;
632 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000633 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000634 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
635 usePath = true;
636 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000637
638 if (usePath) {
639 SkPath path;
640 path.addRect(rect);
641 this->drawPath(draw, path, paint, NULL, true);
642 return;
643 }
644
645 GrPaint grPaint;
646 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000647 if (!this->skPaint2GrPaintShader(paint,
648 &act,
649 *draw.fMatrix,
650 &grPaint,
651 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000652 return;
653 }
reed@google.com20efde72011-05-09 17:00:02 +0000654 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000655}
656
reed@google.com69302852011-02-16 18:08:07 +0000657#include "SkMaskFilter.h"
658#include "SkBounder.h"
659
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000660static GrPathFill skToGrFillType(SkPath::FillType fillType) {
661 switch (fillType) {
662 case SkPath::kWinding_FillType:
663 return kWinding_PathFill;
664 case SkPath::kEvenOdd_FillType:
665 return kEvenOdd_PathFill;
666 case SkPath::kInverseWinding_FillType:
667 return kInverseWinding_PathFill;
668 case SkPath::kInverseEvenOdd_FillType:
669 return kInverseEvenOdd_PathFill;
670 default:
671 SkDebugf("Unsupported path fill type\n");
672 return kHairLine_PathFill;
673 }
674}
675
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000676static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000677 int halfWidth = (kernelWidth - 1) / 2;
678 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000679 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000680 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000681 float x = static_cast<float>(i - halfWidth);
682 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
683 // is dropped here, since we renormalize the kernel below.
684 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000685 sum += kernel[i];
686 }
687 // Normalize the kernel
688 float scale = 1.0f / sum;
689 for (int i = 0; i < kernelWidth; ++i)
690 kernel[i] *= scale;
691}
692
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000693static void scaleRect(SkRect* rect, float xScale, float yScale) {
694 rect->fLeft *= xScale;
695 rect->fTop *= yScale;
696 rect->fRight *= xScale;
697 rect->fBottom *= yScale;
698}
699
700static float adjustSigma(float sigma, int *scaleFactor, int *halfWidth,
701 int *kernelWidth) {
702 *scaleFactor = 1;
703 while (sigma > MAX_BLUR_SIGMA) {
704 *scaleFactor *= 2;
705 sigma *= 0.5f;
706 }
707 *halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
708 *kernelWidth = *halfWidth * 2 + 1;
709 return sigma;
710}
711
712// Apply a Gaussian blur to srcTexture by sigmaX and sigmaY, within the given
713// rect.
714// temp1 and temp2 are used for allocation of intermediate textures.
715// If temp2 is non-NULL, srcTexture will be untouched, and the return
716// value will be either temp1 or temp2.
717// If temp2 is NULL, srcTexture will be overwritten with intermediate
718// results, and the return value will either be temp1 or srcTexture.
719static GrTexture* gaussianBlur(GrContext* context, GrTexture* srcTexture,
720 GrAutoScratchTexture* temp1,
721 GrAutoScratchTexture* temp2,
722 const SkRect& rect,
723 float sigmaX, float sigmaY) {
724
725 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
726 GrClip oldClip = context->getClip();
727 GrTexture* origTexture = srcTexture;
728 GrAutoMatrix avm(context, GrMatrix::I());
729 SkIRect clearRect;
730 int scaleFactorX, halfWidthX, kernelWidthX;
731 int scaleFactorY, halfWidthY, kernelWidthY;
732 sigmaX = adjustSigma(sigmaX, &scaleFactorX, &halfWidthX, &kernelWidthX);
733 sigmaY = adjustSigma(sigmaY, &scaleFactorY, &halfWidthY, &kernelWidthY);
734
735 SkRect srcRect(rect);
736 scaleRect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
737 srcRect.roundOut();
738 scaleRect(&srcRect, scaleFactorX, scaleFactorY);
739 context->setClip(srcRect);
740
741 const GrTextureDesc desc = {
742 kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit,
743 kNone_GrAALevel,
744 srcRect.width(),
745 srcRect.height(),
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +0000746 { kRGBA_8888_GrPixelConfig }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000747 };
748
749 temp1->set(context, desc);
750 if (temp2) temp2->set(context, desc);
751
752 GrTexture* dstTexture = temp1->texture();
753 GrPaint paint;
754 paint.reset();
755 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
756
757 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
758 GrMatrix sampleM;
759 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
760 paint.getTextureSampler(0)->setMatrix(sampleM);
761 context->setRenderTarget(dstTexture->asRenderTarget());
762 SkRect dstRect(srcRect);
763 scaleRect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
764 i < scaleFactorY ? 0.5f : 1.0f);
765 paint.setTexture(0, srcTexture);
766 context->drawRectToRect(paint, dstRect, srcRect);
767 srcRect = dstRect;
768 SkTSwap(srcTexture, dstTexture);
769 // If temp2 is non-NULL, don't render back to origTexture
770 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
771 }
772
773 if (sigmaX > 0.0f) {
774 SkAutoTMalloc<float> kernelStorageX(kernelWidthX);
775 float* kernelX = kernelStorageX.get();
776 buildKernel(sigmaX, kernelX, kernelWidthX);
777
778 if (scaleFactorX > 1) {
779 // Clear out a halfWidth to the right of the srcRect to prevent the
780 // X convolution from reading garbage.
781 clearRect = SkIRect::MakeXYWH(
782 srcRect.fRight, srcRect.fTop, halfWidthX, srcRect.height());
783 context->clear(&clearRect, 0x0);
784 }
785
786 context->setRenderTarget(dstTexture->asRenderTarget());
787 context->convolveInX(srcTexture, srcRect, kernelX, kernelWidthX);
788 SkTSwap(srcTexture, dstTexture);
789 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
790 }
791
792 if (sigmaY > 0.0f) {
793 SkAutoTMalloc<float> kernelStorageY(kernelWidthY);
794 float* kernelY = kernelStorageY.get();
795 buildKernel(sigmaY, kernelY, kernelWidthY);
796
797 if (scaleFactorY > 1 || sigmaX > 0.0f) {
798 // Clear out a halfWidth below the srcRect to prevent the Y
799 // convolution from reading garbage.
800 clearRect = SkIRect::MakeXYWH(
801 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidthY);
802 context->clear(&clearRect, 0x0);
803 }
804
805 context->setRenderTarget(dstTexture->asRenderTarget());
806 context->convolveInY(srcTexture, srcRect, kernelY, kernelWidthY);
807 SkTSwap(srcTexture, dstTexture);
808 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
809 }
810
811 if (scaleFactorX > 1 || scaleFactorY > 1) {
812 // Clear one pixel to the right and below, to accommodate bilinear
813 // upsampling.
814 clearRect = SkIRect::MakeXYWH(
815 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
816 context->clear(&clearRect, 0x0);
817 clearRect = SkIRect::MakeXYWH(
818 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
819 context->clear(&clearRect, 0x0);
820 // FIXME: This should be mitchell, not bilinear.
821 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
822 GrMatrix sampleM;
823 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
824 paint.getTextureSampler(0)->setMatrix(sampleM);
825 context->setRenderTarget(dstTexture->asRenderTarget());
826 paint.setTexture(0, srcTexture);
827 SkRect dstRect(srcRect);
828 scaleRect(&dstRect, scaleFactorX, scaleFactorY);
829 context->drawRectToRect(paint, dstRect, srcRect);
830 srcRect = dstRect;
831 SkTSwap(srcTexture, dstTexture);
832 }
833 context->setRenderTarget(oldRenderTarget);
834 context->setClip(oldClip);
835 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000836}
837
838static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
839 SkMaskFilter* filter, const SkMatrix& matrix,
840 const SkRegion& clip, SkBounder* bounder,
841 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000842#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000843 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000844#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000845 SkMaskFilter::BlurInfo info;
846 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000847 if (SkMaskFilter::kNone_BlurType == blurType ||
848 !context->supportsShaders()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000849 return false;
850 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000851 SkScalar radius = info.fIgnoreTransform ? info.fRadius
852 : matrix.mapRadius(info.fRadius);
853 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000854 if (radius <= 0) {
855 return false;
856 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000857 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000858 float sigma3 = sigma * 3.0f;
859
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000860 SkRect srcRect = path.getBounds();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000861 SkRect clipRect;
862 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000863
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000864 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
865 srcRect.inset(-sigma3, -sigma3);
866 clipRect.inset(-sigma3, -sigma3);
867 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000868 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000869 SkIRect finalIRect;
870 finalRect.roundOut(&finalIRect);
871 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000872 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000873 }
874 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000875 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000876 }
877 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000878 srcRect.offset(offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000879 const GrTextureDesc desc = {
880 kRenderTarget_GrTextureFlagBit,
881 kNone_GrAALevel,
882 srcRect.width(),
883 srcRect.height(),
884 // We actually only need A8, but it often isn't supported as a
885 // render target
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +0000886 { kRGBA_8888_PM_GrPixelConfig }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000887 };
888
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000889 GrAutoScratchTexture pathEntry(context, desc);
890 GrTexture* pathTexture = pathEntry.texture();
891 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000892 return false;
893 }
894 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000895 // Once this code moves into GrContext, this should be changed to use
896 // an AutoClipRestore.
897 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000898 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000899 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000900 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000901 GrPaint tempPaint;
902 tempPaint.reset();
903
904 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000905 tempPaint.fAntiAlias = grp->fAntiAlias;
906 if (tempPaint.fAntiAlias) {
907 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
908 // blend coeff of zero requires dual source blending support in order
909 // to properly blend partially covered pixels. This means the AA
910 // code path may not be taken. So we use a dst blend coeff of ISA. We
911 // could special case AA draws to a dst surface with known alpha=0 to
912 // use a zero dst coeff when dual source blending isn't available.
913 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
914 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
915 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000916 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000917 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000918
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000919 GrAutoScratchTexture temp1, temp2;
920 // If we're doing a normal blur, we can clobber the pathTexture in the
921 // gaussianBlur. Otherwise, we need to save it for later compositing.
922 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
923 GrTexture* blurTexture = gaussianBlur(context, pathTexture,
924 &temp1, isNormalBlur ? NULL : &temp2,
925 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000926
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000927 if (!isNormalBlur) {
928 GrPaint paint;
929 paint.reset();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000930 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000931 GrMatrix sampleM;
932 sampleM.setIDiv(pathTexture->width(), pathTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000933 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000934 // Blend pathTexture over blurTexture.
935 context->setRenderTarget(blurTexture->asRenderTarget());
936 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000937 if (SkMaskFilter::kInner_BlurType == blurType) {
938 // inner: dst = dst * src
939 paint.fSrcBlendCoeff = kDC_BlendCoeff;
940 paint.fDstBlendCoeff = kZero_BlendCoeff;
941 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
942 // solid: dst = src + dst - src * dst
943 // = (1 - dst) * src + 1 * dst
944 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
945 paint.fDstBlendCoeff = kOne_BlendCoeff;
946 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
947 // outer: dst = dst * (1 - src)
948 // = 0 * src + (1 - src) * dst
949 paint.fSrcBlendCoeff = kZero_BlendCoeff;
950 paint.fDstBlendCoeff = kISC_BlendCoeff;
951 }
952 context->drawRect(paint, srcRect);
953 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000954 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000955 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000956
957 if (grp->hasTextureOrMask()) {
958 GrMatrix inverse;
959 if (!matrix.invert(&inverse)) {
960 return false;
961 }
962 grp->preConcatActiveSamplerMatrices(inverse);
963 }
964
965 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
966 // we assume the last mask index is available for use
967 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000968 grp->setMask(MASK_IDX, blurTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000969 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
970
971 GrMatrix m;
972 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000973 m.postIDiv(blurTexture->width(), blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000974 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
975 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000976 return true;
977}
978
reed@google.com69302852011-02-16 18:08:07 +0000979static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
980 SkMaskFilter* filter, const SkMatrix& matrix,
981 const SkRegion& clip, SkBounder* bounder,
982 GrPaint* grp) {
983 SkMask srcM, dstM;
984
985 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
986 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
987 return false;
988 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000989 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000990
991 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
992 return false;
993 }
994 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000995 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000996
997 if (clip.quickReject(dstM.fBounds)) {
998 return false;
999 }
1000 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1001 return false;
1002 }
1003
1004 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1005 // the current clip (and identity matrix) and grpaint settings
1006
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001007 // used to compute inverse view, if necessary
1008 GrMatrix ivm = context->getMatrix();
1009
reed@google.com0c219b62011-02-16 21:31:18 +00001010 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001011
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001012 const GrTextureDesc desc = {
1013 kNone_GrTextureFlags,
1014 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +00001015 dstM.fBounds.width(),
1016 dstM.fBounds.height(),
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +00001017 { kAlpha_8_GrPixelConfig }
reed@google.com69302852011-02-16 18:08:07 +00001018 };
1019
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001020 GrAutoScratchTexture ast(context, desc);
1021 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001022
reed@google.com69302852011-02-16 18:08:07 +00001023 if (NULL == texture) {
1024 return false;
1025 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001026 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
1027 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001028
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001029 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
1030 grp->preConcatActiveSamplerMatrices(ivm);
1031 }
1032
1033 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1034 // we assume the last mask index is available for use
1035 GrAssert(NULL == grp->getMask(MASK_IDX));
1036 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001037 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +00001038
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001039 GrRect d;
1040 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001041 GrIntToScalar(dstM.fBounds.fTop),
1042 GrIntToScalar(dstM.fBounds.fRight),
1043 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001044
1045 GrMatrix m;
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +00001046 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1047 -dstM.fBounds.fTop*SK_Scalar1);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001048 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001049 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1050
1051 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001052 return true;
1053}
reed@google.com69302852011-02-16 18:08:07 +00001054
reed@google.com0c219b62011-02-16 21:31:18 +00001055void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comfe626382011-09-21 13:50:35 +00001056 const SkPaint& origPaint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001057 bool pathIsMutable) {
1058 CHECK_SHOULD_DRAW(draw);
1059
reed@google.comfe626382011-09-21 13:50:35 +00001060 bool doFill = true;
1061 SkTLazy<SkPaint> lazyPaint;
1062 const SkPaint* paint = &origPaint;
1063
1064 // can we cheat, and threat a thin stroke as a hairline (w/ modulated alpha)
1065 // if we can, we draw lots faster (raster device does this same test)
1066 {
1067 SkAlpha newAlpha;
1068 if (SkDrawTreatAsHairline(*paint, *draw.fMatrix, &newAlpha)) {
1069 lazyPaint.set(*paint);
1070 lazyPaint.get()->setAlpha(newAlpha);
1071 lazyPaint.get()->setStrokeWidth(0);
1072 paint = lazyPaint.get();
1073 doFill = false;
1074 }
1075 }
1076 // must reference paint from here down, and not origPaint
1077 // since we may have change the paint (using lazyPaint for storage)
1078
bsalomon@google.com5782d712011-01-21 21:03:59 +00001079 GrPaint grPaint;
1080 SkAutoCachedTexture act;
reed@google.comfe626382011-09-21 13:50:35 +00001081 if (!this->skPaint2GrPaintShader(*paint,
Scroggod757df22011-05-16 13:11:16 +00001082 &act,
1083 *draw.fMatrix,
1084 &grPaint,
1085 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001086 return;
1087 }
1088
reed@google.comfe626382011-09-21 13:50:35 +00001089 // If we have a prematrix, apply it to the path, optimizing for the case
1090 // where the original path can in fact be modified in place (even though
1091 // its parameter type is const).
1092 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1093 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001094
1095 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001096 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001097
reed@google.come3445642011-02-16 23:20:39 +00001098 if (!pathIsMutable) {
1099 result = &tmpPath;
1100 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001101 }
reed@google.come3445642011-02-16 23:20:39 +00001102 // should I push prePathMatrix on our MV stack temporarily, instead
1103 // of applying it here? See SkDraw.cpp
1104 pathPtr->transform(*prePathMatrix, result);
1105 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001106 }
reed@google.com0c219b62011-02-16 21:31:18 +00001107 // at this point we're done with prePathMatrix
1108 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001109
reed@google.comfe626382011-09-21 13:50:35 +00001110 if (doFill && (paint->getPathEffect() ||
1111 paint->getStyle() != SkPaint::kFill_Style)) {
1112 // it is safe to use tmpPath here, even if we already used it for the
1113 // prepathmatrix, since getFillPath can take the same object for its
1114 // input and output safely.
1115 doFill = paint->getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001116 pathPtr = &tmpPath;
1117 }
1118
reed@google.comfe626382011-09-21 13:50:35 +00001119 if (paint->getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001120 // avoid possibly allocating a new path in transform if we can
1121 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1122
1123 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001124 pathPtr->transform(*draw.fMatrix, devPathPtr);
reed@google.comfe626382011-09-21 13:50:35 +00001125 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001126 *draw.fMatrix, *draw.fClip, draw.fBounder,
1127 &grPaint)) {
reed@google.comfe626382011-09-21 13:50:35 +00001128 drawWithMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001129 *draw.fMatrix, *draw.fClip, draw.fBounder,
1130 &grPaint);
1131 }
reed@google.com69302852011-02-16 18:08:07 +00001132 return;
1133 }
reed@google.com69302852011-02-16 18:08:07 +00001134
bsalomon@google.comffca4002011-02-22 20:34:01 +00001135 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001136
reed@google.com0c219b62011-02-16 21:31:18 +00001137 if (doFill) {
1138 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001139 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001140 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001141 break;
1142 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001143 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001144 break;
1145 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001146 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001147 break;
1148 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001149 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001150 break;
1151 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001152 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001153 return;
1154 }
1155 }
1156
reed@google.com07f3ee12011-05-16 17:21:57 +00001157 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001158}
1159
reed@google.comac10a2d2010-12-22 21:39:39 +00001160void SkGpuDevice::drawBitmap(const SkDraw& draw,
1161 const SkBitmap& bitmap,
1162 const SkIRect* srcRectPtr,
1163 const SkMatrix& m,
1164 const SkPaint& paint) {
1165 CHECK_SHOULD_DRAW(draw);
1166
1167 SkIRect srcRect;
1168 if (NULL == srcRectPtr) {
1169 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1170 } else {
1171 srcRect = *srcRectPtr;
1172 }
1173
junov@google.comd935cfb2011-06-27 20:48:23 +00001174 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001175 // Convert the bitmap to a shader so that the rect can be drawn
1176 // through drawRect, which supports mask filters.
1177 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001178 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001179 if (srcRectPtr) {
1180 if (!bitmap.extractSubset(&tmp, srcRect)) {
1181 return; // extraction failed
1182 }
1183 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001184 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001185 }
1186 SkPaint paintWithTexture(paint);
1187 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1188 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001189 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001190 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001191
junov@google.com1d329782011-07-28 20:10:09 +00001192 // Transform 'm' needs to be concatenated to the draw matrix,
1193 // rather than transforming the primitive directly, so that 'm' will
1194 // also affect the behavior of the mask filter.
1195 SkMatrix drawMatrix;
1196 drawMatrix.setConcat(*draw.fMatrix, m);
1197 SkDraw transformedDraw(draw);
1198 transformedDraw.fMatrix = &drawMatrix;
1199
1200 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1201
junov@google.comd935cfb2011-06-27 20:48:23 +00001202 return;
1203 }
1204
bsalomon@google.com5782d712011-01-21 21:03:59 +00001205 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001206 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001207 return;
1208 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001209 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001210 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001211 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001212 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001213 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001214 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001215
bsalomon@google.com91958362011-06-13 17:58:13 +00001216 const int maxTextureSize = fContext->getMaxTextureSize();
1217 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1218 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001219 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001220 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001221 return;
1222 }
1223
1224 // undo the translate done by SkCanvas
1225 int DX = SkMax32(0, srcRect.fLeft);
1226 int DY = SkMax32(0, srcRect.fTop);
1227 // compute clip bounds in local coordinates
1228 SkIRect clipRect;
1229 {
1230 SkRect r;
1231 r.set(draw.fClip->getBounds());
1232 SkMatrix matrix, inverse;
1233 matrix.setConcat(*draw.fMatrix, m);
1234 if (!matrix.invert(&inverse)) {
1235 return;
1236 }
1237 inverse.mapRect(&r);
1238 r.roundOut(&clipRect);
1239 // apply the canvas' translate to our local clip
1240 clipRect.offset(DX, DY);
1241 }
1242
bsalomon@google.com91958362011-06-13 17:58:13 +00001243 int nx = bitmap.width() / maxTextureSize;
1244 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001245 for (int x = 0; x <= nx; x++) {
1246 for (int y = 0; y <= ny; y++) {
1247 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001248 tileR.set(x * maxTextureSize, y * maxTextureSize,
1249 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001250 if (!SkIRect::Intersects(tileR, clipRect)) {
1251 continue;
1252 }
1253
1254 SkIRect srcR = tileR;
1255 if (!srcR.intersect(srcRect)) {
1256 continue;
1257 }
1258
1259 SkBitmap tmpB;
1260 if (bitmap.extractSubset(&tmpB, tileR)) {
1261 // now offset it to make it "local" to our tmp bitmap
1262 srcR.offset(-tileR.fLeft, -tileR.fTop);
1263
1264 SkMatrix tmpM(m);
1265 {
1266 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1267 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1268 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1269 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001270 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001271 }
1272 }
1273 }
1274}
1275
1276/*
1277 * This is called by drawBitmap(), which has to handle images that may be too
1278 * large to be represented by a single texture.
1279 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001280 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1281 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 */
1283void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1284 const SkBitmap& bitmap,
1285 const SkIRect& srcRect,
1286 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001287 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001288 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1289 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001290
reed@google.com9c49bc32011-07-07 13:42:37 +00001291 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001292 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001293 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 return;
1295 }
1296
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001297 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1298
1299 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1300 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1301 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1302 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001303
1304 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001305 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001306 if (NULL == texture) {
1307 return;
1308 }
1309
bsalomon@google.com452943d2011-10-31 17:37:14 +00001310 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001311
reed@google.com20efde72011-05-09 17:00:02 +00001312 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1313 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001314 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001315 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1316 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1317 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001318 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001319
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001320 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001321 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001322 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001323 // If drawing a subrect of the bitmap and filtering is enabled,
1324 // use a constrained texture domain to avoid color bleeding
1325 GrScalar left, top, right, bottom;
1326 if (srcRect.width() > 1) {
1327 GrScalar border = GR_ScalarHalf / bitmap.width();
1328 left = paintRect.left() + border;
1329 right = paintRect.right() - border;
1330 } else {
1331 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1332 }
1333 if (srcRect.height() > 1) {
1334 GrScalar border = GR_ScalarHalf / bitmap.height();
1335 top = paintRect.top() + border;
1336 bottom = paintRect.bottom() - border;
1337 } else {
1338 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1339 }
1340 GrRect textureDomain;
1341 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001342 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001343 }
1344
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001345 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001346}
1347
1348void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1349 int left, int top, const SkPaint& paint) {
1350 CHECK_SHOULD_DRAW(draw);
1351
1352 SkAutoLockPixels alp(bitmap);
1353 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1354 return;
1355 }
1356
bsalomon@google.com5782d712011-01-21 21:03:59 +00001357 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001358 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001359 return;
1360 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001361
bsalomon@google.com5782d712011-01-21 21:03:59 +00001362 GrAutoMatrix avm(fContext, GrMatrix::I());
1363
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001364 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001365
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001366 GrTexture* texture;
1367 sampler->setClampNoFilter();
1368 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1369
1370 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001371
bsalomon@google.com5782d712011-01-21 21:03:59 +00001372 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001373 GrRect::MakeXYWH(GrIntToScalar(left),
1374 GrIntToScalar(top),
1375 GrIntToScalar(bitmap.width()),
1376 GrIntToScalar(bitmap.height())),
1377 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001378}
1379
1380void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1381 int x, int y, const SkPaint& paint) {
1382 CHECK_SHOULD_DRAW(draw);
1383
bsalomon@google.com5782d712011-01-21 21:03:59 +00001384 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001385 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001386 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001387 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001388 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001389
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001390 GrTexture* devTex = grPaint.getTexture(0);
1391 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001392
1393 const SkBitmap& bm = dev->accessBitmap(false);
1394 int w = bm.width();
1395 int h = bm.height();
1396
1397 GrAutoMatrix avm(fContext, GrMatrix::I());
1398
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001399 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001400
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001401 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1402 GrIntToScalar(y),
1403 GrIntToScalar(w),
1404 GrIntToScalar(h));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +00001405 SkImageFilter* imageFilter = paint.getImageFilter();
1406 SkSize size;
1407 if (NULL != imageFilter && imageFilter->asABlur(&size)) {
1408 GrAutoScratchTexture temp1, temp2;
1409 GrTexture* blurTexture = gaussianBlur(fContext,
1410 devTex, &temp1, &temp2,
1411 GrRect::MakeWH(w, h),
1412 size.width(),
1413 size.height());
1414 grPaint.setTexture(kBitmapTextureIdx, blurTexture);
1415 devTex = blurTexture;
1416 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001417 // The device being drawn may not fill up its texture (saveLayer uses
1418 // the approximate ).
1419 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1420 GR_Scalar1 * h / devTex->height());
1421
1422 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001423}
1424
1425///////////////////////////////////////////////////////////////////////////////
1426
1427// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001428static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1429 kTriangles_PrimitiveType,
1430 kTriangleStrip_PrimitiveType,
1431 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001432};
1433
1434void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1435 int vertexCount, const SkPoint vertices[],
1436 const SkPoint texs[], const SkColor colors[],
1437 SkXfermode* xmode,
1438 const uint16_t indices[], int indexCount,
1439 const SkPaint& paint) {
1440 CHECK_SHOULD_DRAW(draw);
1441
bsalomon@google.com5782d712011-01-21 21:03:59 +00001442 GrPaint grPaint;
1443 SkAutoCachedTexture act;
1444 // we ignore the shader if texs is null.
1445 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001446 if (!this->skPaint2GrPaintNoShader(paint,
1447 false,
1448 &grPaint,
1449 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001450 return;
1451 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001452 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001453 if (!this->skPaint2GrPaintShader(paint, &act,
1454 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001455 &grPaint,
1456 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001457 return;
1458 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001459 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001460
1461 if (NULL != xmode && NULL != texs && NULL != colors) {
1462 SkXfermode::Mode mode;
1463 if (!SkXfermode::IsMode(xmode, &mode) ||
1464 SkXfermode::kMultiply_Mode != mode) {
1465 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1466#if 0
1467 return
1468#endif
1469 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001470 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001471
bsalomon@google.com498776a2011-08-16 19:20:44 +00001472 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1473 if (NULL != colors) {
1474 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001475 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001476 for (int i = 0; i < vertexCount; ++i) {
1477 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1478 }
1479 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001480 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001481 fContext->drawVertices(grPaint,
1482 gVertexMode2PrimitiveType[vmode],
1483 vertexCount,
1484 (GrPoint*) vertices,
1485 (GrPoint*) texs,
1486 colors,
1487 indices,
1488 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001489}
1490
1491///////////////////////////////////////////////////////////////////////////////
1492
1493static void GlyphCacheAuxProc(void* data) {
1494 delete (GrFontScaler*)data;
1495}
1496
1497static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1498 void* auxData;
1499 GrFontScaler* scaler = NULL;
1500 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1501 scaler = (GrFontScaler*)auxData;
1502 }
1503 if (NULL == scaler) {
1504 scaler = new SkGrFontScaler(cache);
1505 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1506 }
1507 return scaler;
1508}
1509
1510static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1511 SkFixed fx, SkFixed fy,
1512 const SkGlyph& glyph) {
1513 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1514
1515 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1516
1517 if (NULL == procs->fFontScaler) {
1518 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1519 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001520
1521 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001522 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001523 *
reed@google.com3b139f52011-06-07 17:56:25 +00001524 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1525 * It calls that rather than round, because our caller has already added
1526 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1527 *
1528 * Test code between raster and gpu (they should draw the same)
1529 *
1530 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1531 *
1532 * Perhaps we should only perform this integralization if there is no
1533 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001534 */
reed@google.com3b139f52011-06-07 17:56:25 +00001535 fy = SkFixedFloorToFixed(fy);
1536
reed@google.comac10a2d2010-12-22 21:39:39 +00001537 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001538 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001539 procs->fFontScaler);
1540}
1541
bsalomon@google.com5782d712011-01-21 21:03:59 +00001542SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001543
1544 // deferred allocation
1545 if (NULL == fDrawProcs) {
1546 fDrawProcs = new GrSkDrawProcs;
1547 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1548 fDrawProcs->fContext = fContext;
1549 }
1550
1551 // init our (and GL's) state
1552 fDrawProcs->fTextContext = context;
1553 fDrawProcs->fFontScaler = NULL;
1554 return fDrawProcs;
1555}
1556
1557void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1558 size_t byteLength, SkScalar x, SkScalar y,
1559 const SkPaint& paint) {
1560 CHECK_SHOULD_DRAW(draw);
1561
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001562 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001563 // this guy will just call our drawPath()
1564 draw.drawText((const char*)text, byteLength, x, y, paint);
1565 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001566 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001567
1568 GrPaint grPaint;
1569 SkAutoCachedTexture act;
1570
Scroggod757df22011-05-16 13:11:16 +00001571 if (!this->skPaint2GrPaintShader(paint,
1572 &act,
1573 *draw.fMatrix,
1574 &grPaint,
1575 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001576 return;
1577 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001578 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001579 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001580 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1581 }
1582}
1583
1584void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1585 size_t byteLength, const SkScalar pos[],
1586 SkScalar constY, int scalarsPerPos,
1587 const SkPaint& paint) {
1588 CHECK_SHOULD_DRAW(draw);
1589
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001590 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001591 // this guy will just call our drawPath()
1592 draw.drawPosText((const char*)text, byteLength, pos, constY,
1593 scalarsPerPos, paint);
1594 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001595 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001596
1597 GrPaint grPaint;
1598 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001599 if (!this->skPaint2GrPaintShader(paint,
1600 &act,
1601 *draw.fMatrix,
1602 &grPaint,
1603 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001604 return;
1605 }
1606
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001607 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001608 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001609 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1610 scalarsPerPos, paint);
1611 }
1612}
1613
1614void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1615 size_t len, const SkPath& path,
1616 const SkMatrix* m, const SkPaint& paint) {
1617 CHECK_SHOULD_DRAW(draw);
1618
1619 SkASSERT(draw.fDevice == this);
1620 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1621}
1622
1623///////////////////////////////////////////////////////////////////////////////
1624
reed@google.comf67e4cf2011-03-15 20:56:58 +00001625bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1626 if (!paint.isLCDRenderText()) {
1627 // we're cool with the paint as is
1628 return false;
1629 }
1630
1631 if (paint.getShader() ||
1632 paint.getXfermode() || // unless its srcover
1633 paint.getMaskFilter() ||
1634 paint.getRasterizer() ||
1635 paint.getColorFilter() ||
1636 paint.getPathEffect() ||
1637 paint.isFakeBoldText() ||
1638 paint.getStyle() != SkPaint::kFill_Style) {
1639 // turn off lcd
1640 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1641 flags->fHinting = paint.getHinting();
1642 return true;
1643 }
1644 // we're cool with the paint as is
1645 return false;
1646}
1647
1648///////////////////////////////////////////////////////////////////////////////
1649
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001650SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1651 const GrSamplerState& sampler,
1652 TexType type) {
1653 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001654 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001655
bsalomon@google.come97f0852011-06-17 13:10:25 +00001656 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001657 const GrTextureDesc desc = {
1658 kRenderTarget_GrTextureFlagBit,
1659 kNone_GrAALevel,
1660 bitmap.width(),
1661 bitmap.height(),
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +00001662 { SkGr::Bitmap2PixelConfig(bitmap) }
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001663 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001664 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001665 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001666 // we know layers will only be drawn through drawDevice.
1667 // drawDevice has been made to work with content embedded in a
1668 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001669 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001670 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001671 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001672 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001673 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001674 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001675 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001676 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001677 GrContext::TextureKey key = bitmap.getGenerationID();
1678 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001679
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001680 entry = ctx->findAndLockTexture(key, bitmap.width(),
1681 bitmap.height(), sampler);
1682 if (NULL == entry.texture()) {
1683 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001684 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001685 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001686 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001687 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001688 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001689 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001690 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1691 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001692 }
1693 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001694 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001695}
1696
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001697void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1698 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001699}
1700
bsalomon@google.come97f0852011-06-17 13:10:25 +00001701SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1702 int width, int height,
1703 bool isOpaque,
1704 Usage usage) {
1705 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1706 width, height, usage));
1707}
1708