blob: ad155538102ee362f37b1761b6186fc6e94586f3 [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();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000415 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000416
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000417 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
418 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000419
420 SkXfermode* mode = skPaint.getXfermode();
421 if (mode) {
422 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000423 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000424#if 0
425 return false;
426#endif
427 }
428 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000429 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
430 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
431
bsalomon@google.com5782d712011-01-21 21:03:59 +0000432 if (justAlpha) {
433 uint8_t alpha = skPaint.getAlpha();
434 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000435 // justAlpha is currently set to true only if there is a texture,
436 // so constantColor should not also be true.
437 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000438 } else {
439 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000440 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000441 }
Scroggo97c88c22011-05-11 14:05:25 +0000442 SkColorFilter* colorFilter = skPaint.getColorFilter();
443 SkColor color;
444 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000445 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000446 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000447 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000448 if (!constantColor) {
449 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
450 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000451 } else {
452 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
453 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000454 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000455 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000456 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
457 grPaint->fColorMatrixEnabled = true;
458 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
459 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
460 } else {
461 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000462 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000464}
465
bsalomon@google.com5782d712011-01-21 21:03:59 +0000466bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
467 SkAutoCachedTexture* act,
468 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000469 GrPaint* grPaint,
470 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000471
bsalomon@google.com5782d712011-01-21 21:03:59 +0000472 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000473
bsalomon@google.com5782d712011-01-21 21:03:59 +0000474 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000475 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000476 return this->skPaint2GrPaintNoShader(skPaint,
477 false,
478 grPaint,
479 constantColor);
480 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000481 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000482 }
483
reed@google.comac10a2d2010-12-22 21:39:39 +0000484 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000485 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000486 SkShader::TileMode tileModes[2];
487 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000488 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000489 tileModes, twoPointParams);
490
bsalomon@google.com5782d712011-01-21 21:03:59 +0000491 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
492 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000493 SkShader::GradientInfo info;
494 SkColor color;
495
496 info.fColors = &color;
497 info.fColorOffsets = NULL;
498 info.fColorCount = 1;
499 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
500 SkPaint copy(skPaint);
501 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000502 // modulate the paint alpha by the shader's solid color alpha
503 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
504 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000505 return this->skPaint2GrPaintNoShader(copy,
506 false,
507 grPaint,
508 constantColor);
509 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000510 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000512 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000513 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000514 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000515 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000516 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000517 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000518 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000519 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
520 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000521 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000522 sampler->setRadial2Params(twoPointParams[0],
523 twoPointParams[1],
524 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000525 }
526
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000527 GrTexture* texture = act->set(this, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000528 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000529 SkDebugf("Couldn't convert bitmap to texture.\n");
530 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000531 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000532 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000533
534 // since our texture coords will be in local space, we wack the texture
535 // matrix to map them back into 0...1 before we load it
536 SkMatrix localM;
537 if (shader->getLocalMatrix(&localM)) {
538 SkMatrix inverse;
539 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000540 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000541 }
542 }
543 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000544 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
545 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000546 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000547 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000548 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000549 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000550 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000551
552 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000553}
554
555///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000556
bsalomon@google.com398109c2011-04-14 18:40:27 +0000557void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000558 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000559}
560
reed@google.comac10a2d2010-12-22 21:39:39 +0000561void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
562 CHECK_SHOULD_DRAW(draw);
563
bsalomon@google.com5782d712011-01-21 21:03:59 +0000564 GrPaint grPaint;
565 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000566 if (!this->skPaint2GrPaintShader(paint,
567 &act,
568 *draw.fMatrix,
569 &grPaint,
570 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000571 return;
572 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000573
574 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000575}
576
577// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000578static const GrPrimitiveType gPointMode2PrimtiveType[] = {
579 kPoints_PrimitiveType,
580 kLines_PrimitiveType,
581 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000582};
583
584void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000585 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000586 CHECK_SHOULD_DRAW(draw);
587
588 SkScalar width = paint.getStrokeWidth();
589 if (width < 0) {
590 return;
591 }
592
593 // we only handle hairlines here, else we let the SkDraw call our drawPath()
594 if (width > 0) {
595 draw.drawPoints(mode, count, pts, paint, true);
596 return;
597 }
598
bsalomon@google.com5782d712011-01-21 21:03:59 +0000599 GrPaint grPaint;
600 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000601 if (!this->skPaint2GrPaintShader(paint,
602 &act,
603 *draw.fMatrix,
604 &grPaint,
605 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000606 return;
607 }
608
bsalomon@google.com5782d712011-01-21 21:03:59 +0000609 fContext->drawVertices(grPaint,
610 gPointMode2PrimtiveType[mode],
611 count,
612 (GrPoint*)pts,
613 NULL,
614 NULL,
615 NULL,
616 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000617}
618
reed@google.comc9aa5872011-04-05 21:05:37 +0000619///////////////////////////////////////////////////////////////////////////////
620
reed@google.comac10a2d2010-12-22 21:39:39 +0000621void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
622 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000623 CHECK_SHOULD_DRAW(draw);
624
bungeman@google.com79bd8772011-07-18 15:34:08 +0000625 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000626 SkScalar width = paint.getStrokeWidth();
627
628 /*
629 We have special code for hairline strokes, miter-strokes, and fills.
630 Anything else we just call our path code.
631 */
632 bool usePath = doStroke && width > 0 &&
633 paint.getStrokeJoin() != SkPaint::kMiter_Join;
634 // another reason we might need to call drawPath...
635 if (paint.getMaskFilter()) {
636 usePath = true;
637 }
reed@google.com67db6642011-05-26 11:46:35 +0000638 // until we aa rotated rects...
639 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
640 usePath = true;
641 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000642 // small miter limit means right angles show bevel...
643 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
644 paint.getStrokeMiter() < SK_ScalarSqrt2)
645 {
646 usePath = true;
647 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000648 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000649 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
650 usePath = true;
651 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000652
653 if (usePath) {
654 SkPath path;
655 path.addRect(rect);
656 this->drawPath(draw, path, paint, NULL, true);
657 return;
658 }
659
660 GrPaint grPaint;
661 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000662 if (!this->skPaint2GrPaintShader(paint,
663 &act,
664 *draw.fMatrix,
665 &grPaint,
666 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000667 return;
668 }
reed@google.com20efde72011-05-09 17:00:02 +0000669 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000670}
671
reed@google.com69302852011-02-16 18:08:07 +0000672#include "SkMaskFilter.h"
673#include "SkBounder.h"
674
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000675static GrPathFill skToGrFillType(SkPath::FillType fillType) {
676 switch (fillType) {
677 case SkPath::kWinding_FillType:
678 return kWinding_PathFill;
679 case SkPath::kEvenOdd_FillType:
680 return kEvenOdd_PathFill;
681 case SkPath::kInverseWinding_FillType:
682 return kInverseWinding_PathFill;
683 case SkPath::kInverseEvenOdd_FillType:
684 return kInverseEvenOdd_PathFill;
685 default:
686 SkDebugf("Unsupported path fill type\n");
687 return kHairLine_PathFill;
688 }
689}
690
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000691static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000692 int halfWidth = (kernelWidth - 1) / 2;
693 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000694 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000695 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000696 float x = static_cast<float>(i - halfWidth);
697 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
698 // is dropped here, since we renormalize the kernel below.
699 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000700 sum += kernel[i];
701 }
702 // Normalize the kernel
703 float scale = 1.0f / sum;
704 for (int i = 0; i < kernelWidth; ++i)
705 kernel[i] *= scale;
706}
707
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000708static void scaleRect(SkRect* rect, float xScale, float yScale) {
709 rect->fLeft *= xScale;
710 rect->fTop *= yScale;
711 rect->fRight *= xScale;
712 rect->fBottom *= yScale;
713}
714
715static float adjustSigma(float sigma, int *scaleFactor, int *halfWidth,
716 int *kernelWidth) {
717 *scaleFactor = 1;
718 while (sigma > MAX_BLUR_SIGMA) {
719 *scaleFactor *= 2;
720 sigma *= 0.5f;
721 }
722 *halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
723 *kernelWidth = *halfWidth * 2 + 1;
724 return sigma;
725}
726
727// Apply a Gaussian blur to srcTexture by sigmaX and sigmaY, within the given
728// rect.
729// temp1 and temp2 are used for allocation of intermediate textures.
730// If temp2 is non-NULL, srcTexture will be untouched, and the return
731// value will be either temp1 or temp2.
732// If temp2 is NULL, srcTexture will be overwritten with intermediate
733// results, and the return value will either be temp1 or srcTexture.
734static GrTexture* gaussianBlur(GrContext* context, GrTexture* srcTexture,
735 GrAutoScratchTexture* temp1,
736 GrAutoScratchTexture* temp2,
737 const SkRect& rect,
738 float sigmaX, float sigmaY) {
739
740 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
741 GrClip oldClip = context->getClip();
742 GrTexture* origTexture = srcTexture;
743 GrAutoMatrix avm(context, GrMatrix::I());
744 SkIRect clearRect;
745 int scaleFactorX, halfWidthX, kernelWidthX;
746 int scaleFactorY, halfWidthY, kernelWidthY;
747 sigmaX = adjustSigma(sigmaX, &scaleFactorX, &halfWidthX, &kernelWidthX);
748 sigmaY = adjustSigma(sigmaY, &scaleFactorY, &halfWidthY, &kernelWidthY);
749
750 SkRect srcRect(rect);
751 scaleRect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
752 srcRect.roundOut();
753 scaleRect(&srcRect, scaleFactorX, scaleFactorY);
754 context->setClip(srcRect);
755
756 const GrTextureDesc desc = {
757 kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit,
758 kNone_GrAALevel,
759 srcRect.width(),
760 srcRect.height(),
bsalomon@google.com5bc34f02011-12-06 14:46:34 +0000761 kRGBA_8888_GrPixelConfig
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000762 };
763
764 temp1->set(context, desc);
765 if (temp2) temp2->set(context, desc);
766
767 GrTexture* dstTexture = temp1->texture();
768 GrPaint paint;
769 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000770 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000771
772 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000773 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
774 srcTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000775 context->setRenderTarget(dstTexture->asRenderTarget());
776 SkRect dstRect(srcRect);
777 scaleRect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
778 i < scaleFactorY ? 0.5f : 1.0f);
779 paint.setTexture(0, srcTexture);
780 context->drawRectToRect(paint, dstRect, srcRect);
781 srcRect = dstRect;
782 SkTSwap(srcTexture, dstTexture);
783 // If temp2 is non-NULL, don't render back to origTexture
784 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
785 }
786
787 if (sigmaX > 0.0f) {
788 SkAutoTMalloc<float> kernelStorageX(kernelWidthX);
789 float* kernelX = kernelStorageX.get();
790 buildKernel(sigmaX, kernelX, kernelWidthX);
791
792 if (scaleFactorX > 1) {
793 // Clear out a halfWidth to the right of the srcRect to prevent the
794 // X convolution from reading garbage.
795 clearRect = SkIRect::MakeXYWH(
796 srcRect.fRight, srcRect.fTop, halfWidthX, srcRect.height());
797 context->clear(&clearRect, 0x0);
798 }
799
800 context->setRenderTarget(dstTexture->asRenderTarget());
801 context->convolveInX(srcTexture, srcRect, kernelX, kernelWidthX);
802 SkTSwap(srcTexture, dstTexture);
803 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
804 }
805
806 if (sigmaY > 0.0f) {
807 SkAutoTMalloc<float> kernelStorageY(kernelWidthY);
808 float* kernelY = kernelStorageY.get();
809 buildKernel(sigmaY, kernelY, kernelWidthY);
810
811 if (scaleFactorY > 1 || sigmaX > 0.0f) {
812 // Clear out a halfWidth below the srcRect to prevent the Y
813 // convolution from reading garbage.
814 clearRect = SkIRect::MakeXYWH(
815 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidthY);
816 context->clear(&clearRect, 0x0);
817 }
818
819 context->setRenderTarget(dstTexture->asRenderTarget());
820 context->convolveInY(srcTexture, srcRect, kernelY, kernelWidthY);
821 SkTSwap(srcTexture, dstTexture);
822 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
823 }
824
825 if (scaleFactorX > 1 || scaleFactorY > 1) {
826 // Clear one pixel to the right and below, to accommodate bilinear
827 // upsampling.
828 clearRect = SkIRect::MakeXYWH(
829 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
830 context->clear(&clearRect, 0x0);
831 clearRect = SkIRect::MakeXYWH(
832 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
833 context->clear(&clearRect, 0x0);
834 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000835 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000836 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
837 srcTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000838 context->setRenderTarget(dstTexture->asRenderTarget());
839 paint.setTexture(0, srcTexture);
840 SkRect dstRect(srcRect);
841 scaleRect(&dstRect, scaleFactorX, scaleFactorY);
842 context->drawRectToRect(paint, dstRect, srcRect);
843 srcRect = dstRect;
844 SkTSwap(srcTexture, dstTexture);
845 }
846 context->setRenderTarget(oldRenderTarget);
847 context->setClip(oldClip);
848 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000849}
850
851static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
852 SkMaskFilter* filter, const SkMatrix& matrix,
853 const SkRegion& clip, SkBounder* bounder,
854 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000855#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000856 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000857#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000858 SkMaskFilter::BlurInfo info;
859 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000860 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000861 return false;
862 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000863 SkScalar radius = info.fIgnoreTransform ? info.fRadius
864 : matrix.mapRadius(info.fRadius);
865 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000866 if (radius <= 0) {
867 return false;
868 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000869 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000870 float sigma3 = sigma * 3.0f;
871
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000872 SkRect srcRect = path.getBounds();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000873 SkRect clipRect;
874 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000875
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000876 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
877 srcRect.inset(-sigma3, -sigma3);
878 clipRect.inset(-sigma3, -sigma3);
879 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000880 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000881 SkIRect finalIRect;
882 finalRect.roundOut(&finalIRect);
883 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000884 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000885 }
886 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000887 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000888 }
889 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000890 srcRect.offset(offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000891 const GrTextureDesc desc = {
892 kRenderTarget_GrTextureFlagBit,
893 kNone_GrAALevel,
894 srcRect.width(),
895 srcRect.height(),
896 // We actually only need A8, but it often isn't supported as a
897 // render target
bsalomon@google.com5bc34f02011-12-06 14:46:34 +0000898 kRGBA_8888_PM_GrPixelConfig
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000899 };
900
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000901 GrAutoScratchTexture pathEntry(context, desc);
902 GrTexture* pathTexture = pathEntry.texture();
903 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000904 return false;
905 }
906 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000907 // Once this code moves into GrContext, this should be changed to use
908 // an AutoClipRestore.
909 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000910 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000911 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000912 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000913 GrPaint tempPaint;
914 tempPaint.reset();
915
916 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000917 tempPaint.fAntiAlias = grp->fAntiAlias;
918 if (tempPaint.fAntiAlias) {
919 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
920 // blend coeff of zero requires dual source blending support in order
921 // to properly blend partially covered pixels. This means the AA
922 // code path may not be taken. So we use a dst blend coeff of ISA. We
923 // could special case AA draws to a dst surface with known alpha=0 to
924 // use a zero dst coeff when dual source blending isn't available.
925 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
926 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
927 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000928 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000929 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000930
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000931 GrAutoScratchTexture temp1, temp2;
932 // If we're doing a normal blur, we can clobber the pathTexture in the
933 // gaussianBlur. Otherwise, we need to save it for later compositing.
934 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
935 GrTexture* blurTexture = gaussianBlur(context, pathTexture,
936 &temp1, isNormalBlur ? NULL : &temp2,
937 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000938
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000939 if (!isNormalBlur) {
940 GrPaint paint;
941 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000942 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000943 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
944 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000945 // Blend pathTexture over blurTexture.
946 context->setRenderTarget(blurTexture->asRenderTarget());
947 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000948 if (SkMaskFilter::kInner_BlurType == blurType) {
949 // inner: dst = dst * src
950 paint.fSrcBlendCoeff = kDC_BlendCoeff;
951 paint.fDstBlendCoeff = kZero_BlendCoeff;
952 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
953 // solid: dst = src + dst - src * dst
954 // = (1 - dst) * src + 1 * dst
955 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
956 paint.fDstBlendCoeff = kOne_BlendCoeff;
957 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
958 // outer: dst = dst * (1 - src)
959 // = 0 * src + (1 - src) * dst
960 paint.fSrcBlendCoeff = kZero_BlendCoeff;
961 paint.fDstBlendCoeff = kISC_BlendCoeff;
962 }
963 context->drawRect(paint, srcRect);
964 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000965 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000966 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000967
968 if (grp->hasTextureOrMask()) {
969 GrMatrix inverse;
970 if (!matrix.invert(&inverse)) {
971 return false;
972 }
973 grp->preConcatActiveSamplerMatrices(inverse);
974 }
975
976 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
977 // we assume the last mask index is available for use
978 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000979 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000980 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000981
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000982 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
983 -finalRect.fTop);
984 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
985 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000986 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000987 return true;
988}
989
reed@google.com69302852011-02-16 18:08:07 +0000990static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
991 SkMaskFilter* filter, const SkMatrix& matrix,
992 const SkRegion& clip, SkBounder* bounder,
993 GrPaint* grp) {
994 SkMask srcM, dstM;
995
996 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
997 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
998 return false;
999 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001000 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001001
1002 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
1003 return false;
1004 }
1005 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +00001006 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001007
1008 if (clip.quickReject(dstM.fBounds)) {
1009 return false;
1010 }
1011 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1012 return false;
1013 }
1014
1015 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1016 // the current clip (and identity matrix) and grpaint settings
1017
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001018 // used to compute inverse view, if necessary
1019 GrMatrix ivm = context->getMatrix();
1020
reed@google.com0c219b62011-02-16 21:31:18 +00001021 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001022
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001023 const GrTextureDesc desc = {
1024 kNone_GrTextureFlags,
1025 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +00001026 dstM.fBounds.width(),
1027 dstM.fBounds.height(),
bsalomon@google.com5bc34f02011-12-06 14:46:34 +00001028 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +00001029 };
1030
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001031 GrAutoScratchTexture ast(context, desc);
1032 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001033
reed@google.com69302852011-02-16 18:08:07 +00001034 if (NULL == texture) {
1035 return false;
1036 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001037 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001038 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001039
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001040 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
1041 grp->preConcatActiveSamplerMatrices(ivm);
1042 }
1043
1044 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1045 // we assume the last mask index is available for use
1046 GrAssert(NULL == grp->getMask(MASK_IDX));
1047 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +00001048 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +00001049
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001050 GrRect d;
1051 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001052 GrIntToScalar(dstM.fBounds.fTop),
1053 GrIntToScalar(dstM.fBounds.fRight),
1054 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001055
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001056 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1057 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1058 -dstM.fBounds.fTop*SK_Scalar1);
1059 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001060 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001061 return true;
1062}
reed@google.com69302852011-02-16 18:08:07 +00001063
reed@google.com0c219b62011-02-16 21:31:18 +00001064void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001065 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001066 bool pathIsMutable) {
1067 CHECK_SHOULD_DRAW(draw);
1068
reed@google.comfe626382011-09-21 13:50:35 +00001069 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001070
1071 SkScalar coverage = SK_Scalar1;
1072 // can we cheat, and threat a thin stroke as a hairline w/ coverage
reed@google.comfe626382011-09-21 13:50:35 +00001073 // if we can, we draw lots faster (raster device does this same test)
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001074 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &coverage)) {
1075 doFill = false;
reed@google.comfe626382011-09-21 13:50:35 +00001076 }
reed@google.comfe626382011-09-21 13:50:35 +00001077
bsalomon@google.com5782d712011-01-21 21:03:59 +00001078 GrPaint grPaint;
1079 SkAutoCachedTexture act;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001080 if (!this->skPaint2GrPaintShader(paint,
Scroggod757df22011-05-16 13:11:16 +00001081 &act,
1082 *draw.fMatrix,
1083 &grPaint,
1084 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001085 return;
1086 }
1087
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001088 grPaint.fCoverage = SkScalarRoundToInt(coverage * grPaint.fCoverage);
1089
reed@google.comfe626382011-09-21 13:50:35 +00001090 // If we have a prematrix, apply it to the path, optimizing for the case
1091 // where the original path can in fact be modified in place (even though
1092 // its parameter type is const).
1093 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1094 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001095
1096 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001097 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001098
reed@google.come3445642011-02-16 23:20:39 +00001099 if (!pathIsMutable) {
1100 result = &tmpPath;
1101 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001102 }
reed@google.come3445642011-02-16 23:20:39 +00001103 // should I push prePathMatrix on our MV stack temporarily, instead
1104 // of applying it here? See SkDraw.cpp
1105 pathPtr->transform(*prePathMatrix, result);
1106 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001107 }
reed@google.com0c219b62011-02-16 21:31:18 +00001108 // at this point we're done with prePathMatrix
1109 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001110
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001111 if (doFill && (paint.getPathEffect() ||
1112 paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001113 // it is safe to use tmpPath here, even if we already used it for the
1114 // prepathmatrix, since getFillPath can take the same object for its
1115 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001116 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001117 pathPtr = &tmpPath;
1118 }
1119
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001120 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001121 // avoid possibly allocating a new path in transform if we can
1122 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1123
1124 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001125 pathPtr->transform(*draw.fMatrix, devPathPtr);
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001126 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001127 *draw.fMatrix, *draw.fClip, draw.fBounder,
1128 &grPaint)) {
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001129 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001130 *draw.fMatrix, *draw.fClip, draw.fBounder,
1131 &grPaint);
1132 }
reed@google.com69302852011-02-16 18:08:07 +00001133 return;
1134 }
reed@google.com69302852011-02-16 18:08:07 +00001135
bsalomon@google.comffca4002011-02-22 20:34:01 +00001136 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001137
reed@google.com0c219b62011-02-16 21:31:18 +00001138 if (doFill) {
1139 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001140 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001141 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001142 break;
1143 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001144 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001145 break;
1146 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001147 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001148 break;
1149 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001150 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001151 break;
1152 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001153 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001154 return;
1155 }
1156 }
1157
reed@google.com07f3ee12011-05-16 17:21:57 +00001158 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001159}
1160
bsalomon@google.comfb309512011-11-30 14:13:48 +00001161namespace {
1162
1163inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1164 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1165 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1166 return tilesX * tilesY;
1167}
1168
1169inline int determine_tile_size(const SkBitmap& bitmap,
1170 const SkIRect* srcRectPtr,
1171 int maxTextureSize) {
1172 static const int kSmallTileSize = 1 << 10;
1173 if (maxTextureSize <= kSmallTileSize) {
1174 return maxTextureSize;
1175 }
1176
1177 size_t maxTexTotalTileSize;
1178 size_t smallTotalTileSize;
1179
1180 if (NULL == srcRectPtr) {
1181 int w = bitmap.width();
1182 int h = bitmap.height();
1183 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1184 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1185 } else {
1186 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1187 srcRectPtr->fTop,
1188 srcRectPtr->fRight,
1189 srcRectPtr->fBottom,
1190 maxTextureSize);
1191 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1192 srcRectPtr->fTop,
1193 srcRectPtr->fRight,
1194 srcRectPtr->fBottom,
1195 kSmallTileSize);
1196 }
1197 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1198 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1199
1200 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1201 return kSmallTileSize;
1202 } else {
1203 return maxTextureSize;
1204 }
1205}
1206}
1207
1208bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1209 const GrSamplerState& sampler,
1210 const SkIRect* srcRectPtr,
1211 int* tileSize) const {
1212 SkASSERT(NULL != tileSize);
1213
1214 // if bitmap is explictly texture backed then just use the texture
1215 if (NULL != bitmap.getTexture()) {
1216 return false;
1217 }
1218 // if it's larger than the max texture size, then we have no choice but
1219 // tiling
1220 const int maxTextureSize = fContext->getMaxTextureSize();
1221 if (bitmap.width() > maxTextureSize ||
1222 bitmap.height() > maxTextureSize) {
1223 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1224 return true;
1225 }
1226 // if we are going to have to draw the whole thing, then don't tile
1227 if (NULL == srcRectPtr) {
1228 return false;
1229 }
1230 // if the entire texture is already in our cache then no reason to tile it
1231 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1232 return false;
1233 }
1234
1235 // At this point we know we could do the draw by uploading the entire bitmap
1236 // as a texture. However, if the texture would be large compared to the
1237 // cache size and we don't require most of it for this draw then tile to
1238 // reduce the amount of upload and cache spill.
1239
1240 // assumption here is that sw bitmap size is a good proxy for its size as
1241 // a texture
1242 size_t bmpSize = bitmap.getSize();
1243 size_t cacheSize;
1244 fContext->getTextureCacheLimits(NULL, &cacheSize);
1245 if (bmpSize < cacheSize / 2) {
1246 return false;
1247 }
1248
1249 SkFixed fracUsed =
1250 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1251 (srcRectPtr->height() << 16) / bitmap.height());
1252 if (fracUsed <= SK_FixedHalf) {
1253 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1254 return true;
1255 } else {
1256 return false;
1257 }
1258}
1259
reed@google.comac10a2d2010-12-22 21:39:39 +00001260void SkGpuDevice::drawBitmap(const SkDraw& draw,
1261 const SkBitmap& bitmap,
1262 const SkIRect* srcRectPtr,
1263 const SkMatrix& m,
1264 const SkPaint& paint) {
1265 CHECK_SHOULD_DRAW(draw);
1266
1267 SkIRect srcRect;
1268 if (NULL == srcRectPtr) {
1269 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1270 } else {
1271 srcRect = *srcRectPtr;
1272 }
1273
junov@google.comd935cfb2011-06-27 20:48:23 +00001274 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001275 // Convert the bitmap to a shader so that the rect can be drawn
1276 // through drawRect, which supports mask filters.
1277 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001278 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001279 if (srcRectPtr) {
1280 if (!bitmap.extractSubset(&tmp, srcRect)) {
1281 return; // extraction failed
1282 }
1283 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001284 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001285 }
1286 SkPaint paintWithTexture(paint);
1287 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1288 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001289 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001290 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001291
junov@google.com1d329782011-07-28 20:10:09 +00001292 // Transform 'm' needs to be concatenated to the draw matrix,
1293 // rather than transforming the primitive directly, so that 'm' will
1294 // also affect the behavior of the mask filter.
1295 SkMatrix drawMatrix;
1296 drawMatrix.setConcat(*draw.fMatrix, m);
1297 SkDraw transformedDraw(draw);
1298 transformedDraw.fMatrix = &drawMatrix;
1299
1300 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1301
junov@google.comd935cfb2011-06-27 20:48:23 +00001302 return;
1303 }
1304
bsalomon@google.com5782d712011-01-21 21:03:59 +00001305 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001306 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001307 return;
1308 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001309 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001310 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001311 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001312 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001313 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001314 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001315
bsalomon@google.comfb309512011-11-30 14:13:48 +00001316 int tileSize;
1317 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1318 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001319 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 return;
1321 }
1322
1323 // undo the translate done by SkCanvas
1324 int DX = SkMax32(0, srcRect.fLeft);
1325 int DY = SkMax32(0, srcRect.fTop);
1326 // compute clip bounds in local coordinates
1327 SkIRect clipRect;
1328 {
1329 SkRect r;
1330 r.set(draw.fClip->getBounds());
1331 SkMatrix matrix, inverse;
1332 matrix.setConcat(*draw.fMatrix, m);
1333 if (!matrix.invert(&inverse)) {
1334 return;
1335 }
1336 inverse.mapRect(&r);
1337 r.roundOut(&clipRect);
1338 // apply the canvas' translate to our local clip
1339 clipRect.offset(DX, DY);
1340 }
1341
bsalomon@google.comfb309512011-11-30 14:13:48 +00001342 int nx = bitmap.width() / tileSize;
1343 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001344 for (int x = 0; x <= nx; x++) {
1345 for (int y = 0; y <= ny; y++) {
1346 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001347 tileR.set(x * tileSize, y * tileSize,
1348 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001349 if (!SkIRect::Intersects(tileR, clipRect)) {
1350 continue;
1351 }
1352
1353 SkIRect srcR = tileR;
1354 if (!srcR.intersect(srcRect)) {
1355 continue;
1356 }
1357
1358 SkBitmap tmpB;
1359 if (bitmap.extractSubset(&tmpB, tileR)) {
1360 // now offset it to make it "local" to our tmp bitmap
1361 srcR.offset(-tileR.fLeft, -tileR.fTop);
1362
1363 SkMatrix tmpM(m);
1364 {
1365 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1366 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1367 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1368 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001369 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001370 }
1371 }
1372 }
1373}
1374
1375/*
1376 * This is called by drawBitmap(), which has to handle images that may be too
1377 * large to be represented by a single texture.
1378 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001379 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1380 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001381 */
1382void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1383 const SkBitmap& bitmap,
1384 const SkIRect& srcRect,
1385 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001386 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001387 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1388 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001389
reed@google.com9c49bc32011-07-07 13:42:37 +00001390 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001391 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001392 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001393 return;
1394 }
1395
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001396 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001397
1398 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1399 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1400 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001401 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001402
1403 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001404 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001405 if (NULL == texture) {
1406 return;
1407 }
1408
bsalomon@google.com452943d2011-10-31 17:37:14 +00001409 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001410
reed@google.com20efde72011-05-09 17:00:02 +00001411 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1412 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001413 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001414 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1415 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1416 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001417 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001418
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001419 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001420 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001421 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001422 // If drawing a subrect of the bitmap and filtering is enabled,
1423 // use a constrained texture domain to avoid color bleeding
1424 GrScalar left, top, right, bottom;
1425 if (srcRect.width() > 1) {
1426 GrScalar border = GR_ScalarHalf / bitmap.width();
1427 left = paintRect.left() + border;
1428 right = paintRect.right() - border;
1429 } else {
1430 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1431 }
1432 if (srcRect.height() > 1) {
1433 GrScalar border = GR_ScalarHalf / bitmap.height();
1434 top = paintRect.top() + border;
1435 bottom = paintRect.bottom() - border;
1436 } else {
1437 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1438 }
1439 GrRect textureDomain;
1440 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001441 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001442 }
1443
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001444 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001445}
1446
1447void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1448 int left, int top, const SkPaint& paint) {
1449 CHECK_SHOULD_DRAW(draw);
1450
1451 SkAutoLockPixels alp(bitmap);
1452 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1453 return;
1454 }
1455
reed@google.com76dd2772012-01-05 21:15:07 +00001456 int w = bitmap.width();
1457 int h = bitmap.height();
1458
bsalomon@google.com5782d712011-01-21 21:03:59 +00001459 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001460 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001461 return;
1462 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001463
bsalomon@google.com5782d712011-01-21 21:03:59 +00001464 GrAutoMatrix avm(fContext, GrMatrix::I());
1465
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001466 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001467
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001468 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001469 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001470 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001471
reed@google.com76dd2772012-01-05 21:15:07 +00001472 SkImageFilter* imageFilter = paint.getImageFilter();
1473 SkSize blurSize;
1474 if (NULL != imageFilter && imageFilter->asABlur(&blurSize)) {
1475 GrAutoScratchTexture temp1, temp2;
1476 GrTexture* blurTexture = gaussianBlur(fContext,
1477 texture, &temp1, &temp2,
1478 GrRect::MakeWH(w, h),
1479 blurSize.width(),
1480 blurSize.height());
1481 texture = blurTexture;
1482 grPaint.setTexture(kBitmapTextureIdx, texture);
reed@google.com76dd2772012-01-05 21:15:07 +00001483 } else {
1484 grPaint.setTexture(kBitmapTextureIdx, texture);
1485 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001486
bsalomon@google.com5782d712011-01-21 21:03:59 +00001487 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001488 GrRect::MakeXYWH(GrIntToScalar(left),
1489 GrIntToScalar(top),
1490 GrIntToScalar(w),
1491 GrIntToScalar(h)),
1492 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1493 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001494}
1495
1496void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1497 int x, int y, const SkPaint& paint) {
1498 CHECK_SHOULD_DRAW(draw);
1499
bsalomon@google.com5782d712011-01-21 21:03:59 +00001500 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001501 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001502 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001503 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001504 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001505
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001506 GrTexture* devTex = grPaint.getTexture(0);
1507 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001508
1509 const SkBitmap& bm = dev->accessBitmap(false);
1510 int w = bm.width();
1511 int h = bm.height();
1512
1513 GrAutoMatrix avm(fContext, GrMatrix::I());
1514
bsalomon@google.com97912912011-12-06 16:30:36 +00001515 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001516
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001517 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1518 GrIntToScalar(y),
1519 GrIntToScalar(w),
1520 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001521
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
reed@google.com76dd2772012-01-05 21:15:07 +00001530bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1531 const SkMatrix& ctm,
1532 SkBitmap* result, SkIPoint* offset) {
1533 SkSize size;
1534 if (!filter->asABlur(&size)) {
1535 return false;
1536 }
1537 SkDevice* dev = this->createCompatibleDevice(SkBitmap::kARGB_8888_Config,
1538 src.width(),
1539 src.height(),
1540 false);
1541 if (NULL == dev) {
1542 return false;
1543 }
1544 SkAutoUnref aur(dev);
1545 SkCanvas canvas(dev);
1546 SkPaint paint;
1547 paint.setImageFilter(filter);
1548 canvas.drawSprite(src, 0, 0, &paint);
1549 *result = dev->accessBitmap(false);
1550 return true;
1551}
1552
reed@google.comac10a2d2010-12-22 21:39:39 +00001553///////////////////////////////////////////////////////////////////////////////
1554
1555// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001556static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1557 kTriangles_PrimitiveType,
1558 kTriangleStrip_PrimitiveType,
1559 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001560};
1561
1562void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1563 int vertexCount, const SkPoint vertices[],
1564 const SkPoint texs[], const SkColor colors[],
1565 SkXfermode* xmode,
1566 const uint16_t indices[], int indexCount,
1567 const SkPaint& paint) {
1568 CHECK_SHOULD_DRAW(draw);
1569
bsalomon@google.com5782d712011-01-21 21:03:59 +00001570 GrPaint grPaint;
1571 SkAutoCachedTexture act;
1572 // we ignore the shader if texs is null.
1573 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001574 if (!this->skPaint2GrPaintNoShader(paint,
1575 false,
1576 &grPaint,
1577 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001578 return;
1579 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001580 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001581 if (!this->skPaint2GrPaintShader(paint, &act,
1582 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001583 &grPaint,
1584 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001585 return;
1586 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001587 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001588
1589 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001590 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001591 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1592#if 0
1593 return
1594#endif
1595 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001596 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001597
bsalomon@google.com498776a2011-08-16 19:20:44 +00001598 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1599 if (NULL != colors) {
1600 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001601 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001602 for (int i = 0; i < vertexCount; ++i) {
1603 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1604 }
1605 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001606 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001607 fContext->drawVertices(grPaint,
1608 gVertexMode2PrimitiveType[vmode],
1609 vertexCount,
1610 (GrPoint*) vertices,
1611 (GrPoint*) texs,
1612 colors,
1613 indices,
1614 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001615}
1616
1617///////////////////////////////////////////////////////////////////////////////
1618
1619static void GlyphCacheAuxProc(void* data) {
1620 delete (GrFontScaler*)data;
1621}
1622
1623static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1624 void* auxData;
1625 GrFontScaler* scaler = NULL;
1626 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1627 scaler = (GrFontScaler*)auxData;
1628 }
1629 if (NULL == scaler) {
1630 scaler = new SkGrFontScaler(cache);
1631 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1632 }
1633 return scaler;
1634}
1635
1636static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1637 SkFixed fx, SkFixed fy,
1638 const SkGlyph& glyph) {
1639 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1640
bungeman@google.com15865a72012-01-11 16:28:04 +00001641 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001642
1643 if (NULL == procs->fFontScaler) {
1644 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1645 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001646
bungeman@google.com15865a72012-01-11 16:28:04 +00001647 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1648 glyph.getSubXFixed(),
1649 glyph.getSubYFixed()),
1650 SkFixedFloorToFixed(fx),
1651 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001652 procs->fFontScaler);
1653}
1654
bsalomon@google.com5782d712011-01-21 21:03:59 +00001655SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001656
1657 // deferred allocation
1658 if (NULL == fDrawProcs) {
1659 fDrawProcs = new GrSkDrawProcs;
1660 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1661 fDrawProcs->fContext = fContext;
1662 }
1663
1664 // init our (and GL's) state
1665 fDrawProcs->fTextContext = context;
1666 fDrawProcs->fFontScaler = NULL;
1667 return fDrawProcs;
1668}
1669
1670void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1671 size_t byteLength, SkScalar x, SkScalar y,
1672 const SkPaint& paint) {
1673 CHECK_SHOULD_DRAW(draw);
1674
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001675 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001676 // this guy will just call our drawPath()
1677 draw.drawText((const char*)text, byteLength, x, y, paint);
1678 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001679 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001680
1681 GrPaint grPaint;
1682 SkAutoCachedTexture act;
1683
Scroggod757df22011-05-16 13:11:16 +00001684 if (!this->skPaint2GrPaintShader(paint,
1685 &act,
1686 *draw.fMatrix,
1687 &grPaint,
1688 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001689 return;
1690 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001691 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001692 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001693 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1694 }
1695}
1696
1697void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1698 size_t byteLength, const SkScalar pos[],
1699 SkScalar constY, int scalarsPerPos,
1700 const SkPaint& paint) {
1701 CHECK_SHOULD_DRAW(draw);
1702
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001703 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001704 // this guy will just call our drawPath()
1705 draw.drawPosText((const char*)text, byteLength, pos, constY,
1706 scalarsPerPos, paint);
1707 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001708 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001709
1710 GrPaint grPaint;
1711 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001712 if (!this->skPaint2GrPaintShader(paint,
1713 &act,
1714 *draw.fMatrix,
1715 &grPaint,
1716 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001717 return;
1718 }
1719
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001720 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001721 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001722 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1723 scalarsPerPos, paint);
1724 }
1725}
1726
1727void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1728 size_t len, const SkPath& path,
1729 const SkMatrix* m, const SkPaint& paint) {
1730 CHECK_SHOULD_DRAW(draw);
1731
1732 SkASSERT(draw.fDevice == this);
1733 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1734}
1735
1736///////////////////////////////////////////////////////////////////////////////
1737
reed@google.comf67e4cf2011-03-15 20:56:58 +00001738bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1739 if (!paint.isLCDRenderText()) {
1740 // we're cool with the paint as is
1741 return false;
1742 }
1743
1744 if (paint.getShader() ||
1745 paint.getXfermode() || // unless its srcover
1746 paint.getMaskFilter() ||
1747 paint.getRasterizer() ||
1748 paint.getColorFilter() ||
1749 paint.getPathEffect() ||
1750 paint.isFakeBoldText() ||
1751 paint.getStyle() != SkPaint::kFill_Style) {
1752 // turn off lcd
1753 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1754 flags->fHinting = paint.getHinting();
1755 return true;
1756 }
1757 // we're cool with the paint as is
1758 return false;
1759}
1760
reed@google.com75d939b2011-12-07 15:07:23 +00001761void SkGpuDevice::flush() {
1762 fContext->flush(false);
1763}
1764
reed@google.comf67e4cf2011-03-15 20:56:58 +00001765///////////////////////////////////////////////////////////////////////////////
1766
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001767SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001768 const GrSamplerState* sampler,
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001769 TexType type) {
1770 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001771 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001772
bsalomon@google.come97f0852011-06-17 13:10:25 +00001773 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001774 const GrTextureDesc desc = {
1775 kRenderTarget_GrTextureFlagBit,
1776 kNone_GrAALevel,
1777 bitmap.width(),
1778 bitmap.height(),
bsalomon@google.com5bc34f02011-12-06 14:46:34 +00001779 SkGr::Bitmap2PixelConfig(bitmap)
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001780 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001781 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001782 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001783 // we know layers will only be drawn through drawDevice.
1784 // drawDevice has been made to work with content embedded in a
1785 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001786 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001787 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001788 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001789 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001790 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001791 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001792 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001793 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001794 GrContext::TextureKey key = bitmap.getGenerationID();
1795 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001796
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001797 entry = ctx->findAndLockTexture(key, bitmap.width(),
1798 bitmap.height(), sampler);
1799 if (NULL == entry.texture()) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001800 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001801 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001802 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001803 } else {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001804 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY,
1805 sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001806 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001807 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001808 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1809 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001810 }
1811 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001812 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001813}
1814
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001815void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1816 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001817}
1818
bsalomon@google.comfb309512011-11-30 14:13:48 +00001819bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1820 const GrSamplerState& sampler) const {
1821 GrContext::TextureKey key = bitmap.getGenerationID();
1822 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
1823 return this->context()->isTextureInCache(key, bitmap.width(),
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001824 bitmap.height(), &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001825
1826}
1827
1828
bsalomon@google.come97f0852011-06-17 13:10:25 +00001829SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1830 int width, int height,
1831 bool isOpaque,
1832 Usage usage) {
1833 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1834 width, height, usage));
1835}
1836