blob: db440c149af74b8d4d137d908eaeb4988177f22c [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;
senorblanco@chromium.orgfbcd4152013-10-23 14:03:22 +0000439 if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000440 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 }
Scroggod757df22011-05-16 13:11:16 +0000479 }
Scroggo97c88c22011-05-11 14:05:25 +0000480 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000481
bsalomon@google.com5782d712011-01-21 21:03:59 +0000482 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000483}
484
bsalomon@google.com84405e02012-03-05 19:57:21 +0000485// This function is similar to skPaint2GrPaintNoShader but also converts
bsalomon@google.com08283af2012-10-26 13:01:20 +0000486// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
bsalomon@google.com84405e02012-03-05 19:57:21 +0000487// be used is set on grPaint and returned in param act. constantColor has the
488// same meaning as in skPaint2GrPaintNoShader.
489inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
490 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000491 bool constantColor,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000492 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000493 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 if (NULL == shader) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000495 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000496 }
497
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000498 // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state
499 // Also require shader to set the render target .
500 GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL);
501 GrContext::AutoRenderTarget(dev->context(), NULL);
502
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000503 // setup the shader as the first color effect on the paint
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000504 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000505 if (NULL != effect.get()) {
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000506 grPaint->addColorEffect(effect);
507 // Now setup the rest of the paint.
508 return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
509 } else {
510 // We still don't have SkColorShader::asNewEffect() implemented.
511 SkShader::GradientInfo info;
512 SkColor color;
rileya@google.com91f319c2012-07-25 17:18:31 +0000513
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000514 info.fColors = &color;
515 info.fColorOffsets = NULL;
516 info.fColorCount = 1;
517 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
518 SkPaint copy(skPaint);
519 copy.setShader(NULL);
520 // modulate the paint alpha by the shader's solid color alpha
521 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
522 copy.setColor(SkColorSetA(color, newA));
523 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
524 } else {
525 return false;
526 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000527 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000528}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000529}
reed@google.comac10a2d2010-12-22 21:39:39 +0000530
531///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com07f81a52013-09-17 12:26:23 +0000532void SkGpuDevice::getGlobalBounds(SkIRect* bounds) const {
533 if (NULL != bounds) {
534 const SkIPoint& origin = this->getOrigin();
535 bounds->setXYWH(origin.x(), origin.y(),
536 this->width(), this->height());
537 }
538}
539
540SkBitmap::Config SkGpuDevice::config() const {
541 if (NULL == fRenderTarget) {
542 return SkBitmap::kNo_Config;
543 }
544
545 bool isOpaque;
546 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
547}
548
bsalomon@google.com398109c2011-04-14 18:40:27 +0000549void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com32cf29e2013-01-25 15:03:18 +0000550 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
551 fContext->clear(&rect, SkColor2GrColor(color), fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000552 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000553}
554
reed@google.comac10a2d2010-12-22 21:39:39 +0000555void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000556 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000557
bsalomon@google.com5782d712011-01-21 21:03:59 +0000558 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000559 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000560 return;
561 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000562
563 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000564}
565
566// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000567static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000568 kPoints_GrPrimitiveType,
569 kLines_GrPrimitiveType,
570 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000571};
572
573void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000574 size_t count, const SkPoint pts[], const SkPaint& paint) {
epoger@google.comb58772f2013-03-08 09:09:10 +0000575 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000576 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000577
578 SkScalar width = paint.getStrokeWidth();
579 if (width < 0) {
580 return;
581 }
582
djsollen@google.com24202c72013-08-01 18:36:08 +0000583 // we only handle hairlines and paints without path effects or mask filters,
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000584 // else we let the SkDraw call our drawPath()
djsollen@google.com24202c72013-08-01 18:36:08 +0000585 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000586 draw.drawPoints(mode, count, pts, paint, true);
587 return;
588 }
589
bsalomon@google.com5782d712011-01-21 21:03:59 +0000590 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000591 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000592 return;
593 }
594
bsalomon@google.com5782d712011-01-21 21:03:59 +0000595 fContext->drawVertices(grPaint,
596 gPointMode2PrimtiveType[mode],
597 count,
598 (GrPoint*)pts,
599 NULL,
600 NULL,
601 NULL,
602 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000603}
604
reed@google.comc9aa5872011-04-05 21:05:37 +0000605///////////////////////////////////////////////////////////////////////////////
606
reed@google.comac10a2d2010-12-22 21:39:39 +0000607void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000608 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000609 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000610 CHECK_SHOULD_DRAW(draw, false);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000611
bungeman@google.com79bd8772011-07-18 15:34:08 +0000612 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000613 SkScalar width = paint.getStrokeWidth();
614
615 /*
616 We have special code for hairline strokes, miter-strokes, and fills.
617 Anything else we just call our path code.
618 */
619 bool usePath = doStroke && width > 0 &&
620 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000621 // another two reasons we might need to call drawPath...
622 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000623 usePath = true;
624 }
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000625 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000626#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000627 if (doStroke) {
628#endif
629 usePath = true;
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000630#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000631 } else {
632 usePath = !fContext->getMatrix().preservesRightAngles();
633 }
634#endif
reed@google.com67db6642011-05-26 11:46:35 +0000635 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000636 // small miter limit means right angles show bevel...
637 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
638 paint.getStrokeMiter() < SK_ScalarSqrt2)
639 {
640 usePath = true;
641 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000642 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000643 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
644 usePath = true;
645 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000646
647 if (usePath) {
648 SkPath path;
649 path.addRect(rect);
650 this->drawPath(draw, path, paint, NULL, true);
651 return;
652 }
653
654 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000655 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000656 return;
657 }
reed@google.com20efde72011-05-09 17:00:02 +0000658 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000659}
660
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000661///////////////////////////////////////////////////////////////////////////////
662
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000663void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
664 const SkPaint& paint) {
665 CHECK_FOR_NODRAW_ANNOTATION(paint);
666 CHECK_SHOULD_DRAW(draw, false);
667
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000668 bool usePath = !rect.isSimple();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000669 // another two reasons we might need to call drawPath...
670 if (paint.getMaskFilter() || paint.getPathEffect()) {
671 usePath = true;
672 }
673 // until we can rotate rrects...
674 if (!usePath && !fContext->getMatrix().rectStaysRect()) {
675 usePath = true;
676 }
677
678 if (usePath) {
679 SkPath path;
680 path.addRRect(rect);
681 this->drawPath(draw, path, paint, NULL, true);
682 return;
683 }
684
685 GrPaint grPaint;
686 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
687 return;
688 }
689
690 SkStrokeRec stroke(paint);
691 fContext->drawRRect(grPaint, rect, stroke);
692}
693
694///////////////////////////////////////////////////////////////////////////////
695
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000696void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
697 const SkPaint& paint) {
698 CHECK_FOR_NODRAW_ANNOTATION(paint);
699 CHECK_SHOULD_DRAW(draw, false);
700
701 bool usePath = false;
702 // some basic reasons we might need to call drawPath...
703 if (paint.getMaskFilter() || paint.getPathEffect()) {
704 usePath = true;
705 }
706
707 if (usePath) {
708 SkPath path;
709 path.addOval(oval);
710 this->drawPath(draw, path, paint, NULL, true);
711 return;
712 }
713
714 GrPaint grPaint;
715 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
716 return;
717 }
718 SkStrokeRec stroke(paint);
719
720 fContext->drawOval(grPaint, oval, stroke);
721}
722
reed@google.com69302852011-02-16 18:08:07 +0000723#include "SkMaskFilter.h"
724#include "SkBounder.h"
725
bsalomon@google.com85003222012-03-28 14:44:37 +0000726///////////////////////////////////////////////////////////////////////////////
727
728// helpers for applying mask filters
729namespace {
730
robertphillips@google.com49149312013-07-03 15:34:35 +0000731// Draw a mask using the supplied paint. Since the coverage/geometry
732// is already burnt into the mask this boils down to a rect draw.
733// Return true if the mask was successfully drawn.
734bool draw_mask(GrContext* context, const SkRect& maskRect,
735 GrPaint* grp, GrTexture* mask) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000736 GrContext::AutoMatrix am;
737 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000738 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000739 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000740
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000741 SkMatrix matrix;
robertphillips@google.com49149312013-07-03 15:34:35 +0000742 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
743 matrix.postIDiv(mask->width(), mask->height());
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000744
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000745 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
robertphillips@google.com49149312013-07-03 15:34:35 +0000746 context->drawRect(*grp, maskRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000747 return true;
748}
749
robertphillips@google.com49149312013-07-03 15:34:35 +0000750bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
751 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
752 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000753 SkMask srcM, dstM;
754
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000755 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
756 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000757 return false;
758 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000759 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000760
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000761 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000762 return false;
763 }
764 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000765 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000766
767 if (clip.quickReject(dstM.fBounds)) {
768 return false;
769 }
770 if (bounder && !bounder->doIRect(dstM.fBounds)) {
771 return false;
772 }
773
774 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000775 // the current clip (and identity matrix) and GrPaint settings
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000776 GrTextureDesc desc;
777 desc.fWidth = dstM.fBounds.width();
778 desc.fHeight = dstM.fBounds.height();
779 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000780
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000781 GrAutoScratchTexture ast(context, desc);
782 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000783
reed@google.com69302852011-02-16 18:08:07 +0000784 if (NULL == texture) {
785 return false;
786 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000787 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000788 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000789
robertphillips@google.com49149312013-07-03 15:34:35 +0000790 SkRect maskRect = SkRect::MakeFromIRect(dstM.fBounds);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000791
robertphillips@google.com49149312013-07-03 15:34:35 +0000792 return draw_mask(context, maskRect, grp, texture);
793}
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000794
robertphillips@google.com49149312013-07-03 15:34:35 +0000795// Create a mask of 'devPath' and place the result in 'mask'. Return true on
796// success; false otherwise.
797bool create_mask_GPU(GrContext* context,
798 const SkRect& maskRect,
799 const SkPath& devPath,
800 const SkStrokeRec& stroke,
801 bool doAA,
802 GrAutoScratchTexture* mask) {
803 GrTextureDesc desc;
804 desc.fFlags = kRenderTarget_GrTextureFlagBit;
805 desc.fWidth = SkScalarCeilToInt(maskRect.width());
806 desc.fHeight = SkScalarCeilToInt(maskRect.height());
807 // We actually only need A8, but it often isn't supported as a
808 // render target so default to RGBA_8888
809 desc.fConfig = kRGBA_8888_GrPixelConfig;
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +0000810 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillips@google.com49149312013-07-03 15:34:35 +0000811 desc.fConfig = kAlpha_8_GrPixelConfig;
812 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000813
robertphillips@google.com49149312013-07-03 15:34:35 +0000814 mask->set(context, desc);
815 if (NULL == mask->texture()) {
816 return false;
817 }
818
819 GrTexture* maskTexture = mask->texture();
820 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
821
822 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
823 GrContext::AutoClip ac(context, clipRect);
824
825 context->clear(NULL, 0x0);
826
827 GrPaint tempPaint;
828 if (doAA) {
829 tempPaint.setAntiAlias(true);
830 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
831 // blend coeff of zero requires dual source blending support in order
832 // to properly blend partially covered pixels. This means the AA
833 // code path may not be taken. So we use a dst blend coeff of ISA. We
834 // could special case AA draws to a dst surface with known alpha=0 to
835 // use a zero dst coeff when dual source blending isn't available.
836 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
837 }
838
839 GrContext::AutoMatrix am;
840
841 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
842 SkMatrix translate;
843 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
844 am.set(context, translate);
845 context->drawPath(tempPaint, devPath, stroke);
reed@google.com69302852011-02-16 18:08:07 +0000846 return true;
847}
reed@google.com69302852011-02-16 18:08:07 +0000848
robertphillips@google.com49149312013-07-03 15:34:35 +0000849SkBitmap wrap_texture(GrTexture* texture) {
850 SkBitmap result;
851 bool dummy;
852 SkBitmap::Config config = grConfig2skConfig(texture->config(), &dummy);
853 result.setConfig(config, texture->width(), texture->height());
854 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (texture)))->unref();
855 return result;
bsalomon@google.com85003222012-03-28 14:44:37 +0000856}
857
robertphillips@google.com49149312013-07-03 15:34:35 +0000858};
bsalomon@google.com85003222012-03-28 14:44:37 +0000859
reed@google.com0c219b62011-02-16 21:31:18 +0000860void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000861 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000862 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000863 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000864 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000865
bsalomon@google.com5782d712011-01-21 21:03:59 +0000866 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000867 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000868 return;
869 }
870
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000871 // can we cheat, and treat a thin stroke as a hairline w/ coverage
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000872 // if we can, we draw lots faster (raster device does this same test)
873 SkScalar hairlineCoverage;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000874 bool doHairLine = SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage);
875 if (doHairLine) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000876 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000877 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000878
reed@google.comfe626382011-09-21 13:50:35 +0000879 // If we have a prematrix, apply it to the path, optimizing for the case
880 // where the original path can in fact be modified in place (even though
881 // its parameter type is const).
882 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000883 SkPath tmpPath, effectPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000884
885 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +0000886 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +0000887
reed@google.come3445642011-02-16 23:20:39 +0000888 if (!pathIsMutable) {
889 result = &tmpPath;
890 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000891 }
reed@google.come3445642011-02-16 23:20:39 +0000892 // should I push prePathMatrix on our MV stack temporarily, instead
893 // of applying it here? See SkDraw.cpp
894 pathPtr->transform(*prePathMatrix, result);
895 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +0000896 }
reed@google.com0c219b62011-02-16 21:31:18 +0000897 // at this point we're done with prePathMatrix
898 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +0000899
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000900 SkStrokeRec stroke(paint);
901 SkPathEffect* pathEffect = paint.getPathEffect();
reed@google.com4bbdeac2013-01-24 21:03:11 +0000902 const SkRect* cullRect = NULL; // TODO: what is our bounds?
903 if (pathEffect && pathEffect->filterPath(&effectPath, *pathPtr, &stroke,
904 cullRect)) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000905 pathPtr = &effectPath;
906 }
907
908 if (!pathEffect && doHairLine) {
909 stroke.setHairlineStyle();
reed@google.com0c219b62011-02-16 21:31:18 +0000910 }
911
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000912 if (paint.getMaskFilter()) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000913 if (!stroke.isHairlineStyle()) {
914 if (stroke.applyToPath(&tmpPath, *pathPtr)) {
915 pathPtr = &tmpPath;
robertphillips@google.com49149312013-07-03 15:34:35 +0000916 pathIsMutable = true;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000917 stroke.setFillStyle();
918 }
919 }
920
reed@google.com0c219b62011-02-16 21:31:18 +0000921 // avoid possibly allocating a new path in transform if we can
922 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
923
924 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000925 pathPtr->transform(fContext->getMatrix(), devPathPtr);
robertphillips@google.com49149312013-07-03 15:34:35 +0000926
927 SkRect maskRect;
928 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
929 draw.fClip->getBounds(),
930 fContext->getMatrix(),
931 &maskRect)) {
932 SkIRect finalIRect;
933 maskRect.roundOut(&finalIRect);
934 if (draw.fClip->quickReject(finalIRect)) {
935 // clipped out
936 return;
937 }
938 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
939 // nothing to draw
940 return;
941 }
942
943 GrAutoScratchTexture mask;
944
skia.committer@gmail.com1842adf2013-07-04 07:01:07 +0000945 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
robertphillips@google.com49149312013-07-03 15:34:35 +0000946 grPaint.isAntiAlias(), &mask)) {
947 GrTexture* filtered;
948
949 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(), maskRect, &filtered, true)) {
commit-bot@chromium.org63972172013-10-16 13:53:54 +0000950 // filterMaskGPU gives us ownership of a ref to the result
robertphillips@google.com49149312013-07-03 15:34:35 +0000951 SkAutoTUnref<GrTexture> atu(filtered);
952
commit-bot@chromium.org63972172013-10-16 13:53:54 +0000953 // If the scratch texture that we used as the filter src also holds the filter
954 // result then we must detach so that this texture isn't recycled for a later
955 // draw.
956 if (filtered == mask.texture()) {
957 mask.detach();
958 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
959 }
960
robertphillips@google.com49149312013-07-03 15:34:35 +0000961 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
962 // This path is completely drawn
963 return;
964 }
965 }
966 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000967 }
robertphillips@google.com49149312013-07-03 15:34:35 +0000968
969 // draw the mask on the CPU - this is a fallthrough path in case the
970 // GPU path fails
971 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
972 SkPaint::kFill_Style;
973 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
974 *draw.fClip, draw.fBounder, &grPaint, style);
reed@google.com69302852011-02-16 18:08:07 +0000975 return;
976 }
reed@google.com69302852011-02-16 18:08:07 +0000977
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000978 fContext->drawPath(grPaint, *pathPtr, stroke);
reed@google.comac10a2d2010-12-22 21:39:39 +0000979}
980
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000981static const int kBmpSmallTileSize = 1 << 10;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000982
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000983static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
984 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
985 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000986 return tilesX * tilesY;
987}
988
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000989static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
990 if (maxTileSize <= kBmpSmallTileSize) {
991 return maxTileSize;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000992 }
993
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000994 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
995 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000996
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000997 maxTileTotalTileSize *= maxTileSize * maxTileSize;
998 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
robertphillips@google.combac6b052012-09-28 18:06:49 +0000999
bsalomon@google.comaf562b42013-10-24 17:52:07 +00001000 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
1001 return kBmpSmallTileSize;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001002 } else {
bsalomon@google.comaf562b42013-10-24 17:52:07 +00001003 return maxTileSize;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001004 }
1005}
bsalomon@google.comaf562b42013-10-24 17:52:07 +00001006
1007// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
1008// pixels from the bitmap are necessary.
1009static void determine_clipped_src_rect(const GrContext* context,
1010 const SkBitmap& bitmap,
1011 const SkRect* srcRectPtr,
1012 SkIRect* clippedSrcIRect) {
1013 const GrClipData* clip = context->getClip();
1014 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
1015 SkMatrix inv;
1016 if (!context->getMatrix().invert(&inv)) {
1017 clippedSrcIRect->setEmpty();
1018 return;
1019 }
1020 SkRect clippedSrcRect = SkRect::MakeFromIRect(*clippedSrcIRect);
1021 inv.mapRect(&clippedSrcRect);
1022 if (NULL != srcRectPtr) {
1023 if (!clippedSrcRect.intersect(*srcRectPtr)) {
1024 clippedSrcIRect->setEmpty();
1025 return;
1026 }
1027 }
1028 clippedSrcRect.roundOut(clippedSrcIRect);
1029 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1030 if (!clippedSrcIRect->intersect(bmpBounds)) {
1031 clippedSrcIRect->setEmpty();
1032 }
bsalomon@google.comfb309512011-11-30 14:13:48 +00001033}
1034
bsalomon@google.comaf562b42013-10-24 17:52:07 +00001035
bsalomon@google.comfb309512011-11-30 14:13:48 +00001036bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001037 const GrTextureParams& params,
bsalomon@google.comaf562b42013-10-24 17:52:07 +00001038 const SkRect* srcRectPtr,
1039 int maxTileSize,
1040 int* tileSize) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001041 // if bitmap is explictly texture backed then just use the texture
1042 if (NULL != bitmap.getTexture()) {
1043 return false;
1044 }
bsalomon@google.comaf562b42013-10-24 17:52:07 +00001045
1046 SkIRect clippedSrcRect;
1047
1048 // if it's larger than the max tile size, then we have no choice but tiling.
1049 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
1050 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, &clippedSrcRect);
1051 *tileSize = determine_tile_size(bitmap, clippedSrcRect, maxTileSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001052 return true;
1053 }
bsalomon@google.comaf562b42013-10-24 17:52:07 +00001054
1055 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001056 return false;
1057 }
bsalomon@google.comaf562b42013-10-24 17:52:07 +00001058
bsalomon@google.comfb309512011-11-30 14:13:48 +00001059 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001060 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001061 return false;
1062 }
1063
1064 // At this point we know we could do the draw by uploading the entire bitmap
1065 // as a texture. However, if the texture would be large compared to the
1066 // cache size and we don't require most of it for this draw then tile to
1067 // reduce the amount of upload and cache spill.
1068
1069 // assumption here is that sw bitmap size is a good proxy for its size as
1070 // a texture
1071 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001072 size_t cacheSize;
1073 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001074 if (bmpSize < cacheSize / 2) {
1075 return false;
1076 }
1077
bsalomon@google.comaf562b42013-10-24 17:52:07 +00001078 // Figure out how much of the src we will need based on the src rect and clipping.
1079 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, &clippedSrcRect);
1080 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1081 size_t usedTileBytes = get_tile_count(clippedSrcRect, kBmpSmallTileSize) *
1082 kBmpSmallTileSize * kBmpSmallTileSize;
1083
1084 return usedTileBytes < 2 * bmpSize;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001085}
1086
reed@google.comac10a2d2010-12-22 21:39:39 +00001087void SkGpuDevice::drawBitmap(const SkDraw& draw,
1088 const SkBitmap& bitmap,
reed@google.comac10a2d2010-12-22 21:39:39 +00001089 const SkMatrix& m,
1090 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001091 // We cannot call drawBitmapRect here since 'm' could be anything
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001092 this->drawBitmapCommon(draw, bitmap, NULL, m, paint,
robertphillips@google.com31acc112013-08-20 12:13:48 +00001093 SkCanvas::kNone_DrawBitmapRectFlag);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001094}
1095
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001096// This method outsets 'iRect' by 1 all around and then clamps its extents to
1097// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1098// of 'iRect' for all possible outsets/clamps.
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +00001099static inline void clamped_unit_outset_with_offset(SkIRect* iRect, SkPoint* offset,
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001100 const SkIRect& clamp) {
1101 iRect->outset(1, 1);
1102
1103 if (iRect->fLeft < clamp.fLeft) {
1104 iRect->fLeft = clamp.fLeft;
1105 } else {
1106 offset->fX -= SK_Scalar1;
1107 }
1108 if (iRect->fTop < clamp.fTop) {
1109 iRect->fTop = clamp.fTop;
1110 } else {
1111 offset->fY -= SK_Scalar1;
1112 }
1113
1114 if (iRect->fRight > clamp.fRight) {
1115 iRect->fRight = clamp.fRight;
1116 }
1117 if (iRect->fBottom > clamp.fBottom) {
1118 iRect->fBottom = clamp.fBottom;
1119 }
1120}
1121
robertphillips@google.combac6b052012-09-28 18:06:49 +00001122void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1123 const SkBitmap& bitmap,
1124 const SkRect* srcRectPtr,
1125 const SkMatrix& m,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001126 const SkPaint& paint,
1127 SkCanvas::DrawBitmapRectFlags flags) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001128 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001129
robertphillips@google.combac6b052012-09-28 18:06:49 +00001130 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001131 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001132 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001133 } else {
1134 srcRect = *srcRectPtr;
1135 }
1136
junov@google.comd935cfb2011-06-27 20:48:23 +00001137 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001138 // Convert the bitmap to a shader so that the rect can be drawn
1139 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001140 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001141 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001142 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001143 if (NULL != srcRectPtr) {
1144 SkIRect iSrc;
1145 srcRect.roundOut(&iSrc);
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001146
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +00001147 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001148 SkIntToScalar(iSrc.fTop));
1149
1150 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1151 // In bleed mode we want to expand the src rect on all sides
1152 // but stay within the bitmap bounds
1153 SkIRect iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1154 clamped_unit_outset_with_offset(&iSrc, &offset, iClampRect);
1155 }
1156
robertphillips@google.combac6b052012-09-28 18:06:49 +00001157 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001158 return; // extraction failed
1159 }
1160 bitmapPtr = &tmp;
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001161 srcRect.offset(-offset.fX, -offset.fY);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001162 // The source rect has changed so update the matrix
robertphillips@google.com2cc0b472013-08-20 16:51:20 +00001163 newM.preTranslate(offset.fX, offset.fY);
junov@google.comd935cfb2011-06-27 20:48:23 +00001164 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001165
junov@google.comd935cfb2011-06-27 20:48:23 +00001166 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001167 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001168 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001169
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001170 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001171 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001172 // also affect the behavior of the mask filter.
1173 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001174 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001175 SkDraw transformedDraw(draw);
1176 transformedDraw.fMatrix = &drawMatrix;
1177
robertphillips@google.combac6b052012-09-28 18:06:49 +00001178 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001179
junov@google.comd935cfb2011-06-27 20:48:23 +00001180 return;
1181 }
1182
commit-bot@chromium.orgbbfe4542013-10-24 01:46:11 +00001183 fContext->concatMatrix(m);
1184
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001185 GrTextureParams params;
humper@google.comb86add12013-07-25 18:49:07 +00001186 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1187 GrTextureParams::FilterMode textureFilterMode;
1188 switch(paintFilterLevel) {
1189 case SkPaint::kNone_FilterLevel:
1190 textureFilterMode = GrTextureParams::kNone_FilterMode;
1191 break;
1192 case SkPaint::kLow_FilterLevel:
1193 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1194 break;
1195 case SkPaint::kMedium_FilterLevel:
1196 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1197 break;
1198 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgc5e57bd2013-09-19 22:11:38 +00001199 // Fall back to mips for now
humper@google.comb86add12013-07-25 18:49:07 +00001200 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1201 break;
humper@google.comf9ed6fe2013-07-25 19:09:47 +00001202 default:
1203 SkErrorInternals::SetError( kInvalidPaint_SkError,
1204 "Sorry, I don't understand the filtering "
1205 "mode you asked for. Falling back to "
1206 "MIPMaps.");
1207 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1208 break;
skia.committer@gmail.com956b3102013-07-26 07:00:58 +00001209
humper@google.comb86add12013-07-25 18:49:07 +00001210 }
skia.committer@gmail.com956b3102013-07-26 07:00:58 +00001211
humper@google.comb86add12013-07-25 18:49:07 +00001212 params.setFilterMode(textureFilterMode);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001213
bsalomon@google.comaf562b42013-10-24 17:52:07 +00001214 int maxTileSize = fContext->getMaxTextureSize();
1215 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel()) {
1216 // We may need a skosh more room if we have to bump out the tile
1217 // by 1 pixel all around
1218 maxTileSize -= 2;
1219 }
1220 int tileSize;
1221
1222 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize)) {
1223 this->drawTiledBitmap(bitmap, srcRect, params, paint, flags, tileSize);
1224 } else {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001225 // take the simple case
commit-bot@chromium.orgbbfe4542013-10-24 01:46:11 +00001226 this->internalDrawBitmap(bitmap, srcRect, params, paint, flags);
reed@google.comac10a2d2010-12-22 21:39:39 +00001227 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001228}
1229
1230// Break 'bitmap' into several tiles to draw it since it has already
1231// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001232void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001233 const SkRect& srcRect,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001234 const GrTextureParams& params,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001235 const SkPaint& paint,
bsalomon@google.comaf562b42013-10-24 17:52:07 +00001236 SkCanvas::DrawBitmapRectFlags flags,
1237 int tileSize) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001238
reed@google.comac10a2d2010-12-22 21:39:39 +00001239 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001240 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001241 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001242 const GrRenderTarget* rt = fContext->getRenderTarget();
1243 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1244 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1245 return;
1246 }
commit-bot@chromium.orgbbfe4542013-10-24 01:46:11 +00001247 SkMatrix inverse;
1248 if (!fContext->getMatrix().invert(&inverse)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001249 return;
1250 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001251 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001252 }
1253
bsalomon@google.comfb309512011-11-30 14:13:48 +00001254 int nx = bitmap.width() / tileSize;
1255 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 for (int x = 0; x <= nx; x++) {
1257 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001258 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001259 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001260 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001261 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001262 SkIntToScalar((y + 1) * tileSize));
1263
1264 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 continue;
1266 }
1267
robertphillips@google.combac6b052012-09-28 18:06:49 +00001268 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001269 continue;
1270 }
1271
1272 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001273 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001274 tileR.roundOut(&iTileR);
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +00001275 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comd7ca6612013-08-20 12:09:32 +00001276 SkIntToScalar(iTileR.fTop));
1277
1278 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel()) {
1279 SkIRect iClampRect;
1280
1281 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1282 // In bleed mode we want to always expand the tile on all edges
1283 // but stay within the bitmap bounds
1284 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1285 } else {
1286 // In texture-domain/clamp mode we only want to expand the
1287 // tile on edges interior to "srcRect" (i.e., we want to
1288 // not bleed across the original clamped edges)
1289 srcRect.roundOut(&iClampRect);
1290 }
1291
1292 clamped_unit_outset_with_offset(&iTileR, &offset, iClampRect);
1293 }
1294
robertphillips@google.combac6b052012-09-28 18:06:49 +00001295 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.comd7ca6612013-08-20 12:09:32 +00001297 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orgbbfe4542013-10-24 01:46:11 +00001298 SkMatrix tmpM;
1299 tmpM.setTranslate(offset.fX, offset.fY);
1300 GrContext::AutoMatrix am;
1301 am.setPreConcat(fContext, tmpM);
1302 this->internalDrawBitmap(tmpB, tileR, params, paint, flags);
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 }
1304 }
1305 }
1306}
1307
skia.committer@gmail.com956b3102013-07-26 07:00:58 +00001308static bool has_aligned_samples(const SkRect& srcRect,
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001309 const SkRect& transformedRect) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001310 // detect pixel disalignment
1311 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1312 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001313 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001314 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1315 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1316 COLOR_BLEED_TOLERANCE &&
1317 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1318 COLOR_BLEED_TOLERANCE) {
1319 return true;
1320 }
1321 return false;
1322}
1323
skia.committer@gmail.com956b3102013-07-26 07:00:58 +00001324static bool may_color_bleed(const SkRect& srcRect,
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001325 const SkRect& transformedRect,
1326 const SkMatrix& m) {
1327 // Only gets called if has_aligned_samples returned false.
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001328 // So we can assume that sampling is axis aligned but not texel aligned.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001329 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001330 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001331 outerTransformedRect(transformedRect);
1332 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1333 m.mapRect(&innerTransformedRect, innerSrcRect);
1334
1335 // The gap between outerTransformedRect and innerTransformedRect
1336 // represents the projection of the source border area, which is
1337 // problematic for color bleeding. We must check whether any
1338 // destination pixels sample the border area.
1339 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1340 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1341 SkIRect outer, inner;
1342 outerTransformedRect.round(&outer);
1343 innerTransformedRect.round(&inner);
1344 // If the inner and outer rects round to the same result, it means the
1345 // border does not overlap any pixel centers. Yay!
1346 return inner != outer;
1347}
1348
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001349
reed@google.comac10a2d2010-12-22 21:39:39 +00001350/*
1351 * This is called by drawBitmap(), which has to handle images that may be too
1352 * large to be represented by a single texture.
1353 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001354 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1355 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001356 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001357void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001358 const SkRect& srcRect,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001359 const GrTextureParams& params,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001360 const SkPaint& paint,
1361 SkCanvas::DrawBitmapRectFlags flags) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001362 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1363 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001364
reed@google.comac10a2d2010-12-22 21:39:39 +00001365 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001366 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001367 if (NULL == texture) {
1368 return;
1369 }
1370
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001371 SkRect dstRect(srcRect);
1372 SkRect paintRect;
senorblanco@chromium.org34a849d2013-10-24 15:59:31 +00001373 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1374 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
robertphillips@google.combac6b052012-09-28 18:06:49 +00001375 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1376 SkScalarMul(srcRect.fTop, hInv),
1377 SkScalarMul(srcRect.fRight, wInv),
1378 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001379
rmistry@google.comd6176b02012-08-23 18:14:13 +00001380 bool needsTextureDomain = false;
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001381 if (!(flags & SkCanvas::kBleed_DrawBitmapRectFlag) &&
1382 params.filterMode() != GrTextureParams::kNone_FilterMode) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001383 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001384 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001385 srcRect.height() < bitmap.height();
commit-bot@chromium.orgbbfe4542013-10-24 01:46:11 +00001386 if (needsTextureDomain && fContext->getMatrix().rectStaysRect()) {
1387 const SkMatrix& matrix = fContext->getMatrix();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001388 // sampling is axis-aligned
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001389 SkRect transformedRect;
commit-bot@chromium.orgbbfe4542013-10-24 01:46:11 +00001390 matrix.mapRect(&transformedRect, srcRect);
skia.committer@gmail.com583b18a2013-10-24 07:01:59 +00001391
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001392 if (has_aligned_samples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001393 // We could also turn off filtering here (but we already did a cache lookup with
1394 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001395 needsTextureDomain = false;
1396 } else {
commit-bot@chromium.orgbbfe4542013-10-24 01:46:11 +00001397 needsTextureDomain = may_color_bleed(srcRect, transformedRect, matrix);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001398 }
1399 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001400 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001401
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001402 SkRect textureDomain = SkRect::MakeEmpty();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001403 SkAutoTUnref<GrEffectRef> effect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001404 if (needsTextureDomain) {
1405 // Use a constrained texture domain to avoid color bleeding
bsalomon@google.com81712882012-11-01 17:12:34 +00001406 SkScalar left, top, right, bottom;
1407 if (srcRect.width() > SK_Scalar1) {
senorblanco@chromium.org34a849d2013-10-24 15:59:31 +00001408 SkScalar border = SK_ScalarHalf / texture->width();
junov@google.com6acc9b32011-05-16 18:32:07 +00001409 left = paintRect.left() + border;
1410 right = paintRect.right() - border;
1411 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001412 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
junov@google.com6acc9b32011-05-16 18:32:07 +00001413 }
bsalomon@google.com81712882012-11-01 17:12:34 +00001414 if (srcRect.height() > SK_Scalar1) {
senorblanco@chromium.org34a849d2013-10-24 15:59:31 +00001415 SkScalar border = SK_ScalarHalf / texture->height();
junov@google.com6acc9b32011-05-16 18:32:07 +00001416 top = paintRect.top() + border;
1417 bottom = paintRect.bottom() - border;
1418 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001419 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
junov@google.com6acc9b32011-05-16 18:32:07 +00001420 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001421 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001422 effect.reset(GrTextureDomainEffect::Create(texture,
1423 SkMatrix::I(),
1424 textureDomain,
1425 GrTextureDomainEffect::kClamp_WrapMode,
humper@google.comb86add12013-07-25 18:49:07 +00001426 params.filterMode()));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001427 } else {
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001428 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
junov@google.com6acc9b32011-05-16 18:32:07 +00001429 }
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001430
1431 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1432 // the rest from the SkPaint.
1433 GrPaint grPaint;
1434 grPaint.addColorEffect(effect);
1435 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
1436 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
1437 return;
1438 }
1439
commit-bot@chromium.orgbbfe4542013-10-24 01:46:11 +00001440 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00001441}
1442
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001443static bool filter_texture(SkBaseDevice* device, GrContext* context,
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001444 GrTexture* texture, SkImageFilter* filter,
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001445 int w, int h, const SkMatrix& ctm, SkBitmap* result,
1446 SkIPoint* offset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001447 SkASSERT(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001448 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001449
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001450 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org985fa792012-10-24 15:14:26 +00001451 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1452 // filter. Also set the clip wide open and the matrix to identity.
1453 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001454 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctm, result, offset);
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001455 } else {
1456 return false;
reed@google.com8926b162012-03-23 15:36:36 +00001457 }
reed@google.com8926b162012-03-23 15:36:36 +00001458}
1459
reed@google.comac10a2d2010-12-22 21:39:39 +00001460void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001461 int left, int top, const SkPaint& paint) {
1462 // drawSprite is defined to be in device coords.
1463 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001464
reed@google.com8926b162012-03-23 15:36:36 +00001465 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001466 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1467 return;
1468 }
1469
reed@google.com76dd2772012-01-05 21:15:07 +00001470 int w = bitmap.width();
1471 int h = bitmap.height();
1472
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001473 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001474 // draw sprite uses the default texture params
1475 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001476
reed@google.com8926b162012-03-23 15:36:36 +00001477 SkImageFilter* filter = paint.getImageFilter();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +00001478 SkIPoint offset = SkIPoint::Make(left, top);
commit-bot@chromium.org97e49e92013-07-18 19:21:44 +00001479 // This bitmap will own the filtered result as a texture.
1480 SkBitmap filteredBitmap;
1481
reed@google.com8926b162012-03-23 15:36:36 +00001482 if (NULL != filter) {
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +00001483 SkMatrix matrix(*draw.fMatrix);
1484 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
1485 if (filter_texture(this, fContext, texture, filter, w, h, matrix, &filteredBitmap,
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001486 &offset)) {
commit-bot@chromium.org97e49e92013-07-18 19:21:44 +00001487 texture = (GrTexture*) filteredBitmap.getTexture();
1488 w = filteredBitmap.width();
1489 h = filteredBitmap.height();
senorblanco@chromium.org194d7752013-07-24 22:19:24 +00001490 } else {
1491 return;
reed@google.com8926b162012-03-23 15:36:36 +00001492 }
reed@google.com76dd2772012-01-05 21:15:07 +00001493 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001494
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001495 GrPaint grPaint;
1496 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1497
1498 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1499 return;
1500 }
1501
bsalomon@google.com5782d712011-01-21 21:03:59 +00001502 fContext->drawRectToRect(grPaint,
robertphillips@google.com58c856a2013-07-24 13:18:06 +00001503 SkRect::MakeXYWH(SkIntToScalar(offset.fX),
1504 SkIntToScalar(offset.fY),
senorblanco@chromium.org194d7752013-07-24 22:19:24 +00001505 SkIntToScalar(w),
1506 SkIntToScalar(h)),
1507 SkRect::MakeXYWH(0,
1508 0,
robertphillips@google.com31a40ef2013-07-11 00:01:39 +00001509 SK_Scalar1 * w / texture->width(),
1510 SK_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001511}
1512
reed@google.com33535f32012-09-25 15:37:50 +00001513void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1514 const SkRect* src, const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001515 const SkPaint& paint,
1516 SkCanvas::DrawBitmapRectFlags flags) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001517 SkMatrix matrix;
1518 SkRect bitmapBounds, tmpSrc;
1519
1520 bitmapBounds.set(0, 0,
1521 SkIntToScalar(bitmap.width()),
1522 SkIntToScalar(bitmap.height()));
1523
reed@google.com33535f32012-09-25 15:37:50 +00001524 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001525 if (NULL != src) {
1526 tmpSrc = *src;
1527 } else {
1528 tmpSrc = bitmapBounds;
1529 }
1530 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1531
1532 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1533 if (NULL != src) {
1534 if (!bitmapBounds.contains(tmpSrc)) {
1535 if (!tmpSrc.intersect(bitmapBounds)) {
1536 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001537 }
reed@google.com33535f32012-09-25 15:37:50 +00001538 }
reed@google.com33535f32012-09-25 15:37:50 +00001539 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001540
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001541 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint, flags);
reed@google.com33535f32012-09-25 15:37:50 +00001542}
1543
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001544void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001545 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001546 // clear of the source device must occur before CHECK_SHOULD_DRAW
1547 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1548 if (dev->fNeedClear) {
1549 // TODO: could check here whether we really need to draw at all
1550 dev->clear(0x0);
1551 }
1552
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001553 // drawDevice is defined to be in device coords.
1554 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001555
robertphillips@google.com07f81a52013-09-17 12:26:23 +00001556 GrRenderTarget* devRT = dev->accessRenderTarget();
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001557 GrTexture* devTex;
1558 if (NULL == (devTex = devRT->asTexture())) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001559 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001560 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001561
senorblanco@chromium.org514b9222013-01-18 21:53:12 +00001562 const SkBitmap& bm = dev->accessBitmap(false);
1563 int w = bm.width();
1564 int h = bm.height();
1565
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001566 SkImageFilter* filter = paint.getImageFilter();
commit-bot@chromium.org97e49e92013-07-18 19:21:44 +00001567 // This bitmap will own the filtered result as a texture.
1568 SkBitmap filteredBitmap;
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001569
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001570 if (NULL != filter) {
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001571 SkIPoint offset = SkIPoint::Make(0, 0);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +00001572 SkMatrix matrix(*draw.fMatrix);
1573 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
1574 if (filter_texture(this, fContext, devTex, filter, w, h, matrix, &filteredBitmap,
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001575 &offset)) {
commit-bot@chromium.org97e49e92013-07-18 19:21:44 +00001576 devTex = filteredBitmap.getTexture();
1577 w = filteredBitmap.width();
1578 h = filteredBitmap.height();
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001579 x += offset.fX;
1580 y += offset.fY;
senorblanco@chromium.org194d7752013-07-24 22:19:24 +00001581 } else {
1582 return;
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001583 }
1584 }
1585
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001586 GrPaint grPaint;
1587 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1588
1589 if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
1590 return;
1591 }
1592
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001593 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
bsalomon@google.com81712882012-11-01 17:12:34 +00001594 SkIntToScalar(y),
1595 SkIntToScalar(w),
1596 SkIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001597
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +00001598 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1599 // scratch texture).
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001600 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
bsalomon@google.com81712882012-11-01 17:12:34 +00001601 SK_Scalar1 * h / devTex->height());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001602
1603 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001604}
1605
reed@google.com8926b162012-03-23 15:36:36 +00001606bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
senorblanco@chromium.orgd043cce2013-04-08 19:43:22 +00001607 return filter->canFilterImageGPU();
reed@google.com8926b162012-03-23 15:36:36 +00001608}
1609
1610bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1611 const SkMatrix& ctm,
1612 SkBitmap* result, SkIPoint* offset) {
1613 // want explicitly our impl, so guard against a subclass of us overriding it
1614 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001615 return false;
1616 }
reed@google.com8926b162012-03-23 15:36:36 +00001617
1618 SkAutoLockPixels alp(src, !src.getTexture());
1619 if (!src.getTexture() && !src.readyToDraw()) {
1620 return false;
1621 }
1622
reed@google.com8926b162012-03-23 15:36:36 +00001623 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001624 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1625 // must be pushed upstack.
1626 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001627
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +00001628 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctm, result,
1629 offset);
reed@google.com76dd2772012-01-05 21:15:07 +00001630}
1631
reed@google.comac10a2d2010-12-22 21:39:39 +00001632///////////////////////////////////////////////////////////////////////////////
1633
1634// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001635static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001636 kTriangles_GrPrimitiveType,
1637 kTriangleStrip_GrPrimitiveType,
1638 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001639};
1640
1641void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1642 int vertexCount, const SkPoint vertices[],
1643 const SkPoint texs[], const SkColor colors[],
1644 SkXfermode* xmode,
1645 const uint16_t indices[], int indexCount,
1646 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001647 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001648
bsalomon@google.com5782d712011-01-21 21:03:59 +00001649 GrPaint grPaint;
bsalomon@google.com5782d712011-01-21 21:03:59 +00001650 // we ignore the shader if texs is null.
1651 if (NULL == texs) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001652 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001653 return;
1654 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001655 } else {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001656 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001657 return;
1658 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001659 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001660
1661 if (NULL != xmode && NULL != texs && NULL != colors) {
reed@google.com8d3cd7a2013-01-30 21:36:11 +00001662 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001663 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1664#if 0
1665 return
1666#endif
1667 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001668 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001669
bsalomon@google.com498776a2011-08-16 19:20:44 +00001670 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1671 if (NULL != colors) {
1672 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001673 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001674 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001675 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001676 }
1677 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001678 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001679 fContext->drawVertices(grPaint,
1680 gVertexMode2PrimitiveType[vmode],
1681 vertexCount,
1682 (GrPoint*) vertices,
1683 (GrPoint*) texs,
1684 colors,
1685 indices,
1686 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001687}
1688
1689///////////////////////////////////////////////////////////////////////////////
1690
1691static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001692 GrFontScaler* scaler = (GrFontScaler*)data;
1693 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001694}
1695
1696static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1697 void* auxData;
1698 GrFontScaler* scaler = NULL;
1699 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1700 scaler = (GrFontScaler*)auxData;
1701 }
1702 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001703 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001704 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1705 }
1706 return scaler;
1707}
1708
1709static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1710 SkFixed fx, SkFixed fy,
1711 const SkGlyph& glyph) {
1712 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1713
bungeman@google.com15865a72012-01-11 16:28:04 +00001714 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001715
1716 if (NULL == procs->fFontScaler) {
1717 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1718 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001719
bungeman@google.com15865a72012-01-11 16:28:04 +00001720 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1721 glyph.getSubXFixed(),
1722 glyph.getSubYFixed()),
1723 SkFixedFloorToFixed(fx),
1724 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001725 procs->fFontScaler);
1726}
1727
bsalomon@google.com5782d712011-01-21 21:03:59 +00001728SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001729
1730 // deferred allocation
1731 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001732 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001733 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1734 fDrawProcs->fContext = fContext;
1735 }
1736
1737 // init our (and GL's) state
1738 fDrawProcs->fTextContext = context;
1739 fDrawProcs->fFontScaler = NULL;
1740 return fDrawProcs;
1741}
1742
1743void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1744 size_t byteLength, SkScalar x, SkScalar y,
1745 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001746 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001747
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001748 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001749 // this guy will just call our drawPath()
1750 draw.drawText((const char*)text, byteLength, x, y, paint);
1751 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001752 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001753
1754 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001755 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001756 return;
1757 }
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +00001758 GrBitmapTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001759 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001760 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1761 }
1762}
1763
1764void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1765 size_t byteLength, const SkScalar pos[],
1766 SkScalar constY, int scalarsPerPos,
1767 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001768 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001769
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001770 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001771 // this guy will just call our drawPath()
1772 draw.drawPosText((const char*)text, byteLength, pos, constY,
1773 scalarsPerPos, paint);
1774 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001775 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001776
1777 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001778 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001779 return;
1780 }
jvanverth@google.comc7a40fa2013-10-16 18:15:34 +00001781 GrBitmapTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001782 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001783 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1784 scalarsPerPos, paint);
1785 }
1786}
1787
1788void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1789 size_t len, const SkPath& path,
1790 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001791 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001792
1793 SkASSERT(draw.fDevice == this);
1794 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1795}
1796
1797///////////////////////////////////////////////////////////////////////////////
1798
reed@google.comf67e4cf2011-03-15 20:56:58 +00001799bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1800 if (!paint.isLCDRenderText()) {
1801 // we're cool with the paint as is
1802 return false;
1803 }
1804
1805 if (paint.getShader() ||
1806 paint.getXfermode() || // unless its srcover
1807 paint.getMaskFilter() ||
1808 paint.getRasterizer() ||
1809 paint.getColorFilter() ||
1810 paint.getPathEffect() ||
1811 paint.isFakeBoldText() ||
1812 paint.getStyle() != SkPaint::kFill_Style) {
1813 // turn off lcd
1814 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1815 flags->fHinting = paint.getHinting();
1816 return true;
1817 }
1818 // we're cool with the paint as is
1819 return false;
1820}
1821
reed@google.com75d939b2011-12-07 15:07:23 +00001822void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001823 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001824 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001825}
1826
reed@google.comf67e4cf2011-03-15 20:56:58 +00001827///////////////////////////////////////////////////////////////////////////////
1828
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001829SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1830 int width, int height,
1831 bool isOpaque,
1832 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001833 GrTextureDesc desc;
1834 desc.fConfig = fRenderTarget->config();
1835 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1836 desc.fWidth = width;
1837 desc.fHeight = height;
1838 desc.fSampleCnt = fRenderTarget->numSamples();
1839
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001840 SkAutoTUnref<GrTexture> texture;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001841 // Skia's convention is to only clear a device if it is non-opaque.
1842 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001843
1844#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1845 // layers are never draw in repeat modes, so we can request an approx
1846 // match and ignore any padding.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001847 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1848 GrContext::kApprox_ScratchTexMatch :
1849 GrContext::kExact_ScratchTexMatch;
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001850 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001851#else
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001852 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001853#endif
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001854 if (NULL != texture.get()) {
1855 return SkNEW_ARGS(SkGpuDevice,(fContext, texture, needClear));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001856 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001857 GrPrintf("---- failed to create compatible device texture [%d %d]\n", width, height);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001858 return NULL;
1859 }
1860}
1861
1862SkGpuDevice::SkGpuDevice(GrContext* context,
1863 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001864 bool needClear)
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001865 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001866
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001867 SkASSERT(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001868 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1869 // cache. We pass true for the third argument so that it will get unlocked.
1870 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001871 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001872}