blob: 25ded6316f9eecb295a61131c42fb031bf321de8 [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);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000170
171 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
172 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
173 // the device via accessBitmap. This external ref may outlive the device.
174 // Since textures own their render targets (but not vice-versa) we
175 // are ensuring that both objects will live as long as the pixel ref.
176 SkPixelRef* pr;
177 if (fTexture) {
178 pr = new SkGrTexturePixelRef(fTexture);
179 } else {
180 pr = new SkGrRenderTargetPixelRef(fRenderTarget);
181 }
reed@google.comaf951c92011-06-16 19:10:39 +0000182 this->setPixelRef(pr, 0)->unref();
183}
184
185SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000186 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000187: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000188 fNeedPrepareRenderTarget = false;
189 fDrawProcs = NULL;
190
reed@google.com7b201d22011-01-11 18:59:23 +0000191 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000192 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000193
reed@google.comac10a2d2010-12-22 21:39:39 +0000194 fTexture = NULL;
195 fRenderTarget = NULL;
196 fNeedClear = false;
197
reed@google.comaf951c92011-06-16 19:10:39 +0000198 if (config != SkBitmap::kRGB_565_Config) {
199 config = SkBitmap::kARGB_8888_Config;
200 }
201 SkBitmap bm;
202 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000203
204#if CACHE_LAYER_TEXTURES
bsalomon@google.come97f0852011-06-17 13:10:25 +0000205 TexType type = (kSaveLayer_Usage == usage) ?
206 kSaveLayerDeviceRenderTarget_TexType :
207 kDeviceRenderTarget_TexType;
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000208 fCache = this->lockCachedTexture(bm, NULL, type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000209 fTexture = fCache.texture();
210 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000211 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000212 // hold a ref directly on fTexture (even though fCache has one) to match
213 // other constructor paths. Simplifies cleanup.
214 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000215 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000216#else
reed@google.comaf951c92011-06-16 19:10:39 +0000217 const GrTextureDesc desc = {
218 kRenderTarget_GrTextureFlagBit,
219 kNone_GrAALevel,
220 width,
221 height,
222 SkGr::Bitmap2PixelConfig(bm)
223 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000224
reed@google.comaf951c92011-06-16 19:10:39 +0000225 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000226#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000227 if (NULL != fTexture) {
228 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000229 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000230
reed@google.comaf951c92011-06-16 19:10:39 +0000231 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000232
reed@google.comaf951c92011-06-16 19:10:39 +0000233 // we defer the actual clear until our gainFocus()
234 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000235
reed@google.comaf951c92011-06-16 19:10:39 +0000236 // wrap the bitmap with a pixelref to expose our texture
237 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000238 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000239 } else {
240 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
241 width, height);
242 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000243 }
244}
245
246SkGpuDevice::~SkGpuDevice() {
247 if (fDrawProcs) {
248 delete fDrawProcs;
249 }
250
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000251 SkSafeUnref(fTexture);
252 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000253 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000254 GrAssert(NULL != fTexture);
255 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000256 fContext->unlockTexture(fCache);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000257 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000258 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000259}
260
reed@google.comac10a2d2010-12-22 21:39:39 +0000261///////////////////////////////////////////////////////////////////////////////
262
263void SkGpuDevice::makeRenderTargetCurrent() {
264 fContext->setRenderTarget(fRenderTarget);
265 fContext->flush(true);
266 fNeedPrepareRenderTarget = true;
267}
268
269///////////////////////////////////////////////////////////////////////////////
270
bsalomon@google.comc4364992011-11-07 15:54:49 +0000271namespace {
272GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
273 switch (config8888) {
274 case SkCanvas::kNative_Premul_Config8888:
275 return kSkia8888_PM_GrPixelConfig;
276 case SkCanvas::kNative_Unpremul_Config8888:
277 return kSkia8888_UPM_GrPixelConfig;
278 case SkCanvas::kBGRA_Premul_Config8888:
279 return kBGRA_8888_PM_GrPixelConfig;
280 case SkCanvas::kBGRA_Unpremul_Config8888:
281 return kBGRA_8888_UPM_GrPixelConfig;
282 case SkCanvas::kRGBA_Premul_Config8888:
283 return kRGBA_8888_PM_GrPixelConfig;
284 case SkCanvas::kRGBA_Unpremul_Config8888:
285 return kRGBA_8888_UPM_GrPixelConfig;
286 default:
287 GrCrash("Unexpected Config8888.");
288 return kSkia8888_PM_GrPixelConfig;
289 }
290}
291}
292
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000293bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
294 int x, int y,
295 SkCanvas::Config8888 config8888) {
bsalomon@google.com910267d2011-11-02 20:06:25 +0000296 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
297 SkASSERT(!bitmap.isNull());
298 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000299
bsalomon@google.com910267d2011-11-02 20:06:25 +0000300 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000301 GrPixelConfig config;
302 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000303 return fContext->readRenderTargetPixels(fRenderTarget,
304 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000305 bitmap.width(),
306 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000307 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000308 bitmap.getPixels(),
309 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000310}
311
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000312void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
313 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000314 SkAutoLockPixels alp(bitmap);
315 if (!bitmap.readyToDraw()) {
316 return;
317 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000318
319 GrPixelConfig config;
320 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
321 config = config8888_to_gr_config(config8888);
322 } else {
323 config= SkGr::BitmapConfig2PixelConfig(bitmap.config(),
324 bitmap.isOpaque());
325 }
326
bsalomon@google.com6f379512011-11-16 20:36:03 +0000327 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
328 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000329}
330
331///////////////////////////////////////////////////////////////////////////////
332
333static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000334 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000335 const SkRegion& clipRegion,
336 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000337 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000338
339 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000340 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000341 const SkIRect& skBounds = clipRegion.getBounds();
342 GrRect bounds;
343 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
344 GrIntToScalar(skBounds.fTop),
345 GrIntToScalar(skBounds.fRight),
346 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000347 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
348 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000349 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000350}
351
352// call this ever each draw call, to ensure that the context reflects our state,
353// and not the state from some other canvas/device
354void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
355 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000356 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000357
358 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000359 SkASSERT(draw.fClipStack);
360 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000361 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000362 fNeedPrepareRenderTarget = false;
363 }
364}
365
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000366void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
367 const SkClipStack& clipStack) {
368 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
369 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000370 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000371}
372
373void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000374 const SkRegion& clip, const SkClipStack& clipStack) {
375
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 fContext->setRenderTarget(fRenderTarget);
377
bsalomon@google.comd302f142011-03-03 13:54:13 +0000378 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000379
reed@google.com6f8f2922011-03-04 22:27:10 +0000380 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000381
382 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000383 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000384 fNeedClear = false;
385 }
386}
387
reed@google.com75d939b2011-12-07 15:07:23 +0000388SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
389 return (SkGpuRenderTarget*)fRenderTarget;
390}
391
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000392bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000393 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000394 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000395 return true;
396 }
397 return false;
398}
399
400///////////////////////////////////////////////////////////////////////////////
401
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000402SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
403SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
404SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
405SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
406SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
407 shader_type_mismatch);
408SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000409
bsalomon@google.com5782d712011-01-21 21:03:59 +0000410static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
411 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
412 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
413 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
414 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
415 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
416};
417
418bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
419 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000420 GrPaint* grPaint,
421 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000422
423 grPaint->fDither = skPaint.isDither();
424 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000425 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000426
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000427 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
428 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000429
430 SkXfermode* mode = skPaint.getXfermode();
431 if (mode) {
432 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000433 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000434#if 0
435 return false;
436#endif
437 }
438 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000439 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
440 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
441
bsalomon@google.com5782d712011-01-21 21:03:59 +0000442 if (justAlpha) {
443 uint8_t alpha = skPaint.getAlpha();
444 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000445 // justAlpha is currently set to true only if there is a texture,
446 // so constantColor should not also be true.
447 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000448 } else {
449 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000450 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000451 }
Scroggo97c88c22011-05-11 14:05:25 +0000452 SkColorFilter* colorFilter = skPaint.getColorFilter();
453 SkColor color;
454 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000455 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000456 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000457 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000458 if (!constantColor) {
459 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
460 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000461 } else {
462 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
463 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000464 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000465 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000466 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
467 grPaint->fColorMatrixEnabled = true;
468 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
469 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
470 } else {
471 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000472 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000473 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000474}
475
bsalomon@google.com5782d712011-01-21 21:03:59 +0000476bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
477 SkAutoCachedTexture* act,
478 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000479 GrPaint* grPaint,
480 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000481
bsalomon@google.com5782d712011-01-21 21:03:59 +0000482 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000483
bsalomon@google.com5782d712011-01-21 21:03:59 +0000484 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000485 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000486 return this->skPaint2GrPaintNoShader(skPaint,
487 false,
488 grPaint,
489 constantColor);
490 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000491 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000492 }
493
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000495 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000496 SkShader::TileMode tileModes[2];
497 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000498 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000499 tileModes, twoPointParams);
500
bsalomon@google.com5782d712011-01-21 21:03:59 +0000501 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
502 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000503 SkShader::GradientInfo info;
504 SkColor color;
505
506 info.fColors = &color;
507 info.fColorOffsets = NULL;
508 info.fColorCount = 1;
509 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
510 SkPaint copy(skPaint);
511 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000512 // modulate the paint alpha by the shader's solid color alpha
513 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
514 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000515 return this->skPaint2GrPaintNoShader(copy,
516 false,
517 grPaint,
518 constantColor);
519 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000520 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000521 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000522 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000523 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000524 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000525 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000526 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000527 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000528 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000529 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
530 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000531 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000532 sampler->setRadial2Params(twoPointParams[0],
533 twoPointParams[1],
534 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000535 }
536
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000537 GrTexture* texture = act->set(this, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000538 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000539 SkDebugf("Couldn't convert bitmap to texture.\n");
540 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000541 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000542 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000543
544 // since our texture coords will be in local space, we wack the texture
545 // matrix to map them back into 0...1 before we load it
546 SkMatrix localM;
547 if (shader->getLocalMatrix(&localM)) {
548 SkMatrix inverse;
549 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000550 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000551 }
552 }
553 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000554 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
555 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000556 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000557 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000558 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000559 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000560 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000561
562 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000563}
564
565///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000566
bsalomon@google.com398109c2011-04-14 18:40:27 +0000567void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000568 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000569}
570
reed@google.comac10a2d2010-12-22 21:39:39 +0000571void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
572 CHECK_SHOULD_DRAW(draw);
573
bsalomon@google.com5782d712011-01-21 21:03:59 +0000574 GrPaint grPaint;
575 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000576 if (!this->skPaint2GrPaintShader(paint,
577 &act,
578 *draw.fMatrix,
579 &grPaint,
580 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000581 return;
582 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000583
584 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000585}
586
587// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000588static const GrPrimitiveType gPointMode2PrimtiveType[] = {
589 kPoints_PrimitiveType,
590 kLines_PrimitiveType,
591 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000592};
593
594void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000595 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000596 CHECK_SHOULD_DRAW(draw);
597
598 SkScalar width = paint.getStrokeWidth();
599 if (width < 0) {
600 return;
601 }
602
603 // we only handle hairlines here, else we let the SkDraw call our drawPath()
604 if (width > 0) {
605 draw.drawPoints(mode, count, pts, paint, true);
606 return;
607 }
608
bsalomon@google.com5782d712011-01-21 21:03:59 +0000609 GrPaint grPaint;
610 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000611 if (!this->skPaint2GrPaintShader(paint,
612 &act,
613 *draw.fMatrix,
614 &grPaint,
615 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000616 return;
617 }
618
bsalomon@google.com5782d712011-01-21 21:03:59 +0000619 fContext->drawVertices(grPaint,
620 gPointMode2PrimtiveType[mode],
621 count,
622 (GrPoint*)pts,
623 NULL,
624 NULL,
625 NULL,
626 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000627}
628
reed@google.comc9aa5872011-04-05 21:05:37 +0000629///////////////////////////////////////////////////////////////////////////////
630
reed@google.comac10a2d2010-12-22 21:39:39 +0000631void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
632 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000633 CHECK_SHOULD_DRAW(draw);
634
bungeman@google.com79bd8772011-07-18 15:34:08 +0000635 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000636 SkScalar width = paint.getStrokeWidth();
637
638 /*
639 We have special code for hairline strokes, miter-strokes, and fills.
640 Anything else we just call our path code.
641 */
642 bool usePath = doStroke && width > 0 &&
643 paint.getStrokeJoin() != SkPaint::kMiter_Join;
644 // another reason we might need to call drawPath...
645 if (paint.getMaskFilter()) {
646 usePath = true;
647 }
reed@google.com67db6642011-05-26 11:46:35 +0000648 // until we aa rotated rects...
649 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
650 usePath = true;
651 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000652 // small miter limit means right angles show bevel...
653 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
654 paint.getStrokeMiter() < SK_ScalarSqrt2)
655 {
656 usePath = true;
657 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000658 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000659 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
660 usePath = true;
661 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000662
663 if (usePath) {
664 SkPath path;
665 path.addRect(rect);
666 this->drawPath(draw, path, paint, NULL, true);
667 return;
668 }
669
670 GrPaint grPaint;
671 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000672 if (!this->skPaint2GrPaintShader(paint,
673 &act,
674 *draw.fMatrix,
675 &grPaint,
676 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000677 return;
678 }
reed@google.com20efde72011-05-09 17:00:02 +0000679 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000680}
681
reed@google.com69302852011-02-16 18:08:07 +0000682#include "SkMaskFilter.h"
683#include "SkBounder.h"
684
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000685static GrPathFill skToGrFillType(SkPath::FillType fillType) {
686 switch (fillType) {
687 case SkPath::kWinding_FillType:
688 return kWinding_PathFill;
689 case SkPath::kEvenOdd_FillType:
690 return kEvenOdd_PathFill;
691 case SkPath::kInverseWinding_FillType:
692 return kInverseWinding_PathFill;
693 case SkPath::kInverseEvenOdd_FillType:
694 return kInverseEvenOdd_PathFill;
695 default:
696 SkDebugf("Unsupported path fill type\n");
697 return kHairLine_PathFill;
698 }
699}
700
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000701static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000702 int halfWidth = (kernelWidth - 1) / 2;
703 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000704 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000705 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000706 float x = static_cast<float>(i - halfWidth);
707 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
708 // is dropped here, since we renormalize the kernel below.
709 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000710 sum += kernel[i];
711 }
712 // Normalize the kernel
713 float scale = 1.0f / sum;
714 for (int i = 0; i < kernelWidth; ++i)
715 kernel[i] *= scale;
716}
717
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000718static void scaleRect(SkRect* rect, float xScale, float yScale) {
719 rect->fLeft *= xScale;
720 rect->fTop *= yScale;
721 rect->fRight *= xScale;
722 rect->fBottom *= yScale;
723}
724
725static float adjustSigma(float sigma, int *scaleFactor, int *halfWidth,
726 int *kernelWidth) {
727 *scaleFactor = 1;
728 while (sigma > MAX_BLUR_SIGMA) {
729 *scaleFactor *= 2;
730 sigma *= 0.5f;
731 }
732 *halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
733 *kernelWidth = *halfWidth * 2 + 1;
734 return sigma;
735}
736
737// Apply a Gaussian blur to srcTexture by sigmaX and sigmaY, within the given
738// rect.
739// temp1 and temp2 are used for allocation of intermediate textures.
740// If temp2 is non-NULL, srcTexture will be untouched, and the return
741// value will be either temp1 or temp2.
742// If temp2 is NULL, srcTexture will be overwritten with intermediate
743// results, and the return value will either be temp1 or srcTexture.
744static GrTexture* gaussianBlur(GrContext* context, GrTexture* srcTexture,
745 GrAutoScratchTexture* temp1,
746 GrAutoScratchTexture* temp2,
747 const SkRect& rect,
748 float sigmaX, float sigmaY) {
749
750 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
751 GrClip oldClip = context->getClip();
752 GrTexture* origTexture = srcTexture;
753 GrAutoMatrix avm(context, GrMatrix::I());
754 SkIRect clearRect;
755 int scaleFactorX, halfWidthX, kernelWidthX;
756 int scaleFactorY, halfWidthY, kernelWidthY;
757 sigmaX = adjustSigma(sigmaX, &scaleFactorX, &halfWidthX, &kernelWidthX);
758 sigmaY = adjustSigma(sigmaY, &scaleFactorY, &halfWidthY, &kernelWidthY);
759
760 SkRect srcRect(rect);
761 scaleRect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
762 srcRect.roundOut();
763 scaleRect(&srcRect, scaleFactorX, scaleFactorY);
764 context->setClip(srcRect);
765
766 const GrTextureDesc desc = {
767 kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit,
768 kNone_GrAALevel,
769 srcRect.width(),
770 srcRect.height(),
bsalomon@google.com5bc34f02011-12-06 14:46:34 +0000771 kRGBA_8888_GrPixelConfig
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000772 };
773
774 temp1->set(context, desc);
775 if (temp2) temp2->set(context, desc);
776
777 GrTexture* dstTexture = temp1->texture();
778 GrPaint paint;
779 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000780 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000781
782 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000783 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
784 srcTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000785 context->setRenderTarget(dstTexture->asRenderTarget());
786 SkRect dstRect(srcRect);
787 scaleRect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
788 i < scaleFactorY ? 0.5f : 1.0f);
789 paint.setTexture(0, srcTexture);
790 context->drawRectToRect(paint, dstRect, srcRect);
791 srcRect = dstRect;
792 SkTSwap(srcTexture, dstTexture);
793 // If temp2 is non-NULL, don't render back to origTexture
794 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
795 }
796
797 if (sigmaX > 0.0f) {
798 SkAutoTMalloc<float> kernelStorageX(kernelWidthX);
799 float* kernelX = kernelStorageX.get();
800 buildKernel(sigmaX, kernelX, kernelWidthX);
801
802 if (scaleFactorX > 1) {
803 // Clear out a halfWidth to the right of the srcRect to prevent the
804 // X convolution from reading garbage.
805 clearRect = SkIRect::MakeXYWH(
806 srcRect.fRight, srcRect.fTop, halfWidthX, srcRect.height());
807 context->clear(&clearRect, 0x0);
808 }
809
810 context->setRenderTarget(dstTexture->asRenderTarget());
811 context->convolveInX(srcTexture, srcRect, kernelX, kernelWidthX);
812 SkTSwap(srcTexture, dstTexture);
813 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
814 }
815
816 if (sigmaY > 0.0f) {
817 SkAutoTMalloc<float> kernelStorageY(kernelWidthY);
818 float* kernelY = kernelStorageY.get();
819 buildKernel(sigmaY, kernelY, kernelWidthY);
820
821 if (scaleFactorY > 1 || sigmaX > 0.0f) {
822 // Clear out a halfWidth below the srcRect to prevent the Y
823 // convolution from reading garbage.
824 clearRect = SkIRect::MakeXYWH(
825 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidthY);
826 context->clear(&clearRect, 0x0);
827 }
828
829 context->setRenderTarget(dstTexture->asRenderTarget());
830 context->convolveInY(srcTexture, srcRect, kernelY, kernelWidthY);
831 SkTSwap(srcTexture, dstTexture);
832 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
833 }
834
835 if (scaleFactorX > 1 || scaleFactorY > 1) {
836 // Clear one pixel to the right and below, to accommodate bilinear
837 // upsampling.
838 clearRect = SkIRect::MakeXYWH(
839 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
840 context->clear(&clearRect, 0x0);
841 clearRect = SkIRect::MakeXYWH(
842 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
843 context->clear(&clearRect, 0x0);
844 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000845 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000846 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
847 srcTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000848 context->setRenderTarget(dstTexture->asRenderTarget());
849 paint.setTexture(0, srcTexture);
850 SkRect dstRect(srcRect);
851 scaleRect(&dstRect, scaleFactorX, scaleFactorY);
852 context->drawRectToRect(paint, dstRect, srcRect);
853 srcRect = dstRect;
854 SkTSwap(srcTexture, dstTexture);
855 }
856 context->setRenderTarget(oldRenderTarget);
857 context->setClip(oldClip);
858 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000859}
860
861static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
862 SkMaskFilter* filter, const SkMatrix& matrix,
863 const SkRegion& clip, SkBounder* bounder,
864 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000865#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000866 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000867#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000868 SkMaskFilter::BlurInfo info;
869 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000870 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871 return false;
872 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000873 SkScalar radius = info.fIgnoreTransform ? info.fRadius
874 : matrix.mapRadius(info.fRadius);
875 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000876 if (radius <= 0) {
877 return false;
878 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000879 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000880 float sigma3 = sigma * 3.0f;
881
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000882 SkRect srcRect = path.getBounds();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000883 SkRect clipRect;
884 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000885
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000886 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
887 srcRect.inset(-sigma3, -sigma3);
888 clipRect.inset(-sigma3, -sigma3);
889 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000890 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000891 SkIRect finalIRect;
892 finalRect.roundOut(&finalIRect);
893 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000894 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000895 }
896 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000897 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000898 }
899 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000900 srcRect.offset(offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000901 const GrTextureDesc desc = {
902 kRenderTarget_GrTextureFlagBit,
903 kNone_GrAALevel,
904 srcRect.width(),
905 srcRect.height(),
906 // We actually only need A8, but it often isn't supported as a
907 // render target
bsalomon@google.com5bc34f02011-12-06 14:46:34 +0000908 kRGBA_8888_PM_GrPixelConfig
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000909 };
910
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000911 GrAutoScratchTexture pathEntry(context, desc);
912 GrTexture* pathTexture = pathEntry.texture();
913 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000914 return false;
915 }
916 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000917 // Once this code moves into GrContext, this should be changed to use
918 // an AutoClipRestore.
919 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000920 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000921 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000922 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000923 GrPaint tempPaint;
924 tempPaint.reset();
925
926 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000927 tempPaint.fAntiAlias = grp->fAntiAlias;
928 if (tempPaint.fAntiAlias) {
929 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
930 // blend coeff of zero requires dual source blending support in order
931 // to properly blend partially covered pixels. This means the AA
932 // code path may not be taken. So we use a dst blend coeff of ISA. We
933 // could special case AA draws to a dst surface with known alpha=0 to
934 // use a zero dst coeff when dual source blending isn't available.
935 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
936 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
937 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000938 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000939 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000940
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000941 GrAutoScratchTexture temp1, temp2;
942 // If we're doing a normal blur, we can clobber the pathTexture in the
943 // gaussianBlur. Otherwise, we need to save it for later compositing.
944 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
945 GrTexture* blurTexture = gaussianBlur(context, pathTexture,
946 &temp1, isNormalBlur ? NULL : &temp2,
947 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000948
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000949 if (!isNormalBlur) {
950 GrPaint paint;
951 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000952 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000953 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
954 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000955 // Blend pathTexture over blurTexture.
956 context->setRenderTarget(blurTexture->asRenderTarget());
957 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000958 if (SkMaskFilter::kInner_BlurType == blurType) {
959 // inner: dst = dst * src
960 paint.fSrcBlendCoeff = kDC_BlendCoeff;
961 paint.fDstBlendCoeff = kZero_BlendCoeff;
962 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
963 // solid: dst = src + dst - src * dst
964 // = (1 - dst) * src + 1 * dst
965 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
966 paint.fDstBlendCoeff = kOne_BlendCoeff;
967 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
968 // outer: dst = dst * (1 - src)
969 // = 0 * src + (1 - src) * dst
970 paint.fSrcBlendCoeff = kZero_BlendCoeff;
971 paint.fDstBlendCoeff = kISC_BlendCoeff;
972 }
973 context->drawRect(paint, srcRect);
974 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000975 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000976 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000977
978 if (grp->hasTextureOrMask()) {
979 GrMatrix inverse;
980 if (!matrix.invert(&inverse)) {
981 return false;
982 }
983 grp->preConcatActiveSamplerMatrices(inverse);
984 }
985
986 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
987 // we assume the last mask index is available for use
988 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000989 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000990 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000991
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000992 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
993 -finalRect.fTop);
994 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
995 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000996 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000997 return true;
998}
999
reed@google.com69302852011-02-16 18:08:07 +00001000static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
1001 SkMaskFilter* filter, const SkMatrix& matrix,
1002 const SkRegion& clip, SkBounder* bounder,
1003 GrPaint* grp) {
1004 SkMask srcM, dstM;
1005
1006 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
1007 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
1008 return false;
1009 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001010 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001011
1012 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
1013 return false;
1014 }
1015 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +00001016 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001017
1018 if (clip.quickReject(dstM.fBounds)) {
1019 return false;
1020 }
1021 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1022 return false;
1023 }
1024
1025 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1026 // the current clip (and identity matrix) and grpaint settings
1027
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001028 // used to compute inverse view, if necessary
1029 GrMatrix ivm = context->getMatrix();
1030
reed@google.com0c219b62011-02-16 21:31:18 +00001031 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001032
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001033 const GrTextureDesc desc = {
1034 kNone_GrTextureFlags,
1035 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +00001036 dstM.fBounds.width(),
1037 dstM.fBounds.height(),
bsalomon@google.com5bc34f02011-12-06 14:46:34 +00001038 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +00001039 };
1040
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001041 GrAutoScratchTexture ast(context, desc);
1042 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001043
reed@google.com69302852011-02-16 18:08:07 +00001044 if (NULL == texture) {
1045 return false;
1046 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001047 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001048 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001049
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001050 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
1051 grp->preConcatActiveSamplerMatrices(ivm);
1052 }
1053
1054 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1055 // we assume the last mask index is available for use
1056 GrAssert(NULL == grp->getMask(MASK_IDX));
1057 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +00001058 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +00001059
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001060 GrRect d;
1061 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001062 GrIntToScalar(dstM.fBounds.fTop),
1063 GrIntToScalar(dstM.fBounds.fRight),
1064 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001065
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001066 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1067 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1068 -dstM.fBounds.fTop*SK_Scalar1);
1069 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001070 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001071 return true;
1072}
reed@google.com69302852011-02-16 18:08:07 +00001073
reed@google.com0c219b62011-02-16 21:31:18 +00001074void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001075 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001076 bool pathIsMutable) {
1077 CHECK_SHOULD_DRAW(draw);
1078
reed@google.comfe626382011-09-21 13:50:35 +00001079 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001080
1081 SkScalar coverage = SK_Scalar1;
1082 // can we cheat, and threat a thin stroke as a hairline w/ coverage
reed@google.comfe626382011-09-21 13:50:35 +00001083 // if we can, we draw lots faster (raster device does this same test)
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001084 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &coverage)) {
1085 doFill = false;
reed@google.comfe626382011-09-21 13:50:35 +00001086 }
reed@google.comfe626382011-09-21 13:50:35 +00001087
bsalomon@google.com5782d712011-01-21 21:03:59 +00001088 GrPaint grPaint;
1089 SkAutoCachedTexture act;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001090 if (!this->skPaint2GrPaintShader(paint,
Scroggod757df22011-05-16 13:11:16 +00001091 &act,
1092 *draw.fMatrix,
1093 &grPaint,
1094 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001095 return;
1096 }
1097
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001098 grPaint.fCoverage = SkScalarRoundToInt(coverage * grPaint.fCoverage);
1099
reed@google.comfe626382011-09-21 13:50:35 +00001100 // If we have a prematrix, apply it to the path, optimizing for the case
1101 // where the original path can in fact be modified in place (even though
1102 // its parameter type is const).
1103 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1104 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001105
1106 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001107 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001108
reed@google.come3445642011-02-16 23:20:39 +00001109 if (!pathIsMutable) {
1110 result = &tmpPath;
1111 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001112 }
reed@google.come3445642011-02-16 23:20:39 +00001113 // should I push prePathMatrix on our MV stack temporarily, instead
1114 // of applying it here? See SkDraw.cpp
1115 pathPtr->transform(*prePathMatrix, result);
1116 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001117 }
reed@google.com0c219b62011-02-16 21:31:18 +00001118 // at this point we're done with prePathMatrix
1119 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001120
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001121 if (doFill && (paint.getPathEffect() ||
1122 paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001123 // it is safe to use tmpPath here, even if we already used it for the
1124 // prepathmatrix, since getFillPath can take the same object for its
1125 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001126 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001127 pathPtr = &tmpPath;
1128 }
1129
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001130 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001131 // avoid possibly allocating a new path in transform if we can
1132 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1133
1134 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001135 pathPtr->transform(*draw.fMatrix, devPathPtr);
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001136 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001137 *draw.fMatrix, *draw.fClip, draw.fBounder,
1138 &grPaint)) {
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001139 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001140 *draw.fMatrix, *draw.fClip, draw.fBounder,
1141 &grPaint);
1142 }
reed@google.com69302852011-02-16 18:08:07 +00001143 return;
1144 }
reed@google.com69302852011-02-16 18:08:07 +00001145
bsalomon@google.comffca4002011-02-22 20:34:01 +00001146 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001147
reed@google.com0c219b62011-02-16 21:31:18 +00001148 if (doFill) {
1149 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001150 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001151 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001152 break;
1153 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001154 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001155 break;
1156 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001157 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001158 break;
1159 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001160 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001161 break;
1162 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001163 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001164 return;
1165 }
1166 }
1167
reed@google.com07f3ee12011-05-16 17:21:57 +00001168 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001169}
1170
bsalomon@google.comfb309512011-11-30 14:13:48 +00001171namespace {
1172
1173inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1174 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1175 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1176 return tilesX * tilesY;
1177}
1178
1179inline int determine_tile_size(const SkBitmap& bitmap,
1180 const SkIRect* srcRectPtr,
1181 int maxTextureSize) {
1182 static const int kSmallTileSize = 1 << 10;
1183 if (maxTextureSize <= kSmallTileSize) {
1184 return maxTextureSize;
1185 }
1186
1187 size_t maxTexTotalTileSize;
1188 size_t smallTotalTileSize;
1189
1190 if (NULL == srcRectPtr) {
1191 int w = bitmap.width();
1192 int h = bitmap.height();
1193 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1194 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1195 } else {
1196 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1197 srcRectPtr->fTop,
1198 srcRectPtr->fRight,
1199 srcRectPtr->fBottom,
1200 maxTextureSize);
1201 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1202 srcRectPtr->fTop,
1203 srcRectPtr->fRight,
1204 srcRectPtr->fBottom,
1205 kSmallTileSize);
1206 }
1207 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1208 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1209
1210 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1211 return kSmallTileSize;
1212 } else {
1213 return maxTextureSize;
1214 }
1215}
1216}
1217
1218bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1219 const GrSamplerState& sampler,
1220 const SkIRect* srcRectPtr,
1221 int* tileSize) const {
1222 SkASSERT(NULL != tileSize);
1223
1224 // if bitmap is explictly texture backed then just use the texture
1225 if (NULL != bitmap.getTexture()) {
1226 return false;
1227 }
1228 // if it's larger than the max texture size, then we have no choice but
1229 // tiling
1230 const int maxTextureSize = fContext->getMaxTextureSize();
1231 if (bitmap.width() > maxTextureSize ||
1232 bitmap.height() > maxTextureSize) {
1233 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1234 return true;
1235 }
1236 // if we are going to have to draw the whole thing, then don't tile
1237 if (NULL == srcRectPtr) {
1238 return false;
1239 }
1240 // if the entire texture is already in our cache then no reason to tile it
1241 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1242 return false;
1243 }
1244
1245 // At this point we know we could do the draw by uploading the entire bitmap
1246 // as a texture. However, if the texture would be large compared to the
1247 // cache size and we don't require most of it for this draw then tile to
1248 // reduce the amount of upload and cache spill.
1249
1250 // assumption here is that sw bitmap size is a good proxy for its size as
1251 // a texture
1252 size_t bmpSize = bitmap.getSize();
1253 size_t cacheSize;
1254 fContext->getTextureCacheLimits(NULL, &cacheSize);
1255 if (bmpSize < cacheSize / 2) {
1256 return false;
1257 }
1258
1259 SkFixed fracUsed =
1260 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1261 (srcRectPtr->height() << 16) / bitmap.height());
1262 if (fracUsed <= SK_FixedHalf) {
1263 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1264 return true;
1265 } else {
1266 return false;
1267 }
1268}
1269
reed@google.comac10a2d2010-12-22 21:39:39 +00001270void SkGpuDevice::drawBitmap(const SkDraw& draw,
1271 const SkBitmap& bitmap,
1272 const SkIRect* srcRectPtr,
1273 const SkMatrix& m,
1274 const SkPaint& paint) {
1275 CHECK_SHOULD_DRAW(draw);
1276
1277 SkIRect srcRect;
1278 if (NULL == srcRectPtr) {
1279 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1280 } else {
1281 srcRect = *srcRectPtr;
1282 }
1283
junov@google.comd935cfb2011-06-27 20:48:23 +00001284 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001285 // Convert the bitmap to a shader so that the rect can be drawn
1286 // through drawRect, which supports mask filters.
1287 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001288 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001289 if (srcRectPtr) {
1290 if (!bitmap.extractSubset(&tmp, srcRect)) {
1291 return; // extraction failed
1292 }
1293 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001294 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001295 }
1296 SkPaint paintWithTexture(paint);
1297 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1298 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001299 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001300 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001301
junov@google.com1d329782011-07-28 20:10:09 +00001302 // Transform 'm' needs to be concatenated to the draw matrix,
1303 // rather than transforming the primitive directly, so that 'm' will
1304 // also affect the behavior of the mask filter.
1305 SkMatrix drawMatrix;
1306 drawMatrix.setConcat(*draw.fMatrix, m);
1307 SkDraw transformedDraw(draw);
1308 transformedDraw.fMatrix = &drawMatrix;
1309
1310 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1311
junov@google.comd935cfb2011-06-27 20:48:23 +00001312 return;
1313 }
1314
bsalomon@google.com5782d712011-01-21 21:03:59 +00001315 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001316 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001317 return;
1318 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001319 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001320 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001321 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001322 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001323 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001324 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001325
bsalomon@google.comfb309512011-11-30 14:13:48 +00001326 int tileSize;
1327 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1328 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001329 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001330 return;
1331 }
1332
1333 // undo the translate done by SkCanvas
1334 int DX = SkMax32(0, srcRect.fLeft);
1335 int DY = SkMax32(0, srcRect.fTop);
1336 // compute clip bounds in local coordinates
1337 SkIRect clipRect;
1338 {
1339 SkRect r;
1340 r.set(draw.fClip->getBounds());
1341 SkMatrix matrix, inverse;
1342 matrix.setConcat(*draw.fMatrix, m);
1343 if (!matrix.invert(&inverse)) {
1344 return;
1345 }
1346 inverse.mapRect(&r);
1347 r.roundOut(&clipRect);
1348 // apply the canvas' translate to our local clip
1349 clipRect.offset(DX, DY);
1350 }
1351
bsalomon@google.comfb309512011-11-30 14:13:48 +00001352 int nx = bitmap.width() / tileSize;
1353 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001354 for (int x = 0; x <= nx; x++) {
1355 for (int y = 0; y <= ny; y++) {
1356 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001357 tileR.set(x * tileSize, y * tileSize,
1358 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001359 if (!SkIRect::Intersects(tileR, clipRect)) {
1360 continue;
1361 }
1362
1363 SkIRect srcR = tileR;
1364 if (!srcR.intersect(srcRect)) {
1365 continue;
1366 }
1367
1368 SkBitmap tmpB;
1369 if (bitmap.extractSubset(&tmpB, tileR)) {
1370 // now offset it to make it "local" to our tmp bitmap
1371 srcR.offset(-tileR.fLeft, -tileR.fTop);
1372
1373 SkMatrix tmpM(m);
1374 {
1375 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1376 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1377 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1378 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001379 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001380 }
1381 }
1382 }
1383}
1384
1385/*
1386 * This is called by drawBitmap(), which has to handle images that may be too
1387 * large to be represented by a single texture.
1388 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001389 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1390 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001391 */
1392void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1393 const SkBitmap& bitmap,
1394 const SkIRect& srcRect,
1395 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001396 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001397 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1398 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001399
reed@google.com9c49bc32011-07-07 13:42:37 +00001400 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001401 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001402 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001403 return;
1404 }
1405
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001406 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001407
1408 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1409 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1410 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001411 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001412
1413 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001414 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001415 if (NULL == texture) {
1416 return;
1417 }
1418
bsalomon@google.com452943d2011-10-31 17:37:14 +00001419 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001420
reed@google.com20efde72011-05-09 17:00:02 +00001421 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1422 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001423 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001424 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1425 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1426 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001427 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001428
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001429 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001430 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001431 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001432 // If drawing a subrect of the bitmap and filtering is enabled,
1433 // use a constrained texture domain to avoid color bleeding
1434 GrScalar left, top, right, bottom;
1435 if (srcRect.width() > 1) {
1436 GrScalar border = GR_ScalarHalf / bitmap.width();
1437 left = paintRect.left() + border;
1438 right = paintRect.right() - border;
1439 } else {
1440 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1441 }
1442 if (srcRect.height() > 1) {
1443 GrScalar border = GR_ScalarHalf / bitmap.height();
1444 top = paintRect.top() + border;
1445 bottom = paintRect.bottom() - border;
1446 } else {
1447 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1448 }
1449 GrRect textureDomain;
1450 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001451 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001452 }
1453
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001454 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001455}
1456
1457void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1458 int left, int top, const SkPaint& paint) {
1459 CHECK_SHOULD_DRAW(draw);
1460
1461 SkAutoLockPixels alp(bitmap);
1462 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1463 return;
1464 }
1465
reed@google.com76dd2772012-01-05 21:15:07 +00001466 int w = bitmap.width();
1467 int h = bitmap.height();
1468
bsalomon@google.com5782d712011-01-21 21:03:59 +00001469 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001470 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001471 return;
1472 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001473
bsalomon@google.com5782d712011-01-21 21:03:59 +00001474 GrAutoMatrix avm(fContext, GrMatrix::I());
1475
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001476 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001477
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001478 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001479 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001480 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001481
reed@google.com76dd2772012-01-05 21:15:07 +00001482 SkImageFilter* imageFilter = paint.getImageFilter();
1483 SkSize blurSize;
1484 if (NULL != imageFilter && imageFilter->asABlur(&blurSize)) {
1485 GrAutoScratchTexture temp1, temp2;
1486 GrTexture* blurTexture = gaussianBlur(fContext,
1487 texture, &temp1, &temp2,
1488 GrRect::MakeWH(w, h),
1489 blurSize.width(),
1490 blurSize.height());
1491 texture = blurTexture;
1492 grPaint.setTexture(kBitmapTextureIdx, texture);
reed@google.com76dd2772012-01-05 21:15:07 +00001493 } else {
1494 grPaint.setTexture(kBitmapTextureIdx, texture);
1495 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001496
bsalomon@google.com5782d712011-01-21 21:03:59 +00001497 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001498 GrRect::MakeXYWH(GrIntToScalar(left),
1499 GrIntToScalar(top),
1500 GrIntToScalar(w),
1501 GrIntToScalar(h)),
1502 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1503 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001504}
1505
1506void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1507 int x, int y, const SkPaint& paint) {
1508 CHECK_SHOULD_DRAW(draw);
1509
bsalomon@google.com5782d712011-01-21 21:03:59 +00001510 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001511 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001512 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001513 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001514 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001515
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001516 GrTexture* devTex = grPaint.getTexture(0);
1517 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001518
1519 const SkBitmap& bm = dev->accessBitmap(false);
1520 int w = bm.width();
1521 int h = bm.height();
1522
1523 GrAutoMatrix avm(fContext, GrMatrix::I());
1524
bsalomon@google.com97912912011-12-06 16:30:36 +00001525 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001526
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001527 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1528 GrIntToScalar(y),
1529 GrIntToScalar(w),
1530 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001531
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001532 // The device being drawn may not fill up its texture (saveLayer uses
1533 // the approximate ).
1534 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1535 GR_Scalar1 * h / devTex->height());
1536
1537 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001538}
1539
reed@google.com76dd2772012-01-05 21:15:07 +00001540bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1541 const SkMatrix& ctm,
1542 SkBitmap* result, SkIPoint* offset) {
1543 SkSize size;
1544 if (!filter->asABlur(&size)) {
1545 return false;
1546 }
1547 SkDevice* dev = this->createCompatibleDevice(SkBitmap::kARGB_8888_Config,
1548 src.width(),
1549 src.height(),
1550 false);
1551 if (NULL == dev) {
1552 return false;
1553 }
1554 SkAutoUnref aur(dev);
1555 SkCanvas canvas(dev);
1556 SkPaint paint;
1557 paint.setImageFilter(filter);
1558 canvas.drawSprite(src, 0, 0, &paint);
1559 *result = dev->accessBitmap(false);
1560 return true;
1561}
1562
reed@google.comac10a2d2010-12-22 21:39:39 +00001563///////////////////////////////////////////////////////////////////////////////
1564
1565// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001566static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1567 kTriangles_PrimitiveType,
1568 kTriangleStrip_PrimitiveType,
1569 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001570};
1571
1572void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1573 int vertexCount, const SkPoint vertices[],
1574 const SkPoint texs[], const SkColor colors[],
1575 SkXfermode* xmode,
1576 const uint16_t indices[], int indexCount,
1577 const SkPaint& paint) {
1578 CHECK_SHOULD_DRAW(draw);
1579
bsalomon@google.com5782d712011-01-21 21:03:59 +00001580 GrPaint grPaint;
1581 SkAutoCachedTexture act;
1582 // we ignore the shader if texs is null.
1583 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001584 if (!this->skPaint2GrPaintNoShader(paint,
1585 false,
1586 &grPaint,
1587 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001588 return;
1589 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001590 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001591 if (!this->skPaint2GrPaintShader(paint, &act,
1592 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001593 &grPaint,
1594 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001595 return;
1596 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001597 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001598
1599 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001600 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001601 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1602#if 0
1603 return
1604#endif
1605 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001606 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001607
bsalomon@google.com498776a2011-08-16 19:20:44 +00001608 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1609 if (NULL != colors) {
1610 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001611 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001612 for (int i = 0; i < vertexCount; ++i) {
1613 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1614 }
1615 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001616 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001617 fContext->drawVertices(grPaint,
1618 gVertexMode2PrimitiveType[vmode],
1619 vertexCount,
1620 (GrPoint*) vertices,
1621 (GrPoint*) texs,
1622 colors,
1623 indices,
1624 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001625}
1626
1627///////////////////////////////////////////////////////////////////////////////
1628
1629static void GlyphCacheAuxProc(void* data) {
1630 delete (GrFontScaler*)data;
1631}
1632
1633static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1634 void* auxData;
1635 GrFontScaler* scaler = NULL;
1636 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1637 scaler = (GrFontScaler*)auxData;
1638 }
1639 if (NULL == scaler) {
1640 scaler = new SkGrFontScaler(cache);
1641 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1642 }
1643 return scaler;
1644}
1645
1646static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1647 SkFixed fx, SkFixed fy,
1648 const SkGlyph& glyph) {
1649 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1650
bungeman@google.com15865a72012-01-11 16:28:04 +00001651 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001652
1653 if (NULL == procs->fFontScaler) {
1654 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1655 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001656
bungeman@google.com15865a72012-01-11 16:28:04 +00001657 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1658 glyph.getSubXFixed(),
1659 glyph.getSubYFixed()),
1660 SkFixedFloorToFixed(fx),
1661 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001662 procs->fFontScaler);
1663}
1664
bsalomon@google.com5782d712011-01-21 21:03:59 +00001665SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001666
1667 // deferred allocation
1668 if (NULL == fDrawProcs) {
1669 fDrawProcs = new GrSkDrawProcs;
1670 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1671 fDrawProcs->fContext = fContext;
1672 }
1673
1674 // init our (and GL's) state
1675 fDrawProcs->fTextContext = context;
1676 fDrawProcs->fFontScaler = NULL;
1677 return fDrawProcs;
1678}
1679
1680void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1681 size_t byteLength, SkScalar x, SkScalar y,
1682 const SkPaint& paint) {
1683 CHECK_SHOULD_DRAW(draw);
1684
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001685 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001686 // this guy will just call our drawPath()
1687 draw.drawText((const char*)text, byteLength, x, y, paint);
1688 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001689 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001690
1691 GrPaint grPaint;
1692 SkAutoCachedTexture act;
1693
Scroggod757df22011-05-16 13:11:16 +00001694 if (!this->skPaint2GrPaintShader(paint,
1695 &act,
1696 *draw.fMatrix,
1697 &grPaint,
1698 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001699 return;
1700 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001701 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001702 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001703 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1704 }
1705}
1706
1707void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1708 size_t byteLength, const SkScalar pos[],
1709 SkScalar constY, int scalarsPerPos,
1710 const SkPaint& paint) {
1711 CHECK_SHOULD_DRAW(draw);
1712
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001713 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001714 // this guy will just call our drawPath()
1715 draw.drawPosText((const char*)text, byteLength, pos, constY,
1716 scalarsPerPos, paint);
1717 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001718 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001719
1720 GrPaint grPaint;
1721 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001722 if (!this->skPaint2GrPaintShader(paint,
1723 &act,
1724 *draw.fMatrix,
1725 &grPaint,
1726 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001727 return;
1728 }
1729
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001730 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001731 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001732 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1733 scalarsPerPos, paint);
1734 }
1735}
1736
1737void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1738 size_t len, const SkPath& path,
1739 const SkMatrix* m, const SkPaint& paint) {
1740 CHECK_SHOULD_DRAW(draw);
1741
1742 SkASSERT(draw.fDevice == this);
1743 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1744}
1745
1746///////////////////////////////////////////////////////////////////////////////
1747
reed@google.comf67e4cf2011-03-15 20:56:58 +00001748bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1749 if (!paint.isLCDRenderText()) {
1750 // we're cool with the paint as is
1751 return false;
1752 }
1753
1754 if (paint.getShader() ||
1755 paint.getXfermode() || // unless its srcover
1756 paint.getMaskFilter() ||
1757 paint.getRasterizer() ||
1758 paint.getColorFilter() ||
1759 paint.getPathEffect() ||
1760 paint.isFakeBoldText() ||
1761 paint.getStyle() != SkPaint::kFill_Style) {
1762 // turn off lcd
1763 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1764 flags->fHinting = paint.getHinting();
1765 return true;
1766 }
1767 // we're cool with the paint as is
1768 return false;
1769}
1770
reed@google.com75d939b2011-12-07 15:07:23 +00001771void SkGpuDevice::flush() {
1772 fContext->flush(false);
1773}
1774
reed@google.comf67e4cf2011-03-15 20:56:58 +00001775///////////////////////////////////////////////////////////////////////////////
1776
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001777SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001778 const GrSamplerState* sampler,
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001779 TexType type) {
1780 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001781 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001782
bsalomon@google.come97f0852011-06-17 13:10:25 +00001783 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001784 const GrTextureDesc desc = {
1785 kRenderTarget_GrTextureFlagBit,
1786 kNone_GrAALevel,
1787 bitmap.width(),
1788 bitmap.height(),
bsalomon@google.com5bc34f02011-12-06 14:46:34 +00001789 SkGr::Bitmap2PixelConfig(bitmap)
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001790 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001791 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001792 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001793 // we know layers will only be drawn through drawDevice.
1794 // drawDevice has been made to work with content embedded in a
1795 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001796 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001797 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001798 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001799 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001800 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001801 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001802 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001803 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001804 GrContext::TextureKey key = bitmap.getGenerationID();
1805 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001806
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001807 entry = ctx->findAndLockTexture(key, bitmap.width(),
1808 bitmap.height(), sampler);
1809 if (NULL == entry.texture()) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001810 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001811 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001812 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001813 } else {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001814 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY,
1815 sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001816 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001817 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001818 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1819 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001820 }
1821 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001822 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001823}
1824
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001825void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1826 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001827}
1828
bsalomon@google.comfb309512011-11-30 14:13:48 +00001829bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1830 const GrSamplerState& sampler) const {
1831 GrContext::TextureKey key = bitmap.getGenerationID();
1832 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
1833 return this->context()->isTextureInCache(key, bitmap.width(),
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001834 bitmap.height(), &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001835
1836}
1837
1838
bsalomon@google.come97f0852011-06-17 13:10:25 +00001839SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1840 int width, int height,
1841 bool isOpaque,
1842 Usage usage) {
1843 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1844 width, height, usage));
1845}
1846