blob: a5c261c75d31fe15d7a5a060a29626c2d5840e6b [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"
humper@google.comb86add12013-07-25 18:49:07 +000027#include "SkErrorInternals.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000028
bsalomon@google.com06cd7322012-03-30 18:45:35 +000029#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000030
31#if 0
32 extern bool (*gShouldDrawProc)();
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000033 #define CHECK_SHOULD_DRAW(draw, forceI) \
reed@google.comac10a2d2010-12-22 21:39:39 +000034 do { \
35 if (gShouldDrawProc && !gShouldDrawProc()) return; \
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000036 this->prepareDraw(draw, forceI); \
reed@google.comac10a2d2010-12-22 21:39:39 +000037 } while (0)
38#else
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000039 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
reed@google.comac10a2d2010-12-22 21:39:39 +000040#endif
41
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000042// This constant represents the screen alignment criterion in texels for
rmistry@google.comd6176b02012-08-23 18:14:13 +000043// requiring texture domain clamping to prevent color bleeding when drawing
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000044// a sub region of a larger source image.
45#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000046
junov@google.comdbfac8a2012-12-06 21:47:40 +000047#define DO_DEFERRED_CLEAR() \
48 do { \
49 if (fNeedClear) { \
50 this->clear(SK_ColorTRANSPARENT); \
51 } \
52 } while (false) \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000053
reed@google.comac10a2d2010-12-22 21:39:39 +000054///////////////////////////////////////////////////////////////////////////////
55
reed@google.comb0a34d82012-07-11 19:57:55 +000056#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
57 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
58
59///////////////////////////////////////////////////////////////////////////////
60
61
bsalomon@google.com84405e02012-03-05 19:57:21 +000062class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
63public:
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000064 SkAutoCachedTexture()
65 : fDevice(NULL)
66 , fTexture(NULL) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000067 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000068
bsalomon@google.com84405e02012-03-05 19:57:21 +000069 SkAutoCachedTexture(SkGpuDevice* device,
70 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000071 const GrTextureParams* params,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000072 GrTexture** texture)
73 : fDevice(NULL)
74 , fTexture(NULL) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000075 SkASSERT(NULL != texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000076 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000077 }
reed@google.comac10a2d2010-12-22 21:39:39 +000078
bsalomon@google.com84405e02012-03-05 19:57:21 +000079 ~SkAutoCachedTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000080 if (NULL != fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +000081 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
bsalomon@google.com84405e02012-03-05 19:57:21 +000082 }
reed@google.comac10a2d2010-12-22 21:39:39 +000083 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000084
85 GrTexture* set(SkGpuDevice* device,
86 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000087 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000088 if (NULL != fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +000089 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000090 fTexture = NULL;
bsalomon@google.com84405e02012-03-05 19:57:21 +000091 }
92 fDevice = device;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000093 GrTexture* result = (GrTexture*)bitmap.getTexture();
94 if (NULL == result) {
95 // Cannot return the native texture so look it up in our cache
bsalomon@google.com95ed55a2013-01-24 14:46:47 +000096 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000097 result = fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +000098 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000099 return result;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000100 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101
bsalomon@google.com84405e02012-03-05 19:57:21 +0000102private:
103 SkGpuDevice* fDevice;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000104 GrTexture* fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000105};
reed@google.comac10a2d2010-12-22 21:39:39 +0000106
107///////////////////////////////////////////////////////////////////////////////
108
reed@google.comac10a2d2010-12-22 21:39:39 +0000109struct GrSkDrawProcs : public SkDrawProcs {
110public:
111 GrContext* fContext;
112 GrTextContext* fTextContext;
113 GrFontScaler* fFontScaler; // cached in the skia glyphcache
114};
115
116///////////////////////////////////////////////////////////////////////////////
117
reed@google.comaf951c92011-06-16 19:10:39 +0000118static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
119 switch (config) {
120 case kAlpha_8_GrPixelConfig:
121 *isOpaque = false;
122 return SkBitmap::kA8_Config;
123 case kRGB_565_GrPixelConfig:
124 *isOpaque = true;
125 return SkBitmap::kRGB_565_Config;
126 case kRGBA_4444_GrPixelConfig:
127 *isOpaque = false;
128 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000129 case kSkia8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000130 // we don't currently have a way of knowing whether
131 // a 8888 is opaque based on the config.
132 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000133 return SkBitmap::kARGB_8888_Config;
134 default:
135 *isOpaque = false;
136 return SkBitmap::kNo_Config;
137 }
138}
reed@google.comac10a2d2010-12-22 21:39:39 +0000139
reed@google.comaf951c92011-06-16 19:10:39 +0000140static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000141 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000142
143 bool isOpaque;
144 SkBitmap bitmap;
145 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
146 renderTarget->width(), renderTarget->height());
147 bitmap.setIsOpaque(isOpaque);
148 return bitmap;
149}
150
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000151SkGpuDevice* SkGpuDevice::Create(GrSurface* surface) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000152 SkASSERT(NULL != surface);
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000153 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
154 return NULL;
155 }
156 if (surface->asTexture()) {
157 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture()));
158 } else {
159 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget()));
160 }
161}
162
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000163SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000164 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000165 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000166}
167
reed@google.comaf951c92011-06-16 19:10:39 +0000168SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000169 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000170 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000171}
172
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000173void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000174 GrRenderTarget* renderTarget,
175 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000176 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000177
reed@google.comaf951c92011-06-16 19:10:39 +0000178 fContext = context;
179 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000180
reed@google.comaf951c92011-06-16 19:10:39 +0000181 fRenderTarget = NULL;
182 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000183
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000184 SkASSERT(NULL != renderTarget);
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000185 fRenderTarget = renderTarget;
186 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000187
bsalomon@google.coma2921122012-08-28 12:34:17 +0000188 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
189 // on the RT but not vice-versa.
190 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
191 // busting chrome (for a currently unknown reason).
192 GrSurface* surface = fRenderTarget->asTexture();
193 if (NULL == surface) {
194 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000195 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000196 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000197
reed@google.comaf951c92011-06-16 19:10:39 +0000198 this->setPixelRef(pr, 0)->unref();
199}
200
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000201SkGpuDevice::SkGpuDevice(GrContext* context,
202 SkBitmap::Config config,
203 int width,
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000204 int height,
205 int sampleCount)
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000206 : SkBitmapDevice(config, width, height, false /*isOpaque*/) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000207
reed@google.comac10a2d2010-12-22 21:39:39 +0000208 fDrawProcs = NULL;
209
reed@google.com7b201d22011-01-11 18:59:23 +0000210 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000211 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000212
reed@google.comac10a2d2010-12-22 21:39:39 +0000213 fRenderTarget = NULL;
214 fNeedClear = false;
215
reed@google.comaf951c92011-06-16 19:10:39 +0000216 if (config != SkBitmap::kRGB_565_Config) {
217 config = SkBitmap::kARGB_8888_Config;
218 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000219
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000220 GrTextureDesc desc;
221 desc.fFlags = kRenderTarget_GrTextureFlagBit;
222 desc.fWidth = width;
223 desc.fHeight = height;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000224 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
225 desc.fSampleCnt = sampleCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000226
bsalomon@google.coma2921122012-08-28 12:34:17 +0000227 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000228
bsalomon@google.coma2921122012-08-28 12:34:17 +0000229 if (NULL != texture) {
230 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000231 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000232
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000233 SkASSERT(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000234
reed@google.comaf951c92011-06-16 19:10:39 +0000235 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000236 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000237 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000238 } else {
239 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
240 width, height);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000241 SkASSERT(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000242 }
243}
244
245SkGpuDevice::~SkGpuDevice() {
246 if (fDrawProcs) {
247 delete fDrawProcs;
248 }
249
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000250 // The GrContext takes a ref on the target. We don't want to cause the render
251 // target to be unnecessarily kept alive.
252 if (fContext->getRenderTarget() == fRenderTarget) {
253 fContext->setRenderTarget(NULL);
254 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000255
robertphillips@google.com055f9082012-10-24 13:24:11 +0000256 if (fContext->getClip() == &fClipData) {
257 fContext->setClip(NULL);
258 }
259
bsalomon@google.coma2921122012-08-28 12:34:17 +0000260 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000261 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000262}
263
reed@google.comac10a2d2010-12-22 21:39:39 +0000264///////////////////////////////////////////////////////////////////////////////
265
266void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000267 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000268 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000269}
270
271///////////////////////////////////////////////////////////////////////////////
272
bsalomon@google.comc4364992011-11-07 15:54:49 +0000273namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000274GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000275 switch (config8888) {
276 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000277 *flags = 0;
278 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000279 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000280 *flags = GrContext::kUnpremul_PixelOpsFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000281 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000282 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000283 *flags = 0;
284 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000285 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000286 *flags = GrContext::kUnpremul_PixelOpsFlag;
287 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000288 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000289 *flags = 0;
290 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000292 *flags = GrContext::kUnpremul_PixelOpsFlag;
293 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 default:
295 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000296 *flags = 0; // suppress warning
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000297 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000298 }
299}
300}
301
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000302bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
303 int x, int y,
304 SkCanvas::Config8888 config8888) {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000305 DO_DEFERRED_CLEAR();
bsalomon@google.com910267d2011-11-02 20:06:25 +0000306 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
307 SkASSERT(!bitmap.isNull());
308 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000309
bsalomon@google.com910267d2011-11-02 20:06:25 +0000310 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000311 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000312 uint32_t flags;
313 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000314 return fContext->readRenderTargetPixels(fRenderTarget,
315 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000316 bitmap.width(),
317 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000318 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000319 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000320 bitmap.rowBytes(),
321 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000322}
323
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000324void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
325 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000326 SkAutoLockPixels alp(bitmap);
327 if (!bitmap.readyToDraw()) {
328 return;
329 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000330
331 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000332 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000333 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000334 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000335 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000336 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000337 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000338 }
339
bsalomon@google.com6f379512011-11-16 20:36:03 +0000340 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000341 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000342}
343
robertphillips@google.com46f93502012-08-07 15:38:08 +0000344namespace {
humper@google.com05af1af2013-01-07 16:47:43 +0000345void purgeClipCB(int genID, void* ) {
robertphillips@google.com46f93502012-08-07 15:38:08 +0000346
347 if (SkClipStack::kInvalidGenID == genID ||
348 SkClipStack::kEmptyGenID == genID ||
349 SkClipStack::kWideOpenGenID == genID) {
350 // none of these cases will have a cached clip mask
351 return;
352 }
353
354}
355};
356
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000357void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
358 INHERITED::onAttachToCanvas(canvas);
359
360 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000361 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000362
363 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000364}
365
366void SkGpuDevice::onDetachFromCanvas() {
367 INHERITED::onDetachFromCanvas();
368
robertphillips@google.com46f93502012-08-07 15:38:08 +0000369 // TODO: iterate through the clip stack and clean up any cached clip masks
370 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
371
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000372 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000373}
374
robertphillips@google.com607fe072012-07-24 13:54:00 +0000375#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000376static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000377 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000378 int renderTargetWidth,
379 int renderTargetHeight) {
380
robertphillips@google.com7b112892012-07-31 15:18:21 +0000381 SkIRect devBound;
382
383 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
384
robertphillips@google.com607fe072012-07-24 13:54:00 +0000385 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000386 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000387
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000388 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000389 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000390 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000391
robertphillips@google.com7b112892012-07-31 15:18:21 +0000392 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000393
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000394 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000395
robertphillips@google.com7b112892012-07-31 15:18:21 +0000396 if (!devBound.intersect(devTemp)) {
397 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000398 }
399 }
400
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000401 SkASSERT(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000402}
403#endif
404
reed@google.comac10a2d2010-12-22 21:39:39 +0000405///////////////////////////////////////////////////////////////////////////////
406
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000407// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000408// and not the state from some other canvas/device
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000409void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000410 SkASSERT(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000411
reed@google.comac10a2d2010-12-22 21:39:39 +0000412 fContext->setRenderTarget(fRenderTarget);
413
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000414 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000415
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000416 if (forceIdentity) {
417 fContext->setIdentityMatrix();
418 } else {
419 fContext->setMatrix(*draw.fMatrix);
420 }
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000421 fClipData.fOrigin = this->getOrigin();
reed@google.comac10a2d2010-12-22 21:39:39 +0000422
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000423#ifdef SK_DEBUG
424 check_bounds(fClipData, *draw.fClip, fRenderTarget->width(), fRenderTarget->height());
425#endif
426
427 fContext->setClip(&fClipData);
428
429 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000430}
431
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000432GrRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000433 DO_DEFERRED_CLEAR();
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000434 return fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000435}
436
reed@google.comac10a2d2010-12-22 21:39:39 +0000437///////////////////////////////////////////////////////////////////////////////
438
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000439SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
440SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
441SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
442SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
443SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
444 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000445SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
446 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000447SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
448SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000449
bsalomon@google.com84405e02012-03-05 19:57:21 +0000450namespace {
451
452// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
453// justAlpha indicates that skPaint's alpha should be used rather than the color
454// Callers may subsequently modify the GrPaint. Setting constantColor indicates
455// that the final paint will draw the same color at every pixel. This allows
456// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000457// color once while converting to GrPaint and then ignored.
458inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
459 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000460 bool justAlpha,
461 bool constantColor,
462 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000464 grPaint->setDither(skPaint.isDither());
465 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000466
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000467 SkXfermode::Coeff sm;
468 SkXfermode::Coeff dm;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000469
470 SkXfermode* mode = skPaint.getXfermode();
bsalomon@google.comdb446252013-03-27 18:46:16 +0000471 GrEffectRef* xferEffect = NULL;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000472 if (SkXfermode::AsNewEffectOrCoeff(mode, dev->context(), &xferEffect, &sm, &dm)) {
473 if (NULL != xferEffect) {
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000474 grPaint->addColorEffect(xferEffect)->unref();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000475 sm = SkXfermode::kOne_Coeff;
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000476 dm = SkXfermode::kZero_Coeff;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000477 }
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000478 } else {
479 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000480#if 0
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000481 return false;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000482#else
483 // Fall back to src-over
484 sm = SkXfermode::kOne_Coeff;
485 dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000486#endif
bsalomon@google.com5782d712011-01-21 21:03:59 +0000487 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000488 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000489
bsalomon@google.com5782d712011-01-21 21:03:59 +0000490 if (justAlpha) {
491 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000492 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000493 // justAlpha is currently set to true only if there is a texture,
494 // so constantColor should not also be true.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000495 SkASSERT(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000496 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000497 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000498 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000499
Scroggo97c88c22011-05-11 14:05:25 +0000500 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000501 if (NULL != colorFilter) {
502 // if the source color is a constant then apply the filter here once rather than per pixel
503 // in a shader.
504 if (constantColor) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000505 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000506 grPaint->setColor(SkColor2GrColor(filtered));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000507 } else {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000508 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
bsalomon@google.com021fc732012-10-25 12:47:42 +0000509 if (NULL != effect.get()) {
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000510 grPaint->addColorEffect(effect);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000511 } else {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000512 // TODO: rewrite this using asNewEffect()
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000513 SkColor color;
514 SkXfermode::Mode filterMode;
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000515 if (colorFilter->asColorMode(&color, &filterMode)) {
516 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000517 }
518 }
Scroggod757df22011-05-16 13:11:16 +0000519 }
Scroggo97c88c22011-05-11 14:05:25 +0000520 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000521
bsalomon@google.com5782d712011-01-21 21:03:59 +0000522 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000523}
524
bsalomon@google.com84405e02012-03-05 19:57:21 +0000525// This function is similar to skPaint2GrPaintNoShader but also converts
bsalomon@google.com08283af2012-10-26 13:01:20 +0000526// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
bsalomon@google.com84405e02012-03-05 19:57:21 +0000527// be used is set on grPaint and returned in param act. constantColor has the
528// same meaning as in skPaint2GrPaintNoShader.
529inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
530 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000531 bool constantColor,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000532 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000533 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000534 if (NULL == shader) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000535 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000536 }
537
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000538 // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state
539 // Also require shader to set the render target .
540 GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL);
541 GrContext::AutoRenderTarget(dev->context(), NULL);
542
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000543 // setup the shader as the first color effect on the paint
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000544 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000545 if (NULL != effect.get()) {
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000546 grPaint->addColorEffect(effect);
547 // Now setup the rest of the paint.
548 return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
549 } else {
550 // We still don't have SkColorShader::asNewEffect() implemented.
551 SkShader::GradientInfo info;
552 SkColor color;
rileya@google.com91f319c2012-07-25 17:18:31 +0000553
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000554 info.fColors = &color;
555 info.fColorOffsets = NULL;
556 info.fColorCount = 1;
557 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
558 SkPaint copy(skPaint);
559 copy.setShader(NULL);
560 // modulate the paint alpha by the shader's solid color alpha
561 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
562 copy.setColor(SkColorSetA(color, newA));
563 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
564 } else {
565 return false;
566 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000567 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000568}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000569}
reed@google.comac10a2d2010-12-22 21:39:39 +0000570
571///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com07f81a52013-09-17 12:26:23 +0000572void SkGpuDevice::getGlobalBounds(SkIRect* bounds) const {
573 if (NULL != bounds) {
574 const SkIPoint& origin = this->getOrigin();
575 bounds->setXYWH(origin.x(), origin.y(),
576 this->width(), this->height());
577 }
578}
579
580SkBitmap::Config SkGpuDevice::config() const {
581 if (NULL == fRenderTarget) {
582 return SkBitmap::kNo_Config;
583 }
584
585 bool isOpaque;
586 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
587}
588
bsalomon@google.com398109c2011-04-14 18:40:27 +0000589void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com32cf29e2013-01-25 15:03:18 +0000590 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
591 fContext->clear(&rect, SkColor2GrColor(color), fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000592 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000593}
594
reed@google.comac10a2d2010-12-22 21:39:39 +0000595void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000596 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000597
bsalomon@google.com5782d712011-01-21 21:03:59 +0000598 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000599 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000600 return;
601 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000602
603 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000604}
605
606// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000607static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000608 kPoints_GrPrimitiveType,
609 kLines_GrPrimitiveType,
610 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000611};
612
613void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000614 size_t count, const SkPoint pts[], const SkPaint& paint) {
epoger@google.comb58772f2013-03-08 09:09:10 +0000615 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000616 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000617
618 SkScalar width = paint.getStrokeWidth();
619 if (width < 0) {
620 return;
621 }
622
djsollen@google.com24202c72013-08-01 18:36:08 +0000623 // we only handle hairlines and paints without path effects or mask filters,
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000624 // else we let the SkDraw call our drawPath()
djsollen@google.com24202c72013-08-01 18:36:08 +0000625 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000626 draw.drawPoints(mode, count, pts, paint, true);
627 return;
628 }
629
bsalomon@google.com5782d712011-01-21 21:03:59 +0000630 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000631 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000632 return;
633 }
634
bsalomon@google.com5782d712011-01-21 21:03:59 +0000635 fContext->drawVertices(grPaint,
636 gPointMode2PrimtiveType[mode],
637 count,
638 (GrPoint*)pts,
639 NULL,
640 NULL,
641 NULL,
642 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000643}
644
reed@google.comc9aa5872011-04-05 21:05:37 +0000645///////////////////////////////////////////////////////////////////////////////
646
reed@google.comac10a2d2010-12-22 21:39:39 +0000647void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000648 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000649 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000650 CHECK_SHOULD_DRAW(draw, false);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000651
bungeman@google.com79bd8772011-07-18 15:34:08 +0000652 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000653 SkScalar width = paint.getStrokeWidth();
654
655 /*
656 We have special code for hairline strokes, miter-strokes, and fills.
657 Anything else we just call our path code.
658 */
659 bool usePath = doStroke && width > 0 &&
660 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000661 // another two reasons we might need to call drawPath...
662 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000663 usePath = true;
664 }
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000665 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000666#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000667 if (doStroke) {
668#endif
669 usePath = true;
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000670#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000671 } else {
672 usePath = !fContext->getMatrix().preservesRightAngles();
673 }
674#endif
reed@google.com67db6642011-05-26 11:46:35 +0000675 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000676 // small miter limit means right angles show bevel...
677 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
678 paint.getStrokeMiter() < SK_ScalarSqrt2)
679 {
680 usePath = true;
681 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000682 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000683 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
684 usePath = true;
685 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000686
687 if (usePath) {
688 SkPath path;
689 path.addRect(rect);
690 this->drawPath(draw, path, paint, NULL, true);
691 return;
692 }
693
694 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000695 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000696 return;
697 }
reed@google.com20efde72011-05-09 17:00:02 +0000698 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000699}
700
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000701///////////////////////////////////////////////////////////////////////////////
702
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000703void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
704 const SkPaint& paint) {
705 CHECK_FOR_NODRAW_ANNOTATION(paint);
706 CHECK_SHOULD_DRAW(draw, false);
707
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000708 bool usePath = !rect.isSimple();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000709 // another two reasons we might need to call drawPath...
710 if (paint.getMaskFilter() || paint.getPathEffect()) {
711 usePath = true;
712 }
713 // until we can rotate rrects...
714 if (!usePath && !fContext->getMatrix().rectStaysRect()) {
715 usePath = true;
716 }
717
718 if (usePath) {
719 SkPath path;
720 path.addRRect(rect);
721 this->drawPath(draw, path, paint, NULL, true);
722 return;
723 }
724
725 GrPaint grPaint;
726 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
727 return;
728 }
729
730 SkStrokeRec stroke(paint);
731 fContext->drawRRect(grPaint, rect, stroke);
732}
733
734///////////////////////////////////////////////////////////////////////////////
735
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000736void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
737 const SkPaint& paint) {
738 CHECK_FOR_NODRAW_ANNOTATION(paint);
739 CHECK_SHOULD_DRAW(draw, false);
740
741 bool usePath = false;
742 // some basic reasons we might need to call drawPath...
743 if (paint.getMaskFilter() || paint.getPathEffect()) {
744 usePath = true;
745 }
746
747 if (usePath) {
748 SkPath path;
749 path.addOval(oval);
750 this->drawPath(draw, path, paint, NULL, true);
751 return;
752 }
753
754 GrPaint grPaint;
755 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
756 return;
757 }
758 SkStrokeRec stroke(paint);
759
760 fContext->drawOval(grPaint, oval, stroke);
761}
762
reed@google.com69302852011-02-16 18:08:07 +0000763#include "SkMaskFilter.h"
764#include "SkBounder.h"
765
bsalomon@google.com85003222012-03-28 14:44:37 +0000766///////////////////////////////////////////////////////////////////////////////
767
768// helpers for applying mask filters
769namespace {
770
robertphillips@google.com49149312013-07-03 15:34:35 +0000771// Draw a mask using the supplied paint. Since the coverage/geometry
772// is already burnt into the mask this boils down to a rect draw.
773// Return true if the mask was successfully drawn.
774bool draw_mask(GrContext* context, const SkRect& maskRect,
775 GrPaint* grp, GrTexture* mask) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000776 GrContext::AutoMatrix am;
777 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000778 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000779 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000780
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000781 SkMatrix matrix;
robertphillips@google.com49149312013-07-03 15:34:35 +0000782 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
783 matrix.postIDiv(mask->width(), mask->height());
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000784
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000785 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
robertphillips@google.com49149312013-07-03 15:34:35 +0000786 context->drawRect(*grp, maskRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000787 return true;
788}
789
robertphillips@google.com49149312013-07-03 15:34:35 +0000790bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
791 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
792 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000793 SkMask srcM, dstM;
794
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000795 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
796 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000797 return false;
798 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000799 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000800
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000801 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000802 return false;
803 }
804 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000805 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000806
807 if (clip.quickReject(dstM.fBounds)) {
808 return false;
809 }
810 if (bounder && !bounder->doIRect(dstM.fBounds)) {
811 return false;
812 }
813
814 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000815 // the current clip (and identity matrix) and GrPaint settings
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000816 GrTextureDesc desc;
817 desc.fWidth = dstM.fBounds.width();
818 desc.fHeight = dstM.fBounds.height();
819 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000820
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000821 GrAutoScratchTexture ast(context, desc);
822 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000823
reed@google.com69302852011-02-16 18:08:07 +0000824 if (NULL == texture) {
825 return false;
826 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000827 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000828 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000829
robertphillips@google.com49149312013-07-03 15:34:35 +0000830 SkRect maskRect = SkRect::MakeFromIRect(dstM.fBounds);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000831
robertphillips@google.com49149312013-07-03 15:34:35 +0000832 return draw_mask(context, maskRect, grp, texture);
833}
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000834
robertphillips@google.com49149312013-07-03 15:34:35 +0000835// Create a mask of 'devPath' and place the result in 'mask'. Return true on
836// success; false otherwise.
837bool create_mask_GPU(GrContext* context,
838 const SkRect& maskRect,
839 const SkPath& devPath,
840 const SkStrokeRec& stroke,
841 bool doAA,
842 GrAutoScratchTexture* mask) {
843 GrTextureDesc desc;
844 desc.fFlags = kRenderTarget_GrTextureFlagBit;
845 desc.fWidth = SkScalarCeilToInt(maskRect.width());
846 desc.fHeight = SkScalarCeilToInt(maskRect.height());
847 // We actually only need A8, but it often isn't supported as a
848 // render target so default to RGBA_8888
849 desc.fConfig = kRGBA_8888_GrPixelConfig;
850 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
851 desc.fConfig = kAlpha_8_GrPixelConfig;
852 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000853
robertphillips@google.com49149312013-07-03 15:34:35 +0000854 mask->set(context, desc);
855 if (NULL == mask->texture()) {
856 return false;
857 }
858
859 GrTexture* maskTexture = mask->texture();
860 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
861
862 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
863 GrContext::AutoClip ac(context, clipRect);
864
865 context->clear(NULL, 0x0);
866
867 GrPaint tempPaint;
868 if (doAA) {
869 tempPaint.setAntiAlias(true);
870 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
871 // blend coeff of zero requires dual source blending support in order
872 // to properly blend partially covered pixels. This means the AA
873 // code path may not be taken. So we use a dst blend coeff of ISA. We
874 // could special case AA draws to a dst surface with known alpha=0 to
875 // use a zero dst coeff when dual source blending isn't available.
876 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
877 }
878
879 GrContext::AutoMatrix am;
880
881 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
882 SkMatrix translate;
883 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
884 am.set(context, translate);
885 context->drawPath(tempPaint, devPath, stroke);
reed@google.com69302852011-02-16 18:08:07 +0000886 return true;
887}
reed@google.com69302852011-02-16 18:08:07 +0000888
robertphillips@google.com49149312013-07-03 15:34:35 +0000889SkBitmap wrap_texture(GrTexture* texture) {
890 SkBitmap result;
891 bool dummy;
892 SkBitmap::Config config = grConfig2skConfig(texture->config(), &dummy);
893 result.setConfig(config, texture->width(), texture->height());
894 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (texture)))->unref();
895 return result;
bsalomon@google.com85003222012-03-28 14:44:37 +0000896}
897
robertphillips@google.com49149312013-07-03 15:34:35 +0000898};
bsalomon@google.com85003222012-03-28 14:44:37 +0000899
reed@google.com0c219b62011-02-16 21:31:18 +0000900void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000901 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000902 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000903 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000904 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000905
bsalomon@google.com5782d712011-01-21 21:03:59 +0000906 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000907 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000908 return;
909 }
910
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000911 // can we cheat, and treat a thin stroke as a hairline w/ coverage
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000912 // if we can, we draw lots faster (raster device does this same test)
913 SkScalar hairlineCoverage;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000914 bool doHairLine = SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage);
915 if (doHairLine) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000916 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000917 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000918
reed@google.comfe626382011-09-21 13:50:35 +0000919 // If we have a prematrix, apply it to the path, optimizing for the case
920 // where the original path can in fact be modified in place (even though
921 // its parameter type is const).
922 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000923 SkPath tmpPath, effectPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000924
925 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +0000926 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +0000927
reed@google.come3445642011-02-16 23:20:39 +0000928 if (!pathIsMutable) {
929 result = &tmpPath;
930 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000931 }
reed@google.come3445642011-02-16 23:20:39 +0000932 // should I push prePathMatrix on our MV stack temporarily, instead
933 // of applying it here? See SkDraw.cpp
934 pathPtr->transform(*prePathMatrix, result);
935 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +0000936 }
reed@google.com0c219b62011-02-16 21:31:18 +0000937 // at this point we're done with prePathMatrix
938 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +0000939
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000940 SkStrokeRec stroke(paint);
941 SkPathEffect* pathEffect = paint.getPathEffect();
reed@google.com4bbdeac2013-01-24 21:03:11 +0000942 const SkRect* cullRect = NULL; // TODO: what is our bounds?
943 if (pathEffect && pathEffect->filterPath(&effectPath, *pathPtr, &stroke,
944 cullRect)) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000945 pathPtr = &effectPath;
946 }
947
948 if (!pathEffect && doHairLine) {
949 stroke.setHairlineStyle();
reed@google.com0c219b62011-02-16 21:31:18 +0000950 }
951
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000952 if (paint.getMaskFilter()) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000953 if (!stroke.isHairlineStyle()) {
954 if (stroke.applyToPath(&tmpPath, *pathPtr)) {
955 pathPtr = &tmpPath;
robertphillips@google.com49149312013-07-03 15:34:35 +0000956 pathIsMutable = true;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000957 stroke.setFillStyle();
958 }
959 }
960
reed@google.com0c219b62011-02-16 21:31:18 +0000961 // avoid possibly allocating a new path in transform if we can
962 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
963
964 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000965 pathPtr->transform(fContext->getMatrix(), devPathPtr);
robertphillips@google.com49149312013-07-03 15:34:35 +0000966
967 SkRect maskRect;
968 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
969 draw.fClip->getBounds(),
970 fContext->getMatrix(),
971 &maskRect)) {
972 SkIRect finalIRect;
973 maskRect.roundOut(&finalIRect);
974 if (draw.fClip->quickReject(finalIRect)) {
975 // clipped out
976 return;
977 }
978 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
979 // nothing to draw
980 return;
981 }
982
983 GrAutoScratchTexture mask;
984
skia.committer@gmail.com1842adf2013-07-04 07:01:07 +0000985 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
robertphillips@google.com49149312013-07-03 15:34:35 +0000986 grPaint.isAntiAlias(), &mask)) {
987 GrTexture* filtered;
988
989 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(), maskRect, &filtered, true)) {
990 SkAutoTUnref<GrTexture> atu(filtered);
991
992 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
993 // This path is completely drawn
994 return;
995 }
996 }
997 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000998 }
robertphillips@google.com49149312013-07-03 15:34:35 +0000999
1000 // draw the mask on the CPU - this is a fallthrough path in case the
1001 // GPU path fails
1002 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
1003 SkPaint::kFill_Style;
1004 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
1005 *draw.fClip, draw.fBounder, &grPaint, style);
reed@google.com69302852011-02-16 18:08:07 +00001006 return;
1007 }
reed@google.com69302852011-02-16 18:08:07 +00001008
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001009 fContext->drawPath(grPaint, *pathPtr, stroke);
reed@google.comac10a2d2010-12-22 21:39:39 +00001010}
1011
bsalomon@google.comfb309512011-11-30 14:13:48 +00001012namespace {
1013
1014inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1015 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1016 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1017 return tilesX * tilesY;
1018}
1019
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001020inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001021 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001022 int maxTextureSize) {
1023 static const int kSmallTileSize = 1 << 10;
1024 if (maxTextureSize <= kSmallTileSize) {
1025 return maxTextureSize;
1026 }
1027
1028 size_t maxTexTotalTileSize;
1029 size_t smallTotalTileSize;
1030
robertphillips@google.combac6b052012-09-28 18:06:49 +00001031 SkIRect iSrc;
1032 src.roundOut(&iSrc);
1033
1034 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1035 iSrc.fTop,
1036 iSrc.fRight,
1037 iSrc.fBottom,
1038 maxTextureSize);
1039 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1040 iSrc.fTop,
1041 iSrc.fRight,
1042 iSrc.fBottom,
1043 kSmallTileSize);
1044
bsalomon@google.comfb309512011-11-30 14:13:48 +00001045 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1046 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1047
1048 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1049 return kSmallTileSize;
1050 } else {
1051 return maxTextureSize;
1052 }
1053}
1054}
1055
1056bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001057 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001058 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001059 // if bitmap is explictly texture backed then just use the texture
1060 if (NULL != bitmap.getTexture()) {
1061 return false;
1062 }
1063 // if it's larger than the max texture size, then we have no choice but
1064 // tiling
1065 const int maxTextureSize = fContext->getMaxTextureSize();
1066 if (bitmap.width() > maxTextureSize ||
1067 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001068 return true;
1069 }
1070 // if we are going to have to draw the whole thing, then don't tile
1071 if (NULL == srcRectPtr) {
1072 return false;
1073 }
1074 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001075 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001076 return false;
1077 }
1078
1079 // At this point we know we could do the draw by uploading the entire bitmap
1080 // as a texture. However, if the texture would be large compared to the
1081 // cache size and we don't require most of it for this draw then tile to
1082 // reduce the amount of upload and cache spill.
1083
1084 // assumption here is that sw bitmap size is a good proxy for its size as
1085 // a texture
1086 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001087 size_t cacheSize;
1088 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001089 if (bmpSize < cacheSize / 2) {
1090 return false;
1091 }
1092
robertphillips@google.combac6b052012-09-28 18:06:49 +00001093 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1094 srcRectPtr->height() / bitmap.height());
1095 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001096 return true;
1097 } else {
1098 return false;
1099 }
1100}
1101
reed@google.comac10a2d2010-12-22 21:39:39 +00001102void SkGpuDevice::drawBitmap(const SkDraw& draw,
1103 const SkBitmap& bitmap,
reed@google.comac10a2d2010-12-22 21:39:39 +00001104 const SkMatrix& m,
1105 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001106 // We cannot call drawBitmapRect here since 'm' could be anything
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001107 this->drawBitmapCommon(draw, bitmap, NULL, m, paint,
robertphillips@google.com31acc112013-08-20 12:13:48 +00001108 SkCanvas::kNone_DrawBitmapRectFlag);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001109}
1110
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001111// This method outsets 'iRect' by 1 all around and then clamps its extents to
1112// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1113// of 'iRect' for all possible outsets/clamps.
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +00001114static inline void clamped_unit_outset_with_offset(SkIRect* iRect, SkPoint* offset,
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001115 const SkIRect& clamp) {
1116 iRect->outset(1, 1);
1117
1118 if (iRect->fLeft < clamp.fLeft) {
1119 iRect->fLeft = clamp.fLeft;
1120 } else {
1121 offset->fX -= SK_Scalar1;
1122 }
1123 if (iRect->fTop < clamp.fTop) {
1124 iRect->fTop = clamp.fTop;
1125 } else {
1126 offset->fY -= SK_Scalar1;
1127 }
1128
1129 if (iRect->fRight > clamp.fRight) {
1130 iRect->fRight = clamp.fRight;
1131 }
1132 if (iRect->fBottom > clamp.fBottom) {
1133 iRect->fBottom = clamp.fBottom;
1134 }
1135}
1136
robertphillips@google.combac6b052012-09-28 18:06:49 +00001137void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1138 const SkBitmap& bitmap,
1139 const SkRect* srcRectPtr,
1140 const SkMatrix& m,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001141 const SkPaint& paint,
1142 SkCanvas::DrawBitmapRectFlags flags) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001143 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001144
robertphillips@google.combac6b052012-09-28 18:06:49 +00001145 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001146 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001147 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001148 } else {
1149 srcRect = *srcRectPtr;
1150 }
1151
junov@google.comd935cfb2011-06-27 20:48:23 +00001152 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001153 // Convert the bitmap to a shader so that the rect can be drawn
1154 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001155 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001156 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001157 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001158 if (NULL != srcRectPtr) {
1159 SkIRect iSrc;
1160 srcRect.roundOut(&iSrc);
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001161
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +00001162 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001163 SkIntToScalar(iSrc.fTop));
1164
1165 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1166 // In bleed mode we want to expand the src rect on all sides
1167 // but stay within the bitmap bounds
1168 SkIRect iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1169 clamped_unit_outset_with_offset(&iSrc, &offset, iClampRect);
1170 }
1171
robertphillips@google.combac6b052012-09-28 18:06:49 +00001172 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001173 return; // extraction failed
1174 }
1175 bitmapPtr = &tmp;
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001176 srcRect.offset(-offset.fX, -offset.fY);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001177 // The source rect has changed so update the matrix
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001178 newM.preTranslate(offset.fX, offset.fY);
junov@google.comd935cfb2011-06-27 20:48:23 +00001179 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001180
junov@google.comd935cfb2011-06-27 20:48:23 +00001181 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001182 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001183 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001184
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001185 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001186 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001187 // also affect the behavior of the mask filter.
1188 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001189 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001190 SkDraw transformedDraw(draw);
1191 transformedDraw.fMatrix = &drawMatrix;
1192
robertphillips@google.combac6b052012-09-28 18:06:49 +00001193 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001194
junov@google.comd935cfb2011-06-27 20:48:23 +00001195 return;
1196 }
1197
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001198 GrTextureParams params;
humper@google.comb86add12013-07-25 18:49:07 +00001199 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1200 GrTextureParams::FilterMode textureFilterMode;
1201 switch(paintFilterLevel) {
1202 case SkPaint::kNone_FilterLevel:
1203 textureFilterMode = GrTextureParams::kNone_FilterMode;
1204 break;
1205 case SkPaint::kLow_FilterLevel:
1206 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1207 break;
1208 case SkPaint::kMedium_FilterLevel:
1209 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1210 break;
1211 case SkPaint::kHigh_FilterLevel:
1212 SkErrorInternals::SetError( kInvalidPaint_SkError,
1213 "Sorry, I don't yet support high quality "
1214 "filtering on the GPU. Falling back to "
1215 "MIPMaps.");
1216 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1217 break;
humper@google.comf9ed6fe2013-07-25 19:09:47 +00001218 default:
1219 SkErrorInternals::SetError( kInvalidPaint_SkError,
1220 "Sorry, I don't understand the filtering "
1221 "mode you asked for. Falling back to "
1222 "MIPMaps.");
1223 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1224 break;
skia.committer@gmail.com956b3102013-07-26 07:00:58 +00001225
humper@google.comb86add12013-07-25 18:49:07 +00001226 }
skia.committer@gmail.com956b3102013-07-26 07:00:58 +00001227
humper@google.comb86add12013-07-25 18:49:07 +00001228 params.setFilterMode(textureFilterMode);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001229
robertphillips@google.combac6b052012-09-28 18:06:49 +00001230 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001231 // take the simple case
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001232 this->internalDrawBitmap(bitmap, srcRect, m, params, paint, flags);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001233 } else {
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001234 this->drawTiledBitmap(bitmap, srcRect, m, params, paint, flags);
reed@google.comac10a2d2010-12-22 21:39:39 +00001235 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001236}
1237
1238// Break 'bitmap' into several tiles to draw it since it has already
1239// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001240void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001241 const SkRect& srcRect,
1242 const SkMatrix& m,
1243 const GrTextureParams& params,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001244 const SkPaint& paint,
1245 SkCanvas::DrawBitmapRectFlags flags) {
robertphillips@google.comd7ca6612013-08-20 12:09:32 +00001246 int maxTextureSize = fContext->getMaxTextureSize();
1247 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel()) {
1248 // We may need a skosh more room if we have to bump out the tile
1249 // by 1 pixel all around
1250 maxTextureSize -= 2;
1251 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001252
1253 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001254
reed@google.comac10a2d2010-12-22 21:39:39 +00001255 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001256 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001257 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001258 const GrRenderTarget* rt = fContext->getRenderTarget();
1259 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1260 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1261 return;
1262 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001263 SkMatrix matrix, inverse;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001264 matrix.setConcat(fContext->getMatrix(), m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 if (!matrix.invert(&inverse)) {
1266 return;
1267 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001268 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001269 }
1270
bsalomon@google.comfb309512011-11-30 14:13:48 +00001271 int nx = bitmap.width() / tileSize;
1272 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001273 for (int x = 0; x <= nx; x++) {
1274 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001275 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001276 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001277 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001278 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001279 SkIntToScalar((y + 1) * tileSize));
1280
1281 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 continue;
1283 }
1284
robertphillips@google.combac6b052012-09-28 18:06:49 +00001285 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 continue;
1287 }
1288
1289 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001290 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001291 tileR.roundOut(&iTileR);
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +00001292 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comd7ca6612013-08-20 12:09:32 +00001293 SkIntToScalar(iTileR.fTop));
1294
1295 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel()) {
1296 SkIRect iClampRect;
1297
1298 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1299 // In bleed mode we want to always expand the tile on all edges
1300 // but stay within the bitmap bounds
1301 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1302 } else {
1303 // In texture-domain/clamp mode we only want to expand the
1304 // tile on edges interior to "srcRect" (i.e., we want to
1305 // not bleed across the original clamped edges)
1306 srcRect.roundOut(&iClampRect);
1307 }
1308
1309 clamped_unit_outset_with_offset(&iTileR, &offset, iClampRect);
1310 }
1311
robertphillips@google.combac6b052012-09-28 18:06:49 +00001312 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001313 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.comd7ca6612013-08-20 12:09:32 +00001314 tileR.offset(-offset.fX, -offset.fY);
reed@google.comac10a2d2010-12-22 21:39:39 +00001315 SkMatrix tmpM(m);
robertphillips@google.comd7ca6612013-08-20 12:09:32 +00001316 tmpM.preTranslate(offset.fX, offset.fY);
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001317
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001318 this->internalDrawBitmap(tmpB, tileR, tmpM, params, paint, flags);
reed@google.comac10a2d2010-12-22 21:39:39 +00001319 }
1320 }
1321 }
1322}
1323
skia.committer@gmail.com956b3102013-07-26 07:00:58 +00001324static bool has_aligned_samples(const SkRect& srcRect,
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001325 const SkRect& transformedRect) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001326 // detect pixel disalignment
1327 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1328 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001329 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001330 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1331 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1332 COLOR_BLEED_TOLERANCE &&
1333 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1334 COLOR_BLEED_TOLERANCE) {
1335 return true;
1336 }
1337 return false;
1338}
1339
skia.committer@gmail.com956b3102013-07-26 07:00:58 +00001340static bool may_color_bleed(const SkRect& srcRect,
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001341 const SkRect& transformedRect,
1342 const SkMatrix& m) {
1343 // Only gets called if has_aligned_samples returned false.
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001344 // So we can assume that sampling is axis aligned but not texel aligned.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001345 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001346 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001347 outerTransformedRect(transformedRect);
1348 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1349 m.mapRect(&innerTransformedRect, innerSrcRect);
1350
1351 // The gap between outerTransformedRect and innerTransformedRect
1352 // represents the projection of the source border area, which is
1353 // problematic for color bleeding. We must check whether any
1354 // destination pixels sample the border area.
1355 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1356 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1357 SkIRect outer, inner;
1358 outerTransformedRect.round(&outer);
1359 innerTransformedRect.round(&inner);
1360 // If the inner and outer rects round to the same result, it means the
1361 // border does not overlap any pixel centers. Yay!
1362 return inner != outer;
1363}
1364
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001365
reed@google.comac10a2d2010-12-22 21:39:39 +00001366/*
1367 * This is called by drawBitmap(), which has to handle images that may be too
1368 * large to be represented by a single texture.
1369 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001370 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1371 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001372 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001373void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001374 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001375 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001376 const GrTextureParams& params,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001377 const SkPaint& paint,
1378 SkCanvas::DrawBitmapRectFlags flags) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001379 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1380 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001381
reed@google.comac10a2d2010-12-22 21:39:39 +00001382 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001383 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001384 if (NULL == texture) {
1385 return;
1386 }
1387
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001388 SkRect dstRect(srcRect);
1389 SkRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001390 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1391 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1392 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1393 SkScalarMul(srcRect.fTop, hInv),
1394 SkScalarMul(srcRect.fRight, wInv),
1395 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001396
rmistry@google.comd6176b02012-08-23 18:14:13 +00001397 bool needsTextureDomain = false;
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001398 if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) &&
1399 params.filterMode() != GrTextureParams::kNone_FilterMode) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001400 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001401 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001402 srcRect.height() < bitmap.height();
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001403 if (needsTextureDomain && m.rectStaysRect() && fContext->getMatrix().rectStaysRect()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001404 // sampling is axis-aligned
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001405 SkRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001406 SkMatrix srcToDeviceMatrix(m);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001407 srcToDeviceMatrix.postConcat(fContext->getMatrix());
robertphillips@google.combac6b052012-09-28 18:06:49 +00001408 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001409
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001410 if (has_aligned_samples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001411 // We could also turn off filtering here (but we already did a cache lookup with
1412 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001413 needsTextureDomain = false;
1414 } else {
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001415 needsTextureDomain = may_color_bleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001416 }
1417 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001418 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001419
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001420 SkRect textureDomain = SkRect::MakeEmpty();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001421 SkAutoTUnref<GrEffectRef> effect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001422 if (needsTextureDomain) {
1423 // Use a constrained texture domain to avoid color bleeding
bsalomon@google.com81712882012-11-01 17:12:34 +00001424 SkScalar left, top, right, bottom;
1425 if (srcRect.width() > SK_Scalar1) {
1426 SkScalar border = SK_ScalarHalf / bitmap.width();
junov@google.com6acc9b32011-05-16 18:32:07 +00001427 left = paintRect.left() + border;
1428 right = paintRect.right() - border;
1429 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001430 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
junov@google.com6acc9b32011-05-16 18:32:07 +00001431 }
bsalomon@google.com81712882012-11-01 17:12:34 +00001432 if (srcRect.height() > SK_Scalar1) {
1433 SkScalar border = SK_ScalarHalf / bitmap.height();
junov@google.com6acc9b32011-05-16 18:32:07 +00001434 top = paintRect.top() + border;
1435 bottom = paintRect.bottom() - border;
1436 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001437 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
junov@google.com6acc9b32011-05-16 18:32:07 +00001438 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001439 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001440 effect.reset(GrTextureDomainEffect::Create(texture,
1441 SkMatrix::I(),
1442 textureDomain,
1443 GrTextureDomainEffect::kClamp_WrapMode,
humper@google.comb86add12013-07-25 18:49:07 +00001444 params.filterMode()));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001445 } else {
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001446 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
junov@google.com6acc9b32011-05-16 18:32:07 +00001447 }
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001448
1449 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1450 // the rest from the SkPaint.
1451 GrPaint grPaint;
1452 grPaint.addColorEffect(effect);
1453 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1454 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1455 return;
1456 }
1457
1458 fContext->drawRectToRect(grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001459}
1460
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001461static bool filter_texture(SkBaseDevice* device, GrContext* context,
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001462 GrTexture* texture, SkImageFilter* filter,
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001463 int w, int h, const SkMatrix& ctm, SkBitmap* result,
1464 SkIPoint* offset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001465 SkASSERT(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001466 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001467
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001468 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org985fa792012-10-24 15:14:26 +00001469 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1470 // filter. Also set the clip wide open and the matrix to identity.
1471 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001472 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctm, result, offset);
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001473 } else {
1474 return false;
reed@google.com8926b162012-03-23 15:36:36 +00001475 }
reed@google.com8926b162012-03-23 15:36:36 +00001476}
1477
reed@google.comac10a2d2010-12-22 21:39:39 +00001478void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001479 int left, int top, const SkPaint& paint) {
1480 // drawSprite is defined to be in device coords.
1481 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001482
reed@google.com8926b162012-03-23 15:36:36 +00001483 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001484 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1485 return;
1486 }
1487
reed@google.com76dd2772012-01-05 21:15:07 +00001488 int w = bitmap.width();
1489 int h = bitmap.height();
1490
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001491 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001492 // draw sprite uses the default texture params
1493 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001494
reed@google.com8926b162012-03-23 15:36:36 +00001495 SkImageFilter* filter = paint.getImageFilter();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +00001496 SkIPoint offset = SkIPoint::Make(left, top);
commit-bot@chromium.org97e49e92013-07-18 19:21:44 +00001497 // This bitmap will own the filtered result as a texture.
1498 SkBitmap filteredBitmap;
1499
reed@google.com8926b162012-03-23 15:36:36 +00001500 if (NULL != filter) {
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +00001501 SkMatrix matrix(*draw.fMatrix);
1502 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
1503 if (filter_texture(this, fContext, texture, filter, w, h, matrix, &filteredBitmap,
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001504 &offset)) {
commit-bot@chromium.org97e49e92013-07-18 19:21:44 +00001505 texture = (GrTexture*) filteredBitmap.getTexture();
1506 w = filteredBitmap.width();
1507 h = filteredBitmap.height();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +00001508 } else {
1509 return;
reed@google.com8926b162012-03-23 15:36:36 +00001510 }
reed@google.com76dd2772012-01-05 21:15:07 +00001511 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001512
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001513 GrPaint grPaint;
1514 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1515
1516 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1517 return;
1518 }
1519
bsalomon@google.com5782d712011-01-21 21:03:59 +00001520 fContext->drawRectToRect(grPaint,
robertphillips@google.com58c856a2013-07-24 13:18:06 +00001521 SkRect::MakeXYWH(SkIntToScalar(offset.fX),
1522 SkIntToScalar(offset.fY),
senorblanco@chromium.org194d7752013-07-24 22:19:24 +00001523 SkIntToScalar(w),
1524 SkIntToScalar(h)),
1525 SkRect::MakeXYWH(0,
1526 0,
robertphillips@google.com31a40ef2013-07-11 00:01:39 +00001527 SK_Scalar1 * w / texture->width(),
1528 SK_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001529}
1530
reed@google.com33535f32012-09-25 15:37:50 +00001531void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1532 const SkRect* src, const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001533 const SkPaint& paint,
1534 SkCanvas::DrawBitmapRectFlags flags) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001535 SkMatrix matrix;
1536 SkRect bitmapBounds, tmpSrc;
1537
1538 bitmapBounds.set(0, 0,
1539 SkIntToScalar(bitmap.width()),
1540 SkIntToScalar(bitmap.height()));
1541
reed@google.com33535f32012-09-25 15:37:50 +00001542 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001543 if (NULL != src) {
1544 tmpSrc = *src;
1545 } else {
1546 tmpSrc = bitmapBounds;
1547 }
1548 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1549
1550 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1551 if (NULL != src) {
1552 if (!bitmapBounds.contains(tmpSrc)) {
1553 if (!tmpSrc.intersect(bitmapBounds)) {
1554 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001555 }
reed@google.com33535f32012-09-25 15:37:50 +00001556 }
reed@google.com33535f32012-09-25 15:37:50 +00001557 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001558
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001559 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint, flags);
reed@google.com33535f32012-09-25 15:37:50 +00001560}
1561
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001562void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001563 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001564 // clear of the source device must occur before CHECK_SHOULD_DRAW
1565 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1566 if (dev->fNeedClear) {
1567 // TODO: could check here whether we really need to draw at all
1568 dev->clear(0x0);
1569 }
1570
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001571 // drawDevice is defined to be in device coords.
1572 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001573
robertphillips@google.com07f81a52013-09-17 12:26:23 +00001574 GrRenderTarget* devRT = dev->accessRenderTarget();
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001575 GrTexture* devTex;
1576 if (NULL == (devTex = devRT->asTexture())) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001577 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001578 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001579
senorblanco@chromium.org514b9222013-01-18 21:53:12 +00001580 const SkBitmap& bm = dev->accessBitmap(false);
1581 int w = bm.width();
1582 int h = bm.height();
1583
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001584 SkImageFilter* filter = paint.getImageFilter();
commit-bot@chromium.org97e49e92013-07-18 19:21:44 +00001585 // This bitmap will own the filtered result as a texture.
1586 SkBitmap filteredBitmap;
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001587
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001588 if (NULL != filter) {
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001589 SkIPoint offset = SkIPoint::Make(0, 0);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +00001590 SkMatrix matrix(*draw.fMatrix);
1591 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
1592 if (filter_texture(this, fContext, devTex, filter, w, h, matrix, &filteredBitmap,
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001593 &offset)) {
commit-bot@chromium.org97e49e92013-07-18 19:21:44 +00001594 devTex = filteredBitmap.getTexture();
1595 w = filteredBitmap.width();
1596 h = filteredBitmap.height();
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001597 x += offset.fX;
1598 y += offset.fY;
senorblanco@chromium.org194d7752013-07-24 22:19:24 +00001599 } else {
1600 return;
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001601 }
1602 }
1603
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001604 GrPaint grPaint;
1605 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1606
1607 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1608 return;
1609 }
1610
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001611 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
bsalomon@google.com81712882012-11-01 17:12:34 +00001612 SkIntToScalar(y),
1613 SkIntToScalar(w),
1614 SkIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001615
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001616 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1617 // scratch texture).
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001618 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
bsalomon@google.com81712882012-11-01 17:12:34 +00001619 SK_Scalar1 * h / devTex->height());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001620
1621 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001622}
1623
reed@google.com8926b162012-03-23 15:36:36 +00001624bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
senorblanco@chromium.orgd043cce2013-04-08 19:43:22 +00001625 return filter->canFilterImageGPU();
reed@google.com8926b162012-03-23 15:36:36 +00001626}
1627
1628bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1629 const SkMatrix& ctm,
1630 SkBitmap* result, SkIPoint* offset) {
1631 // want explicitly our impl, so guard against a subclass of us overriding it
1632 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001633 return false;
1634 }
reed@google.com8926b162012-03-23 15:36:36 +00001635
1636 SkAutoLockPixels alp(src, !src.getTexture());
1637 if (!src.getTexture() && !src.readyToDraw()) {
1638 return false;
1639 }
1640
reed@google.com8926b162012-03-23 15:36:36 +00001641 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001642 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1643 // must be pushed upstack.
1644 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001645
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001646 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctm, result,
1647 offset);
reed@google.com76dd2772012-01-05 21:15:07 +00001648}
1649
reed@google.comac10a2d2010-12-22 21:39:39 +00001650///////////////////////////////////////////////////////////////////////////////
1651
1652// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001653static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001654 kTriangles_GrPrimitiveType,
1655 kTriangleStrip_GrPrimitiveType,
1656 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001657};
1658
1659void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1660 int vertexCount, const SkPoint vertices[],
1661 const SkPoint texs[], const SkColor colors[],
1662 SkXfermode* xmode,
1663 const uint16_t indices[], int indexCount,
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.com5782d712011-01-21 21:03:59 +00001667 GrPaint grPaint;
bsalomon@google.com5782d712011-01-21 21:03:59 +00001668 // we ignore the shader if texs is null.
1669 if (NULL == texs) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001670 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001671 return;
1672 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001673 } else {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001674 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001675 return;
1676 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001677 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001678
1679 if (NULL != xmode && NULL != texs && NULL != colors) {
reed@google.com8d3cd7a2013-01-30 21:36:11 +00001680 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001681 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1682#if 0
1683 return
1684#endif
1685 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001686 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001687
bsalomon@google.com498776a2011-08-16 19:20:44 +00001688 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1689 if (NULL != colors) {
1690 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001691 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001692 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001693 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001694 }
1695 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001696 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001697 fContext->drawVertices(grPaint,
1698 gVertexMode2PrimitiveType[vmode],
1699 vertexCount,
1700 (GrPoint*) vertices,
1701 (GrPoint*) texs,
1702 colors,
1703 indices,
1704 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001705}
1706
1707///////////////////////////////////////////////////////////////////////////////
1708
1709static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001710 GrFontScaler* scaler = (GrFontScaler*)data;
1711 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001712}
1713
1714static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1715 void* auxData;
1716 GrFontScaler* scaler = NULL;
1717 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1718 scaler = (GrFontScaler*)auxData;
1719 }
1720 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001721 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001722 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1723 }
1724 return scaler;
1725}
1726
1727static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1728 SkFixed fx, SkFixed fy,
1729 const SkGlyph& glyph) {
1730 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1731
bungeman@google.com15865a72012-01-11 16:28:04 +00001732 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001733
1734 if (NULL == procs->fFontScaler) {
1735 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1736 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001737
bungeman@google.com15865a72012-01-11 16:28:04 +00001738 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1739 glyph.getSubXFixed(),
1740 glyph.getSubYFixed()),
1741 SkFixedFloorToFixed(fx),
1742 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001743 procs->fFontScaler);
1744}
1745
bsalomon@google.com5782d712011-01-21 21:03:59 +00001746SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001747
1748 // deferred allocation
1749 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001750 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001751 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1752 fDrawProcs->fContext = fContext;
1753 }
1754
1755 // init our (and GL's) state
1756 fDrawProcs->fTextContext = context;
1757 fDrawProcs->fFontScaler = NULL;
1758 return fDrawProcs;
1759}
1760
1761void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1762 size_t byteLength, SkScalar x, SkScalar y,
1763 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001764 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001765
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001766 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001767 // this guy will just call our drawPath()
1768 draw.drawText((const char*)text, byteLength, x, y, paint);
1769 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001770 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001771
1772 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001773 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001774 return;
1775 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001776 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001777 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001778 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1779 }
1780}
1781
1782void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1783 size_t byteLength, const SkScalar pos[],
1784 SkScalar constY, int scalarsPerPos,
1785 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001786 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001787
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001788 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001789 // this guy will just call our drawPath()
1790 draw.drawPosText((const char*)text, byteLength, pos, constY,
1791 scalarsPerPos, paint);
1792 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001793 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001794
1795 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001796 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001797 return;
1798 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001799 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001800 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001801 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1802 scalarsPerPos, paint);
1803 }
1804}
1805
1806void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1807 size_t len, const SkPath& path,
1808 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001809 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001810
1811 SkASSERT(draw.fDevice == this);
1812 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1813}
1814
1815///////////////////////////////////////////////////////////////////////////////
1816
reed@google.comf67e4cf2011-03-15 20:56:58 +00001817bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1818 if (!paint.isLCDRenderText()) {
1819 // we're cool with the paint as is
1820 return false;
1821 }
1822
1823 if (paint.getShader() ||
1824 paint.getXfermode() || // unless its srcover
1825 paint.getMaskFilter() ||
1826 paint.getRasterizer() ||
1827 paint.getColorFilter() ||
1828 paint.getPathEffect() ||
1829 paint.isFakeBoldText() ||
1830 paint.getStyle() != SkPaint::kFill_Style) {
1831 // turn off lcd
1832 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1833 flags->fHinting = paint.getHinting();
1834 return true;
1835 }
1836 // we're cool with the paint as is
1837 return false;
1838}
1839
reed@google.com75d939b2011-12-07 15:07:23 +00001840void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001841 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001842 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001843}
1844
reed@google.comf67e4cf2011-03-15 20:56:58 +00001845///////////////////////////////////////////////////////////////////////////////
1846
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001847SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1848 int width, int height,
1849 bool isOpaque,
1850 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001851 GrTextureDesc desc;
1852 desc.fConfig = fRenderTarget->config();
1853 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1854 desc.fWidth = width;
1855 desc.fHeight = height;
1856 desc.fSampleCnt = fRenderTarget->numSamples();
1857
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001858 SkAutoTUnref<GrTexture> texture;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001859 // Skia's convention is to only clear a device if it is non-opaque.
1860 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001861
1862#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1863 // layers are never draw in repeat modes, so we can request an approx
1864 // match and ignore any padding.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001865 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1866 GrContext::kApprox_ScratchTexMatch :
1867 GrContext::kExact_ScratchTexMatch;
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001868 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001869#else
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001870 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001871#endif
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001872 if (NULL != texture.get()) {
1873 return SkNEW_ARGS(SkGpuDevice,(fContext, texture, needClear));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001874 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001875 GrPrintf("---- failed to create compatible device texture [%d %d]\n", width, height);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001876 return NULL;
1877 }
1878}
1879
1880SkGpuDevice::SkGpuDevice(GrContext* context,
1881 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001882 bool needClear)
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001883 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001884
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001885 SkASSERT(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001886 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1887 // cache. We pass true for the third argument so that it will get unlocked.
1888 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001889 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001890}