blob: d897f3ce88ff4686c97c65b4f4edbca175c3825e [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"
joshualitta61c8172015-08-17 10:51:22 -070042#include "batches/GrRectBatchFactory.h"
kkinnunenabcfab42015-02-22 22:53:44 -080043#include "effects/GrBicubicEffect.h"
44#include "effects/GrDashingEffect.h"
45#include "effects/GrSimpleTextureEffect.h"
46#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000047
reedf037e0b2014-10-30 11:34:15 -070048#if SK_SUPPORT_GPU
49
senorblanco55b6d8b2014-07-30 11:26:46 -070050enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
51
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000052#if 0
53 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080054 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000055 do { \
56 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080057 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058 } while (0)
59#else
joshualitt5531d512014-12-17 15:50:11 -080060 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000061#endif
62
63// This constant represents the screen alignment criterion in texels for
64// requiring texture domain clamping to prevent color bleeding when drawing
65// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000066#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000067
68#define DO_DEFERRED_CLEAR() \
69 do { \
bsalomonafe30052015-01-16 07:32:33 -080070 if (fNeedClear) { \
71 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000072 } \
73 } while (false) \
74
75///////////////////////////////////////////////////////////////////////////////
76
77#define CHECK_FOR_ANNOTATION(paint) \
78 do { if (paint.getAnnotation()) { return; } } while (0)
79
80///////////////////////////////////////////////////////////////////////////////
81
bsalomonbcf0a522014-10-08 08:40:09 -070082// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
83// just accesses the backing GrTexture. Otherwise, it creates a cached texture
84// representation and releases it in the destructor.
85class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040086public:
bsalomonbcf0a522014-10-08 08:40:09 -070087 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070088
bsalomonbcf0a522014-10-08 08:40:09 -070089 AutoBitmapTexture(GrContext* context,
90 const SkBitmap& bitmap,
91 const GrTextureParams* params,
92 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040093 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070094 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040095 }
96
bsalomonbcf0a522014-10-08 08:40:09 -070097 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040098 const SkBitmap& bitmap,
99 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -0700100 // Either get the texture directly from the bitmap, or else use the cache and
101 // remember to unref it.
102 if (GrTexture* bmpTexture = bitmap.getTexture()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700103 fTexture.reset(nullptr);
bsalomonbcf0a522014-10-08 08:40:09 -0700104 return bmpTexture;
105 } else {
106 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
107 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400108 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400109 }
110
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000111private:
bsalomonbcf0a522014-10-08 08:40:09 -0700112 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000113};
114
115///////////////////////////////////////////////////////////////////////////////
116
117struct GrSkDrawProcs : public SkDrawProcs {
118public:
119 GrContext* fContext;
120 GrTextContext* fTextContext;
121 GrFontScaler* fFontScaler; // cached in the skia glyphcache
122};
123
124///////////////////////////////////////////////////////////////////////////////
125
bsalomon74f681d2015-06-23 14:38:48 -0700126/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
127 should fail. */
128bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
129 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
130 *flags = 0;
131 if (info) {
132 switch (info->alphaType()) {
133 case kPremul_SkAlphaType:
134 break;
135 case kOpaque_SkAlphaType:
136 *flags |= SkGpuDevice::kIsOpaque_Flag;
137 break;
138 default: // If it is unpremul or unknown don't try to render
139 return false;
140 }
141 }
142 if (kClear_InitContents == init) {
143 *flags |= kNeedClear_Flag;
144 }
145 return true;
146}
147
148SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props,
149 InitContents init) {
150 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, init);
senorblancod0d37ca2015-04-02 04:54:56 -0700151}
152
153SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
bsalomon74f681d2015-06-23 14:38:48 -0700154 const SkSurfaceProps* props, InitContents init) {
bsalomonafe30052015-01-16 07:32:33 -0800155 if (!rt || rt->wasDestroyed()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700156 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000157 }
bsalomon74f681d2015-06-23 14:38:48 -0700158 unsigned flags;
halcanary96fcdcc2015-08-27 07:41:13 -0700159 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
160 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700161 }
halcanary385fe4d2015-08-26 13:07:48 -0700162 return new SkGpuDevice(rt, width, height, props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000163}
164
bsalomon74f681d2015-06-23 14:38:48 -0700165SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
166 const SkImageInfo& info, int sampleCount,
167 const SkSurfaceProps* props, InitContents init) {
168 unsigned flags;
169 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700170 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700171 }
172
173 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
halcanary96fcdcc2015-08-27 07:41:13 -0700174 if (nullptr == rt) {
175 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700176 }
177
halcanary385fe4d2015-08-26 13:07:48 -0700178 return new SkGpuDevice(rt, info.width(), info.height(), props, flags);
bsalomon74f681d2015-06-23 14:38:48 -0700179}
180
senorblancod0d37ca2015-04-02 04:54:56 -0700181SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
182 const SkSurfaceProps* props, unsigned flags)
robertphillipsfcf78292015-06-19 11:49:52 -0700183 : INHERITED(SkSurfacePropsCopyOrDefault(props))
reedb2db8982014-11-13 12:41:02 -0800184{
halcanary96fcdcc2015-08-27 07:41:13 -0700185 fDrawProcs = nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000186
bsalomonafe30052015-01-16 07:32:33 -0800187 fContext = SkRef(rt->getContext());
bsalomon74f681d2015-06-23 14:38:48 -0700188 fNeedClear = SkToBool(flags & kNeedClear_Flag);
189 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000190
bsalomonafe30052015-01-16 07:32:33 -0800191 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000192
bsalomon74f681d2015-06-23 14:38:48 -0700193 SkAlphaType at = fOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
194 SkImageInfo info = rt->surfacePriv().info(at).makeWH(width, height);
halcanary385fe4d2015-08-26 13:07:48 -0700195 SkPixelRef* pr = new SkGrPixelRef(info, rt);
bsalomonafbf2d62014-09-30 12:18:44 -0700196 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700197 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700198
robertphillipsc9a37062015-09-01 08:34:28 -0700199 fDrawContext.reset(fContext->drawContext(&this->surfaceProps()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000200}
201
kkinnunenabcfab42015-02-22 22:53:44 -0800202GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
203 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000204 if (kUnknown_SkColorType == origInfo.colorType() ||
205 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700206 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000207 }
208
bsalomonafe30052015-01-16 07:32:33 -0800209 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700210 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800211 }
212
reede5ea5002014-09-03 11:54:58 -0700213 SkColorType ct = origInfo.colorType();
214 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700215 if (kRGB_565_SkColorType == ct) {
216 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800217 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
218 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700219 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800220 }
221 if (kOpaque_SkAlphaType != at) {
222 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000223 }
reede5ea5002014-09-03 11:54:58 -0700224 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000225
bsalomonf2703d82014-10-28 14:33:06 -0700226 GrSurfaceDesc desc;
227 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000228 desc.fWidth = info.width();
229 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000230 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000231 desc.fSampleCnt = sampleCount;
bsalomond309e7a2015-04-30 14:18:54 -0700232 GrTexture* texture = context->textureProvider()->createTexture(
halcanary96fcdcc2015-08-27 07:41:13 -0700233 desc, SkToBool(budgeted), nullptr, 0);
234 if (nullptr == texture) {
235 return nullptr;
kkinnunenabcfab42015-02-22 22:53:44 -0800236 }
halcanary96fcdcc2015-08-27 07:41:13 -0700237 SkASSERT(nullptr != texture->asRenderTarget());
kkinnunenabcfab42015-02-22 22:53:44 -0800238 return texture->asRenderTarget();
239}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000240
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000241SkGpuDevice::~SkGpuDevice() {
242 if (fDrawProcs) {
243 delete fDrawProcs;
244 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000245
bsalomon32d0b3b2014-08-29 07:50:23 -0700246 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000247 fContext->unref();
248}
249
250///////////////////////////////////////////////////////////////////////////////
251
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000252bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
253 int x, int y) {
254 DO_DEFERRED_CLEAR();
255
256 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000257 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000258 if (kUnknown_GrPixelConfig == config) {
259 return false;
260 }
261
262 uint32_t flags = 0;
263 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
264 flags = GrContext::kUnpremul_PixelOpsFlag;
265 }
bsalomon74f681d2015-06-23 14:38:48 -0700266 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
267 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000268}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000269
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000270bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
271 int x, int y) {
272 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000273 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000274 if (kUnknown_GrPixelConfig == config) {
275 return false;
276 }
277 uint32_t flags = 0;
278 if (kUnpremul_SkAlphaType == info.alphaType()) {
279 flags = GrContext::kUnpremul_PixelOpsFlag;
280 }
281 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
282
283 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700284 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000285
286 return true;
287}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000288
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000289const SkBitmap& SkGpuDevice::onAccessBitmap() {
290 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700291 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000292}
293
reed41e010c2015-06-09 12:16:53 -0700294bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
295 DO_DEFERRED_CLEAR();
296 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
297 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
298 // ... ugh.
299 fLegacyBitmap.notifyPixelsChanged();
300 return false;
301}
302
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000303void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
304 INHERITED::onAttachToCanvas(canvas);
305
306 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800307 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000308}
309
310void SkGpuDevice::onDetachFromCanvas() {
311 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800312 fClip.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700313 fClipStack.reset(nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000314}
315
316// call this every draw call, to ensure that the context reflects our state,
317// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800318void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800319 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000320
joshualitt44701df2015-02-23 14:44:57 -0800321 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000322
joshualitt570d2f82015-02-25 13:19:48 -0800323 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000324
325 DO_DEFERRED_CLEAR();
326}
327
328GrRenderTarget* SkGpuDevice::accessRenderTarget() {
robertphillips7156b092015-05-14 08:54:12 -0700329 DO_DEFERRED_CLEAR();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000330 return fRenderTarget;
331}
332
reed8eddfb52014-12-04 07:50:14 -0800333void SkGpuDevice::clearAll() {
334 GrColor color = 0;
335 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
336 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillipsea461502015-05-26 11:38:03 -0700337 fDrawContext->clear(fRenderTarget, &rect, color, true);
bsalomonafe30052015-01-16 07:32:33 -0800338 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800339}
340
kkinnunenabcfab42015-02-22 22:53:44 -0800341void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
342 // Caller must have accessed the render target, because it knows the rt must be replaced.
343 SkASSERT(!fNeedClear);
344
345 SkSurface::Budgeted budgeted =
346 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
347 : SkSurface::kNo_Budgeted;
348
349 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
vbuzinovdded6962015-06-12 08:59:45 -0700350 fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt));
kkinnunenabcfab42015-02-22 22:53:44 -0800351
halcanary96fcdcc2015-08-27 07:41:13 -0700352 if (nullptr == newRT) {
kkinnunenabcfab42015-02-22 22:53:44 -0800353 return;
354 }
355
356 if (shouldRetainContent) {
357 if (fRenderTarget->wasDestroyed()) {
358 return;
359 }
360 this->context()->copySurface(newRT, fRenderTarget);
361 }
362
363 SkASSERT(fRenderTarget != newRT);
364
365 fRenderTarget->unref();
366 fRenderTarget = newRT.detach();
367
bsalomon74f681d2015-06-23 14:38:48 -0700368#ifdef SK_DEBUG
369 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
370 kPremul_SkAlphaType);
371 SkASSERT(info == fLegacyBitmap.info());
372#endif
halcanary385fe4d2015-08-26 13:07:48 -0700373 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget);
kkinnunenabcfab42015-02-22 22:53:44 -0800374 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700375
robertphillipsc9a37062015-09-01 08:34:28 -0700376 fDrawContext.reset(fRenderTarget->getContext()->drawContext(&this->surfaceProps()));
kkinnunenabcfab42015-02-22 22:53:44 -0800377}
378
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000379///////////////////////////////////////////////////////////////////////////////
380
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000381void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800382 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700383 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000384
385 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700386 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
387 return;
388 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000389
robertphillipsea461502015-05-26 11:38:03 -0700390 fDrawContext->drawPaint(fRenderTarget, fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000391}
392
393// must be in SkCanvas::PointMode order
394static const GrPrimitiveType gPointMode2PrimtiveType[] = {
395 kPoints_GrPrimitiveType,
396 kLines_GrPrimitiveType,
397 kLineStrip_GrPrimitiveType
398};
399
ethannicholas330bb952015-07-17 06:44:02 -0700400// suppress antialiasing on axis-aligned integer-coordinate lines
401static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
402 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
403 return false;
404 }
405 if (count == 2) {
406 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
407 // the other coordinates are not. This does mean the two end pixels of the line will be
408 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
409 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
410 // more complete fix is possible down the road, for the time being we accept the error on
411 // the two end pixels as being the lesser of two evils.
412 if (pts[0].fX == pts[1].fX) {
413 return ((int) pts[0].fX) != pts[0].fX;
414 }
415 if (pts[0].fY == pts[1].fY) {
416 return ((int) pts[0].fY) != pts[0].fY;
417 }
418 }
419 return true;
420}
421
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000422void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
423 size_t count, const SkPoint pts[], const SkPaint& paint) {
424 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800425 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000426
427 SkScalar width = paint.getStrokeWidth();
428 if (width < 0) {
429 return;
430 }
431
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000432 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700433 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
434 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700435 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
436 &grPaint)) {
437 return;
438 }
egdaniele61c4112014-06-12 10:24:21 -0700439 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700440 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700441 path.moveTo(pts[0]);
442 path.lineTo(pts[1]);
robertphillipsea461502015-05-26 11:38:03 -0700443 fDrawContext->drawPath(fRenderTarget, fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700444 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000445 }
446
ethannicholas330bb952015-07-17 06:44:02 -0700447 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000448 // else we let the SkDraw call our drawPath()
ethannicholas330bb952015-07-17 06:44:02 -0700449 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
450 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000451 draw.drawPoints(mode, count, pts, paint, true);
452 return;
453 }
454
455 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700456 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
457 return;
458 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000459
robertphillipsea461502015-05-26 11:38:03 -0700460 fDrawContext->drawVertices(fRenderTarget,
461 fClip,
462 grPaint,
463 *draw.fMatrix,
464 gPointMode2PrimtiveType[mode],
465 SkToS32(count),
466 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700467 nullptr,
468 nullptr,
469 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700470 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000471}
472
473///////////////////////////////////////////////////////////////////////////////
474
475void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
476 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700477 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
478
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000479 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800480 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000481
482 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
483 SkScalar width = paint.getStrokeWidth();
484
485 /*
486 We have special code for hairline strokes, miter-strokes, bevel-stroke
487 and fills. Anything else we just call our path code.
488 */
489 bool usePath = doStroke && width > 0 &&
490 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
491 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
egdanield58a0ba2014-06-11 10:30:05 -0700492
robertphillipsd8aa59d2015-08-05 09:07:12 -0700493 // a few other reasons we might need to call drawPath...
494 if (paint.getMaskFilter() ||
495 paint.getStyle() == SkPaint::kStrokeAndFill_Style) { // we can't both stroke and fill rects
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000496 usePath = true;
497 }
egdanield58a0ba2014-06-11 10:30:05 -0700498
joshualitt5531d512014-12-17 15:50:11 -0800499 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000500 usePath = true;
501 }
502
egdanield58a0ba2014-06-11 10:30:05 -0700503 GrStrokeInfo strokeInfo(paint);
504
505 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700506 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700507 usePath = true;
508 }
509
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000510 if (usePath) {
511 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700512 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000513 path.addRect(rect);
halcanary96fcdcc2015-08-27 07:41:13 -0700514 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000515 return;
516 }
517
518 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700519 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
520 return;
521 }
Mike Klein744fb732014-06-23 15:13:26 -0400522
robertphillipsea461502015-05-26 11:38:03 -0700523 fDrawContext->drawRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000524}
525
526///////////////////////////////////////////////////////////////////////////////
527
528void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800529 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700530 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000531 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800532 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000533
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000534 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700535 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
536 return;
537 }
Mike Klein744fb732014-06-23 15:13:26 -0400538
egdanield58a0ba2014-06-11 10:30:05 -0700539 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000540 if (paint.getMaskFilter()) {
541 // try to hit the fast path for drawing filtered round rects
542
543 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800544 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000545 if (devRRect.allCornersCircular()) {
546 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700547 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
joshualitt5531d512014-12-17 15:50:11 -0800548 draw.fClip->getBounds(),
549 *draw.fMatrix,
550 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000551 SkIRect finalIRect;
552 maskRect.roundOut(&finalIRect);
553 if (draw.fClip->quickReject(finalIRect)) {
554 // clipped out
555 return;
556 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700557 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
558 fDrawContext,
joshualitt25d9c152015-02-18 12:29:52 -0800559 fRenderTarget,
560 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800561 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800562 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700563 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700564 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000565 return;
566 }
567 }
568
569 }
570 }
571
572 }
573
egdanield58a0ba2014-06-11 10:30:05 -0700574 bool usePath = false;
575
576 if (paint.getMaskFilter()) {
577 usePath = true;
578 } else {
579 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700580 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700581 usePath = true;
582 }
583 }
584
585
586 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000587 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700588 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000589 path.addRRect(rect);
halcanary96fcdcc2015-08-27 07:41:13 -0700590 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000591 return;
592 }
Mike Klein744fb732014-06-23 15:13:26 -0400593
robertphillipsea461502015-05-26 11:38:03 -0700594 fDrawContext->drawRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000595}
596
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000597void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800598 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000599 SkStrokeRec stroke(paint);
600 if (stroke.isFillStyle()) {
601
602 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800603 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000604
605 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700606 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
607 &grPaint)) {
608 return;
609 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000610
halcanary96fcdcc2015-08-27 07:41:13 -0700611 if (nullptr == paint.getMaskFilter() && nullptr == paint.getPathEffect()) {
robertphillipsea461502015-05-26 11:38:03 -0700612 fDrawContext->drawDRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000613 return;
614 }
615 }
616
617 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700618 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000619 path.addRRect(outer);
620 path.addRRect(inner);
621 path.setFillType(SkPath::kEvenOdd_FillType);
622
halcanary96fcdcc2015-08-27 07:41:13 -0700623 this->drawPath(draw, path, paint, nullptr, true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000624}
625
626
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000627/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000628
629void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
630 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700631 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000632 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800633 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000634
egdanield58a0ba2014-06-11 10:30:05 -0700635 GrStrokeInfo strokeInfo(paint);
636
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000637 bool usePath = false;
638 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700639 if (paint.getMaskFilter()) {
robertphillips30c4cae2015-09-15 10:20:55 -0700640 // The RRect path can handle special case blurring
641 SkRRect rr = SkRRect::MakeOval(oval);
642 return this->drawRRect(draw, rr, paint);
egdanield58a0ba2014-06-11 10:30:05 -0700643 } else {
644 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700645 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700646 usePath = true;
647 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000648 }
649
650 if (usePath) {
651 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700652 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000653 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700654 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000655 return;
656 }
657
658 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700659 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
660 return;
661 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000662
robertphillipsea461502015-05-26 11:38:03 -0700663 fDrawContext->drawOval(fRenderTarget, fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000664}
665
666#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000667
668///////////////////////////////////////////////////////////////////////////////
669
robertphillipsccb1b572015-05-27 11:02:55 -0700670static SkBitmap wrap_texture(GrTexture* texture, int width, int height) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000671 SkBitmap result;
senorblancod0d37ca2015-04-02 04:54:56 -0700672 result.setInfo(SkImageInfo::MakeN32Premul(width, height));
halcanary385fe4d2015-08-26 13:07:48 -0700673 result.setPixelRef(new SkGrPixelRef(result.info(), texture))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000674 return result;
675}
676
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000677void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
678 const SkPaint& paint, const SkMatrix* prePathMatrix,
679 bool pathIsMutable) {
680 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800681 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700682 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000683
robertphillipsccb1b572015-05-27 11:02:55 -0700684 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, fRenderTarget,
685 fClip, origSrcPath, paint,
686 *draw.fMatrix, prePathMatrix,
687 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000688}
689
690static const int kBmpSmallTileSize = 1 << 10;
691
692static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
693 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
694 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
695 return tilesX * tilesY;
696}
697
reed85d91782015-09-10 14:33:38 -0700698static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000699 if (maxTileSize <= kBmpSmallTileSize) {
700 return maxTileSize;
701 }
702
703 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
704 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
705
706 maxTileTotalTileSize *= maxTileSize * maxTileSize;
707 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
708
709 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
710 return kBmpSmallTileSize;
711 } else {
712 return maxTileSize;
713 }
714}
715
716// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
717// pixels from the bitmap are necessary.
bsalomon74f681d2015-06-23 14:38:48 -0700718static void determine_clipped_src_rect(const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800719 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800720 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700721 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000722 const SkRect* srcRectPtr,
723 SkIRect* clippedSrcIRect) {
halcanary96fcdcc2015-08-27 07:41:13 -0700724 clip.getConservativeBounds(rt, clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000725 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800726 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000727 clippedSrcIRect->setEmpty();
728 return;
729 }
730 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
731 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700732 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000733 // we've setup src space 0,0 to map to the top left of the src rect.
734 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000735 if (!clippedSrcRect.intersect(*srcRectPtr)) {
736 clippedSrcIRect->setEmpty();
737 return;
738 }
739 }
740 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700741 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742 if (!clippedSrcIRect->intersect(bmpBounds)) {
743 clippedSrcIRect->setEmpty();
744 }
745}
746
reed85d91782015-09-10 14:33:38 -0700747bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
748 const SkMatrix& viewMatrix,
749 const GrTextureParams& params,
750 const SkRect* srcRectPtr,
751 int maxTileSize,
752 int* tileSize,
753 SkIRect* clippedSubset) const {
754 // if it's larger than the max tile size, then we have no choice but tiling.
755 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
756 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(),
757 srcRectPtr, clippedSubset);
758 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
759 return true;
760 }
761
762 const size_t area = imageRect.width() * imageRect.height();
763 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
764 return false;
765 }
766
767 // if the entire image/bitmap is already in our cache then no reason to tile it
768 if (GrIsImageInCache(fContext, imageID, imageRect, nullptr, &params)) {
769 return false;
770 }
771
772 // At this point we know we could do the draw by uploading the entire bitmap
773 // as a texture. However, if the texture would be large compared to the
774 // cache size and we don't require most of it for this draw then tile to
775 // reduce the amount of upload and cache spill.
776
777 // assumption here is that sw bitmap size is a good proxy for its size as
778 // a texture
779 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
780 size_t cacheSize;
781 fContext->getResourceCacheLimits(nullptr, &cacheSize);
782 if (bmpSize < cacheSize / 2) {
783 return false;
784 }
785
786 // Figure out how much of the src we will need based on the src rect and clipping.
787 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(), srcRectPtr,
788 clippedSubset);
789 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
790 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
791 kBmpSmallTileSize * kBmpSmallTileSize;
792
793 return usedTileBytes < 2 * bmpSize;
794}
795
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000796bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800797 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000798 const GrTextureParams& params,
799 const SkRect* srcRectPtr,
800 int maxTileSize,
801 int* tileSize,
802 SkIRect* clippedSrcRect) const {
803 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700804 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000805 return false;
806 }
807
reed85d91782015-09-10 14:33:38 -0700808 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
809 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
810}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000811
reed85d91782015-09-10 14:33:38 -0700812bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
813 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
814 const SkMatrix& viewMatrix) const {
815 // if image is explictly texture backed then just use the texture
816 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000817 return false;
818 }
819
reed85d91782015-09-10 14:33:38 -0700820 GrTextureParams params;
821 bool doBicubic;
822 GrTextureParams::FilterMode textureFilterMode =
823 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
824
825 int tileFilterPad;
826 if (doBicubic) {
827 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
828 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
829 tileFilterPad = 0;
830 } else {
831 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000832 }
reed85d91782015-09-10 14:33:38 -0700833 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000834
reed85d91782015-09-10 14:33:38 -0700835 int maxTileSize = fContext->caps()->maxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000836
reed85d91782015-09-10 14:33:38 -0700837 // these are output, which we safely ignore, as we just want to know the predicate
838 int outTileSize;
839 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000840
reed85d91782015-09-10 14:33:38 -0700841 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
842 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000843}
844
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000845void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000846 const SkBitmap& bitmap,
847 const SkMatrix& m,
848 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000849 SkMatrix concat;
850 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
851 if (!m.isIdentity()) {
852 concat.setConcat(*draw->fMatrix, m);
853 draw.writable()->fMatrix = &concat;
854 }
halcanary96fcdcc2015-08-27 07:41:13 -0700855 this->drawBitmapCommon(*draw, bitmap, nullptr, nullptr, paint, SkCanvas::kStrict_SrcRectConstraint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000856}
857
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000858// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000859// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
860// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000861static inline void clamped_outset_with_offset(SkIRect* iRect,
862 int outset,
863 SkPoint* offset,
864 const SkIRect& clamp) {
865 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000866
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000867 int leftClampDelta = clamp.fLeft - iRect->fLeft;
868 if (leftClampDelta > 0) {
869 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000870 iRect->fLeft = clamp.fLeft;
871 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000872 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000873 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000874
875 int topClampDelta = clamp.fTop - iRect->fTop;
876 if (topClampDelta > 0) {
877 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000878 iRect->fTop = clamp.fTop;
879 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000880 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000881 }
882
883 if (iRect->fRight > clamp.fRight) {
884 iRect->fRight = clamp.fRight;
885 }
886 if (iRect->fBottom > clamp.fBottom) {
887 iRect->fBottom = clamp.fBottom;
888 }
889}
890
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000891static bool has_aligned_samples(const SkRect& srcRect,
892 const SkRect& transformedRect) {
893 // detect pixel disalignment
894 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
895 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
896 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
897 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
898 SkScalarAbs(transformedRect.width() - srcRect.width()) <
899 COLOR_BLEED_TOLERANCE &&
900 SkScalarAbs(transformedRect.height() - srcRect.height()) <
901 COLOR_BLEED_TOLERANCE) {
902 return true;
903 }
904 return false;
905}
906
907static bool may_color_bleed(const SkRect& srcRect,
908 const SkRect& transformedRect,
robertphillips1accc4c2015-07-20 10:22:29 -0700909 const SkMatrix& m,
910 bool isMSAA) {
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000911 // Only gets called if has_aligned_samples returned false.
912 // So we can assume that sampling is axis aligned but not texel aligned.
913 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
914 SkRect innerSrcRect(srcRect), innerTransformedRect,
915 outerTransformedRect(transformedRect);
robertphillips1accc4c2015-07-20 10:22:29 -0700916 if (isMSAA) {
917 innerSrcRect.inset(SK_Scalar1, SK_Scalar1);
918 } else {
919 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
920 }
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000921 m.mapRect(&innerTransformedRect, innerSrcRect);
922
923 // The gap between outerTransformedRect and innerTransformedRect
924 // represents the projection of the source border area, which is
925 // problematic for color bleeding. We must check whether any
926 // destination pixels sample the border area.
927 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
928 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
929 SkIRect outer, inner;
930 outerTransformedRect.round(&outer);
931 innerTransformedRect.round(&inner);
932 // If the inner and outer rects round to the same result, it means the
933 // border does not overlap any pixel centers. Yay!
934 return inner != outer;
935}
936
937static bool needs_texture_domain(const SkBitmap& bitmap,
938 const SkRect& srcRect,
939 GrTextureParams &params,
940 const SkMatrix& contextMatrix,
robertphillips1accc4c2015-07-20 10:22:29 -0700941 bool bicubic,
942 bool isMSAA) {
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000943 bool needsTextureDomain = false;
senorblancod0d37ca2015-04-02 04:54:56 -0700944 GrTexture* tex = bitmap.getTexture();
945 int width = tex ? tex->width() : bitmap.width();
946 int height = tex ? tex->height() : bitmap.height();
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000947
948 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
949 // Need texture domain if drawing a sub rect
senorblancod0d37ca2015-04-02 04:54:56 -0700950 needsTextureDomain = srcRect.width() < width ||
951 srcRect.height() < height;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000952 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
953 // sampling is axis-aligned
954 SkRect transformedRect;
955 contextMatrix.mapRect(&transformedRect, srcRect);
956
957 if (has_aligned_samples(srcRect, transformedRect)) {
958 params.setFilterMode(GrTextureParams::kNone_FilterMode);
959 needsTextureDomain = false;
960 } else {
robertphillips1accc4c2015-07-20 10:22:29 -0700961 needsTextureDomain = may_color_bleed(srcRect, transformedRect,
962 contextMatrix, isMSAA);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000963 }
964 }
965 }
966 return needsTextureDomain;
967}
968
joshualitta61c8172015-08-17 10:51:22 -0700969static void draw_aa_bitmap(GrDrawContext* drawContext, GrContext* context,
970 GrRenderTarget* renderTarget, const GrClip& clip,
971 const SkMatrix& viewMatrix, const SkMatrix& srcRectToDstRect,
972 const SkPaint& paint, const SkBitmap* bitmapPtr, const SkSize& dstSize) {
973 SkShader::TileMode tm[] = {
974 SkShader::kClamp_TileMode,
975 SkShader::kClamp_TileMode,
976 };
977
978 bool doBicubic;
979 GrTextureParams::FilterMode textureFilterMode =
980 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix,
981 srcRectToDstRect,
982 &doBicubic);
983
984 // Setup texture to wrap bitmap
985 GrTextureParams params(tm, textureFilterMode);
986 SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, *bitmapPtr, &params));
987
988 if (!texture) {
989 SkErrorInternals::SetError(kInternalError_SkError,
990 "Couldn't convert bitmap to texture.");
991 return;
992 }
993
994 // Setup paint
995 GrColor paintColor = (kAlpha_8_SkColorType == bitmapPtr->colorType()) ?
996 SkColor2GrColor(paint.getColor()) :
997 SkColor2GrColorJustAlpha(paint.getColor());
998
999 GrPaint grPaint;
1000
1001 // Create and insert texture effect
1002 SkAutoTUnref<const GrFragmentProcessor> fp;
1003 if (doBicubic) {
1004 fp.reset(GrBicubicEffect::Create(grPaint.getProcessorDataManager(), texture,
1005 SkMatrix::I(),
1006 tm));
1007 } else {
1008 fp.reset(GrSimpleTextureEffect::Create(grPaint.getProcessorDataManager(), texture,
1009 SkMatrix::I(), params));
1010 }
1011
1012 // The bitmap read has to be first
bsalomonac856c92015-08-27 06:30:17 -07001013 grPaint.addColorFragmentProcessor(fp);
joshualitta61c8172015-08-17 10:51:22 -07001014 if (!SkPaint2GrPaintNoShader(context, renderTarget, paint, SkColor2GrColor(paint.getColor()),
1015 false, &grPaint)) {
1016 return;
1017 }
1018
1019 grPaint.setColor(paintColor);
1020
1021 // Setup dst rect and final matrix
1022 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1023
1024 SkRect devRect;
1025 viewMatrix.mapRect(&devRect, dstRect);
1026
1027 SkMatrix matrix;
1028 matrix.setIDiv(bitmapPtr->width(), bitmapPtr->height());
1029
1030 SkMatrix dstRectToSrcRect;
1031 if (!srcRectToDstRect.invert(&dstRectToSrcRect)) {
1032 return;
1033 }
1034 matrix.preConcat(dstRectToSrcRect);
1035
joshualittd2b23e02015-08-21 10:53:34 -07001036 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFill(grPaint.getColor(),
joshualitta61c8172015-08-17 10:51:22 -07001037 viewMatrix,
1038 matrix,
1039 dstRect,
1040 devRect));
1041
1042 drawContext->drawBatch(renderTarget, clip, grPaint, batch);
1043}
1044
reed3322a812015-09-16 10:09:24 -07001045static bool can_ignore_strict_subset_constraint(const SkBitmap& bitmap, const SkRect& subset) {
1046 GrTexture* tex = bitmap.getTexture();
1047 int width = tex ? tex->width() : bitmap.width();
1048 int height = tex ? tex->height() : bitmap.height();
1049 return subset.contains(SkRect::MakeIWH(width, height));
1050}
1051
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001052void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1053 const SkBitmap& bitmap,
1054 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001055 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001056 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001057 SkCanvas::SrcRectConstraint constraint) {
joshualitt5531d512014-12-17 15:50:11 -08001058 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001059
1060 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001061 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001062 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1063 // in the (easier) bleed case, so update flags.
halcanary96fcdcc2015-08-27 07:41:13 -07001064 if (nullptr == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001065 SkScalar w = SkIntToScalar(bitmap.width());
1066 SkScalar h = SkIntToScalar(bitmap.height());
1067 dstSize.fWidth = w;
1068 dstSize.fHeight = h;
1069 srcRect.set(0, 0, w, h);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001070 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001071 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001072 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001073 dstSize = *dstSizePtr;
senorblancod0d37ca2015-04-02 04:54:56 -07001074 }
reed3322a812015-09-16 10:09:24 -07001075
1076 if (can_ignore_strict_subset_constraint(bitmap, srcRect)) {
reeda5517e22015-07-14 10:54:12 -07001077 constraint = SkCanvas::kFast_SrcRectConstraint;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001078 }
1079
derekf367e1862014-12-02 11:02:06 -08001080 // If the render target is not msaa and draw is antialiased, we call
1081 // drawRect instead of drawing on the render target directly.
1082 // FIXME: the tiled bitmap code path doesn't currently support
1083 // anti-aliased edges, we work around that for now by drawing directly
1084 // if the image size exceeds maximum texture size.
bsalomon76228632015-05-29 08:02:10 -07001085 int maxTextureSize = fContext->caps()->maxTextureSize();
joshualitt1a899c92015-08-17 11:53:44 -07001086 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
1087 paint.isAntiAlias() &&
1088 bitmap.width() <= maxTextureSize &&
1089 bitmap.height() <= maxTextureSize;
derekf367e1862014-12-02 11:02:06 -08001090
joshualitt1a899c92015-08-17 11:53:44 -07001091 if (paint.getMaskFilter() || drawAA) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001092 // Convert the bitmap to a shader so that the rect can be drawn
1093 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001094 SkBitmap tmp; // subset of bitmap, if necessary
1095 const SkBitmap* bitmapPtr = &bitmap;
joshualitta61c8172015-08-17 10:51:22 -07001096 SkMatrix srcRectToDstRect;
bsalomon49f085d2014-09-05 13:34:00 -07001097 if (srcRectPtr) {
joshualitta61c8172015-08-17 10:51:22 -07001098 srcRectToDstRect.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1099 srcRectToDstRect.postScale(dstSize.fWidth / srcRectPtr->width(),
1100 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001101 // In bleed mode we position and trim the bitmap based on the src rect which is
1102 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1103 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1104 // compensate.
reeda5517e22015-07-14 10:54:12 -07001105 if (SkCanvas::kStrict_SrcRectConstraint == constraint) {
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001106 SkIRect iSrc;
1107 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001108
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001109 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1110 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001111
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001112 if (!bitmap.extractSubset(&tmp, iSrc)) {
1113 return; // extraction failed
1114 }
1115 bitmapPtr = &tmp;
1116 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001117
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001118 // The source rect has changed so update the matrix
joshualitta61c8172015-08-17 10:51:22 -07001119 srcRectToDstRect.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001120 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001121 } else {
joshualitta61c8172015-08-17 10:51:22 -07001122 srcRectToDstRect.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001123 }
1124
joshualitta61c8172015-08-17 10:51:22 -07001125 // If we have a maskfilter then we can't batch, so we take a slow path. However, we fast
1126 // path the case where we are drawing an AA rect so we can batch many drawImageRect calls
1127 if (paint.getMaskFilter()) {
1128 SkPaint paintWithShader(paint);
1129 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
1130 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
1131 &srcRectToDstRect))->unref();
1132 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1133 this->drawRect(draw, dstRect, paintWithShader);
1134 } else {
1135 draw_aa_bitmap(fDrawContext, fContext, fRenderTarget, fClip, *draw.fMatrix,
1136 srcRectToDstRect, paint, bitmapPtr, dstSize);
1137 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001138
1139 return;
1140 }
1141
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001142 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1143 // the view matrix rather than a local matrix.
joshualitt5531d512014-12-17 15:50:11 -08001144 SkMatrix viewM = *draw.fMatrix;
reed85d91782015-09-10 14:33:38 -07001145 viewM.preScale(dstSize.fWidth / srcRect.width(),
1146 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001147
1148 GrTextureParams params;
joshualitt9bc39542015-08-12 12:57:54 -07001149 bool doBicubic;
1150 GrTextureParams::FilterMode textureFilterMode =
1151 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewM, SkMatrix::I(),
1152 &doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001153
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001154 int tileFilterPad;
1155 if (doBicubic) {
1156 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1157 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1158 tileFilterPad = 0;
1159 } else {
1160 tileFilterPad = 1;
1161 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001162 params.setFilterMode(textureFilterMode);
1163
bsalomon76228632015-05-29 08:02:10 -07001164 int maxTileSize = fContext->caps()->maxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001165 int tileSize;
1166
1167 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001168 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001169 &clippedSrcRect)) {
reeda5517e22015-07-14 10:54:12 -07001170 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, constraint,
joshualitt5531d512014-12-17 15:50:11 -08001171 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001172 } else {
1173 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001174 bool needsTextureDomain = needs_texture_domain(bitmap,
1175 srcRect,
1176 params,
joshualitt5531d512014-12-17 15:50:11 -08001177 viewM,
robertphillips1accc4c2015-07-20 10:22:29 -07001178 doBicubic,
1179 fRenderTarget->isUnifiedMultisampled());
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001180 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001181 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001182 srcRect,
1183 params,
1184 paint,
reeda5517e22015-07-14 10:54:12 -07001185 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001186 doBicubic,
1187 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001188 }
1189}
1190
1191// Break 'bitmap' into several tiles to draw it since it has already
1192// been determined to be too large to fit in VRAM
1193void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001194 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195 const SkRect& srcRect,
1196 const SkIRect& clippedSrcIRect,
1197 const GrTextureParams& params,
1198 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001199 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001200 int tileSize,
1201 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001202 // The following pixel lock is technically redundant, but it is desirable
1203 // to lock outside of the tile loop to prevent redecoding the whole image
1204 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1205 // is larger than the limit of the discardable memory pool.
1206 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001207 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1208
1209 int nx = bitmap.width() / tileSize;
1210 int ny = bitmap.height() / tileSize;
1211 for (int x = 0; x <= nx; x++) {
1212 for (int y = 0; y <= ny; y++) {
1213 SkRect tileR;
1214 tileR.set(SkIntToScalar(x * tileSize),
1215 SkIntToScalar(y * tileSize),
1216 SkIntToScalar((x + 1) * tileSize),
1217 SkIntToScalar((y + 1) * tileSize));
1218
1219 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1220 continue;
1221 }
1222
1223 if (!tileR.intersect(srcRect)) {
1224 continue;
1225 }
1226
1227 SkBitmap tmpB;
1228 SkIRect iTileR;
1229 tileR.roundOut(&iTileR);
1230 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1231 SkIntToScalar(iTileR.fTop));
1232
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001233 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001234 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001235 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001236 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001237 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001238
robertphillipsec8bb942014-11-21 10:16:25 -08001239 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001240 SkIRect iClampRect;
1241
reeda5517e22015-07-14 10:54:12 -07001242 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001243 // In bleed mode we want to always expand the tile on all edges
1244 // but stay within the bitmap bounds
1245 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1246 } else {
1247 // In texture-domain/clamp mode we only want to expand the
1248 // tile on edges interior to "srcRect" (i.e., we want to
1249 // not bleed across the original clamped edges)
1250 srcRect.roundOut(&iClampRect);
1251 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001252 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1253 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001254 }
1255
1256 if (bitmap.extractSubset(&tmpB, iTileR)) {
1257 // now offset it to make it "local" to our tmp bitmap
1258 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001259 GrTextureParams paramsTemp = params;
robertphillips1accc4c2015-07-20 10:22:29 -07001260 bool needsTextureDomain = needs_texture_domain(
1261 bitmap, srcRect, paramsTemp,
1262 viewM, bicubic,
1263 fRenderTarget->isUnifiedMultisampled());
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001264 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001265 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001266 tileR,
1267 paramsTemp,
1268 paint,
reeda5517e22015-07-14 10:54:12 -07001269 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001270 bicubic,
1271 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001272 }
1273 }
1274 }
1275}
1276
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001277
1278/*
1279 * This is called by drawBitmap(), which has to handle images that may be too
1280 * large to be represented by a single texture.
1281 *
1282 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1283 * and that non-texture portion of the GrPaint has already been setup.
1284 */
1285void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001286 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001287 const SkRect& srcRect,
1288 const GrTextureParams& params,
1289 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001290 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001291 bool bicubic,
1292 bool needsTextureDomain) {
bsalomon76228632015-05-29 08:02:10 -07001293 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1294 bitmap.height() <= fContext->caps()->maxTextureSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001295
1296 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001297 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001298 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001299 return;
1300 }
1301
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001302 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001303 SkRect paintRect;
1304 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1305 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1306 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1307 SkScalarMul(srcRect.fTop, hInv),
1308 SkScalarMul(srcRect.fRight, wInv),
1309 SkScalarMul(srcRect.fBottom, hInv));
1310
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001311 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001312
1313 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1314 // the rest from the SkPaint.
1315 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001316 SkAutoTUnref<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001317
reeda5517e22015-07-14 10:54:12 -07001318 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001319 // Use a constrained texture domain to avoid color bleeding
1320 SkScalar left, top, right, bottom;
1321 if (srcRect.width() > SK_Scalar1) {
1322 SkScalar border = SK_ScalarHalf / texture->width();
1323 left = paintRect.left() + border;
1324 right = paintRect.right() - border;
1325 } else {
1326 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1327 }
1328 if (srcRect.height() > SK_Scalar1) {
1329 SkScalar border = SK_ScalarHalf / texture->height();
1330 top = paintRect.top() + border;
1331 bottom = paintRect.bottom() - border;
1332 } else {
1333 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1334 }
1335 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001336 if (bicubic) {
joshualitt5f10b5c2015-07-09 10:24:35 -07001337 fp.reset(GrBicubicEffect::Create(grPaint.getProcessorDataManager(), texture,
1338 SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001339 } else {
joshualitt5f10b5c2015-07-09 10:24:35 -07001340 fp.reset(GrTextureDomainEffect::Create(grPaint.getProcessorDataManager(),
1341 texture,
joshualitt5531d512014-12-17 15:50:11 -08001342 SkMatrix::I(),
1343 textureDomain,
1344 GrTextureDomain::kClamp_Mode,
1345 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001346 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001347 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001348 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1349 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualitt5f10b5c2015-07-09 10:24:35 -07001350 fp.reset(GrBicubicEffect::Create(grPaint.getProcessorDataManager(), texture, SkMatrix::I(),
1351 tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001352 } else {
joshualitt5f10b5c2015-07-09 10:24:35 -07001353 fp.reset(GrSimpleTextureEffect::Create(grPaint.getProcessorDataManager(), texture,
1354 SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001355 }
1356
bsalomonac856c92015-08-27 06:30:17 -07001357 grPaint.addColorFragmentProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001358 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001359 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1360 SkColor2GrColor(paint.getColor());
bsalomonbed83a62015-04-15 14:18:34 -07001361 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false,
1362 &grPaint)) {
1363 return;
1364 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001365
robertphillipsea461502015-05-26 11:38:03 -07001366 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dstRect,
1367 paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001368}
1369
fmalita2d97bc12014-11-20 10:44:58 -08001370bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001371 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001372 const SkImageFilter* filter,
1373 const SkImageFilter::Context& ctx,
1374 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001375 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001376
robertphillipsefbffed2015-06-22 12:06:08 -07001377 SkImageFilter::Proxy proxy(this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001378
1379 if (filter->canFilterImageGPU()) {
senorblancod0d37ca2015-04-02 04:54:56 -07001380 return filter->filterImageGPU(&proxy, wrap_texture(texture, width, height),
1381 ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001382 } else {
1383 return false;
1384 }
1385}
1386
1387void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1388 int left, int top, const SkPaint& paint) {
1389 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001390 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001391
1392 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1393 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1394 return;
1395 }
1396
1397 int w = bitmap.width();
1398 int h = bitmap.height();
1399
1400 GrTexture* texture;
1401 // draw sprite uses the default texture params
halcanary96fcdcc2015-08-27 07:41:13 -07001402 AutoBitmapTexture abt(fContext, bitmap, nullptr, &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001403 if (!texture) {
1404 return;
1405 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001406
1407 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001408 // This bitmap will own the filtered result as a texture.
1409 SkBitmap filteredBitmap;
1410
bsalomon49f085d2014-09-05 13:34:00 -07001411 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001412 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001413 SkMatrix matrix(*draw.fMatrix);
1414 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001415 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001416 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001417 // This cache is transient, and is freed (along with all its contained
1418 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001419 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001420 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001421 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001422 texture = (GrTexture*) filteredBitmap.getTexture();
1423 w = filteredBitmap.width();
1424 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001425 left += offset.x();
1426 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001427 } else {
1428 return;
1429 }
1430 }
1431
1432 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001433 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001434
bsalomonbed83a62015-04-15 14:18:34 -07001435 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1436 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1437 return;
1438 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001439
robertphillipsea461502015-05-26 11:38:03 -07001440 fDrawContext->drawNonAARectToRect(fRenderTarget,
1441 fClip,
1442 grPaint,
1443 SkMatrix::I(),
1444 SkRect::MakeXYWH(SkIntToScalar(left),
1445 SkIntToScalar(top),
1446 SkIntToScalar(w),
1447 SkIntToScalar(h)),
1448 SkRect::MakeXYWH(0,
1449 0,
1450 SK_Scalar1 * w / texture->width(),
1451 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001452}
1453
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001454void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001455 const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -07001456 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001457 SkMatrix matrix;
1458 SkRect bitmapBounds, tmpSrc;
1459
1460 bitmapBounds.set(0, 0,
1461 SkIntToScalar(bitmap.width()),
1462 SkIntToScalar(bitmap.height()));
1463
1464 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001465 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001466 tmpSrc = *src;
1467 } else {
1468 tmpSrc = bitmapBounds;
1469 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001470
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001471 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1472
1473 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001474 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001475 if (!bitmapBounds.contains(tmpSrc)) {
1476 if (!tmpSrc.intersect(bitmapBounds)) {
1477 return; // nothing to draw
1478 }
1479 }
1480 }
1481
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001482 SkRect tmpDst;
1483 matrix.mapRect(&tmpDst, tmpSrc);
1484
1485 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1486 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1487 // Translate so that tempDst's top left is at the origin.
1488 matrix = *origDraw.fMatrix;
1489 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1490 draw.writable()->fMatrix = &matrix;
1491 }
1492 SkSize dstSize;
1493 dstSize.fWidth = tmpDst.width();
1494 dstSize.fHeight = tmpDst.height();
1495
reeda5517e22015-07-14 10:54:12 -07001496 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, constraint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001497}
1498
1499void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1500 int x, int y, const SkPaint& paint) {
1501 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001502 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001503 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001504
1505 // TODO: If the source device covers the whole of this device, we could
1506 // omit fNeedsClear -related flushing.
1507 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001508
1509 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001510 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001511
1512 GrRenderTarget* devRT = dev->accessRenderTarget();
1513 GrTexture* devTex;
halcanary96fcdcc2015-08-27 07:41:13 -07001514 if (nullptr == (devTex = devRT->asTexture())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001515 return;
1516 }
1517
robertphillips7b9e8a42014-12-11 08:20:31 -08001518 const SkImageInfo ii = dev->imageInfo();
1519 int w = ii.width();
1520 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001521
1522 SkImageFilter* filter = paint.getImageFilter();
1523 // This bitmap will own the filtered result as a texture.
1524 SkBitmap filteredBitmap;
1525
bsalomon49f085d2014-09-05 13:34:00 -07001526 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001527 SkIPoint offset = SkIPoint::Make(0, 0);
1528 SkMatrix matrix(*draw.fMatrix);
1529 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001530 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001531 // This cache is transient, and is freed (along with all its contained
1532 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001533 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001534 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001535 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1536 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001537 devTex = filteredBitmap.getTexture();
1538 w = filteredBitmap.width();
1539 h = filteredBitmap.height();
1540 x += offset.fX;
1541 y += offset.fY;
1542 } else {
1543 return;
1544 }
1545 }
1546
1547 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001548 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001549
bsalomonbed83a62015-04-15 14:18:34 -07001550 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1551 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1552 return;
1553 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001554
1555 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1556 SkIntToScalar(y),
1557 SkIntToScalar(w),
1558 SkIntToScalar(h));
1559
1560 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1561 // scratch texture).
1562 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1563 SK_Scalar1 * h / devTex->height());
1564
robertphillipsea461502015-05-26 11:38:03 -07001565 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, SkMatrix::I(), dstRect,
1566 srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001567}
1568
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001569bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001570 return filter->canFilterImageGPU();
1571}
1572
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001573bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001574 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001575 SkBitmap* result, SkIPoint* offset) {
1576 // want explicitly our impl, so guard against a subclass of us overriding it
1577 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1578 return false;
1579 }
1580
1581 SkAutoLockPixels alp(src, !src.getTexture());
1582 if (!src.getTexture() && !src.readyToDraw()) {
1583 return false;
1584 }
1585
1586 GrTexture* texture;
1587 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1588 // must be pushed upstack.
halcanary96fcdcc2015-08-27 07:41:13 -07001589 AutoBitmapTexture abt(fContext, src, nullptr, &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001590 if (!texture) {
1591 return false;
1592 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001593
senorblancod0d37ca2015-04-02 04:54:56 -07001594 return this->filterTexture(fContext, texture, src.width(), src.height(),
1595 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001596}
1597
reed85d91782015-09-10 14:33:38 -07001598static bool wrap_as_bm(GrContext* ctx, const SkImage* image, SkBitmap* bm) {
1599 SkAutoTUnref<GrTexture> tex(as_IB(image)->asTextureRef(ctx, kUntiled_SkImageUsageType));
reeda85d4d02015-05-06 12:56:48 -07001600 if (tex) {
reed8b26b992015-05-07 15:36:17 -07001601 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), bm);
1602 return true;
reeda85d4d02015-05-06 12:56:48 -07001603 } else {
reed8b26b992015-05-07 15:36:17 -07001604 return as_IB(image)->getROPixels(bm);
reeda85d4d02015-05-06 12:56:48 -07001605 }
reeda85d4d02015-05-06 12:56:48 -07001606}
1607
1608void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1609 const SkPaint& paint) {
1610 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001611 if (GrTexture* tex = as_IB(image)->peekTexture()) {
1612 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), &bm);
1613 } else {
1614 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1615 paint.getFilterQuality(), *draw.fMatrix)) {
1616 // only support tiling as bitmap at the moment, so force raster-version
1617 if (!as_IB(image)->getROPixels(&bm)) {
1618 return;
1619 }
1620 } else {
1621 if (!wrap_as_bm(this->context(), image, &bm)) {
1622 return;
1623 }
1624 }
reeda85d4d02015-05-06 12:56:48 -07001625 }
reed85d91782015-09-10 14:33:38 -07001626 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reeda85d4d02015-05-06 12:56:48 -07001627}
1628
1629void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001630 const SkRect& dst, const SkPaint& paint,
1631 SkCanvas::SrcRectConstraint constraint) {
reeda85d4d02015-05-06 12:56:48 -07001632 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001633 if (GrTexture* tex = as_IB(image)->peekTexture()) {
1634 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), &bm);
1635 } else {
1636 SkMatrix viewMatrix = *draw.fMatrix;
1637 viewMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1638 dst.height() / (src ? src->height() : image->height()));
1639
1640 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), viewMatrix)) {
1641 // only support tiling as bitmap at the moment, so force raster-version
1642 if (!as_IB(image)->getROPixels(&bm)) {
1643 return;
1644 }
1645 } else {
1646 if (!wrap_as_bm(this->context(), image, &bm)) {
1647 return;
1648 }
1649 }
reeda85d4d02015-05-06 12:56:48 -07001650 }
reed85d91782015-09-10 14:33:38 -07001651 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001652}
1653
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001654///////////////////////////////////////////////////////////////////////////////
1655
1656// must be in SkCanvas::VertexMode order
1657static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1658 kTriangles_GrPrimitiveType,
1659 kTriangleStrip_GrPrimitiveType,
1660 kTriangleFan_GrPrimitiveType,
1661};
1662
1663void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1664 int vertexCount, const SkPoint vertices[],
1665 const SkPoint texs[], const SkColor colors[],
1666 SkXfermode* xmode,
1667 const uint16_t indices[], int indexCount,
1668 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001669 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001670 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001671
dandov32a311b2014-07-15 19:46:26 -07001672 const uint16_t* outIndices;
halcanary96fcdcc2015-08-27 07:41:13 -07001673 SkAutoTDeleteArray<uint16_t> outAlloc(nullptr);
dandov32a311b2014-07-15 19:46:26 -07001674 GrPrimitiveType primType;
1675 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001676
halcanary96fcdcc2015-08-27 07:41:13 -07001677 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1678 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001679
halcanary96fcdcc2015-08-27 07:41:13 -07001680 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001681
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001682 SkPaint copy(paint);
1683 copy.setStyle(SkPaint::kStroke_Style);
1684 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001685
dandov32a311b2014-07-15 19:46:26 -07001686 // we ignore the shader if texs is null.
bsalomonbed83a62015-04-15 14:18:34 -07001687 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy,
halcanary96fcdcc2015-08-27 07:41:13 -07001688 SkColor2GrColor(copy.getColor()), nullptr == colors, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001689 return;
1690 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001691
dandov32a311b2014-07-15 19:46:26 -07001692 primType = kLines_GrPrimitiveType;
1693 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001694 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001695 switch (vmode) {
1696 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001697 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001698 break;
1699 case SkCanvas::kTriangleStrip_VertexMode:
1700 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001701 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001702 break;
1703 }
mtklein533eb782014-08-27 10:39:42 -07001704
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001705 VertState state(vertexCount, indices, indexCount);
1706 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001707
dandov32a311b2014-07-15 19:46:26 -07001708 //number of indices for lines per triangle with kLines
1709 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001710
halcanary385fe4d2015-08-26 13:07:48 -07001711 outAlloc.reset(new uint16_t[indexCount]);
dandov32a311b2014-07-15 19:46:26 -07001712 outIndices = outAlloc.get();
1713 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001714 int i = 0;
1715 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001716 auxIndices[i] = state.f0;
1717 auxIndices[i + 1] = state.f1;
1718 auxIndices[i + 2] = state.f1;
1719 auxIndices[i + 3] = state.f2;
1720 auxIndices[i + 4] = state.f2;
1721 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001722 i += 6;
1723 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001724 } else {
dandov32a311b2014-07-15 19:46:26 -07001725 outIndices = indices;
1726 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001727
halcanary96fcdcc2015-08-27 07:41:13 -07001728 if (nullptr == texs || nullptr == paint.getShader()) {
bsalomonbed83a62015-04-15 14:18:34 -07001729 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1730 SkColor2GrColor(paint.getColor()),
halcanary96fcdcc2015-08-27 07:41:13 -07001731 nullptr == colors, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001732 return;
1733 }
dandov32a311b2014-07-15 19:46:26 -07001734 } else {
bsalomonbed83a62015-04-15 14:18:34 -07001735 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix,
halcanary96fcdcc2015-08-27 07:41:13 -07001736 nullptr == colors, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001737 return;
1738 }
dandov32a311b2014-07-15 19:46:26 -07001739 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001740 }
1741
mtklein2583b622014-06-04 08:20:41 -07001742#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001743 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001744 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1745 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001746 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001747 }
1748 }
mtklein2583b622014-06-04 08:20:41 -07001749#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001750
1751 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001752 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001753 // need to convert byte order and from non-PM to PM
1754 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001755 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001756 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001757 color = colors[i];
1758 if (paint.getAlpha() != 255) {
1759 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1760 }
1761 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001762 }
1763 colors = convertedColors.get();
1764 }
robertphillipsea461502015-05-26 11:38:03 -07001765 fDrawContext->drawVertices(fRenderTarget,
1766 fClip,
1767 grPaint,
1768 *draw.fMatrix,
1769 primType,
1770 vertexCount,
1771 vertices,
1772 texs,
1773 colors,
1774 outIndices,
1775 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001776}
1777
1778///////////////////////////////////////////////////////////////////////////////
1779
jvanverth31ff7622015-08-07 10:09:28 -07001780void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001781 const SkRect texRect[], const SkColor colors[], int count,
1782 SkXfermode::Mode mode, const SkPaint& paint) {
1783 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001784 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001785 return;
1786 }
1787
jvanverth31ff7622015-08-07 10:09:28 -07001788 CHECK_SHOULD_DRAW(draw);
1789 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
1790
reedca109532015-06-25 16:25:25 -07001791 SkPaint p(paint);
1792 p.setShader(atlas->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
1793
jvanverth31ff7622015-08-07 10:09:28 -07001794 GrPaint grPaint;
1795 if (!SkPaint2GrPaint(this->context(), fRenderTarget, p, *draw.fMatrix, !colors, &grPaint)) {
1796 return;
reedca109532015-06-25 16:25:25 -07001797 }
jvanverth31ff7622015-08-07 10:09:28 -07001798
1799 SkDEBUGCODE(this->validate();)
1800
1801#if 0
robertphillips29ccdf82015-07-24 10:20:45 -07001802 if (colors) {
jvanverth31ff7622015-08-07 10:09:28 -07001803 if (SkXfermode::kModulate_Mode != mode) {
1804 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1805 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001806 }
1807 }
jvanverth31ff7622015-08-07 10:09:28 -07001808#endif
1809
1810 fDrawContext->drawAtlas(fRenderTarget, fClip, grPaint, *draw.fMatrix,
1811 count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001812}
1813
1814///////////////////////////////////////////////////////////////////////////////
1815
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001816void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001817 size_t byteLength, SkScalar x, SkScalar y,
1818 const SkPaint& paint) {
1819 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001820 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001821
jvanverth8c27a182014-10-14 08:45:50 -07001822 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001823 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1824 return;
1825 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001826
jvanverth8c27a182014-10-14 08:45:50 -07001827 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001828
robertphillips2334fb62015-06-17 05:43:33 -07001829 fDrawContext->drawText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001830 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001831}
1832
fmalita05c4a432014-09-29 06:29:53 -07001833void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1834 const SkScalar pos[], int scalarsPerPos,
1835 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001836 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001837 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001838
jvanverth8c27a182014-10-14 08:45:50 -07001839 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001840 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1841 return;
1842 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001843
jvanverth8c27a182014-10-14 08:45:50 -07001844 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001845
robertphillips2334fb62015-06-17 05:43:33 -07001846 fDrawContext->drawPosText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001847 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1848 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001849}
1850
joshualitt9c328182015-03-23 08:13:04 -07001851void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1852 const SkPaint& paint, SkDrawFilter* drawFilter) {
1853 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawTextBlob", fContext);
1854 CHECK_SHOULD_DRAW(draw);
1855
1856 SkDEBUGCODE(this->validate();)
1857
robertphillips2334fb62015-06-17 05:43:33 -07001858 fDrawContext->drawTextBlob(fRenderTarget, fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001859 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001860}
1861
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001862///////////////////////////////////////////////////////////////////////////////
1863
reedb2db8982014-11-13 12:41:02 -08001864bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
robertphillips9c240a12015-05-28 07:45:59 -07001865 return GrTextContext::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001866}
1867
1868void SkGpuDevice::flush() {
1869 DO_DEFERRED_CLEAR();
bsalomonc49e8682015-06-30 11:37:35 -07001870 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001871}
1872
1873///////////////////////////////////////////////////////////////////////////////
1874
reed76033be2015-03-14 10:54:31 -07001875SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
bsalomonf2703d82014-10-28 14:33:06 -07001876 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001877 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001878 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001879 desc.fWidth = cinfo.fInfo.width();
1880 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001881 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001882
1883 SkAutoTUnref<GrTexture> texture;
1884 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001885 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001886
hcm4396fa52014-10-27 21:43:30 -07001887 // layers are never draw in repeat modes, so we can request an approx
1888 // match and ignore any padding.
bsalomoneae62002015-07-31 13:59:30 -07001889 if (kNever_TileUsage == cinfo.fTileUsage) {
1890 texture.reset(fContext->textureProvider()->createApproxTexture(desc));
1891 } else {
1892 texture.reset(fContext->textureProvider()->createTexture(desc, true));
1893 }
bsalomonafe30052015-01-16 07:32:33 -08001894
1895 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001896 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001897 return SkGpuDevice::Create(
bsalomon74f681d2015-06-23 14:38:48 -07001898 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, init);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001899 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001900 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001901 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001902 cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary96fcdcc2015-08-27 07:41:13 -07001903 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001904 }
1905}
1906
reed4a8126e2014-09-22 07:29:03 -07001907SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001908 // TODO: Change the signature of newSurface to take a budgeted parameter.
1909 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
vbuzinovdded6962015-06-12 08:59:45 -07001910 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
bsalomonafe30052015-01-16 07:32:33 -08001911 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001912}
1913
robertphillips30d2cc62014-09-24 08:52:18 -07001914bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001915 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001916#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001917 // todo: should handle this natively
1918 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001919 return false;
1920 }
1921
halcanary96fcdcc2015-08-27 07:41:13 -07001922 const SkBigPicture::AccelData* data = nullptr;
mtklein9db912c2015-05-19 11:11:26 -07001923 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1924 data = bp->accelData();
1925 }
robertphillips81f71b62014-11-11 04:54:49 -08001926 if (!data) {
1927 return false;
1928 }
1929
robertphillipse5524cd2015-02-20 12:30:26 -08001930 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1931 if (0 == gpuData->numBlocks()) {
1932 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001933 }
1934
robertphillipsfd61ed02014-10-28 07:21:44 -07001935 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001936
robertphillipse5524cd2015-02-20 12:30:26 -08001937 SkIRect iBounds;
1938 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1939 return false;
1940 }
1941
1942 SkRect clipBounds = SkRect::Make(iBounds);
1943
1944 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1945
robertphillipsfd61ed02014-10-28 07:21:44 -07001946 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001947 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001948 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001949 &atlasedNeedRendering, &atlasedRecycled,
vbuzinovdded6962015-06-12 08:59:45 -07001950 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001951
1952 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1953
1954 SkTDArray<GrHoistedLayer> needRendering, recycled;
1955
robertphillipse5524cd2015-02-20 12:30:26 -08001956 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1957
robertphillipsfd61ed02014-10-28 07:21:44 -07001958 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001959 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001960 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001961 &needRendering, &recycled,
vbuzinovdded6962015-06-12 08:59:45 -07001962 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001963
1964 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001965
robertphillips64bf7672014-08-21 13:07:35 -07001966 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001967 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
halcanary96fcdcc2015-08-27 07:41:13 -07001968 initialMatrix, nullptr);
robertphillips64bf7672014-08-21 13:07:35 -07001969
robertphillipsfd61ed02014-10-28 07:21:44 -07001970 GrLayerHoister::UnlockLayers(fContext, needRendering);
1971 GrLayerHoister::UnlockLayers(fContext, recycled);
1972 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1973 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001974
1975 return true;
robertphillips63242d72014-12-04 08:31:02 -08001976#else
1977 return false;
1978#endif
robertphillips64bf7672014-08-21 13:07:35 -07001979}
1980
senorblancobe129b22014-08-08 07:14:35 -07001981SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001982 // We always return a transient cache, so it is freed after each
1983 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001984 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001985}
reedf037e0b2014-10-30 11:34:15 -07001986
1987#endif