blob: 89578e8006d6a63e290265957e41d2eb3783d692 [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"
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +000014#include "GrBitmapTextContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
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.com383a6972013-10-21 14:00:07 +0000140/*
141 * GrRenderTarget does not know its opaqueness, only its config, so we have
142 * to make conservative guesses when we return an "equivalent" bitmap.
143 */
reed@google.comaf951c92011-06-16 19:10:39 +0000144static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000145 bool isOpaque;
reed@google.com383a6972013-10-21 14:00:07 +0000146 SkBitmap::Config config = grConfig2skConfig(renderTarget->config(), &isOpaque);
147
reed@google.comaf951c92011-06-16 19:10:39 +0000148 SkBitmap bitmap;
reed@google.com383a6972013-10-21 14:00:07 +0000149 bitmap.setConfig(config, renderTarget->width(), renderTarget->height(), 0,
150 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
reed@google.comaf951c92011-06-16 19:10:39 +0000151 return bitmap;
152}
153
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000154SkGpuDevice* SkGpuDevice::Create(GrSurface* surface) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000155 SkASSERT(NULL != surface);
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000156 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
157 return NULL;
158 }
159 if (surface->asTexture()) {
160 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture()));
161 } else {
162 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget()));
163 }
164}
165
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000166SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000167 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000168 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000169}
170
reed@google.comaf951c92011-06-16 19:10:39 +0000171SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000172 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000173 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000174}
175
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000176void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000177 GrRenderTarget* renderTarget,
178 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000179 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000180
reed@google.comaf951c92011-06-16 19:10:39 +0000181 fContext = context;
182 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000183
reed@google.comaf951c92011-06-16 19:10:39 +0000184 fRenderTarget = NULL;
185 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000186
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000187 SkASSERT(NULL != renderTarget);
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000188 fRenderTarget = renderTarget;
189 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000190
bsalomon@google.coma2921122012-08-28 12:34:17 +0000191 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
192 // on the RT but not vice-versa.
193 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
194 // busting chrome (for a currently unknown reason).
195 GrSurface* surface = fRenderTarget->asTexture();
196 if (NULL == surface) {
197 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000198 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000199 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000200
reed@google.comaf951c92011-06-16 19:10:39 +0000201 this->setPixelRef(pr, 0)->unref();
202}
203
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000204SkGpuDevice::SkGpuDevice(GrContext* context,
205 SkBitmap::Config config,
206 int width,
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000207 int height,
208 int sampleCount)
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000209 : SkBitmapDevice(config, width, height, false /*isOpaque*/) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000210
reed@google.comac10a2d2010-12-22 21:39:39 +0000211 fDrawProcs = NULL;
212
reed@google.com7b201d22011-01-11 18:59:23 +0000213 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000214 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000215
reed@google.comac10a2d2010-12-22 21:39:39 +0000216 fRenderTarget = NULL;
217 fNeedClear = false;
218
reed@google.comaf951c92011-06-16 19:10:39 +0000219 if (config != SkBitmap::kRGB_565_Config) {
220 config = SkBitmap::kARGB_8888_Config;
221 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000222
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000223 GrTextureDesc desc;
224 desc.fFlags = kRenderTarget_GrTextureFlagBit;
225 desc.fWidth = width;
226 desc.fHeight = height;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000227 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
228 desc.fSampleCnt = sampleCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000229
bsalomon@google.coma2921122012-08-28 12:34:17 +0000230 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000231
bsalomon@google.coma2921122012-08-28 12:34:17 +0000232 if (NULL != texture) {
233 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000234 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000235
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000236 SkASSERT(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000237
reed@google.comaf951c92011-06-16 19:10:39 +0000238 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000239 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000240 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000241 } else {
242 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
243 width, height);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000244 SkASSERT(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000245 }
246}
247
248SkGpuDevice::~SkGpuDevice() {
249 if (fDrawProcs) {
250 delete fDrawProcs;
251 }
252
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000253 // The GrContext takes a ref on the target. We don't want to cause the render
254 // target to be unnecessarily kept alive.
255 if (fContext->getRenderTarget() == fRenderTarget) {
256 fContext->setRenderTarget(NULL);
257 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000258
robertphillips@google.com055f9082012-10-24 13:24:11 +0000259 if (fContext->getClip() == &fClipData) {
260 fContext->setClip(NULL);
261 }
262
bsalomon@google.coma2921122012-08-28 12:34:17 +0000263 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000264 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000265}
266
reed@google.comac10a2d2010-12-22 21:39:39 +0000267///////////////////////////////////////////////////////////////////////////////
268
269void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000270 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000271 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000272}
273
274///////////////////////////////////////////////////////////////////////////////
275
bsalomon@google.comc4364992011-11-07 15:54:49 +0000276namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000277GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000278 switch (config8888) {
279 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000280 *flags = 0;
281 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000282 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000283 *flags = GrContext::kUnpremul_PixelOpsFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000284 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000285 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000286 *flags = 0;
287 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000288 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000289 *flags = GrContext::kUnpremul_PixelOpsFlag;
290 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000292 *flags = 0;
293 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000295 *flags = GrContext::kUnpremul_PixelOpsFlag;
296 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000297 default:
298 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000299 *flags = 0; // suppress warning
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000300 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000301 }
302}
303}
304
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000305bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
306 int x, int y,
307 SkCanvas::Config8888 config8888) {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000308 DO_DEFERRED_CLEAR();
bsalomon@google.com910267d2011-11-02 20:06:25 +0000309 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
310 SkASSERT(!bitmap.isNull());
311 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000312
bsalomon@google.com910267d2011-11-02 20:06:25 +0000313 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000314 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000315 uint32_t flags;
316 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000317 return fContext->readRenderTargetPixels(fRenderTarget,
318 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000319 bitmap.width(),
320 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000321 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000322 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000323 bitmap.rowBytes(),
324 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000325}
326
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000327void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
328 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000329 SkAutoLockPixels alp(bitmap);
330 if (!bitmap.readyToDraw()) {
331 return;
332 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000333
334 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000335 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000336 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000337 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000338 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000339 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000340 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000341 }
342
bsalomon@google.com6f379512011-11-16 20:36:03 +0000343 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000344 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000345}
346
robertphillips@google.com46f93502012-08-07 15:38:08 +0000347namespace {
humper@google.com05af1af2013-01-07 16:47:43 +0000348void purgeClipCB(int genID, void* ) {
robertphillips@google.com46f93502012-08-07 15:38:08 +0000349
350 if (SkClipStack::kInvalidGenID == genID ||
351 SkClipStack::kEmptyGenID == genID ||
352 SkClipStack::kWideOpenGenID == genID) {
353 // none of these cases will have a cached clip mask
354 return;
355 }
356
357}
358};
359
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000360void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
361 INHERITED::onAttachToCanvas(canvas);
362
363 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000364 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000365
366 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000367}
368
369void SkGpuDevice::onDetachFromCanvas() {
370 INHERITED::onDetachFromCanvas();
371
robertphillips@google.com46f93502012-08-07 15:38:08 +0000372 // TODO: iterate through the clip stack and clean up any cached clip masks
373 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
374
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000375 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000376}
377
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000378// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000379// and not the state from some other canvas/device
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000380void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000381 SkASSERT(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000382
reed@google.comac10a2d2010-12-22 21:39:39 +0000383 fContext->setRenderTarget(fRenderTarget);
384
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000385 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000386
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000387 if (forceIdentity) {
388 fContext->setIdentityMatrix();
389 } else {
390 fContext->setMatrix(*draw.fMatrix);
391 }
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000392 fClipData.fOrigin = this->getOrigin();
reed@google.comac10a2d2010-12-22 21:39:39 +0000393
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000394 fContext->setClip(&fClipData);
395
396 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000397}
398
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000399GrRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000400 DO_DEFERRED_CLEAR();
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000401 return fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000402}
403
reed@google.comac10a2d2010-12-22 21:39:39 +0000404///////////////////////////////////////////////////////////////////////////////
405
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000406SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
407SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
408SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
409SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
410SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
411 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000412SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
413 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000414SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
415SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000416
bsalomon@google.com84405e02012-03-05 19:57:21 +0000417namespace {
418
419// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
420// justAlpha indicates that skPaint's alpha should be used rather than the color
421// Callers may subsequently modify the GrPaint. Setting constantColor indicates
422// that the final paint will draw the same color at every pixel. This allows
423// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000424// color once while converting to GrPaint and then ignored.
425inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
426 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000427 bool justAlpha,
428 bool constantColor,
429 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000430
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000431 grPaint->setDither(skPaint.isDither());
432 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000433
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000434 SkXfermode::Coeff sm;
435 SkXfermode::Coeff dm;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000436
437 SkXfermode* mode = skPaint.getXfermode();
bsalomon@google.comdb446252013-03-27 18:46:16 +0000438 GrEffectRef* xferEffect = NULL;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000439 if (SkXfermode::AsNewEffectOrCoeff(mode, dev->context(), &xferEffect, &sm, &dm)) {
440 if (NULL != xferEffect) {
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000441 grPaint->addColorEffect(xferEffect)->unref();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000442 sm = SkXfermode::kOne_Coeff;
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000443 dm = SkXfermode::kZero_Coeff;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000444 }
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000445 } else {
446 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000447#if 0
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000448 return false;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000449#else
450 // Fall back to src-over
451 sm = SkXfermode::kOne_Coeff;
452 dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000453#endif
bsalomon@google.com5782d712011-01-21 21:03:59 +0000454 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000455 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000456
bsalomon@google.com5782d712011-01-21 21:03:59 +0000457 if (justAlpha) {
458 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000459 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000460 // justAlpha is currently set to true only if there is a texture,
461 // so constantColor should not also be true.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000462 SkASSERT(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000464 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000465 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000466
Scroggo97c88c22011-05-11 14:05:25 +0000467 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000468 if (NULL != colorFilter) {
469 // if the source color is a constant then apply the filter here once rather than per pixel
470 // in a shader.
471 if (constantColor) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000472 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000473 grPaint->setColor(SkColor2GrColor(filtered));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000474 } else {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000475 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
bsalomon@google.com021fc732012-10-25 12:47:42 +0000476 if (NULL != effect.get()) {
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000477 grPaint->addColorEffect(effect);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000478 } else {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000479 // TODO: rewrite this using asNewEffect()
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000480 SkColor color;
481 SkXfermode::Mode filterMode;
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000482 if (colorFilter->asColorMode(&color, &filterMode)) {
483 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000484 }
485 }
Scroggod757df22011-05-16 13:11:16 +0000486 }
Scroggo97c88c22011-05-11 14:05:25 +0000487 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000488
bsalomon@google.com5782d712011-01-21 21:03:59 +0000489 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000490}
491
bsalomon@google.com84405e02012-03-05 19:57:21 +0000492// This function is similar to skPaint2GrPaintNoShader but also converts
bsalomon@google.com08283af2012-10-26 13:01:20 +0000493// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
bsalomon@google.com84405e02012-03-05 19:57:21 +0000494// be used is set on grPaint and returned in param act. constantColor has the
495// same meaning as in skPaint2GrPaintNoShader.
496inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
497 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000498 bool constantColor,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000499 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000500 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000501 if (NULL == shader) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000502 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000503 }
504
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000505 // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state
506 // Also require shader to set the render target .
507 GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL);
508 GrContext::AutoRenderTarget(dev->context(), NULL);
509
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000510 // setup the shader as the first color effect on the paint
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000511 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000512 if (NULL != effect.get()) {
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000513 grPaint->addColorEffect(effect);
514 // Now setup the rest of the paint.
515 return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
516 } else {
517 // We still don't have SkColorShader::asNewEffect() implemented.
518 SkShader::GradientInfo info;
519 SkColor color;
rileya@google.com91f319c2012-07-25 17:18:31 +0000520
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000521 info.fColors = &color;
522 info.fColorOffsets = NULL;
523 info.fColorCount = 1;
524 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
525 SkPaint copy(skPaint);
526 copy.setShader(NULL);
527 // modulate the paint alpha by the shader's solid color alpha
528 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
529 copy.setColor(SkColorSetA(color, newA));
530 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
531 } else {
532 return false;
533 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000534 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000535}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000536}
reed@google.comac10a2d2010-12-22 21:39:39 +0000537
538///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com07f81a52013-09-17 12:26:23 +0000539void SkGpuDevice::getGlobalBounds(SkIRect* bounds) const {
540 if (NULL != bounds) {
541 const SkIPoint& origin = this->getOrigin();
542 bounds->setXYWH(origin.x(), origin.y(),
543 this->width(), this->height());
544 }
545}
546
547SkBitmap::Config SkGpuDevice::config() const {
548 if (NULL == fRenderTarget) {
549 return SkBitmap::kNo_Config;
550 }
551
552 bool isOpaque;
553 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
554}
555
bsalomon@google.com398109c2011-04-14 18:40:27 +0000556void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com32cf29e2013-01-25 15:03:18 +0000557 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
558 fContext->clear(&rect, SkColor2GrColor(color), fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000559 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000560}
561
reed@google.comac10a2d2010-12-22 21:39:39 +0000562void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000563 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000564
bsalomon@google.com5782d712011-01-21 21:03:59 +0000565 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000566 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000567 return;
568 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000569
570 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000571}
572
573// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000574static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000575 kPoints_GrPrimitiveType,
576 kLines_GrPrimitiveType,
577 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000578};
579
580void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000581 size_t count, const SkPoint pts[], const SkPaint& paint) {
epoger@google.comb58772f2013-03-08 09:09:10 +0000582 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000583 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000584
585 SkScalar width = paint.getStrokeWidth();
586 if (width < 0) {
587 return;
588 }
589
djsollen@google.com24202c72013-08-01 18:36:08 +0000590 // we only handle hairlines and paints without path effects or mask filters,
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000591 // else we let the SkDraw call our drawPath()
djsollen@google.com24202c72013-08-01 18:36:08 +0000592 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000593 draw.drawPoints(mode, count, pts, paint, true);
594 return;
595 }
596
bsalomon@google.com5782d712011-01-21 21:03:59 +0000597 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000598 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 return;
600 }
601
bsalomon@google.com5782d712011-01-21 21:03:59 +0000602 fContext->drawVertices(grPaint,
603 gPointMode2PrimtiveType[mode],
604 count,
605 (GrPoint*)pts,
606 NULL,
607 NULL,
608 NULL,
609 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000610}
611
reed@google.comc9aa5872011-04-05 21:05:37 +0000612///////////////////////////////////////////////////////////////////////////////
613
reed@google.comac10a2d2010-12-22 21:39:39 +0000614void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000615 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000616 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000617 CHECK_SHOULD_DRAW(draw, false);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000618
bungeman@google.com79bd8772011-07-18 15:34:08 +0000619 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000620 SkScalar width = paint.getStrokeWidth();
621
622 /*
623 We have special code for hairline strokes, miter-strokes, and fills.
624 Anything else we just call our path code.
625 */
626 bool usePath = doStroke && width > 0 &&
627 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000628 // another two reasons we might need to call drawPath...
629 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000630 usePath = true;
631 }
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000632 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000633#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000634 if (doStroke) {
635#endif
636 usePath = true;
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000637#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000638 } else {
639 usePath = !fContext->getMatrix().preservesRightAngles();
640 }
641#endif
reed@google.com67db6642011-05-26 11:46:35 +0000642 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000643 // small miter limit means right angles show bevel...
644 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
645 paint.getStrokeMiter() < SK_ScalarSqrt2)
646 {
647 usePath = true;
648 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000649 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000650 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
651 usePath = true;
652 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000653
654 if (usePath) {
655 SkPath path;
656 path.addRect(rect);
657 this->drawPath(draw, path, paint, NULL, true);
658 return;
659 }
660
661 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000662 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000663 return;
664 }
reed@google.com20efde72011-05-09 17:00:02 +0000665 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000666}
667
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000668///////////////////////////////////////////////////////////////////////////////
669
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000670void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
671 const SkPaint& paint) {
672 CHECK_FOR_NODRAW_ANNOTATION(paint);
673 CHECK_SHOULD_DRAW(draw, false);
674
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000675 bool usePath = !rect.isSimple();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000676 // another two reasons we might need to call drawPath...
677 if (paint.getMaskFilter() || paint.getPathEffect()) {
678 usePath = true;
679 }
680 // until we can rotate rrects...
681 if (!usePath && !fContext->getMatrix().rectStaysRect()) {
682 usePath = true;
683 }
684
685 if (usePath) {
686 SkPath path;
687 path.addRRect(rect);
688 this->drawPath(draw, path, paint, NULL, true);
689 return;
690 }
691
692 GrPaint grPaint;
693 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
694 return;
695 }
696
697 SkStrokeRec stroke(paint);
698 fContext->drawRRect(grPaint, rect, stroke);
699}
700
701///////////////////////////////////////////////////////////////////////////////
702
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000703void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
704 const SkPaint& paint) {
705 CHECK_FOR_NODRAW_ANNOTATION(paint);
706 CHECK_SHOULD_DRAW(draw, false);
707
708 bool usePath = false;
709 // some basic reasons we might need to call drawPath...
710 if (paint.getMaskFilter() || paint.getPathEffect()) {
711 usePath = true;
712 }
713
714 if (usePath) {
715 SkPath path;
716 path.addOval(oval);
717 this->drawPath(draw, path, paint, NULL, true);
718 return;
719 }
720
721 GrPaint grPaint;
722 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
723 return;
724 }
725 SkStrokeRec stroke(paint);
726
727 fContext->drawOval(grPaint, oval, stroke);
728}
729
reed@google.com69302852011-02-16 18:08:07 +0000730#include "SkMaskFilter.h"
731#include "SkBounder.h"
732
bsalomon@google.com85003222012-03-28 14:44:37 +0000733///////////////////////////////////////////////////////////////////////////////
734
735// helpers for applying mask filters
736namespace {
737
robertphillips@google.com49149312013-07-03 15:34:35 +0000738// Draw a mask using the supplied paint. Since the coverage/geometry
739// is already burnt into the mask this boils down to a rect draw.
740// Return true if the mask was successfully drawn.
741bool draw_mask(GrContext* context, const SkRect& maskRect,
742 GrPaint* grp, GrTexture* mask) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000743 GrContext::AutoMatrix am;
744 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000745 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000746 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000747
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000748 SkMatrix matrix;
robertphillips@google.com49149312013-07-03 15:34:35 +0000749 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
750 matrix.postIDiv(mask->width(), mask->height());
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000751
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000752 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
robertphillips@google.com49149312013-07-03 15:34:35 +0000753 context->drawRect(*grp, maskRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000754 return true;
755}
756
robertphillips@google.com49149312013-07-03 15:34:35 +0000757bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
758 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
759 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000760 SkMask srcM, dstM;
761
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000762 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
763 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000764 return false;
765 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000766 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000767
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000768 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000769 return false;
770 }
771 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000772 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000773
774 if (clip.quickReject(dstM.fBounds)) {
775 return false;
776 }
777 if (bounder && !bounder->doIRect(dstM.fBounds)) {
778 return false;
779 }
780
781 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000782 // the current clip (and identity matrix) and GrPaint settings
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000783 GrTextureDesc desc;
784 desc.fWidth = dstM.fBounds.width();
785 desc.fHeight = dstM.fBounds.height();
786 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000787
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000788 GrAutoScratchTexture ast(context, desc);
789 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000790
reed@google.com69302852011-02-16 18:08:07 +0000791 if (NULL == texture) {
792 return false;
793 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000794 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000795 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000796
robertphillips@google.com49149312013-07-03 15:34:35 +0000797 SkRect maskRect = SkRect::MakeFromIRect(dstM.fBounds);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000798
robertphillips@google.com49149312013-07-03 15:34:35 +0000799 return draw_mask(context, maskRect, grp, texture);
800}
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000801
robertphillips@google.com49149312013-07-03 15:34:35 +0000802// Create a mask of 'devPath' and place the result in 'mask'. Return true on
803// success; false otherwise.
804bool create_mask_GPU(GrContext* context,
805 const SkRect& maskRect,
806 const SkPath& devPath,
807 const SkStrokeRec& stroke,
808 bool doAA,
809 GrAutoScratchTexture* mask) {
810 GrTextureDesc desc;
811 desc.fFlags = kRenderTarget_GrTextureFlagBit;
812 desc.fWidth = SkScalarCeilToInt(maskRect.width());
813 desc.fHeight = SkScalarCeilToInt(maskRect.height());
814 // We actually only need A8, but it often isn't supported as a
815 // render target so default to RGBA_8888
816 desc.fConfig = kRGBA_8888_GrPixelConfig;
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +0000817 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillips@google.com49149312013-07-03 15:34:35 +0000818 desc.fConfig = kAlpha_8_GrPixelConfig;
819 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000820
robertphillips@google.com49149312013-07-03 15:34:35 +0000821 mask->set(context, desc);
822 if (NULL == mask->texture()) {
823 return false;
824 }
825
826 GrTexture* maskTexture = mask->texture();
827 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
828
829 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
830 GrContext::AutoClip ac(context, clipRect);
831
832 context->clear(NULL, 0x0);
833
834 GrPaint tempPaint;
835 if (doAA) {
836 tempPaint.setAntiAlias(true);
837 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
838 // blend coeff of zero requires dual source blending support in order
839 // to properly blend partially covered pixels. This means the AA
840 // code path may not be taken. So we use a dst blend coeff of ISA. We
841 // could special case AA draws to a dst surface with known alpha=0 to
842 // use a zero dst coeff when dual source blending isn't available.
843 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
844 }
845
846 GrContext::AutoMatrix am;
847
848 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
849 SkMatrix translate;
850 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
851 am.set(context, translate);
852 context->drawPath(tempPaint, devPath, stroke);
reed@google.com69302852011-02-16 18:08:07 +0000853 return true;
854}
reed@google.com69302852011-02-16 18:08:07 +0000855
robertphillips@google.com49149312013-07-03 15:34:35 +0000856SkBitmap wrap_texture(GrTexture* texture) {
857 SkBitmap result;
858 bool dummy;
859 SkBitmap::Config config = grConfig2skConfig(texture->config(), &dummy);
860 result.setConfig(config, texture->width(), texture->height());
861 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (texture)))->unref();
862 return result;
bsalomon@google.com85003222012-03-28 14:44:37 +0000863}
864
robertphillips@google.com49149312013-07-03 15:34:35 +0000865};
bsalomon@google.com85003222012-03-28 14:44:37 +0000866
reed@google.com0c219b62011-02-16 21:31:18 +0000867void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000868 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000869 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000870 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000871 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000872
bsalomon@google.com5782d712011-01-21 21:03:59 +0000873 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000874 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000875 return;
876 }
877
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000878 // can we cheat, and treat a thin stroke as a hairline w/ coverage
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000879 // if we can, we draw lots faster (raster device does this same test)
880 SkScalar hairlineCoverage;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000881 bool doHairLine = SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage);
882 if (doHairLine) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000883 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000884 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000885
reed@google.comfe626382011-09-21 13:50:35 +0000886 // If we have a prematrix, apply it to the path, optimizing for the case
887 // where the original path can in fact be modified in place (even though
888 // its parameter type is const).
889 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000890 SkPath tmpPath, effectPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000891
892 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +0000893 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +0000894
reed@google.come3445642011-02-16 23:20:39 +0000895 if (!pathIsMutable) {
896 result = &tmpPath;
897 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000898 }
reed@google.come3445642011-02-16 23:20:39 +0000899 // should I push prePathMatrix on our MV stack temporarily, instead
900 // of applying it here? See SkDraw.cpp
901 pathPtr->transform(*prePathMatrix, result);
902 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +0000903 }
reed@google.com0c219b62011-02-16 21:31:18 +0000904 // at this point we're done with prePathMatrix
905 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +0000906
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000907 SkStrokeRec stroke(paint);
908 SkPathEffect* pathEffect = paint.getPathEffect();
reed@google.com4bbdeac2013-01-24 21:03:11 +0000909 const SkRect* cullRect = NULL; // TODO: what is our bounds?
910 if (pathEffect && pathEffect->filterPath(&effectPath, *pathPtr, &stroke,
911 cullRect)) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000912 pathPtr = &effectPath;
913 }
914
915 if (!pathEffect && doHairLine) {
916 stroke.setHairlineStyle();
reed@google.com0c219b62011-02-16 21:31:18 +0000917 }
918
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000919 if (paint.getMaskFilter()) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000920 if (!stroke.isHairlineStyle()) {
921 if (stroke.applyToPath(&tmpPath, *pathPtr)) {
922 pathPtr = &tmpPath;
robertphillips@google.com49149312013-07-03 15:34:35 +0000923 pathIsMutable = true;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000924 stroke.setFillStyle();
925 }
926 }
927
reed@google.com0c219b62011-02-16 21:31:18 +0000928 // avoid possibly allocating a new path in transform if we can
929 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
930
931 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000932 pathPtr->transform(fContext->getMatrix(), devPathPtr);
robertphillips@google.com49149312013-07-03 15:34:35 +0000933
934 SkRect maskRect;
935 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
936 draw.fClip->getBounds(),
937 fContext->getMatrix(),
938 &maskRect)) {
939 SkIRect finalIRect;
940 maskRect.roundOut(&finalIRect);
941 if (draw.fClip->quickReject(finalIRect)) {
942 // clipped out
943 return;
944 }
945 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
946 // nothing to draw
947 return;
948 }
949
950 GrAutoScratchTexture mask;
951
skia.committer@gmail.com1842adf2013-07-04 07:01:07 +0000952 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
robertphillips@google.com49149312013-07-03 15:34:35 +0000953 grPaint.isAntiAlias(), &mask)) {
954 GrTexture* filtered;
955
956 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(), maskRect, &filtered, true)) {
commit-bot@chromium.org63972172013-10-16 13:53:54 +0000957 // filterMaskGPU gives us ownership of a ref to the result
robertphillips@google.com49149312013-07-03 15:34:35 +0000958 SkAutoTUnref<GrTexture> atu(filtered);
959
commit-bot@chromium.org63972172013-10-16 13:53:54 +0000960 // If the scratch texture that we used as the filter src also holds the filter
961 // result then we must detach so that this texture isn't recycled for a later
962 // draw.
963 if (filtered == mask.texture()) {
964 mask.detach();
965 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
966 }
967
robertphillips@google.com49149312013-07-03 15:34:35 +0000968 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
969 // This path is completely drawn
970 return;
971 }
972 }
973 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000974 }
robertphillips@google.com49149312013-07-03 15:34:35 +0000975
976 // draw the mask on the CPU - this is a fallthrough path in case the
977 // GPU path fails
978 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
979 SkPaint::kFill_Style;
980 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
981 *draw.fClip, draw.fBounder, &grPaint, style);
reed@google.com69302852011-02-16 18:08:07 +0000982 return;
983 }
reed@google.com69302852011-02-16 18:08:07 +0000984
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000985 fContext->drawPath(grPaint, *pathPtr, stroke);
reed@google.comac10a2d2010-12-22 21:39:39 +0000986}
987
bsalomon@google.comfb309512011-11-30 14:13:48 +0000988namespace {
989
990inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
991 int tilesX = (r / tileSize) - (l / tileSize) + 1;
992 int tilesY = (b / tileSize) - (t / tileSize) + 1;
993 return tilesX * tilesY;
994}
995
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000996inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +0000997 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +0000998 int maxTextureSize) {
999 static const int kSmallTileSize = 1 << 10;
1000 if (maxTextureSize <= kSmallTileSize) {
1001 return maxTextureSize;
1002 }
1003
1004 size_t maxTexTotalTileSize;
1005 size_t smallTotalTileSize;
1006
robertphillips@google.combac6b052012-09-28 18:06:49 +00001007 SkIRect iSrc;
1008 src.roundOut(&iSrc);
1009
1010 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1011 iSrc.fTop,
1012 iSrc.fRight,
1013 iSrc.fBottom,
1014 maxTextureSize);
1015 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1016 iSrc.fTop,
1017 iSrc.fRight,
1018 iSrc.fBottom,
1019 kSmallTileSize);
1020
bsalomon@google.comfb309512011-11-30 14:13:48 +00001021 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1022 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1023
1024 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1025 return kSmallTileSize;
1026 } else {
1027 return maxTextureSize;
1028 }
1029}
1030}
1031
1032bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001033 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001034 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001035 // if bitmap is explictly texture backed then just use the texture
1036 if (NULL != bitmap.getTexture()) {
1037 return false;
1038 }
1039 // if it's larger than the max texture size, then we have no choice but
1040 // tiling
1041 const int maxTextureSize = fContext->getMaxTextureSize();
1042 if (bitmap.width() > maxTextureSize ||
1043 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001044 return true;
1045 }
1046 // if we are going to have to draw the whole thing, then don't tile
1047 if (NULL == srcRectPtr) {
1048 return false;
1049 }
1050 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001051 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001052 return false;
1053 }
1054
1055 // At this point we know we could do the draw by uploading the entire bitmap
1056 // as a texture. However, if the texture would be large compared to the
1057 // cache size and we don't require most of it for this draw then tile to
1058 // reduce the amount of upload and cache spill.
1059
1060 // assumption here is that sw bitmap size is a good proxy for its size as
1061 // a texture
1062 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001063 size_t cacheSize;
1064 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001065 if (bmpSize < cacheSize / 2) {
1066 return false;
1067 }
1068
robertphillips@google.combac6b052012-09-28 18:06:49 +00001069 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1070 srcRectPtr->height() / bitmap.height());
1071 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001072 return true;
1073 } else {
1074 return false;
1075 }
1076}
1077
reed@google.comac10a2d2010-12-22 21:39:39 +00001078void SkGpuDevice::drawBitmap(const SkDraw& draw,
1079 const SkBitmap& bitmap,
reed@google.comac10a2d2010-12-22 21:39:39 +00001080 const SkMatrix& m,
1081 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001082 // We cannot call drawBitmapRect here since 'm' could be anything
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001083 this->drawBitmapCommon(draw, bitmap, NULL, m, paint,
robertphillips@google.com31acc112013-08-20 12:13:48 +00001084 SkCanvas::kNone_DrawBitmapRectFlag);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001085}
1086
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001087// This method outsets 'iRect' by 1 all around and then clamps its extents to
1088// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1089// of 'iRect' for all possible outsets/clamps.
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +00001090static inline void clamped_unit_outset_with_offset(SkIRect* iRect, SkPoint* offset,
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001091 const SkIRect& clamp) {
1092 iRect->outset(1, 1);
1093
1094 if (iRect->fLeft < clamp.fLeft) {
1095 iRect->fLeft = clamp.fLeft;
1096 } else {
1097 offset->fX -= SK_Scalar1;
1098 }
1099 if (iRect->fTop < clamp.fTop) {
1100 iRect->fTop = clamp.fTop;
1101 } else {
1102 offset->fY -= SK_Scalar1;
1103 }
1104
1105 if (iRect->fRight > clamp.fRight) {
1106 iRect->fRight = clamp.fRight;
1107 }
1108 if (iRect->fBottom > clamp.fBottom) {
1109 iRect->fBottom = clamp.fBottom;
1110 }
1111}
1112
robertphillips@google.combac6b052012-09-28 18:06:49 +00001113void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1114 const SkBitmap& bitmap,
1115 const SkRect* srcRectPtr,
1116 const SkMatrix& m,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001117 const SkPaint& paint,
1118 SkCanvas::DrawBitmapRectFlags flags) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001119 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001120
robertphillips@google.combac6b052012-09-28 18:06:49 +00001121 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001122 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001123 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001124 } else {
1125 srcRect = *srcRectPtr;
1126 }
1127
junov@google.comd935cfb2011-06-27 20:48:23 +00001128 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001129 // Convert the bitmap to a shader so that the rect can be drawn
1130 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001131 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001132 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001133 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001134 if (NULL != srcRectPtr) {
1135 SkIRect iSrc;
1136 srcRect.roundOut(&iSrc);
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001137
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +00001138 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001139 SkIntToScalar(iSrc.fTop));
1140
1141 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1142 // In bleed mode we want to expand the src rect on all sides
1143 // but stay within the bitmap bounds
1144 SkIRect iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1145 clamped_unit_outset_with_offset(&iSrc, &offset, iClampRect);
1146 }
1147
robertphillips@google.combac6b052012-09-28 18:06:49 +00001148 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001149 return; // extraction failed
1150 }
1151 bitmapPtr = &tmp;
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001152 srcRect.offset(-offset.fX, -offset.fY);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001153 // The source rect has changed so update the matrix
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001154 newM.preTranslate(offset.fX, offset.fY);
junov@google.comd935cfb2011-06-27 20:48:23 +00001155 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001156
junov@google.comd935cfb2011-06-27 20:48:23 +00001157 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001158 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001159 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001160
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001161 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001162 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001163 // also affect the behavior of the mask filter.
1164 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001165 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001166 SkDraw transformedDraw(draw);
1167 transformedDraw.fMatrix = &drawMatrix;
1168
robertphillips@google.combac6b052012-09-28 18:06:49 +00001169 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001170
junov@google.comd935cfb2011-06-27 20:48:23 +00001171 return;
1172 }
commit-bot@chromium.org15455b22013-10-21 14:48:17 +00001173
1174 fContext->concatMatrix(m);
junov@google.comd935cfb2011-06-27 20:48:23 +00001175
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001176 GrTextureParams params;
humper@google.comb86add12013-07-25 18:49:07 +00001177 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1178 GrTextureParams::FilterMode textureFilterMode;
1179 switch(paintFilterLevel) {
1180 case SkPaint::kNone_FilterLevel:
1181 textureFilterMode = GrTextureParams::kNone_FilterMode;
1182 break;
1183 case SkPaint::kLow_FilterLevel:
1184 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1185 break;
1186 case SkPaint::kMedium_FilterLevel:
1187 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1188 break;
1189 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgc5e57bd2013-09-19 22:11:38 +00001190 // Fall back to mips for now
humper@google.comb86add12013-07-25 18:49:07 +00001191 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1192 break;
humper@google.comf9ed6fe2013-07-25 19:09:47 +00001193 default:
1194 SkErrorInternals::SetError( kInvalidPaint_SkError,
1195 "Sorry, I don't understand the filtering "
1196 "mode you asked for. Falling back to "
1197 "MIPMaps.");
1198 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1199 break;
skia.committer@gmail.com956b3102013-07-26 07:00:58 +00001200
humper@google.comb86add12013-07-25 18:49:07 +00001201 }
skia.committer@gmail.com956b3102013-07-26 07:00:58 +00001202
humper@google.comb86add12013-07-25 18:49:07 +00001203 params.setFilterMode(textureFilterMode);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001204
robertphillips@google.combac6b052012-09-28 18:06:49 +00001205 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001206 // take the simple case
commit-bot@chromium.org15455b22013-10-21 14:48:17 +00001207 this->internalDrawBitmap(bitmap, srcRect, params, paint, flags);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001208 } else {
commit-bot@chromium.org15455b22013-10-21 14:48:17 +00001209 this->drawTiledBitmap(bitmap, srcRect, params, paint, flags);
reed@google.comac10a2d2010-12-22 21:39:39 +00001210 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001211}
1212
1213// Break 'bitmap' into several tiles to draw it since it has already
1214// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001215void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001216 const SkRect& srcRect,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001217 const GrTextureParams& params,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001218 const SkPaint& paint,
1219 SkCanvas::DrawBitmapRectFlags flags) {
robertphillips@google.comd7ca6612013-08-20 12:09:32 +00001220 int maxTextureSize = fContext->getMaxTextureSize();
1221 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel()) {
1222 // We may need a skosh more room if we have to bump out the tile
1223 // by 1 pixel all around
1224 maxTextureSize -= 2;
1225 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001226
1227 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001228
reed@google.comac10a2d2010-12-22 21:39:39 +00001229 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001230 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001231 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001232 const GrRenderTarget* rt = fContext->getRenderTarget();
1233 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1234 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1235 return;
1236 }
commit-bot@chromium.org15455b22013-10-21 14:48:17 +00001237 SkMatrix inverse;
1238 if (!fContext->getMatrix().invert(&inverse)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001239 return;
1240 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001241 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001242 }
1243
bsalomon@google.comfb309512011-11-30 14:13:48 +00001244 int nx = bitmap.width() / tileSize;
1245 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001246 for (int x = 0; x <= nx; x++) {
1247 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001248 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001249 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001250 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001251 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001252 SkIntToScalar((y + 1) * tileSize));
1253
1254 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001255 continue;
1256 }
1257
robertphillips@google.combac6b052012-09-28 18:06:49 +00001258 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001259 continue;
1260 }
1261
1262 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001263 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001264 tileR.roundOut(&iTileR);
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +00001265 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comd7ca6612013-08-20 12:09:32 +00001266 SkIntToScalar(iTileR.fTop));
1267
1268 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel()) {
1269 SkIRect iClampRect;
1270
1271 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1272 // In bleed mode we want to always expand the tile on all edges
1273 // but stay within the bitmap bounds
1274 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1275 } else {
1276 // In texture-domain/clamp mode we only want to expand the
1277 // tile on edges interior to "srcRect" (i.e., we want to
1278 // not bleed across the original clamped edges)
1279 srcRect.roundOut(&iClampRect);
1280 }
1281
1282 clamped_unit_outset_with_offset(&iTileR, &offset, iClampRect);
1283 }
1284
robertphillips@google.combac6b052012-09-28 18:06:49 +00001285 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.comd7ca6612013-08-20 12:09:32 +00001287 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.org15455b22013-10-21 14:48:17 +00001288 SkMatrix tmpM;
1289 tmpM.setTranslate(offset.fX, offset.fY);
1290 GrContext::AutoMatrix am;
1291 am.setPreConcat(fContext, tmpM);
1292 this->internalDrawBitmap(tmpB, tileR, params, paint, flags);
reed@google.comac10a2d2010-12-22 21:39:39 +00001293 }
1294 }
1295 }
1296}
1297
skia.committer@gmail.com956b3102013-07-26 07:00:58 +00001298static bool has_aligned_samples(const SkRect& srcRect,
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001299 const SkRect& transformedRect) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001300 // detect pixel disalignment
1301 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1302 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001303 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001304 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1305 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1306 COLOR_BLEED_TOLERANCE &&
1307 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1308 COLOR_BLEED_TOLERANCE) {
1309 return true;
1310 }
1311 return false;
1312}
1313
skia.committer@gmail.com956b3102013-07-26 07:00:58 +00001314static bool may_color_bleed(const SkRect& srcRect,
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001315 const SkRect& transformedRect,
1316 const SkMatrix& m) {
1317 // Only gets called if has_aligned_samples returned false.
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001318 // So we can assume that sampling is axis aligned but not texel aligned.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001319 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001320 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001321 outerTransformedRect(transformedRect);
1322 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1323 m.mapRect(&innerTransformedRect, innerSrcRect);
1324
1325 // The gap between outerTransformedRect and innerTransformedRect
1326 // represents the projection of the source border area, which is
1327 // problematic for color bleeding. We must check whether any
1328 // destination pixels sample the border area.
1329 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1330 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1331 SkIRect outer, inner;
1332 outerTransformedRect.round(&outer);
1333 innerTransformedRect.round(&inner);
1334 // If the inner and outer rects round to the same result, it means the
1335 // border does not overlap any pixel centers. Yay!
1336 return inner != outer;
1337}
1338
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001339
reed@google.comac10a2d2010-12-22 21:39:39 +00001340/*
1341 * This is called by drawBitmap(), which has to handle images that may be too
1342 * large to be represented by a single texture.
1343 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001344 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1345 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001347void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001348 const SkRect& srcRect,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001349 const GrTextureParams& params,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001350 const SkPaint& paint,
1351 SkCanvas::DrawBitmapRectFlags flags) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001352 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1353 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001354
reed@google.comac10a2d2010-12-22 21:39:39 +00001355 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001356 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001357 if (NULL == texture) {
1358 return;
1359 }
1360
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001361 SkRect dstRect(srcRect);
1362 SkRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001363 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1364 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1365 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1366 SkScalarMul(srcRect.fTop, hInv),
1367 SkScalarMul(srcRect.fRight, wInv),
1368 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001369
rmistry@google.comd6176b02012-08-23 18:14:13 +00001370 bool needsTextureDomain = false;
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001371 if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) &&
1372 params.filterMode() != GrTextureParams::kNone_FilterMode) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001373 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001374 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001375 srcRect.height() < bitmap.height();
commit-bot@chromium.org15455b22013-10-21 14:48:17 +00001376 if (needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
1377 const SkMatrix& matrix = fContext->getMatrix();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001378 // sampling is axis-aligned
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001379 SkRect transformedRect;
commit-bot@chromium.org15455b22013-10-21 14:48:17 +00001380 matrix.mapRect(&transformedRect, srcRect);
1381
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001382 if (has_aligned_samples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001383 // We could also turn off filtering here (but we already did a cache lookup with
1384 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001385 needsTextureDomain = false;
1386 } else {
commit-bot@chromium.org15455b22013-10-21 14:48:17 +00001387 needsTextureDomain = may_color_bleed(srcRect, transformedRect, matrix);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001388 }
1389 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001390 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001391
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001392 SkRect textureDomain = SkRect::MakeEmpty();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001393 SkAutoTUnref<GrEffectRef> effect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001394 if (needsTextureDomain) {
1395 // Use a constrained texture domain to avoid color bleeding
bsalomon@google.com81712882012-11-01 17:12:34 +00001396 SkScalar left, top, right, bottom;
1397 if (srcRect.width() > SK_Scalar1) {
1398 SkScalar border = SK_ScalarHalf / bitmap.width();
junov@google.com6acc9b32011-05-16 18:32:07 +00001399 left = paintRect.left() + border;
1400 right = paintRect.right() - border;
1401 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001402 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
junov@google.com6acc9b32011-05-16 18:32:07 +00001403 }
bsalomon@google.com81712882012-11-01 17:12:34 +00001404 if (srcRect.height() > SK_Scalar1) {
1405 SkScalar border = SK_ScalarHalf / bitmap.height();
junov@google.com6acc9b32011-05-16 18:32:07 +00001406 top = paintRect.top() + border;
1407 bottom = paintRect.bottom() - border;
1408 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001409 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
junov@google.com6acc9b32011-05-16 18:32:07 +00001410 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001411 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001412 effect.reset(GrTextureDomainEffect::Create(texture,
1413 SkMatrix::I(),
1414 textureDomain,
1415 GrTextureDomainEffect::kClamp_WrapMode,
humper@google.comb86add12013-07-25 18:49:07 +00001416 params.filterMode()));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001417 } else {
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001418 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
junov@google.com6acc9b32011-05-16 18:32:07 +00001419 }
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001420
1421 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1422 // the rest from the SkPaint.
1423 GrPaint grPaint;
1424 grPaint.addColorEffect(effect);
1425 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1426 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1427 return;
1428 }
1429
commit-bot@chromium.org15455b22013-10-21 14:48:17 +00001430 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00001431}
1432
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001433static bool filter_texture(SkBaseDevice* device, GrContext* context,
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001434 GrTexture* texture, SkImageFilter* filter,
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001435 int w, int h, const SkMatrix& ctm, SkBitmap* result,
1436 SkIPoint* offset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001437 SkASSERT(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001438 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001439
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001440 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org985fa792012-10-24 15:14:26 +00001441 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1442 // filter. Also set the clip wide open and the matrix to identity.
1443 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001444 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctm, result, offset);
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001445 } else {
1446 return false;
reed@google.com8926b162012-03-23 15:36:36 +00001447 }
reed@google.com8926b162012-03-23 15:36:36 +00001448}
1449
reed@google.comac10a2d2010-12-22 21:39:39 +00001450void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001451 int left, int top, const SkPaint& paint) {
1452 // drawSprite is defined to be in device coords.
1453 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001454
reed@google.com8926b162012-03-23 15:36:36 +00001455 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001456 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1457 return;
1458 }
1459
reed@google.com76dd2772012-01-05 21:15:07 +00001460 int w = bitmap.width();
1461 int h = bitmap.height();
1462
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001463 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001464 // draw sprite uses the default texture params
1465 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001466
reed@google.com8926b162012-03-23 15:36:36 +00001467 SkImageFilter* filter = paint.getImageFilter();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +00001468 SkIPoint offset = SkIPoint::Make(left, top);
commit-bot@chromium.org97e49e92013-07-18 19:21:44 +00001469 // This bitmap will own the filtered result as a texture.
1470 SkBitmap filteredBitmap;
1471
reed@google.com8926b162012-03-23 15:36:36 +00001472 if (NULL != filter) {
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +00001473 SkMatrix matrix(*draw.fMatrix);
1474 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
1475 if (filter_texture(this, fContext, texture, filter, w, h, matrix, &filteredBitmap,
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001476 &offset)) {
commit-bot@chromium.org97e49e92013-07-18 19:21:44 +00001477 texture = (GrTexture*) filteredBitmap.getTexture();
1478 w = filteredBitmap.width();
1479 h = filteredBitmap.height();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +00001480 } else {
1481 return;
reed@google.com8926b162012-03-23 15:36:36 +00001482 }
reed@google.com76dd2772012-01-05 21:15:07 +00001483 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001484
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001485 GrPaint grPaint;
1486 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1487
1488 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1489 return;
1490 }
1491
bsalomon@google.com5782d712011-01-21 21:03:59 +00001492 fContext->drawRectToRect(grPaint,
robertphillips@google.com58c856a2013-07-24 13:18:06 +00001493 SkRect::MakeXYWH(SkIntToScalar(offset.fX),
1494 SkIntToScalar(offset.fY),
senorblanco@chromium.org194d7752013-07-24 22:19:24 +00001495 SkIntToScalar(w),
1496 SkIntToScalar(h)),
1497 SkRect::MakeXYWH(0,
1498 0,
robertphillips@google.com31a40ef2013-07-11 00:01:39 +00001499 SK_Scalar1 * w / texture->width(),
1500 SK_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001501}
1502
reed@google.com33535f32012-09-25 15:37:50 +00001503void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1504 const SkRect* src, const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001505 const SkPaint& paint,
1506 SkCanvas::DrawBitmapRectFlags flags) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001507 SkMatrix matrix;
1508 SkRect bitmapBounds, tmpSrc;
1509
1510 bitmapBounds.set(0, 0,
1511 SkIntToScalar(bitmap.width()),
1512 SkIntToScalar(bitmap.height()));
1513
reed@google.com33535f32012-09-25 15:37:50 +00001514 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001515 if (NULL != src) {
1516 tmpSrc = *src;
1517 } else {
1518 tmpSrc = bitmapBounds;
1519 }
1520 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1521
1522 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1523 if (NULL != src) {
1524 if (!bitmapBounds.contains(tmpSrc)) {
1525 if (!tmpSrc.intersect(bitmapBounds)) {
1526 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001527 }
reed@google.com33535f32012-09-25 15:37:50 +00001528 }
reed@google.com33535f32012-09-25 15:37:50 +00001529 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001530
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001531 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint, flags);
reed@google.com33535f32012-09-25 15:37:50 +00001532}
1533
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001534void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001535 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001536 // clear of the source device must occur before CHECK_SHOULD_DRAW
1537 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1538 if (dev->fNeedClear) {
1539 // TODO: could check here whether we really need to draw at all
1540 dev->clear(0x0);
1541 }
1542
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001543 // drawDevice is defined to be in device coords.
1544 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001545
robertphillips@google.com07f81a52013-09-17 12:26:23 +00001546 GrRenderTarget* devRT = dev->accessRenderTarget();
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001547 GrTexture* devTex;
1548 if (NULL == (devTex = devRT->asTexture())) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001549 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001550 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001551
senorblanco@chromium.org514b9222013-01-18 21:53:12 +00001552 const SkBitmap& bm = dev->accessBitmap(false);
1553 int w = bm.width();
1554 int h = bm.height();
1555
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001556 SkImageFilter* filter = paint.getImageFilter();
commit-bot@chromium.org97e49e92013-07-18 19:21:44 +00001557 // This bitmap will own the filtered result as a texture.
1558 SkBitmap filteredBitmap;
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001559
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001560 if (NULL != filter) {
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001561 SkIPoint offset = SkIPoint::Make(0, 0);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +00001562 SkMatrix matrix(*draw.fMatrix);
1563 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
1564 if (filter_texture(this, fContext, devTex, filter, w, h, matrix, &filteredBitmap,
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001565 &offset)) {
commit-bot@chromium.org97e49e92013-07-18 19:21:44 +00001566 devTex = filteredBitmap.getTexture();
1567 w = filteredBitmap.width();
1568 h = filteredBitmap.height();
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001569 x += offset.fX;
1570 y += offset.fY;
senorblanco@chromium.org194d7752013-07-24 22:19:24 +00001571 } else {
1572 return;
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001573 }
1574 }
1575
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001576 GrPaint grPaint;
1577 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1578
1579 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1580 return;
1581 }
1582
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001583 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
bsalomon@google.com81712882012-11-01 17:12:34 +00001584 SkIntToScalar(y),
1585 SkIntToScalar(w),
1586 SkIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001587
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001588 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1589 // scratch texture).
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001590 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
bsalomon@google.com81712882012-11-01 17:12:34 +00001591 SK_Scalar1 * h / devTex->height());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001592
1593 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001594}
1595
reed@google.com8926b162012-03-23 15:36:36 +00001596bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
senorblanco@chromium.orgd043cce2013-04-08 19:43:22 +00001597 return filter->canFilterImageGPU();
reed@google.com8926b162012-03-23 15:36:36 +00001598}
1599
1600bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1601 const SkMatrix& ctm,
1602 SkBitmap* result, SkIPoint* offset) {
1603 // want explicitly our impl, so guard against a subclass of us overriding it
1604 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001605 return false;
1606 }
reed@google.com8926b162012-03-23 15:36:36 +00001607
1608 SkAutoLockPixels alp(src, !src.getTexture());
1609 if (!src.getTexture() && !src.readyToDraw()) {
1610 return false;
1611 }
1612
reed@google.com8926b162012-03-23 15:36:36 +00001613 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001614 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1615 // must be pushed upstack.
1616 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001617
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001618 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctm, result,
1619 offset);
reed@google.com76dd2772012-01-05 21:15:07 +00001620}
1621
reed@google.comac10a2d2010-12-22 21:39:39 +00001622///////////////////////////////////////////////////////////////////////////////
1623
1624// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001625static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001626 kTriangles_GrPrimitiveType,
1627 kTriangleStrip_GrPrimitiveType,
1628 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001629};
1630
1631void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1632 int vertexCount, const SkPoint vertices[],
1633 const SkPoint texs[], const SkColor colors[],
1634 SkXfermode* xmode,
1635 const uint16_t indices[], int indexCount,
1636 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001637 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001638
bsalomon@google.com5782d712011-01-21 21:03:59 +00001639 GrPaint grPaint;
bsalomon@google.com5782d712011-01-21 21:03:59 +00001640 // we ignore the shader if texs is null.
1641 if (NULL == texs) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001642 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001643 return;
1644 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001645 } else {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001646 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001647 return;
1648 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001649 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001650
1651 if (NULL != xmode && NULL != texs && NULL != colors) {
reed@google.com8d3cd7a2013-01-30 21:36:11 +00001652 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001653 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1654#if 0
1655 return
1656#endif
1657 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001658 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001659
bsalomon@google.com498776a2011-08-16 19:20:44 +00001660 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1661 if (NULL != colors) {
1662 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001663 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001664 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001665 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001666 }
1667 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001668 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001669 fContext->drawVertices(grPaint,
1670 gVertexMode2PrimitiveType[vmode],
1671 vertexCount,
1672 (GrPoint*) vertices,
1673 (GrPoint*) texs,
1674 colors,
1675 indices,
1676 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001677}
1678
1679///////////////////////////////////////////////////////////////////////////////
1680
1681static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001682 GrFontScaler* scaler = (GrFontScaler*)data;
1683 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001684}
1685
1686static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1687 void* auxData;
1688 GrFontScaler* scaler = NULL;
1689 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1690 scaler = (GrFontScaler*)auxData;
1691 }
1692 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001693 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001694 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1695 }
1696 return scaler;
1697}
1698
1699static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1700 SkFixed fx, SkFixed fy,
1701 const SkGlyph& glyph) {
1702 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1703
bungeman@google.com15865a72012-01-11 16:28:04 +00001704 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001705
1706 if (NULL == procs->fFontScaler) {
1707 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1708 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001709
bungeman@google.com15865a72012-01-11 16:28:04 +00001710 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1711 glyph.getSubXFixed(),
1712 glyph.getSubYFixed()),
1713 SkFixedFloorToFixed(fx),
1714 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001715 procs->fFontScaler);
1716}
1717
bsalomon@google.com5782d712011-01-21 21:03:59 +00001718SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001719
1720 // deferred allocation
1721 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001722 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001723 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1724 fDrawProcs->fContext = fContext;
1725 }
1726
1727 // init our (and GL's) state
1728 fDrawProcs->fTextContext = context;
1729 fDrawProcs->fFontScaler = NULL;
1730 return fDrawProcs;
1731}
1732
1733void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1734 size_t byteLength, SkScalar x, SkScalar y,
1735 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001736 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001737
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001738 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001739 // this guy will just call our drawPath()
1740 draw.drawText((const char*)text, byteLength, x, y, paint);
1741 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001742 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001743
1744 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001745 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001746 return;
1747 }
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +00001748 GrBitmapTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001749 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001750 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1751 }
1752}
1753
1754void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1755 size_t byteLength, const SkScalar pos[],
1756 SkScalar constY, int scalarsPerPos,
1757 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001758 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001759
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001760 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001761 // this guy will just call our drawPath()
1762 draw.drawPosText((const char*)text, byteLength, pos, constY,
1763 scalarsPerPos, paint);
1764 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001765 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001766
1767 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001768 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001769 return;
1770 }
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +00001771 GrBitmapTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001772 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001773 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1774 scalarsPerPos, paint);
1775 }
1776}
1777
1778void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1779 size_t len, const SkPath& path,
1780 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001781 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001782
1783 SkASSERT(draw.fDevice == this);
1784 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1785}
1786
1787///////////////////////////////////////////////////////////////////////////////
1788
reed@google.comf67e4cf2011-03-15 20:56:58 +00001789bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1790 if (!paint.isLCDRenderText()) {
1791 // we're cool with the paint as is
1792 return false;
1793 }
1794
1795 if (paint.getShader() ||
1796 paint.getXfermode() || // unless its srcover
1797 paint.getMaskFilter() ||
1798 paint.getRasterizer() ||
1799 paint.getColorFilter() ||
1800 paint.getPathEffect() ||
1801 paint.isFakeBoldText() ||
1802 paint.getStyle() != SkPaint::kFill_Style) {
1803 // turn off lcd
1804 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1805 flags->fHinting = paint.getHinting();
1806 return true;
1807 }
1808 // we're cool with the paint as is
1809 return false;
1810}
1811
reed@google.com75d939b2011-12-07 15:07:23 +00001812void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001813 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001814 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001815}
1816
reed@google.comf67e4cf2011-03-15 20:56:58 +00001817///////////////////////////////////////////////////////////////////////////////
1818
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001819SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1820 int width, int height,
1821 bool isOpaque,
1822 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001823 GrTextureDesc desc;
1824 desc.fConfig = fRenderTarget->config();
1825 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1826 desc.fWidth = width;
1827 desc.fHeight = height;
1828 desc.fSampleCnt = fRenderTarget->numSamples();
1829
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001830 SkAutoTUnref<GrTexture> texture;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001831 // Skia's convention is to only clear a device if it is non-opaque.
1832 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001833
1834#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1835 // layers are never draw in repeat modes, so we can request an approx
1836 // match and ignore any padding.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001837 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1838 GrContext::kApprox_ScratchTexMatch :
1839 GrContext::kExact_ScratchTexMatch;
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001840 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001841#else
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001842 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001843#endif
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001844 if (NULL != texture.get()) {
1845 return SkNEW_ARGS(SkGpuDevice,(fContext, texture, needClear));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001846 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001847 GrPrintf("---- failed to create compatible device texture [%d %d]\n", width, height);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001848 return NULL;
1849 }
1850}
1851
1852SkGpuDevice::SkGpuDevice(GrContext* context,
1853 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001854 bool needClear)
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001855 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001856
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001857 SkASSERT(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001858 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1859 // cache. We pass true for the third argument so that it will get unlocked.
1860 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001861 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001862}