blob: ea2b537520340ce5ba3e737ee0f56b58996d3b50 [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * 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.
6 */
7
8#include "SkGpuDevice.h"
9
robertphillipsccb1b572015-05-27 11:02:55 -070010#include "GrBlurUtils.h"
kkinnunenabcfab42015-02-22 22:53:44 -080011#include "GrContext.h"
robertphillipsea461502015-05-26 11:38:03 -070012#include "GrDrawContext.h"
robertphillips2334fb62015-06-17 05:43:33 -070013#include "GrFontScaler.h"
kkinnunenabcfab42015-02-22 22:53:44 -080014#include "GrGpu.h"
15#include "GrGpuResourcePriv.h"
robertphillips98d709b2014-09-02 10:20:50 -070016#include "GrLayerHoister.h"
robertphillips274b4ba2014-09-04 07:24:18 -070017#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070018#include "GrStrokeInfo.h"
joshualitt8f94bb22015-04-28 07:04:11 -070019#include "GrTextContext.h"
egdanielbbcb38d2014-06-19 10:19:29 -070020#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080021#include "SkCanvasPriv.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000022#include "SkDrawProcs.h"
kkinnunenabcfab42015-02-22 22:53:44 -080023#include "SkErrorInternals.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000024#include "SkGlyphCache.h"
kkinnunenabcfab42015-02-22 22:53:44 -080025#include "SkGrTexturePixelRef.h"
reeda85d4d02015-05-06 12:56:48 -070026#include "SkImage_Base.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000027#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080028#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000029#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000030#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000031#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070032#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080034#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000036#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080037#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000038#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000039#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000040#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070041#include "SkXfermode.h"
kkinnunenabcfab42015-02-22 22:53:44 -080042#include "effects/GrBicubicEffect.h"
43#include "effects/GrDashingEffect.h"
44#include "effects/GrSimpleTextureEffect.h"
45#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000046
reedf037e0b2014-10-30 11:34:15 -070047#if SK_SUPPORT_GPU
48
senorblanco55b6d8b2014-07-30 11:26:46 -070049enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
50
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000051#if 0
52 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080053 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000054 do { \
55 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080056 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000057 } while (0)
58#else
joshualitt5531d512014-12-17 15:50:11 -080059 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000060#endif
61
62// This constant represents the screen alignment criterion in texels for
63// requiring texture domain clamping to prevent color bleeding when drawing
64// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000065#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000066
67#define DO_DEFERRED_CLEAR() \
68 do { \
bsalomonafe30052015-01-16 07:32:33 -080069 if (fNeedClear) { \
70 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000071 } \
72 } while (false) \
73
74///////////////////////////////////////////////////////////////////////////////
75
76#define CHECK_FOR_ANNOTATION(paint) \
77 do { if (paint.getAnnotation()) { return; } } while (0)
78
79///////////////////////////////////////////////////////////////////////////////
80
bsalomonbcf0a522014-10-08 08:40:09 -070081// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
82// just accesses the backing GrTexture. Otherwise, it creates a cached texture
83// representation and releases it in the destructor.
84class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040085public:
bsalomonbcf0a522014-10-08 08:40:09 -070086 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070087
bsalomonbcf0a522014-10-08 08:40:09 -070088 AutoBitmapTexture(GrContext* context,
89 const SkBitmap& bitmap,
90 const GrTextureParams* params,
91 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040092 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070093 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040094 }
95
bsalomonbcf0a522014-10-08 08:40:09 -070096 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040097 const SkBitmap& bitmap,
98 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -070099 // Either get the texture directly from the bitmap, or else use the cache and
100 // remember to unref it.
101 if (GrTexture* bmpTexture = bitmap.getTexture()) {
102 fTexture.reset(NULL);
103 return bmpTexture;
104 } else {
105 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
106 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400107 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400108 }
109
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000110private:
bsalomonbcf0a522014-10-08 08:40:09 -0700111 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000112};
113
114///////////////////////////////////////////////////////////////////////////////
115
116struct GrSkDrawProcs : public SkDrawProcs {
117public:
118 GrContext* fContext;
119 GrTextContext* fTextContext;
120 GrFontScaler* fFontScaler; // cached in the skia glyphcache
121};
122
123///////////////////////////////////////////////////////////////////////////////
124
bsalomon74f681d2015-06-23 14:38:48 -0700125/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
126 should fail. */
127bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
128 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
129 *flags = 0;
130 if (info) {
131 switch (info->alphaType()) {
132 case kPremul_SkAlphaType:
133 break;
134 case kOpaque_SkAlphaType:
135 *flags |= SkGpuDevice::kIsOpaque_Flag;
136 break;
137 default: // If it is unpremul or unknown don't try to render
138 return false;
139 }
140 }
141 if (kClear_InitContents == init) {
142 *flags |= kNeedClear_Flag;
143 }
144 return true;
145}
146
147SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props,
148 InitContents init) {
149 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, init);
senorblancod0d37ca2015-04-02 04:54:56 -0700150}
151
152SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
bsalomon74f681d2015-06-23 14:38:48 -0700153 const SkSurfaceProps* props, InitContents init) {
bsalomonafe30052015-01-16 07:32:33 -0800154 if (!rt || rt->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000155 return NULL;
156 }
bsalomon74f681d2015-06-23 14:38:48 -0700157 unsigned flags;
158 if (!CheckAlphaTypeAndGetFlags(NULL, init, &flags)) {
159 return NULL;
160 }
senorblancod0d37ca2015-04-02 04:54:56 -0700161 return SkNEW_ARGS(SkGpuDevice, (rt, width, height, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000162}
163
bsalomon74f681d2015-06-23 14:38:48 -0700164SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
165 const SkImageInfo& info, int sampleCount,
166 const SkSurfaceProps* props, InitContents init) {
167 unsigned flags;
168 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
169 return NULL;
170 }
171
172 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
173 if (NULL == rt) {
174 return NULL;
175 }
176
177 return SkNEW_ARGS(SkGpuDevice, (rt, info.width(), info.height(), props, flags));
178}
179
senorblancod0d37ca2015-04-02 04:54:56 -0700180SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
181 const SkSurfaceProps* props, unsigned flags)
robertphillipsfcf78292015-06-19 11:49:52 -0700182 : INHERITED(SkSurfacePropsCopyOrDefault(props))
reedb2db8982014-11-13 12:41:02 -0800183{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000184 fDrawProcs = NULL;
185
bsalomonafe30052015-01-16 07:32:33 -0800186 fContext = SkRef(rt->getContext());
bsalomon74f681d2015-06-23 14:38:48 -0700187 fNeedClear = SkToBool(flags & kNeedClear_Flag);
188 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000189
bsalomonafe30052015-01-16 07:32:33 -0800190 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000191
bsalomon74f681d2015-06-23 14:38:48 -0700192 SkAlphaType at = fOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
193 SkImageInfo info = rt->surfacePriv().info(at).makeWH(width, height);
bsalomonafe30052015-01-16 07:32:33 -0800194 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt));
bsalomonafbf2d62014-09-30 12:18:44 -0700195 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700196 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700197
robertphillips7b05ff12015-06-19 14:14:54 -0700198 fDrawContext.reset(SkRef(fContext->drawContext(&this->surfaceProps())));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000199}
200
kkinnunenabcfab42015-02-22 22:53:44 -0800201GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
202 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000203 if (kUnknown_SkColorType == origInfo.colorType() ||
204 origInfo.width() < 0 || origInfo.height() < 0) {
205 return NULL;
206 }
207
bsalomonafe30052015-01-16 07:32:33 -0800208 if (!context) {
209 return NULL;
210 }
211
reede5ea5002014-09-03 11:54:58 -0700212 SkColorType ct = origInfo.colorType();
213 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700214 if (kRGB_565_SkColorType == ct) {
215 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800216 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
217 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700218 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800219 }
220 if (kOpaque_SkAlphaType != at) {
221 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000222 }
reede5ea5002014-09-03 11:54:58 -0700223 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000224
bsalomonf2703d82014-10-28 14:33:06 -0700225 GrSurfaceDesc desc;
226 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000227 desc.fWidth = info.width();
228 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000229 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000230 desc.fSampleCnt = sampleCount;
bsalomond309e7a2015-04-30 14:18:54 -0700231 GrTexture* texture = context->textureProvider()->createTexture(
232 desc, SkToBool(budgeted), NULL, 0);
kkinnunenabcfab42015-02-22 22:53:44 -0800233 if (NULL == texture) {
234 return NULL;
235 }
236 SkASSERT(NULL != texture->asRenderTarget());
237 return texture->asRenderTarget();
238}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000239
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000240SkGpuDevice::~SkGpuDevice() {
241 if (fDrawProcs) {
242 delete fDrawProcs;
243 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000244
bsalomon32d0b3b2014-08-29 07:50:23 -0700245 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000246 fContext->unref();
247}
248
249///////////////////////////////////////////////////////////////////////////////
250
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000251bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
252 int x, int y) {
253 DO_DEFERRED_CLEAR();
254
255 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000256 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000257 if (kUnknown_GrPixelConfig == config) {
258 return false;
259 }
260
261 uint32_t flags = 0;
262 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
263 flags = GrContext::kUnpremul_PixelOpsFlag;
264 }
bsalomon74f681d2015-06-23 14:38:48 -0700265 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
266 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000267}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000268
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000269bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
270 int x, int y) {
271 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000272 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000273 if (kUnknown_GrPixelConfig == config) {
274 return false;
275 }
276 uint32_t flags = 0;
277 if (kUnpremul_SkAlphaType == info.alphaType()) {
278 flags = GrContext::kUnpremul_PixelOpsFlag;
279 }
280 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
281
282 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700283 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000284
285 return true;
286}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000287
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000288const SkBitmap& SkGpuDevice::onAccessBitmap() {
289 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700290 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000291}
292
reed41e010c2015-06-09 12:16:53 -0700293bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
294 DO_DEFERRED_CLEAR();
295 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
296 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
297 // ... ugh.
298 fLegacyBitmap.notifyPixelsChanged();
299 return false;
300}
301
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000302void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
303 INHERITED::onAttachToCanvas(canvas);
304
305 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800306 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000307}
308
309void SkGpuDevice::onDetachFromCanvas() {
310 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800311 fClip.reset();
joshualitt44701df2015-02-23 14:44:57 -0800312 fClipStack.reset(NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000313}
314
315// call this every draw call, to ensure that the context reflects our state,
316// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800317void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800318 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000319
joshualitt44701df2015-02-23 14:44:57 -0800320 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000321
joshualitt570d2f82015-02-25 13:19:48 -0800322 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323
324 DO_DEFERRED_CLEAR();
325}
326
327GrRenderTarget* SkGpuDevice::accessRenderTarget() {
robertphillips7156b092015-05-14 08:54:12 -0700328 DO_DEFERRED_CLEAR();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000329 return fRenderTarget;
330}
331
reed8eddfb52014-12-04 07:50:14 -0800332void SkGpuDevice::clearAll() {
333 GrColor color = 0;
334 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
335 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillipsea461502015-05-26 11:38:03 -0700336 fDrawContext->clear(fRenderTarget, &rect, color, true);
bsalomonafe30052015-01-16 07:32:33 -0800337 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800338}
339
kkinnunenabcfab42015-02-22 22:53:44 -0800340void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
341 // Caller must have accessed the render target, because it knows the rt must be replaced.
342 SkASSERT(!fNeedClear);
343
344 SkSurface::Budgeted budgeted =
345 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
346 : SkSurface::kNo_Budgeted;
347
348 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
vbuzinovdded6962015-06-12 08:59:45 -0700349 fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt));
kkinnunenabcfab42015-02-22 22:53:44 -0800350
351 if (NULL == newRT) {
352 return;
353 }
354
355 if (shouldRetainContent) {
356 if (fRenderTarget->wasDestroyed()) {
357 return;
358 }
359 this->context()->copySurface(newRT, fRenderTarget);
360 }
361
362 SkASSERT(fRenderTarget != newRT);
363
364 fRenderTarget->unref();
365 fRenderTarget = newRT.detach();
366
bsalomon74f681d2015-06-23 14:38:48 -0700367#ifdef SK_DEBUG
368 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
369 kPremul_SkAlphaType);
370 SkASSERT(info == fLegacyBitmap.info());
371#endif
372 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (fLegacyBitmap.info(), fRenderTarget));
kkinnunenabcfab42015-02-22 22:53:44 -0800373 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700374
robertphillips7b05ff12015-06-19 14:14:54 -0700375 fDrawContext.reset(SkRef(fRenderTarget->getContext()->drawContext(&this->surfaceProps())));
kkinnunenabcfab42015-02-22 22:53:44 -0800376}
377
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000378///////////////////////////////////////////////////////////////////////////////
379
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000380void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800381 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700382 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000383
384 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700385 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
386 return;
387 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000388
robertphillipsea461502015-05-26 11:38:03 -0700389 fDrawContext->drawPaint(fRenderTarget, fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000390}
391
392// must be in SkCanvas::PointMode order
393static const GrPrimitiveType gPointMode2PrimtiveType[] = {
394 kPoints_GrPrimitiveType,
395 kLines_GrPrimitiveType,
396 kLineStrip_GrPrimitiveType
397};
398
ethannicholas330bb952015-07-17 06:44:02 -0700399// suppress antialiasing on axis-aligned integer-coordinate lines
400static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
401 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
402 return false;
403 }
404 if (count == 2) {
405 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
406 // the other coordinates are not. This does mean the two end pixels of the line will be
407 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
408 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
409 // more complete fix is possible down the road, for the time being we accept the error on
410 // the two end pixels as being the lesser of two evils.
411 if (pts[0].fX == pts[1].fX) {
412 return ((int) pts[0].fX) != pts[0].fX;
413 }
414 if (pts[0].fY == pts[1].fY) {
415 return ((int) pts[0].fY) != pts[0].fY;
416 }
417 }
418 return true;
419}
420
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000421void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
422 size_t count, const SkPoint pts[], const SkPaint& paint) {
423 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800424 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000425
426 SkScalar width = paint.getStrokeWidth();
427 if (width < 0) {
428 return;
429 }
430
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000431 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700432 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
433 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700434 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
435 &grPaint)) {
436 return;
437 }
egdaniele61c4112014-06-12 10:24:21 -0700438 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700439 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700440 path.moveTo(pts[0]);
441 path.lineTo(pts[1]);
robertphillipsea461502015-05-26 11:38:03 -0700442 fDrawContext->drawPath(fRenderTarget, fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700443 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000444 }
445
ethannicholas330bb952015-07-17 06:44:02 -0700446 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000447 // else we let the SkDraw call our drawPath()
ethannicholas330bb952015-07-17 06:44:02 -0700448 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
449 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000450 draw.drawPoints(mode, count, pts, paint, true);
451 return;
452 }
453
454 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700455 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
456 return;
457 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000458
robertphillipsea461502015-05-26 11:38:03 -0700459 fDrawContext->drawVertices(fRenderTarget,
460 fClip,
461 grPaint,
462 *draw.fMatrix,
463 gPointMode2PrimtiveType[mode],
464 SkToS32(count),
465 (SkPoint*)pts,
466 NULL,
467 NULL,
468 NULL,
469 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000470}
471
472///////////////////////////////////////////////////////////////////////////////
473
474void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
475 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700476 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
477
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000478 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800479 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000480
481 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
482 SkScalar width = paint.getStrokeWidth();
483
484 /*
485 We have special code for hairline strokes, miter-strokes, bevel-stroke
486 and fills. Anything else we just call our path code.
487 */
488 bool usePath = doStroke && width > 0 &&
489 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
490 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
491 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700492
493 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000494 usePath = true;
495 }
egdanield58a0ba2014-06-11 10:30:05 -0700496
joshualitt5531d512014-12-17 15:50:11 -0800497 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000498#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
499 if (doStroke) {
500#endif
501 usePath = true;
502#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
503 } else {
joshualitt5531d512014-12-17 15:50:11 -0800504 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000505 }
506#endif
507 }
508 // until we can both stroke and fill rectangles
509 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
510 usePath = true;
511 }
512
egdanield58a0ba2014-06-11 10:30:05 -0700513 GrStrokeInfo strokeInfo(paint);
514
515 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700516 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700517 usePath = true;
518 }
519
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000520 if (usePath) {
521 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700522 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000523 path.addRect(rect);
524 this->drawPath(draw, path, paint, NULL, true);
525 return;
526 }
527
528 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700529 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
530 return;
531 }
Mike Klein744fb732014-06-23 15:13:26 -0400532
robertphillipsea461502015-05-26 11:38:03 -0700533 fDrawContext->drawRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000534}
535
536///////////////////////////////////////////////////////////////////////////////
537
538void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800539 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700540 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800542 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000543
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000544 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700545 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
546 return;
547 }
Mike Klein744fb732014-06-23 15:13:26 -0400548
egdanield58a0ba2014-06-11 10:30:05 -0700549 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000550 if (paint.getMaskFilter()) {
551 // try to hit the fast path for drawing filtered round rects
552
553 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800554 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000555 if (devRRect.allCornersCircular()) {
556 SkRect maskRect;
557 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800558 draw.fClip->getBounds(),
559 *draw.fMatrix,
560 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000561 SkIRect finalIRect;
562 maskRect.roundOut(&finalIRect);
563 if (draw.fClip->quickReject(finalIRect)) {
564 // clipped out
565 return;
566 }
joshualitt25d9c152015-02-18 12:29:52 -0800567 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
568 fRenderTarget,
569 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800570 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800571 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700572 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700573 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000574 return;
575 }
576 }
577
578 }
579 }
580
581 }
582
egdanield58a0ba2014-06-11 10:30:05 -0700583 bool usePath = false;
584
585 if (paint.getMaskFilter()) {
586 usePath = true;
587 } else {
588 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700589 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700590 usePath = true;
591 }
592 }
593
594
595 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000596 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700597 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000598 path.addRRect(rect);
599 this->drawPath(draw, path, paint, NULL, true);
600 return;
601 }
Mike Klein744fb732014-06-23 15:13:26 -0400602
robertphillipsea461502015-05-26 11:38:03 -0700603 fDrawContext->drawRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000604}
605
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000606void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800607 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000608 SkStrokeRec stroke(paint);
609 if (stroke.isFillStyle()) {
610
611 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800612 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000613
614 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700615 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
616 &grPaint)) {
617 return;
618 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000619
620 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
robertphillipsea461502015-05-26 11:38:03 -0700621 fDrawContext->drawDRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000622 return;
623 }
624 }
625
626 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700627 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000628 path.addRRect(outer);
629 path.addRRect(inner);
630 path.setFillType(SkPath::kEvenOdd_FillType);
631
632 this->drawPath(draw, path, paint, NULL, true);
633}
634
635
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000636/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000637
638void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
639 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700640 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000641 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800642 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000643
egdanield58a0ba2014-06-11 10:30:05 -0700644 GrStrokeInfo strokeInfo(paint);
645
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000646 bool usePath = false;
647 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700648 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000649 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700650 } else {
651 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700652 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700653 usePath = true;
654 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000655 }
656
657 if (usePath) {
658 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700659 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660 path.addOval(oval);
661 this->drawPath(draw, path, paint, NULL, true);
662 return;
663 }
664
665 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700666 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
667 return;
668 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000669
robertphillipsea461502015-05-26 11:38:03 -0700670 fDrawContext->drawOval(fRenderTarget, fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000671}
672
673#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000674
675///////////////////////////////////////////////////////////////////////////////
676
robertphillipsccb1b572015-05-27 11:02:55 -0700677static SkBitmap wrap_texture(GrTexture* texture, int width, int height) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000678 SkBitmap result;
senorblancod0d37ca2015-04-02 04:54:56 -0700679 result.setInfo(SkImageInfo::MakeN32Premul(width, height));
reed6c225732014-06-09 19:52:07 -0700680 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000681 return result;
682}
683
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000684void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
685 const SkPaint& paint, const SkMatrix* prePathMatrix,
686 bool pathIsMutable) {
687 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800688 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700689 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000690
robertphillipsccb1b572015-05-27 11:02:55 -0700691 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, fRenderTarget,
692 fClip, origSrcPath, paint,
693 *draw.fMatrix, prePathMatrix,
694 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000695}
696
697static const int kBmpSmallTileSize = 1 << 10;
698
699static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
700 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
701 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
702 return tilesX * tilesY;
703}
704
705static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
706 if (maxTileSize <= kBmpSmallTileSize) {
707 return maxTileSize;
708 }
709
710 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
711 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
712
713 maxTileTotalTileSize *= maxTileSize * maxTileSize;
714 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
715
716 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
717 return kBmpSmallTileSize;
718 } else {
719 return maxTileSize;
720 }
721}
722
723// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
724// pixels from the bitmap are necessary.
bsalomon74f681d2015-06-23 14:38:48 -0700725static void determine_clipped_src_rect(const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800726 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800727 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000728 const SkBitmap& bitmap,
729 const SkRect* srcRectPtr,
730 SkIRect* clippedSrcIRect) {
joshualitt570d2f82015-02-25 13:19:48 -0800731 clip.getConservativeBounds(rt, clippedSrcIRect, NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000732 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800733 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000734 clippedSrcIRect->setEmpty();
735 return;
736 }
737 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
738 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700739 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000740 // we've setup src space 0,0 to map to the top left of the src rect.
741 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742 if (!clippedSrcRect.intersect(*srcRectPtr)) {
743 clippedSrcIRect->setEmpty();
744 return;
745 }
746 }
747 clippedSrcRect.roundOut(clippedSrcIRect);
748 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
749 if (!clippedSrcIRect->intersect(bmpBounds)) {
750 clippedSrcIRect->setEmpty();
751 }
752}
753
754bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800755 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000756 const GrTextureParams& params,
757 const SkRect* srcRectPtr,
758 int maxTileSize,
759 int* tileSize,
760 SkIRect* clippedSrcRect) const {
761 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700762 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000763 return false;
764 }
765
766 // if it's larger than the max tile size, then we have no choice but tiling.
767 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
bsalomon74f681d2015-06-23 14:38:48 -0700768 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, bitmap,
joshualitt570d2f82015-02-25 13:19:48 -0800769 srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000770 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
771 return true;
772 }
773
774 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
775 return false;
776 }
777
778 // if the entire texture is already in our cache then no reason to tile it
779 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
780 return false;
781 }
782
783 // At this point we know we could do the draw by uploading the entire bitmap
784 // as a texture. However, if the texture would be large compared to the
785 // cache size and we don't require most of it for this draw then tile to
786 // reduce the amount of upload and cache spill.
787
788 // assumption here is that sw bitmap size is a good proxy for its size as
789 // a texture
790 size_t bmpSize = bitmap.getSize();
791 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000792 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000793 if (bmpSize < cacheSize / 2) {
794 return false;
795 }
796
797 // Figure out how much of the src we will need based on the src rect and clipping.
bsalomon74f681d2015-06-23 14:38:48 -0700798 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, bitmap, srcRectPtr,
joshualitt25d9c152015-02-18 12:29:52 -0800799 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000800 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
801 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
802 kBmpSmallTileSize * kBmpSmallTileSize;
803
804 return usedTileBytes < 2 * bmpSize;
805}
806
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000807void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000808 const SkBitmap& bitmap,
809 const SkMatrix& m,
810 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000811 SkMatrix concat;
812 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
813 if (!m.isIdentity()) {
814 concat.setConcat(*draw->fMatrix, m);
815 draw.writable()->fMatrix = &concat;
816 }
reeda5517e22015-07-14 10:54:12 -0700817 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kStrict_SrcRectConstraint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000818}
819
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000820// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000821// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
822// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000823static inline void clamped_outset_with_offset(SkIRect* iRect,
824 int outset,
825 SkPoint* offset,
826 const SkIRect& clamp) {
827 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000828
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000829 int leftClampDelta = clamp.fLeft - iRect->fLeft;
830 if (leftClampDelta > 0) {
831 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000832 iRect->fLeft = clamp.fLeft;
833 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000834 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000835 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000836
837 int topClampDelta = clamp.fTop - iRect->fTop;
838 if (topClampDelta > 0) {
839 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000840 iRect->fTop = clamp.fTop;
841 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000842 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000843 }
844
845 if (iRect->fRight > clamp.fRight) {
846 iRect->fRight = clamp.fRight;
847 }
848 if (iRect->fBottom > clamp.fBottom) {
849 iRect->fBottom = clamp.fBottom;
850 }
851}
852
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000853static bool has_aligned_samples(const SkRect& srcRect,
854 const SkRect& transformedRect) {
855 // detect pixel disalignment
856 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
857 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
858 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
859 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
860 SkScalarAbs(transformedRect.width() - srcRect.width()) <
861 COLOR_BLEED_TOLERANCE &&
862 SkScalarAbs(transformedRect.height() - srcRect.height()) <
863 COLOR_BLEED_TOLERANCE) {
864 return true;
865 }
866 return false;
867}
868
869static bool may_color_bleed(const SkRect& srcRect,
870 const SkRect& transformedRect,
robertphillips1accc4c2015-07-20 10:22:29 -0700871 const SkMatrix& m,
872 bool isMSAA) {
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000873 // Only gets called if has_aligned_samples returned false.
874 // So we can assume that sampling is axis aligned but not texel aligned.
875 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
876 SkRect innerSrcRect(srcRect), innerTransformedRect,
877 outerTransformedRect(transformedRect);
robertphillips1accc4c2015-07-20 10:22:29 -0700878 if (isMSAA) {
879 innerSrcRect.inset(SK_Scalar1, SK_Scalar1);
880 } else {
881 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
882 }
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000883 m.mapRect(&innerTransformedRect, innerSrcRect);
884
885 // The gap between outerTransformedRect and innerTransformedRect
886 // represents the projection of the source border area, which is
887 // problematic for color bleeding. We must check whether any
888 // destination pixels sample the border area.
889 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
890 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
891 SkIRect outer, inner;
892 outerTransformedRect.round(&outer);
893 innerTransformedRect.round(&inner);
894 // If the inner and outer rects round to the same result, it means the
895 // border does not overlap any pixel centers. Yay!
896 return inner != outer;
897}
898
899static bool needs_texture_domain(const SkBitmap& bitmap,
900 const SkRect& srcRect,
901 GrTextureParams &params,
902 const SkMatrix& contextMatrix,
robertphillips1accc4c2015-07-20 10:22:29 -0700903 bool bicubic,
904 bool isMSAA) {
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000905 bool needsTextureDomain = false;
senorblancod0d37ca2015-04-02 04:54:56 -0700906 GrTexture* tex = bitmap.getTexture();
907 int width = tex ? tex->width() : bitmap.width();
908 int height = tex ? tex->height() : bitmap.height();
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000909
910 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
911 // Need texture domain if drawing a sub rect
senorblancod0d37ca2015-04-02 04:54:56 -0700912 needsTextureDomain = srcRect.width() < width ||
913 srcRect.height() < height;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000914 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
915 // sampling is axis-aligned
916 SkRect transformedRect;
917 contextMatrix.mapRect(&transformedRect, srcRect);
918
919 if (has_aligned_samples(srcRect, transformedRect)) {
920 params.setFilterMode(GrTextureParams::kNone_FilterMode);
921 needsTextureDomain = false;
922 } else {
robertphillips1accc4c2015-07-20 10:22:29 -0700923 needsTextureDomain = may_color_bleed(srcRect, transformedRect,
924 contextMatrix, isMSAA);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000925 }
926 }
927 }
928 return needsTextureDomain;
929}
930
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000931void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
932 const SkBitmap& bitmap,
933 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000934 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000935 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -0700936 SkCanvas::SrcRectConstraint constraint) {
joshualitt5531d512014-12-17 15:50:11 -0800937 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000938
939 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000940 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000941 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
942 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000943 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000944 SkScalar w = SkIntToScalar(bitmap.width());
945 SkScalar h = SkIntToScalar(bitmap.height());
946 dstSize.fWidth = w;
947 dstSize.fHeight = h;
948 srcRect.set(0, 0, w, h);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000949 } else {
bsalomon49f085d2014-09-05 13:34:00 -0700950 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000951 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000952 dstSize = *dstSizePtr;
senorblancod0d37ca2015-04-02 04:54:56 -0700953 }
954 GrTexture* tex = bitmap.getTexture();
955 int width = tex ? tex->width() : bitmap.width();
956 int height = tex ? tex->height() : bitmap.height();
957 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
958 srcRect.fRight >= width && srcRect.fBottom >= height) {
reeda5517e22015-07-14 10:54:12 -0700959 constraint = SkCanvas::kFast_SrcRectConstraint;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000960 }
961
derekf367e1862014-12-02 11:02:06 -0800962 // If the render target is not msaa and draw is antialiased, we call
963 // drawRect instead of drawing on the render target directly.
964 // FIXME: the tiled bitmap code path doesn't currently support
965 // anti-aliased edges, we work around that for now by drawing directly
966 // if the image size exceeds maximum texture size.
bsalomon76228632015-05-29 08:02:10 -0700967 int maxTextureSize = fContext->caps()->maxTextureSize();
vbuzinovdded6962015-06-12 08:59:45 -0700968 bool directDraw = fRenderTarget->isUnifiedMultisampled() ||
derekf367e1862014-12-02 11:02:06 -0800969 !paint.isAntiAlias() ||
970 bitmap.width() > maxTextureSize ||
971 bitmap.height() > maxTextureSize;
972
973 // we check whether dst rect are pixel aligned
974 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -0800975 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -0800976
977 if (staysRect) {
978 SkRect rect;
979 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -0800980 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -0800981 const SkScalar *scalars = rect.asScalars();
982 bool isDstPixelAligned = true;
983 for (int i = 0; i < 4; i++) {
984 if (!SkScalarIsInt(scalars[i])) {
985 isDstPixelAligned = false;
986 break;
987 }
988 }
989
990 if (isDstPixelAligned)
991 directDraw = true;
992 }
993 }
994
995 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000996 // Convert the bitmap to a shader so that the rect can be drawn
997 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000998 SkBitmap tmp; // subset of bitmap, if necessary
999 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001000 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001001 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001002 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1003 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1004 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001005 // In bleed mode we position and trim the bitmap based on the src rect which is
1006 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1007 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1008 // compensate.
reeda5517e22015-07-14 10:54:12 -07001009 if (SkCanvas::kStrict_SrcRectConstraint == constraint) {
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001010 SkIRect iSrc;
1011 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001012
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001013 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1014 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001015
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001016 if (!bitmap.extractSubset(&tmp, iSrc)) {
1017 return; // extraction failed
1018 }
1019 bitmapPtr = &tmp;
1020 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001021
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001022 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001023 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001024 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001025 } else {
1026 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001027 }
1028
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001029 SkPaint paintWithShader(paint);
1030 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001031 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001032 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1033 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001034
1035 return;
1036 }
1037
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001038 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1039 // the view matrix rather than a local matrix.
1040 SkMatrix m;
1041 m.setScale(dstSize.fWidth / srcRect.width(),
1042 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -08001043 SkMatrix viewM = *draw.fMatrix;
1044 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045
1046 GrTextureParams params;
reed93a12152015-03-16 10:08:34 -07001047 SkFilterQuality paintFilterQuality = paint.getFilterQuality();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001048 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001049
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001050 bool doBicubic = false;
1051
reed93a12152015-03-16 10:08:34 -07001052 switch(paintFilterQuality) {
1053 case kNone_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001054 textureFilterMode = GrTextureParams::kNone_FilterMode;
1055 break;
reed93a12152015-03-16 10:08:34 -07001056 case kLow_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001057 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1058 break;
reed93a12152015-03-16 10:08:34 -07001059 case kMedium_SkFilterQuality:
joshualitt5531d512014-12-17 15:50:11 -08001060 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001061 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1062 } else {
1063 // Don't trigger MIP level generation unnecessarily.
1064 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1065 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001066 break;
reed93a12152015-03-16 10:08:34 -07001067 case kHigh_SkFilterQuality:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001068 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001069 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001070 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001071 break;
1072 default:
1073 SkErrorInternals::SetError( kInvalidPaint_SkError,
1074 "Sorry, I don't understand the filtering "
1075 "mode you asked for. Falling back to "
1076 "MIPMaps.");
1077 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1078 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001079 }
1080
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001081 int tileFilterPad;
1082 if (doBicubic) {
1083 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1084 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1085 tileFilterPad = 0;
1086 } else {
1087 tileFilterPad = 1;
1088 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001089 params.setFilterMode(textureFilterMode);
1090
bsalomon76228632015-05-29 08:02:10 -07001091 int maxTileSize = fContext->caps()->maxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001092 int tileSize;
1093
1094 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001095 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001096 &clippedSrcRect)) {
reeda5517e22015-07-14 10:54:12 -07001097 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, constraint,
joshualitt5531d512014-12-17 15:50:11 -08001098 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001099 } else {
1100 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001101 bool needsTextureDomain = needs_texture_domain(bitmap,
1102 srcRect,
1103 params,
joshualitt5531d512014-12-17 15:50:11 -08001104 viewM,
robertphillips1accc4c2015-07-20 10:22:29 -07001105 doBicubic,
1106 fRenderTarget->isUnifiedMultisampled());
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001107 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001108 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001109 srcRect,
1110 params,
1111 paint,
reeda5517e22015-07-14 10:54:12 -07001112 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001113 doBicubic,
1114 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001115 }
1116}
1117
1118// Break 'bitmap' into several tiles to draw it since it has already
1119// been determined to be too large to fit in VRAM
1120void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001121 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001122 const SkRect& srcRect,
1123 const SkIRect& clippedSrcIRect,
1124 const GrTextureParams& params,
1125 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001126 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001127 int tileSize,
1128 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001129 // The following pixel lock is technically redundant, but it is desirable
1130 // to lock outside of the tile loop to prevent redecoding the whole image
1131 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1132 // is larger than the limit of the discardable memory pool.
1133 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001134 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1135
1136 int nx = bitmap.width() / tileSize;
1137 int ny = bitmap.height() / tileSize;
1138 for (int x = 0; x <= nx; x++) {
1139 for (int y = 0; y <= ny; y++) {
1140 SkRect tileR;
1141 tileR.set(SkIntToScalar(x * tileSize),
1142 SkIntToScalar(y * tileSize),
1143 SkIntToScalar((x + 1) * tileSize),
1144 SkIntToScalar((y + 1) * tileSize));
1145
1146 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1147 continue;
1148 }
1149
1150 if (!tileR.intersect(srcRect)) {
1151 continue;
1152 }
1153
1154 SkBitmap tmpB;
1155 SkIRect iTileR;
1156 tileR.roundOut(&iTileR);
1157 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1158 SkIntToScalar(iTileR.fTop));
1159
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001160 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001161 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001162 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001163 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001164 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001165
robertphillipsec8bb942014-11-21 10:16:25 -08001166 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167 SkIRect iClampRect;
1168
reeda5517e22015-07-14 10:54:12 -07001169 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001170 // In bleed mode we want to always expand the tile on all edges
1171 // but stay within the bitmap bounds
1172 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1173 } else {
1174 // In texture-domain/clamp mode we only want to expand the
1175 // tile on edges interior to "srcRect" (i.e., we want to
1176 // not bleed across the original clamped edges)
1177 srcRect.roundOut(&iClampRect);
1178 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001179 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1180 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001181 }
1182
1183 if (bitmap.extractSubset(&tmpB, iTileR)) {
1184 // now offset it to make it "local" to our tmp bitmap
1185 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001186 GrTextureParams paramsTemp = params;
robertphillips1accc4c2015-07-20 10:22:29 -07001187 bool needsTextureDomain = needs_texture_domain(
1188 bitmap, srcRect, paramsTemp,
1189 viewM, bicubic,
1190 fRenderTarget->isUnifiedMultisampled());
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001191 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001192 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001193 tileR,
1194 paramsTemp,
1195 paint,
reeda5517e22015-07-14 10:54:12 -07001196 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001197 bicubic,
1198 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001199 }
1200 }
1201 }
1202}
1203
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001204
1205/*
1206 * This is called by drawBitmap(), which has to handle images that may be too
1207 * large to be represented by a single texture.
1208 *
1209 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1210 * and that non-texture portion of the GrPaint has already been setup.
1211 */
1212void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001213 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001214 const SkRect& srcRect,
1215 const GrTextureParams& params,
1216 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001217 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001218 bool bicubic,
1219 bool needsTextureDomain) {
bsalomon76228632015-05-29 08:02:10 -07001220 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1221 bitmap.height() <= fContext->caps()->maxTextureSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001222
1223 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001224 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001225 if (NULL == texture) {
1226 return;
1227 }
1228
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001229 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001230 SkRect paintRect;
1231 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1232 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1233 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1234 SkScalarMul(srcRect.fTop, hInv),
1235 SkScalarMul(srcRect.fRight, wInv),
1236 SkScalarMul(srcRect.fBottom, hInv));
1237
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001238 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001239
1240 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1241 // the rest from the SkPaint.
1242 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001243 SkAutoTUnref<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001244
reeda5517e22015-07-14 10:54:12 -07001245 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001246 // Use a constrained texture domain to avoid color bleeding
1247 SkScalar left, top, right, bottom;
1248 if (srcRect.width() > SK_Scalar1) {
1249 SkScalar border = SK_ScalarHalf / texture->width();
1250 left = paintRect.left() + border;
1251 right = paintRect.right() - border;
1252 } else {
1253 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1254 }
1255 if (srcRect.height() > SK_Scalar1) {
1256 SkScalar border = SK_ScalarHalf / texture->height();
1257 top = paintRect.top() + border;
1258 bottom = paintRect.bottom() - border;
1259 } else {
1260 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1261 }
1262 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001263 if (bicubic) {
joshualitt5f10b5c2015-07-09 10:24:35 -07001264 fp.reset(GrBicubicEffect::Create(grPaint.getProcessorDataManager(), texture,
1265 SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001266 } else {
joshualitt5f10b5c2015-07-09 10:24:35 -07001267 fp.reset(GrTextureDomainEffect::Create(grPaint.getProcessorDataManager(),
1268 texture,
joshualitt5531d512014-12-17 15:50:11 -08001269 SkMatrix::I(),
1270 textureDomain,
1271 GrTextureDomain::kClamp_Mode,
1272 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001273 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001274 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001275 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1276 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualitt5f10b5c2015-07-09 10:24:35 -07001277 fp.reset(GrBicubicEffect::Create(grPaint.getProcessorDataManager(), texture, SkMatrix::I(),
1278 tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001279 } else {
joshualitt5f10b5c2015-07-09 10:24:35 -07001280 fp.reset(GrSimpleTextureEffect::Create(grPaint.getProcessorDataManager(), texture,
1281 SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001282 }
1283
joshualittb0a8a372014-09-23 09:50:21 -07001284 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001285 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001286 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1287 SkColor2GrColor(paint.getColor());
bsalomonbed83a62015-04-15 14:18:34 -07001288 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false,
1289 &grPaint)) {
1290 return;
1291 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001292
robertphillipsea461502015-05-26 11:38:03 -07001293 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dstRect,
1294 paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001295}
1296
fmalita2d97bc12014-11-20 10:44:58 -08001297bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001298 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001299 const SkImageFilter* filter,
1300 const SkImageFilter::Context& ctx,
1301 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001302 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001303
robertphillipsefbffed2015-06-22 12:06:08 -07001304 SkImageFilter::Proxy proxy(this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001305
1306 if (filter->canFilterImageGPU()) {
senorblancod0d37ca2015-04-02 04:54:56 -07001307 return filter->filterImageGPU(&proxy, wrap_texture(texture, width, height),
1308 ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001309 } else {
1310 return false;
1311 }
1312}
1313
1314void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1315 int left, int top, const SkPaint& paint) {
1316 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001317 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001318
1319 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1320 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1321 return;
1322 }
1323
1324 int w = bitmap.width();
1325 int h = bitmap.height();
1326
1327 GrTexture* texture;
1328 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001329 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001330 if (!texture) {
1331 return;
1332 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001333
1334 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001335 // This bitmap will own the filtered result as a texture.
1336 SkBitmap filteredBitmap;
1337
bsalomon49f085d2014-09-05 13:34:00 -07001338 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001339 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001340 SkMatrix matrix(*draw.fMatrix);
1341 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001342 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001343 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001344 // This cache is transient, and is freed (along with all its contained
1345 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001346 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001347 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001348 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001349 texture = (GrTexture*) filteredBitmap.getTexture();
1350 w = filteredBitmap.width();
1351 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001352 left += offset.x();
1353 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001354 } else {
1355 return;
1356 }
1357 }
1358
1359 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001360 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001361
bsalomonbed83a62015-04-15 14:18:34 -07001362 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1363 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1364 return;
1365 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001366
robertphillipsea461502015-05-26 11:38:03 -07001367 fDrawContext->drawNonAARectToRect(fRenderTarget,
1368 fClip,
1369 grPaint,
1370 SkMatrix::I(),
1371 SkRect::MakeXYWH(SkIntToScalar(left),
1372 SkIntToScalar(top),
1373 SkIntToScalar(w),
1374 SkIntToScalar(h)),
1375 SkRect::MakeXYWH(0,
1376 0,
1377 SK_Scalar1 * w / texture->width(),
1378 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001379}
1380
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001381void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001382 const SkRect* src, const SkRect& dst,
1383 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001384 SK_VIRTUAL_CONSTRAINT_TYPE legacyConstraint) {
1385 SkCanvas::SrcRectConstraint constraint = (SkCanvas::SrcRectConstraint)legacyConstraint;
1386
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001387 SkMatrix matrix;
1388 SkRect bitmapBounds, tmpSrc;
1389
1390 bitmapBounds.set(0, 0,
1391 SkIntToScalar(bitmap.width()),
1392 SkIntToScalar(bitmap.height()));
1393
1394 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001395 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001396 tmpSrc = *src;
1397 } else {
1398 tmpSrc = bitmapBounds;
1399 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001400
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001401 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1402
1403 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001404 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001405 if (!bitmapBounds.contains(tmpSrc)) {
1406 if (!tmpSrc.intersect(bitmapBounds)) {
1407 return; // nothing to draw
1408 }
1409 }
1410 }
1411
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001412 SkRect tmpDst;
1413 matrix.mapRect(&tmpDst, tmpSrc);
1414
1415 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1416 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1417 // Translate so that tempDst's top left is at the origin.
1418 matrix = *origDraw.fMatrix;
1419 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1420 draw.writable()->fMatrix = &matrix;
1421 }
1422 SkSize dstSize;
1423 dstSize.fWidth = tmpDst.width();
1424 dstSize.fHeight = tmpDst.height();
1425
reeda5517e22015-07-14 10:54:12 -07001426 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, constraint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001427}
1428
1429void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1430 int x, int y, const SkPaint& paint) {
1431 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001432 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001433 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001434
1435 // TODO: If the source device covers the whole of this device, we could
1436 // omit fNeedsClear -related flushing.
1437 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001438
1439 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001440 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001441
1442 GrRenderTarget* devRT = dev->accessRenderTarget();
1443 GrTexture* devTex;
1444 if (NULL == (devTex = devRT->asTexture())) {
1445 return;
1446 }
1447
robertphillips7b9e8a42014-12-11 08:20:31 -08001448 const SkImageInfo ii = dev->imageInfo();
1449 int w = ii.width();
1450 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001451
1452 SkImageFilter* filter = paint.getImageFilter();
1453 // This bitmap will own the filtered result as a texture.
1454 SkBitmap filteredBitmap;
1455
bsalomon49f085d2014-09-05 13:34:00 -07001456 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001457 SkIPoint offset = SkIPoint::Make(0, 0);
1458 SkMatrix matrix(*draw.fMatrix);
1459 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001460 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001461 // This cache is transient, and is freed (along with all its contained
1462 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001463 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001464 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001465 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1466 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001467 devTex = filteredBitmap.getTexture();
1468 w = filteredBitmap.width();
1469 h = filteredBitmap.height();
1470 x += offset.fX;
1471 y += offset.fY;
1472 } else {
1473 return;
1474 }
1475 }
1476
1477 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001478 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001479
bsalomonbed83a62015-04-15 14:18:34 -07001480 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1481 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1482 return;
1483 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001484
1485 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1486 SkIntToScalar(y),
1487 SkIntToScalar(w),
1488 SkIntToScalar(h));
1489
1490 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1491 // scratch texture).
1492 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1493 SK_Scalar1 * h / devTex->height());
1494
robertphillipsea461502015-05-26 11:38:03 -07001495 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, SkMatrix::I(), dstRect,
1496 srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001497}
1498
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001499bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001500 return filter->canFilterImageGPU();
1501}
1502
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001503bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001504 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001505 SkBitmap* result, SkIPoint* offset) {
1506 // want explicitly our impl, so guard against a subclass of us overriding it
1507 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1508 return false;
1509 }
1510
1511 SkAutoLockPixels alp(src, !src.getTexture());
1512 if (!src.getTexture() && !src.readyToDraw()) {
1513 return false;
1514 }
1515
1516 GrTexture* texture;
1517 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1518 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001519 AutoBitmapTexture abt(fContext, src, NULL, &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001520 if (!texture) {
1521 return false;
1522 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001523
senorblancod0d37ca2015-04-02 04:54:56 -07001524 return this->filterTexture(fContext, texture, src.width(), src.height(),
1525 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001526}
1527
reeda85d4d02015-05-06 12:56:48 -07001528static bool wrap_as_bm(const SkImage* image, SkBitmap* bm) {
bsalomon55812362015-06-10 08:49:28 -07001529 GrTexture* tex = as_IB(image)->getTexture();
reeda85d4d02015-05-06 12:56:48 -07001530 if (tex) {
reed8b26b992015-05-07 15:36:17 -07001531 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), bm);
1532 return true;
reeda85d4d02015-05-06 12:56:48 -07001533 } else {
reed8b26b992015-05-07 15:36:17 -07001534 return as_IB(image)->getROPixels(bm);
reeda85d4d02015-05-06 12:56:48 -07001535 }
reeda85d4d02015-05-06 12:56:48 -07001536}
1537
1538void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1539 const SkPaint& paint) {
1540 SkBitmap bm;
1541 if (wrap_as_bm(image, &bm)) {
1542 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1543 }
1544}
1545
1546void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001547 const SkRect& dst, const SkPaint& paint,
1548 SkCanvas::SrcRectConstraint constraint) {
reeda85d4d02015-05-06 12:56:48 -07001549 SkBitmap bm;
1550 if (wrap_as_bm(image, &bm)) {
reeda5517e22015-07-14 10:54:12 -07001551 this->drawBitmapRect(draw, bm, src, dst, paint, (SK_VIRTUAL_CONSTRAINT_TYPE)constraint);
reeda85d4d02015-05-06 12:56:48 -07001552 }
1553}
1554
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001555///////////////////////////////////////////////////////////////////////////////
1556
1557// must be in SkCanvas::VertexMode order
1558static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1559 kTriangles_GrPrimitiveType,
1560 kTriangleStrip_GrPrimitiveType,
1561 kTriangleFan_GrPrimitiveType,
1562};
1563
1564void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1565 int vertexCount, const SkPoint vertices[],
1566 const SkPoint texs[], const SkColor colors[],
1567 SkXfermode* xmode,
1568 const uint16_t indices[], int indexCount,
1569 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001570 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001571 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001572
dandov32a311b2014-07-15 19:46:26 -07001573 const uint16_t* outIndices;
1574 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1575 GrPrimitiveType primType;
1576 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001577
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001578 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1579 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001580
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001581 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001582
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001583 SkPaint copy(paint);
1584 copy.setStyle(SkPaint::kStroke_Style);
1585 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001586
dandov32a311b2014-07-15 19:46:26 -07001587 // we ignore the shader if texs is null.
bsalomonbed83a62015-04-15 14:18:34 -07001588 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy,
1589 SkColor2GrColor(copy.getColor()), NULL == colors, &grPaint)) {
1590 return;
1591 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001592
dandov32a311b2014-07-15 19:46:26 -07001593 primType = kLines_GrPrimitiveType;
1594 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001595 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001596 switch (vmode) {
1597 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001598 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001599 break;
1600 case SkCanvas::kTriangleStrip_VertexMode:
1601 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001602 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001603 break;
1604 }
mtklein533eb782014-08-27 10:39:42 -07001605
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001606 VertState state(vertexCount, indices, indexCount);
1607 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001608
dandov32a311b2014-07-15 19:46:26 -07001609 //number of indices for lines per triangle with kLines
1610 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001611
dandov32a311b2014-07-15 19:46:26 -07001612 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1613 outIndices = outAlloc.get();
1614 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001615 int i = 0;
1616 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001617 auxIndices[i] = state.f0;
1618 auxIndices[i + 1] = state.f1;
1619 auxIndices[i + 2] = state.f1;
1620 auxIndices[i + 3] = state.f2;
1621 auxIndices[i + 4] = state.f2;
1622 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001623 i += 6;
1624 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001625 } else {
dandov32a311b2014-07-15 19:46:26 -07001626 outIndices = indices;
1627 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001628
dandov32a311b2014-07-15 19:46:26 -07001629 if (NULL == texs || NULL == paint.getShader()) {
bsalomonbed83a62015-04-15 14:18:34 -07001630 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1631 SkColor2GrColor(paint.getColor()),
1632 NULL == colors, &grPaint)) {
1633 return;
1634 }
dandov32a311b2014-07-15 19:46:26 -07001635 } else {
bsalomonbed83a62015-04-15 14:18:34 -07001636 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix,
1637 NULL == colors, &grPaint)) {
1638 return;
1639 }
dandov32a311b2014-07-15 19:46:26 -07001640 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001641 }
1642
mtklein2583b622014-06-04 08:20:41 -07001643#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001644 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001645 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1646 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001647 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001648 }
1649 }
mtklein2583b622014-06-04 08:20:41 -07001650#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001651
1652 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001653 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001654 // need to convert byte order and from non-PM to PM
1655 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001656 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001657 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001658 color = colors[i];
1659 if (paint.getAlpha() != 255) {
1660 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1661 }
1662 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001663 }
1664 colors = convertedColors.get();
1665 }
robertphillipsea461502015-05-26 11:38:03 -07001666 fDrawContext->drawVertices(fRenderTarget,
1667 fClip,
1668 grPaint,
1669 *draw.fMatrix,
1670 primType,
1671 vertexCount,
1672 vertices,
1673 texs,
1674 colors,
1675 outIndices,
1676 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001677}
1678
1679///////////////////////////////////////////////////////////////////////////////
1680
reedca109532015-06-25 16:25:25 -07001681static void append_quad_indices(uint16_t indices[], int quadIndex) {
1682 int i = quadIndex * 4;
1683 indices[0] = i + 0; indices[1] = i + 1; indices[2] = i + 2;
1684 indices[3] = i + 2; indices[4] = i + 3; indices[5] = i + 0;
1685}
1686
1687void SkGpuDevice::drawAtlas(const SkDraw& d, const SkImage* atlas, const SkRSXform xform[],
1688 const SkRect texRect[], const SkColor colors[], int count,
1689 SkXfermode::Mode mode, const SkPaint& paint) {
1690 if (paint.isAntiAlias()) {
1691 this->INHERITED::drawAtlas(d, atlas, xform, texRect, colors, count, mode, paint);
1692 return;
1693 }
1694
1695 SkPaint p(paint);
1696 p.setShader(atlas->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
1697
1698 const int vertCount = count * 4;
1699 const int indexCount = count * 6;
1700 SkAutoTMalloc<SkPoint> vertStorage(vertCount * 2);
1701 SkPoint* verts = vertStorage.get();
1702 SkPoint* texs = verts + vertCount;
1703 SkAutoTMalloc<uint16_t> indexStorage(indexCount);
1704 uint16_t* indices = indexStorage.get();
1705 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(mode));
1706
1707 for (int i = 0; i < count; ++i) {
1708 xform[i].toQuad(texRect[i].width(), texRect[i].height(), verts);
1709 texRect[i].toQuad(texs);
1710 append_quad_indices(indices, i);
1711 verts += 4;
1712 texs += 4;
1713 indices += 6;
1714 }
1715
1716 verts = vertStorage.get();
1717 texs = verts + vertCount;
1718 indices = indexStorage.get();
1719 this->drawVertices(d, SkCanvas::kTriangles_VertexMode, vertCount, verts, texs, colors, xfer,
1720 indices, indexCount, p);
1721}
1722
1723///////////////////////////////////////////////////////////////////////////////
1724
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001725void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001726 size_t byteLength, SkScalar x, SkScalar y,
1727 const SkPaint& paint) {
1728 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001729 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001730
jvanverth8c27a182014-10-14 08:45:50 -07001731 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001732 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1733 return;
1734 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001735
jvanverth8c27a182014-10-14 08:45:50 -07001736 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001737
robertphillips2334fb62015-06-17 05:43:33 -07001738 fDrawContext->drawText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001739 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001740}
1741
fmalita05c4a432014-09-29 06:29:53 -07001742void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1743 const SkScalar pos[], int scalarsPerPos,
1744 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001745 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001746 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001747
jvanverth8c27a182014-10-14 08:45:50 -07001748 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001749 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1750 return;
1751 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001752
jvanverth8c27a182014-10-14 08:45:50 -07001753 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001754
robertphillips2334fb62015-06-17 05:43:33 -07001755 fDrawContext->drawPosText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001756 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1757 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001758}
1759
joshualitt9c328182015-03-23 08:13:04 -07001760void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1761 const SkPaint& paint, SkDrawFilter* drawFilter) {
1762 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawTextBlob", fContext);
1763 CHECK_SHOULD_DRAW(draw);
1764
1765 SkDEBUGCODE(this->validate();)
1766
robertphillips2334fb62015-06-17 05:43:33 -07001767 fDrawContext->drawTextBlob(fRenderTarget, fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001768 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001769}
1770
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001771///////////////////////////////////////////////////////////////////////////////
1772
reedb2db8982014-11-13 12:41:02 -08001773bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
robertphillips9c240a12015-05-28 07:45:59 -07001774 return GrTextContext::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001775}
1776
1777void SkGpuDevice::flush() {
1778 DO_DEFERRED_CLEAR();
bsalomonc49e8682015-06-30 11:37:35 -07001779 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001780}
1781
1782///////////////////////////////////////////////////////////////////////////////
1783
reed76033be2015-03-14 10:54:31 -07001784SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
bsalomonf2703d82014-10-28 14:33:06 -07001785 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001786 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001787 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001788 desc.fWidth = cinfo.fInfo.width();
1789 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001790 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001791
1792 SkAutoTUnref<GrTexture> texture;
1793 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001794 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001795
hcm4396fa52014-10-27 21:43:30 -07001796 // layers are never draw in repeat modes, so we can request an approx
1797 // match and ignore any padding.
bsalomond309e7a2015-04-30 14:18:54 -07001798 const GrTextureProvider::ScratchTexMatch match = (kNever_TileUsage == cinfo.fTileUsage) ?
1799 GrTextureProvider::kApprox_ScratchTexMatch :
1800 GrTextureProvider::kExact_ScratchTexMatch;
1801 texture.reset(fContext->textureProvider()->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001802
1803 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001804 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001805 return SkGpuDevice::Create(
bsalomon74f681d2015-06-23 14:38:48 -07001806 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, init);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001807 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001808 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001809 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001810 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001811 return NULL;
1812 }
1813}
1814
reed4a8126e2014-09-22 07:29:03 -07001815SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001816 // TODO: Change the signature of newSurface to take a budgeted parameter.
1817 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
vbuzinovdded6962015-06-12 08:59:45 -07001818 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
bsalomonafe30052015-01-16 07:32:33 -08001819 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001820}
1821
robertphillips30d2cc62014-09-24 08:52:18 -07001822bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001823 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001824#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001825 // todo: should handle this natively
1826 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001827 return false;
1828 }
1829
mtklein9db912c2015-05-19 11:11:26 -07001830 const SkBigPicture::AccelData* data = NULL;
1831 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1832 data = bp->accelData();
1833 }
robertphillips81f71b62014-11-11 04:54:49 -08001834 if (!data) {
1835 return false;
1836 }
1837
robertphillipse5524cd2015-02-20 12:30:26 -08001838 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1839 if (0 == gpuData->numBlocks()) {
1840 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001841 }
1842
robertphillipsfd61ed02014-10-28 07:21:44 -07001843 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001844
robertphillipse5524cd2015-02-20 12:30:26 -08001845 SkIRect iBounds;
1846 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1847 return false;
1848 }
1849
1850 SkRect clipBounds = SkRect::Make(iBounds);
1851
1852 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1853
robertphillipsfd61ed02014-10-28 07:21:44 -07001854 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001855 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001856 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001857 &atlasedNeedRendering, &atlasedRecycled,
vbuzinovdded6962015-06-12 08:59:45 -07001858 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001859
1860 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1861
1862 SkTDArray<GrHoistedLayer> needRendering, recycled;
1863
robertphillipse5524cd2015-02-20 12:30:26 -08001864 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1865
robertphillipsfd61ed02014-10-28 07:21:44 -07001866 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001867 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001868 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001869 &needRendering, &recycled,
vbuzinovdded6962015-06-12 08:59:45 -07001870 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001871
1872 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001873
robertphillips64bf7672014-08-21 13:07:35 -07001874 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001875 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1876 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001877
robertphillipsfd61ed02014-10-28 07:21:44 -07001878 GrLayerHoister::UnlockLayers(fContext, needRendering);
1879 GrLayerHoister::UnlockLayers(fContext, recycled);
1880 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1881 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001882
1883 return true;
robertphillips63242d72014-12-04 08:31:02 -08001884#else
1885 return false;
1886#endif
robertphillips64bf7672014-08-21 13:07:35 -07001887}
1888
senorblancobe129b22014-08-08 07:14:35 -07001889SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001890 // We always return a transient cache, so it is freed after each
1891 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001892 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001893}
reedf037e0b2014-10-30 11:34:15 -07001894
1895#endif