blob: df2ac2283aa19f8e5596487e19c331ab01eb5409 [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.com97912912011-12-06 16:30:36 +0000198 fCache = this->lockCachedTexture(bm, GrSamplerState::ClampNearest(), 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
416 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
417 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
418
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 }
428 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
429 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
430
431 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;
444 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000445 if (!constantColor) {
446 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
447 grPaint->fColorFilterXfermode = filterMode;
448 return true;
449 }
450 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
451 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000452 }
Scroggod757df22011-05-16 13:11:16 +0000453 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000454 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000455}
456
bsalomon@google.com5782d712011-01-21 21:03:59 +0000457bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
458 SkAutoCachedTexture* act,
459 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000460 GrPaint* grPaint,
461 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000462
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000464
bsalomon@google.com5782d712011-01-21 21:03:59 +0000465 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000466 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000467 return this->skPaint2GrPaintNoShader(skPaint,
468 false,
469 grPaint,
470 constantColor);
471 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000472 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000473 }
474
reed@google.comac10a2d2010-12-22 21:39:39 +0000475 SkBitmap bitmap;
476 SkMatrix matrix;
477 SkShader::TileMode tileModes[2];
478 SkScalar twoPointParams[3];
479 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
480 tileModes, twoPointParams);
481
bsalomon@google.com5782d712011-01-21 21:03:59 +0000482 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
483 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000484 SkShader::GradientInfo info;
485 SkColor color;
486
487 info.fColors = &color;
488 info.fColorOffsets = NULL;
489 info.fColorCount = 1;
490 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
491 SkPaint copy(skPaint);
492 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000493 // modulate the paint alpha by the shader's solid color alpha
494 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
495 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000496 return this->skPaint2GrPaintNoShader(copy,
497 false,
498 grPaint,
499 constantColor);
500 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000501 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000502 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000503 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000504 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000505 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000506 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000507 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000508 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000509 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000510 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
511 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000512 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000513 sampler->setRadial2Params(twoPointParams[0],
514 twoPointParams[1],
515 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000516 }
517
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000518 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000519 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000520 SkDebugf("Couldn't convert bitmap to texture.\n");
521 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000522 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000523 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000524
525 // since our texture coords will be in local space, we wack the texture
526 // matrix to map them back into 0...1 before we load it
527 SkMatrix localM;
528 if (shader->getLocalMatrix(&localM)) {
529 SkMatrix inverse;
530 if (localM.invert(&inverse)) {
531 matrix.preConcat(inverse);
532 }
533 }
534 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000535 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
536 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000537 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000538 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000539 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000540 matrix.postScale(s, s);
541 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000542 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000543
544 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000545}
546
547///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000548
bsalomon@google.com398109c2011-04-14 18:40:27 +0000549void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000550 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000551}
552
reed@google.comac10a2d2010-12-22 21:39:39 +0000553void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
554 CHECK_SHOULD_DRAW(draw);
555
bsalomon@google.com5782d712011-01-21 21:03:59 +0000556 GrPaint grPaint;
557 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000558 if (!this->skPaint2GrPaintShader(paint,
559 &act,
560 *draw.fMatrix,
561 &grPaint,
562 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000563 return;
564 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000565
566 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000567}
568
569// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000570static const GrPrimitiveType gPointMode2PrimtiveType[] = {
571 kPoints_PrimitiveType,
572 kLines_PrimitiveType,
573 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000574};
575
576void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000577 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000578 CHECK_SHOULD_DRAW(draw);
579
580 SkScalar width = paint.getStrokeWidth();
581 if (width < 0) {
582 return;
583 }
584
585 // we only handle hairlines here, else we let the SkDraw call our drawPath()
586 if (width > 0) {
587 draw.drawPoints(mode, count, pts, paint, true);
588 return;
589 }
590
bsalomon@google.com5782d712011-01-21 21:03:59 +0000591 GrPaint grPaint;
592 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000593 if (!this->skPaint2GrPaintShader(paint,
594 &act,
595 *draw.fMatrix,
596 &grPaint,
597 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000598 return;
599 }
600
bsalomon@google.com5782d712011-01-21 21:03:59 +0000601 fContext->drawVertices(grPaint,
602 gPointMode2PrimtiveType[mode],
603 count,
604 (GrPoint*)pts,
605 NULL,
606 NULL,
607 NULL,
608 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000609}
610
reed@google.comc9aa5872011-04-05 21:05:37 +0000611///////////////////////////////////////////////////////////////////////////////
612
reed@google.comac10a2d2010-12-22 21:39:39 +0000613void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
614 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000615 CHECK_SHOULD_DRAW(draw);
616
bungeman@google.com79bd8772011-07-18 15:34:08 +0000617 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000618 SkScalar width = paint.getStrokeWidth();
619
620 /*
621 We have special code for hairline strokes, miter-strokes, and fills.
622 Anything else we just call our path code.
623 */
624 bool usePath = doStroke && width > 0 &&
625 paint.getStrokeJoin() != SkPaint::kMiter_Join;
626 // another reason we might need to call drawPath...
627 if (paint.getMaskFilter()) {
628 usePath = true;
629 }
reed@google.com67db6642011-05-26 11:46:35 +0000630 // until we aa rotated rects...
631 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
632 usePath = true;
633 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000634 // small miter limit means right angles show bevel...
635 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
636 paint.getStrokeMiter() < SK_ScalarSqrt2)
637 {
638 usePath = true;
639 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000640 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000641 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
642 usePath = true;
643 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000644
645 if (usePath) {
646 SkPath path;
647 path.addRect(rect);
648 this->drawPath(draw, path, paint, NULL, true);
649 return;
650 }
651
652 GrPaint grPaint;
653 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000654 if (!this->skPaint2GrPaintShader(paint,
655 &act,
656 *draw.fMatrix,
657 &grPaint,
658 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000659 return;
660 }
reed@google.com20efde72011-05-09 17:00:02 +0000661 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000662}
663
reed@google.com69302852011-02-16 18:08:07 +0000664#include "SkMaskFilter.h"
665#include "SkBounder.h"
666
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000667static GrPathFill skToGrFillType(SkPath::FillType fillType) {
668 switch (fillType) {
669 case SkPath::kWinding_FillType:
670 return kWinding_PathFill;
671 case SkPath::kEvenOdd_FillType:
672 return kEvenOdd_PathFill;
673 case SkPath::kInverseWinding_FillType:
674 return kInverseWinding_PathFill;
675 case SkPath::kInverseEvenOdd_FillType:
676 return kInverseEvenOdd_PathFill;
677 default:
678 SkDebugf("Unsupported path fill type\n");
679 return kHairLine_PathFill;
680 }
681}
682
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000683static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000684 int halfWidth = (kernelWidth - 1) / 2;
685 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000686 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000687 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000688 float x = static_cast<float>(i - halfWidth);
689 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
690 // is dropped here, since we renormalize the kernel below.
691 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000692 sum += kernel[i];
693 }
694 // Normalize the kernel
695 float scale = 1.0f / sum;
696 for (int i = 0; i < kernelWidth; ++i)
697 kernel[i] *= scale;
698}
699
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000700static void scaleRect(SkRect* rect, float xScale, float yScale) {
701 rect->fLeft *= xScale;
702 rect->fTop *= yScale;
703 rect->fRight *= xScale;
704 rect->fBottom *= yScale;
705}
706
707static float adjustSigma(float sigma, int *scaleFactor, int *halfWidth,
708 int *kernelWidth) {
709 *scaleFactor = 1;
710 while (sigma > MAX_BLUR_SIGMA) {
711 *scaleFactor *= 2;
712 sigma *= 0.5f;
713 }
714 *halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
715 *kernelWidth = *halfWidth * 2 + 1;
716 return sigma;
717}
718
719// Apply a Gaussian blur to srcTexture by sigmaX and sigmaY, within the given
720// rect.
721// temp1 and temp2 are used for allocation of intermediate textures.
722// If temp2 is non-NULL, srcTexture will be untouched, and the return
723// value will be either temp1 or temp2.
724// If temp2 is NULL, srcTexture will be overwritten with intermediate
725// results, and the return value will either be temp1 or srcTexture.
726static GrTexture* gaussianBlur(GrContext* context, GrTexture* srcTexture,
727 GrAutoScratchTexture* temp1,
728 GrAutoScratchTexture* temp2,
729 const SkRect& rect,
730 float sigmaX, float sigmaY) {
731
732 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
733 GrClip oldClip = context->getClip();
734 GrTexture* origTexture = srcTexture;
735 GrAutoMatrix avm(context, GrMatrix::I());
736 SkIRect clearRect;
737 int scaleFactorX, halfWidthX, kernelWidthX;
738 int scaleFactorY, halfWidthY, kernelWidthY;
739 sigmaX = adjustSigma(sigmaX, &scaleFactorX, &halfWidthX, &kernelWidthX);
740 sigmaY = adjustSigma(sigmaY, &scaleFactorY, &halfWidthY, &kernelWidthY);
741
742 SkRect srcRect(rect);
743 scaleRect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
744 srcRect.roundOut();
745 scaleRect(&srcRect, scaleFactorX, scaleFactorY);
746 context->setClip(srcRect);
747
748 const GrTextureDesc desc = {
749 kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit,
750 kNone_GrAALevel,
751 srcRect.width(),
752 srcRect.height(),
bsalomon@google.com5bc34f02011-12-06 14:46:34 +0000753 kRGBA_8888_GrPixelConfig
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000754 };
755
756 temp1->set(context, desc);
757 if (temp2) temp2->set(context, desc);
758
759 GrTexture* dstTexture = temp1->texture();
760 GrPaint paint;
761 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000762 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000763
764 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
765 GrMatrix sampleM;
766 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000767 paint.textureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000768 context->setRenderTarget(dstTexture->asRenderTarget());
769 SkRect dstRect(srcRect);
770 scaleRect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
771 i < scaleFactorY ? 0.5f : 1.0f);
772 paint.setTexture(0, srcTexture);
773 context->drawRectToRect(paint, dstRect, srcRect);
774 srcRect = dstRect;
775 SkTSwap(srcTexture, dstTexture);
776 // If temp2 is non-NULL, don't render back to origTexture
777 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
778 }
779
780 if (sigmaX > 0.0f) {
781 SkAutoTMalloc<float> kernelStorageX(kernelWidthX);
782 float* kernelX = kernelStorageX.get();
783 buildKernel(sigmaX, kernelX, kernelWidthX);
784
785 if (scaleFactorX > 1) {
786 // Clear out a halfWidth to the right of the srcRect to prevent the
787 // X convolution from reading garbage.
788 clearRect = SkIRect::MakeXYWH(
789 srcRect.fRight, srcRect.fTop, halfWidthX, srcRect.height());
790 context->clear(&clearRect, 0x0);
791 }
792
793 context->setRenderTarget(dstTexture->asRenderTarget());
794 context->convolveInX(srcTexture, srcRect, kernelX, kernelWidthX);
795 SkTSwap(srcTexture, dstTexture);
796 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
797 }
798
799 if (sigmaY > 0.0f) {
800 SkAutoTMalloc<float> kernelStorageY(kernelWidthY);
801 float* kernelY = kernelStorageY.get();
802 buildKernel(sigmaY, kernelY, kernelWidthY);
803
804 if (scaleFactorY > 1 || sigmaX > 0.0f) {
805 // Clear out a halfWidth below the srcRect to prevent the Y
806 // convolution from reading garbage.
807 clearRect = SkIRect::MakeXYWH(
808 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidthY);
809 context->clear(&clearRect, 0x0);
810 }
811
812 context->setRenderTarget(dstTexture->asRenderTarget());
813 context->convolveInY(srcTexture, srcRect, kernelY, kernelWidthY);
814 SkTSwap(srcTexture, dstTexture);
815 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
816 }
817
818 if (scaleFactorX > 1 || scaleFactorY > 1) {
819 // Clear one pixel to the right and below, to accommodate bilinear
820 // upsampling.
821 clearRect = SkIRect::MakeXYWH(
822 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
823 context->clear(&clearRect, 0x0);
824 clearRect = SkIRect::MakeXYWH(
825 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
826 context->clear(&clearRect, 0x0);
827 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000828 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000829 GrMatrix sampleM;
830 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000831 paint.textureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000832 context->setRenderTarget(dstTexture->asRenderTarget());
833 paint.setTexture(0, srcTexture);
834 SkRect dstRect(srcRect);
835 scaleRect(&dstRect, scaleFactorX, scaleFactorY);
836 context->drawRectToRect(paint, dstRect, srcRect);
837 srcRect = dstRect;
838 SkTSwap(srcTexture, dstTexture);
839 }
840 context->setRenderTarget(oldRenderTarget);
841 context->setClip(oldClip);
842 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000843}
844
845static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
846 SkMaskFilter* filter, const SkMatrix& matrix,
847 const SkRegion& clip, SkBounder* bounder,
848 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000849#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000850 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000851#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000852 SkMaskFilter::BlurInfo info;
853 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000854 if (SkMaskFilter::kNone_BlurType == blurType ||
855 !context->supportsShaders()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000856 return false;
857 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000858 SkScalar radius = info.fIgnoreTransform ? info.fRadius
859 : matrix.mapRadius(info.fRadius);
860 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000861 if (radius <= 0) {
862 return false;
863 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000864 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000865 float sigma3 = sigma * 3.0f;
866
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000867 SkRect srcRect = path.getBounds();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000868 SkRect clipRect;
869 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000870
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000871 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
872 srcRect.inset(-sigma3, -sigma3);
873 clipRect.inset(-sigma3, -sigma3);
874 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000875 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000876 SkIRect finalIRect;
877 finalRect.roundOut(&finalIRect);
878 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000879 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000880 }
881 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000882 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000883 }
884 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000885 srcRect.offset(offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000886 const GrTextureDesc desc = {
887 kRenderTarget_GrTextureFlagBit,
888 kNone_GrAALevel,
889 srcRect.width(),
890 srcRect.height(),
891 // We actually only need A8, but it often isn't supported as a
892 // render target
bsalomon@google.com5bc34f02011-12-06 14:46:34 +0000893 kRGBA_8888_PM_GrPixelConfig
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000894 };
895
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000896 GrAutoScratchTexture pathEntry(context, desc);
897 GrTexture* pathTexture = pathEntry.texture();
898 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000899 return false;
900 }
901 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000902 // Once this code moves into GrContext, this should be changed to use
903 // an AutoClipRestore.
904 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000905 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000906 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000907 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000908 GrPaint tempPaint;
909 tempPaint.reset();
910
911 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000912 tempPaint.fAntiAlias = grp->fAntiAlias;
913 if (tempPaint.fAntiAlias) {
914 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
915 // blend coeff of zero requires dual source blending support in order
916 // to properly blend partially covered pixels. This means the AA
917 // code path may not be taken. So we use a dst blend coeff of ISA. We
918 // could special case AA draws to a dst surface with known alpha=0 to
919 // use a zero dst coeff when dual source blending isn't available.
920 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
921 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
922 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000923 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000924 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000925
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000926 GrAutoScratchTexture temp1, temp2;
927 // If we're doing a normal blur, we can clobber the pathTexture in the
928 // gaussianBlur. Otherwise, we need to save it for later compositing.
929 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
930 GrTexture* blurTexture = gaussianBlur(context, pathTexture,
931 &temp1, isNormalBlur ? NULL : &temp2,
932 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000933
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000934 if (!isNormalBlur) {
935 GrPaint paint;
936 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000937 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000938 GrMatrix sampleM;
939 sampleM.setIDiv(pathTexture->width(), pathTexture->height());
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000940 paint.textureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000941 // Blend pathTexture over blurTexture.
942 context->setRenderTarget(blurTexture->asRenderTarget());
943 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000944 if (SkMaskFilter::kInner_BlurType == blurType) {
945 // inner: dst = dst * src
946 paint.fSrcBlendCoeff = kDC_BlendCoeff;
947 paint.fDstBlendCoeff = kZero_BlendCoeff;
948 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
949 // solid: dst = src + dst - src * dst
950 // = (1 - dst) * src + 1 * dst
951 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
952 paint.fDstBlendCoeff = kOne_BlendCoeff;
953 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
954 // outer: dst = dst * (1 - src)
955 // = 0 * src + (1 - src) * dst
956 paint.fSrcBlendCoeff = kZero_BlendCoeff;
957 paint.fDstBlendCoeff = kISC_BlendCoeff;
958 }
959 context->drawRect(paint, srcRect);
960 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000961 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000962 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000963
964 if (grp->hasTextureOrMask()) {
965 GrMatrix inverse;
966 if (!matrix.invert(&inverse)) {
967 return false;
968 }
969 grp->preConcatActiveSamplerMatrices(inverse);
970 }
971
972 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
973 // we assume the last mask index is available for use
974 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000975 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000976 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000977
978 GrMatrix m;
979 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000980 m.postIDiv(blurTexture->width(), blurTexture->height());
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000981 grp->maskSampler(MASK_IDX)->setMatrix(m);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000982 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000983 return true;
984}
985
reed@google.com69302852011-02-16 18:08:07 +0000986static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
987 SkMaskFilter* filter, const SkMatrix& matrix,
988 const SkRegion& clip, SkBounder* bounder,
989 GrPaint* grp) {
990 SkMask srcM, dstM;
991
992 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
993 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
994 return false;
995 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000996 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000997
998 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
999 return false;
1000 }
1001 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +00001002 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001003
1004 if (clip.quickReject(dstM.fBounds)) {
1005 return false;
1006 }
1007 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1008 return false;
1009 }
1010
1011 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1012 // the current clip (and identity matrix) and grpaint settings
1013
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001014 // used to compute inverse view, if necessary
1015 GrMatrix ivm = context->getMatrix();
1016
reed@google.com0c219b62011-02-16 21:31:18 +00001017 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001018
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001019 const GrTextureDesc desc = {
1020 kNone_GrTextureFlags,
1021 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +00001022 dstM.fBounds.width(),
1023 dstM.fBounds.height(),
bsalomon@google.com5bc34f02011-12-06 14:46:34 +00001024 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +00001025 };
1026
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001027 GrAutoScratchTexture ast(context, desc);
1028 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001029
reed@google.com69302852011-02-16 18:08:07 +00001030 if (NULL == texture) {
1031 return false;
1032 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001033 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001034 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001035
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001036 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
1037 grp->preConcatActiveSamplerMatrices(ivm);
1038 }
1039
1040 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1041 // we assume the last mask index is available for use
1042 GrAssert(NULL == grp->getMask(MASK_IDX));
1043 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +00001044 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +00001045
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001046 GrRect d;
1047 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001048 GrIntToScalar(dstM.fBounds.fTop),
1049 GrIntToScalar(dstM.fBounds.fRight),
1050 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001051
1052 GrMatrix m;
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +00001053 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1054 -dstM.fBounds.fTop*SK_Scalar1);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001055 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001056 grp->maskSampler(MASK_IDX)->setMatrix(m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001057
1058 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001059 return true;
1060}
reed@google.com69302852011-02-16 18:08:07 +00001061
reed@google.com0c219b62011-02-16 21:31:18 +00001062void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comfe626382011-09-21 13:50:35 +00001063 const SkPaint& origPaint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 bool pathIsMutable) {
1065 CHECK_SHOULD_DRAW(draw);
1066
reed@google.comfe626382011-09-21 13:50:35 +00001067 bool doFill = true;
1068 SkTLazy<SkPaint> lazyPaint;
1069 const SkPaint* paint = &origPaint;
1070
1071 // can we cheat, and threat a thin stroke as a hairline (w/ modulated alpha)
1072 // if we can, we draw lots faster (raster device does this same test)
1073 {
1074 SkAlpha newAlpha;
1075 if (SkDrawTreatAsHairline(*paint, *draw.fMatrix, &newAlpha)) {
1076 lazyPaint.set(*paint);
1077 lazyPaint.get()->setAlpha(newAlpha);
1078 lazyPaint.get()->setStrokeWidth(0);
1079 paint = lazyPaint.get();
1080 doFill = false;
1081 }
1082 }
1083 // must reference paint from here down, and not origPaint
1084 // since we may have change the paint (using lazyPaint for storage)
1085
bsalomon@google.com5782d712011-01-21 21:03:59 +00001086 GrPaint grPaint;
1087 SkAutoCachedTexture act;
reed@google.comfe626382011-09-21 13:50:35 +00001088 if (!this->skPaint2GrPaintShader(*paint,
Scroggod757df22011-05-16 13:11:16 +00001089 &act,
1090 *draw.fMatrix,
1091 &grPaint,
1092 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001093 return;
1094 }
1095
reed@google.comfe626382011-09-21 13:50:35 +00001096 // If we have a prematrix, apply it to the path, optimizing for the case
1097 // where the original path can in fact be modified in place (even though
1098 // its parameter type is const).
1099 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1100 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001101
1102 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001103 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001104
reed@google.come3445642011-02-16 23:20:39 +00001105 if (!pathIsMutable) {
1106 result = &tmpPath;
1107 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001108 }
reed@google.come3445642011-02-16 23:20:39 +00001109 // should I push prePathMatrix on our MV stack temporarily, instead
1110 // of applying it here? See SkDraw.cpp
1111 pathPtr->transform(*prePathMatrix, result);
1112 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001113 }
reed@google.com0c219b62011-02-16 21:31:18 +00001114 // at this point we're done with prePathMatrix
1115 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001116
reed@google.comfe626382011-09-21 13:50:35 +00001117 if (doFill && (paint->getPathEffect() ||
1118 paint->getStyle() != SkPaint::kFill_Style)) {
1119 // it is safe to use tmpPath here, even if we already used it for the
1120 // prepathmatrix, since getFillPath can take the same object for its
1121 // input and output safely.
1122 doFill = paint->getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001123 pathPtr = &tmpPath;
1124 }
1125
reed@google.comfe626382011-09-21 13:50:35 +00001126 if (paint->getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001127 // avoid possibly allocating a new path in transform if we can
1128 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1129
1130 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001131 pathPtr->transform(*draw.fMatrix, devPathPtr);
reed@google.comfe626382011-09-21 13:50:35 +00001132 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001133 *draw.fMatrix, *draw.fClip, draw.fBounder,
1134 &grPaint)) {
reed@google.comfe626382011-09-21 13:50:35 +00001135 drawWithMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001136 *draw.fMatrix, *draw.fClip, draw.fBounder,
1137 &grPaint);
1138 }
reed@google.com69302852011-02-16 18:08:07 +00001139 return;
1140 }
reed@google.com69302852011-02-16 18:08:07 +00001141
bsalomon@google.comffca4002011-02-22 20:34:01 +00001142 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001143
reed@google.com0c219b62011-02-16 21:31:18 +00001144 if (doFill) {
1145 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001146 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001147 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001148 break;
1149 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001150 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001151 break;
1152 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001153 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001154 break;
1155 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001156 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001157 break;
1158 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001159 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001160 return;
1161 }
1162 }
1163
reed@google.com07f3ee12011-05-16 17:21:57 +00001164 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001165}
1166
bsalomon@google.comfb309512011-11-30 14:13:48 +00001167namespace {
1168
1169inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1170 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1171 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1172 return tilesX * tilesY;
1173}
1174
1175inline int determine_tile_size(const SkBitmap& bitmap,
1176 const SkIRect* srcRectPtr,
1177 int maxTextureSize) {
1178 static const int kSmallTileSize = 1 << 10;
1179 if (maxTextureSize <= kSmallTileSize) {
1180 return maxTextureSize;
1181 }
1182
1183 size_t maxTexTotalTileSize;
1184 size_t smallTotalTileSize;
1185
1186 if (NULL == srcRectPtr) {
1187 int w = bitmap.width();
1188 int h = bitmap.height();
1189 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1190 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1191 } else {
1192 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1193 srcRectPtr->fTop,
1194 srcRectPtr->fRight,
1195 srcRectPtr->fBottom,
1196 maxTextureSize);
1197 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1198 srcRectPtr->fTop,
1199 srcRectPtr->fRight,
1200 srcRectPtr->fBottom,
1201 kSmallTileSize);
1202 }
1203 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1204 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1205
1206 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1207 return kSmallTileSize;
1208 } else {
1209 return maxTextureSize;
1210 }
1211}
1212}
1213
1214bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1215 const GrSamplerState& sampler,
1216 const SkIRect* srcRectPtr,
1217 int* tileSize) const {
1218 SkASSERT(NULL != tileSize);
1219
1220 // if bitmap is explictly texture backed then just use the texture
1221 if (NULL != bitmap.getTexture()) {
1222 return false;
1223 }
1224 // if it's larger than the max texture size, then we have no choice but
1225 // tiling
1226 const int maxTextureSize = fContext->getMaxTextureSize();
1227 if (bitmap.width() > maxTextureSize ||
1228 bitmap.height() > maxTextureSize) {
1229 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1230 return true;
1231 }
1232 // if we are going to have to draw the whole thing, then don't tile
1233 if (NULL == srcRectPtr) {
1234 return false;
1235 }
1236 // if the entire texture is already in our cache then no reason to tile it
1237 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1238 return false;
1239 }
1240
1241 // At this point we know we could do the draw by uploading the entire bitmap
1242 // as a texture. However, if the texture would be large compared to the
1243 // cache size and we don't require most of it for this draw then tile to
1244 // reduce the amount of upload and cache spill.
1245
1246 // assumption here is that sw bitmap size is a good proxy for its size as
1247 // a texture
1248 size_t bmpSize = bitmap.getSize();
1249 size_t cacheSize;
1250 fContext->getTextureCacheLimits(NULL, &cacheSize);
1251 if (bmpSize < cacheSize / 2) {
1252 return false;
1253 }
1254
1255 SkFixed fracUsed =
1256 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1257 (srcRectPtr->height() << 16) / bitmap.height());
1258 if (fracUsed <= SK_FixedHalf) {
1259 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1260 return true;
1261 } else {
1262 return false;
1263 }
1264}
1265
reed@google.comac10a2d2010-12-22 21:39:39 +00001266void SkGpuDevice::drawBitmap(const SkDraw& draw,
1267 const SkBitmap& bitmap,
1268 const SkIRect* srcRectPtr,
1269 const SkMatrix& m,
1270 const SkPaint& paint) {
1271 CHECK_SHOULD_DRAW(draw);
1272
1273 SkIRect srcRect;
1274 if (NULL == srcRectPtr) {
1275 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1276 } else {
1277 srcRect = *srcRectPtr;
1278 }
1279
junov@google.comd935cfb2011-06-27 20:48:23 +00001280 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001281 // Convert the bitmap to a shader so that the rect can be drawn
1282 // through drawRect, which supports mask filters.
1283 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001284 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001285 if (srcRectPtr) {
1286 if (!bitmap.extractSubset(&tmp, srcRect)) {
1287 return; // extraction failed
1288 }
1289 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001290 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001291 }
1292 SkPaint paintWithTexture(paint);
1293 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1294 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001295 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001296 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001297
junov@google.com1d329782011-07-28 20:10:09 +00001298 // Transform 'm' needs to be concatenated to the draw matrix,
1299 // rather than transforming the primitive directly, so that 'm' will
1300 // also affect the behavior of the mask filter.
1301 SkMatrix drawMatrix;
1302 drawMatrix.setConcat(*draw.fMatrix, m);
1303 SkDraw transformedDraw(draw);
1304 transformedDraw.fMatrix = &drawMatrix;
1305
1306 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1307
junov@google.comd935cfb2011-06-27 20:48:23 +00001308 return;
1309 }
1310
bsalomon@google.com5782d712011-01-21 21:03:59 +00001311 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001312 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001313 return;
1314 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001315 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001316 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001317 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001318 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001319 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001320 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001321
bsalomon@google.comfb309512011-11-30 14:13:48 +00001322 int tileSize;
1323 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1324 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001325 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001326 return;
1327 }
1328
1329 // undo the translate done by SkCanvas
1330 int DX = SkMax32(0, srcRect.fLeft);
1331 int DY = SkMax32(0, srcRect.fTop);
1332 // compute clip bounds in local coordinates
1333 SkIRect clipRect;
1334 {
1335 SkRect r;
1336 r.set(draw.fClip->getBounds());
1337 SkMatrix matrix, inverse;
1338 matrix.setConcat(*draw.fMatrix, m);
1339 if (!matrix.invert(&inverse)) {
1340 return;
1341 }
1342 inverse.mapRect(&r);
1343 r.roundOut(&clipRect);
1344 // apply the canvas' translate to our local clip
1345 clipRect.offset(DX, DY);
1346 }
1347
bsalomon@google.comfb309512011-11-30 14:13:48 +00001348 int nx = bitmap.width() / tileSize;
1349 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001350 for (int x = 0; x <= nx; x++) {
1351 for (int y = 0; y <= ny; y++) {
1352 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001353 tileR.set(x * tileSize, y * tileSize,
1354 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001355 if (!SkIRect::Intersects(tileR, clipRect)) {
1356 continue;
1357 }
1358
1359 SkIRect srcR = tileR;
1360 if (!srcR.intersect(srcRect)) {
1361 continue;
1362 }
1363
1364 SkBitmap tmpB;
1365 if (bitmap.extractSubset(&tmpB, tileR)) {
1366 // now offset it to make it "local" to our tmp bitmap
1367 srcR.offset(-tileR.fLeft, -tileR.fTop);
1368
1369 SkMatrix tmpM(m);
1370 {
1371 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1372 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1373 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1374 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001375 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001376 }
1377 }
1378 }
1379}
1380
1381/*
1382 * This is called by drawBitmap(), which has to handle images that may be too
1383 * large to be represented by a single texture.
1384 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001385 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1386 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001387 */
1388void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1389 const SkBitmap& bitmap,
1390 const SkIRect& srcRect,
1391 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001392 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001393 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1394 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001395
reed@google.com9c49bc32011-07-07 13:42:37 +00001396 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001397 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001398 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001399 return;
1400 }
1401
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001402 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001403
1404 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1405 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1406 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1407 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001408
1409 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001410 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001411 if (NULL == texture) {
1412 return;
1413 }
1414
bsalomon@google.com452943d2011-10-31 17:37:14 +00001415 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001416
reed@google.com20efde72011-05-09 17:00:02 +00001417 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1418 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001419 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001420 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1421 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1422 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001423 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001424
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001425 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001426 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001427 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001428 // If drawing a subrect of the bitmap and filtering is enabled,
1429 // use a constrained texture domain to avoid color bleeding
1430 GrScalar left, top, right, bottom;
1431 if (srcRect.width() > 1) {
1432 GrScalar border = GR_ScalarHalf / bitmap.width();
1433 left = paintRect.left() + border;
1434 right = paintRect.right() - border;
1435 } else {
1436 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1437 }
1438 if (srcRect.height() > 1) {
1439 GrScalar border = GR_ScalarHalf / bitmap.height();
1440 top = paintRect.top() + border;
1441 bottom = paintRect.bottom() - border;
1442 } else {
1443 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1444 }
1445 GrRect textureDomain;
1446 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001447 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001448 }
1449
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001450 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001451}
1452
1453void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1454 int left, int top, const SkPaint& paint) {
1455 CHECK_SHOULD_DRAW(draw);
1456
1457 SkAutoLockPixels alp(bitmap);
1458 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1459 return;
1460 }
1461
bsalomon@google.com5782d712011-01-21 21:03:59 +00001462 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001463 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001464 return;
1465 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001466
bsalomon@google.com5782d712011-01-21 21:03:59 +00001467 GrAutoMatrix avm(fContext, GrMatrix::I());
1468
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001469 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001470
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001471 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001472 sampler->reset();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001473 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1474
1475 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001476
bsalomon@google.com5782d712011-01-21 21:03:59 +00001477 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001478 GrRect::MakeXYWH(GrIntToScalar(left),
1479 GrIntToScalar(top),
1480 GrIntToScalar(bitmap.width()),
1481 GrIntToScalar(bitmap.height())),
1482 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001483}
1484
1485void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1486 int x, int y, const SkPaint& paint) {
1487 CHECK_SHOULD_DRAW(draw);
1488
bsalomon@google.com5782d712011-01-21 21:03:59 +00001489 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001490 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001491 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001492 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001493 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001494
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001495 GrTexture* devTex = grPaint.getTexture(0);
1496 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001497
1498 const SkBitmap& bm = dev->accessBitmap(false);
1499 int w = bm.width();
1500 int h = bm.height();
1501
1502 GrAutoMatrix avm(fContext, GrMatrix::I());
1503
bsalomon@google.com97912912011-12-06 16:30:36 +00001504 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001505
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001506 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1507 GrIntToScalar(y),
1508 GrIntToScalar(w),
1509 GrIntToScalar(h));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +00001510 SkImageFilter* imageFilter = paint.getImageFilter();
1511 SkSize size;
1512 if (NULL != imageFilter && imageFilter->asABlur(&size)) {
1513 GrAutoScratchTexture temp1, temp2;
1514 GrTexture* blurTexture = gaussianBlur(fContext,
1515 devTex, &temp1, &temp2,
1516 GrRect::MakeWH(w, h),
1517 size.width(),
1518 size.height());
1519 grPaint.setTexture(kBitmapTextureIdx, blurTexture);
1520 devTex = blurTexture;
1521 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001522 // The device being drawn may not fill up its texture (saveLayer uses
1523 // the approximate ).
1524 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1525 GR_Scalar1 * h / devTex->height());
1526
1527 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001528}
1529
1530///////////////////////////////////////////////////////////////////////////////
1531
1532// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001533static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1534 kTriangles_PrimitiveType,
1535 kTriangleStrip_PrimitiveType,
1536 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001537};
1538
1539void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1540 int vertexCount, const SkPoint vertices[],
1541 const SkPoint texs[], const SkColor colors[],
1542 SkXfermode* xmode,
1543 const uint16_t indices[], int indexCount,
1544 const SkPaint& paint) {
1545 CHECK_SHOULD_DRAW(draw);
1546
bsalomon@google.com5782d712011-01-21 21:03:59 +00001547 GrPaint grPaint;
1548 SkAutoCachedTexture act;
1549 // we ignore the shader if texs is null.
1550 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001551 if (!this->skPaint2GrPaintNoShader(paint,
1552 false,
1553 &grPaint,
1554 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001555 return;
1556 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001557 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001558 if (!this->skPaint2GrPaintShader(paint, &act,
1559 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001560 &grPaint,
1561 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001562 return;
1563 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001564 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001565
1566 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001567 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001568 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1569#if 0
1570 return
1571#endif
1572 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001573 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001574
bsalomon@google.com498776a2011-08-16 19:20:44 +00001575 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1576 if (NULL != colors) {
1577 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001578 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001579 for (int i = 0; i < vertexCount; ++i) {
1580 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1581 }
1582 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001583 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001584 fContext->drawVertices(grPaint,
1585 gVertexMode2PrimitiveType[vmode],
1586 vertexCount,
1587 (GrPoint*) vertices,
1588 (GrPoint*) texs,
1589 colors,
1590 indices,
1591 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001592}
1593
1594///////////////////////////////////////////////////////////////////////////////
1595
1596static void GlyphCacheAuxProc(void* data) {
1597 delete (GrFontScaler*)data;
1598}
1599
1600static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1601 void* auxData;
1602 GrFontScaler* scaler = NULL;
1603 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1604 scaler = (GrFontScaler*)auxData;
1605 }
1606 if (NULL == scaler) {
1607 scaler = new SkGrFontScaler(cache);
1608 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1609 }
1610 return scaler;
1611}
1612
1613static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1614 SkFixed fx, SkFixed fy,
1615 const SkGlyph& glyph) {
1616 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1617
1618 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1619
1620 if (NULL == procs->fFontScaler) {
1621 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1622 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001623
1624 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001625 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001626 *
reed@google.com3b139f52011-06-07 17:56:25 +00001627 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1628 * It calls that rather than round, because our caller has already added
1629 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1630 *
1631 * Test code between raster and gpu (they should draw the same)
1632 *
1633 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1634 *
1635 * Perhaps we should only perform this integralization if there is no
1636 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001637 */
reed@google.com3b139f52011-06-07 17:56:25 +00001638 fy = SkFixedFloorToFixed(fy);
1639
reed@google.comac10a2d2010-12-22 21:39:39 +00001640 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001641 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001642 procs->fFontScaler);
1643}
1644
bsalomon@google.com5782d712011-01-21 21:03:59 +00001645SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001646
1647 // deferred allocation
1648 if (NULL == fDrawProcs) {
1649 fDrawProcs = new GrSkDrawProcs;
1650 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1651 fDrawProcs->fContext = fContext;
1652 }
1653
1654 // init our (and GL's) state
1655 fDrawProcs->fTextContext = context;
1656 fDrawProcs->fFontScaler = NULL;
1657 return fDrawProcs;
1658}
1659
1660void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1661 size_t byteLength, SkScalar x, SkScalar y,
1662 const SkPaint& paint) {
1663 CHECK_SHOULD_DRAW(draw);
1664
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001665 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001666 // this guy will just call our drawPath()
1667 draw.drawText((const char*)text, byteLength, x, y, paint);
1668 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001669 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001670
1671 GrPaint grPaint;
1672 SkAutoCachedTexture act;
1673
Scroggod757df22011-05-16 13:11:16 +00001674 if (!this->skPaint2GrPaintShader(paint,
1675 &act,
1676 *draw.fMatrix,
1677 &grPaint,
1678 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001679 return;
1680 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001681 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001682 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001683 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1684 }
1685}
1686
1687void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1688 size_t byteLength, const SkScalar pos[],
1689 SkScalar constY, int scalarsPerPos,
1690 const SkPaint& paint) {
1691 CHECK_SHOULD_DRAW(draw);
1692
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001693 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001694 // this guy will just call our drawPath()
1695 draw.drawPosText((const char*)text, byteLength, pos, constY,
1696 scalarsPerPos, paint);
1697 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001698 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001699
1700 GrPaint grPaint;
1701 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001702 if (!this->skPaint2GrPaintShader(paint,
1703 &act,
1704 *draw.fMatrix,
1705 &grPaint,
1706 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001707 return;
1708 }
1709
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001710 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001711 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001712 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1713 scalarsPerPos, paint);
1714 }
1715}
1716
1717void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1718 size_t len, const SkPath& path,
1719 const SkMatrix* m, const SkPaint& paint) {
1720 CHECK_SHOULD_DRAW(draw);
1721
1722 SkASSERT(draw.fDevice == this);
1723 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1724}
1725
1726///////////////////////////////////////////////////////////////////////////////
1727
reed@google.comf67e4cf2011-03-15 20:56:58 +00001728bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1729 if (!paint.isLCDRenderText()) {
1730 // we're cool with the paint as is
1731 return false;
1732 }
1733
1734 if (paint.getShader() ||
1735 paint.getXfermode() || // unless its srcover
1736 paint.getMaskFilter() ||
1737 paint.getRasterizer() ||
1738 paint.getColorFilter() ||
1739 paint.getPathEffect() ||
1740 paint.isFakeBoldText() ||
1741 paint.getStyle() != SkPaint::kFill_Style) {
1742 // turn off lcd
1743 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1744 flags->fHinting = paint.getHinting();
1745 return true;
1746 }
1747 // we're cool with the paint as is
1748 return false;
1749}
1750
reed@google.com75d939b2011-12-07 15:07:23 +00001751void SkGpuDevice::flush() {
1752 fContext->flush(false);
1753}
1754
reed@google.comf67e4cf2011-03-15 20:56:58 +00001755///////////////////////////////////////////////////////////////////////////////
1756
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001757SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1758 const GrSamplerState& sampler,
1759 TexType type) {
1760 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001761 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001762
bsalomon@google.come97f0852011-06-17 13:10:25 +00001763 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001764 const GrTextureDesc desc = {
1765 kRenderTarget_GrTextureFlagBit,
1766 kNone_GrAALevel,
1767 bitmap.width(),
1768 bitmap.height(),
bsalomon@google.com5bc34f02011-12-06 14:46:34 +00001769 SkGr::Bitmap2PixelConfig(bitmap)
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001770 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001771 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001772 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001773 // we know layers will only be drawn through drawDevice.
1774 // drawDevice has been made to work with content embedded in a
1775 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001776 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001777 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001778 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001779 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001780 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001781 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001782 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001783 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001784 GrContext::TextureKey key = bitmap.getGenerationID();
1785 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001786
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001787 entry = ctx->findAndLockTexture(key, bitmap.width(),
1788 bitmap.height(), sampler);
1789 if (NULL == entry.texture()) {
1790 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001791 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001792 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001793 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001794 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001795 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001796 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001797 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1798 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001799 }
1800 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001801 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001802}
1803
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001804void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1805 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001806}
1807
bsalomon@google.comfb309512011-11-30 14:13:48 +00001808bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1809 const GrSamplerState& sampler) const {
1810 GrContext::TextureKey key = bitmap.getGenerationID();
1811 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
1812 return this->context()->isTextureInCache(key, bitmap.width(),
1813 bitmap.height(), sampler);
1814
1815}
1816
1817
bsalomon@google.come97f0852011-06-17 13:10:25 +00001818SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1819 int width, int height,
1820 bool isOpaque,
1821 Usage usage) {
1822 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1823 width, height, usage));
1824}
1825