blob: ab7693c7f1c0d1e276a9701fefa5cfebf50c04cb [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
tomhudson@google.com2f68e762012-07-17 18:43:21 +000010#include "effects/GrTextureDomainEffect.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000011#include "effects/GrSimpleTextureEffect.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"
senorblanco@chromium.org9c397442012-09-27 21:57:45 +000019#include "SkDeviceImageFilterProxy.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020#include "SkDrawProcs.h"
21#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000022#include "SkImageFilter.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000023#include "SkPathEffect.h"
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000024#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000025#include "SkStroke.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000026#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000027
bsalomon@google.com06cd7322012-03-30 18:45:35 +000028#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000029
30#if 0
31 extern bool (*gShouldDrawProc)();
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000032 #define CHECK_SHOULD_DRAW(draw, forceI) \
reed@google.comac10a2d2010-12-22 21:39:39 +000033 do { \
34 if (gShouldDrawProc && !gShouldDrawProc()) return; \
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000035 this->prepareDraw(draw, forceI); \
reed@google.comac10a2d2010-12-22 21:39:39 +000036 } while (0)
37#else
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000038 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
reed@google.comac10a2d2010-12-22 21:39:39 +000039#endif
40
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000041// This constant represents the screen alignment criterion in texels for
rmistry@google.comd6176b02012-08-23 18:14:13 +000042// requiring texture domain clamping to prevent color bleeding when drawing
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000043// a sub region of a larger source image.
44#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000045
junov@google.comdbfac8a2012-12-06 21:47:40 +000046#define DO_DEFERRED_CLEAR() \
47 do { \
48 if (fNeedClear) { \
49 this->clear(SK_ColorTRANSPARENT); \
50 } \
51 } while (false) \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000052
reed@google.comac10a2d2010-12-22 21:39:39 +000053///////////////////////////////////////////////////////////////////////////////
54
reed@google.comb0a34d82012-07-11 19:57:55 +000055#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
56 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
57
58///////////////////////////////////////////////////////////////////////////////
59
60
bsalomon@google.com84405e02012-03-05 19:57:21 +000061class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
62public:
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000063 SkAutoCachedTexture()
64 : fDevice(NULL)
65 , fTexture(NULL) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000066 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000067
bsalomon@google.com84405e02012-03-05 19:57:21 +000068 SkAutoCachedTexture(SkGpuDevice* device,
69 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000070 const GrTextureParams* params,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000071 GrTexture** texture)
72 : fDevice(NULL)
73 , fTexture(NULL) {
74 GrAssert(NULL != texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000075 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000076 }
reed@google.comac10a2d2010-12-22 21:39:39 +000077
bsalomon@google.com84405e02012-03-05 19:57:21 +000078 ~SkAutoCachedTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000079 if (NULL != fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +000080 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
bsalomon@google.com84405e02012-03-05 19:57:21 +000081 }
reed@google.comac10a2d2010-12-22 21:39:39 +000082 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000083
84 GrTexture* set(SkGpuDevice* device,
85 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000086 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000087 if (NULL != fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +000088 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000089 fTexture = NULL;
bsalomon@google.com84405e02012-03-05 19:57:21 +000090 }
91 fDevice = device;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000092 GrTexture* result = (GrTexture*)bitmap.getTexture();
93 if (NULL == result) {
94 // Cannot return the native texture so look it up in our cache
bsalomon@google.com95ed55a2013-01-24 14:46:47 +000095 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000096 result = fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +000097 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000098 return result;
bsalomon@google.com84405e02012-03-05 19:57:21 +000099 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000100
bsalomon@google.com84405e02012-03-05 19:57:21 +0000101private:
102 SkGpuDevice* fDevice;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000103 GrTexture* fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000104};
reed@google.comac10a2d2010-12-22 21:39:39 +0000105
106///////////////////////////////////////////////////////////////////////////////
107
reed@google.comac10a2d2010-12-22 21:39:39 +0000108struct GrSkDrawProcs : public SkDrawProcs {
109public:
110 GrContext* fContext;
111 GrTextContext* fTextContext;
112 GrFontScaler* fFontScaler; // cached in the skia glyphcache
113};
114
115///////////////////////////////////////////////////////////////////////////////
116
reed@google.comaf951c92011-06-16 19:10:39 +0000117static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
118 switch (config) {
119 case kAlpha_8_GrPixelConfig:
120 *isOpaque = false;
121 return SkBitmap::kA8_Config;
122 case kRGB_565_GrPixelConfig:
123 *isOpaque = true;
124 return SkBitmap::kRGB_565_Config;
125 case kRGBA_4444_GrPixelConfig:
126 *isOpaque = false;
127 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000128 case kSkia8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000129 // we don't currently have a way of knowing whether
130 // a 8888 is opaque based on the config.
131 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000132 return SkBitmap::kARGB_8888_Config;
133 default:
134 *isOpaque = false;
135 return SkBitmap::kNo_Config;
136 }
137}
reed@google.comac10a2d2010-12-22 21:39:39 +0000138
reed@google.comaf951c92011-06-16 19:10:39 +0000139static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000140 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000141
142 bool isOpaque;
143 SkBitmap bitmap;
144 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
145 renderTarget->width(), renderTarget->height());
146 bitmap.setIsOpaque(isOpaque);
147 return bitmap;
148}
149
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000150SkGpuDevice* SkGpuDevice::Create(GrSurface* surface) {
151 GrAssert(NULL != surface);
152 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
153 return NULL;
154 }
155 if (surface->asTexture()) {
156 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture()));
157 } else {
158 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget()));
159 }
160}
161
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000162SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000163: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000164 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000165}
166
reed@google.comaf951c92011-06-16 19:10:39 +0000167SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000168: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000169 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000170}
171
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000172void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000173 GrRenderTarget* renderTarget,
174 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000175 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000176
reed@google.comaf951c92011-06-16 19:10:39 +0000177 fContext = context;
178 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000179
reed@google.comaf951c92011-06-16 19:10:39 +0000180 fRenderTarget = NULL;
181 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000182
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000183 GrAssert(NULL != renderTarget);
184 fRenderTarget = renderTarget;
185 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000186
bsalomon@google.coma2921122012-08-28 12:34:17 +0000187 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
188 // on the RT but not vice-versa.
189 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
190 // busting chrome (for a currently unknown reason).
191 GrSurface* surface = fRenderTarget->asTexture();
192 if (NULL == surface) {
193 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000194 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000195 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000196
reed@google.comaf951c92011-06-16 19:10:39 +0000197 this->setPixelRef(pr, 0)->unref();
198}
199
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000200SkGpuDevice::SkGpuDevice(GrContext* context,
201 SkBitmap::Config config,
202 int width,
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000203 int height,
204 int sampleCount)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000205 : SkDevice(config, width, height, false /*isOpaque*/) {
206
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 fDrawProcs = NULL;
208
reed@google.com7b201d22011-01-11 18:59:23 +0000209 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000210 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000211
reed@google.comac10a2d2010-12-22 21:39:39 +0000212 fRenderTarget = NULL;
213 fNeedClear = false;
214
reed@google.comaf951c92011-06-16 19:10:39 +0000215 if (config != SkBitmap::kRGB_565_Config) {
216 config = SkBitmap::kARGB_8888_Config;
217 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000218
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000219 GrTextureDesc desc;
220 desc.fFlags = kRenderTarget_GrTextureFlagBit;
221 desc.fWidth = width;
222 desc.fHeight = height;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000223 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
224 desc.fSampleCnt = sampleCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000225
bsalomon@google.coma2921122012-08-28 12:34:17 +0000226 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000227
bsalomon@google.coma2921122012-08-28 12:34:17 +0000228 if (NULL != texture) {
229 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000230 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
reed@google.comaf951c92011-06-16 19:10:39 +0000232 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
reed@google.comaf951c92011-06-16 19:10:39 +0000234 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000235 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000236 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000237 } else {
238 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
239 width, height);
240 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000241 }
242}
243
244SkGpuDevice::~SkGpuDevice() {
245 if (fDrawProcs) {
246 delete fDrawProcs;
247 }
248
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000249 // The GrContext takes a ref on the target. We don't want to cause the render
250 // target to be unnecessarily kept alive.
251 if (fContext->getRenderTarget() == fRenderTarget) {
252 fContext->setRenderTarget(NULL);
253 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000254
robertphillips@google.com055f9082012-10-24 13:24:11 +0000255 if (fContext->getClip() == &fClipData) {
256 fContext->setClip(NULL);
257 }
258
bsalomon@google.coma2921122012-08-28 12:34:17 +0000259 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000260 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000261}
262
reed@google.comac10a2d2010-12-22 21:39:39 +0000263///////////////////////////////////////////////////////////////////////////////
264
265void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000266 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000267 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000268}
269
270///////////////////////////////////////////////////////////////////////////////
271
bsalomon@google.comc4364992011-11-07 15:54:49 +0000272namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000273GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000274 switch (config8888) {
275 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000276 *flags = 0;
277 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000278 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000279 *flags = GrContext::kUnpremul_PixelOpsFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000280 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000281 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000282 *flags = 0;
283 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000284 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000285 *flags = GrContext::kUnpremul_PixelOpsFlag;
286 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000287 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000288 *flags = 0;
289 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000290 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000291 *flags = GrContext::kUnpremul_PixelOpsFlag;
292 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000293 default:
294 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000295 *flags = 0; // suppress warning
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000296 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000297 }
298}
299}
300
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000301bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
302 int x, int y,
303 SkCanvas::Config8888 config8888) {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000304 DO_DEFERRED_CLEAR();
bsalomon@google.com910267d2011-11-02 20:06:25 +0000305 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
306 SkASSERT(!bitmap.isNull());
307 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000308
bsalomon@google.com910267d2011-11-02 20:06:25 +0000309 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000310 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000311 uint32_t flags;
312 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000313 return fContext->readRenderTargetPixels(fRenderTarget,
314 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000315 bitmap.width(),
316 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000317 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000318 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000319 bitmap.rowBytes(),
320 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000321}
322
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000323void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
324 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000325 SkAutoLockPixels alp(bitmap);
326 if (!bitmap.readyToDraw()) {
327 return;
328 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000329
330 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000331 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000332 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000333 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000334 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000335 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000336 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000337 }
338
bsalomon@google.com6f379512011-11-16 20:36:03 +0000339 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000340 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000341}
342
robertphillips@google.com46f93502012-08-07 15:38:08 +0000343namespace {
humper@google.com05af1af2013-01-07 16:47:43 +0000344void purgeClipCB(int genID, void* ) {
robertphillips@google.com46f93502012-08-07 15:38:08 +0000345
346 if (SkClipStack::kInvalidGenID == genID ||
347 SkClipStack::kEmptyGenID == genID ||
348 SkClipStack::kWideOpenGenID == genID) {
349 // none of these cases will have a cached clip mask
350 return;
351 }
352
353}
354};
355
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000356void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
357 INHERITED::onAttachToCanvas(canvas);
358
359 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000360 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000361
362 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000363}
364
365void SkGpuDevice::onDetachFromCanvas() {
366 INHERITED::onDetachFromCanvas();
367
robertphillips@google.com46f93502012-08-07 15:38:08 +0000368 // TODO: iterate through the clip stack and clean up any cached clip masks
369 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
370
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000371 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000372}
373
robertphillips@google.com607fe072012-07-24 13:54:00 +0000374#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000375static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000376 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000377 int renderTargetWidth,
378 int renderTargetHeight) {
379
robertphillips@google.com7b112892012-07-31 15:18:21 +0000380 SkIRect devBound;
381
382 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
383
robertphillips@google.com607fe072012-07-24 13:54:00 +0000384 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000385 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000386
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000387 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000388 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000389 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000390
robertphillips@google.com7b112892012-07-31 15:18:21 +0000391 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000392
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000393 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000394
robertphillips@google.com7b112892012-07-31 15:18:21 +0000395 if (!devBound.intersect(devTemp)) {
396 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000397 }
398 }
399
robertphillips@google.com768fee82012-08-02 12:42:43 +0000400 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000401}
402#endif
403
reed@google.comac10a2d2010-12-22 21:39:39 +0000404///////////////////////////////////////////////////////////////////////////////
405
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000406// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000407// and not the state from some other canvas/device
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000408void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000409 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000410
reed@google.comac10a2d2010-12-22 21:39:39 +0000411 fContext->setRenderTarget(fRenderTarget);
412
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000413 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000414
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000415 if (forceIdentity) {
416 fContext->setIdentityMatrix();
417 } else {
418 fContext->setMatrix(*draw.fMatrix);
419 }
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000420 fClipData.fOrigin = this->getOrigin();
reed@google.comac10a2d2010-12-22 21:39:39 +0000421
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000422#ifdef SK_DEBUG
423 check_bounds(fClipData, *draw.fClip, fRenderTarget->width(), fRenderTarget->height());
424#endif
425
426 fContext->setClip(&fClipData);
427
428 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000429}
430
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000431GrRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000432 DO_DEFERRED_CLEAR();
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000433 return fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000434}
435
reed@google.comac10a2d2010-12-22 21:39:39 +0000436///////////////////////////////////////////////////////////////////////////////
437
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000438SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
439SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
440SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
441SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
442SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
443 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000444SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
445 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000446SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
447SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000448
bsalomon@google.com84405e02012-03-05 19:57:21 +0000449namespace {
450
451// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
452// justAlpha indicates that skPaint's alpha should be used rather than the color
453// Callers may subsequently modify the GrPaint. Setting constantColor indicates
454// that the final paint will draw the same color at every pixel. This allows
455// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000456// color once while converting to GrPaint and then ignored.
457inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
458 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000459 bool justAlpha,
460 bool constantColor,
461 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000462
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000463 grPaint->setDither(skPaint.isDither());
464 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000465
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000466 SkXfermode::Coeff sm;
467 SkXfermode::Coeff dm;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000468
469 SkXfermode* mode = skPaint.getXfermode();
bsalomon@google.comdb446252013-03-27 18:46:16 +0000470 GrEffectRef* xferEffect = NULL;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000471 if (SkXfermode::AsNewEffectOrCoeff(mode, dev->context(), &xferEffect, &sm, &dm)) {
472 if (NULL != xferEffect) {
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000473 grPaint->addColorEffect(xferEffect)->unref();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000474 sm = SkXfermode::kOne_Coeff;
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000475 dm = SkXfermode::kZero_Coeff;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000476 }
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000477 } else {
478 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000479#if 0
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000480 return false;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000481#else
482 // Fall back to src-over
483 sm = SkXfermode::kOne_Coeff;
484 dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000485#endif
bsalomon@google.com5782d712011-01-21 21:03:59 +0000486 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000487 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000488
bsalomon@google.com5782d712011-01-21 21:03:59 +0000489 if (justAlpha) {
490 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000491 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000492 // justAlpha is currently set to true only if there is a texture,
493 // so constantColor should not also be true.
494 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000495 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000496 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000497 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000498
Scroggo97c88c22011-05-11 14:05:25 +0000499 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000500 if (NULL != colorFilter) {
501 // if the source color is a constant then apply the filter here once rather than per pixel
502 // in a shader.
503 if (constantColor) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000504 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000505 grPaint->setColor(SkColor2GrColor(filtered));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000506 } else {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000507 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
bsalomon@google.com021fc732012-10-25 12:47:42 +0000508 if (NULL != effect.get()) {
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000509 grPaint->addColorEffect(effect);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000510 } else {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000511 // TODO: rewrite this using asNewEffect()
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000512 SkColor color;
513 SkXfermode::Mode filterMode;
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000514 if (colorFilter->asColorMode(&color, &filterMode)) {
515 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000516 }
517 }
Scroggod757df22011-05-16 13:11:16 +0000518 }
Scroggo97c88c22011-05-11 14:05:25 +0000519 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000520
bsalomon@google.com5782d712011-01-21 21:03:59 +0000521 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000522}
523
bsalomon@google.com84405e02012-03-05 19:57:21 +0000524// This function is similar to skPaint2GrPaintNoShader but also converts
bsalomon@google.com08283af2012-10-26 13:01:20 +0000525// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
bsalomon@google.com84405e02012-03-05 19:57:21 +0000526// be used is set on grPaint and returned in param act. constantColor has the
527// same meaning as in skPaint2GrPaintNoShader.
528inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
529 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000530 bool constantColor,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000531 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000532 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000533 if (NULL == shader) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000534 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000535 }
536
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000537 // setup the shader as the first color effect on the paint
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000538 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000539 if (NULL != effect.get()) {
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000540 grPaint->addColorEffect(effect);
541 // Now setup the rest of the paint.
542 return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
543 } else {
544 // We still don't have SkColorShader::asNewEffect() implemented.
545 SkShader::GradientInfo info;
546 SkColor color;
rileya@google.com91f319c2012-07-25 17:18:31 +0000547
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000548 info.fColors = &color;
549 info.fColorOffsets = NULL;
550 info.fColorCount = 1;
551 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
552 SkPaint copy(skPaint);
553 copy.setShader(NULL);
554 // modulate the paint alpha by the shader's solid color alpha
555 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
556 copy.setColor(SkColorSetA(color, newA));
557 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
558 } else {
559 return false;
560 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000561 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000562}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000563}
reed@google.comac10a2d2010-12-22 21:39:39 +0000564
565///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000566void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com32cf29e2013-01-25 15:03:18 +0000567 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
568 fContext->clear(&rect, SkColor2GrColor(color), fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000569 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000570}
571
reed@google.comac10a2d2010-12-22 21:39:39 +0000572void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000573 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000574
bsalomon@google.com5782d712011-01-21 21:03:59 +0000575 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000576 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000577 return;
578 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000579
580 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000581}
582
583// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000584static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000585 kPoints_GrPrimitiveType,
586 kLines_GrPrimitiveType,
587 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000588};
589
590void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000591 size_t count, const SkPoint pts[], const SkPaint& paint) {
epoger@google.comb58772f2013-03-08 09:09:10 +0000592 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000593 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000594
595 SkScalar width = paint.getStrokeWidth();
596 if (width < 0) {
597 return;
598 }
599
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000600 // we only handle hairlines and paints without path effects or mask filters,
601 // else we let the SkDraw call our drawPath()
602 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000603 draw.drawPoints(mode, count, pts, paint, true);
604 return;
605 }
606
bsalomon@google.com5782d712011-01-21 21:03:59 +0000607 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000608 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000609 return;
610 }
611
bsalomon@google.com5782d712011-01-21 21:03:59 +0000612 fContext->drawVertices(grPaint,
613 gPointMode2PrimtiveType[mode],
614 count,
615 (GrPoint*)pts,
616 NULL,
617 NULL,
618 NULL,
619 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000620}
621
reed@google.comc9aa5872011-04-05 21:05:37 +0000622///////////////////////////////////////////////////////////////////////////////
623
reed@google.comac10a2d2010-12-22 21:39:39 +0000624void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000625 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000626 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000627 CHECK_SHOULD_DRAW(draw, false);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000628
bungeman@google.com79bd8772011-07-18 15:34:08 +0000629 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000630 SkScalar width = paint.getStrokeWidth();
631
632 /*
633 We have special code for hairline strokes, miter-strokes, and fills.
634 Anything else we just call our path code.
635 */
636 bool usePath = doStroke && width > 0 &&
637 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000638 // another two reasons we might need to call drawPath...
639 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000640 usePath = true;
641 }
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000642 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000643#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000644 if (doStroke) {
645#endif
646 usePath = true;
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000647#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000648 } else {
649 usePath = !fContext->getMatrix().preservesRightAngles();
650 }
651#endif
reed@google.com67db6642011-05-26 11:46:35 +0000652 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000653 // small miter limit means right angles show bevel...
654 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
655 paint.getStrokeMiter() < SK_ScalarSqrt2)
656 {
657 usePath = true;
658 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000659 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000660 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
661 usePath = true;
662 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000663
664 if (usePath) {
665 SkPath path;
666 path.addRect(rect);
667 this->drawPath(draw, path, paint, NULL, true);
668 return;
669 }
670
671 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000672 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000673 return;
674 }
reed@google.com20efde72011-05-09 17:00:02 +0000675 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000676}
677
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000678///////////////////////////////////////////////////////////////////////////////
679
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000680void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
681 const SkPaint& paint) {
682 CHECK_FOR_NODRAW_ANNOTATION(paint);
683 CHECK_SHOULD_DRAW(draw, false);
684
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000685 bool usePath = !rect.isSimple();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000686 // another two reasons we might need to call drawPath...
687 if (paint.getMaskFilter() || paint.getPathEffect()) {
688 usePath = true;
689 }
690 // until we can rotate rrects...
691 if (!usePath && !fContext->getMatrix().rectStaysRect()) {
692 usePath = true;
693 }
694
695 if (usePath) {
696 SkPath path;
697 path.addRRect(rect);
698 this->drawPath(draw, path, paint, NULL, true);
699 return;
700 }
701
702 GrPaint grPaint;
703 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
704 return;
705 }
706
707 SkStrokeRec stroke(paint);
708 fContext->drawRRect(grPaint, rect, stroke);
709}
710
711///////////////////////////////////////////////////////////////////////////////
712
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000713void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
714 const SkPaint& paint) {
715 CHECK_FOR_NODRAW_ANNOTATION(paint);
716 CHECK_SHOULD_DRAW(draw, false);
717
718 bool usePath = false;
719 // some basic reasons we might need to call drawPath...
720 if (paint.getMaskFilter() || paint.getPathEffect()) {
721 usePath = true;
722 }
723
724 if (usePath) {
725 SkPath path;
726 path.addOval(oval);
727 this->drawPath(draw, path, paint, NULL, true);
728 return;
729 }
730
731 GrPaint grPaint;
732 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
733 return;
734 }
735 SkStrokeRec stroke(paint);
736
737 fContext->drawOval(grPaint, oval, stroke);
738}
739
reed@google.com69302852011-02-16 18:08:07 +0000740#include "SkMaskFilter.h"
741#include "SkBounder.h"
742
bsalomon@google.com85003222012-03-28 14:44:37 +0000743///////////////////////////////////////////////////////////////////////////////
744
745// helpers for applying mask filters
746namespace {
747
robertphillips@google.com49149312013-07-03 15:34:35 +0000748// Draw a mask using the supplied paint. Since the coverage/geometry
749// is already burnt into the mask this boils down to a rect draw.
750// Return true if the mask was successfully drawn.
751bool draw_mask(GrContext* context, const SkRect& maskRect,
752 GrPaint* grp, GrTexture* mask) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000753 GrContext::AutoMatrix am;
754 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000755 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000756 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000757
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000758 SkMatrix matrix;
robertphillips@google.com49149312013-07-03 15:34:35 +0000759 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
760 matrix.postIDiv(mask->width(), mask->height());
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000761
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000762 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
robertphillips@google.com49149312013-07-03 15:34:35 +0000763 context->drawRect(*grp, maskRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000764 return true;
765}
766
robertphillips@google.com49149312013-07-03 15:34:35 +0000767bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
768 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
769 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000770 SkMask srcM, dstM;
771
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000772 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
773 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000774 return false;
775 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000776 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000777
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000778 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000779 return false;
780 }
781 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000782 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000783
784 if (clip.quickReject(dstM.fBounds)) {
785 return false;
786 }
787 if (bounder && !bounder->doIRect(dstM.fBounds)) {
788 return false;
789 }
790
791 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000792 // the current clip (and identity matrix) and GrPaint settings
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000793 GrTextureDesc desc;
794 desc.fWidth = dstM.fBounds.width();
795 desc.fHeight = dstM.fBounds.height();
796 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000797
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000798 GrAutoScratchTexture ast(context, desc);
799 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000800
reed@google.com69302852011-02-16 18:08:07 +0000801 if (NULL == texture) {
802 return false;
803 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000804 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000805 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000806
robertphillips@google.com49149312013-07-03 15:34:35 +0000807 SkRect maskRect = SkRect::MakeFromIRect(dstM.fBounds);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000808
robertphillips@google.com49149312013-07-03 15:34:35 +0000809 return draw_mask(context, maskRect, grp, texture);
810}
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000811
robertphillips@google.com49149312013-07-03 15:34:35 +0000812// Create a mask of 'devPath' and place the result in 'mask'. Return true on
813// success; false otherwise.
814bool create_mask_GPU(GrContext* context,
815 const SkRect& maskRect,
816 const SkPath& devPath,
817 const SkStrokeRec& stroke,
818 bool doAA,
819 GrAutoScratchTexture* mask) {
820 GrTextureDesc desc;
821 desc.fFlags = kRenderTarget_GrTextureFlagBit;
822 desc.fWidth = SkScalarCeilToInt(maskRect.width());
823 desc.fHeight = SkScalarCeilToInt(maskRect.height());
824 // We actually only need A8, but it often isn't supported as a
825 // render target so default to RGBA_8888
826 desc.fConfig = kRGBA_8888_GrPixelConfig;
827 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
828 desc.fConfig = kAlpha_8_GrPixelConfig;
829 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000830
robertphillips@google.com49149312013-07-03 15:34:35 +0000831 mask->set(context, desc);
832 if (NULL == mask->texture()) {
833 return false;
834 }
835
836 GrTexture* maskTexture = mask->texture();
837 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
838
839 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
840 GrContext::AutoClip ac(context, clipRect);
841
842 context->clear(NULL, 0x0);
843
844 GrPaint tempPaint;
845 if (doAA) {
846 tempPaint.setAntiAlias(true);
847 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
848 // blend coeff of zero requires dual source blending support in order
849 // to properly blend partially covered pixels. This means the AA
850 // code path may not be taken. So we use a dst blend coeff of ISA. We
851 // could special case AA draws to a dst surface with known alpha=0 to
852 // use a zero dst coeff when dual source blending isn't available.
853 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
854 }
855
856 GrContext::AutoMatrix am;
857
858 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
859 SkMatrix translate;
860 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
861 am.set(context, translate);
862 context->drawPath(tempPaint, devPath, stroke);
reed@google.com69302852011-02-16 18:08:07 +0000863 return true;
864}
reed@google.com69302852011-02-16 18:08:07 +0000865
robertphillips@google.com49149312013-07-03 15:34:35 +0000866SkBitmap wrap_texture(GrTexture* texture) {
867 SkBitmap result;
868 bool dummy;
869 SkBitmap::Config config = grConfig2skConfig(texture->config(), &dummy);
870 result.setConfig(config, texture->width(), texture->height());
871 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (texture)))->unref();
872 return result;
bsalomon@google.com85003222012-03-28 14:44:37 +0000873}
874
robertphillips@google.com49149312013-07-03 15:34:35 +0000875};
bsalomon@google.com85003222012-03-28 14:44:37 +0000876
reed@google.com0c219b62011-02-16 21:31:18 +0000877void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000878 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000879 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000880 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000881 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000882
bsalomon@google.com5782d712011-01-21 21:03:59 +0000883 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000884 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000885 return;
886 }
887
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000888 // can we cheat, and treat a thin stroke as a hairline w/ coverage
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000889 // if we can, we draw lots faster (raster device does this same test)
890 SkScalar hairlineCoverage;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000891 bool doHairLine = SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage);
892 if (doHairLine) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000893 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000894 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000895
reed@google.comfe626382011-09-21 13:50:35 +0000896 // If we have a prematrix, apply it to the path, optimizing for the case
897 // where the original path can in fact be modified in place (even though
898 // its parameter type is const).
899 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000900 SkPath tmpPath, effectPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000901
902 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +0000903 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +0000904
reed@google.come3445642011-02-16 23:20:39 +0000905 if (!pathIsMutable) {
906 result = &tmpPath;
907 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000908 }
reed@google.come3445642011-02-16 23:20:39 +0000909 // should I push prePathMatrix on our MV stack temporarily, instead
910 // of applying it here? See SkDraw.cpp
911 pathPtr->transform(*prePathMatrix, result);
912 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +0000913 }
reed@google.com0c219b62011-02-16 21:31:18 +0000914 // at this point we're done with prePathMatrix
915 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +0000916
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000917 SkStrokeRec stroke(paint);
918 SkPathEffect* pathEffect = paint.getPathEffect();
reed@google.com4bbdeac2013-01-24 21:03:11 +0000919 const SkRect* cullRect = NULL; // TODO: what is our bounds?
920 if (pathEffect && pathEffect->filterPath(&effectPath, *pathPtr, &stroke,
921 cullRect)) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000922 pathPtr = &effectPath;
923 }
924
925 if (!pathEffect && doHairLine) {
926 stroke.setHairlineStyle();
reed@google.com0c219b62011-02-16 21:31:18 +0000927 }
928
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000929 if (paint.getMaskFilter()) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000930 if (!stroke.isHairlineStyle()) {
931 if (stroke.applyToPath(&tmpPath, *pathPtr)) {
932 pathPtr = &tmpPath;
robertphillips@google.com49149312013-07-03 15:34:35 +0000933 pathIsMutable = true;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000934 stroke.setFillStyle();
935 }
936 }
937
reed@google.com0c219b62011-02-16 21:31:18 +0000938 // avoid possibly allocating a new path in transform if we can
939 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
940
941 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000942 pathPtr->transform(fContext->getMatrix(), devPathPtr);
robertphillips@google.com49149312013-07-03 15:34:35 +0000943
944 SkRect maskRect;
945 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
946 draw.fClip->getBounds(),
947 fContext->getMatrix(),
948 &maskRect)) {
949 SkIRect finalIRect;
950 maskRect.roundOut(&finalIRect);
951 if (draw.fClip->quickReject(finalIRect)) {
952 // clipped out
953 return;
954 }
955 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
956 // nothing to draw
957 return;
958 }
959
960 GrAutoScratchTexture mask;
961
skia.committer@gmail.com1842adf2013-07-04 07:01:07 +0000962 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
robertphillips@google.com49149312013-07-03 15:34:35 +0000963 grPaint.isAntiAlias(), &mask)) {
964 GrTexture* filtered;
965
966 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(), maskRect, &filtered, true)) {
967 SkAutoTUnref<GrTexture> atu(filtered);
968
969 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
970 // This path is completely drawn
971 return;
972 }
973 }
974 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000975 }
robertphillips@google.com49149312013-07-03 15:34:35 +0000976
977 // draw the mask on the CPU - this is a fallthrough path in case the
978 // GPU path fails
979 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
980 SkPaint::kFill_Style;
981 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
982 *draw.fClip, draw.fBounder, &grPaint, style);
reed@google.com69302852011-02-16 18:08:07 +0000983 return;
984 }
reed@google.com69302852011-02-16 18:08:07 +0000985
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000986 fContext->drawPath(grPaint, *pathPtr, stroke);
reed@google.comac10a2d2010-12-22 21:39:39 +0000987}
988
bsalomon@google.comfb309512011-11-30 14:13:48 +0000989namespace {
990
991inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
992 int tilesX = (r / tileSize) - (l / tileSize) + 1;
993 int tilesY = (b / tileSize) - (t / tileSize) + 1;
994 return tilesX * tilesY;
995}
996
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000997inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +0000998 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +0000999 int maxTextureSize) {
1000 static const int kSmallTileSize = 1 << 10;
1001 if (maxTextureSize <= kSmallTileSize) {
1002 return maxTextureSize;
1003 }
1004
1005 size_t maxTexTotalTileSize;
1006 size_t smallTotalTileSize;
1007
robertphillips@google.combac6b052012-09-28 18:06:49 +00001008 SkIRect iSrc;
1009 src.roundOut(&iSrc);
1010
1011 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1012 iSrc.fTop,
1013 iSrc.fRight,
1014 iSrc.fBottom,
1015 maxTextureSize);
1016 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1017 iSrc.fTop,
1018 iSrc.fRight,
1019 iSrc.fBottom,
1020 kSmallTileSize);
1021
bsalomon@google.comfb309512011-11-30 14:13:48 +00001022 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1023 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1024
1025 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1026 return kSmallTileSize;
1027 } else {
1028 return maxTextureSize;
1029 }
1030}
1031}
1032
1033bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001034 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001035 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001036 // if bitmap is explictly texture backed then just use the texture
1037 if (NULL != bitmap.getTexture()) {
1038 return false;
1039 }
1040 // if it's larger than the max texture size, then we have no choice but
1041 // tiling
1042 const int maxTextureSize = fContext->getMaxTextureSize();
1043 if (bitmap.width() > maxTextureSize ||
1044 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001045 return true;
1046 }
1047 // if we are going to have to draw the whole thing, then don't tile
1048 if (NULL == srcRectPtr) {
1049 return false;
1050 }
1051 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001052 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001053 return false;
1054 }
1055
1056 // At this point we know we could do the draw by uploading the entire bitmap
1057 // as a texture. However, if the texture would be large compared to the
1058 // cache size and we don't require most of it for this draw then tile to
1059 // reduce the amount of upload and cache spill.
1060
1061 // assumption here is that sw bitmap size is a good proxy for its size as
1062 // a texture
1063 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001064 size_t cacheSize;
1065 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001066 if (bmpSize < cacheSize / 2) {
1067 return false;
1068 }
1069
robertphillips@google.combac6b052012-09-28 18:06:49 +00001070 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1071 srcRectPtr->height() / bitmap.height());
1072 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001073 return true;
1074 } else {
1075 return false;
1076 }
1077}
1078
reed@google.comac10a2d2010-12-22 21:39:39 +00001079void SkGpuDevice::drawBitmap(const SkDraw& draw,
1080 const SkBitmap& bitmap,
1081 const SkIRect* srcRectPtr,
1082 const SkMatrix& m,
1083 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001084
1085 SkRect tmp;
1086 SkRect* tmpPtr = NULL;
1087
1088 // convert from SkIRect to SkRect
1089 if (NULL != srcRectPtr) {
1090 tmp.set(*srcRectPtr);
1091 tmpPtr = &tmp;
1092 }
1093
1094 // We cannot call drawBitmapRect here since 'm' could be anything
1095 this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint);
1096}
1097
1098void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1099 const SkBitmap& bitmap,
1100 const SkRect* srcRectPtr,
1101 const SkMatrix& m,
1102 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001103 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001104
robertphillips@google.combac6b052012-09-28 18:06:49 +00001105 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001106 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001107 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001108 } else {
1109 srcRect = *srcRectPtr;
1110 }
1111
junov@google.comd935cfb2011-06-27 20:48:23 +00001112 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001113 // Convert the bitmap to a shader so that the rect can be drawn
1114 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001115 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001116 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001117 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001118 if (NULL != srcRectPtr) {
1119 SkIRect iSrc;
1120 srcRect.roundOut(&iSrc);
1121 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001122 return; // extraction failed
1123 }
1124 bitmapPtr = &tmp;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001125 srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop));
1126 // The source rect has changed so update the matrix
1127 newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop));
junov@google.comd935cfb2011-06-27 20:48:23 +00001128 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001129
junov@google.comd935cfb2011-06-27 20:48:23 +00001130 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001131 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001132 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001133
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001134 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001135 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001136 // also affect the behavior of the mask filter.
1137 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001138 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001139 SkDraw transformedDraw(draw);
1140 transformedDraw.fMatrix = &drawMatrix;
1141
robertphillips@google.combac6b052012-09-28 18:06:49 +00001142 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001143
junov@google.comd935cfb2011-06-27 20:48:23 +00001144 return;
1145 }
1146
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001147 GrTextureParams params;
1148 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001149
robertphillips@google.combac6b052012-09-28 18:06:49 +00001150 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001151 // take the simple case
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001152 this->internalDrawBitmap(bitmap, srcRect, m, params, paint);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001153 } else {
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001154 this->drawTiledBitmap(bitmap, srcRect, m, params, paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001155 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001156}
1157
1158// Break 'bitmap' into several tiles to draw it since it has already
1159// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001160void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001161 const SkRect& srcRect,
1162 const SkMatrix& m,
1163 const GrTextureParams& params,
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001164 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001165 const int maxTextureSize = fContext->getMaxTextureSize();
1166
1167 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001168
reed@google.comac10a2d2010-12-22 21:39:39 +00001169 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001170 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001171 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001172 const GrRenderTarget* rt = fContext->getRenderTarget();
1173 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1174 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1175 return;
1176 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001177 SkMatrix matrix, inverse;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001178 matrix.setConcat(fContext->getMatrix(), m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001179 if (!matrix.invert(&inverse)) {
1180 return;
1181 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001182 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001183 }
1184
bsalomon@google.comfb309512011-11-30 14:13:48 +00001185 int nx = bitmap.width() / tileSize;
1186 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001187 for (int x = 0; x <= nx; x++) {
1188 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001189 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001190 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001191 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001192 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001193 SkIntToScalar((y + 1) * tileSize));
1194
1195 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001196 continue;
1197 }
1198
robertphillips@google.combac6b052012-09-28 18:06:49 +00001199 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001200 continue;
1201 }
1202
1203 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001204 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001205 tileR.roundOut(&iTileR);
1206 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001207 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.combac6b052012-09-28 18:06:49 +00001208 tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop));
reed@google.comac10a2d2010-12-22 21:39:39 +00001209 SkMatrix tmpM(m);
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +00001210 tmpM.preTranslate(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001211 SkIntToScalar(iTileR.fTop));
1212
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001213 this->internalDrawBitmap(tmpB, tileR, tmpM, params, paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001214 }
1215 }
1216 }
1217}
1218
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001219namespace {
1220
1221bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1222 // detect pixel disalignment
1223 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1224 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001225 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001226 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1227 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1228 COLOR_BLEED_TOLERANCE &&
1229 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1230 COLOR_BLEED_TOLERANCE) {
1231 return true;
1232 }
1233 return false;
1234}
1235
1236bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1237 const SkMatrix& m) {
1238 // Only gets called if hasAlignedSamples returned false.
1239 // So we can assume that sampling is axis aligned but not texel aligned.
1240 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001241 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001242 outerTransformedRect(transformedRect);
1243 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1244 m.mapRect(&innerTransformedRect, innerSrcRect);
1245
1246 // The gap between outerTransformedRect and innerTransformedRect
1247 // represents the projection of the source border area, which is
1248 // problematic for color bleeding. We must check whether any
1249 // destination pixels sample the border area.
1250 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1251 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1252 SkIRect outer, inner;
1253 outerTransformedRect.round(&outer);
1254 innerTransformedRect.round(&inner);
1255 // If the inner and outer rects round to the same result, it means the
1256 // border does not overlap any pixel centers. Yay!
1257 return inner != outer;
1258}
1259
1260} // unnamed namespace
1261
reed@google.comac10a2d2010-12-22 21:39:39 +00001262/*
1263 * This is called by drawBitmap(), which has to handle images that may be too
1264 * large to be represented by a single texture.
1265 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001266 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1267 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001268 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001269void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001270 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001271 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001272 const GrTextureParams& params,
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001273 const SkPaint& paint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001274 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1275 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001276
reed@google.comac10a2d2010-12-22 21:39:39 +00001277 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001278 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001279 if (NULL == texture) {
1280 return;
1281 }
1282
robertphillips@google.combac6b052012-09-28 18:06:49 +00001283 GrRect dstRect(srcRect);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001284 GrRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001285 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1286 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1287 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1288 SkScalarMul(srcRect.fTop, hInv),
1289 SkScalarMul(srcRect.fRight, wInv),
1290 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001291
rmistry@google.comd6176b02012-08-23 18:14:13 +00001292 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001293 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001294 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001295 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001296 srcRect.height() < bitmap.height();
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001297 if (m.rectStaysRect() && fContext->getMatrix().rectStaysRect()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001298 // sampling is axis-aligned
robertphillips@google.combac6b052012-09-28 18:06:49 +00001299 GrRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001300 SkMatrix srcToDeviceMatrix(m);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001301 srcToDeviceMatrix.postConcat(fContext->getMatrix());
robertphillips@google.combac6b052012-09-28 18:06:49 +00001302 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001303
robertphillips@google.combac6b052012-09-28 18:06:49 +00001304 if (hasAlignedSamples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001305 // We could also turn off filtering here (but we already did a cache lookup with
1306 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001307 needsTextureDomain = false;
1308 } else {
1309 needsTextureDomain = needsTextureDomain &&
robertphillips@google.combac6b052012-09-28 18:06:49 +00001310 mayColorBleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001311 }
1312 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001313 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001314
1315 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001316 SkAutoTUnref<GrEffectRef> effect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001317 if (needsTextureDomain) {
1318 // Use a constrained texture domain to avoid color bleeding
bsalomon@google.com81712882012-11-01 17:12:34 +00001319 SkScalar left, top, right, bottom;
1320 if (srcRect.width() > SK_Scalar1) {
1321 SkScalar border = SK_ScalarHalf / bitmap.width();
junov@google.com6acc9b32011-05-16 18:32:07 +00001322 left = paintRect.left() + border;
1323 right = paintRect.right() - border;
1324 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001325 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
junov@google.com6acc9b32011-05-16 18:32:07 +00001326 }
bsalomon@google.com81712882012-11-01 17:12:34 +00001327 if (srcRect.height() > SK_Scalar1) {
1328 SkScalar border = SK_ScalarHalf / bitmap.height();
junov@google.com6acc9b32011-05-16 18:32:07 +00001329 top = paintRect.top() + border;
1330 bottom = paintRect.bottom() - border;
1331 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001332 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
junov@google.com6acc9b32011-05-16 18:32:07 +00001333 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001334 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001335 effect.reset(GrTextureDomainEffect::Create(texture,
1336 SkMatrix::I(),
1337 textureDomain,
1338 GrTextureDomainEffect::kClamp_WrapMode,
1339 params.isBilerp()));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001340 } else {
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001341 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
junov@google.com6acc9b32011-05-16 18:32:07 +00001342 }
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001343
1344 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1345 // the rest from the SkPaint.
1346 GrPaint grPaint;
1347 grPaint.addColorEffect(effect);
1348 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1349 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1350 return;
1351 }
1352
1353 fContext->drawRectToRect(grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001354}
1355
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001356static bool filter_texture(SkDevice* device, GrContext* context,
1357 GrTexture* texture, SkImageFilter* filter,
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001358 int w, int h, SkBitmap* result, SkIPoint* offset) {
reed@google.com8926b162012-03-23 15:36:36 +00001359 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001360 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001361
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001362 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org985fa792012-10-24 15:14:26 +00001363 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1364 // filter. Also set the clip wide open and the matrix to identity.
1365 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001366 return filter->filterImageGPU(&proxy, wrap_texture(texture), result, offset);
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001367 } else {
1368 return false;
reed@google.com8926b162012-03-23 15:36:36 +00001369 }
reed@google.com8926b162012-03-23 15:36:36 +00001370}
1371
reed@google.comac10a2d2010-12-22 21:39:39 +00001372void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001373 int left, int top, const SkPaint& paint) {
1374 // drawSprite is defined to be in device coords.
1375 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001376
reed@google.com8926b162012-03-23 15:36:36 +00001377 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001378 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1379 return;
1380 }
1381
reed@google.com76dd2772012-01-05 21:15:07 +00001382 int w = bitmap.width();
1383 int h = bitmap.height();
1384
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001385 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001386 // draw sprite uses the default texture params
1387 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001388
reed@google.com8926b162012-03-23 15:36:36 +00001389 SkImageFilter* filter = paint.getImageFilter();
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001390 SkIPoint offset = SkIPoint::Make(0, 0);
reed@google.com8926b162012-03-23 15:36:36 +00001391 if (NULL != filter) {
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001392 SkBitmap filterBitmap;
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001393 if (filter_texture(this, fContext, texture, filter, w, h, &filterBitmap, &offset)) {
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001394 texture = (GrTexture*) filterBitmap.getTexture();
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001395 w = filterBitmap.width();
1396 h = filterBitmap.height();
reed@google.com8926b162012-03-23 15:36:36 +00001397 }
reed@google.com76dd2772012-01-05 21:15:07 +00001398 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001399
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001400 GrPaint grPaint;
1401 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1402
1403 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1404 return;
1405 }
1406
bsalomon@google.com5782d712011-01-21 21:03:59 +00001407 fContext->drawRectToRect(grPaint,
robertphillips@google.com31a40ef2013-07-11 00:01:39 +00001408 GrRect::MakeXYWH(SkIntToScalar(left),
1409 SkIntToScalar(top),
1410 SkIntToScalar(w),
1411 SkIntToScalar(h)),
1412 GrRect::MakeXYWH(SkIntToScalar(offset.fX),
1413 SkIntToScalar(offset.fY),
1414 SK_Scalar1 * w / texture->width(),
1415 SK_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001416}
1417
reed@google.com33535f32012-09-25 15:37:50 +00001418void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1419 const SkRect* src, const SkRect& dst,
1420 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001421 SkMatrix matrix;
1422 SkRect bitmapBounds, tmpSrc;
1423
1424 bitmapBounds.set(0, 0,
1425 SkIntToScalar(bitmap.width()),
1426 SkIntToScalar(bitmap.height()));
1427
reed@google.com33535f32012-09-25 15:37:50 +00001428 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001429 if (NULL != src) {
1430 tmpSrc = *src;
1431 } else {
1432 tmpSrc = bitmapBounds;
1433 }
1434 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1435
1436 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1437 if (NULL != src) {
1438 if (!bitmapBounds.contains(tmpSrc)) {
1439 if (!tmpSrc.intersect(bitmapBounds)) {
1440 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001441 }
reed@google.com33535f32012-09-25 15:37:50 +00001442 }
reed@google.com33535f32012-09-25 15:37:50 +00001443 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001444
robertphillips@google.combac6b052012-09-28 18:06:49 +00001445 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint);
reed@google.com33535f32012-09-25 15:37:50 +00001446}
1447
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001448void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001449 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001450 // clear of the source device must occur before CHECK_SHOULD_DRAW
1451 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1452 if (dev->fNeedClear) {
1453 // TODO: could check here whether we really need to draw at all
1454 dev->clear(0x0);
1455 }
1456
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001457 // drawDevice is defined to be in device coords.
1458 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001459
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001460 GrRenderTarget* devRT = device->accessRenderTarget();
1461 GrTexture* devTex;
1462 if (NULL == (devTex = devRT->asTexture())) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001463 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001464 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001465
senorblanco@chromium.org514b9222013-01-18 21:53:12 +00001466 const SkBitmap& bm = dev->accessBitmap(false);
1467 int w = bm.width();
1468 int h = bm.height();
1469
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001470 SkImageFilter* filter = paint.getImageFilter();
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001471
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001472 if (NULL != filter) {
1473 SkBitmap filterBitmap;
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001474 SkIPoint offset = SkIPoint::Make(0, 0);
1475 if (filter_texture(this, fContext, devTex, filter, w, h, &filterBitmap, &offset)) {
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +00001476 devTex = filterBitmap.getTexture();
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001477 w = filterBitmap.width();
1478 h = filterBitmap.height();
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001479 x += offset.fX;
1480 y += offset.fY;
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001481 }
1482 }
1483
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001484 GrPaint grPaint;
1485 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1486
1487 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1488 return;
1489 }
1490
bsalomon@google.com81712882012-11-01 17:12:34 +00001491 GrRect dstRect = GrRect::MakeXYWH(SkIntToScalar(x),
1492 SkIntToScalar(y),
1493 SkIntToScalar(w),
1494 SkIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001495
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001496 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1497 // scratch texture).
bsalomon@google.com81712882012-11-01 17:12:34 +00001498 GrRect srcRect = GrRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1499 SK_Scalar1 * h / devTex->height());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001500
1501 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001502}
1503
reed@google.com8926b162012-03-23 15:36:36 +00001504bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
senorblanco@chromium.orgd043cce2013-04-08 19:43:22 +00001505 return filter->canFilterImageGPU();
reed@google.com8926b162012-03-23 15:36:36 +00001506}
1507
1508bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1509 const SkMatrix& ctm,
1510 SkBitmap* result, SkIPoint* offset) {
1511 // want explicitly our impl, so guard against a subclass of us overriding it
1512 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001513 return false;
1514 }
reed@google.com8926b162012-03-23 15:36:36 +00001515
1516 SkAutoLockPixels alp(src, !src.getTexture());
1517 if (!src.getTexture() && !src.readyToDraw()) {
1518 return false;
1519 }
1520
reed@google.com8926b162012-03-23 15:36:36 +00001521 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001522 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1523 // must be pushed upstack.
1524 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001525
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001526 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), result, offset);
reed@google.com76dd2772012-01-05 21:15:07 +00001527}
1528
reed@google.comac10a2d2010-12-22 21:39:39 +00001529///////////////////////////////////////////////////////////////////////////////
1530
1531// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001532static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001533 kTriangles_GrPrimitiveType,
1534 kTriangleStrip_GrPrimitiveType,
1535 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001536};
1537
1538void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1539 int vertexCount, const SkPoint vertices[],
1540 const SkPoint texs[], const SkColor colors[],
1541 SkXfermode* xmode,
1542 const uint16_t indices[], int indexCount,
1543 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001544 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001545
bsalomon@google.com5782d712011-01-21 21:03:59 +00001546 GrPaint grPaint;
bsalomon@google.com5782d712011-01-21 21:03:59 +00001547 // we ignore the shader if texs is null.
1548 if (NULL == texs) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001549 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001550 return;
1551 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001552 } else {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001553 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001554 return;
1555 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001556 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001557
1558 if (NULL != xmode && NULL != texs && NULL != colors) {
reed@google.com8d3cd7a2013-01-30 21:36:11 +00001559 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001560 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1561#if 0
1562 return
1563#endif
1564 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001565 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001566
bsalomon@google.com498776a2011-08-16 19:20:44 +00001567 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1568 if (NULL != colors) {
1569 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001570 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001571 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001572 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001573 }
1574 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001575 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001576 fContext->drawVertices(grPaint,
1577 gVertexMode2PrimitiveType[vmode],
1578 vertexCount,
1579 (GrPoint*) vertices,
1580 (GrPoint*) texs,
1581 colors,
1582 indices,
1583 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001584}
1585
1586///////////////////////////////////////////////////////////////////////////////
1587
1588static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001589 GrFontScaler* scaler = (GrFontScaler*)data;
1590 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001591}
1592
1593static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1594 void* auxData;
1595 GrFontScaler* scaler = NULL;
1596 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1597 scaler = (GrFontScaler*)auxData;
1598 }
1599 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001600 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001601 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1602 }
1603 return scaler;
1604}
1605
1606static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1607 SkFixed fx, SkFixed fy,
1608 const SkGlyph& glyph) {
1609 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1610
bungeman@google.com15865a72012-01-11 16:28:04 +00001611 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001612
1613 if (NULL == procs->fFontScaler) {
1614 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1615 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001616
bungeman@google.com15865a72012-01-11 16:28:04 +00001617 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1618 glyph.getSubXFixed(),
1619 glyph.getSubYFixed()),
1620 SkFixedFloorToFixed(fx),
1621 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001622 procs->fFontScaler);
1623}
1624
bsalomon@google.com5782d712011-01-21 21:03:59 +00001625SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001626
1627 // deferred allocation
1628 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001629 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001630 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1631 fDrawProcs->fContext = fContext;
1632 }
1633
1634 // init our (and GL's) state
1635 fDrawProcs->fTextContext = context;
1636 fDrawProcs->fFontScaler = NULL;
1637 return fDrawProcs;
1638}
1639
1640void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1641 size_t byteLength, SkScalar x, SkScalar y,
1642 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001643 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001644
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001645 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001646 // this guy will just call our drawPath()
1647 draw.drawText((const char*)text, byteLength, x, y, paint);
1648 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001649 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001650
1651 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001652 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001653 return;
1654 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001655 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001656 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001657 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1658 }
1659}
1660
1661void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1662 size_t byteLength, const SkScalar pos[],
1663 SkScalar constY, int scalarsPerPos,
1664 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001665 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001666
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001667 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001668 // this guy will just call our drawPath()
1669 draw.drawPosText((const char*)text, byteLength, pos, constY,
1670 scalarsPerPos, paint);
1671 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001672 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001673
1674 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001675 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001676 return;
1677 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001678 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001679 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001680 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1681 scalarsPerPos, paint);
1682 }
1683}
1684
1685void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1686 size_t len, const SkPath& path,
1687 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001688 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001689
1690 SkASSERT(draw.fDevice == this);
1691 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1692}
1693
1694///////////////////////////////////////////////////////////////////////////////
1695
reed@google.comf67e4cf2011-03-15 20:56:58 +00001696bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1697 if (!paint.isLCDRenderText()) {
1698 // we're cool with the paint as is
1699 return false;
1700 }
1701
1702 if (paint.getShader() ||
1703 paint.getXfermode() || // unless its srcover
1704 paint.getMaskFilter() ||
1705 paint.getRasterizer() ||
1706 paint.getColorFilter() ||
1707 paint.getPathEffect() ||
1708 paint.isFakeBoldText() ||
1709 paint.getStyle() != SkPaint::kFill_Style) {
1710 // turn off lcd
1711 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1712 flags->fHinting = paint.getHinting();
1713 return true;
1714 }
1715 // we're cool with the paint as is
1716 return false;
1717}
1718
reed@google.com75d939b2011-12-07 15:07:23 +00001719void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001720 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001721 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001722}
1723
reed@google.comf67e4cf2011-03-15 20:56:58 +00001724///////////////////////////////////////////////////////////////////////////////
1725
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001726SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1727 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001728 bool isOpaque,
1729 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001730 GrTextureDesc desc;
1731 desc.fConfig = fRenderTarget->config();
1732 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1733 desc.fWidth = width;
1734 desc.fHeight = height;
1735 desc.fSampleCnt = fRenderTarget->numSamples();
1736
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001737 SkAutoTUnref<GrTexture> texture;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001738 // Skia's convention is to only clear a device if it is non-opaque.
1739 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001740
1741#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1742 // layers are never draw in repeat modes, so we can request an approx
1743 // match and ignore any padding.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001744 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1745 GrContext::kApprox_ScratchTexMatch :
1746 GrContext::kExact_ScratchTexMatch;
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001747 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001748#else
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001749 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001750#endif
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001751 if (NULL != texture.get()) {
1752 return SkNEW_ARGS(SkGpuDevice,(fContext, texture, needClear));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001753 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001754 GrPrintf("---- failed to create compatible device texture [%d %d]\n", width, height);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001755 return NULL;
1756 }
1757}
1758
1759SkGpuDevice::SkGpuDevice(GrContext* context,
1760 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001761 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001762 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1763
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001764 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001765 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1766 // cache. We pass true for the third argument so that it will get unlocked.
1767 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001768 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001769}