blob: d9de0ca774cb27e46191d788ac81b6de76e85ca6 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
tomhudson@google.com898e7b52012-06-01 20:42:15 +00008#include "SkGpuDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00009
tomhudson@google.com2f68e762012-07-17 18:43:21 +000010#include "effects/GrTextureDomainEffect.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000011#include "effects/GrSimpleTextureEffect.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000012
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrContext.h"
14#include "GrTextContext.h"
15
robertphillips@google.come9c04692012-06-29 00:30:13 +000016#include "SkGrTexturePixelRef.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
Scroggo97c88c22011-05-11 14:05:25 +000018#include "SkColorFilter.h"
senorblanco@chromium.org9c397442012-09-27 21:57:45 +000019#include "SkDeviceImageFilterProxy.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020#include "SkDrawProcs.h"
21#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000022#include "SkImageFilter.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000023#include "SkPathEffect.h"
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000024#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000025#include "SkStroke.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000026#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000027
bsalomon@google.com06cd7322012-03-30 18:45:35 +000028#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000029
30#if 0
31 extern bool (*gShouldDrawProc)();
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000032 #define CHECK_SHOULD_DRAW(draw, forceI) \
reed@google.comac10a2d2010-12-22 21:39:39 +000033 do { \
34 if (gShouldDrawProc && !gShouldDrawProc()) return; \
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000035 this->prepareDraw(draw, forceI); \
reed@google.comac10a2d2010-12-22 21:39:39 +000036 } while (0)
37#else
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000038 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
reed@google.comac10a2d2010-12-22 21:39:39 +000039#endif
40
bsalomon@google.com73818dc2013-03-28 13:23:29 +000041// we use the same effect slot on GrPaint for bitmaps and shaders (since drawBitmap, drawSprite,
42// and drawDevice ignore SkShader)
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000043enum {
bsalomon@google.com73818dc2013-03-28 13:23:29 +000044 kShaderEffectIdx = 0,
45 kBitmapEffectIdx = 0,
46 kColorFilterEffectIdx = 1,
47 kXfermodeEffectIdx = 2,
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000048};
49
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000050// This constant represents the screen alignment criterion in texels for
rmistry@google.comd6176b02012-08-23 18:14:13 +000051// requiring texture domain clamping to prevent color bleeding when drawing
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000052// a sub region of a larger source image.
53#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000054
junov@google.comdbfac8a2012-12-06 21:47:40 +000055#define DO_DEFERRED_CLEAR() \
56 do { \
57 if (fNeedClear) { \
58 this->clear(SK_ColorTRANSPARENT); \
59 } \
60 } while (false) \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000061
reed@google.comac10a2d2010-12-22 21:39:39 +000062///////////////////////////////////////////////////////////////////////////////
63
reed@google.comb0a34d82012-07-11 19:57:55 +000064#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
65 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
66
67///////////////////////////////////////////////////////////////////////////////
68
69
bsalomon@google.com84405e02012-03-05 19:57:21 +000070class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
71public:
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000072 SkAutoCachedTexture()
73 : fDevice(NULL)
74 , fTexture(NULL) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000075 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000076
bsalomon@google.com84405e02012-03-05 19:57:21 +000077 SkAutoCachedTexture(SkGpuDevice* device,
78 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000079 const GrTextureParams* params,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000080 GrTexture** texture)
81 : fDevice(NULL)
82 , fTexture(NULL) {
83 GrAssert(NULL != texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000084 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000085 }
reed@google.comac10a2d2010-12-22 21:39:39 +000086
bsalomon@google.com84405e02012-03-05 19:57:21 +000087 ~SkAutoCachedTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000088 if (NULL != fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +000089 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
bsalomon@google.com84405e02012-03-05 19:57:21 +000090 }
reed@google.comac10a2d2010-12-22 21:39:39 +000091 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000092
93 GrTexture* set(SkGpuDevice* device,
94 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000095 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000096 if (NULL != fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +000097 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000098 fTexture = NULL;
bsalomon@google.com84405e02012-03-05 19:57:21 +000099 }
100 fDevice = device;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000101 GrTexture* result = (GrTexture*)bitmap.getTexture();
102 if (NULL == result) {
103 // Cannot return the native texture so look it up in our cache
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000104 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000105 result = fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000106 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000107 return result;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000108 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000109
bsalomon@google.com84405e02012-03-05 19:57:21 +0000110private:
111 SkGpuDevice* fDevice;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000112 GrTexture* fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000113};
reed@google.comac10a2d2010-12-22 21:39:39 +0000114
115///////////////////////////////////////////////////////////////////////////////
116
reed@google.comac10a2d2010-12-22 21:39:39 +0000117struct GrSkDrawProcs : public SkDrawProcs {
118public:
119 GrContext* fContext;
120 GrTextContext* fTextContext;
121 GrFontScaler* fFontScaler; // cached in the skia glyphcache
122};
123
124///////////////////////////////////////////////////////////////////////////////
125
reed@google.comaf951c92011-06-16 19:10:39 +0000126static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
127 switch (config) {
128 case kAlpha_8_GrPixelConfig:
129 *isOpaque = false;
130 return SkBitmap::kA8_Config;
131 case kRGB_565_GrPixelConfig:
132 *isOpaque = true;
133 return SkBitmap::kRGB_565_Config;
134 case kRGBA_4444_GrPixelConfig:
135 *isOpaque = false;
136 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000137 case kSkia8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000138 // we don't currently have a way of knowing whether
139 // a 8888 is opaque based on the config.
140 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000141 return SkBitmap::kARGB_8888_Config;
142 default:
143 *isOpaque = false;
144 return SkBitmap::kNo_Config;
145 }
146}
reed@google.comac10a2d2010-12-22 21:39:39 +0000147
reed@google.comaf951c92011-06-16 19:10:39 +0000148static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000149 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000150
151 bool isOpaque;
152 SkBitmap bitmap;
153 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
154 renderTarget->width(), renderTarget->height());
155 bitmap.setIsOpaque(isOpaque);
156 return bitmap;
157}
158
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000159SkGpuDevice* SkGpuDevice::Create(GrSurface* surface) {
160 GrAssert(NULL != surface);
161 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
162 return NULL;
163 }
164 if (surface->asTexture()) {
165 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture()));
166 } else {
167 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget()));
168 }
169}
170
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000171SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000172: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000173 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000174}
175
reed@google.comaf951c92011-06-16 19:10:39 +0000176SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000177: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000178 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000179}
180
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000181void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000182 GrRenderTarget* renderTarget,
183 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000184 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000185
reed@google.comaf951c92011-06-16 19:10:39 +0000186 fContext = context;
187 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000188
reed@google.comaf951c92011-06-16 19:10:39 +0000189 fRenderTarget = NULL;
190 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000191
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000192 GrAssert(NULL != renderTarget);
193 fRenderTarget = renderTarget;
194 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000195
bsalomon@google.coma2921122012-08-28 12:34:17 +0000196 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
197 // on the RT but not vice-versa.
198 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
199 // busting chrome (for a currently unknown reason).
200 GrSurface* surface = fRenderTarget->asTexture();
201 if (NULL == surface) {
202 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000203 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000204 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000205
reed@google.comaf951c92011-06-16 19:10:39 +0000206 this->setPixelRef(pr, 0)->unref();
207}
208
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000209SkGpuDevice::SkGpuDevice(GrContext* context,
210 SkBitmap::Config config,
211 int width,
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000212 int height,
213 int sampleCount)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000214 : SkDevice(config, width, height, false /*isOpaque*/) {
215
reed@google.comac10a2d2010-12-22 21:39:39 +0000216 fDrawProcs = NULL;
217
reed@google.com7b201d22011-01-11 18:59:23 +0000218 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000219 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000220
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 fRenderTarget = NULL;
222 fNeedClear = false;
223
reed@google.comaf951c92011-06-16 19:10:39 +0000224 if (config != SkBitmap::kRGB_565_Config) {
225 config = SkBitmap::kARGB_8888_Config;
226 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000227
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000228 GrTextureDesc desc;
229 desc.fFlags = kRenderTarget_GrTextureFlagBit;
230 desc.fWidth = width;
231 desc.fHeight = height;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000232 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
233 desc.fSampleCnt = sampleCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000234
bsalomon@google.coma2921122012-08-28 12:34:17 +0000235 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000236
bsalomon@google.coma2921122012-08-28 12:34:17 +0000237 if (NULL != texture) {
238 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000239 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000240
reed@google.comaf951c92011-06-16 19:10:39 +0000241 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
reed@google.comaf951c92011-06-16 19:10:39 +0000243 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000244 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000245 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000246 } else {
247 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
248 width, height);
249 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000250 }
251}
252
253SkGpuDevice::~SkGpuDevice() {
254 if (fDrawProcs) {
255 delete fDrawProcs;
256 }
257
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000258 // The GrContext takes a ref on the target. We don't want to cause the render
259 // target to be unnecessarily kept alive.
260 if (fContext->getRenderTarget() == fRenderTarget) {
261 fContext->setRenderTarget(NULL);
262 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000263
robertphillips@google.com055f9082012-10-24 13:24:11 +0000264 if (fContext->getClip() == &fClipData) {
265 fContext->setClip(NULL);
266 }
267
bsalomon@google.coma2921122012-08-28 12:34:17 +0000268 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000269 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000270}
271
reed@google.comac10a2d2010-12-22 21:39:39 +0000272///////////////////////////////////////////////////////////////////////////////
273
274void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000275 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000276 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000277}
278
279///////////////////////////////////////////////////////////////////////////////
280
bsalomon@google.comc4364992011-11-07 15:54:49 +0000281namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000282GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000283 switch (config8888) {
284 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000285 *flags = 0;
286 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000287 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000288 *flags = GrContext::kUnpremul_PixelOpsFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000289 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000290 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000291 *flags = 0;
292 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000293 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000294 *flags = GrContext::kUnpremul_PixelOpsFlag;
295 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000296 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000297 *flags = 0;
298 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000299 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000300 *flags = GrContext::kUnpremul_PixelOpsFlag;
301 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000302 default:
303 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000304 *flags = 0; // suppress warning
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000305 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000306 }
307}
308}
309
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000310bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
311 int x, int y,
312 SkCanvas::Config8888 config8888) {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000313 DO_DEFERRED_CLEAR();
bsalomon@google.com910267d2011-11-02 20:06:25 +0000314 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
315 SkASSERT(!bitmap.isNull());
316 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000317
bsalomon@google.com910267d2011-11-02 20:06:25 +0000318 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000319 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000320 uint32_t flags;
321 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000322 return fContext->readRenderTargetPixels(fRenderTarget,
323 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000324 bitmap.width(),
325 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000326 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000327 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000328 bitmap.rowBytes(),
329 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000330}
331
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000332void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
333 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000334 SkAutoLockPixels alp(bitmap);
335 if (!bitmap.readyToDraw()) {
336 return;
337 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000338
339 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000340 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000341 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000342 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000343 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000344 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000345 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000346 }
347
bsalomon@google.com6f379512011-11-16 20:36:03 +0000348 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000349 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000350}
351
robertphillips@google.com46f93502012-08-07 15:38:08 +0000352namespace {
humper@google.com05af1af2013-01-07 16:47:43 +0000353void purgeClipCB(int genID, void* ) {
robertphillips@google.com46f93502012-08-07 15:38:08 +0000354
355 if (SkClipStack::kInvalidGenID == genID ||
356 SkClipStack::kEmptyGenID == genID ||
357 SkClipStack::kWideOpenGenID == genID) {
358 // none of these cases will have a cached clip mask
359 return;
360 }
361
362}
363};
364
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000365void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
366 INHERITED::onAttachToCanvas(canvas);
367
368 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000369 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000370
371 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000372}
373
374void SkGpuDevice::onDetachFromCanvas() {
375 INHERITED::onDetachFromCanvas();
376
robertphillips@google.com46f93502012-08-07 15:38:08 +0000377 // TODO: iterate through the clip stack and clean up any cached clip masks
378 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
379
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000380 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000381}
382
robertphillips@google.com607fe072012-07-24 13:54:00 +0000383#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000384static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000385 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000386 int renderTargetWidth,
387 int renderTargetHeight) {
388
robertphillips@google.com7b112892012-07-31 15:18:21 +0000389 SkIRect devBound;
390
391 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
392
robertphillips@google.com607fe072012-07-24 13:54:00 +0000393 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000394 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000395
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000396 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000397 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000398 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000399
robertphillips@google.com7b112892012-07-31 15:18:21 +0000400 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000401
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000402 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000403
robertphillips@google.com7b112892012-07-31 15:18:21 +0000404 if (!devBound.intersect(devTemp)) {
405 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000406 }
407 }
408
robertphillips@google.com768fee82012-08-02 12:42:43 +0000409 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000410}
411#endif
412
reed@google.comac10a2d2010-12-22 21:39:39 +0000413///////////////////////////////////////////////////////////////////////////////
414
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000415// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000416// and not the state from some other canvas/device
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000417void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000418 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000419
reed@google.comac10a2d2010-12-22 21:39:39 +0000420 fContext->setRenderTarget(fRenderTarget);
421
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000422 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000423
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000424 if (forceIdentity) {
425 fContext->setIdentityMatrix();
426 } else {
427 fContext->setMatrix(*draw.fMatrix);
428 }
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000429 fClipData.fOrigin = this->getOrigin();
reed@google.comac10a2d2010-12-22 21:39:39 +0000430
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000431#ifdef SK_DEBUG
432 check_bounds(fClipData, *draw.fClip, fRenderTarget->width(), fRenderTarget->height());
433#endif
434
435 fContext->setClip(&fClipData);
436
437 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000438}
439
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000440GrRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000441 DO_DEFERRED_CLEAR();
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000442 return fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000443}
444
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000445bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000446 GrTexture* texture = fRenderTarget->asTexture();
447 if (NULL != texture) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000448 paint->colorStage(kBitmapEffectIdx)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000449 GrSimpleTextureEffect::Create(texture, SkMatrix::I()))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000450 return true;
451 }
452 return false;
453}
454
455///////////////////////////////////////////////////////////////////////////////
456
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000457SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
458SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
459SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
460SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
461SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
462 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000463SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
464 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000465SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
466SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000467
bsalomon@google.com84405e02012-03-05 19:57:21 +0000468namespace {
469
470// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
471// justAlpha indicates that skPaint's alpha should be used rather than the color
472// Callers may subsequently modify the GrPaint. Setting constantColor indicates
473// that the final paint will draw the same color at every pixel. This allows
474// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000475// color once while converting to GrPaint and then ignored.
476inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
477 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000478 bool justAlpha,
479 bool constantColor,
480 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000481
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000482 grPaint->setDither(skPaint.isDither());
483 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000484
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000485 SkXfermode::Coeff sm;
486 SkXfermode::Coeff dm;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000487
488 SkXfermode* mode = skPaint.getXfermode();
bsalomon@google.comdb446252013-03-27 18:46:16 +0000489 GrEffectRef* xferEffect = NULL;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000490 if (SkXfermode::AsNewEffectOrCoeff(mode, dev->context(), &xferEffect, &sm, &dm)) {
491 if (NULL != xferEffect) {
492 grPaint->colorStage(kXfermodeEffectIdx)->setEffect(xferEffect)->unref();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000493 sm = SkXfermode::kOne_Coeff;
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000494 dm = SkXfermode::kZero_Coeff;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000495 }
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000496 } else {
497 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000498#if 0
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000499 return false;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000500#else
501 // Fall back to src-over
502 sm = SkXfermode::kOne_Coeff;
503 dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000504#endif
bsalomon@google.com5782d712011-01-21 21:03:59 +0000505 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000506 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000507
bsalomon@google.com5782d712011-01-21 21:03:59 +0000508 if (justAlpha) {
509 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000510 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000511 // justAlpha is currently set to true only if there is a texture,
512 // so constantColor should not also be true.
513 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000514 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000515 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000516 GrAssert(!grPaint->isColorStageEnabled(kShaderEffectIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000517 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000518
Scroggo97c88c22011-05-11 14:05:25 +0000519 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000520 if (NULL != colorFilter) {
521 // if the source color is a constant then apply the filter here once rather than per pixel
522 // in a shader.
523 if (constantColor) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000524 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000525 grPaint->setColor(SkColor2GrColor(filtered));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000526 } else {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000527 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
bsalomon@google.com021fc732012-10-25 12:47:42 +0000528 if (NULL != effect.get()) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000529 grPaint->colorStage(kColorFilterEffectIdx)->setEffect(effect);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000530 } else {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000531 // TODO: rewrite this using asNewEffect()
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000532 SkColor color;
533 SkXfermode::Mode filterMode;
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000534 if (colorFilter->asColorMode(&color, &filterMode)) {
535 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000536 }
537 }
Scroggod757df22011-05-16 13:11:16 +0000538 }
Scroggo97c88c22011-05-11 14:05:25 +0000539 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000540
bsalomon@google.com5782d712011-01-21 21:03:59 +0000541 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000542}
543
bsalomon@google.com84405e02012-03-05 19:57:21 +0000544// This function is similar to skPaint2GrPaintNoShader but also converts
bsalomon@google.com08283af2012-10-26 13:01:20 +0000545// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
bsalomon@google.com84405e02012-03-05 19:57:21 +0000546// be used is set on grPaint and returned in param act. constantColor has the
547// same meaning as in skPaint2GrPaintNoShader.
548inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
549 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000550 bool constantColor,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000551 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000552 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000553 if (NULL == shader) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000554 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
555 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000556 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000557 }
558
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000559 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000560 if (NULL != effect.get()) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000561 grPaint->colorStage(kShaderEffectIdx)->setEffect(effect);
rileya@google.com91f319c2012-07-25 17:18:31 +0000562 return true;
563 }
564
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000565 // We still don't have SkColorShader::asNewEffect() implemented.
566 SkShader::GradientInfo info;
567 SkColor color;
reed@google.comac10a2d2010-12-22 21:39:39 +0000568
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000569 info.fColors = &color;
570 info.fColorOffsets = NULL;
571 info.fColorCount = 1;
572 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
573 SkPaint copy(skPaint);
574 copy.setShader(NULL);
575 // modulate the paint alpha by the shader's solid color alpha
576 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
577 copy.setColor(SkColorSetA(color, newA));
578 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000579 }
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000580 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000581}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000582}
reed@google.comac10a2d2010-12-22 21:39:39 +0000583
584///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000585void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com32cf29e2013-01-25 15:03:18 +0000586 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
587 fContext->clear(&rect, SkColor2GrColor(color), fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000588 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000589}
590
reed@google.comac10a2d2010-12-22 21:39:39 +0000591void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000592 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000593
bsalomon@google.com5782d712011-01-21 21:03:59 +0000594 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000595 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000596 return;
597 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000598
599 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000600}
601
602// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000603static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000604 kPoints_GrPrimitiveType,
605 kLines_GrPrimitiveType,
606 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000607};
608
609void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000610 size_t count, const SkPoint pts[], const SkPaint& paint) {
epoger@google.comb58772f2013-03-08 09:09:10 +0000611 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000612 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000613
614 SkScalar width = paint.getStrokeWidth();
615 if (width < 0) {
616 return;
617 }
618
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000619 // we only handle hairlines and paints without path effects or mask filters,
620 // else we let the SkDraw call our drawPath()
621 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000622 draw.drawPoints(mode, count, pts, paint, true);
623 return;
624 }
625
bsalomon@google.com5782d712011-01-21 21:03:59 +0000626 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000627 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000628 return;
629 }
630
bsalomon@google.com5782d712011-01-21 21:03:59 +0000631 fContext->drawVertices(grPaint,
632 gPointMode2PrimtiveType[mode],
633 count,
634 (GrPoint*)pts,
635 NULL,
636 NULL,
637 NULL,
638 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000639}
640
reed@google.comc9aa5872011-04-05 21:05:37 +0000641///////////////////////////////////////////////////////////////////////////////
642
reed@google.comac10a2d2010-12-22 21:39:39 +0000643void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000644 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000645 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000646 CHECK_SHOULD_DRAW(draw, false);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000647
bungeman@google.com79bd8772011-07-18 15:34:08 +0000648 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000649 SkScalar width = paint.getStrokeWidth();
650
651 /*
652 We have special code for hairline strokes, miter-strokes, and fills.
653 Anything else we just call our path code.
654 */
655 bool usePath = doStroke && width > 0 &&
656 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000657 // another two reasons we might need to call drawPath...
658 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000659 usePath = true;
660 }
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000661 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000662#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000663 if (doStroke) {
664#endif
665 usePath = true;
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000666#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000667 } else {
668 usePath = !fContext->getMatrix().preservesRightAngles();
669 }
670#endif
reed@google.com67db6642011-05-26 11:46:35 +0000671 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000672 // small miter limit means right angles show bevel...
673 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
674 paint.getStrokeMiter() < SK_ScalarSqrt2)
675 {
676 usePath = true;
677 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000678 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000679 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
680 usePath = true;
681 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000682
683 if (usePath) {
684 SkPath path;
685 path.addRect(rect);
686 this->drawPath(draw, path, paint, NULL, true);
687 return;
688 }
689
690 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000691 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000692 return;
693 }
reed@google.com20efde72011-05-09 17:00:02 +0000694 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000695}
696
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000697///////////////////////////////////////////////////////////////////////////////
698
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000699void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
700 const SkPaint& paint) {
701 CHECK_FOR_NODRAW_ANNOTATION(paint);
702 CHECK_SHOULD_DRAW(draw, false);
703
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000704 bool usePath = !rect.isSimple();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000705 // another two reasons we might need to call drawPath...
706 if (paint.getMaskFilter() || paint.getPathEffect()) {
707 usePath = true;
708 }
709 // until we can rotate rrects...
710 if (!usePath && !fContext->getMatrix().rectStaysRect()) {
711 usePath = true;
712 }
713
714 if (usePath) {
715 SkPath path;
716 path.addRRect(rect);
717 this->drawPath(draw, path, paint, NULL, true);
718 return;
719 }
720
721 GrPaint grPaint;
722 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
723 return;
724 }
725
726 SkStrokeRec stroke(paint);
727 fContext->drawRRect(grPaint, rect, stroke);
728}
729
730///////////////////////////////////////////////////////////////////////////////
731
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000732void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
733 const SkPaint& paint) {
734 CHECK_FOR_NODRAW_ANNOTATION(paint);
735 CHECK_SHOULD_DRAW(draw, false);
736
737 bool usePath = false;
738 // some basic reasons we might need to call drawPath...
739 if (paint.getMaskFilter() || paint.getPathEffect()) {
740 usePath = true;
741 }
742
743 if (usePath) {
744 SkPath path;
745 path.addOval(oval);
746 this->drawPath(draw, path, paint, NULL, true);
747 return;
748 }
749
750 GrPaint grPaint;
751 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
752 return;
753 }
754 SkStrokeRec stroke(paint);
755
756 fContext->drawOval(grPaint, oval, stroke);
757}
758
reed@google.com69302852011-02-16 18:08:07 +0000759#include "SkMaskFilter.h"
760#include "SkBounder.h"
761
bsalomon@google.com85003222012-03-28 14:44:37 +0000762///////////////////////////////////////////////////////////////////////////////
763
764// helpers for applying mask filters
765namespace {
766
robertphillips@google.com49149312013-07-03 15:34:35 +0000767// Draw a mask using the supplied paint. Since the coverage/geometry
768// is already burnt into the mask this boils down to a rect draw.
769// Return true if the mask was successfully drawn.
770bool draw_mask(GrContext* context, const SkRect& maskRect,
771 GrPaint* grp, GrTexture* mask) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000772
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000773 GrContext::AutoMatrix am;
774 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000775 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000776 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000777
bsalomon@google.com88becf42012-10-05 14:54:42 +0000778 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000779 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000780 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000781
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000782 SkMatrix matrix;
robertphillips@google.com49149312013-07-03 15:34:35 +0000783 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
784 matrix.postIDiv(mask->width(), mask->height());
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000785
bsalomon@google.com08283af2012-10-26 13:01:20 +0000786 grp->coverageStage(MASK_IDX)->reset();
robertphillips@google.com49149312013-07-03 15:34:35 +0000787 grp->coverageStage(MASK_IDX)->setEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
788 context->drawRect(*grp, maskRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000789 return true;
790}
791
robertphillips@google.com49149312013-07-03 15:34:35 +0000792bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
793 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
794 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000795 SkMask srcM, dstM;
796
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000797 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
798 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000799 return false;
800 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000801 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000802
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000803 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000804 return false;
805 }
806 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000807 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000808
809 if (clip.quickReject(dstM.fBounds)) {
810 return false;
811 }
812 if (bounder && !bounder->doIRect(dstM.fBounds)) {
813 return false;
814 }
815
816 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000817 // the current clip (and identity matrix) and GrPaint settings
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000818 GrTextureDesc desc;
819 desc.fWidth = dstM.fBounds.width();
820 desc.fHeight = dstM.fBounds.height();
821 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000822
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000823 GrAutoScratchTexture ast(context, desc);
824 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000825
reed@google.com69302852011-02-16 18:08:07 +0000826 if (NULL == texture) {
827 return false;
828 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000829 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000830 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000831
robertphillips@google.com49149312013-07-03 15:34:35 +0000832 SkRect maskRect = SkRect::MakeFromIRect(dstM.fBounds);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000833
robertphillips@google.com49149312013-07-03 15:34:35 +0000834 return draw_mask(context, maskRect, grp, texture);
835}
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000836
robertphillips@google.com49149312013-07-03 15:34:35 +0000837// Create a mask of 'devPath' and place the result in 'mask'. Return true on
838// success; false otherwise.
839bool create_mask_GPU(GrContext* context,
840 const SkRect& maskRect,
841 const SkPath& devPath,
842 const SkStrokeRec& stroke,
843 bool doAA,
844 GrAutoScratchTexture* mask) {
845 GrTextureDesc desc;
846 desc.fFlags = kRenderTarget_GrTextureFlagBit;
847 desc.fWidth = SkScalarCeilToInt(maskRect.width());
848 desc.fHeight = SkScalarCeilToInt(maskRect.height());
849 // We actually only need A8, but it often isn't supported as a
850 // render target so default to RGBA_8888
851 desc.fConfig = kRGBA_8888_GrPixelConfig;
852 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
853 desc.fConfig = kAlpha_8_GrPixelConfig;
854 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000855
robertphillips@google.com49149312013-07-03 15:34:35 +0000856 mask->set(context, desc);
857 if (NULL == mask->texture()) {
858 return false;
859 }
860
861 GrTexture* maskTexture = mask->texture();
862 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
863
864 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
865 GrContext::AutoClip ac(context, clipRect);
866
867 context->clear(NULL, 0x0);
868
869 GrPaint tempPaint;
870 if (doAA) {
871 tempPaint.setAntiAlias(true);
872 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
873 // blend coeff of zero requires dual source blending support in order
874 // to properly blend partially covered pixels. This means the AA
875 // code path may not be taken. So we use a dst blend coeff of ISA. We
876 // could special case AA draws to a dst surface with known alpha=0 to
877 // use a zero dst coeff when dual source blending isn't available.
878 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
879 }
880
881 GrContext::AutoMatrix am;
882
883 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
884 SkMatrix translate;
885 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
886 am.set(context, translate);
887 context->drawPath(tempPaint, devPath, stroke);
reed@google.com69302852011-02-16 18:08:07 +0000888 return true;
889}
reed@google.com69302852011-02-16 18:08:07 +0000890
robertphillips@google.com49149312013-07-03 15:34:35 +0000891SkBitmap wrap_texture(GrTexture* texture) {
892 SkBitmap result;
893 bool dummy;
894 SkBitmap::Config config = grConfig2skConfig(texture->config(), &dummy);
895 result.setConfig(config, texture->width(), texture->height());
896 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (texture)))->unref();
897 return result;
bsalomon@google.com85003222012-03-28 14:44:37 +0000898}
899
robertphillips@google.com49149312013-07-03 15:34:35 +0000900};
bsalomon@google.com85003222012-03-28 14:44:37 +0000901
reed@google.com0c219b62011-02-16 21:31:18 +0000902void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000903 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000904 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000905 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000906 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000907
bsalomon@google.com5782d712011-01-21 21:03:59 +0000908 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000909 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000910 return;
911 }
912
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000913 // can we cheat, and treat a thin stroke as a hairline w/ coverage
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000914 // if we can, we draw lots faster (raster device does this same test)
915 SkScalar hairlineCoverage;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000916 bool doHairLine = SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage);
917 if (doHairLine) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000918 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000919 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000920
reed@google.comfe626382011-09-21 13:50:35 +0000921 // If we have a prematrix, apply it to the path, optimizing for the case
922 // where the original path can in fact be modified in place (even though
923 // its parameter type is const).
924 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000925 SkPath tmpPath, effectPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000926
927 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +0000928 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +0000929
reed@google.come3445642011-02-16 23:20:39 +0000930 if (!pathIsMutable) {
931 result = &tmpPath;
932 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000933 }
reed@google.come3445642011-02-16 23:20:39 +0000934 // should I push prePathMatrix on our MV stack temporarily, instead
935 // of applying it here? See SkDraw.cpp
936 pathPtr->transform(*prePathMatrix, result);
937 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +0000938 }
reed@google.com0c219b62011-02-16 21:31:18 +0000939 // at this point we're done with prePathMatrix
940 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +0000941
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000942 SkStrokeRec stroke(paint);
943 SkPathEffect* pathEffect = paint.getPathEffect();
reed@google.com4bbdeac2013-01-24 21:03:11 +0000944 const SkRect* cullRect = NULL; // TODO: what is our bounds?
945 if (pathEffect && pathEffect->filterPath(&effectPath, *pathPtr, &stroke,
946 cullRect)) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000947 pathPtr = &effectPath;
948 }
949
950 if (!pathEffect && doHairLine) {
951 stroke.setHairlineStyle();
reed@google.com0c219b62011-02-16 21:31:18 +0000952 }
953
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000954 if (paint.getMaskFilter()) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000955 if (!stroke.isHairlineStyle()) {
956 if (stroke.applyToPath(&tmpPath, *pathPtr)) {
957 pathPtr = &tmpPath;
robertphillips@google.com49149312013-07-03 15:34:35 +0000958 pathIsMutable = true;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000959 stroke.setFillStyle();
960 }
961 }
962
reed@google.com0c219b62011-02-16 21:31:18 +0000963 // avoid possibly allocating a new path in transform if we can
964 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
965
966 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000967 pathPtr->transform(fContext->getMatrix(), devPathPtr);
robertphillips@google.com49149312013-07-03 15:34:35 +0000968
969 SkRect maskRect;
970 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
971 draw.fClip->getBounds(),
972 fContext->getMatrix(),
973 &maskRect)) {
974 SkIRect finalIRect;
975 maskRect.roundOut(&finalIRect);
976 if (draw.fClip->quickReject(finalIRect)) {
977 // clipped out
978 return;
979 }
980 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
981 // nothing to draw
982 return;
983 }
984
985 GrAutoScratchTexture mask;
986
skia.committer@gmail.com1842adf2013-07-04 07:01:07 +0000987 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
robertphillips@google.com49149312013-07-03 15:34:35 +0000988 grPaint.isAntiAlias(), &mask)) {
989 GrTexture* filtered;
990
991 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(), maskRect, &filtered, true)) {
992 SkAutoTUnref<GrTexture> atu(filtered);
993
994 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
995 // This path is completely drawn
996 return;
997 }
998 }
999 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001000 }
robertphillips@google.com49149312013-07-03 15:34:35 +00001001
1002 // draw the mask on the CPU - this is a fallthrough path in case the
1003 // GPU path fails
1004 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
1005 SkPaint::kFill_Style;
1006 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
1007 *draw.fClip, draw.fBounder, &grPaint, style);
reed@google.com69302852011-02-16 18:08:07 +00001008 return;
1009 }
reed@google.com69302852011-02-16 18:08:07 +00001010
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001011 fContext->drawPath(grPaint, *pathPtr, stroke);
reed@google.comac10a2d2010-12-22 21:39:39 +00001012}
1013
bsalomon@google.comfb309512011-11-30 14:13:48 +00001014namespace {
1015
1016inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1017 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1018 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1019 return tilesX * tilesY;
1020}
1021
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001022inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001023 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001024 int maxTextureSize) {
1025 static const int kSmallTileSize = 1 << 10;
1026 if (maxTextureSize <= kSmallTileSize) {
1027 return maxTextureSize;
1028 }
1029
1030 size_t maxTexTotalTileSize;
1031 size_t smallTotalTileSize;
1032
robertphillips@google.combac6b052012-09-28 18:06:49 +00001033 SkIRect iSrc;
1034 src.roundOut(&iSrc);
1035
1036 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1037 iSrc.fTop,
1038 iSrc.fRight,
1039 iSrc.fBottom,
1040 maxTextureSize);
1041 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1042 iSrc.fTop,
1043 iSrc.fRight,
1044 iSrc.fBottom,
1045 kSmallTileSize);
1046
bsalomon@google.comfb309512011-11-30 14:13:48 +00001047 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1048 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1049
1050 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1051 return kSmallTileSize;
1052 } else {
1053 return maxTextureSize;
1054 }
1055}
1056}
1057
1058bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001059 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001060 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001061 // if bitmap is explictly texture backed then just use the texture
1062 if (NULL != bitmap.getTexture()) {
1063 return false;
1064 }
1065 // if it's larger than the max texture size, then we have no choice but
1066 // tiling
1067 const int maxTextureSize = fContext->getMaxTextureSize();
1068 if (bitmap.width() > maxTextureSize ||
1069 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001070 return true;
1071 }
1072 // if we are going to have to draw the whole thing, then don't tile
1073 if (NULL == srcRectPtr) {
1074 return false;
1075 }
1076 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001077 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001078 return false;
1079 }
1080
1081 // At this point we know we could do the draw by uploading the entire bitmap
1082 // as a texture. However, if the texture would be large compared to the
1083 // cache size and we don't require most of it for this draw then tile to
1084 // reduce the amount of upload and cache spill.
1085
1086 // assumption here is that sw bitmap size is a good proxy for its size as
1087 // a texture
1088 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001089 size_t cacheSize;
1090 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001091 if (bmpSize < cacheSize / 2) {
1092 return false;
1093 }
1094
robertphillips@google.combac6b052012-09-28 18:06:49 +00001095 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1096 srcRectPtr->height() / bitmap.height());
1097 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001098 return true;
1099 } else {
1100 return false;
1101 }
1102}
1103
reed@google.comac10a2d2010-12-22 21:39:39 +00001104void SkGpuDevice::drawBitmap(const SkDraw& draw,
1105 const SkBitmap& bitmap,
1106 const SkIRect* srcRectPtr,
1107 const SkMatrix& m,
1108 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001109
1110 SkRect tmp;
1111 SkRect* tmpPtr = NULL;
1112
1113 // convert from SkIRect to SkRect
1114 if (NULL != srcRectPtr) {
1115 tmp.set(*srcRectPtr);
1116 tmpPtr = &tmp;
1117 }
1118
1119 // We cannot call drawBitmapRect here since 'm' could be anything
1120 this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint);
1121}
1122
1123void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1124 const SkBitmap& bitmap,
1125 const SkRect* srcRectPtr,
1126 const SkMatrix& m,
1127 const SkPaint& paint) {
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);
1146 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001147 return; // extraction failed
1148 }
1149 bitmapPtr = &tmp;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001150 srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop));
1151 // The source rect has changed so update the matrix
1152 newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop));
junov@google.comd935cfb2011-06-27 20:48:23 +00001153 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001154
junov@google.comd935cfb2011-06-27 20:48:23 +00001155 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001156 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001157 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001158
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001159 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001160 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001161 // also affect the behavior of the mask filter.
1162 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001163 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001164 SkDraw transformedDraw(draw);
1165 transformedDraw.fMatrix = &drawMatrix;
1166
robertphillips@google.combac6b052012-09-28 18:06:49 +00001167 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001168
junov@google.comd935cfb2011-06-27 20:48:23 +00001169 return;
1170 }
1171
bsalomon@google.com5782d712011-01-21 21:03:59 +00001172 GrPaint grPaint;
skia.committer@gmail.com4e73aa12013-01-09 02:01:30 +00001173
humper@google.com9aaf36d2013-01-08 16:08:01 +00001174 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001175 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001176 return;
1177 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001178 GrTextureParams params;
1179 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001180
robertphillips@google.combac6b052012-09-28 18:06:49 +00001181 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001182 // take the simple case
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001183 this->internalDrawBitmap(bitmap, srcRect, m, params, &grPaint);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001184 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001185 this->drawTiledBitmap(bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001186 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001187}
1188
1189// Break 'bitmap' into several tiles to draw it since it has already
1190// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001191void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001192 const SkRect& srcRect,
1193 const SkMatrix& m,
1194 const GrTextureParams& params,
1195 GrPaint* grPaint) {
1196 const int maxTextureSize = fContext->getMaxTextureSize();
1197
1198 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001199
reed@google.comac10a2d2010-12-22 21:39:39 +00001200 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001201 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001202 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001203 const GrRenderTarget* rt = fContext->getRenderTarget();
1204 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1205 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1206 return;
1207 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001208 SkMatrix matrix, inverse;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001209 matrix.setConcat(fContext->getMatrix(), m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001210 if (!matrix.invert(&inverse)) {
1211 return;
1212 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001213 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001214 }
1215
bsalomon@google.comfb309512011-11-30 14:13:48 +00001216 int nx = bitmap.width() / tileSize;
1217 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001218 for (int x = 0; x <= nx; x++) {
1219 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001220 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001221 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001222 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001223 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001224 SkIntToScalar((y + 1) * tileSize));
1225
1226 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001227 continue;
1228 }
1229
robertphillips@google.combac6b052012-09-28 18:06:49 +00001230 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001231 continue;
1232 }
1233
1234 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001235 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001236 tileR.roundOut(&iTileR);
1237 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001238 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.combac6b052012-09-28 18:06:49 +00001239 tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop));
reed@google.comac10a2d2010-12-22 21:39:39 +00001240 SkMatrix tmpM(m);
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +00001241 tmpM.preTranslate(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001242 SkIntToScalar(iTileR.fTop));
1243
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001244 this->internalDrawBitmap(tmpB, tileR, tmpM, params, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001245 }
1246 }
1247 }
1248}
1249
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001250namespace {
1251
1252bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1253 // detect pixel disalignment
1254 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1255 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001256 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001257 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1258 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1259 COLOR_BLEED_TOLERANCE &&
1260 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1261 COLOR_BLEED_TOLERANCE) {
1262 return true;
1263 }
1264 return false;
1265}
1266
1267bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1268 const SkMatrix& m) {
1269 // Only gets called if hasAlignedSamples returned false.
1270 // So we can assume that sampling is axis aligned but not texel aligned.
1271 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001272 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001273 outerTransformedRect(transformedRect);
1274 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1275 m.mapRect(&innerTransformedRect, innerSrcRect);
1276
1277 // The gap between outerTransformedRect and innerTransformedRect
1278 // represents the projection of the source border area, which is
1279 // problematic for color bleeding. We must check whether any
1280 // destination pixels sample the border area.
1281 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1282 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1283 SkIRect outer, inner;
1284 outerTransformedRect.round(&outer);
1285 innerTransformedRect.round(&inner);
1286 // If the inner and outer rects round to the same result, it means the
1287 // border does not overlap any pixel centers. Yay!
1288 return inner != outer;
1289}
1290
1291} // unnamed namespace
1292
reed@google.comac10a2d2010-12-22 21:39:39 +00001293/*
1294 * This is called by drawBitmap(), which has to handle images that may be too
1295 * large to be represented by a single texture.
1296 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001297 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1298 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001299 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001300void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001301 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001303 const GrTextureParams& params,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001304 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001305 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1306 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001307
reed@google.comac10a2d2010-12-22 21:39:39 +00001308 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001309 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001310 if (NULL == texture) {
1311 return;
1312 }
1313
robertphillips@google.combac6b052012-09-28 18:06:49 +00001314 GrRect dstRect(srcRect);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001315 GrRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001316 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1317 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1318 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1319 SkScalarMul(srcRect.fTop, hInv),
1320 SkScalarMul(srcRect.fRight, wInv),
1321 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001322
rmistry@google.comd6176b02012-08-23 18:14:13 +00001323 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001324 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001325 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001326 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001327 srcRect.height() < bitmap.height();
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001328 if (m.rectStaysRect() && fContext->getMatrix().rectStaysRect()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001329 // sampling is axis-aligned
robertphillips@google.combac6b052012-09-28 18:06:49 +00001330 GrRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001331 SkMatrix srcToDeviceMatrix(m);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001332 srcToDeviceMatrix.postConcat(fContext->getMatrix());
robertphillips@google.combac6b052012-09-28 18:06:49 +00001333 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001334
robertphillips@google.combac6b052012-09-28 18:06:49 +00001335 if (hasAlignedSamples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001336 // We could also turn off filtering here (but we already did a cache lookup with
1337 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001338 needsTextureDomain = false;
1339 } else {
1340 needsTextureDomain = needsTextureDomain &&
robertphillips@google.combac6b052012-09-28 18:06:49 +00001341 mayColorBleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001342 }
1343 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001344 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001345
1346 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001347 SkAutoTUnref<GrEffectRef> effect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001348 if (needsTextureDomain) {
1349 // Use a constrained texture domain to avoid color bleeding
bsalomon@google.com81712882012-11-01 17:12:34 +00001350 SkScalar left, top, right, bottom;
1351 if (srcRect.width() > SK_Scalar1) {
1352 SkScalar border = SK_ScalarHalf / bitmap.width();
junov@google.com6acc9b32011-05-16 18:32:07 +00001353 left = paintRect.left() + border;
1354 right = paintRect.right() - border;
1355 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001356 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
junov@google.com6acc9b32011-05-16 18:32:07 +00001357 }
bsalomon@google.com81712882012-11-01 17:12:34 +00001358 if (srcRect.height() > SK_Scalar1) {
1359 SkScalar border = SK_ScalarHalf / bitmap.height();
junov@google.com6acc9b32011-05-16 18:32:07 +00001360 top = paintRect.top() + border;
1361 bottom = paintRect.bottom() - border;
1362 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001363 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
junov@google.com6acc9b32011-05-16 18:32:07 +00001364 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001365 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001366 effect.reset(GrTextureDomainEffect::Create(texture,
1367 SkMatrix::I(),
1368 textureDomain,
1369 GrTextureDomainEffect::kClamp_WrapMode,
1370 params.isBilerp()));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001371 } else {
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001372 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
junov@google.com6acc9b32011-05-16 18:32:07 +00001373 }
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001374 grPaint->colorStage(kBitmapEffectIdx)->setEffect(effect);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001375 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001376}
1377
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001378static bool filter_texture(SkDevice* device, GrContext* context,
1379 GrTexture* texture, SkImageFilter* filter,
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001380 int w, int h, SkBitmap* result, SkIPoint* offset) {
reed@google.com8926b162012-03-23 15:36:36 +00001381 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001382 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001383
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001384 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org985fa792012-10-24 15:14:26 +00001385 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1386 // filter. Also set the clip wide open and the matrix to identity.
1387 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001388 return filter->filterImageGPU(&proxy, wrap_texture(texture), result, offset);
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001389 } else {
1390 return false;
reed@google.com8926b162012-03-23 15:36:36 +00001391 }
reed@google.com8926b162012-03-23 15:36:36 +00001392}
1393
reed@google.comac10a2d2010-12-22 21:39:39 +00001394void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001395 int left, int top, const SkPaint& paint) {
1396 // drawSprite is defined to be in device coords.
1397 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001398
reed@google.com8926b162012-03-23 15:36:36 +00001399 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001400 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1401 return;
1402 }
1403
reed@google.com76dd2772012-01-05 21:15:07 +00001404 int w = bitmap.width();
1405 int h = bitmap.height();
1406
bsalomon@google.com5782d712011-01-21 21:03:59 +00001407 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001408 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001409 return;
1410 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001411
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001412 GrEffectStage* stage = grPaint.colorStage(kBitmapEffectIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001413
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001414 GrTexture* texture;
bsalomon@google.com08283af2012-10-26 13:01:20 +00001415 stage->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001416 // draw sprite uses the default texture params
1417 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001418 grPaint.colorStage(kBitmapEffectIdx)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001419 GrSimpleTextureEffect::Create(texture, SkMatrix::I()))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001420
reed@google.com8926b162012-03-23 15:36:36 +00001421 SkImageFilter* filter = paint.getImageFilter();
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001422 SkIPoint offset = SkIPoint::Make(0, 0);
reed@google.com8926b162012-03-23 15:36:36 +00001423 if (NULL != filter) {
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001424 SkBitmap filterBitmap;
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001425 if (filter_texture(this, fContext, texture, filter, w, h, &filterBitmap, &offset)) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001426 grPaint.colorStage(kBitmapEffectIdx)->setEffect(
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +00001427 GrSimpleTextureEffect::Create(filterBitmap.getTexture(), SkMatrix::I()))->unref();
1428 texture = filterBitmap.getTexture();
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001429 w = filterBitmap.width();
1430 h = filterBitmap.height();
reed@google.com8926b162012-03-23 15:36:36 +00001431 }
reed@google.com76dd2772012-01-05 21:15:07 +00001432 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001433
bsalomon@google.com5782d712011-01-21 21:03:59 +00001434 fContext->drawRectToRect(grPaint,
bsalomon@google.com81712882012-11-01 17:12:34 +00001435 GrRect::MakeXYWH(SkIntToScalar(left),
1436 SkIntToScalar(top),
1437 SkIntToScalar(w),
1438 SkIntToScalar(h)),
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001439 GrRect::MakeXYWH(offset.fX,
1440 offset.fY,
1441 SK_Scalar1 * w / texture->width(),
1442 SK_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001443}
1444
reed@google.com33535f32012-09-25 15:37:50 +00001445void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1446 const SkRect* src, const SkRect& dst,
1447 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001448 SkMatrix matrix;
1449 SkRect bitmapBounds, tmpSrc;
1450
1451 bitmapBounds.set(0, 0,
1452 SkIntToScalar(bitmap.width()),
1453 SkIntToScalar(bitmap.height()));
1454
reed@google.com33535f32012-09-25 15:37:50 +00001455 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001456 if (NULL != src) {
1457 tmpSrc = *src;
1458 } else {
1459 tmpSrc = bitmapBounds;
1460 }
1461 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1462
1463 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1464 if (NULL != src) {
1465 if (!bitmapBounds.contains(tmpSrc)) {
1466 if (!tmpSrc.intersect(bitmapBounds)) {
1467 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001468 }
reed@google.com33535f32012-09-25 15:37:50 +00001469 }
reed@google.com33535f32012-09-25 15:37:50 +00001470 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001471
robertphillips@google.combac6b052012-09-28 18:06:49 +00001472 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint);
reed@google.com33535f32012-09-25 15:37:50 +00001473}
1474
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001475void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001476 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001477 // clear of the source device must occur before CHECK_SHOULD_DRAW
1478 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1479 if (dev->fNeedClear) {
1480 // TODO: could check here whether we really need to draw at all
1481 dev->clear(0x0);
1482 }
1483
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001484 // drawDevice is defined to be in device coords.
1485 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001486
bsalomon@google.com5782d712011-01-21 21:03:59 +00001487 GrPaint grPaint;
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001488 grPaint.colorStage(kBitmapEffectIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001489 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001490 !skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001491 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001492 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001493
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001494 GrTexture* devTex = (*grPaint.getColorStage(kBitmapEffectIdx).getEffect())->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001495 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001496
senorblanco@chromium.org514b9222013-01-18 21:53:12 +00001497 const SkBitmap& bm = dev->accessBitmap(false);
1498 int w = bm.width();
1499 int h = bm.height();
1500
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001501 SkImageFilter* filter = paint.getImageFilter();
1502 if (NULL != filter) {
1503 SkBitmap filterBitmap;
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001504 SkIPoint offset = SkIPoint::Make(0, 0);
1505 if (filter_texture(this, fContext, devTex, filter, w, h, &filterBitmap, &offset)) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001506 grPaint.colorStage(kBitmapEffectIdx)->setEffect(
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +00001507 GrSimpleTextureEffect::Create(filterBitmap.getTexture(), SkMatrix::I()))->unref();
1508 devTex = filterBitmap.getTexture();
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001509 w = filterBitmap.width();
1510 h = filterBitmap.height();
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001511 x += offset.fX;
1512 y += offset.fY;
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001513 }
1514 }
1515
bsalomon@google.com81712882012-11-01 17:12:34 +00001516 GrRect dstRect = GrRect::MakeXYWH(SkIntToScalar(x),
1517 SkIntToScalar(y),
1518 SkIntToScalar(w),
1519 SkIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001520
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001521 // The device being drawn may not fill up its texture (saveLayer uses
1522 // the approximate ).
bsalomon@google.com81712882012-11-01 17:12:34 +00001523 GrRect srcRect = GrRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1524 SK_Scalar1 * h / devTex->height());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001525
1526 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001527}
1528
reed@google.com8926b162012-03-23 15:36:36 +00001529bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
senorblanco@chromium.orgd043cce2013-04-08 19:43:22 +00001530 return filter->canFilterImageGPU();
reed@google.com8926b162012-03-23 15:36:36 +00001531}
1532
1533bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1534 const SkMatrix& ctm,
1535 SkBitmap* result, SkIPoint* offset) {
1536 // want explicitly our impl, so guard against a subclass of us overriding it
1537 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001538 return false;
1539 }
reed@google.com8926b162012-03-23 15:36:36 +00001540
1541 SkAutoLockPixels alp(src, !src.getTexture());
1542 if (!src.getTexture() && !src.readyToDraw()) {
1543 return false;
1544 }
1545
reed@google.com8926b162012-03-23 15:36:36 +00001546 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001547 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1548 // must be pushed upstack.
1549 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001550
commit-bot@chromium.org7b320702013-07-10 21:22:18 +00001551 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), result, offset);
reed@google.com76dd2772012-01-05 21:15:07 +00001552}
1553
reed@google.comac10a2d2010-12-22 21:39:39 +00001554///////////////////////////////////////////////////////////////////////////////
1555
1556// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001557static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001558 kTriangles_GrPrimitiveType,
1559 kTriangleStrip_GrPrimitiveType,
1560 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001561};
1562
1563void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1564 int vertexCount, const SkPoint vertices[],
1565 const SkPoint texs[], const SkColor colors[],
1566 SkXfermode* xmode,
1567 const uint16_t indices[], int indexCount,
1568 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001569 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001570
bsalomon@google.com5782d712011-01-21 21:03:59 +00001571 GrPaint grPaint;
bsalomon@google.com5782d712011-01-21 21:03:59 +00001572 // we ignore the shader if texs is null.
1573 if (NULL == texs) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001574 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001575 return;
1576 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001577 } else {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001578 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001579 return;
1580 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001581 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001582
1583 if (NULL != xmode && NULL != texs && NULL != colors) {
reed@google.com8d3cd7a2013-01-30 21:36:11 +00001584 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001585 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1586#if 0
1587 return
1588#endif
1589 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001590 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001591
bsalomon@google.com498776a2011-08-16 19:20:44 +00001592 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1593 if (NULL != colors) {
1594 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001595 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001596 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001597 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001598 }
1599 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001600 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001601 fContext->drawVertices(grPaint,
1602 gVertexMode2PrimitiveType[vmode],
1603 vertexCount,
1604 (GrPoint*) vertices,
1605 (GrPoint*) texs,
1606 colors,
1607 indices,
1608 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001609}
1610
1611///////////////////////////////////////////////////////////////////////////////
1612
1613static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001614 GrFontScaler* scaler = (GrFontScaler*)data;
1615 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001616}
1617
1618static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1619 void* auxData;
1620 GrFontScaler* scaler = NULL;
1621 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1622 scaler = (GrFontScaler*)auxData;
1623 }
1624 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001625 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001626 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1627 }
1628 return scaler;
1629}
1630
1631static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1632 SkFixed fx, SkFixed fy,
1633 const SkGlyph& glyph) {
1634 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1635
bungeman@google.com15865a72012-01-11 16:28:04 +00001636 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001637
1638 if (NULL == procs->fFontScaler) {
1639 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1640 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001641
bungeman@google.com15865a72012-01-11 16:28:04 +00001642 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1643 glyph.getSubXFixed(),
1644 glyph.getSubYFixed()),
1645 SkFixedFloorToFixed(fx),
1646 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001647 procs->fFontScaler);
1648}
1649
bsalomon@google.com5782d712011-01-21 21:03:59 +00001650SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001651
1652 // deferred allocation
1653 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001654 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001655 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1656 fDrawProcs->fContext = fContext;
1657 }
1658
1659 // init our (and GL's) state
1660 fDrawProcs->fTextContext = context;
1661 fDrawProcs->fFontScaler = NULL;
1662 return fDrawProcs;
1663}
1664
1665void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1666 size_t byteLength, SkScalar x, SkScalar y,
1667 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001668 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001669
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001670 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001671 // this guy will just call our drawPath()
1672 draw.drawText((const char*)text, byteLength, x, y, paint);
1673 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001674 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001675
1676 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001677 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001678 return;
1679 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001680 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001681 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001682 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1683 }
1684}
1685
1686void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1687 size_t byteLength, const SkScalar pos[],
1688 SkScalar constY, int scalarsPerPos,
1689 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001690 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001691
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001692 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001693 // this guy will just call our drawPath()
1694 draw.drawPosText((const char*)text, byteLength, pos, constY,
1695 scalarsPerPos, paint);
1696 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001697 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001698
1699 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001700 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001701 return;
1702 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001703 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001704 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001705 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1706 scalarsPerPos, paint);
1707 }
1708}
1709
1710void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1711 size_t len, const SkPath& path,
1712 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001713 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001714
1715 SkASSERT(draw.fDevice == this);
1716 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1717}
1718
1719///////////////////////////////////////////////////////////////////////////////
1720
reed@google.comf67e4cf2011-03-15 20:56:58 +00001721bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1722 if (!paint.isLCDRenderText()) {
1723 // we're cool with the paint as is
1724 return false;
1725 }
1726
1727 if (paint.getShader() ||
1728 paint.getXfermode() || // unless its srcover
1729 paint.getMaskFilter() ||
1730 paint.getRasterizer() ||
1731 paint.getColorFilter() ||
1732 paint.getPathEffect() ||
1733 paint.isFakeBoldText() ||
1734 paint.getStyle() != SkPaint::kFill_Style) {
1735 // turn off lcd
1736 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1737 flags->fHinting = paint.getHinting();
1738 return true;
1739 }
1740 // we're cool with the paint as is
1741 return false;
1742}
1743
reed@google.com75d939b2011-12-07 15:07:23 +00001744void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001745 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001746 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001747}
1748
reed@google.comf67e4cf2011-03-15 20:56:58 +00001749///////////////////////////////////////////////////////////////////////////////
1750
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001751SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1752 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001753 bool isOpaque,
1754 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001755 GrTextureDesc desc;
1756 desc.fConfig = fRenderTarget->config();
1757 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1758 desc.fWidth = width;
1759 desc.fHeight = height;
1760 desc.fSampleCnt = fRenderTarget->numSamples();
1761
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001762 SkAutoTUnref<GrTexture> texture;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001763 // Skia's convention is to only clear a device if it is non-opaque.
1764 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001765
1766#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1767 // layers are never draw in repeat modes, so we can request an approx
1768 // match and ignore any padding.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001769 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1770 GrContext::kApprox_ScratchTexMatch :
1771 GrContext::kExact_ScratchTexMatch;
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001772 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001773#else
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001774 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001775#endif
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001776 if (NULL != texture.get()) {
1777 return SkNEW_ARGS(SkGpuDevice,(fContext, texture, needClear));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001778 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001779 GrPrintf("---- failed to create compatible device texture [%d %d]\n", width, height);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001780 return NULL;
1781 }
1782}
1783
1784SkGpuDevice::SkGpuDevice(GrContext* context,
1785 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001786 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001787 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1788
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001789 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001790 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1791 // cache. We pass true for the third argument so that it will get unlocked.
1792 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001793 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001794}