blob: 496c096de5ec25f288734b965790b7bba6baf8c5 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
tomhudson@google.com898e7b52012-06-01 20:42:15 +00008#include "SkGpuDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00009
twiz@google.com58071162012-07-18 21:41:50 +000010#include "effects/GrColorTableEffect.h"
tomhudson@google.com2f68e762012-07-17 18:43:21 +000011#include "effects/GrTextureDomainEffect.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000012
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrContext.h"
14#include "GrTextContext.h"
15
robertphillips@google.come9c04692012-06-29 00:30:13 +000016#include "SkGrTexturePixelRef.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
Scroggo97c88c22011-05-11 14:05:25 +000018#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019#include "SkDrawProcs.h"
20#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000021#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000022#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000023#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.com06cd7322012-03-30 18:45:35 +000025#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000026
27#if 0
28 extern bool (*gShouldDrawProc)();
29 #define CHECK_SHOULD_DRAW(draw) \
30 do { \
31 if (gShouldDrawProc && !gShouldDrawProc()) return; \
32 this->prepareRenderTarget(draw); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000033 GrAssert(!fNeedClear) \
reed@google.comac10a2d2010-12-22 21:39:39 +000034 } while (0)
35#else
bsalomon@google.com06cd7322012-03-30 18:45:35 +000036 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \
37 GrAssert(!fNeedClear)
reed@google.comac10a2d2010-12-22 21:39:39 +000038#endif
39
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000040// we use the same texture slot on GrPaint for bitmaps and shaders
41// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
42enum {
43 kBitmapTextureIdx = 0,
twiz@google.com58071162012-07-18 21:41:50 +000044 kShaderTextureIdx = 0,
45 kColorFilterTextureIdx = 1
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000046};
47
reed@google.comcde92112011-07-06 20:00:52 +000048
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000049#define MAX_BLUR_SIGMA 4.0f
50// FIXME: This value comes from from SkBlurMaskFilter.cpp.
51// Should probably be put in a common header someplace.
52#define MAX_BLUR_RADIUS SkIntToScalar(128)
53// This constant approximates the scaling done in the software path's
54// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
55// IMHO, it actually should be 1: we blur "less" than we should do
56// according to the CSS and canvas specs, simply because Safari does the same.
57// Firefox used to do the same too, until 4.0 where they fixed it. So at some
58// point we should probably get rid of these scaling constants and rebaseline
59// all the blur tests.
60#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000061// This constant represents the screen alignment criterion in texels for
62// requiring texture domain clamping to prevent color bleeding when drawing
63// a sub region of a larger source image.
64#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000065
66#define DO_DEFERRED_CLEAR \
67 do { \
68 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000069 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000070 fNeedClear = false; \
71 } \
72 } while (false) \
73
reed@google.comac10a2d2010-12-22 21:39:39 +000074///////////////////////////////////////////////////////////////////////////////
75
reed@google.comb0a34d82012-07-11 19:57:55 +000076#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
77 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
78
79///////////////////////////////////////////////////////////////////////////////
80
81
bsalomon@google.com84405e02012-03-05 19:57:21 +000082class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
83public:
84 SkAutoCachedTexture() { }
85 SkAutoCachedTexture(SkGpuDevice* device,
86 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000087 const GrTextureParams* params,
bsalomon@google.com84405e02012-03-05 19:57:21 +000088 GrTexture** texture) {
89 GrAssert(texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000090 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000091 }
reed@google.comac10a2d2010-12-22 21:39:39 +000092
bsalomon@google.com84405e02012-03-05 19:57:21 +000093 ~SkAutoCachedTexture() {
94 if (fTex.texture()) {
rileya@google.com24f3ad12012-07-18 21:47:40 +000095 GrUnlockCachedBitmapTexture(fDevice->context(), fTex);
bsalomon@google.com84405e02012-03-05 19:57:21 +000096 }
reed@google.comac10a2d2010-12-22 21:39:39 +000097 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000098
99 GrTexture* set(SkGpuDevice* device,
100 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000101 const GrTextureParams* params) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000102 if (fTex.texture()) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000103 GrUnlockCachedBitmapTexture(fDevice->context(), fTex);
bsalomon@google.com84405e02012-03-05 19:57:21 +0000104 }
105 fDevice = device;
106 GrTexture* texture = (GrTexture*)bitmap.getTexture();
107 if (texture) {
108 // return the native texture
109 fTex.reset();
110 } else {
111 // look it up in our cache
bsalomon@google.comb8670992012-07-25 21:27:09 +0000112 fTex = GrLockCachedBitmapTexture(device->context(), bitmap, params);
bsalomon@google.com84405e02012-03-05 19:57:21 +0000113 texture = fTex.texture();
114 }
115 return texture;
116 }
117
118private:
119 SkGpuDevice* fDevice;
120 GrContext::TextureCacheEntry fTex;
121};
reed@google.comac10a2d2010-12-22 21:39:39 +0000122
123///////////////////////////////////////////////////////////////////////////////
124
125bool gDoTraceDraw;
126
127struct GrSkDrawProcs : public SkDrawProcs {
128public:
129 GrContext* fContext;
130 GrTextContext* fTextContext;
131 GrFontScaler* fFontScaler; // cached in the skia glyphcache
132};
133
134///////////////////////////////////////////////////////////////////////////////
135
reed@google.comaf951c92011-06-16 19:10:39 +0000136static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
137 switch (config) {
138 case kAlpha_8_GrPixelConfig:
139 *isOpaque = false;
140 return SkBitmap::kA8_Config;
141 case kRGB_565_GrPixelConfig:
142 *isOpaque = true;
143 return SkBitmap::kRGB_565_Config;
144 case kRGBA_4444_GrPixelConfig:
145 *isOpaque = false;
146 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000147 case kSkia8888_PM_GrPixelConfig:
148 // we don't currently have a way of knowing whether
149 // a 8888 is opaque based on the config.
150 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000151 return SkBitmap::kARGB_8888_Config;
152 default:
153 *isOpaque = false;
154 return SkBitmap::kNo_Config;
155 }
156}
reed@google.comac10a2d2010-12-22 21:39:39 +0000157
reed@google.comaf951c92011-06-16 19:10:39 +0000158static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000159 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000160
161 bool isOpaque;
162 SkBitmap bitmap;
163 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
164 renderTarget->width(), renderTarget->height());
165 bitmap.setIsOpaque(isOpaque);
166 return bitmap;
167}
168
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000169SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000170: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000171 this->initFromRenderTarget(context, texture->asRenderTarget());
172}
173
reed@google.comaf951c92011-06-16 19:10:39 +0000174SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000175: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000176 this->initFromRenderTarget(context, renderTarget);
177}
178
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000179void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000180 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000181 fNeedPrepareRenderTarget = false;
182 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000183
reed@google.comaf951c92011-06-16 19:10:39 +0000184 fContext = context;
185 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000186
reed@google.comaf951c92011-06-16 19:10:39 +0000187 fTexture = NULL;
188 fRenderTarget = NULL;
189 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000190
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000191 GrAssert(NULL != renderTarget);
192 fRenderTarget = renderTarget;
193 fRenderTarget->ref();
194 // if this RT is also a texture, hold a ref on it
195 fTexture = fRenderTarget->asTexture();
196 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000197
198 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
199 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
200 // the device via accessBitmap. This external ref may outlive the device.
201 // Since textures own their render targets (but not vice-versa) we
202 // are ensuring that both objects will live as long as the pixel ref.
203 SkPixelRef* pr;
204 if (fTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000205 pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000206 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000207 pr = SkNEW_ARGS(SkGrRenderTargetPixelRef, (fRenderTarget));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000208 }
reed@google.comaf951c92011-06-16 19:10:39 +0000209 this->setPixelRef(pr, 0)->unref();
210}
211
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000212SkGpuDevice::SkGpuDevice(GrContext* context,
213 SkBitmap::Config config,
214 int width,
215 int height)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000216 : SkDevice(config, width, height, false /*isOpaque*/) {
217
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 fNeedPrepareRenderTarget = false;
219 fDrawProcs = NULL;
220
reed@google.com7b201d22011-01-11 18:59:23 +0000221 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000222 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000223
reed@google.comac10a2d2010-12-22 21:39:39 +0000224 fTexture = NULL;
225 fRenderTarget = NULL;
226 fNeedClear = false;
227
reed@google.comaf951c92011-06-16 19:10:39 +0000228 if (config != SkBitmap::kRGB_565_Config) {
229 config = SkBitmap::kARGB_8888_Config;
230 }
231 SkBitmap bm;
232 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000234 GrTextureDesc desc;
235 desc.fFlags = kRenderTarget_GrTextureFlagBit;
236 desc.fWidth = width;
237 desc.fHeight = height;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000238 desc.fConfig = SkBitmapConfig2GrPixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
reed@google.comaf951c92011-06-16 19:10:39 +0000240 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000241
reed@google.comaf951c92011-06-16 19:10:39 +0000242 if (NULL != fTexture) {
243 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000244 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000245
reed@google.comaf951c92011-06-16 19:10:39 +0000246 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000247
reed@google.comaf951c92011-06-16 19:10:39 +0000248 // wrap the bitmap with a pixelref to expose our texture
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000249 SkGrTexturePixelRef* pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000250 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000251 } else {
252 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
253 width, height);
254 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000255 }
256}
257
258SkGpuDevice::~SkGpuDevice() {
259 if (fDrawProcs) {
260 delete fDrawProcs;
261 }
262
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000263 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
264 // This call gives the context a chance to relinquish it
265 fContext->setRenderTarget(NULL);
266
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000267 SkSafeUnref(fTexture);
268 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000269 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000270 GrAssert(NULL != fTexture);
271 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000272 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000273 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000274 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000275}
276
reed@google.comac10a2d2010-12-22 21:39:39 +0000277///////////////////////////////////////////////////////////////////////////////
278
279void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000280 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000281 fContext->setRenderTarget(fRenderTarget);
282 fContext->flush(true);
283 fNeedPrepareRenderTarget = true;
284}
285
286///////////////////////////////////////////////////////////////////////////////
287
bsalomon@google.comc4364992011-11-07 15:54:49 +0000288namespace {
289GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
290 switch (config8888) {
291 case SkCanvas::kNative_Premul_Config8888:
292 return kSkia8888_PM_GrPixelConfig;
293 case SkCanvas::kNative_Unpremul_Config8888:
294 return kSkia8888_UPM_GrPixelConfig;
295 case SkCanvas::kBGRA_Premul_Config8888:
296 return kBGRA_8888_PM_GrPixelConfig;
297 case SkCanvas::kBGRA_Unpremul_Config8888:
298 return kBGRA_8888_UPM_GrPixelConfig;
299 case SkCanvas::kRGBA_Premul_Config8888:
300 return kRGBA_8888_PM_GrPixelConfig;
301 case SkCanvas::kRGBA_Unpremul_Config8888:
302 return kRGBA_8888_UPM_GrPixelConfig;
303 default:
304 GrCrash("Unexpected Config8888.");
305 return kSkia8888_PM_GrPixelConfig;
306 }
307}
308}
309
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000310bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
311 int x, int y,
312 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000313 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000314 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
315 SkASSERT(!bitmap.isNull());
316 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000317
bsalomon@google.com910267d2011-11-02 20:06:25 +0000318 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000319 GrPixelConfig config;
320 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000321 return fContext->readRenderTargetPixels(fRenderTarget,
322 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000323 bitmap.width(),
324 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000325 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000326 bitmap.getPixels(),
327 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000328}
329
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000330void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
331 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000332 SkAutoLockPixels alp(bitmap);
333 if (!bitmap.readyToDraw()) {
334 return;
335 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000336
337 GrPixelConfig config;
338 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
339 config = config8888_to_gr_config(config8888);
340 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000341 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000342 }
343
bsalomon@google.com6f379512011-11-16 20:36:03 +0000344 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
345 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000346}
347
robertphillips@google.com46f93502012-08-07 15:38:08 +0000348namespace {
349void purgeClipCB(int genID, void* data) {
350 GrContext* context = (GrContext*) data;
351
352 if (SkClipStack::kInvalidGenID == genID ||
353 SkClipStack::kEmptyGenID == genID ||
354 SkClipStack::kWideOpenGenID == genID) {
355 // none of these cases will have a cached clip mask
356 return;
357 }
358
359}
360};
361
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000362void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
363 INHERITED::onAttachToCanvas(canvas);
364
365 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000366 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000367
368 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000369}
370
371void SkGpuDevice::onDetachFromCanvas() {
372 INHERITED::onDetachFromCanvas();
373
robertphillips@google.com46f93502012-08-07 15:38:08 +0000374 // TODO: iterate through the clip stack and clean up any cached clip masks
375 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
376
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000377 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000378}
379
robertphillips@google.com607fe072012-07-24 13:54:00 +0000380#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000381static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000382 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000383 int renderTargetWidth,
384 int renderTargetHeight) {
385
robertphillips@google.com7b112892012-07-31 15:18:21 +0000386 SkIRect devBound;
387
388 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
389
robertphillips@google.com607fe072012-07-24 13:54:00 +0000390 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000391 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000392
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000393 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000394 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000395 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000396
robertphillips@google.com7b112892012-07-31 15:18:21 +0000397 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000398
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000399 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000400
robertphillips@google.com7b112892012-07-31 15:18:21 +0000401 if (!devBound.intersect(devTemp)) {
402 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000403 }
404 }
405
robertphillips@google.com768fee82012-08-02 12:42:43 +0000406 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000407}
408#endif
409
reed@google.comac10a2d2010-12-22 21:39:39 +0000410///////////////////////////////////////////////////////////////////////////////
411
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000412static void set_matrix_and_clip(GrContext* context, const SkMatrix& matrix,
413 GrClipData& clipData,
414 const SkRegion& clipRegion,
415 const SkIPoint& origin,
416 int renderTargetWidth, int renderTargetHeight) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000417 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000418
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000419 clipData.fOrigin = origin;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000420
421#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000422 check_bounds(clipData, clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000423 renderTargetWidth, renderTargetHeight);
424#endif
425
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000426 context->setClip(&clipData);
reed@google.comac10a2d2010-12-22 21:39:39 +0000427}
428
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000429// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000430// and not the state from some other canvas/device
431void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000432 GrAssert(NULL != fClipData.fClipStack);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000433
reed@google.comac10a2d2010-12-22 21:39:39 +0000434 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000435 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000436
437 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000438 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000439
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000440 set_matrix_and_clip(fContext, *draw.fMatrix,
441 fClipData, *draw.fClip, this->getOrigin(),
442 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000443 fNeedPrepareRenderTarget = false;
444 }
445}
446
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000447void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
448 const SkClipStack& clipStack) {
449 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
450 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000451 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000452}
453
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000454void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
455
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000456 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000457
reed@google.comac10a2d2010-12-22 21:39:39 +0000458 fContext->setRenderTarget(fRenderTarget);
459
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000460 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000461
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000462 set_matrix_and_clip(fContext, matrix, fClipData, clip, this->getOrigin(),
463 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000464
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000465 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000466}
467
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000468SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000469 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000470 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000471}
472
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000473bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000474 if (NULL != fTexture) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000475 paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
476 SkNEW_ARGS(GrSingleTextureEffect, (fTexture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000477 return true;
478 }
479 return false;
480}
481
482///////////////////////////////////////////////////////////////////////////////
483
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000484SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
485SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
486SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
487SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
488SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
489 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000490SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
491 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000492SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
493SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000494
bsalomon@google.com84405e02012-03-05 19:57:21 +0000495namespace {
496
497// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
498// justAlpha indicates that skPaint's alpha should be used rather than the color
499// Callers may subsequently modify the GrPaint. Setting constantColor indicates
500// that the final paint will draw the same color at every pixel. This allows
501// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000502// color once while converting to GrPaint and then ignored.
503inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
504 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000505 bool justAlpha,
506 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000507 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000508 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000509
510 grPaint->fDither = skPaint.isDither();
511 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000512 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000513
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000514 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
515 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000516
517 SkXfermode* mode = skPaint.getXfermode();
518 if (mode) {
519 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000520 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000521#if 0
522 return false;
523#endif
524 }
525 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000526 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
527 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
528
bsalomon@google.com5782d712011-01-21 21:03:59 +0000529 if (justAlpha) {
530 uint8_t alpha = skPaint.getAlpha();
531 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000532 // justAlpha is currently set to true only if there is a texture,
533 // so constantColor should not also be true.
534 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000535 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000536 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000537 GrAssert(!grPaint->isTextureStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000538 }
Scroggo97c88c22011-05-11 14:05:25 +0000539 SkColorFilter* colorFilter = skPaint.getColorFilter();
540 SkColor color;
541 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000542 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000543 SkBitmap colorTransformTable;
Scroggo97c88c22011-05-11 14:05:25 +0000544 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000545 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000546 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000547 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000548 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000549 } else {
550 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000551 grPaint->fColor = SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000552 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000553 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000554 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
555 grPaint->fColorMatrixEnabled = true;
556 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
557 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
twiz@google.com58071162012-07-18 21:41:50 +0000558 } else if (colorFilter != NULL && colorFilter->asComponentTable(
559 &colorTransformTable)) {
560 grPaint->resetColorFilter();
561
562 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000563 GrTexture* texture = act->set(dev, colorTransformTable, colorSampler->textureParams());
twiz@google.com58071162012-07-18 21:41:50 +0000564
bsalomon@google.comb8670992012-07-25 21:27:09 +0000565 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000566 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000567 } else {
568 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000569 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000570 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000571}
572
bsalomon@google.com84405e02012-03-05 19:57:21 +0000573// This function is similar to skPaint2GrPaintNoShader but also converts
574// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
575// be used is set on grPaint and returned in param act. constantColor has the
576// same meaning as in skPaint2GrPaintNoShader.
577inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
578 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000579 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000580 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000581 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000582 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000583 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000584 return skPaint2GrPaintNoShader(dev,
585 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000586 false,
587 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000588 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000589 grPaint);
twiz@google.com58071162012-07-18 21:41:50 +0000590 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
591 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000592 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000593 }
594
rileya@google.com91f319c2012-07-25 17:18:31 +0000595 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
596 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
597
598 if (NULL != stage) {
599 sampler->setCustomStage(stage)->unref();
600 SkMatrix localM;
601 if (shader->getLocalMatrix(&localM)) {
602 SkMatrix inverse;
603 if (localM.invert(&inverse)) {
604 sampler->matrix()->preConcat(inverse);
605 }
606 }
607 return true;
608 }
609
reed@google.comac10a2d2010-12-22 21:39:39 +0000610 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000611 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000612 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000613 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000614 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000615
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000616 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000617 SkShader::GradientInfo info;
618 SkColor color;
619
620 info.fColors = &color;
621 info.fColorOffsets = NULL;
622 info.fColorCount = 1;
623 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
624 SkPaint copy(skPaint);
625 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000626 // modulate the paint alpha by the shader's solid color alpha
627 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
628 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000629 return skPaint2GrPaintNoShader(dev,
630 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000631 false,
632 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000633 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000634 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000635 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000636 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000637 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000638
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000639 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.comb8670992012-07-25 21:27:09 +0000640 sampler->textureParams()->reset(tileModes, skPaint.isFilterBitmap());
641 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, sampler->textureParams());
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000642
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000643 if (NULL == texture) {
644 SkDebugf("Couldn't convert bitmap to texture.\n");
645 return false;
646 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000647
rileya@google.com91f319c2012-07-25 17:18:31 +0000648 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000649
reed@google.comac10a2d2010-12-22 21:39:39 +0000650 // since our texture coords will be in local space, we wack the texture
651 // matrix to map them back into 0...1 before we load it
652 SkMatrix localM;
653 if (shader->getLocalMatrix(&localM)) {
654 SkMatrix inverse;
655 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000656 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000657 }
658 }
659 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000660 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
661 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000662 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000663 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000664
665 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000666}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000667}
reed@google.comac10a2d2010-12-22 21:39:39 +0000668
669///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000670void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000671 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000672}
673
reed@google.comac10a2d2010-12-22 21:39:39 +0000674void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
675 CHECK_SHOULD_DRAW(draw);
676
bsalomon@google.com5782d712011-01-21 21:03:59 +0000677 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000678 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000679 if (!skPaint2GrPaintShader(this,
680 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000681 true,
twiz@google.com58071162012-07-18 21:41:50 +0000682 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000683 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000684 return;
685 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000686
687 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000688}
689
690// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000691static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000692 kPoints_GrPrimitiveType,
693 kLines_GrPrimitiveType,
694 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000695};
696
697void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000698 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000699 CHECK_SHOULD_DRAW(draw);
700
701 SkScalar width = paint.getStrokeWidth();
702 if (width < 0) {
703 return;
704 }
705
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000706 // we only handle hairlines and paints without path effects or mask filters,
707 // else we let the SkDraw call our drawPath()
708 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000709 draw.drawPoints(mode, count, pts, paint, true);
710 return;
711 }
712
bsalomon@google.com5782d712011-01-21 21:03:59 +0000713 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000714 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000715 if (!skPaint2GrPaintShader(this,
716 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000717 true,
twiz@google.com58071162012-07-18 21:41:50 +0000718 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000719 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000720 return;
721 }
722
bsalomon@google.com5782d712011-01-21 21:03:59 +0000723 fContext->drawVertices(grPaint,
724 gPointMode2PrimtiveType[mode],
725 count,
726 (GrPoint*)pts,
727 NULL,
728 NULL,
729 NULL,
730 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000731}
732
reed@google.comc9aa5872011-04-05 21:05:37 +0000733///////////////////////////////////////////////////////////////////////////////
734
reed@google.comac10a2d2010-12-22 21:39:39 +0000735void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
736 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000737 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000738 CHECK_SHOULD_DRAW(draw);
739
bungeman@google.com79bd8772011-07-18 15:34:08 +0000740 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000741 SkScalar width = paint.getStrokeWidth();
742
743 /*
744 We have special code for hairline strokes, miter-strokes, and fills.
745 Anything else we just call our path code.
746 */
747 bool usePath = doStroke && width > 0 &&
748 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000749 // another two reasons we might need to call drawPath...
750 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000751 usePath = true;
752 }
reed@google.com67db6642011-05-26 11:46:35 +0000753 // until we aa rotated rects...
754 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
755 usePath = true;
756 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000757 // small miter limit means right angles show bevel...
758 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
759 paint.getStrokeMiter() < SK_ScalarSqrt2)
760 {
761 usePath = true;
762 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000763 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000764 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
765 usePath = true;
766 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000767
768 if (usePath) {
769 SkPath path;
770 path.addRect(rect);
771 this->drawPath(draw, path, paint, NULL, true);
772 return;
773 }
774
775 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000776 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000777 if (!skPaint2GrPaintShader(this,
778 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000779 true,
twiz@google.com58071162012-07-18 21:41:50 +0000780 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000781 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000782 return;
783 }
reed@google.com20efde72011-05-09 17:00:02 +0000784 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000785}
786
reed@google.com69302852011-02-16 18:08:07 +0000787#include "SkMaskFilter.h"
788#include "SkBounder.h"
789
bsalomon@google.com85003222012-03-28 14:44:37 +0000790///////////////////////////////////////////////////////////////////////////////
791
792// helpers for applying mask filters
793namespace {
794
795GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000796 switch (fillType) {
797 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000798 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000799 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000800 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000801 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000802 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000803 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000804 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000805 default:
806 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000807 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000808 }
809}
810
bsalomon@google.com85003222012-03-28 14:44:37 +0000811// We prefer to blur small rect with small radius via CPU.
812#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
813#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
814inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
815 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
816 rect.height() <= MIN_GPU_BLUR_SIZE &&
817 radius <= MIN_GPU_BLUR_RADIUS) {
818 return true;
819 }
820 return false;
821}
822
823bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
824 SkMaskFilter* filter, const SkMatrix& matrix,
825 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000826 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000827#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000828 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000829#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000830 SkMaskFilter::BlurInfo info;
831 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000832 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000833 return false;
834 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000835 SkScalar radius = info.fIgnoreTransform ? info.fRadius
836 : matrix.mapRadius(info.fRadius);
837 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000838 if (radius <= 0) {
839 return false;
840 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000841
842 SkRect srcRect = path.getBounds();
843 if (shouldDrawBlurWithCPU(srcRect, radius)) {
844 return false;
845 }
846
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000847 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000848 float sigma3 = sigma * 3.0f;
849
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000850 SkRect clipRect;
851 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000852
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000853 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000854 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
855 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000856 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000857 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000858 SkIRect finalIRect;
859 finalRect.roundOut(&finalIRect);
860 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000861 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000862 }
863 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000864 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000865 }
866 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000867 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000868 GrTextureDesc desc;
869 desc.fFlags = kRenderTarget_GrTextureFlagBit;
870 desc.fWidth = SkScalarCeilToInt(srcRect.width());
871 desc.fHeight = SkScalarCeilToInt(srcRect.height());
872 // We actually only need A8, but it often isn't supported as a
873 // render target so default to RGBA_8888
874 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000875
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000876 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
877 desc.fConfig = kAlpha_8_GrPixelConfig;
878 }
879
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000880 GrAutoScratchTexture pathEntry(context, desc);
881 GrTexture* pathTexture = pathEntry.texture();
882 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000883 return false;
884 }
885 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000886 // Once this code moves into GrContext, this should be changed to use
887 // an AutoClipRestore.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000888 const GrClipData* oldClipData = context->getClip();
889
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000890 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000891
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000892 SkClipStack newClipStack(srcRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000893 GrClipData newClipData;
894 newClipData.fClipStack = &newClipStack;
895 context->setClip(&newClipData);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000896
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000897 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000898 GrPaint tempPaint;
899 tempPaint.reset();
900
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000901 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000902 tempPaint.fAntiAlias = grp->fAntiAlias;
903 if (tempPaint.fAntiAlias) {
904 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
905 // blend coeff of zero requires dual source blending support in order
906 // to properly blend partially covered pixels. This means the AA
907 // code path may not be taken. So we use a dst blend coeff of ISA. We
908 // could special case AA draws to a dst surface with known alpha=0 to
909 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000910 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
911 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000912 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000913 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000914 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000915
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000916 // If we're doing a normal blur, we can clobber the pathTexture in the
917 // gaussianBlur. Otherwise, we need to save it for later compositing.
918 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000919 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
920 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000921
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000922 if (!isNormalBlur) {
923 GrPaint paint;
924 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +0000925 paint.textureSampler(0)->textureParams()->setClampNoFilter();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000926 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
927 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000928 // Blend pathTexture over blurTexture.
929 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000930 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
931 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000932 if (SkMaskFilter::kInner_BlurType == blurType) {
933 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000934 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
935 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000936 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
937 // solid: dst = src + dst - src * dst
938 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000939 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
940 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000941 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
942 // outer: dst = dst * (1 - src)
943 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000944 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
945 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000946 }
947 context->drawRect(paint, srcRect);
948 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000949 context->setRenderTarget(oldRenderTarget);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000950 context->setClip(oldClipData);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000951
bsalomon@google.come3d32162012-07-20 13:37:06 +0000952 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
953 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000954 }
955
956 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
957 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000958 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +0000959 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000960 grp->maskSampler(MASK_IDX)->setCustomStage(
961 SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)))->unref();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000962 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
963 -finalRect.fTop);
964 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
965 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000966 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000967 return true;
968}
969
bsalomon@google.com85003222012-03-28 14:44:37 +0000970bool drawWithMaskFilter(GrContext* context, const SkPath& path,
971 SkMaskFilter* filter, const SkMatrix& matrix,
972 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000973 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000974 SkMask srcM, dstM;
975
976 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000977 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000978 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000979 return false;
980 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000981 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000982
983 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
984 return false;
985 }
986 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000987 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000988
989 if (clip.quickReject(dstM.fBounds)) {
990 return false;
991 }
992 if (bounder && !bounder->doIRect(dstM.fBounds)) {
993 return false;
994 }
995
996 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
997 // the current clip (and identity matrix) and grpaint settings
998
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000999 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001000
bsalomon@google.come3d32162012-07-20 13:37:06 +00001001 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
1002 return false;
1003 }
1004
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001005 GrTextureDesc desc;
1006 desc.fWidth = dstM.fBounds.width();
1007 desc.fHeight = dstM.fBounds.height();
1008 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +00001009
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001010 GrAutoScratchTexture ast(context, desc);
1011 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001012
reed@google.com69302852011-02-16 18:08:07 +00001013 if (NULL == texture) {
1014 return false;
1015 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001016 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001017 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001018
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001019 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1020 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001021 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +00001022 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001023 grp->maskSampler(MASK_IDX)->setCustomStage(
1024 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001025 GrRect d;
1026 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001027 GrIntToScalar(dstM.fBounds.fTop),
1028 GrIntToScalar(dstM.fBounds.fRight),
1029 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001030
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001031 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1032 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1033 -dstM.fBounds.fTop*SK_Scalar1);
1034 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001035 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001036 return true;
1037}
reed@google.com69302852011-02-16 18:08:07 +00001038
bsalomon@google.com85003222012-03-28 14:44:37 +00001039}
1040
1041///////////////////////////////////////////////////////////////////////////////
1042
reed@google.com0c219b62011-02-16 21:31:18 +00001043void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001044 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001045 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001046 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001047 CHECK_SHOULD_DRAW(draw);
1048
reed@google.comfe626382011-09-21 13:50:35 +00001049 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001050
bsalomon@google.com5782d712011-01-21 21:03:59 +00001051 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001052 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001053 if (!skPaint2GrPaintShader(this,
1054 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001055 true,
twiz@google.com58071162012-07-18 21:41:50 +00001056 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001057 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 return;
1059 }
1060
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001061 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1062 // if we can, we draw lots faster (raster device does this same test)
1063 SkScalar hairlineCoverage;
1064 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1065 doFill = false;
1066 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1067 grPaint.fCoverage);
1068 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001069
reed@google.comfe626382011-09-21 13:50:35 +00001070 // If we have a prematrix, apply it to the path, optimizing for the case
1071 // where the original path can in fact be modified in place (even though
1072 // its parameter type is const).
1073 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1074 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001075
1076 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001077 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001078
reed@google.come3445642011-02-16 23:20:39 +00001079 if (!pathIsMutable) {
1080 result = &tmpPath;
1081 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001082 }
reed@google.come3445642011-02-16 23:20:39 +00001083 // should I push prePathMatrix on our MV stack temporarily, instead
1084 // of applying it here? See SkDraw.cpp
1085 pathPtr->transform(*prePathMatrix, result);
1086 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001087 }
reed@google.com0c219b62011-02-16 21:31:18 +00001088 // at this point we're done with prePathMatrix
1089 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001090
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001091 if (paint.getPathEffect() ||
1092 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001093 // it is safe to use tmpPath here, even if we already used it for the
1094 // prepathmatrix, since getFillPath can take the same object for its
1095 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001096 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001097 pathPtr = &tmpPath;
1098 }
1099
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001100 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001101 // avoid possibly allocating a new path in transform if we can
1102 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1103
1104 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001105 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001106 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001107 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001108 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001109 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001110 &grPaint, pathFillType)) {
1111 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1112 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001113 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001114 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001115 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001116 }
reed@google.com69302852011-02-16 18:08:07 +00001117 return;
1118 }
reed@google.com69302852011-02-16 18:08:07 +00001119
bsalomon@google.com47059542012-06-06 20:51:20 +00001120 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001121
reed@google.com0c219b62011-02-16 21:31:18 +00001122 if (doFill) {
1123 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001124 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001125 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001126 break;
1127 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001128 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001129 break;
1130 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001131 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001132 break;
1133 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001134 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001135 break;
1136 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001137 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001138 return;
1139 }
1140 }
1141
reed@google.com07f3ee12011-05-16 17:21:57 +00001142 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001143}
1144
bsalomon@google.comfb309512011-11-30 14:13:48 +00001145namespace {
1146
1147inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1148 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1149 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1150 return tilesX * tilesY;
1151}
1152
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001153inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001154 const SkIRect* srcRectPtr,
1155 int maxTextureSize) {
1156 static const int kSmallTileSize = 1 << 10;
1157 if (maxTextureSize <= kSmallTileSize) {
1158 return maxTextureSize;
1159 }
1160
1161 size_t maxTexTotalTileSize;
1162 size_t smallTotalTileSize;
1163
1164 if (NULL == srcRectPtr) {
1165 int w = bitmap.width();
1166 int h = bitmap.height();
1167 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1168 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1169 } else {
1170 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1171 srcRectPtr->fTop,
1172 srcRectPtr->fRight,
1173 srcRectPtr->fBottom,
1174 maxTextureSize);
1175 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1176 srcRectPtr->fTop,
1177 srcRectPtr->fRight,
1178 srcRectPtr->fBottom,
1179 kSmallTileSize);
1180 }
1181 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1182 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1183
1184 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1185 return kSmallTileSize;
1186 } else {
1187 return maxTextureSize;
1188 }
1189}
1190}
1191
1192bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001193 const GrTextureParams& params,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001194 const SkIRect* srcRectPtr,
1195 int* tileSize) const {
1196 SkASSERT(NULL != tileSize);
1197
1198 // if bitmap is explictly texture backed then just use the texture
1199 if (NULL != bitmap.getTexture()) {
1200 return false;
1201 }
1202 // if it's larger than the max texture size, then we have no choice but
1203 // tiling
1204 const int maxTextureSize = fContext->getMaxTextureSize();
1205 if (bitmap.width() > maxTextureSize ||
1206 bitmap.height() > maxTextureSize) {
1207 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1208 return true;
1209 }
1210 // if we are going to have to draw the whole thing, then don't tile
1211 if (NULL == srcRectPtr) {
1212 return false;
1213 }
1214 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001215 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001216 return false;
1217 }
1218
1219 // At this point we know we could do the draw by uploading the entire bitmap
1220 // as a texture. However, if the texture would be large compared to the
1221 // cache size and we don't require most of it for this draw then tile to
1222 // reduce the amount of upload and cache spill.
1223
1224 // assumption here is that sw bitmap size is a good proxy for its size as
1225 // a texture
1226 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001227 size_t cacheSize;
1228 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001229 if (bmpSize < cacheSize / 2) {
1230 return false;
1231 }
1232
1233 SkFixed fracUsed =
1234 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1235 (srcRectPtr->height() << 16) / bitmap.height());
1236 if (fracUsed <= SK_FixedHalf) {
1237 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1238 return true;
1239 } else {
1240 return false;
1241 }
1242}
1243
reed@google.comac10a2d2010-12-22 21:39:39 +00001244void SkGpuDevice::drawBitmap(const SkDraw& draw,
1245 const SkBitmap& bitmap,
1246 const SkIRect* srcRectPtr,
1247 const SkMatrix& m,
1248 const SkPaint& paint) {
1249 CHECK_SHOULD_DRAW(draw);
1250
1251 SkIRect srcRect;
1252 if (NULL == srcRectPtr) {
1253 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1254 } else {
1255 srcRect = *srcRectPtr;
1256 }
1257
junov@google.comd935cfb2011-06-27 20:48:23 +00001258 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001259 // Convert the bitmap to a shader so that the rect can be drawn
1260 // through drawRect, which supports mask filters.
1261 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001262 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001263 if (srcRectPtr) {
1264 if (!bitmap.extractSubset(&tmp, srcRect)) {
1265 return; // extraction failed
1266 }
1267 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001268 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001269 }
1270 SkPaint paintWithTexture(paint);
1271 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1272 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001273 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001274 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001275
junov@google.com1d329782011-07-28 20:10:09 +00001276 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001277 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001278 // also affect the behavior of the mask filter.
1279 SkMatrix drawMatrix;
1280 drawMatrix.setConcat(*draw.fMatrix, m);
1281 SkDraw transformedDraw(draw);
1282 transformedDraw.fMatrix = &drawMatrix;
1283
1284 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1285
junov@google.comd935cfb2011-06-27 20:48:23 +00001286 return;
1287 }
1288
bsalomon@google.com5782d712011-01-21 21:03:59 +00001289 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001290 SkAutoCachedTexture colorLutTexture;
1291 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001292 return;
1293 }
bsalomon@google.comb8670992012-07-25 21:27:09 +00001294 GrTextureParams* params = grPaint.textureSampler(kBitmapTextureIdx)->textureParams();
1295 params->setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001296
bsalomon@google.comfb309512011-11-30 14:13:48 +00001297 int tileSize;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001298 if (!this->shouldTileBitmap(bitmap, *params, srcRectPtr, &tileSize)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001299 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001300 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001301 return;
1302 }
1303
1304 // undo the translate done by SkCanvas
1305 int DX = SkMax32(0, srcRect.fLeft);
1306 int DY = SkMax32(0, srcRect.fTop);
1307 // compute clip bounds in local coordinates
1308 SkIRect clipRect;
1309 {
1310 SkRect r;
1311 r.set(draw.fClip->getBounds());
1312 SkMatrix matrix, inverse;
1313 matrix.setConcat(*draw.fMatrix, m);
1314 if (!matrix.invert(&inverse)) {
1315 return;
1316 }
1317 inverse.mapRect(&r);
1318 r.roundOut(&clipRect);
1319 // apply the canvas' translate to our local clip
1320 clipRect.offset(DX, DY);
1321 }
1322
bsalomon@google.comfb309512011-11-30 14:13:48 +00001323 int nx = bitmap.width() / tileSize;
1324 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001325 for (int x = 0; x <= nx; x++) {
1326 for (int y = 0; y <= ny; y++) {
1327 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001328 tileR.set(x * tileSize, y * tileSize,
1329 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001330 if (!SkIRect::Intersects(tileR, clipRect)) {
1331 continue;
1332 }
1333
1334 SkIRect srcR = tileR;
1335 if (!srcR.intersect(srcRect)) {
1336 continue;
1337 }
1338
1339 SkBitmap tmpB;
1340 if (bitmap.extractSubset(&tmpB, tileR)) {
1341 // now offset it to make it "local" to our tmp bitmap
1342 srcR.offset(-tileR.fLeft, -tileR.fTop);
1343
1344 SkMatrix tmpM(m);
1345 {
1346 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1347 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1348 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1349 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001350 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001351 }
1352 }
1353 }
1354}
1355
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001356namespace {
1357
1358bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1359 // detect pixel disalignment
1360 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1361 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1362 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1363 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1364 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1365 COLOR_BLEED_TOLERANCE &&
1366 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1367 COLOR_BLEED_TOLERANCE) {
1368 return true;
1369 }
1370 return false;
1371}
1372
1373bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1374 const SkMatrix& m) {
1375 // Only gets called if hasAlignedSamples returned false.
1376 // So we can assume that sampling is axis aligned but not texel aligned.
1377 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1378 SkRect innerSrcRect(srcRect), innerTransformedRect,
1379 outerTransformedRect(transformedRect);
1380 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1381 m.mapRect(&innerTransformedRect, innerSrcRect);
1382
1383 // The gap between outerTransformedRect and innerTransformedRect
1384 // represents the projection of the source border area, which is
1385 // problematic for color bleeding. We must check whether any
1386 // destination pixels sample the border area.
1387 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1388 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1389 SkIRect outer, inner;
1390 outerTransformedRect.round(&outer);
1391 innerTransformedRect.round(&inner);
1392 // If the inner and outer rects round to the same result, it means the
1393 // border does not overlap any pixel centers. Yay!
1394 return inner != outer;
1395}
1396
1397} // unnamed namespace
1398
reed@google.comac10a2d2010-12-22 21:39:39 +00001399/*
1400 * This is called by drawBitmap(), which has to handle images that may be too
1401 * large to be represented by a single texture.
1402 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001403 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1404 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001405 */
1406void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1407 const SkBitmap& bitmap,
1408 const SkIRect& srcRect,
1409 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001410 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001411 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1412 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001413
reed@google.com9c49bc32011-07-07 13:42:37 +00001414 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001415 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001416 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001417 return;
1418 }
1419
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001420 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001421
bsalomon@google.comb8670992012-07-25 21:27:09 +00001422 sampler->textureParams()->setClamp();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001423 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001424
1425 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001426 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001427 if (NULL == texture) {
1428 return;
1429 }
1430
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001431 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1432 (GrSingleTextureEffect, (texture)))->unref();
reed@google.com46799cd2011-02-22 20:56:26 +00001433
reed@google.com20efde72011-05-09 17:00:02 +00001434 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1435 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001436 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001437 float wInv = 1.f / bitmap.width();
1438 float hInv = 1.f / bitmap.height();
1439 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1440 SkFloatToScalar(srcRect.fTop * hInv),
1441 SkFloatToScalar(srcRect.fRight * wInv),
1442 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001443
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001444 bool needsTextureDomain = false;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001445 if (sampler->textureParams()->isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001446 // Need texture domain if drawing a sub rect.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001447 needsTextureDomain = srcRect.width() < bitmap.width() || srcRect.height() < bitmap.height();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001448 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1449 // sampling is axis-aligned
1450 GrRect floatSrcRect, transformedRect;
1451 floatSrcRect.set(srcRect);
1452 SkMatrix srcToDeviceMatrix(m);
1453 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1454 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1455
1456 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1457 // Samples are texel-aligned, so filtering is futile
bsalomon@google.comb8670992012-07-25 21:27:09 +00001458 sampler->textureParams()->setBilerp(false);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001459 needsTextureDomain = false;
1460 } else {
1461 needsTextureDomain = needsTextureDomain &&
1462 mayColorBleed(floatSrcRect, transformedRect, m);
1463 }
1464 }
1465 }
1466
1467 GrRect textureDomain = GrRect::MakeEmpty();
1468
1469 if (needsTextureDomain) {
1470 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001471 GrScalar left, top, right, bottom;
1472 if (srcRect.width() > 1) {
1473 GrScalar border = GR_ScalarHalf / bitmap.width();
1474 left = paintRect.left() + border;
1475 right = paintRect.right() - border;
1476 } else {
1477 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1478 }
1479 if (srcRect.height() > 1) {
1480 GrScalar border = GR_ScalarHalf / bitmap.height();
1481 top = paintRect.top() + border;
1482 bottom = paintRect.bottom() - border;
1483 } else {
1484 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1485 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001486 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001487 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1488 (texture,
1489 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001490 }
1491
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001492 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001493}
1494
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001495namespace {
1496
1497void apply_custom_stage(GrContext* context,
1498 GrTexture* srcTexture,
1499 GrTexture* dstTexture,
1500 const GrRect& rect,
1501 GrCustomStage* stage) {
1502 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001503 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001504 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001505 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001506
1507 GrMatrix sampleM;
1508 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1509 GrPaint paint;
1510 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001511 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001512 paint.textureSampler(0)->reset(sampleM);
1513 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001514 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001515}
1516
1517};
1518
reed@google.com8926b162012-03-23 15:36:36 +00001519static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1520 SkImageFilter* filter, const GrRect& rect) {
1521 GrAssert(filter);
1522
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001523 GrTextureDesc desc;
1524 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1525 desc.fWidth = SkScalarCeilToInt(rect.width());
1526 desc.fHeight = SkScalarCeilToInt(rect.height());
1527 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001528 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001529
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001530 if (filter->canFilterImageGPU()) {
1531 texture = filter->onFilterImageGPU(texture, rect);
1532 } else if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001533 GrAutoScratchTexture dst(context, desc);
1534 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1535 texture = dst.detach();
1536 stage->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001537 }
1538 return texture;
1539}
1540
reed@google.comac10a2d2010-12-22 21:39:39 +00001541void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1542 int left, int top, const SkPaint& paint) {
1543 CHECK_SHOULD_DRAW(draw);
1544
reed@google.com8926b162012-03-23 15:36:36 +00001545 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001546 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1547 return;
1548 }
1549
reed@google.com76dd2772012-01-05 21:15:07 +00001550 int w = bitmap.width();
1551 int h = bitmap.height();
1552
bsalomon@google.com5782d712011-01-21 21:03:59 +00001553 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001554 SkAutoCachedTexture colorLutTexture;
1555 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001556 return;
1557 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001558
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001559 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001560
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001561 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001562
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001563 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001564 sampler->reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001565 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001566 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1567 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001568
reed@google.com8926b162012-03-23 15:36:36 +00001569 SkImageFilter* filter = paint.getImageFilter();
1570 if (NULL != filter) {
1571 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001572 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001573 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001574 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1575 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001576 texture = filteredTexture;
1577 filteredTexture->unref();
1578 }
reed@google.com76dd2772012-01-05 21:15:07 +00001579 }
reed@google.com8926b162012-03-23 15:36:36 +00001580
bsalomon@google.com5782d712011-01-21 21:03:59 +00001581 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001582 GrRect::MakeXYWH(GrIntToScalar(left),
1583 GrIntToScalar(top),
1584 GrIntToScalar(w),
1585 GrIntToScalar(h)),
1586 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1587 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001588}
1589
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001590void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001591 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001592 // clear of the source device must occur before CHECK_SHOULD_DRAW
1593 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1594 if (dev->fNeedClear) {
1595 // TODO: could check here whether we really need to draw at all
1596 dev->clear(0x0);
1597 }
1598
reed@google.comac10a2d2010-12-22 21:39:39 +00001599 CHECK_SHOULD_DRAW(draw);
1600
bsalomon@google.com5782d712011-01-21 21:03:59 +00001601 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001602 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001603 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001604 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001605 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001606 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001607 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001608
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001609 GrTexture* devTex = grPaint.getTextureSampler(kBitmapTextureIdx).getCustomStage()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001610 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001611
reed@google.com8926b162012-03-23 15:36:36 +00001612 SkImageFilter* filter = paint.getImageFilter();
1613 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001614 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001615 SkIntToScalar(devTex->height()));
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001616 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001617 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001618 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1619 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001620 devTex = filteredTexture;
1621 filteredTexture->unref();
1622 }
1623 }
1624
bsalomon@google.com5782d712011-01-21 21:03:59 +00001625 const SkBitmap& bm = dev->accessBitmap(false);
1626 int w = bm.width();
1627 int h = bm.height();
1628
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001629 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001630 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1631 GrIntToScalar(y),
1632 GrIntToScalar(w),
1633 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001634
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001635 // The device being drawn may not fill up its texture (saveLayer uses
1636 // the approximate ).
1637 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1638 GR_Scalar1 * h / devTex->height());
1639
1640 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001641}
1642
reed@google.com8926b162012-03-23 15:36:36 +00001643bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001644 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001645 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001646 return false;
1647 }
reed@google.com8926b162012-03-23 15:36:36 +00001648 return true;
1649}
1650
1651bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1652 const SkMatrix& ctm,
1653 SkBitmap* result, SkIPoint* offset) {
1654 // want explicitly our impl, so guard against a subclass of us overriding it
1655 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001656 return false;
1657 }
reed@google.com8926b162012-03-23 15:36:36 +00001658
1659 SkAutoLockPixels alp(src, !src.getTexture());
1660 if (!src.getTexture() && !src.readyToDraw()) {
1661 return false;
1662 }
1663
1664 GrPaint paint;
1665 paint.reset();
1666
1667 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1668
1669 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001670 SkAutoCachedTexture act(this, src, sampler->textureParams(), &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001671
1672 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001673 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1674 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001675 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1676 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001677 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1678 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001679 resultTexture->unref();
1680 }
reed@google.com76dd2772012-01-05 21:15:07 +00001681 return true;
1682}
1683
reed@google.comac10a2d2010-12-22 21:39:39 +00001684///////////////////////////////////////////////////////////////////////////////
1685
1686// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001687static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001688 kTriangles_GrPrimitiveType,
1689 kTriangleStrip_GrPrimitiveType,
1690 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001691};
1692
1693void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1694 int vertexCount, const SkPoint vertices[],
1695 const SkPoint texs[], const SkColor colors[],
1696 SkXfermode* xmode,
1697 const uint16_t indices[], int indexCount,
1698 const SkPaint& paint) {
1699 CHECK_SHOULD_DRAW(draw);
1700
bsalomon@google.com5782d712011-01-21 21:03:59 +00001701 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001702 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001703 // we ignore the shader if texs is null.
1704 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001705 if (!skPaint2GrPaintNoShader(this,
1706 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001707 false,
1708 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001709 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001710 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001711 return;
1712 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001713 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001714 if (!skPaint2GrPaintShader(this,
1715 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001716 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001717 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001718 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001719 return;
1720 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001721 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001722
1723 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001724 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001725 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1726#if 0
1727 return
1728#endif
1729 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001730 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001731
bsalomon@google.com498776a2011-08-16 19:20:44 +00001732 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1733 if (NULL != colors) {
1734 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001735 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001736 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001737 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001738 }
1739 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001740 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001741 fContext->drawVertices(grPaint,
1742 gVertexMode2PrimitiveType[vmode],
1743 vertexCount,
1744 (GrPoint*) vertices,
1745 (GrPoint*) texs,
1746 colors,
1747 indices,
1748 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001749}
1750
1751///////////////////////////////////////////////////////////////////////////////
1752
1753static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001754 GrFontScaler* scaler = (GrFontScaler*)data;
1755 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001756}
1757
1758static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1759 void* auxData;
1760 GrFontScaler* scaler = NULL;
1761 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1762 scaler = (GrFontScaler*)auxData;
1763 }
1764 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001765 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001766 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1767 }
1768 return scaler;
1769}
1770
1771static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1772 SkFixed fx, SkFixed fy,
1773 const SkGlyph& glyph) {
1774 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1775
bungeman@google.com15865a72012-01-11 16:28:04 +00001776 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001777
1778 if (NULL == procs->fFontScaler) {
1779 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1780 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001781
bungeman@google.com15865a72012-01-11 16:28:04 +00001782 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1783 glyph.getSubXFixed(),
1784 glyph.getSubYFixed()),
1785 SkFixedFloorToFixed(fx),
1786 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001787 procs->fFontScaler);
1788}
1789
bsalomon@google.com5782d712011-01-21 21:03:59 +00001790SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001791
1792 // deferred allocation
1793 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001794 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001795 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1796 fDrawProcs->fContext = fContext;
1797 }
1798
1799 // init our (and GL's) state
1800 fDrawProcs->fTextContext = context;
1801 fDrawProcs->fFontScaler = NULL;
1802 return fDrawProcs;
1803}
1804
1805void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1806 size_t byteLength, SkScalar x, SkScalar y,
1807 const SkPaint& paint) {
1808 CHECK_SHOULD_DRAW(draw);
1809
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001810 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001811 // this guy will just call our drawPath()
1812 draw.drawText((const char*)text, byteLength, x, y, paint);
1813 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001814 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001815
1816 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001817 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001818 if (!skPaint2GrPaintShader(this,
1819 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001820 true,
twiz@google.com58071162012-07-18 21:41:50 +00001821 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001822 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001823 return;
1824 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001825 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1826 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001827 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1828 }
1829}
1830
1831void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1832 size_t byteLength, const SkScalar pos[],
1833 SkScalar constY, int scalarsPerPos,
1834 const SkPaint& paint) {
1835 CHECK_SHOULD_DRAW(draw);
1836
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001837 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001838 // this guy will just call our drawPath()
1839 draw.drawPosText((const char*)text, byteLength, pos, constY,
1840 scalarsPerPos, paint);
1841 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001842 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001843
1844 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001845 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001846 if (!skPaint2GrPaintShader(this,
1847 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001848 true,
twiz@google.com58071162012-07-18 21:41:50 +00001849 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001850 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001851 return;
1852 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001853 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1854 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001855 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1856 scalarsPerPos, paint);
1857 }
1858}
1859
1860void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1861 size_t len, const SkPath& path,
1862 const SkMatrix* m, const SkPaint& paint) {
1863 CHECK_SHOULD_DRAW(draw);
1864
1865 SkASSERT(draw.fDevice == this);
1866 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1867}
1868
1869///////////////////////////////////////////////////////////////////////////////
1870
reed@google.comf67e4cf2011-03-15 20:56:58 +00001871bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1872 if (!paint.isLCDRenderText()) {
1873 // we're cool with the paint as is
1874 return false;
1875 }
1876
1877 if (paint.getShader() ||
1878 paint.getXfermode() || // unless its srcover
1879 paint.getMaskFilter() ||
1880 paint.getRasterizer() ||
1881 paint.getColorFilter() ||
1882 paint.getPathEffect() ||
1883 paint.isFakeBoldText() ||
1884 paint.getStyle() != SkPaint::kFill_Style) {
1885 // turn off lcd
1886 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1887 flags->fHinting = paint.getHinting();
1888 return true;
1889 }
1890 // we're cool with the paint as is
1891 return false;
1892}
1893
reed@google.com75d939b2011-12-07 15:07:23 +00001894void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001895 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001896 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001897}
1898
reed@google.comf67e4cf2011-03-15 20:56:58 +00001899///////////////////////////////////////////////////////////////////////////////
1900
bsalomon@google.comfb309512011-11-30 14:13:48 +00001901bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001902 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001903 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001904 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001905
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001906 GrTextureDesc desc;
1907 desc.fWidth = bitmap.width();
1908 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001909 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001910
robertphillips@google.com9c2ea842012-08-13 17:47:59 +00001911 GrCacheData cacheData(key);
1912
1913 return this->context()->isTextureInCache(desc, cacheData, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001914}
1915
1916
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001917SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1918 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001919 bool isOpaque,
1920 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001921 GrTextureDesc desc;
1922 desc.fConfig = fRenderTarget->config();
1923 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1924 desc.fWidth = width;
1925 desc.fHeight = height;
1926 desc.fSampleCnt = fRenderTarget->numSamples();
1927
1928 GrContext::TextureCacheEntry cacheEntry;
1929 GrTexture* texture;
1930 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001931 // Skia's convention is to only clear a device if it is non-opaque.
1932 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001933
1934#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1935 // layers are never draw in repeat modes, so we can request an approx
1936 // match and ignore any padding.
1937 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1938 GrContext::kApprox_ScratchTexMatch :
1939 GrContext::kExact_ScratchTexMatch;
1940 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1941 texture = cacheEntry.texture();
1942#else
1943 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1944 texture = tunref.get();
1945#endif
1946 if (texture) {
1947 return SkNEW_ARGS(SkGpuDevice,(fContext,
1948 texture,
1949 cacheEntry,
1950 needClear));
1951 } else {
1952 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1953 width, height);
1954 return NULL;
1955 }
1956}
1957
1958SkGpuDevice::SkGpuDevice(GrContext* context,
1959 GrTexture* texture,
1960 TexCache cacheEntry,
1961 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001962 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1963
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001964 GrAssert(texture && texture->asRenderTarget());
1965 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1966 this->initFromRenderTarget(context, texture->asRenderTarget());
1967 fCache = cacheEntry;
1968 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001969}