blob: c27314a7729bf62065b23072b09ff42eaee90a7f [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
bsalomon@google.com84405e02012-03-05 19:57:21 +000059class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
60public:
61 SkAutoCachedTexture() { }
62 SkAutoCachedTexture(SkGpuDevice* device,
63 const SkBitmap& bitmap,
64 const GrSamplerState* sampler,
65 GrTexture** texture) {
66 GrAssert(texture);
67 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000068 }
reed@google.comac10a2d2010-12-22 21:39:39 +000069
bsalomon@google.com84405e02012-03-05 19:57:21 +000070 ~SkAutoCachedTexture() {
71 if (fTex.texture()) {
72 fDevice->unlockCachedTexture(fTex);
73 }
reed@google.comac10a2d2010-12-22 21:39:39 +000074 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000075
76 GrTexture* set(SkGpuDevice* device,
77 const SkBitmap& bitmap,
78 const GrSamplerState* sampler) {
79 if (fTex.texture()) {
80 fDevice->unlockCachedTexture(fTex);
81 }
82 fDevice = device;
83 GrTexture* texture = (GrTexture*)bitmap.getTexture();
84 if (texture) {
85 // return the native texture
86 fTex.reset();
87 } else {
88 // look it up in our cache
89 fTex = device->lockCachedTexture(bitmap, sampler);
90 texture = fTex.texture();
91 }
92 return texture;
93 }
94
95private:
96 SkGpuDevice* fDevice;
97 GrContext::TextureCacheEntry fTex;
98};
reed@google.comac10a2d2010-12-22 21:39:39 +000099
100///////////////////////////////////////////////////////////////////////////////
101
102bool gDoTraceDraw;
103
104struct GrSkDrawProcs : public SkDrawProcs {
105public:
106 GrContext* fContext;
107 GrTextContext* fTextContext;
108 GrFontScaler* fFontScaler; // cached in the skia glyphcache
109};
110
111///////////////////////////////////////////////////////////////////////////////
112
reed@google.comaf951c92011-06-16 19:10:39 +0000113static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
114 switch (config) {
115 case kAlpha_8_GrPixelConfig:
116 *isOpaque = false;
117 return SkBitmap::kA8_Config;
118 case kRGB_565_GrPixelConfig:
119 *isOpaque = true;
120 return SkBitmap::kRGB_565_Config;
121 case kRGBA_4444_GrPixelConfig:
122 *isOpaque = false;
123 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000124 case kSkia8888_PM_GrPixelConfig:
125 // we don't currently have a way of knowing whether
126 // a 8888 is opaque based on the config.
127 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000128 return SkBitmap::kARGB_8888_Config;
129 default:
130 *isOpaque = false;
131 return SkBitmap::kNo_Config;
132 }
133}
reed@google.comac10a2d2010-12-22 21:39:39 +0000134
reed@google.comaf951c92011-06-16 19:10:39 +0000135static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000136 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000137
138 bool isOpaque;
139 SkBitmap bitmap;
140 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
141 renderTarget->width(), renderTarget->height());
142 bitmap.setIsOpaque(isOpaque);
143 return bitmap;
144}
145
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000146SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
147: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
148 this->initFromRenderTarget(context, texture->asRenderTarget());
149}
150
reed@google.comaf951c92011-06-16 19:10:39 +0000151SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
152: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000153 this->initFromRenderTarget(context, renderTarget);
154}
155
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000156void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000157 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000158 fNeedPrepareRenderTarget = false;
159 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000160
reed@google.comaf951c92011-06-16 19:10:39 +0000161 fContext = context;
162 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000163
reed@google.comaf951c92011-06-16 19:10:39 +0000164 fTexture = NULL;
165 fRenderTarget = NULL;
166 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000167
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000168 GrAssert(NULL != renderTarget);
169 fRenderTarget = renderTarget;
170 fRenderTarget->ref();
171 // if this RT is also a texture, hold a ref on it
172 fTexture = fRenderTarget->asTexture();
173 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000174
175 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
176 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
177 // the device via accessBitmap. This external ref may outlive the device.
178 // Since textures own their render targets (but not vice-versa) we
179 // are ensuring that both objects will live as long as the pixel ref.
180 SkPixelRef* pr;
181 if (fTexture) {
182 pr = new SkGrTexturePixelRef(fTexture);
183 } else {
184 pr = new SkGrRenderTargetPixelRef(fRenderTarget);
185 }
reed@google.comaf951c92011-06-16 19:10:39 +0000186 this->setPixelRef(pr, 0)->unref();
187}
188
189SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000190 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000191: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000192 fNeedPrepareRenderTarget = false;
193 fDrawProcs = NULL;
194
reed@google.com7b201d22011-01-11 18:59:23 +0000195 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000196 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000197
reed@google.comac10a2d2010-12-22 21:39:39 +0000198 fTexture = NULL;
199 fRenderTarget = NULL;
200 fNeedClear = false;
201
reed@google.comaf951c92011-06-16 19:10:39 +0000202 if (config != SkBitmap::kRGB_565_Config) {
203 config = SkBitmap::kARGB_8888_Config;
204 }
205 SkBitmap bm;
206 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000207
208#if CACHE_LAYER_TEXTURES
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000209 TexType type = (kSaveLayer_Usage == usage) ?
bsalomon@google.come97f0852011-06-17 13:10:25 +0000210 kSaveLayerDeviceRenderTarget_TexType :
211 kDeviceRenderTarget_TexType;
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000212 fCache = this->lockCachedTexture(bm, NULL, type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000213 fTexture = fCache.texture();
214 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000215 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000216 // hold a ref directly on fTexture (even though fCache has one) to match
217 // other constructor paths. Simplifies cleanup.
218 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000219 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000220#else
reed@google.comaf951c92011-06-16 19:10:39 +0000221 const GrTextureDesc desc = {
222 kRenderTarget_GrTextureFlagBit,
reed@google.comaf951c92011-06-16 19:10:39 +0000223 width,
224 height,
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000225 SkGr::Bitmap2PixelConfig(bm),
226 {0} // samples
reed@google.comaf951c92011-06-16 19:10:39 +0000227 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000228
reed@google.comaf951c92011-06-16 19:10:39 +0000229 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000230#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000231 if (NULL != fTexture) {
232 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000233 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000234
reed@google.comaf951c92011-06-16 19:10:39 +0000235 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
reed@google.comaf951c92011-06-16 19:10:39 +0000237 // we defer the actual clear until our gainFocus()
238 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
reed@google.comaf951c92011-06-16 19:10:39 +0000240 // wrap the bitmap with a pixelref to expose our texture
241 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000242 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000243 } else {
244 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
245 width, height);
246 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000247 }
248}
249
250SkGpuDevice::~SkGpuDevice() {
251 if (fDrawProcs) {
252 delete fDrawProcs;
253 }
254
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000255 SkSafeUnref(fTexture);
256 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000257 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000258 GrAssert(NULL != fTexture);
259 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000260 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000261 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000262 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000263}
264
reed@google.comac10a2d2010-12-22 21:39:39 +0000265///////////////////////////////////////////////////////////////////////////////
266
267void SkGpuDevice::makeRenderTargetCurrent() {
268 fContext->setRenderTarget(fRenderTarget);
269 fContext->flush(true);
270 fNeedPrepareRenderTarget = true;
271}
272
273///////////////////////////////////////////////////////////////////////////////
274
bsalomon@google.comc4364992011-11-07 15:54:49 +0000275namespace {
276GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
277 switch (config8888) {
278 case SkCanvas::kNative_Premul_Config8888:
279 return kSkia8888_PM_GrPixelConfig;
280 case SkCanvas::kNative_Unpremul_Config8888:
281 return kSkia8888_UPM_GrPixelConfig;
282 case SkCanvas::kBGRA_Premul_Config8888:
283 return kBGRA_8888_PM_GrPixelConfig;
284 case SkCanvas::kBGRA_Unpremul_Config8888:
285 return kBGRA_8888_UPM_GrPixelConfig;
286 case SkCanvas::kRGBA_Premul_Config8888:
287 return kRGBA_8888_PM_GrPixelConfig;
288 case SkCanvas::kRGBA_Unpremul_Config8888:
289 return kRGBA_8888_UPM_GrPixelConfig;
290 default:
291 GrCrash("Unexpected Config8888.");
292 return kSkia8888_PM_GrPixelConfig;
293 }
294}
295}
296
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000297bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
298 int x, int y,
299 SkCanvas::Config8888 config8888) {
bsalomon@google.com910267d2011-11-02 20:06:25 +0000300 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
301 SkASSERT(!bitmap.isNull());
302 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000303
bsalomon@google.com910267d2011-11-02 20:06:25 +0000304 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000305 GrPixelConfig config;
306 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000307 return fContext->readRenderTargetPixels(fRenderTarget,
308 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000309 bitmap.width(),
310 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000311 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000312 bitmap.getPixels(),
313 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000314}
315
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000316void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
317 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000318 SkAutoLockPixels alp(bitmap);
319 if (!bitmap.readyToDraw()) {
320 return;
321 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000322
323 GrPixelConfig config;
324 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
325 config = config8888_to_gr_config(config8888);
326 } else {
327 config= SkGr::BitmapConfig2PixelConfig(bitmap.config(),
328 bitmap.isOpaque());
329 }
330
bsalomon@google.com6f379512011-11-16 20:36:03 +0000331 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
332 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000333}
334
335///////////////////////////////////////////////////////////////////////////////
336
337static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000338 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000339 const SkRegion& clipRegion,
340 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000341 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000342
343 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000344 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000345 const SkIRect& skBounds = clipRegion.getBounds();
346 GrRect bounds;
347 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
348 GrIntToScalar(skBounds.fTop),
349 GrIntToScalar(skBounds.fRight),
350 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000351 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
352 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000353 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000354}
355
356// call this ever each draw call, to ensure that the context reflects our state,
357// and not the state from some other canvas/device
358void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
359 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000360 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000361
362 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000363 SkASSERT(draw.fClipStack);
364 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000365 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000366 fNeedPrepareRenderTarget = false;
367 }
368}
369
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000370void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
371 const SkClipStack& clipStack) {
372 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
373 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000374 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000375}
376
377void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000378 const SkRegion& clip, const SkClipStack& clipStack) {
379
reed@google.comac10a2d2010-12-22 21:39:39 +0000380 fContext->setRenderTarget(fRenderTarget);
381
bsalomon@google.comd302f142011-03-03 13:54:13 +0000382 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000383
reed@google.com6f8f2922011-03-04 22:27:10 +0000384 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000385
386 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000387 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000388 fNeedClear = false;
389 }
390}
391
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000392SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
393 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000394}
395
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000396bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000397 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000398 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000399 return true;
400 }
401 return false;
402}
403
404///////////////////////////////////////////////////////////////////////////////
405
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000406SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
407SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
408SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
409SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
410SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
411 shader_type_mismatch);
412SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000413
bsalomon@google.com5782d712011-01-21 21:03:59 +0000414static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
415 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
416 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
417 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
418 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
419 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
420};
421
bsalomon@google.com84405e02012-03-05 19:57:21 +0000422namespace {
423
424// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
425// justAlpha indicates that skPaint's alpha should be used rather than the color
426// Callers may subsequently modify the GrPaint. Setting constantColor indicates
427// that the final paint will draw the same color at every pixel. This allows
428// an optimization where the the color filter can be applied to the skPaint's
429// color once while converting to GrPain and then ignored.
430inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
431 bool justAlpha,
432 bool constantColor,
433 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000434
435 grPaint->fDither = skPaint.isDither();
436 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000437 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000438
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000439 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
440 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000441
442 SkXfermode* mode = skPaint.getXfermode();
443 if (mode) {
444 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000445 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000446#if 0
447 return false;
448#endif
449 }
450 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000451 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
452 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
453
bsalomon@google.com5782d712011-01-21 21:03:59 +0000454 if (justAlpha) {
455 uint8_t alpha = skPaint.getAlpha();
456 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000457 // justAlpha is currently set to true only if there is a texture,
458 // so constantColor should not also be true.
459 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000460 } else {
461 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000462 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463 }
Scroggo97c88c22011-05-11 14:05:25 +0000464 SkColorFilter* colorFilter = skPaint.getColorFilter();
465 SkColor color;
466 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000467 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000468 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000469 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000470 if (!constantColor) {
471 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
472 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000473 } else {
474 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
475 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000476 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000477 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000478 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
479 grPaint->fColorMatrixEnabled = true;
480 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
481 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
482 } else {
483 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000484 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000485 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000486}
487
bsalomon@google.com84405e02012-03-05 19:57:21 +0000488// This function is similar to skPaint2GrPaintNoShader but also converts
489// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
490// be used is set on grPaint and returned in param act. constantColor has the
491// same meaning as in skPaint2GrPaintNoShader.
492inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
493 const SkPaint& skPaint,
494 const SkMatrix& ctm,
495 bool constantColor,
496 SkGpuDevice::SkAutoCachedTexture* act,
497 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000498
bsalomon@google.com5782d712011-01-21 21:03:59 +0000499 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000500
bsalomon@google.com5782d712011-01-21 21:03:59 +0000501 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000502 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000503 return skPaint2GrPaintNoShader(skPaint,
504 false,
505 constantColor,
506 grPaint);
507 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000508 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000509 }
510
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000512 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000513 SkShader::TileMode tileModes[2];
514 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000515 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000516 tileModes, twoPointParams);
517
bsalomon@google.com5782d712011-01-21 21:03:59 +0000518 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
519 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000520 SkShader::GradientInfo info;
521 SkColor color;
522
523 info.fColors = &color;
524 info.fColorOffsets = NULL;
525 info.fColorCount = 1;
526 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
527 SkPaint copy(skPaint);
528 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000529 // modulate the paint alpha by the shader's solid color alpha
530 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
531 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000532 return skPaint2GrPaintNoShader(copy,
533 false,
534 constantColor,
535 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000536 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000537 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000538 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000539 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000540 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000541 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000542 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000543 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000544 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000545 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000546 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
547 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000548 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000549 sampler->setRadial2Params(twoPointParams[0],
550 twoPointParams[1],
551 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000552 }
553
bsalomon@google.com84405e02012-03-05 19:57:21 +0000554 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000555 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000556 SkDebugf("Couldn't convert bitmap to texture.\n");
557 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000558 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000559 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000560
561 // since our texture coords will be in local space, we wack the texture
562 // matrix to map them back into 0...1 before we load it
563 SkMatrix localM;
564 if (shader->getLocalMatrix(&localM)) {
565 SkMatrix inverse;
566 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000567 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000568 }
569 }
570 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000571 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
572 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000573 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000574 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000575 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000576 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000577 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000578
579 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000580}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000581}
reed@google.comac10a2d2010-12-22 21:39:39 +0000582
583///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000584
bsalomon@google.com398109c2011-04-14 18:40:27 +0000585void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000586 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000587}
588
reed@google.comac10a2d2010-12-22 21:39:39 +0000589void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
590 CHECK_SHOULD_DRAW(draw);
591
bsalomon@google.com5782d712011-01-21 21:03:59 +0000592 GrPaint grPaint;
593 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000594 if (!skPaint2GrPaintShader(this,
595 paint,
596 *draw.fMatrix,
597 true,
598 &act,
599 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000600 return;
601 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000602
603 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000604}
605
606// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000607static const GrPrimitiveType gPointMode2PrimtiveType[] = {
608 kPoints_PrimitiveType,
609 kLines_PrimitiveType,
610 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000611};
612
613void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000614 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000615 CHECK_SHOULD_DRAW(draw);
616
617 SkScalar width = paint.getStrokeWidth();
618 if (width < 0) {
619 return;
620 }
621
622 // we only handle hairlines here, else we let the SkDraw call our drawPath()
623 if (width > 0) {
624 draw.drawPoints(mode, count, pts, paint, true);
625 return;
626 }
627
bsalomon@google.com5782d712011-01-21 21:03:59 +0000628 GrPaint grPaint;
629 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000630 if (!skPaint2GrPaintShader(this,
631 paint,
632 *draw.fMatrix,
633 true,
634 &act,
635 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000636 return;
637 }
638
bsalomon@google.com5782d712011-01-21 21:03:59 +0000639 fContext->drawVertices(grPaint,
640 gPointMode2PrimtiveType[mode],
641 count,
642 (GrPoint*)pts,
643 NULL,
644 NULL,
645 NULL,
646 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000647}
648
reed@google.comc9aa5872011-04-05 21:05:37 +0000649///////////////////////////////////////////////////////////////////////////////
650
reed@google.comac10a2d2010-12-22 21:39:39 +0000651void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
652 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000653 CHECK_SHOULD_DRAW(draw);
654
bungeman@google.com79bd8772011-07-18 15:34:08 +0000655 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000656 SkScalar width = paint.getStrokeWidth();
657
658 /*
659 We have special code for hairline strokes, miter-strokes, and fills.
660 Anything else we just call our path code.
661 */
662 bool usePath = doStroke && width > 0 &&
663 paint.getStrokeJoin() != SkPaint::kMiter_Join;
664 // another reason we might need to call drawPath...
665 if (paint.getMaskFilter()) {
666 usePath = true;
667 }
reed@google.com67db6642011-05-26 11:46:35 +0000668 // until we aa rotated rects...
669 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
670 usePath = true;
671 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000672 // small miter limit means right angles show bevel...
673 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
674 paint.getStrokeMiter() < SK_ScalarSqrt2)
675 {
676 usePath = true;
677 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000678 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000679 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
680 usePath = true;
681 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000682
683 if (usePath) {
684 SkPath path;
685 path.addRect(rect);
686 this->drawPath(draw, path, paint, NULL, true);
687 return;
688 }
689
690 GrPaint grPaint;
691 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000692 if (!skPaint2GrPaintShader(this,
693 paint,
694 *draw.fMatrix,
695 true,
696 &act,
697 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000698 return;
699 }
reed@google.com20efde72011-05-09 17:00:02 +0000700 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000701}
702
reed@google.com69302852011-02-16 18:08:07 +0000703#include "SkMaskFilter.h"
704#include "SkBounder.h"
705
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000706static GrPathFill skToGrFillType(SkPath::FillType fillType) {
707 switch (fillType) {
708 case SkPath::kWinding_FillType:
709 return kWinding_PathFill;
710 case SkPath::kEvenOdd_FillType:
711 return kEvenOdd_PathFill;
712 case SkPath::kInverseWinding_FillType:
713 return kInverseWinding_PathFill;
714 case SkPath::kInverseEvenOdd_FillType:
715 return kInverseEvenOdd_PathFill;
716 default:
717 SkDebugf("Unsupported path fill type\n");
718 return kHairLine_PathFill;
719 }
720}
721
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000722static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
723 SkMaskFilter* filter, const SkMatrix& matrix,
724 const SkRegion& clip, SkBounder* bounder,
725 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000726#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000727 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000728#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000729 SkMaskFilter::BlurInfo info;
730 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000731 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000732 return false;
733 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000734 SkScalar radius = info.fIgnoreTransform ? info.fRadius
735 : matrix.mapRadius(info.fRadius);
736 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000737 if (radius <= 0) {
738 return false;
739 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000740 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000741 float sigma3 = sigma * 3.0f;
742
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000743 SkRect srcRect = path.getBounds();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000744 SkRect clipRect;
745 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000746
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000747 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
748 srcRect.inset(-sigma3, -sigma3);
749 clipRect.inset(-sigma3, -sigma3);
750 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000751 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000752 SkIRect finalIRect;
753 finalRect.roundOut(&finalIRect);
754 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000755 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000756 }
757 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000758 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000759 }
760 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000761 srcRect.offset(offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000762 const GrTextureDesc desc = {
763 kRenderTarget_GrTextureFlagBit,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000764 srcRect.width(),
765 srcRect.height(),
766 // We actually only need A8, but it often isn't supported as a
767 // render target
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000768 kRGBA_8888_PM_GrPixelConfig,
769 {0} // samples
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000770 };
771
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000772 GrAutoScratchTexture pathEntry(context, desc);
773 GrTexture* pathTexture = pathEntry.texture();
774 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000775 return false;
776 }
777 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000778 // Once this code moves into GrContext, this should be changed to use
779 // an AutoClipRestore.
780 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000781 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000782 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000783 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000784 GrPaint tempPaint;
785 tempPaint.reset();
786
787 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000788 tempPaint.fAntiAlias = grp->fAntiAlias;
789 if (tempPaint.fAntiAlias) {
790 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
791 // blend coeff of zero requires dual source blending support in order
792 // to properly blend partially covered pixels. This means the AA
793 // code path may not be taken. So we use a dst blend coeff of ISA. We
794 // could special case AA draws to a dst surface with known alpha=0 to
795 // use a zero dst coeff when dual source blending isn't available.
796 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
797 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
798 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000799 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000800 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000801
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000802 GrAutoScratchTexture temp1, temp2;
803 // If we're doing a normal blur, we can clobber the pathTexture in the
804 // gaussianBlur. Otherwise, we need to save it for later compositing.
805 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000806 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
807 &temp1,
808 isNormalBlur ? NULL : &temp2,
809 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000810
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000811 if (!isNormalBlur) {
812 GrPaint paint;
813 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000814 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000815 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
816 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000817 // Blend pathTexture over blurTexture.
818 context->setRenderTarget(blurTexture->asRenderTarget());
819 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000820 if (SkMaskFilter::kInner_BlurType == blurType) {
821 // inner: dst = dst * src
822 paint.fSrcBlendCoeff = kDC_BlendCoeff;
823 paint.fDstBlendCoeff = kZero_BlendCoeff;
824 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
825 // solid: dst = src + dst - src * dst
826 // = (1 - dst) * src + 1 * dst
827 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
828 paint.fDstBlendCoeff = kOne_BlendCoeff;
829 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
830 // outer: dst = dst * (1 - src)
831 // = 0 * src + (1 - src) * dst
832 paint.fSrcBlendCoeff = kZero_BlendCoeff;
833 paint.fDstBlendCoeff = kISC_BlendCoeff;
834 }
835 context->drawRect(paint, srcRect);
836 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000837 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000838 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000839
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000840 if (grp->hasTextureOrMask()) {
841 GrMatrix inverse;
842 if (!matrix.invert(&inverse)) {
843 return false;
844 }
845 grp->preConcatActiveSamplerMatrices(inverse);
846 }
847
848 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
849 // we assume the last mask index is available for use
850 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000851 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000852 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000853
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000854 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
855 -finalRect.fTop);
856 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
857 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000858 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000859 return true;
860}
861
reed@google.com69302852011-02-16 18:08:07 +0000862static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
863 SkMaskFilter* filter, const SkMatrix& matrix,
864 const SkRegion& clip, SkBounder* bounder,
865 GrPaint* grp) {
866 SkMask srcM, dstM;
867
868 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
869 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
870 return false;
871 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000872 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000873
874 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
875 return false;
876 }
877 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000878 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000879
880 if (clip.quickReject(dstM.fBounds)) {
881 return false;
882 }
883 if (bounder && !bounder->doIRect(dstM.fBounds)) {
884 return false;
885 }
886
887 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
888 // the current clip (and identity matrix) and grpaint settings
889
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000890 // used to compute inverse view, if necessary
891 GrMatrix ivm = context->getMatrix();
892
reed@google.com0c219b62011-02-16 21:31:18 +0000893 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000894
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000895 const GrTextureDesc desc = {
896 kNone_GrTextureFlags,
reed@google.com69302852011-02-16 18:08:07 +0000897 dstM.fBounds.width(),
898 dstM.fBounds.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000899 kAlpha_8_GrPixelConfig,
900 {0}, // samples
reed@google.com69302852011-02-16 18:08:07 +0000901 };
902
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000903 GrAutoScratchTexture ast(context, desc);
904 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000905
reed@google.com69302852011-02-16 18:08:07 +0000906 if (NULL == texture) {
907 return false;
908 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000909 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000910 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000911
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000912 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
913 grp->preConcatActiveSamplerMatrices(ivm);
914 }
915
916 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
917 // we assume the last mask index is available for use
918 GrAssert(NULL == grp->getMask(MASK_IDX));
919 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000920 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000921
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000922 GrRect d;
923 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000924 GrIntToScalar(dstM.fBounds.fTop),
925 GrIntToScalar(dstM.fBounds.fRight),
926 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000927
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000928 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
929 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
930 -dstM.fBounds.fTop*SK_Scalar1);
931 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000932 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000933 return true;
934}
reed@google.com69302852011-02-16 18:08:07 +0000935
reed@google.com0c219b62011-02-16 21:31:18 +0000936void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000937 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000938 bool pathIsMutable) {
939 CHECK_SHOULD_DRAW(draw);
940
reed@google.comfe626382011-09-21 13:50:35 +0000941 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000942
bsalomon@google.com5782d712011-01-21 21:03:59 +0000943 GrPaint grPaint;
944 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000945 if (!skPaint2GrPaintShader(this,
946 paint,
947 *draw.fMatrix,
948 true,
949 &act,
950 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000951 return;
952 }
953
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000954 // can we cheat, and threat a thin stroke as a hairline w/ coverage
955 // if we can, we draw lots faster (raster device does this same test)
956 SkScalar hairlineCoverage;
957 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
958 doFill = false;
959 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
960 grPaint.fCoverage);
961 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000962
reed@google.comfe626382011-09-21 13:50:35 +0000963 // If we have a prematrix, apply it to the path, optimizing for the case
964 // where the original path can in fact be modified in place (even though
965 // its parameter type is const).
966 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
967 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000968
969 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +0000970 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +0000971
reed@google.come3445642011-02-16 23:20:39 +0000972 if (!pathIsMutable) {
973 result = &tmpPath;
974 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000975 }
reed@google.come3445642011-02-16 23:20:39 +0000976 // should I push prePathMatrix on our MV stack temporarily, instead
977 // of applying it here? See SkDraw.cpp
978 pathPtr->transform(*prePathMatrix, result);
979 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +0000980 }
reed@google.com0c219b62011-02-16 21:31:18 +0000981 // at this point we're done with prePathMatrix
982 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +0000983
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +0000984 if (paint.getPathEffect() ||
985 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +0000986 // it is safe to use tmpPath here, even if we already used it for the
987 // prepathmatrix, since getFillPath can take the same object for its
988 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000989 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +0000990 pathPtr = &tmpPath;
991 }
992
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000993 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +0000994 // avoid possibly allocating a new path in transform if we can
995 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
996
997 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +0000998 pathPtr->transform(*draw.fMatrix, devPathPtr);
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000999 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001000 *draw.fMatrix, *draw.fClip, draw.fBounder,
1001 &grPaint)) {
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001002 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001003 *draw.fMatrix, *draw.fClip, draw.fBounder,
1004 &grPaint);
1005 }
reed@google.com69302852011-02-16 18:08:07 +00001006 return;
1007 }
reed@google.com69302852011-02-16 18:08:07 +00001008
bsalomon@google.comffca4002011-02-22 20:34:01 +00001009 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001010
reed@google.com0c219b62011-02-16 21:31:18 +00001011 if (doFill) {
1012 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001013 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001014 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001015 break;
1016 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001017 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001018 break;
1019 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001020 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001021 break;
1022 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001023 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001024 break;
1025 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001026 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001027 return;
1028 }
1029 }
1030
reed@google.com07f3ee12011-05-16 17:21:57 +00001031 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001032}
1033
bsalomon@google.comfb309512011-11-30 14:13:48 +00001034namespace {
1035
1036inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1037 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1038 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1039 return tilesX * tilesY;
1040}
1041
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001042inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001043 const SkIRect* srcRectPtr,
1044 int maxTextureSize) {
1045 static const int kSmallTileSize = 1 << 10;
1046 if (maxTextureSize <= kSmallTileSize) {
1047 return maxTextureSize;
1048 }
1049
1050 size_t maxTexTotalTileSize;
1051 size_t smallTotalTileSize;
1052
1053 if (NULL == srcRectPtr) {
1054 int w = bitmap.width();
1055 int h = bitmap.height();
1056 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1057 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1058 } else {
1059 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1060 srcRectPtr->fTop,
1061 srcRectPtr->fRight,
1062 srcRectPtr->fBottom,
1063 maxTextureSize);
1064 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1065 srcRectPtr->fTop,
1066 srcRectPtr->fRight,
1067 srcRectPtr->fBottom,
1068 kSmallTileSize);
1069 }
1070 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1071 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1072
1073 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1074 return kSmallTileSize;
1075 } else {
1076 return maxTextureSize;
1077 }
1078}
1079}
1080
1081bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1082 const GrSamplerState& sampler,
1083 const SkIRect* srcRectPtr,
1084 int* tileSize) const {
1085 SkASSERT(NULL != tileSize);
1086
1087 // if bitmap is explictly texture backed then just use the texture
1088 if (NULL != bitmap.getTexture()) {
1089 return false;
1090 }
1091 // if it's larger than the max texture size, then we have no choice but
1092 // tiling
1093 const int maxTextureSize = fContext->getMaxTextureSize();
1094 if (bitmap.width() > maxTextureSize ||
1095 bitmap.height() > maxTextureSize) {
1096 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1097 return true;
1098 }
1099 // if we are going to have to draw the whole thing, then don't tile
1100 if (NULL == srcRectPtr) {
1101 return false;
1102 }
1103 // if the entire texture is already in our cache then no reason to tile it
1104 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1105 return false;
1106 }
1107
1108 // At this point we know we could do the draw by uploading the entire bitmap
1109 // as a texture. However, if the texture would be large compared to the
1110 // cache size and we don't require most of it for this draw then tile to
1111 // reduce the amount of upload and cache spill.
1112
1113 // assumption here is that sw bitmap size is a good proxy for its size as
1114 // a texture
1115 size_t bmpSize = bitmap.getSize();
1116 size_t cacheSize;
1117 fContext->getTextureCacheLimits(NULL, &cacheSize);
1118 if (bmpSize < cacheSize / 2) {
1119 return false;
1120 }
1121
1122 SkFixed fracUsed =
1123 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1124 (srcRectPtr->height() << 16) / bitmap.height());
1125 if (fracUsed <= SK_FixedHalf) {
1126 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1127 return true;
1128 } else {
1129 return false;
1130 }
1131}
1132
reed@google.comac10a2d2010-12-22 21:39:39 +00001133void SkGpuDevice::drawBitmap(const SkDraw& draw,
1134 const SkBitmap& bitmap,
1135 const SkIRect* srcRectPtr,
1136 const SkMatrix& m,
1137 const SkPaint& paint) {
1138 CHECK_SHOULD_DRAW(draw);
1139
1140 SkIRect srcRect;
1141 if (NULL == srcRectPtr) {
1142 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1143 } else {
1144 srcRect = *srcRectPtr;
1145 }
1146
junov@google.comd935cfb2011-06-27 20:48:23 +00001147 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001148 // Convert the bitmap to a shader so that the rect can be drawn
1149 // through drawRect, which supports mask filters.
1150 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001151 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001152 if (srcRectPtr) {
1153 if (!bitmap.extractSubset(&tmp, srcRect)) {
1154 return; // extraction failed
1155 }
1156 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001157 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001158 }
1159 SkPaint paintWithTexture(paint);
1160 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1161 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001162 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001163 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001164
junov@google.com1d329782011-07-28 20:10:09 +00001165 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001166 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001167 // also affect the behavior of the mask filter.
1168 SkMatrix drawMatrix;
1169 drawMatrix.setConcat(*draw.fMatrix, m);
1170 SkDraw transformedDraw(draw);
1171 transformedDraw.fMatrix = &drawMatrix;
1172
1173 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1174
junov@google.comd935cfb2011-06-27 20:48:23 +00001175 return;
1176 }
1177
bsalomon@google.com5782d712011-01-21 21:03:59 +00001178 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001179 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001180 return;
1181 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001182 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001183 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001184 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001185 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001186 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001187 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001188
bsalomon@google.comfb309512011-11-30 14:13:48 +00001189 int tileSize;
1190 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1191 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001192 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001193 return;
1194 }
1195
1196 // undo the translate done by SkCanvas
1197 int DX = SkMax32(0, srcRect.fLeft);
1198 int DY = SkMax32(0, srcRect.fTop);
1199 // compute clip bounds in local coordinates
1200 SkIRect clipRect;
1201 {
1202 SkRect r;
1203 r.set(draw.fClip->getBounds());
1204 SkMatrix matrix, inverse;
1205 matrix.setConcat(*draw.fMatrix, m);
1206 if (!matrix.invert(&inverse)) {
1207 return;
1208 }
1209 inverse.mapRect(&r);
1210 r.roundOut(&clipRect);
1211 // apply the canvas' translate to our local clip
1212 clipRect.offset(DX, DY);
1213 }
1214
bsalomon@google.comfb309512011-11-30 14:13:48 +00001215 int nx = bitmap.width() / tileSize;
1216 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001217 for (int x = 0; x <= nx; x++) {
1218 for (int y = 0; y <= ny; y++) {
1219 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001220 tileR.set(x * tileSize, y * tileSize,
1221 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001222 if (!SkIRect::Intersects(tileR, clipRect)) {
1223 continue;
1224 }
1225
1226 SkIRect srcR = tileR;
1227 if (!srcR.intersect(srcRect)) {
1228 continue;
1229 }
1230
1231 SkBitmap tmpB;
1232 if (bitmap.extractSubset(&tmpB, tileR)) {
1233 // now offset it to make it "local" to our tmp bitmap
1234 srcR.offset(-tileR.fLeft, -tileR.fTop);
1235
1236 SkMatrix tmpM(m);
1237 {
1238 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1239 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1240 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1241 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001242 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001243 }
1244 }
1245 }
1246}
1247
1248/*
1249 * This is called by drawBitmap(), which has to handle images that may be too
1250 * large to be represented by a single texture.
1251 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001252 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1253 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001254 */
1255void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1256 const SkBitmap& bitmap,
1257 const SkIRect& srcRect,
1258 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001259 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001260 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1261 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001262
reed@google.com9c49bc32011-07-07 13:42:37 +00001263 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001264 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001265 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001266 return;
1267 }
1268
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001269 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001270
1271 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1272 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1273 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001274 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001275
1276 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001277 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 if (NULL == texture) {
1279 return;
1280 }
1281
bsalomon@google.com452943d2011-10-31 17:37:14 +00001282 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001283
reed@google.com20efde72011-05-09 17:00:02 +00001284 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1285 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001286 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001287 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1288 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1289 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001290 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001291
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001292 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001293 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001294 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001295 // If drawing a subrect of the bitmap and filtering is enabled,
1296 // use a constrained texture domain to avoid color bleeding
1297 GrScalar left, top, right, bottom;
1298 if (srcRect.width() > 1) {
1299 GrScalar border = GR_ScalarHalf / bitmap.width();
1300 left = paintRect.left() + border;
1301 right = paintRect.right() - border;
1302 } else {
1303 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1304 }
1305 if (srcRect.height() > 1) {
1306 GrScalar border = GR_ScalarHalf / bitmap.height();
1307 top = paintRect.top() + border;
1308 bottom = paintRect.bottom() - border;
1309 } else {
1310 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1311 }
1312 GrRect textureDomain;
1313 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001314 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001315 }
1316
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001317 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001318}
1319
1320void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1321 int left, int top, const SkPaint& paint) {
1322 CHECK_SHOULD_DRAW(draw);
1323
1324 SkAutoLockPixels alp(bitmap);
1325 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1326 return;
1327 }
1328
reed@google.com76dd2772012-01-05 21:15:07 +00001329 int w = bitmap.width();
1330 int h = bitmap.height();
1331
bsalomon@google.com5782d712011-01-21 21:03:59 +00001332 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001333 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001334 return;
1335 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001336
bsalomon@google.com5782d712011-01-21 21:03:59 +00001337 GrAutoMatrix avm(fContext, GrMatrix::I());
1338
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001339 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001340
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001341 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001342 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001343 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001344
reed@google.com76dd2772012-01-05 21:15:07 +00001345 SkImageFilter* imageFilter = paint.getImageFilter();
1346 SkSize blurSize;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001347 SkISize radius;
reed@google.com76dd2772012-01-05 21:15:07 +00001348 if (NULL != imageFilter && imageFilter->asABlur(&blurSize)) {
1349 GrAutoScratchTexture temp1, temp2;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001350 GrTexture* blurTexture = fContext->gaussianBlur(texture, &temp1, &temp2,
1351 GrRect::MakeWH(w, h),
1352 blurSize.width(),
1353 blurSize.height());
reed@google.com76dd2772012-01-05 21:15:07 +00001354 texture = blurTexture;
1355 grPaint.setTexture(kBitmapTextureIdx, texture);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001356 } else if (NULL != imageFilter && imageFilter->asADilate(&radius)) {
1357 const GrTextureDesc desc = {
1358 kRenderTarget_GrTextureFlagBit,
1359 w,
1360 h,
1361 kRGBA_8888_PM_GrPixelConfig,
1362 {0} // samples
1363 };
1364 GrAutoScratchTexture temp1(fContext, desc), temp2(fContext, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001365 texture = fContext->applyMorphology(texture, GrRect::MakeWH(w, h),
1366 temp1.texture(), temp2.texture(),
1367 GrSamplerState::kDilate_Filter,
1368 radius);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001369 grPaint.setTexture(kBitmapTextureIdx, texture);
1370 } else if (NULL != imageFilter && imageFilter->asAnErode(&radius)) {
1371 const GrTextureDesc desc = {
1372 kRenderTarget_GrTextureFlagBit,
1373 w,
1374 h,
1375 kRGBA_8888_PM_GrPixelConfig,
1376 {0} // samples
1377 };
1378 GrAutoScratchTexture temp1(fContext, desc), temp2(fContext, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001379 texture = fContext->applyMorphology(texture, GrRect::MakeWH(w, h),
1380 temp1.texture(), temp2.texture(),
1381 GrSamplerState::kErode_Filter,
1382 radius);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001383 grPaint.setTexture(kBitmapTextureIdx, texture);
reed@google.com76dd2772012-01-05 21:15:07 +00001384 } else {
1385 grPaint.setTexture(kBitmapTextureIdx, texture);
1386 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001387
bsalomon@google.com5782d712011-01-21 21:03:59 +00001388 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001389 GrRect::MakeXYWH(GrIntToScalar(left),
1390 GrIntToScalar(top),
1391 GrIntToScalar(w),
1392 GrIntToScalar(h)),
1393 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1394 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001395}
1396
1397void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1398 int x, int y, const SkPaint& paint) {
1399 CHECK_SHOULD_DRAW(draw);
1400
bsalomon@google.com5782d712011-01-21 21:03:59 +00001401 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001402 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001403 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001404 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001405 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001406
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001407 GrTexture* devTex = grPaint.getTexture(0);
1408 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001409
1410 const SkBitmap& bm = dev->accessBitmap(false);
1411 int w = bm.width();
1412 int h = bm.height();
1413
1414 GrAutoMatrix avm(fContext, GrMatrix::I());
1415
bsalomon@google.com97912912011-12-06 16:30:36 +00001416 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001417
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001418 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1419 GrIntToScalar(y),
1420 GrIntToScalar(w),
1421 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001422
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001423 // The device being drawn may not fill up its texture (saveLayer uses
1424 // the approximate ).
1425 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1426 GR_Scalar1 * h / devTex->height());
1427
1428 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001429}
1430
reed@google.com76dd2772012-01-05 21:15:07 +00001431bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1432 const SkMatrix& ctm,
1433 SkBitmap* result, SkIPoint* offset) {
1434 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001435 SkISize radius;
1436 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001437 return false;
1438 }
1439 SkDevice* dev = this->createCompatibleDevice(SkBitmap::kARGB_8888_Config,
1440 src.width(),
1441 src.height(),
1442 false);
1443 if (NULL == dev) {
1444 return false;
1445 }
1446 SkAutoUnref aur(dev);
1447 SkCanvas canvas(dev);
1448 SkPaint paint;
1449 paint.setImageFilter(filter);
1450 canvas.drawSprite(src, 0, 0, &paint);
1451 *result = dev->accessBitmap(false);
1452 return true;
1453}
1454
reed@google.comac10a2d2010-12-22 21:39:39 +00001455///////////////////////////////////////////////////////////////////////////////
1456
1457// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001458static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1459 kTriangles_PrimitiveType,
1460 kTriangleStrip_PrimitiveType,
1461 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001462};
1463
1464void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1465 int vertexCount, const SkPoint vertices[],
1466 const SkPoint texs[], const SkColor colors[],
1467 SkXfermode* xmode,
1468 const uint16_t indices[], int indexCount,
1469 const SkPaint& paint) {
1470 CHECK_SHOULD_DRAW(draw);
1471
bsalomon@google.com5782d712011-01-21 21:03:59 +00001472 GrPaint grPaint;
1473 SkAutoCachedTexture act;
1474 // we ignore the shader if texs is null.
1475 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001476 if (!skPaint2GrPaintNoShader(paint,
1477 false,
1478 NULL == colors,
1479 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001480 return;
1481 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001482 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001483 if (!skPaint2GrPaintShader(this,
1484 paint,
1485 *draw.fMatrix,
1486 NULL == colors,
1487 &act,
1488 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001489 return;
1490 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001491 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001492
1493 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001494 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001495 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1496#if 0
1497 return
1498#endif
1499 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001500 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001501
bsalomon@google.com498776a2011-08-16 19:20:44 +00001502 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1503 if (NULL != colors) {
1504 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001505 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001506 for (int i = 0; i < vertexCount; ++i) {
1507 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1508 }
1509 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001510 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001511 fContext->drawVertices(grPaint,
1512 gVertexMode2PrimitiveType[vmode],
1513 vertexCount,
1514 (GrPoint*) vertices,
1515 (GrPoint*) texs,
1516 colors,
1517 indices,
1518 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001519}
1520
1521///////////////////////////////////////////////////////////////////////////////
1522
1523static void GlyphCacheAuxProc(void* data) {
1524 delete (GrFontScaler*)data;
1525}
1526
1527static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1528 void* auxData;
1529 GrFontScaler* scaler = NULL;
1530 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1531 scaler = (GrFontScaler*)auxData;
1532 }
1533 if (NULL == scaler) {
1534 scaler = new SkGrFontScaler(cache);
1535 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1536 }
1537 return scaler;
1538}
1539
1540static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1541 SkFixed fx, SkFixed fy,
1542 const SkGlyph& glyph) {
1543 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1544
bungeman@google.com15865a72012-01-11 16:28:04 +00001545 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001546
1547 if (NULL == procs->fFontScaler) {
1548 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1549 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001550
bungeman@google.com15865a72012-01-11 16:28:04 +00001551 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1552 glyph.getSubXFixed(),
1553 glyph.getSubYFixed()),
1554 SkFixedFloorToFixed(fx),
1555 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001556 procs->fFontScaler);
1557}
1558
bsalomon@google.com5782d712011-01-21 21:03:59 +00001559SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001560
1561 // deferred allocation
1562 if (NULL == fDrawProcs) {
1563 fDrawProcs = new GrSkDrawProcs;
1564 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1565 fDrawProcs->fContext = fContext;
1566 }
1567
1568 // init our (and GL's) state
1569 fDrawProcs->fTextContext = context;
1570 fDrawProcs->fFontScaler = NULL;
1571 return fDrawProcs;
1572}
1573
1574void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1575 size_t byteLength, SkScalar x, SkScalar y,
1576 const SkPaint& paint) {
1577 CHECK_SHOULD_DRAW(draw);
1578
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001579 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001580 // this guy will just call our drawPath()
1581 draw.drawText((const char*)text, byteLength, x, y, paint);
1582 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001583 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001584
1585 GrPaint grPaint;
1586 SkAutoCachedTexture act;
1587
bsalomon@google.com84405e02012-03-05 19:57:21 +00001588 if (!skPaint2GrPaintShader(this,
1589 paint,
1590 *draw.fMatrix,
1591 true,
1592 &act,
1593 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001594 return;
1595 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001596 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001597 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001598 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1599 }
1600}
1601
1602void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1603 size_t byteLength, const SkScalar pos[],
1604 SkScalar constY, int scalarsPerPos,
1605 const SkPaint& paint) {
1606 CHECK_SHOULD_DRAW(draw);
1607
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001608 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001609 // this guy will just call our drawPath()
1610 draw.drawPosText((const char*)text, byteLength, pos, constY,
1611 scalarsPerPos, paint);
1612 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001613 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001614
1615 GrPaint grPaint;
1616 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001617 if (!skPaint2GrPaintShader(this,
1618 paint,
1619 *draw.fMatrix,
1620 true,
1621 &act,
1622 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001623 return;
1624 }
1625
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001626 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001627 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001628 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1629 scalarsPerPos, paint);
1630 }
1631}
1632
1633void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1634 size_t len, const SkPath& path,
1635 const SkMatrix* m, const SkPaint& paint) {
1636 CHECK_SHOULD_DRAW(draw);
1637
1638 SkASSERT(draw.fDevice == this);
1639 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1640}
1641
1642///////////////////////////////////////////////////////////////////////////////
1643
reed@google.comf67e4cf2011-03-15 20:56:58 +00001644bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1645 if (!paint.isLCDRenderText()) {
1646 // we're cool with the paint as is
1647 return false;
1648 }
1649
1650 if (paint.getShader() ||
1651 paint.getXfermode() || // unless its srcover
1652 paint.getMaskFilter() ||
1653 paint.getRasterizer() ||
1654 paint.getColorFilter() ||
1655 paint.getPathEffect() ||
1656 paint.isFakeBoldText() ||
1657 paint.getStyle() != SkPaint::kFill_Style) {
1658 // turn off lcd
1659 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1660 flags->fHinting = paint.getHinting();
1661 return true;
1662 }
1663 // we're cool with the paint as is
1664 return false;
1665}
1666
reed@google.com75d939b2011-12-07 15:07:23 +00001667void SkGpuDevice::flush() {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001668 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001669}
1670
reed@google.comf67e4cf2011-03-15 20:56:58 +00001671///////////////////////////////////////////////////////////////////////////////
1672
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001673SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001674 const GrSamplerState* sampler,
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001675 TexType type) {
1676 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001677 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001678
bsalomon@google.come97f0852011-06-17 13:10:25 +00001679 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001680 const GrTextureDesc desc = {
1681 kRenderTarget_GrTextureFlagBit,
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001682 bitmap.width(),
1683 bitmap.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001684 SkGr::Bitmap2PixelConfig(bitmap),
1685 {0} // samples
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001686 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001687 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001688 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001689 // we know layers will only be drawn through drawDevice.
1690 // drawDevice has been made to work with content embedded in a
1691 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001692 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001693 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001694 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001695 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001696 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001697 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001698 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001699 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001700 GrContext::TextureKey key = bitmap.getGenerationID();
1701 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001702
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001703 entry = ctx->findAndLockTexture(key, bitmap.width(),
1704 bitmap.height(), sampler);
1705 if (NULL == entry.texture()) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001706 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001707 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001708 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001709 } else {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001710 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY,
1711 sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001712 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001713 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001714 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1715 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001716 }
1717 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001718 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001719}
1720
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001721void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1722 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001723}
1724
bsalomon@google.comfb309512011-11-30 14:13:48 +00001725bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1726 const GrSamplerState& sampler) const {
1727 GrContext::TextureKey key = bitmap.getGenerationID();
1728 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
1729 return this->context()->isTextureInCache(key, bitmap.width(),
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001730 bitmap.height(), &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001731
1732}
1733
1734
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001735SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1736 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001737 bool isOpaque,
1738 Usage usage) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001739 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001740 width, height, usage));
1741}
1742