blob: 96d9fc61bcdb893ee14f1849a01bfbac25109cbf [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,
bsalomon@google.com1fadb202011-12-12 16:10:08 +000062 const GrSamplerState* sampler,
reed@google.comac10a2d2010-12-22 21:39:39 +000063 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,
bsalomon@google.com1fadb202011-12-12 16:10:08 +000073 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.com1fadb202011-12-12 16:10:08 +0000198 fCache = this->lockCachedTexture(bm, NULL, type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000199 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
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000302void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
303 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000304 SkAutoLockPixels alp(bitmap);
305 if (!bitmap.readyToDraw()) {
306 return;
307 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000308
309 GrPixelConfig config;
310 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
311 config = config8888_to_gr_config(config8888);
312 } else {
313 config= SkGr::BitmapConfig2PixelConfig(bitmap.config(),
314 bitmap.isOpaque());
315 }
316
bsalomon@google.com6f379512011-11-16 20:36:03 +0000317 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
318 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000319}
320
321///////////////////////////////////////////////////////////////////////////////
322
323static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000324 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000325 const SkRegion& clipRegion,
326 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000327 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000328
329 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000330 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000331 const SkIRect& skBounds = clipRegion.getBounds();
332 GrRect bounds;
333 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
334 GrIntToScalar(skBounds.fTop),
335 GrIntToScalar(skBounds.fRight),
336 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000337 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
338 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000339 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000340}
341
342// call this ever each draw call, to ensure that the context reflects our state,
343// and not the state from some other canvas/device
344void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
345 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000346 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000347
348 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000349 SkASSERT(draw.fClipStack);
350 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000351 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000352 fNeedPrepareRenderTarget = false;
353 }
354}
355
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000356void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
357 const SkClipStack& clipStack) {
358 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
359 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000360 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000361}
362
363void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000364 const SkRegion& clip, const SkClipStack& clipStack) {
365
reed@google.comac10a2d2010-12-22 21:39:39 +0000366 fContext->setRenderTarget(fRenderTarget);
367
bsalomon@google.comd302f142011-03-03 13:54:13 +0000368 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000369
reed@google.com6f8f2922011-03-04 22:27:10 +0000370 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000371
372 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000373 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000374 fNeedClear = false;
375 }
376}
377
reed@google.com75d939b2011-12-07 15:07:23 +0000378SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
379 return (SkGpuRenderTarget*)fRenderTarget;
380}
381
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000382bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000383 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000384 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000385 return true;
386 }
387 return false;
388}
389
390///////////////////////////////////////////////////////////////////////////////
391
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000392SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
393SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
394SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
395SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
396SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
397 shader_type_mismatch);
398SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000399
bsalomon@google.com5782d712011-01-21 21:03:59 +0000400static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
401 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
402 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
403 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
404 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
405 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
406};
407
408bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
409 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000410 GrPaint* grPaint,
411 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000412
413 grPaint->fDither = skPaint.isDither();
414 grPaint->fAntiAlias = skPaint.isAntiAlias();
415
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000416 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
417 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000418
419 SkXfermode* mode = skPaint.getXfermode();
420 if (mode) {
421 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000422 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000423#if 0
424 return false;
425#endif
426 }
427 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000428 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
429 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
430
bsalomon@google.com5782d712011-01-21 21:03:59 +0000431 if (justAlpha) {
432 uint8_t alpha = skPaint.getAlpha();
433 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000434 // justAlpha is currently set to true only if there is a texture,
435 // so constantColor should not also be true.
436 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000437 } else {
438 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000439 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000440 }
Scroggo97c88c22011-05-11 14:05:25 +0000441 SkColorFilter* colorFilter = skPaint.getColorFilter();
442 SkColor color;
443 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000444 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000445 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000446 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000447 if (!constantColor) {
448 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
449 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000450 } else {
451 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
452 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000453 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000454 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000455 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
456 grPaint->fColorMatrixEnabled = true;
457 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
458 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
459 } else {
460 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000461 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000462 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000463}
464
bsalomon@google.com5782d712011-01-21 21:03:59 +0000465bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
466 SkAutoCachedTexture* act,
467 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000468 GrPaint* grPaint,
469 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000470
bsalomon@google.com5782d712011-01-21 21:03:59 +0000471 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000472
bsalomon@google.com5782d712011-01-21 21:03:59 +0000473 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000474 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000475 return this->skPaint2GrPaintNoShader(skPaint,
476 false,
477 grPaint,
478 constantColor);
479 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000480 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000481 }
482
reed@google.comac10a2d2010-12-22 21:39:39 +0000483 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000484 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000485 SkShader::TileMode tileModes[2];
486 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000487 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000488 tileModes, twoPointParams);
489
bsalomon@google.com5782d712011-01-21 21:03:59 +0000490 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
491 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000492 SkShader::GradientInfo info;
493 SkColor color;
494
495 info.fColors = &color;
496 info.fColorOffsets = NULL;
497 info.fColorCount = 1;
498 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
499 SkPaint copy(skPaint);
500 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000501 // modulate the paint alpha by the shader's solid color alpha
502 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
503 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000504 return this->skPaint2GrPaintNoShader(copy,
505 false,
506 grPaint,
507 constantColor);
508 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000509 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000510 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000511 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000512 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000513 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000514 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000515 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000516 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000517 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000518 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
519 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000520 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000521 sampler->setRadial2Params(twoPointParams[0],
522 twoPointParams[1],
523 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000524 }
525
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000526 GrTexture* texture = act->set(this, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000527 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000528 SkDebugf("Couldn't convert bitmap to texture.\n");
529 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000530 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000531 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000532
533 // since our texture coords will be in local space, we wack the texture
534 // matrix to map them back into 0...1 before we load it
535 SkMatrix localM;
536 if (shader->getLocalMatrix(&localM)) {
537 SkMatrix inverse;
538 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000539 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000540 }
541 }
542 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000543 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
544 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000545 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000546 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000547 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000548 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000549 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000550
551 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000552}
553
554///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000555
bsalomon@google.com398109c2011-04-14 18:40:27 +0000556void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000557 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000558}
559
reed@google.comac10a2d2010-12-22 21:39:39 +0000560void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
561 CHECK_SHOULD_DRAW(draw);
562
bsalomon@google.com5782d712011-01-21 21:03:59 +0000563 GrPaint grPaint;
564 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000565 if (!this->skPaint2GrPaintShader(paint,
566 &act,
567 *draw.fMatrix,
568 &grPaint,
569 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000570 return;
571 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000572
573 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000574}
575
576// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000577static const GrPrimitiveType gPointMode2PrimtiveType[] = {
578 kPoints_PrimitiveType,
579 kLines_PrimitiveType,
580 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000581};
582
583void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000584 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000585 CHECK_SHOULD_DRAW(draw);
586
587 SkScalar width = paint.getStrokeWidth();
588 if (width < 0) {
589 return;
590 }
591
592 // we only handle hairlines here, else we let the SkDraw call our drawPath()
593 if (width > 0) {
594 draw.drawPoints(mode, count, pts, paint, true);
595 return;
596 }
597
bsalomon@google.com5782d712011-01-21 21:03:59 +0000598 GrPaint grPaint;
599 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000600 if (!this->skPaint2GrPaintShader(paint,
601 &act,
602 *draw.fMatrix,
603 &grPaint,
604 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000605 return;
606 }
607
bsalomon@google.com5782d712011-01-21 21:03:59 +0000608 fContext->drawVertices(grPaint,
609 gPointMode2PrimtiveType[mode],
610 count,
611 (GrPoint*)pts,
612 NULL,
613 NULL,
614 NULL,
615 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000616}
617
reed@google.comc9aa5872011-04-05 21:05:37 +0000618///////////////////////////////////////////////////////////////////////////////
619
reed@google.comac10a2d2010-12-22 21:39:39 +0000620void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
621 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000622 CHECK_SHOULD_DRAW(draw);
623
bungeman@google.com79bd8772011-07-18 15:34:08 +0000624 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000625 SkScalar width = paint.getStrokeWidth();
626
627 /*
628 We have special code for hairline strokes, miter-strokes, and fills.
629 Anything else we just call our path code.
630 */
631 bool usePath = doStroke && width > 0 &&
632 paint.getStrokeJoin() != SkPaint::kMiter_Join;
633 // another reason we might need to call drawPath...
634 if (paint.getMaskFilter()) {
635 usePath = true;
636 }
reed@google.com67db6642011-05-26 11:46:35 +0000637 // until we aa rotated rects...
638 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
639 usePath = true;
640 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000641 // small miter limit means right angles show bevel...
642 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
643 paint.getStrokeMiter() < SK_ScalarSqrt2)
644 {
645 usePath = true;
646 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000647 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000648 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
649 usePath = true;
650 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000651
652 if (usePath) {
653 SkPath path;
654 path.addRect(rect);
655 this->drawPath(draw, path, paint, NULL, true);
656 return;
657 }
658
659 GrPaint grPaint;
660 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000661 if (!this->skPaint2GrPaintShader(paint,
662 &act,
663 *draw.fMatrix,
664 &grPaint,
665 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000666 return;
667 }
reed@google.com20efde72011-05-09 17:00:02 +0000668 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000669}
670
reed@google.com69302852011-02-16 18:08:07 +0000671#include "SkMaskFilter.h"
672#include "SkBounder.h"
673
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000674static GrPathFill skToGrFillType(SkPath::FillType fillType) {
675 switch (fillType) {
676 case SkPath::kWinding_FillType:
677 return kWinding_PathFill;
678 case SkPath::kEvenOdd_FillType:
679 return kEvenOdd_PathFill;
680 case SkPath::kInverseWinding_FillType:
681 return kInverseWinding_PathFill;
682 case SkPath::kInverseEvenOdd_FillType:
683 return kInverseEvenOdd_PathFill;
684 default:
685 SkDebugf("Unsupported path fill type\n");
686 return kHairLine_PathFill;
687 }
688}
689
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000690static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000691 int halfWidth = (kernelWidth - 1) / 2;
692 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000693 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000694 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000695 float x = static_cast<float>(i - halfWidth);
696 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
697 // is dropped here, since we renormalize the kernel below.
698 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000699 sum += kernel[i];
700 }
701 // Normalize the kernel
702 float scale = 1.0f / sum;
703 for (int i = 0; i < kernelWidth; ++i)
704 kernel[i] *= scale;
705}
706
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000707static void scaleRect(SkRect* rect, float xScale, float yScale) {
708 rect->fLeft *= xScale;
709 rect->fTop *= yScale;
710 rect->fRight *= xScale;
711 rect->fBottom *= yScale;
712}
713
714static float adjustSigma(float sigma, int *scaleFactor, int *halfWidth,
715 int *kernelWidth) {
716 *scaleFactor = 1;
717 while (sigma > MAX_BLUR_SIGMA) {
718 *scaleFactor *= 2;
719 sigma *= 0.5f;
720 }
721 *halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
722 *kernelWidth = *halfWidth * 2 + 1;
723 return sigma;
724}
725
726// Apply a Gaussian blur to srcTexture by sigmaX and sigmaY, within the given
727// rect.
728// temp1 and temp2 are used for allocation of intermediate textures.
729// If temp2 is non-NULL, srcTexture will be untouched, and the return
730// value will be either temp1 or temp2.
731// If temp2 is NULL, srcTexture will be overwritten with intermediate
732// results, and the return value will either be temp1 or srcTexture.
733static GrTexture* gaussianBlur(GrContext* context, GrTexture* srcTexture,
734 GrAutoScratchTexture* temp1,
735 GrAutoScratchTexture* temp2,
736 const SkRect& rect,
737 float sigmaX, float sigmaY) {
738
739 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
740 GrClip oldClip = context->getClip();
741 GrTexture* origTexture = srcTexture;
742 GrAutoMatrix avm(context, GrMatrix::I());
743 SkIRect clearRect;
744 int scaleFactorX, halfWidthX, kernelWidthX;
745 int scaleFactorY, halfWidthY, kernelWidthY;
746 sigmaX = adjustSigma(sigmaX, &scaleFactorX, &halfWidthX, &kernelWidthX);
747 sigmaY = adjustSigma(sigmaY, &scaleFactorY, &halfWidthY, &kernelWidthY);
748
749 SkRect srcRect(rect);
750 scaleRect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
751 srcRect.roundOut();
752 scaleRect(&srcRect, scaleFactorX, scaleFactorY);
753 context->setClip(srcRect);
754
755 const GrTextureDesc desc = {
756 kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit,
757 kNone_GrAALevel,
758 srcRect.width(),
759 srcRect.height(),
bsalomon@google.com5bc34f02011-12-06 14:46:34 +0000760 kRGBA_8888_GrPixelConfig
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000761 };
762
763 temp1->set(context, desc);
764 if (temp2) temp2->set(context, desc);
765
766 GrTexture* dstTexture = temp1->texture();
767 GrPaint paint;
768 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000769 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000770
771 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000772 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
773 srcTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000774 context->setRenderTarget(dstTexture->asRenderTarget());
775 SkRect dstRect(srcRect);
776 scaleRect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
777 i < scaleFactorY ? 0.5f : 1.0f);
778 paint.setTexture(0, srcTexture);
779 context->drawRectToRect(paint, dstRect, srcRect);
780 srcRect = dstRect;
781 SkTSwap(srcTexture, dstTexture);
782 // If temp2 is non-NULL, don't render back to origTexture
783 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
784 }
785
786 if (sigmaX > 0.0f) {
787 SkAutoTMalloc<float> kernelStorageX(kernelWidthX);
788 float* kernelX = kernelStorageX.get();
789 buildKernel(sigmaX, kernelX, kernelWidthX);
790
791 if (scaleFactorX > 1) {
792 // Clear out a halfWidth to the right of the srcRect to prevent the
793 // X convolution from reading garbage.
794 clearRect = SkIRect::MakeXYWH(
795 srcRect.fRight, srcRect.fTop, halfWidthX, srcRect.height());
796 context->clear(&clearRect, 0x0);
797 }
798
799 context->setRenderTarget(dstTexture->asRenderTarget());
800 context->convolveInX(srcTexture, srcRect, kernelX, kernelWidthX);
801 SkTSwap(srcTexture, dstTexture);
802 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
803 }
804
805 if (sigmaY > 0.0f) {
806 SkAutoTMalloc<float> kernelStorageY(kernelWidthY);
807 float* kernelY = kernelStorageY.get();
808 buildKernel(sigmaY, kernelY, kernelWidthY);
809
810 if (scaleFactorY > 1 || sigmaX > 0.0f) {
811 // Clear out a halfWidth below the srcRect to prevent the Y
812 // convolution from reading garbage.
813 clearRect = SkIRect::MakeXYWH(
814 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidthY);
815 context->clear(&clearRect, 0x0);
816 }
817
818 context->setRenderTarget(dstTexture->asRenderTarget());
819 context->convolveInY(srcTexture, srcRect, kernelY, kernelWidthY);
820 SkTSwap(srcTexture, dstTexture);
821 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
822 }
823
824 if (scaleFactorX > 1 || scaleFactorY > 1) {
825 // Clear one pixel to the right and below, to accommodate bilinear
826 // upsampling.
827 clearRect = SkIRect::MakeXYWH(
828 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
829 context->clear(&clearRect, 0x0);
830 clearRect = SkIRect::MakeXYWH(
831 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
832 context->clear(&clearRect, 0x0);
833 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000834 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000835 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
836 srcTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000837 context->setRenderTarget(dstTexture->asRenderTarget());
838 paint.setTexture(0, srcTexture);
839 SkRect dstRect(srcRect);
840 scaleRect(&dstRect, scaleFactorX, scaleFactorY);
841 context->drawRectToRect(paint, dstRect, srcRect);
842 srcRect = dstRect;
843 SkTSwap(srcTexture, dstTexture);
844 }
845 context->setRenderTarget(oldRenderTarget);
846 context->setClip(oldClip);
847 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000848}
849
850static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
851 SkMaskFilter* filter, const SkMatrix& matrix,
852 const SkRegion& clip, SkBounder* bounder,
853 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000854#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000855 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000856#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000857 SkMaskFilter::BlurInfo info;
858 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000859 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000860 return false;
861 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000862 SkScalar radius = info.fIgnoreTransform ? info.fRadius
863 : matrix.mapRadius(info.fRadius);
864 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000865 if (radius <= 0) {
866 return false;
867 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000868 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000869 float sigma3 = sigma * 3.0f;
870
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871 SkRect srcRect = path.getBounds();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000872 SkRect clipRect;
873 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000874
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000875 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
876 srcRect.inset(-sigma3, -sigma3);
877 clipRect.inset(-sigma3, -sigma3);
878 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000879 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000880 SkIRect finalIRect;
881 finalRect.roundOut(&finalIRect);
882 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000883 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000884 }
885 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000886 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000887 }
888 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000889 srcRect.offset(offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000890 const GrTextureDesc desc = {
891 kRenderTarget_GrTextureFlagBit,
892 kNone_GrAALevel,
893 srcRect.width(),
894 srcRect.height(),
895 // We actually only need A8, but it often isn't supported as a
896 // render target
bsalomon@google.com5bc34f02011-12-06 14:46:34 +0000897 kRGBA_8888_PM_GrPixelConfig
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000898 };
899
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000900 GrAutoScratchTexture pathEntry(context, desc);
901 GrTexture* pathTexture = pathEntry.texture();
902 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000903 return false;
904 }
905 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000906 // Once this code moves into GrContext, this should be changed to use
907 // an AutoClipRestore.
908 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000909 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000910 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000911 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000912 GrPaint tempPaint;
913 tempPaint.reset();
914
915 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000916 tempPaint.fAntiAlias = grp->fAntiAlias;
917 if (tempPaint.fAntiAlias) {
918 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
919 // blend coeff of zero requires dual source blending support in order
920 // to properly blend partially covered pixels. This means the AA
921 // code path may not be taken. So we use a dst blend coeff of ISA. We
922 // could special case AA draws to a dst surface with known alpha=0 to
923 // use a zero dst coeff when dual source blending isn't available.
924 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
925 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
926 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000927 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000928 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000929
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000930 GrAutoScratchTexture temp1, temp2;
931 // If we're doing a normal blur, we can clobber the pathTexture in the
932 // gaussianBlur. Otherwise, we need to save it for later compositing.
933 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
934 GrTexture* blurTexture = gaussianBlur(context, pathTexture,
935 &temp1, isNormalBlur ? NULL : &temp2,
936 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000937
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000938 if (!isNormalBlur) {
939 GrPaint paint;
940 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000941 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000942 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
943 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000944 // Blend pathTexture over blurTexture.
945 context->setRenderTarget(blurTexture->asRenderTarget());
946 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000947 if (SkMaskFilter::kInner_BlurType == blurType) {
948 // inner: dst = dst * src
949 paint.fSrcBlendCoeff = kDC_BlendCoeff;
950 paint.fDstBlendCoeff = kZero_BlendCoeff;
951 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
952 // solid: dst = src + dst - src * dst
953 // = (1 - dst) * src + 1 * dst
954 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
955 paint.fDstBlendCoeff = kOne_BlendCoeff;
956 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
957 // outer: dst = dst * (1 - src)
958 // = 0 * src + (1 - src) * dst
959 paint.fSrcBlendCoeff = kZero_BlendCoeff;
960 paint.fDstBlendCoeff = kISC_BlendCoeff;
961 }
962 context->drawRect(paint, srcRect);
963 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000964 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000965 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000966
967 if (grp->hasTextureOrMask()) {
968 GrMatrix inverse;
969 if (!matrix.invert(&inverse)) {
970 return false;
971 }
972 grp->preConcatActiveSamplerMatrices(inverse);
973 }
974
975 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
976 // we assume the last mask index is available for use
977 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000978 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000979 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000980
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000981 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
982 -finalRect.fTop);
983 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
984 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000985 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000986 return true;
987}
988
reed@google.com69302852011-02-16 18:08:07 +0000989static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
990 SkMaskFilter* filter, const SkMatrix& matrix,
991 const SkRegion& clip, SkBounder* bounder,
992 GrPaint* grp) {
993 SkMask srcM, dstM;
994
995 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
996 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
997 return false;
998 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000999 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001000
1001 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
1002 return false;
1003 }
1004 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +00001005 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001006
1007 if (clip.quickReject(dstM.fBounds)) {
1008 return false;
1009 }
1010 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1011 return false;
1012 }
1013
1014 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1015 // the current clip (and identity matrix) and grpaint settings
1016
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001017 // used to compute inverse view, if necessary
1018 GrMatrix ivm = context->getMatrix();
1019
reed@google.com0c219b62011-02-16 21:31:18 +00001020 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001021
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001022 const GrTextureDesc desc = {
1023 kNone_GrTextureFlags,
1024 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +00001025 dstM.fBounds.width(),
1026 dstM.fBounds.height(),
bsalomon@google.com5bc34f02011-12-06 14:46:34 +00001027 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +00001028 };
1029
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001030 GrAutoScratchTexture ast(context, desc);
1031 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001032
reed@google.com69302852011-02-16 18:08:07 +00001033 if (NULL == texture) {
1034 return false;
1035 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001036 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001037 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001038
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001039 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
1040 grp->preConcatActiveSamplerMatrices(ivm);
1041 }
1042
1043 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1044 // we assume the last mask index is available for use
1045 GrAssert(NULL == grp->getMask(MASK_IDX));
1046 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +00001047 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +00001048
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001049 GrRect d;
1050 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001051 GrIntToScalar(dstM.fBounds.fTop),
1052 GrIntToScalar(dstM.fBounds.fRight),
1053 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001054
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001055 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1056 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1057 -dstM.fBounds.fTop*SK_Scalar1);
1058 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001059 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001060 return true;
1061}
reed@google.com69302852011-02-16 18:08:07 +00001062
reed@google.com0c219b62011-02-16 21:31:18 +00001063void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comfe626382011-09-21 13:50:35 +00001064 const SkPaint& origPaint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001065 bool pathIsMutable) {
1066 CHECK_SHOULD_DRAW(draw);
1067
reed@google.comfe626382011-09-21 13:50:35 +00001068 bool doFill = true;
1069 SkTLazy<SkPaint> lazyPaint;
1070 const SkPaint* paint = &origPaint;
1071
1072 // can we cheat, and threat a thin stroke as a hairline (w/ modulated alpha)
1073 // if we can, we draw lots faster (raster device does this same test)
1074 {
1075 SkAlpha newAlpha;
1076 if (SkDrawTreatAsHairline(*paint, *draw.fMatrix, &newAlpha)) {
1077 lazyPaint.set(*paint);
1078 lazyPaint.get()->setAlpha(newAlpha);
1079 lazyPaint.get()->setStrokeWidth(0);
1080 paint = lazyPaint.get();
1081 doFill = false;
1082 }
1083 }
1084 // must reference paint from here down, and not origPaint
1085 // since we may have change the paint (using lazyPaint for storage)
1086
bsalomon@google.com5782d712011-01-21 21:03:59 +00001087 GrPaint grPaint;
1088 SkAutoCachedTexture act;
reed@google.comfe626382011-09-21 13:50:35 +00001089 if (!this->skPaint2GrPaintShader(*paint,
Scroggod757df22011-05-16 13:11:16 +00001090 &act,
1091 *draw.fMatrix,
1092 &grPaint,
1093 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001094 return;
1095 }
1096
reed@google.comfe626382011-09-21 13:50:35 +00001097 // If we have a prematrix, apply it to the path, optimizing for the case
1098 // where the original path can in fact be modified in place (even though
1099 // its parameter type is const).
1100 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1101 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001102
1103 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001104 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001105
reed@google.come3445642011-02-16 23:20:39 +00001106 if (!pathIsMutable) {
1107 result = &tmpPath;
1108 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001109 }
reed@google.come3445642011-02-16 23:20:39 +00001110 // should I push prePathMatrix on our MV stack temporarily, instead
1111 // of applying it here? See SkDraw.cpp
1112 pathPtr->transform(*prePathMatrix, result);
1113 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001114 }
reed@google.com0c219b62011-02-16 21:31:18 +00001115 // at this point we're done with prePathMatrix
1116 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001117
reed@google.comfe626382011-09-21 13:50:35 +00001118 if (doFill && (paint->getPathEffect() ||
1119 paint->getStyle() != SkPaint::kFill_Style)) {
1120 // it is safe to use tmpPath here, even if we already used it for the
1121 // prepathmatrix, since getFillPath can take the same object for its
1122 // input and output safely.
1123 doFill = paint->getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001124 pathPtr = &tmpPath;
1125 }
1126
reed@google.comfe626382011-09-21 13:50:35 +00001127 if (paint->getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001128 // avoid possibly allocating a new path in transform if we can
1129 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1130
1131 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001132 pathPtr->transform(*draw.fMatrix, devPathPtr);
reed@google.comfe626382011-09-21 13:50:35 +00001133 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001134 *draw.fMatrix, *draw.fClip, draw.fBounder,
1135 &grPaint)) {
reed@google.comfe626382011-09-21 13:50:35 +00001136 drawWithMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001137 *draw.fMatrix, *draw.fClip, draw.fBounder,
1138 &grPaint);
1139 }
reed@google.com69302852011-02-16 18:08:07 +00001140 return;
1141 }
reed@google.com69302852011-02-16 18:08:07 +00001142
bsalomon@google.comffca4002011-02-22 20:34:01 +00001143 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001144
reed@google.com0c219b62011-02-16 21:31:18 +00001145 if (doFill) {
1146 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001147 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001148 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001149 break;
1150 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001151 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001152 break;
1153 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001154 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001155 break;
1156 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001157 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001158 break;
1159 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001160 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001161 return;
1162 }
1163 }
1164
reed@google.com07f3ee12011-05-16 17:21:57 +00001165 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001166}
1167
bsalomon@google.comfb309512011-11-30 14:13:48 +00001168namespace {
1169
1170inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1171 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1172 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1173 return tilesX * tilesY;
1174}
1175
1176inline int determine_tile_size(const SkBitmap& bitmap,
1177 const SkIRect* srcRectPtr,
1178 int maxTextureSize) {
1179 static const int kSmallTileSize = 1 << 10;
1180 if (maxTextureSize <= kSmallTileSize) {
1181 return maxTextureSize;
1182 }
1183
1184 size_t maxTexTotalTileSize;
1185 size_t smallTotalTileSize;
1186
1187 if (NULL == srcRectPtr) {
1188 int w = bitmap.width();
1189 int h = bitmap.height();
1190 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1191 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1192 } else {
1193 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1194 srcRectPtr->fTop,
1195 srcRectPtr->fRight,
1196 srcRectPtr->fBottom,
1197 maxTextureSize);
1198 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1199 srcRectPtr->fTop,
1200 srcRectPtr->fRight,
1201 srcRectPtr->fBottom,
1202 kSmallTileSize);
1203 }
1204 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1205 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1206
1207 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1208 return kSmallTileSize;
1209 } else {
1210 return maxTextureSize;
1211 }
1212}
1213}
1214
1215bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1216 const GrSamplerState& sampler,
1217 const SkIRect* srcRectPtr,
1218 int* tileSize) const {
1219 SkASSERT(NULL != tileSize);
1220
1221 // if bitmap is explictly texture backed then just use the texture
1222 if (NULL != bitmap.getTexture()) {
1223 return false;
1224 }
1225 // if it's larger than the max texture size, then we have no choice but
1226 // tiling
1227 const int maxTextureSize = fContext->getMaxTextureSize();
1228 if (bitmap.width() > maxTextureSize ||
1229 bitmap.height() > maxTextureSize) {
1230 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1231 return true;
1232 }
1233 // if we are going to have to draw the whole thing, then don't tile
1234 if (NULL == srcRectPtr) {
1235 return false;
1236 }
1237 // if the entire texture is already in our cache then no reason to tile it
1238 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1239 return false;
1240 }
1241
1242 // At this point we know we could do the draw by uploading the entire bitmap
1243 // as a texture. However, if the texture would be large compared to the
1244 // cache size and we don't require most of it for this draw then tile to
1245 // reduce the amount of upload and cache spill.
1246
1247 // assumption here is that sw bitmap size is a good proxy for its size as
1248 // a texture
1249 size_t bmpSize = bitmap.getSize();
1250 size_t cacheSize;
1251 fContext->getTextureCacheLimits(NULL, &cacheSize);
1252 if (bmpSize < cacheSize / 2) {
1253 return false;
1254 }
1255
1256 SkFixed fracUsed =
1257 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1258 (srcRectPtr->height() << 16) / bitmap.height());
1259 if (fracUsed <= SK_FixedHalf) {
1260 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1261 return true;
1262 } else {
1263 return false;
1264 }
1265}
1266
reed@google.comac10a2d2010-12-22 21:39:39 +00001267void SkGpuDevice::drawBitmap(const SkDraw& draw,
1268 const SkBitmap& bitmap,
1269 const SkIRect* srcRectPtr,
1270 const SkMatrix& m,
1271 const SkPaint& paint) {
1272 CHECK_SHOULD_DRAW(draw);
1273
1274 SkIRect srcRect;
1275 if (NULL == srcRectPtr) {
1276 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1277 } else {
1278 srcRect = *srcRectPtr;
1279 }
1280
junov@google.comd935cfb2011-06-27 20:48:23 +00001281 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001282 // Convert the bitmap to a shader so that the rect can be drawn
1283 // through drawRect, which supports mask filters.
1284 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001285 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001286 if (srcRectPtr) {
1287 if (!bitmap.extractSubset(&tmp, srcRect)) {
1288 return; // extraction failed
1289 }
1290 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001291 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001292 }
1293 SkPaint paintWithTexture(paint);
1294 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1295 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001296 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001297 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001298
junov@google.com1d329782011-07-28 20:10:09 +00001299 // Transform 'm' needs to be concatenated to the draw matrix,
1300 // rather than transforming the primitive directly, so that 'm' will
1301 // also affect the behavior of the mask filter.
1302 SkMatrix drawMatrix;
1303 drawMatrix.setConcat(*draw.fMatrix, m);
1304 SkDraw transformedDraw(draw);
1305 transformedDraw.fMatrix = &drawMatrix;
1306
1307 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1308
junov@google.comd935cfb2011-06-27 20:48:23 +00001309 return;
1310 }
1311
bsalomon@google.com5782d712011-01-21 21:03:59 +00001312 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001313 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001314 return;
1315 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001316 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001317 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001318 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001319 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001320 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001321 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001322
bsalomon@google.comfb309512011-11-30 14:13:48 +00001323 int tileSize;
1324 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1325 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001326 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 return;
1328 }
1329
1330 // undo the translate done by SkCanvas
1331 int DX = SkMax32(0, srcRect.fLeft);
1332 int DY = SkMax32(0, srcRect.fTop);
1333 // compute clip bounds in local coordinates
1334 SkIRect clipRect;
1335 {
1336 SkRect r;
1337 r.set(draw.fClip->getBounds());
1338 SkMatrix matrix, inverse;
1339 matrix.setConcat(*draw.fMatrix, m);
1340 if (!matrix.invert(&inverse)) {
1341 return;
1342 }
1343 inverse.mapRect(&r);
1344 r.roundOut(&clipRect);
1345 // apply the canvas' translate to our local clip
1346 clipRect.offset(DX, DY);
1347 }
1348
bsalomon@google.comfb309512011-11-30 14:13:48 +00001349 int nx = bitmap.width() / tileSize;
1350 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001351 for (int x = 0; x <= nx; x++) {
1352 for (int y = 0; y <= ny; y++) {
1353 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001354 tileR.set(x * tileSize, y * tileSize,
1355 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001356 if (!SkIRect::Intersects(tileR, clipRect)) {
1357 continue;
1358 }
1359
1360 SkIRect srcR = tileR;
1361 if (!srcR.intersect(srcRect)) {
1362 continue;
1363 }
1364
1365 SkBitmap tmpB;
1366 if (bitmap.extractSubset(&tmpB, tileR)) {
1367 // now offset it to make it "local" to our tmp bitmap
1368 srcR.offset(-tileR.fLeft, -tileR.fTop);
1369
1370 SkMatrix tmpM(m);
1371 {
1372 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1373 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1374 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1375 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001376 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001377 }
1378 }
1379 }
1380}
1381
1382/*
1383 * This is called by drawBitmap(), which has to handle images that may be too
1384 * large to be represented by a single texture.
1385 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001386 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1387 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001388 */
1389void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1390 const SkBitmap& bitmap,
1391 const SkIRect& srcRect,
1392 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001393 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001394 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1395 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001396
reed@google.com9c49bc32011-07-07 13:42:37 +00001397 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001398 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001399 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001400 return;
1401 }
1402
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001403 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001404
1405 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1406 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1407 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001408 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001409
1410 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001411 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001412 if (NULL == texture) {
1413 return;
1414 }
1415
bsalomon@google.com452943d2011-10-31 17:37:14 +00001416 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001417
reed@google.com20efde72011-05-09 17:00:02 +00001418 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1419 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001420 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001421 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1422 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1423 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001424 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001425
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001426 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001427 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001428 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001429 // If drawing a subrect of the bitmap and filtering is enabled,
1430 // use a constrained texture domain to avoid color bleeding
1431 GrScalar left, top, right, bottom;
1432 if (srcRect.width() > 1) {
1433 GrScalar border = GR_ScalarHalf / bitmap.width();
1434 left = paintRect.left() + border;
1435 right = paintRect.right() - border;
1436 } else {
1437 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1438 }
1439 if (srcRect.height() > 1) {
1440 GrScalar border = GR_ScalarHalf / bitmap.height();
1441 top = paintRect.top() + border;
1442 bottom = paintRect.bottom() - border;
1443 } else {
1444 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1445 }
1446 GrRect textureDomain;
1447 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001448 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001449 }
1450
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001451 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001452}
1453
1454void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1455 int left, int top, const SkPaint& paint) {
1456 CHECK_SHOULD_DRAW(draw);
1457
1458 SkAutoLockPixels alp(bitmap);
1459 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1460 return;
1461 }
1462
reed@google.com76dd2772012-01-05 21:15:07 +00001463 int w = bitmap.width();
1464 int h = bitmap.height();
1465
bsalomon@google.com5782d712011-01-21 21:03:59 +00001466 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001467 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001468 return;
1469 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001470
bsalomon@google.com5782d712011-01-21 21:03:59 +00001471 GrAutoMatrix avm(fContext, GrMatrix::I());
1472
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001473 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001474
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001475 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001476 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001477 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001478
reed@google.com76dd2772012-01-05 21:15:07 +00001479 SkImageFilter* imageFilter = paint.getImageFilter();
1480 SkSize blurSize;
1481 if (NULL != imageFilter && imageFilter->asABlur(&blurSize)) {
1482 GrAutoScratchTexture temp1, temp2;
1483 GrTexture* blurTexture = gaussianBlur(fContext,
1484 texture, &temp1, &temp2,
1485 GrRect::MakeWH(w, h),
1486 blurSize.width(),
1487 blurSize.height());
1488 texture = blurTexture;
1489 grPaint.setTexture(kBitmapTextureIdx, texture);
reed@google.com76dd2772012-01-05 21:15:07 +00001490 } else {
1491 grPaint.setTexture(kBitmapTextureIdx, texture);
1492 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001493
bsalomon@google.com5782d712011-01-21 21:03:59 +00001494 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001495 GrRect::MakeXYWH(GrIntToScalar(left),
1496 GrIntToScalar(top),
1497 GrIntToScalar(w),
1498 GrIntToScalar(h)),
1499 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1500 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001501}
1502
1503void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1504 int x, int y, const SkPaint& paint) {
1505 CHECK_SHOULD_DRAW(draw);
1506
bsalomon@google.com5782d712011-01-21 21:03:59 +00001507 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001508 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001509 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001510 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001511 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001512
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001513 GrTexture* devTex = grPaint.getTexture(0);
1514 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001515
1516 const SkBitmap& bm = dev->accessBitmap(false);
1517 int w = bm.width();
1518 int h = bm.height();
1519
1520 GrAutoMatrix avm(fContext, GrMatrix::I());
1521
bsalomon@google.com97912912011-12-06 16:30:36 +00001522 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001523
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001524 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1525 GrIntToScalar(y),
1526 GrIntToScalar(w),
1527 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001528
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001529 // The device being drawn may not fill up its texture (saveLayer uses
1530 // the approximate ).
1531 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1532 GR_Scalar1 * h / devTex->height());
1533
1534 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001535}
1536
reed@google.com76dd2772012-01-05 21:15:07 +00001537bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1538 const SkMatrix& ctm,
1539 SkBitmap* result, SkIPoint* offset) {
1540 SkSize size;
1541 if (!filter->asABlur(&size)) {
1542 return false;
1543 }
1544 SkDevice* dev = this->createCompatibleDevice(SkBitmap::kARGB_8888_Config,
1545 src.width(),
1546 src.height(),
1547 false);
1548 if (NULL == dev) {
1549 return false;
1550 }
1551 SkAutoUnref aur(dev);
1552 SkCanvas canvas(dev);
1553 SkPaint paint;
1554 paint.setImageFilter(filter);
1555 canvas.drawSprite(src, 0, 0, &paint);
1556 *result = dev->accessBitmap(false);
1557 return true;
1558}
1559
reed@google.comac10a2d2010-12-22 21:39:39 +00001560///////////////////////////////////////////////////////////////////////////////
1561
1562// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001563static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1564 kTriangles_PrimitiveType,
1565 kTriangleStrip_PrimitiveType,
1566 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001567};
1568
1569void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1570 int vertexCount, const SkPoint vertices[],
1571 const SkPoint texs[], const SkColor colors[],
1572 SkXfermode* xmode,
1573 const uint16_t indices[], int indexCount,
1574 const SkPaint& paint) {
1575 CHECK_SHOULD_DRAW(draw);
1576
bsalomon@google.com5782d712011-01-21 21:03:59 +00001577 GrPaint grPaint;
1578 SkAutoCachedTexture act;
1579 // we ignore the shader if texs is null.
1580 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001581 if (!this->skPaint2GrPaintNoShader(paint,
1582 false,
1583 &grPaint,
1584 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001585 return;
1586 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001587 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001588 if (!this->skPaint2GrPaintShader(paint, &act,
1589 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001590 &grPaint,
1591 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001592 return;
1593 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001594 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001595
1596 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001597 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001598 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1599#if 0
1600 return
1601#endif
1602 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001603 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001604
bsalomon@google.com498776a2011-08-16 19:20:44 +00001605 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1606 if (NULL != colors) {
1607 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001608 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001609 for (int i = 0; i < vertexCount; ++i) {
1610 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1611 }
1612 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001613 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001614 fContext->drawVertices(grPaint,
1615 gVertexMode2PrimitiveType[vmode],
1616 vertexCount,
1617 (GrPoint*) vertices,
1618 (GrPoint*) texs,
1619 colors,
1620 indices,
1621 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001622}
1623
1624///////////////////////////////////////////////////////////////////////////////
1625
1626static void GlyphCacheAuxProc(void* data) {
1627 delete (GrFontScaler*)data;
1628}
1629
1630static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1631 void* auxData;
1632 GrFontScaler* scaler = NULL;
1633 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1634 scaler = (GrFontScaler*)auxData;
1635 }
1636 if (NULL == scaler) {
1637 scaler = new SkGrFontScaler(cache);
1638 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1639 }
1640 return scaler;
1641}
1642
1643static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1644 SkFixed fx, SkFixed fy,
1645 const SkGlyph& glyph) {
1646 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1647
bungeman@google.com15865a72012-01-11 16:28:04 +00001648 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001649
1650 if (NULL == procs->fFontScaler) {
1651 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1652 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001653
bungeman@google.com15865a72012-01-11 16:28:04 +00001654 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1655 glyph.getSubXFixed(),
1656 glyph.getSubYFixed()),
1657 SkFixedFloorToFixed(fx),
1658 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001659 procs->fFontScaler);
1660}
1661
bsalomon@google.com5782d712011-01-21 21:03:59 +00001662SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001663
1664 // deferred allocation
1665 if (NULL == fDrawProcs) {
1666 fDrawProcs = new GrSkDrawProcs;
1667 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1668 fDrawProcs->fContext = fContext;
1669 }
1670
1671 // init our (and GL's) state
1672 fDrawProcs->fTextContext = context;
1673 fDrawProcs->fFontScaler = NULL;
1674 return fDrawProcs;
1675}
1676
1677void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1678 size_t byteLength, SkScalar x, SkScalar y,
1679 const SkPaint& paint) {
1680 CHECK_SHOULD_DRAW(draw);
1681
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001682 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001683 // this guy will just call our drawPath()
1684 draw.drawText((const char*)text, byteLength, x, y, paint);
1685 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001686 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001687
1688 GrPaint grPaint;
1689 SkAutoCachedTexture act;
1690
Scroggod757df22011-05-16 13:11:16 +00001691 if (!this->skPaint2GrPaintShader(paint,
1692 &act,
1693 *draw.fMatrix,
1694 &grPaint,
1695 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001696 return;
1697 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001698 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001699 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001700 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1701 }
1702}
1703
1704void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1705 size_t byteLength, const SkScalar pos[],
1706 SkScalar constY, int scalarsPerPos,
1707 const SkPaint& paint) {
1708 CHECK_SHOULD_DRAW(draw);
1709
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001710 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001711 // this guy will just call our drawPath()
1712 draw.drawPosText((const char*)text, byteLength, pos, constY,
1713 scalarsPerPos, paint);
1714 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001715 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001716
1717 GrPaint grPaint;
1718 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001719 if (!this->skPaint2GrPaintShader(paint,
1720 &act,
1721 *draw.fMatrix,
1722 &grPaint,
1723 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001724 return;
1725 }
1726
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001727 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001728 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001729 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1730 scalarsPerPos, paint);
1731 }
1732}
1733
1734void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1735 size_t len, const SkPath& path,
1736 const SkMatrix* m, const SkPaint& paint) {
1737 CHECK_SHOULD_DRAW(draw);
1738
1739 SkASSERT(draw.fDevice == this);
1740 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1741}
1742
1743///////////////////////////////////////////////////////////////////////////////
1744
reed@google.comf67e4cf2011-03-15 20:56:58 +00001745bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1746 if (!paint.isLCDRenderText()) {
1747 // we're cool with the paint as is
1748 return false;
1749 }
1750
1751 if (paint.getShader() ||
1752 paint.getXfermode() || // unless its srcover
1753 paint.getMaskFilter() ||
1754 paint.getRasterizer() ||
1755 paint.getColorFilter() ||
1756 paint.getPathEffect() ||
1757 paint.isFakeBoldText() ||
1758 paint.getStyle() != SkPaint::kFill_Style) {
1759 // turn off lcd
1760 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1761 flags->fHinting = paint.getHinting();
1762 return true;
1763 }
1764 // we're cool with the paint as is
1765 return false;
1766}
1767
reed@google.com75d939b2011-12-07 15:07:23 +00001768void SkGpuDevice::flush() {
1769 fContext->flush(false);
1770}
1771
reed@google.comf67e4cf2011-03-15 20:56:58 +00001772///////////////////////////////////////////////////////////////////////////////
1773
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001774SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001775 const GrSamplerState* sampler,
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001776 TexType type) {
1777 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001778 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001779
bsalomon@google.come97f0852011-06-17 13:10:25 +00001780 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001781 const GrTextureDesc desc = {
1782 kRenderTarget_GrTextureFlagBit,
1783 kNone_GrAALevel,
1784 bitmap.width(),
1785 bitmap.height(),
bsalomon@google.com5bc34f02011-12-06 14:46:34 +00001786 SkGr::Bitmap2PixelConfig(bitmap)
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001787 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001788 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001789 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001790 // we know layers will only be drawn through drawDevice.
1791 // drawDevice has been made to work with content embedded in a
1792 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001793 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001794 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001795 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001796 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001797 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001798 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001799 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001800 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001801 GrContext::TextureKey key = bitmap.getGenerationID();
1802 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001803
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001804 entry = ctx->findAndLockTexture(key, bitmap.width(),
1805 bitmap.height(), sampler);
1806 if (NULL == entry.texture()) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001807 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001808 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001809 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001810 } else {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001811 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY,
1812 sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001813 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001814 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001815 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1816 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001817 }
1818 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001819 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001820}
1821
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001822void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1823 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001824}
1825
bsalomon@google.comfb309512011-11-30 14:13:48 +00001826bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1827 const GrSamplerState& sampler) const {
1828 GrContext::TextureKey key = bitmap.getGenerationID();
1829 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
1830 return this->context()->isTextureInCache(key, bitmap.width(),
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001831 bitmap.height(), &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001832
1833}
1834
1835
bsalomon@google.come97f0852011-06-17 13:10:25 +00001836SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1837 int width, int height,
1838 bool isOpaque,
1839 Usage usage) {
1840 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1841 width, height, usage));
1842}
1843