blob: 725c26b248a2f3c235ba270275f52669feeffc4c [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"
bsalomonf276ac52015-10-09 13:36:42 -070026#include "SkGr.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070027#include "SkGrPriv.h"
reeda85d4d02015-05-06 12:56:48 -070028#include "SkImage_Base.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000029#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080030#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000031#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000032#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000033#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070034#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080036#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000038#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080039#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000040#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000041#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000042#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070043#include "SkXfermode.h"
joshualitta61c8172015-08-17 10:51:22 -070044#include "batches/GrRectBatchFactory.h"
kkinnunenabcfab42015-02-22 22:53:44 -080045#include "effects/GrBicubicEffect.h"
46#include "effects/GrDashingEffect.h"
47#include "effects/GrSimpleTextureEffect.h"
48#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000049
reedf037e0b2014-10-30 11:34:15 -070050#if SK_SUPPORT_GPU
51
senorblanco55b6d8b2014-07-30 11:26:46 -070052enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
53
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000054#if 0
55 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080056 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000057 do { \
58 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080059 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000060 } while (0)
61#else
joshualitt5531d512014-12-17 15:50:11 -080062 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000063#endif
64
65// This constant represents the screen alignment criterion in texels for
66// requiring texture domain clamping to prevent color bleeding when drawing
67// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000068#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000069
70#define DO_DEFERRED_CLEAR() \
71 do { \
bsalomonafe30052015-01-16 07:32:33 -080072 if (fNeedClear) { \
73 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000074 } \
75 } while (false) \
76
77///////////////////////////////////////////////////////////////////////////////
78
79#define CHECK_FOR_ANNOTATION(paint) \
80 do { if (paint.getAnnotation()) { return; } } while (0)
81
82///////////////////////////////////////////////////////////////////////////////
83
bsalomonbcf0a522014-10-08 08:40:09 -070084// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
85// just accesses the backing GrTexture. Otherwise, it creates a cached texture
86// representation and releases it in the destructor.
87class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040088public:
bsalomonbcf0a522014-10-08 08:40:09 -070089 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070090
bsalomonbcf0a522014-10-08 08:40:09 -070091 AutoBitmapTexture(GrContext* context,
92 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -070093 const GrTextureParams& params,
bsalomonbcf0a522014-10-08 08:40:09 -070094 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040095 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070096 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040097 }
98
bsalomonbcf0a522014-10-08 08:40:09 -070099 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -0400100 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -0700101 const GrTextureParams& params) {
bsalomonbcf0a522014-10-08 08:40:09 -0700102 // Either get the texture directly from the bitmap, or else use the cache and
103 // remember to unref it.
104 if (GrTexture* bmpTexture = bitmap.getTexture()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700105 fTexture.reset(nullptr);
bsalomonbcf0a522014-10-08 08:40:09 -0700106 return bmpTexture;
107 } else {
108 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
109 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400110 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400111 }
112
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000113private:
bsalomonbcf0a522014-10-08 08:40:09 -0700114 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000115};
116
117///////////////////////////////////////////////////////////////////////////////
118
119struct GrSkDrawProcs : public SkDrawProcs {
120public:
121 GrContext* fContext;
122 GrTextContext* fTextContext;
123 GrFontScaler* fFontScaler; // cached in the skia glyphcache
124};
125
126///////////////////////////////////////////////////////////////////////////////
127
bsalomon74f681d2015-06-23 14:38:48 -0700128/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
129 should fail. */
130bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
131 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
132 *flags = 0;
133 if (info) {
134 switch (info->alphaType()) {
135 case kPremul_SkAlphaType:
136 break;
137 case kOpaque_SkAlphaType:
138 *flags |= SkGpuDevice::kIsOpaque_Flag;
139 break;
140 default: // If it is unpremul or unknown don't try to render
141 return false;
142 }
143 }
144 if (kClear_InitContents == init) {
145 *flags |= kNeedClear_Flag;
146 }
147 return true;
148}
149
150SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props,
151 InitContents init) {
152 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, init);
senorblancod0d37ca2015-04-02 04:54:56 -0700153}
154
155SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
bsalomon74f681d2015-06-23 14:38:48 -0700156 const SkSurfaceProps* props, InitContents init) {
bsalomonafe30052015-01-16 07:32:33 -0800157 if (!rt || rt->wasDestroyed()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700158 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000159 }
bsalomon74f681d2015-06-23 14:38:48 -0700160 unsigned flags;
halcanary96fcdcc2015-08-27 07:41:13 -0700161 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
162 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700163 }
halcanary385fe4d2015-08-26 13:07:48 -0700164 return new SkGpuDevice(rt, width, height, props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000165}
166
bsalomon74f681d2015-06-23 14:38:48 -0700167SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
168 const SkImageInfo& info, int sampleCount,
169 const SkSurfaceProps* props, InitContents init) {
170 unsigned flags;
171 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700172 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700173 }
174
175 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
halcanary96fcdcc2015-08-27 07:41:13 -0700176 if (nullptr == rt) {
177 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700178 }
179
halcanary385fe4d2015-08-26 13:07:48 -0700180 return new SkGpuDevice(rt, info.width(), info.height(), props, flags);
bsalomon74f681d2015-06-23 14:38:48 -0700181}
182
senorblancod0d37ca2015-04-02 04:54:56 -0700183SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
184 const SkSurfaceProps* props, unsigned flags)
robertphillipsfcf78292015-06-19 11:49:52 -0700185 : INHERITED(SkSurfacePropsCopyOrDefault(props))
reedb2db8982014-11-13 12:41:02 -0800186{
halcanary96fcdcc2015-08-27 07:41:13 -0700187 fDrawProcs = nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000188
bsalomonafe30052015-01-16 07:32:33 -0800189 fContext = SkRef(rt->getContext());
bsalomon74f681d2015-06-23 14:38:48 -0700190 fNeedClear = SkToBool(flags & kNeedClear_Flag);
191 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000192
bsalomonafe30052015-01-16 07:32:33 -0800193 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000194
bsalomon74f681d2015-06-23 14:38:48 -0700195 SkAlphaType at = fOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
196 SkImageInfo info = rt->surfacePriv().info(at).makeWH(width, height);
halcanary385fe4d2015-08-26 13:07:48 -0700197 SkPixelRef* pr = new SkGrPixelRef(info, rt);
bsalomonafbf2d62014-09-30 12:18:44 -0700198 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700199 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700200
robertphillips77a2e522015-10-17 07:43:27 -0700201 fDrawContext.reset(this->context()->drawContext(rt, &this->surfaceProps()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000202}
203
kkinnunenabcfab42015-02-22 22:53:44 -0800204GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
205 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000206 if (kUnknown_SkColorType == origInfo.colorType() ||
207 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700208 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000209 }
210
bsalomonafe30052015-01-16 07:32:33 -0800211 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700212 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800213 }
214
reede5ea5002014-09-03 11:54:58 -0700215 SkColorType ct = origInfo.colorType();
216 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700217 if (kRGB_565_SkColorType == ct) {
218 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800219 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
220 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700221 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800222 }
223 if (kOpaque_SkAlphaType != at) {
224 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000225 }
reede5ea5002014-09-03 11:54:58 -0700226 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000227
bsalomonf2703d82014-10-28 14:33:06 -0700228 GrSurfaceDesc desc;
229 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000230 desc.fWidth = info.width();
231 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000232 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000233 desc.fSampleCnt = sampleCount;
bsalomond309e7a2015-04-30 14:18:54 -0700234 GrTexture* texture = context->textureProvider()->createTexture(
halcanary96fcdcc2015-08-27 07:41:13 -0700235 desc, SkToBool(budgeted), nullptr, 0);
236 if (nullptr == texture) {
237 return nullptr;
kkinnunenabcfab42015-02-22 22:53:44 -0800238 }
halcanary96fcdcc2015-08-27 07:41:13 -0700239 SkASSERT(nullptr != texture->asRenderTarget());
kkinnunenabcfab42015-02-22 22:53:44 -0800240 return texture->asRenderTarget();
241}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000242
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000243SkGpuDevice::~SkGpuDevice() {
244 if (fDrawProcs) {
245 delete fDrawProcs;
246 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000247
bsalomon32d0b3b2014-08-29 07:50:23 -0700248 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000249 fContext->unref();
250}
251
252///////////////////////////////////////////////////////////////////////////////
253
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000254bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
255 int x, int y) {
256 DO_DEFERRED_CLEAR();
257
258 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000259 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000260 if (kUnknown_GrPixelConfig == config) {
261 return false;
262 }
263
264 uint32_t flags = 0;
265 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
266 flags = GrContext::kUnpremul_PixelOpsFlag;
267 }
bsalomon74f681d2015-06-23 14:38:48 -0700268 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
269 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000270}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000271
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000272bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
273 int x, int y) {
274 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000275 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000276 if (kUnknown_GrPixelConfig == config) {
277 return false;
278 }
279 uint32_t flags = 0;
280 if (kUnpremul_SkAlphaType == info.alphaType()) {
281 flags = GrContext::kUnpremul_PixelOpsFlag;
282 }
283 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
284
285 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700286 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000287
288 return true;
289}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000290
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000291const SkBitmap& SkGpuDevice::onAccessBitmap() {
292 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700293 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000294}
295
reed41e010c2015-06-09 12:16:53 -0700296bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
297 DO_DEFERRED_CLEAR();
298 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
299 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
300 // ... ugh.
301 fLegacyBitmap.notifyPixelsChanged();
302 return false;
303}
304
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000305void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
306 INHERITED::onAttachToCanvas(canvas);
307
308 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800309 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000310}
311
312void SkGpuDevice::onDetachFromCanvas() {
313 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800314 fClip.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700315 fClipStack.reset(nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000316}
317
318// call this every draw call, to ensure that the context reflects our state,
319// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800320void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800321 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000322
joshualitt44701df2015-02-23 14:44:57 -0800323 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000324
joshualitt570d2f82015-02-25 13:19:48 -0800325 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000326
327 DO_DEFERRED_CLEAR();
328}
329
330GrRenderTarget* SkGpuDevice::accessRenderTarget() {
robertphillips7156b092015-05-14 08:54:12 -0700331 DO_DEFERRED_CLEAR();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000332 return fRenderTarget;
333}
334
reed8eddfb52014-12-04 07:50:14 -0800335void SkGpuDevice::clearAll() {
336 GrColor color = 0;
337 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
338 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700339 fDrawContext->clear(&rect, color, true);
bsalomonafe30052015-01-16 07:32:33 -0800340 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800341}
342
kkinnunenabcfab42015-02-22 22:53:44 -0800343void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
344 // Caller must have accessed the render target, because it knows the rt must be replaced.
345 SkASSERT(!fNeedClear);
346
347 SkSurface::Budgeted budgeted =
348 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
349 : SkSurface::kNo_Budgeted;
350
351 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
robertphillips77a2e522015-10-17 07:43:27 -0700352 this->context(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt));
kkinnunenabcfab42015-02-22 22:53:44 -0800353
halcanary96fcdcc2015-08-27 07:41:13 -0700354 if (nullptr == newRT) {
kkinnunenabcfab42015-02-22 22:53:44 -0800355 return;
356 }
357
358 if (shouldRetainContent) {
359 if (fRenderTarget->wasDestroyed()) {
360 return;
361 }
362 this->context()->copySurface(newRT, fRenderTarget);
363 }
364
365 SkASSERT(fRenderTarget != newRT);
366
367 fRenderTarget->unref();
368 fRenderTarget = newRT.detach();
369
bsalomon74f681d2015-06-23 14:38:48 -0700370#ifdef SK_DEBUG
371 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
372 kPremul_SkAlphaType);
373 SkASSERT(info == fLegacyBitmap.info());
374#endif
halcanary385fe4d2015-08-26 13:07:48 -0700375 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget);
kkinnunenabcfab42015-02-22 22:53:44 -0800376 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700377
robertphillips77a2e522015-10-17 07:43:27 -0700378 fDrawContext.reset(this->context()->drawContext(fRenderTarget, &this->surfaceProps()));
kkinnunenabcfab42015-02-22 22:53:44 -0800379}
380
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000381///////////////////////////////////////////////////////////////////////////////
382
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000383void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800384 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700385 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000386
387 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700388 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700389 return;
390 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000391
robertphillips2e1e51f2015-10-15 08:01:48 -0700392 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000393}
394
395// must be in SkCanvas::PointMode order
396static const GrPrimitiveType gPointMode2PrimtiveType[] = {
397 kPoints_GrPrimitiveType,
398 kLines_GrPrimitiveType,
399 kLineStrip_GrPrimitiveType
400};
401
ethannicholas330bb952015-07-17 06:44:02 -0700402// suppress antialiasing on axis-aligned integer-coordinate lines
403static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
404 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
405 return false;
406 }
407 if (count == 2) {
408 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
409 // the other coordinates are not. This does mean the two end pixels of the line will be
410 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
411 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
412 // more complete fix is possible down the road, for the time being we accept the error on
413 // the two end pixels as being the lesser of two evils.
414 if (pts[0].fX == pts[1].fX) {
415 return ((int) pts[0].fX) != pts[0].fX;
416 }
417 if (pts[0].fY == pts[1].fY) {
418 return ((int) pts[0].fY) != pts[0].fY;
419 }
420 }
421 return true;
422}
423
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000424void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
425 size_t count, const SkPoint pts[], const SkPaint& paint) {
426 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800427 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000428
429 SkScalar width = paint.getStrokeWidth();
430 if (width < 0) {
431 return;
432 }
433
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000434 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700435 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
436 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700437 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700438 return;
439 }
egdaniele61c4112014-06-12 10:24:21 -0700440 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700441 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700442 path.moveTo(pts[0]);
443 path.lineTo(pts[1]);
robertphillips2e1e51f2015-10-15 08:01:48 -0700444 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700445 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000446 }
447
ethannicholas330bb952015-07-17 06:44:02 -0700448 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000449 // else we let the SkDraw call our drawPath()
ethannicholas330bb952015-07-17 06:44:02 -0700450 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
451 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000452 draw.drawPoints(mode, count, pts, paint, true);
453 return;
454 }
455
456 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700457 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700458 return;
459 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000460
robertphillips2e1e51f2015-10-15 08:01:48 -0700461 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700462 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;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700519 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700520 return;
521 }
Mike Klein744fb732014-06-23 15:13:26 -0400522
robertphillips2e1e51f2015-10-15 08:01:48 -0700523 fDrawContext->drawRect(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;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700535 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700536 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 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800560 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800561 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700562 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700563 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000564 return;
565 }
566 }
567
568 }
569 }
570
571 }
572
egdanield58a0ba2014-06-11 10:30:05 -0700573 bool usePath = false;
574
575 if (paint.getMaskFilter()) {
576 usePath = true;
577 } else {
578 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700579 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700580 usePath = true;
581 }
582 }
583
584
585 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000586 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700587 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000588 path.addRRect(rect);
halcanary96fcdcc2015-08-27 07:41:13 -0700589 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000590 return;
591 }
Mike Klein744fb732014-06-23 15:13:26 -0400592
robertphillips2e1e51f2015-10-15 08:01:48 -0700593 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000594}
595
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000596void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800597 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000598 SkStrokeRec stroke(paint);
599 if (stroke.isFillStyle()) {
600
601 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800602 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000603
604 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700605 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700606 return;
607 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000608
halcanary96fcdcc2015-08-27 07:41:13 -0700609 if (nullptr == paint.getMaskFilter() && nullptr == paint.getPathEffect()) {
robertphillips2e1e51f2015-10-15 08:01:48 -0700610 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000611 return;
612 }
613 }
614
615 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700616 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000617 path.addRRect(outer);
618 path.addRRect(inner);
619 path.setFillType(SkPath::kEvenOdd_FillType);
620
halcanary96fcdcc2015-08-27 07:41:13 -0700621 this->drawPath(draw, path, paint, nullptr, true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000622}
623
624
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000625/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000626
627void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
628 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700629 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000630 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800631 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000632
egdanield58a0ba2014-06-11 10:30:05 -0700633 GrStrokeInfo strokeInfo(paint);
634
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000635 bool usePath = false;
636 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700637 if (paint.getMaskFilter()) {
robertphillips30c4cae2015-09-15 10:20:55 -0700638 // The RRect path can handle special case blurring
639 SkRRect rr = SkRRect::MakeOval(oval);
640 return this->drawRRect(draw, rr, paint);
egdanield58a0ba2014-06-11 10:30:05 -0700641 } else {
642 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700643 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700644 usePath = true;
645 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000646 }
647
648 if (usePath) {
649 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700650 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000651 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700652 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000653 return;
654 }
655
656 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700657 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700658 return;
659 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660
robertphillips2e1e51f2015-10-15 08:01:48 -0700661 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000662}
663
664#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000665
666///////////////////////////////////////////////////////////////////////////////
667
robertphillipsccb1b572015-05-27 11:02:55 -0700668static SkBitmap wrap_texture(GrTexture* texture, int width, int height) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000669 SkBitmap result;
senorblancod0d37ca2015-04-02 04:54:56 -0700670 result.setInfo(SkImageInfo::MakeN32Premul(width, height));
halcanary385fe4d2015-08-26 13:07:48 -0700671 result.setPixelRef(new SkGrPixelRef(result.info(), texture))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000672 return result;
673}
674
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000675void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
676 const SkPaint& paint, const SkMatrix* prePathMatrix,
677 bool pathIsMutable) {
678 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800679 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700680 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000681
robertphillipsccb1b572015-05-27 11:02:55 -0700682 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, fRenderTarget,
683 fClip, origSrcPath, paint,
684 *draw.fMatrix, prePathMatrix,
685 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000686}
687
688static const int kBmpSmallTileSize = 1 << 10;
689
690static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
691 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
692 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
693 return tilesX * tilesY;
694}
695
reed85d91782015-09-10 14:33:38 -0700696static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000697 if (maxTileSize <= kBmpSmallTileSize) {
698 return maxTileSize;
699 }
700
701 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
702 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
703
704 maxTileTotalTileSize *= maxTileSize * maxTileSize;
705 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
706
707 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
708 return kBmpSmallTileSize;
709 } else {
710 return maxTileSize;
711 }
712}
713
714// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
715// pixels from the bitmap are necessary.
bsalomon74f681d2015-06-23 14:38:48 -0700716static void determine_clipped_src_rect(const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800717 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800718 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700719 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000720 const SkRect* srcRectPtr,
721 SkIRect* clippedSrcIRect) {
halcanary96fcdcc2015-08-27 07:41:13 -0700722 clip.getConservativeBounds(rt, clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000723 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800724 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000725 clippedSrcIRect->setEmpty();
726 return;
727 }
728 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
729 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700730 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000731 // we've setup src space 0,0 to map to the top left of the src rect.
732 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000733 if (!clippedSrcRect.intersect(*srcRectPtr)) {
734 clippedSrcIRect->setEmpty();
735 return;
736 }
737 }
738 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700739 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000740 if (!clippedSrcIRect->intersect(bmpBounds)) {
741 clippedSrcIRect->setEmpty();
742 }
743}
744
reed85d91782015-09-10 14:33:38 -0700745bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
746 const SkMatrix& viewMatrix,
747 const GrTextureParams& params,
748 const SkRect* srcRectPtr,
749 int maxTileSize,
750 int* tileSize,
751 SkIRect* clippedSubset) const {
752 // if it's larger than the max tile size, then we have no choice but tiling.
753 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
754 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(),
755 srcRectPtr, clippedSubset);
756 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
757 return true;
758 }
759
bsalomon1a1d0b82015-10-16 07:49:42 -0700760 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700761 const size_t area = imageRect.width() * imageRect.height();
762 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
763 return false;
764 }
765
reed85d91782015-09-10 14:33:38 -0700766 // At this point we know we could do the draw by uploading the entire bitmap
767 // as a texture. However, if the texture would be large compared to the
768 // cache size and we don't require most of it for this draw then tile to
769 // reduce the amount of upload and cache spill.
770
771 // assumption here is that sw bitmap size is a good proxy for its size as
772 // a texture
773 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
774 size_t cacheSize;
775 fContext->getResourceCacheLimits(nullptr, &cacheSize);
776 if (bmpSize < cacheSize / 2) {
777 return false;
778 }
779
bsalomon1a1d0b82015-10-16 07:49:42 -0700780 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
781 // tiling memory savings would be < 50%.
reed85d91782015-09-10 14:33:38 -0700782 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(), srcRectPtr,
783 clippedSubset);
784 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
785 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
786 kBmpSmallTileSize * kBmpSmallTileSize;
787
788 return usedTileBytes < 2 * bmpSize;
789}
790
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000791bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800792 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000793 const GrTextureParams& params,
794 const SkRect* srcRectPtr,
795 int maxTileSize,
796 int* tileSize,
797 SkIRect* clippedSrcRect) const {
798 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700799 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000800 return false;
801 }
802
reed85d91782015-09-10 14:33:38 -0700803 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
804 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
805}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000806
reed85d91782015-09-10 14:33:38 -0700807bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
808 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
809 const SkMatrix& viewMatrix) const {
810 // if image is explictly texture backed then just use the texture
811 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000812 return false;
813 }
814
reed85d91782015-09-10 14:33:38 -0700815 GrTextureParams params;
816 bool doBicubic;
817 GrTextureParams::FilterMode textureFilterMode =
818 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
819
820 int tileFilterPad;
821 if (doBicubic) {
822 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
823 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
824 tileFilterPad = 0;
825 } else {
826 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000827 }
reed85d91782015-09-10 14:33:38 -0700828 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000829
bsalomon8c07b7a2015-11-02 11:36:52 -0800830 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000831
reed85d91782015-09-10 14:33:38 -0700832 // these are output, which we safely ignore, as we just want to know the predicate
833 int outTileSize;
834 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000835
reed85d91782015-09-10 14:33:38 -0700836 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
837 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000838}
839
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000840void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000841 const SkBitmap& bitmap,
842 const SkMatrix& m,
843 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000844 SkMatrix concat;
845 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
846 if (!m.isIdentity()) {
847 concat.setConcat(*draw->fMatrix, m);
848 draw.writable()->fMatrix = &concat;
849 }
halcanary96fcdcc2015-08-27 07:41:13 -0700850 this->drawBitmapCommon(*draw, bitmap, nullptr, nullptr, paint, SkCanvas::kStrict_SrcRectConstraint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000851}
852
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000853// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000854// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
855// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000856static inline void clamped_outset_with_offset(SkIRect* iRect,
857 int outset,
858 SkPoint* offset,
859 const SkIRect& clamp) {
860 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000861
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000862 int leftClampDelta = clamp.fLeft - iRect->fLeft;
863 if (leftClampDelta > 0) {
864 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000865 iRect->fLeft = clamp.fLeft;
866 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000867 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000868 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000869
870 int topClampDelta = clamp.fTop - iRect->fTop;
871 if (topClampDelta > 0) {
872 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000873 iRect->fTop = clamp.fTop;
874 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000875 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000876 }
877
878 if (iRect->fRight > clamp.fRight) {
879 iRect->fRight = clamp.fRight;
880 }
881 if (iRect->fBottom > clamp.fBottom) {
882 iRect->fBottom = clamp.fBottom;
883 }
884}
885
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000886static bool has_aligned_samples(const SkRect& srcRect,
887 const SkRect& transformedRect) {
888 // detect pixel disalignment
889 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
890 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
891 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
892 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
893 SkScalarAbs(transformedRect.width() - srcRect.width()) <
894 COLOR_BLEED_TOLERANCE &&
895 SkScalarAbs(transformedRect.height() - srcRect.height()) <
896 COLOR_BLEED_TOLERANCE) {
897 return true;
898 }
899 return false;
900}
901
902static bool may_color_bleed(const SkRect& srcRect,
903 const SkRect& transformedRect,
robertphillips1accc4c2015-07-20 10:22:29 -0700904 const SkMatrix& m,
905 bool isMSAA) {
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000906 // Only gets called if has_aligned_samples returned false.
907 // So we can assume that sampling is axis aligned but not texel aligned.
908 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
909 SkRect innerSrcRect(srcRect), innerTransformedRect,
910 outerTransformedRect(transformedRect);
robertphillips1accc4c2015-07-20 10:22:29 -0700911 if (isMSAA) {
912 innerSrcRect.inset(SK_Scalar1, SK_Scalar1);
913 } else {
914 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
915 }
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000916 m.mapRect(&innerTransformedRect, innerSrcRect);
917
918 // The gap between outerTransformedRect and innerTransformedRect
919 // represents the projection of the source border area, which is
920 // problematic for color bleeding. We must check whether any
921 // destination pixels sample the border area.
922 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
923 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
924 SkIRect outer, inner;
925 outerTransformedRect.round(&outer);
926 innerTransformedRect.round(&inner);
927 // If the inner and outer rects round to the same result, it means the
928 // border does not overlap any pixel centers. Yay!
929 return inner != outer;
930}
931
932static bool needs_texture_domain(const SkBitmap& bitmap,
933 const SkRect& srcRect,
934 GrTextureParams &params,
935 const SkMatrix& contextMatrix,
robertphillips1accc4c2015-07-20 10:22:29 -0700936 bool bicubic,
937 bool isMSAA) {
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000938 bool needsTextureDomain = false;
senorblancod0d37ca2015-04-02 04:54:56 -0700939 GrTexture* tex = bitmap.getTexture();
940 int width = tex ? tex->width() : bitmap.width();
941 int height = tex ? tex->height() : bitmap.height();
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000942
943 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
944 // Need texture domain if drawing a sub rect
senorblancod0d37ca2015-04-02 04:54:56 -0700945 needsTextureDomain = srcRect.width() < width ||
946 srcRect.height() < height;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000947 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
948 // sampling is axis-aligned
949 SkRect transformedRect;
950 contextMatrix.mapRect(&transformedRect, srcRect);
951
952 if (has_aligned_samples(srcRect, transformedRect)) {
953 params.setFilterMode(GrTextureParams::kNone_FilterMode);
954 needsTextureDomain = false;
955 } else {
robertphillips1accc4c2015-07-20 10:22:29 -0700956 needsTextureDomain = may_color_bleed(srcRect, transformedRect,
957 contextMatrix, isMSAA);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000958 }
959 }
960 }
961 return needsTextureDomain;
962}
963
joshualitta61c8172015-08-17 10:51:22 -0700964static void draw_aa_bitmap(GrDrawContext* drawContext, GrContext* context,
965 GrRenderTarget* renderTarget, const GrClip& clip,
966 const SkMatrix& viewMatrix, const SkMatrix& srcRectToDstRect,
967 const SkPaint& paint, const SkBitmap* bitmapPtr, const SkSize& dstSize) {
968 SkShader::TileMode tm[] = {
969 SkShader::kClamp_TileMode,
970 SkShader::kClamp_TileMode,
971 };
972
973 bool doBicubic;
974 GrTextureParams::FilterMode textureFilterMode =
975 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix,
976 srcRectToDstRect,
977 &doBicubic);
978
979 // Setup texture to wrap bitmap
980 GrTextureParams params(tm, textureFilterMode);
bsalomonafa95e22015-10-12 10:39:46 -0700981 SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, *bitmapPtr, params));
joshualitta61c8172015-08-17 10:51:22 -0700982
983 if (!texture) {
984 SkErrorInternals::SetError(kInternalError_SkError,
985 "Couldn't convert bitmap to texture.");
986 return;
987 }
988
joshualitta61c8172015-08-17 10:51:22 -0700989
990 GrPaint grPaint;
991
992 // Create and insert texture effect
993 SkAutoTUnref<const GrFragmentProcessor> fp;
994 if (doBicubic) {
bsalomon4a339522015-10-06 08:40:50 -0700995 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tm));
joshualitta61c8172015-08-17 10:51:22 -0700996 } else {
bsalomon4a339522015-10-06 08:40:50 -0700997 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
joshualitta61c8172015-08-17 10:51:22 -0700998 }
999
bsalomonf1b7a1d2015-09-28 06:26:28 -07001000 if (kAlpha_8_SkColorType == bitmapPtr->colorType()) {
1001 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1002 } else {
1003 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
joshualitta61c8172015-08-17 10:51:22 -07001004 }
1005
bsalomonf1b7a1d2015-09-28 06:26:28 -07001006 if (!SkPaintToGrPaintReplaceShader(context, paint, fp, &grPaint)) {
1007 return;
1008 }
joshualitta61c8172015-08-17 10:51:22 -07001009
1010 // Setup dst rect and final matrix
1011 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1012
1013 SkRect devRect;
1014 viewMatrix.mapRect(&devRect, dstRect);
1015
1016 SkMatrix matrix;
1017 matrix.setIDiv(bitmapPtr->width(), bitmapPtr->height());
1018
1019 SkMatrix dstRectToSrcRect;
1020 if (!srcRectToDstRect.invert(&dstRectToSrcRect)) {
1021 return;
1022 }
1023 matrix.preConcat(dstRectToSrcRect);
1024
joshualittd2b23e02015-08-21 10:53:34 -07001025 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFill(grPaint.getColor(),
joshualitta61c8172015-08-17 10:51:22 -07001026 viewMatrix,
1027 matrix,
1028 dstRect,
1029 devRect));
1030
robertphillips2e1e51f2015-10-15 08:01:48 -07001031 drawContext->drawBatch(clip, grPaint, batch);
joshualitta61c8172015-08-17 10:51:22 -07001032}
1033
reed3322a812015-09-16 10:09:24 -07001034static bool can_ignore_strict_subset_constraint(const SkBitmap& bitmap, const SkRect& subset) {
1035 GrTexture* tex = bitmap.getTexture();
1036 int width = tex ? tex->width() : bitmap.width();
1037 int height = tex ? tex->height() : bitmap.height();
1038 return subset.contains(SkRect::MakeIWH(width, height));
1039}
1040
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001041void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1042 const SkBitmap& bitmap,
1043 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001044 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001046 SkCanvas::SrcRectConstraint constraint) {
joshualitt5531d512014-12-17 15:50:11 -08001047 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001048
1049 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001050 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001051 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1052 // in the (easier) bleed case, so update flags.
halcanary96fcdcc2015-08-27 07:41:13 -07001053 if (nullptr == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001054 SkScalar w = SkIntToScalar(bitmap.width());
1055 SkScalar h = SkIntToScalar(bitmap.height());
1056 dstSize.fWidth = w;
1057 dstSize.fHeight = h;
1058 srcRect.set(0, 0, w, h);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001059 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001060 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001061 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001062 dstSize = *dstSizePtr;
senorblancod0d37ca2015-04-02 04:54:56 -07001063 }
reed3322a812015-09-16 10:09:24 -07001064
1065 if (can_ignore_strict_subset_constraint(bitmap, srcRect)) {
reeda5517e22015-07-14 10:54:12 -07001066 constraint = SkCanvas::kFast_SrcRectConstraint;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001067 }
1068
derekf367e1862014-12-02 11:02:06 -08001069 // If the render target is not msaa and draw is antialiased, we call
1070 // drawRect instead of drawing on the render target directly.
1071 // FIXME: the tiled bitmap code path doesn't currently support
1072 // anti-aliased edges, we work around that for now by drawing directly
1073 // if the image size exceeds maximum texture size.
bsalomon8c07b7a2015-11-02 11:36:52 -08001074 int maxTileSize = fContext->caps()->maxTileSize();
joshualitt1a899c92015-08-17 11:53:44 -07001075 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
1076 paint.isAntiAlias() &&
bsalomon8c07b7a2015-11-02 11:36:52 -08001077 bitmap.width() <= maxTileSize &&
1078 bitmap.height() <= maxTileSize;
derekf367e1862014-12-02 11:02:06 -08001079
joshualitt1a899c92015-08-17 11:53:44 -07001080 if (paint.getMaskFilter() || drawAA) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001081 // Convert the bitmap to a shader so that the rect can be drawn
1082 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001083 SkBitmap tmp; // subset of bitmap, if necessary
1084 const SkBitmap* bitmapPtr = &bitmap;
joshualitta61c8172015-08-17 10:51:22 -07001085 SkMatrix srcRectToDstRect;
bsalomon49f085d2014-09-05 13:34:00 -07001086 if (srcRectPtr) {
joshualitta61c8172015-08-17 10:51:22 -07001087 srcRectToDstRect.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1088 srcRectToDstRect.postScale(dstSize.fWidth / srcRectPtr->width(),
1089 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001090 // In bleed mode we position and trim the bitmap based on the src rect which is
1091 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1092 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1093 // compensate.
reeda5517e22015-07-14 10:54:12 -07001094 if (SkCanvas::kStrict_SrcRectConstraint == constraint) {
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001095 SkIRect iSrc;
1096 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001097
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001098 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1099 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001100
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001101 if (!bitmap.extractSubset(&tmp, iSrc)) {
1102 return; // extraction failed
1103 }
1104 bitmapPtr = &tmp;
1105 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001106
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001107 // The source rect has changed so update the matrix
joshualitta61c8172015-08-17 10:51:22 -07001108 srcRectToDstRect.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001109 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001110 } else {
joshualitta61c8172015-08-17 10:51:22 -07001111 srcRectToDstRect.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001112 }
1113
joshualitta61c8172015-08-17 10:51:22 -07001114 // If we have a maskfilter then we can't batch, so we take a slow path. However, we fast
1115 // path the case where we are drawing an AA rect so we can batch many drawImageRect calls
1116 if (paint.getMaskFilter()) {
1117 SkPaint paintWithShader(paint);
1118 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
1119 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
1120 &srcRectToDstRect))->unref();
1121 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1122 this->drawRect(draw, dstRect, paintWithShader);
1123 } else {
1124 draw_aa_bitmap(fDrawContext, fContext, fRenderTarget, fClip, *draw.fMatrix,
1125 srcRectToDstRect, paint, bitmapPtr, dstSize);
1126 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001127
1128 return;
1129 }
1130
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001131 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1132 // the view matrix rather than a local matrix.
joshualitt5531d512014-12-17 15:50:11 -08001133 SkMatrix viewM = *draw.fMatrix;
reed85d91782015-09-10 14:33:38 -07001134 viewM.preScale(dstSize.fWidth / srcRect.width(),
1135 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001136
1137 GrTextureParams params;
joshualitt9bc39542015-08-12 12:57:54 -07001138 bool doBicubic;
1139 GrTextureParams::FilterMode textureFilterMode =
1140 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewM, SkMatrix::I(),
1141 &doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001142
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001143 int tileFilterPad;
1144 if (doBicubic) {
1145 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1146 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1147 tileFilterPad = 0;
1148 } else {
1149 tileFilterPad = 1;
1150 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001151 params.setFilterMode(textureFilterMode);
1152
bsalomon8c07b7a2015-11-02 11:36:52 -08001153 maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001154 int tileSize;
1155
1156 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001157 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001158 &clippedSrcRect)) {
reeda5517e22015-07-14 10:54:12 -07001159 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, constraint,
joshualitt5531d512014-12-17 15:50:11 -08001160 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001161 } else {
1162 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001163 bool needsTextureDomain = needs_texture_domain(bitmap,
1164 srcRect,
1165 params,
joshualitt5531d512014-12-17 15:50:11 -08001166 viewM,
robertphillips1accc4c2015-07-20 10:22:29 -07001167 doBicubic,
1168 fRenderTarget->isUnifiedMultisampled());
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001169 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001170 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001171 srcRect,
1172 params,
1173 paint,
reeda5517e22015-07-14 10:54:12 -07001174 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001175 doBicubic,
1176 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001177 }
1178}
1179
1180// Break 'bitmap' into several tiles to draw it since it has already
1181// been determined to be too large to fit in VRAM
1182void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001183 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001184 const SkRect& srcRect,
1185 const SkIRect& clippedSrcIRect,
1186 const GrTextureParams& params,
1187 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001188 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001189 int tileSize,
1190 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001191 // The following pixel lock is technically redundant, but it is desirable
1192 // to lock outside of the tile loop to prevent redecoding the whole image
1193 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1194 // is larger than the limit of the discardable memory pool.
1195 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001196 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1197
1198 int nx = bitmap.width() / tileSize;
1199 int ny = bitmap.height() / tileSize;
1200 for (int x = 0; x <= nx; x++) {
1201 for (int y = 0; y <= ny; y++) {
1202 SkRect tileR;
1203 tileR.set(SkIntToScalar(x * tileSize),
1204 SkIntToScalar(y * tileSize),
1205 SkIntToScalar((x + 1) * tileSize),
1206 SkIntToScalar((y + 1) * tileSize));
1207
1208 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1209 continue;
1210 }
1211
1212 if (!tileR.intersect(srcRect)) {
1213 continue;
1214 }
1215
1216 SkBitmap tmpB;
1217 SkIRect iTileR;
1218 tileR.roundOut(&iTileR);
1219 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1220 SkIntToScalar(iTileR.fTop));
1221
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001222 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001223 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001224 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001225 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001226 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001227
robertphillipsec8bb942014-11-21 10:16:25 -08001228 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001229 SkIRect iClampRect;
1230
reeda5517e22015-07-14 10:54:12 -07001231 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001232 // In bleed mode we want to always expand the tile on all edges
1233 // but stay within the bitmap bounds
1234 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1235 } else {
1236 // In texture-domain/clamp mode we only want to expand the
1237 // tile on edges interior to "srcRect" (i.e., we want to
1238 // not bleed across the original clamped edges)
1239 srcRect.roundOut(&iClampRect);
1240 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001241 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1242 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001243 }
1244
1245 if (bitmap.extractSubset(&tmpB, iTileR)) {
1246 // now offset it to make it "local" to our tmp bitmap
1247 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001248 GrTextureParams paramsTemp = params;
robertphillips1accc4c2015-07-20 10:22:29 -07001249 bool needsTextureDomain = needs_texture_domain(
1250 bitmap, srcRect, paramsTemp,
1251 viewM, bicubic,
1252 fRenderTarget->isUnifiedMultisampled());
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001253 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001254 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001255 tileR,
1256 paramsTemp,
1257 paint,
reeda5517e22015-07-14 10:54:12 -07001258 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001259 bicubic,
1260 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001261 }
1262 }
1263 }
1264}
1265
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001266
1267/*
1268 * This is called by drawBitmap(), which has to handle images that may be too
1269 * large to be represented by a single texture.
1270 *
1271 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1272 * and that non-texture portion of the GrPaint has already been setup.
1273 */
1274void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001275 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001276 const SkRect& srcRect,
1277 const GrTextureParams& params,
1278 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001279 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001280 bool bicubic,
1281 bool needsTextureDomain) {
bsalomon8c07b7a2015-11-02 11:36:52 -08001282 SkASSERT(bitmap.width() <= fContext->caps()->maxTileSize() &&
1283 bitmap.height() <= fContext->caps()->maxTileSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001284
1285 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001286 AutoBitmapTexture abt(fContext, bitmap, params, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001287 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001288 return;
1289 }
1290
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001291 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001292 SkRect paintRect;
1293 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1294 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1295 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1296 SkScalarMul(srcRect.fTop, hInv),
1297 SkScalarMul(srcRect.fRight, wInv),
1298 SkScalarMul(srcRect.fBottom, hInv));
1299
egdaniel79da63f2015-10-09 10:55:16 -07001300 SkMatrix texMatrix;
1301 texMatrix.reset();
1302 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1303 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1304 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1305 // Thus we need to pass in the transform matrix directly to the texture processor used for
1306 // the bitmap draw.
1307 texMatrix.setScale(wInv, hInv);
1308 }
1309
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001310 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001311
1312 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1313 // the rest from the SkPaint.
1314 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001315 SkAutoTUnref<const GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001316
reeda5517e22015-07-14 10:54:12 -07001317 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001318 // Use a constrained texture domain to avoid color bleeding
1319 SkScalar left, top, right, bottom;
1320 if (srcRect.width() > SK_Scalar1) {
1321 SkScalar border = SK_ScalarHalf / texture->width();
1322 left = paintRect.left() + border;
1323 right = paintRect.right() - border;
1324 } else {
1325 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1326 }
1327 if (srcRect.height() > SK_Scalar1) {
1328 SkScalar border = SK_ScalarHalf / texture->height();
1329 top = paintRect.top() + border;
1330 bottom = paintRect.bottom() - border;
1331 } else {
1332 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1333 }
1334 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001335 if (bicubic) {
egdaniel79da63f2015-10-09 10:55:16 -07001336 fp.reset(GrBicubicEffect::Create(texture, texMatrix, textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001337 } else {
bsalomon4a339522015-10-06 08:40:50 -07001338 fp.reset(GrTextureDomainEffect::Create(texture,
egdaniel79da63f2015-10-09 10:55:16 -07001339 texMatrix,
joshualitt5531d512014-12-17 15:50:11 -08001340 textureDomain,
1341 GrTextureDomain::kClamp_Mode,
1342 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001343 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001344 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001345 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1346 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
egdaniel79da63f2015-10-09 10:55:16 -07001347 fp.reset(GrBicubicEffect::Create(texture, texMatrix, tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001348 } else {
egdaniel79da63f2015-10-09 10:55:16 -07001349 fp.reset(GrSimpleTextureEffect::Create(texture, texMatrix, params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001350 }
1351
egdaniel79da63f2015-10-09 10:55:16 -07001352 SkAutoTUnref<const GrFragmentProcessor> shaderFP;
1353
bsalomonf1b7a1d2015-09-28 06:26:28 -07001354 if (kAlpha_8_SkColorType == bitmap.colorType()) {
egdaniel79da63f2015-10-09 10:55:16 -07001355 if (const SkShader* shader = paint.getShader()) {
1356 shaderFP.reset(shader->asFragmentProcessor(this->context(),
1357 viewMatrix,
1358 nullptr,
1359 paint.getFilterQuality()));
1360 if (!shaderFP) {
1361 return;
1362 }
1363 const GrFragmentProcessor* fpSeries[] = { shaderFP.get(), fp.get() };
1364 fp.reset(GrFragmentProcessor::RunInSeries(fpSeries, 2));
1365 } else {
1366 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1367 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001368 } else {
1369 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1370 }
1371
1372 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001373 return;
1374 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001375
egdaniel79da63f2015-10-09 10:55:16 -07001376 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1377 // We don't have local coords in this case and have previously set the transform
1378 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001379 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001380 } else {
robertphillips2e1e51f2015-10-15 08:01:48 -07001381 fDrawContext->drawNonAARectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001382 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001383}
1384
fmalita2d97bc12014-11-20 10:44:58 -08001385bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001386 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001387 const SkImageFilter* filter,
1388 const SkImageFilter::Context& ctx,
1389 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001390 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001391
reed88d064d2015-10-12 11:30:02 -07001392 SkImageFilter::DeviceProxy proxy(this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001393
1394 if (filter->canFilterImageGPU()) {
senorblancod0d37ca2015-04-02 04:54:56 -07001395 return filter->filterImageGPU(&proxy, wrap_texture(texture, width, height),
1396 ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001397 } else {
1398 return false;
1399 }
1400}
1401
1402void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1403 int left, int top, const SkPaint& paint) {
1404 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001405 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001406
1407 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1408 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1409 return;
1410 }
1411
1412 int w = bitmap.width();
1413 int h = bitmap.height();
1414
1415 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001416 // draw sprite neither filters nor tiles.
1417 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001418 if (!texture) {
1419 return;
1420 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001421
bsalomonf1b7a1d2015-09-28 06:26:28 -07001422 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
1423
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001424 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001425 // This bitmap will own the filtered result as a texture.
1426 SkBitmap filteredBitmap;
1427
bsalomon49f085d2014-09-05 13:34:00 -07001428 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001429 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001430 SkMatrix matrix(*draw.fMatrix);
1431 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001432 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001433 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001434 // This cache is transient, and is freed (along with all its contained
1435 // textures) when it goes out of scope.
reedc9b5f8b2015-10-22 13:20:20 -07001436 SkImageFilter::Context ctx(matrix, clipBounds, cache, SkImageFilter::kApprox_SizeConstraint);
senorblancod0d37ca2015-04-02 04:54:56 -07001437 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001438 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001439 texture = (GrTexture*) filteredBitmap.getTexture();
1440 w = filteredBitmap.width();
1441 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001442 left += offset.x();
1443 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001444 } else {
1445 return;
1446 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001447 SkASSERT(!GrPixelConfigIsAlphaOnly(texture->config()));
1448 alphaOnly = false;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001449 }
1450
1451 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001452 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001453 GrSimpleTextureEffect::Create(texture, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001454 if (alphaOnly) {
1455 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1456 } else {
1457 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1458 }
1459 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001460 return;
1461 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001462
robertphillips2e1e51f2015-10-15 08:01:48 -07001463 fDrawContext->drawNonAARectToRect(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001464 grPaint,
1465 SkMatrix::I(),
1466 SkRect::MakeXYWH(SkIntToScalar(left),
1467 SkIntToScalar(top),
1468 SkIntToScalar(w),
1469 SkIntToScalar(h)),
1470 SkRect::MakeXYWH(0,
1471 0,
1472 SK_Scalar1 * w / texture->width(),
1473 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001474}
1475
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001476void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001477 const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -07001478 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001479 SkMatrix matrix;
1480 SkRect bitmapBounds, tmpSrc;
1481
1482 bitmapBounds.set(0, 0,
1483 SkIntToScalar(bitmap.width()),
1484 SkIntToScalar(bitmap.height()));
1485
1486 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001487 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001488 tmpSrc = *src;
1489 } else {
1490 tmpSrc = bitmapBounds;
1491 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001492
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001493 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1494
1495 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001496 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001497 if (!bitmapBounds.contains(tmpSrc)) {
1498 if (!tmpSrc.intersect(bitmapBounds)) {
1499 return; // nothing to draw
1500 }
1501 }
1502 }
1503
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001504 SkRect tmpDst;
1505 matrix.mapRect(&tmpDst, tmpSrc);
1506
1507 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1508 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1509 // Translate so that tempDst's top left is at the origin.
1510 matrix = *origDraw.fMatrix;
1511 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1512 draw.writable()->fMatrix = &matrix;
1513 }
1514 SkSize dstSize;
1515 dstSize.fWidth = tmpDst.width();
1516 dstSize.fHeight = tmpDst.height();
1517
reeda5517e22015-07-14 10:54:12 -07001518 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, constraint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001519}
1520
1521void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1522 int x, int y, const SkPaint& paint) {
1523 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001524 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001525 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001526
1527 // TODO: If the source device covers the whole of this device, we could
1528 // omit fNeedsClear -related flushing.
1529 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001530
1531 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001532 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001533
1534 GrRenderTarget* devRT = dev->accessRenderTarget();
1535 GrTexture* devTex;
halcanary96fcdcc2015-08-27 07:41:13 -07001536 if (nullptr == (devTex = devRT->asTexture())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001537 return;
1538 }
1539
robertphillips7b9e8a42014-12-11 08:20:31 -08001540 const SkImageInfo ii = dev->imageInfo();
1541 int w = ii.width();
1542 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001543
1544 SkImageFilter* filter = paint.getImageFilter();
1545 // This bitmap will own the filtered result as a texture.
1546 SkBitmap filteredBitmap;
1547
bsalomon49f085d2014-09-05 13:34:00 -07001548 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001549 SkIPoint offset = SkIPoint::Make(0, 0);
1550 SkMatrix matrix(*draw.fMatrix);
1551 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001552 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001553 // This cache is transient, and is freed (along with all its contained
1554 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001555 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
reedc9b5f8b2015-10-22 13:20:20 -07001556 SkImageFilter::Context ctx(matrix, clipBounds, cache, SkImageFilter::kApprox_SizeConstraint);
senorblancod0d37ca2015-04-02 04:54:56 -07001557 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1558 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001559 devTex = filteredBitmap.getTexture();
1560 w = filteredBitmap.width();
1561 h = filteredBitmap.height();
1562 x += offset.fX;
1563 y += offset.fY;
1564 } else {
1565 return;
1566 }
1567 }
1568
1569 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001570 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001571 GrSimpleTextureEffect::Create(devTex, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001572 if (GrPixelConfigIsAlphaOnly(devTex->config())) {
1573 // Can this happen?
1574 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1575 } else {
1576 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1577 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001578
bsalomonf1b7a1d2015-09-28 06:26:28 -07001579 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001580 return;
1581 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001582
1583 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1584 SkIntToScalar(y),
1585 SkIntToScalar(w),
1586 SkIntToScalar(h));
1587
1588 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1589 // scratch texture).
1590 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1591 SK_Scalar1 * h / devTex->height());
1592
robertphillips2e1e51f2015-10-15 08:01:48 -07001593 fDrawContext->drawNonAARectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001594}
1595
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001596bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001597 return filter->canFilterImageGPU();
1598}
1599
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001600bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001601 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001602 SkBitmap* result, SkIPoint* offset) {
1603 // want explicitly our impl, so guard against a subclass of us overriding it
1604 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1605 return false;
1606 }
1607
1608 SkAutoLockPixels alp(src, !src.getTexture());
1609 if (!src.getTexture() && !src.readyToDraw()) {
1610 return false;
1611 }
1612
1613 GrTexture* texture;
1614 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1615 // must be pushed upstack.
bsalomonafa95e22015-10-12 10:39:46 -07001616 AutoBitmapTexture abt(fContext, src, GrTextureParams::ClampNoFilter(), &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001617 if (!texture) {
1618 return false;
1619 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001620
senorblancod0d37ca2015-04-02 04:54:56 -07001621 return this->filterTexture(fContext, texture, src.width(), src.height(),
1622 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001623}
1624
reed85d91782015-09-10 14:33:38 -07001625static bool wrap_as_bm(GrContext* ctx, const SkImage* image, SkBitmap* bm) {
bsalomonafa95e22015-10-12 10:39:46 -07001626 // TODO: It is wrong to assume these texture params here.
1627 SkAutoTUnref<GrTexture> tex(as_IB(image)->asTextureRef(ctx, GrTextureParams::ClampNoFilter()));
reeda85d4d02015-05-06 12:56:48 -07001628 if (tex) {
reed8b26b992015-05-07 15:36:17 -07001629 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), bm);
1630 return true;
reeda85d4d02015-05-06 12:56:48 -07001631 } else {
reed8b26b992015-05-07 15:36:17 -07001632 return as_IB(image)->getROPixels(bm);
reeda85d4d02015-05-06 12:56:48 -07001633 }
reeda85d4d02015-05-06 12:56:48 -07001634}
1635
1636void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1637 const SkPaint& paint) {
1638 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001639 if (GrTexture* tex = as_IB(image)->peekTexture()) {
1640 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), &bm);
1641 } else {
1642 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1643 paint.getFilterQuality(), *draw.fMatrix)) {
1644 // only support tiling as bitmap at the moment, so force raster-version
1645 if (!as_IB(image)->getROPixels(&bm)) {
1646 return;
1647 }
1648 } else {
1649 if (!wrap_as_bm(this->context(), image, &bm)) {
1650 return;
1651 }
1652 }
reeda85d4d02015-05-06 12:56:48 -07001653 }
reed85d91782015-09-10 14:33:38 -07001654 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reeda85d4d02015-05-06 12:56:48 -07001655}
1656
1657void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001658 const SkRect& dst, const SkPaint& paint,
1659 SkCanvas::SrcRectConstraint constraint) {
reeda85d4d02015-05-06 12:56:48 -07001660 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001661 if (GrTexture* tex = as_IB(image)->peekTexture()) {
1662 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), &bm);
1663 } else {
1664 SkMatrix viewMatrix = *draw.fMatrix;
1665 viewMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1666 dst.height() / (src ? src->height() : image->height()));
1667
1668 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), viewMatrix)) {
1669 // only support tiling as bitmap at the moment, so force raster-version
1670 if (!as_IB(image)->getROPixels(&bm)) {
1671 return;
1672 }
1673 } else {
1674 if (!wrap_as_bm(this->context(), image, &bm)) {
1675 return;
1676 }
1677 }
reeda85d4d02015-05-06 12:56:48 -07001678 }
reed85d91782015-09-10 14:33:38 -07001679 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001680}
1681
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001682///////////////////////////////////////////////////////////////////////////////
1683
1684// must be in SkCanvas::VertexMode order
1685static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1686 kTriangles_GrPrimitiveType,
1687 kTriangleStrip_GrPrimitiveType,
1688 kTriangleFan_GrPrimitiveType,
1689};
1690
1691void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1692 int vertexCount, const SkPoint vertices[],
1693 const SkPoint texs[], const SkColor colors[],
1694 SkXfermode* xmode,
1695 const uint16_t indices[], int indexCount,
1696 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001697 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001698 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001699
halcanary96fcdcc2015-08-27 07:41:13 -07001700 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1701 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001702
halcanary96fcdcc2015-08-27 07:41:13 -07001703 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001704
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001705 SkPaint copy(paint);
1706 copy.setStyle(SkPaint::kStroke_Style);
1707 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001708
bsalomonf1b7a1d2015-09-28 06:26:28 -07001709 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001710 // we ignore the shader if texs is null.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001711 if (!SkPaintToGrPaintNoShader(this->context(), copy, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001712 return;
1713 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001714
dandov32a311b2014-07-15 19:46:26 -07001715 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001716 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001717 switch (vmode) {
1718 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001719 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001720 break;
1721 case SkCanvas::kTriangleStrip_VertexMode:
1722 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001723 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001724 break;
1725 }
mtklein533eb782014-08-27 10:39:42 -07001726
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001727 VertState state(vertexCount, indices, indexCount);
1728 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001729
dandov32a311b2014-07-15 19:46:26 -07001730 //number of indices for lines per triangle with kLines
1731 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001732
bsalomonf1b7a1d2015-09-28 06:26:28 -07001733 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001734 int i = 0;
1735 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001736 lineIndices[i] = state.f0;
1737 lineIndices[i + 1] = state.f1;
1738 lineIndices[i + 2] = state.f1;
1739 lineIndices[i + 3] = state.f2;
1740 lineIndices[i + 4] = state.f2;
1741 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001742 i += 6;
1743 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001744 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001745 grPaint,
1746 *draw.fMatrix,
1747 kLines_GrPrimitiveType,
1748 vertexCount,
1749 vertices,
1750 texs,
1751 colors,
1752 lineIndices.get(),
1753 indexCount);
1754 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001755 }
1756
bsalomonf1b7a1d2015-09-28 06:26:28 -07001757 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001758
1759 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001760 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001761 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1762 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001763 convertedColors.reset(vertexCount);
1764 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001765 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001766 }
1767 colors = convertedColors.get();
1768 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001769 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001770 if (texs && paint.getShader()) {
1771 if (colors) {
1772 // When there are texs and colors the shader and colors are combined using xmode. A null
1773 // xmode is defined to mean modulate.
1774 SkXfermode::Mode colorMode;
1775 if (xmode) {
1776 if (!xmode->asMode(&colorMode)) {
1777 return;
1778 }
1779 } else {
1780 colorMode = SkXfermode::kModulate_Mode;
1781 }
1782 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
1783 false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001784 return;
1785 }
1786 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001787 // We have a shader, but no colors to blend it against.
1788 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
1789 return;
1790 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001791 }
bsalomonaa48d362015-10-01 08:34:17 -07001792 } else {
1793 if (colors) {
1794 // We have colors, but either have no shader or no texture coords (which implies that
1795 // we should ignore the shader).
1796 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint, &grPaint)) {
1797 return;
1798 }
1799 } else {
1800 // No colors and no shaders. Just draw with the paint color.
1801 if (!SkPaintToGrPaintNoShader(this->context(), paint, &grPaint)) {
1802 return;
1803 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001804 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001805 }
1806
robertphillips2e1e51f2015-10-15 08:01:48 -07001807 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001808 grPaint,
1809 *draw.fMatrix,
1810 primType,
1811 vertexCount,
1812 vertices,
1813 texs,
1814 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001815 indices,
robertphillipsea461502015-05-26 11:38:03 -07001816 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001817}
1818
1819///////////////////////////////////////////////////////////////////////////////
1820
jvanverth31ff7622015-08-07 10:09:28 -07001821void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001822 const SkRect texRect[], const SkColor colors[], int count,
1823 SkXfermode::Mode mode, const SkPaint& paint) {
1824 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001825 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001826 return;
1827 }
1828
jvanverth31ff7622015-08-07 10:09:28 -07001829 CHECK_SHOULD_DRAW(draw);
1830 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
1831
reedca109532015-06-25 16:25:25 -07001832 SkPaint p(paint);
1833 p.setShader(atlas->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
1834
jvanverth31ff7622015-08-07 10:09:28 -07001835 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001836 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001837 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
1838 &grPaint)) {
1839 return;
1840 }
1841 } else {
1842 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix, &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001843 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001844 }
1845 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001846
1847 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001848 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001849}
1850
1851///////////////////////////////////////////////////////////////////////////////
1852
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001853void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001854 size_t byteLength, SkScalar x, SkScalar y,
1855 const SkPaint& paint) {
1856 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001857 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001858
jvanverth8c27a182014-10-14 08:45:50 -07001859 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001860 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001861 return;
1862 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001863
jvanverth8c27a182014-10-14 08:45:50 -07001864 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001865
robertphillips2e1e51f2015-10-15 08:01:48 -07001866 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001867 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001868}
1869
fmalita05c4a432014-09-29 06:29:53 -07001870void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1871 const SkScalar pos[], int scalarsPerPos,
1872 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001873 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001874 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001875
jvanverth8c27a182014-10-14 08:45:50 -07001876 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001877 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001878 return;
1879 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001880
jvanverth8c27a182014-10-14 08:45:50 -07001881 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001882
robertphillips2e1e51f2015-10-15 08:01:48 -07001883 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001884 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1885 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001886}
1887
joshualitt9c328182015-03-23 08:13:04 -07001888void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1889 const SkPaint& paint, SkDrawFilter* drawFilter) {
1890 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawTextBlob", fContext);
1891 CHECK_SHOULD_DRAW(draw);
1892
1893 SkDEBUGCODE(this->validate();)
1894
robertphillips2e1e51f2015-10-15 08:01:48 -07001895 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001896 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001897}
1898
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001899///////////////////////////////////////////////////////////////////////////////
1900
reedb2db8982014-11-13 12:41:02 -08001901bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
robertphillips9c240a12015-05-28 07:45:59 -07001902 return GrTextContext::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001903}
1904
1905void SkGpuDevice::flush() {
1906 DO_DEFERRED_CLEAR();
bsalomonc49e8682015-06-30 11:37:35 -07001907 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001908}
1909
1910///////////////////////////////////////////////////////////////////////////////
1911
reed76033be2015-03-14 10:54:31 -07001912SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
bsalomonf2703d82014-10-28 14:33:06 -07001913 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001914 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001915 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001916 desc.fWidth = cinfo.fInfo.width();
1917 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001918 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001919
1920 SkAutoTUnref<GrTexture> texture;
1921 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001922 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001923
hcm4396fa52014-10-27 21:43:30 -07001924 // layers are never draw in repeat modes, so we can request an approx
1925 // match and ignore any padding.
bsalomoneae62002015-07-31 13:59:30 -07001926 if (kNever_TileUsage == cinfo.fTileUsage) {
1927 texture.reset(fContext->textureProvider()->createApproxTexture(desc));
1928 } else {
1929 texture.reset(fContext->textureProvider()->createTexture(desc, true));
1930 }
bsalomonafe30052015-01-16 07:32:33 -08001931
1932 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001933 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001934 return SkGpuDevice::Create(
bsalomon74f681d2015-06-23 14:38:48 -07001935 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, init);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001936 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001937 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001938 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001939 cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary96fcdcc2015-08-27 07:41:13 -07001940 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001941 }
1942}
1943
reed4a8126e2014-09-22 07:29:03 -07001944SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001945 // TODO: Change the signature of newSurface to take a budgeted parameter.
1946 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
vbuzinovdded6962015-06-12 08:59:45 -07001947 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
bsalomonafe30052015-01-16 07:32:33 -08001948 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001949}
1950
robertphillips30d2cc62014-09-24 08:52:18 -07001951bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001952 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001953#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001954 // todo: should handle this natively
1955 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001956 return false;
1957 }
1958
halcanary96fcdcc2015-08-27 07:41:13 -07001959 const SkBigPicture::AccelData* data = nullptr;
mtklein9db912c2015-05-19 11:11:26 -07001960 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1961 data = bp->accelData();
1962 }
robertphillips81f71b62014-11-11 04:54:49 -08001963 if (!data) {
1964 return false;
1965 }
1966
robertphillipse5524cd2015-02-20 12:30:26 -08001967 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1968 if (0 == gpuData->numBlocks()) {
1969 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001970 }
1971
robertphillipsfd61ed02014-10-28 07:21:44 -07001972 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001973
robertphillipse5524cd2015-02-20 12:30:26 -08001974 SkIRect iBounds;
1975 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1976 return false;
1977 }
1978
1979 SkRect clipBounds = SkRect::Make(iBounds);
1980
1981 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1982
robertphillipsfd61ed02014-10-28 07:21:44 -07001983 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001984 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001985 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001986 &atlasedNeedRendering, &atlasedRecycled,
vbuzinovdded6962015-06-12 08:59:45 -07001987 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001988
1989 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1990
1991 SkTDArray<GrHoistedLayer> needRendering, recycled;
1992
robertphillipse5524cd2015-02-20 12:30:26 -08001993 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1994
robertphillipsfd61ed02014-10-28 07:21:44 -07001995 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001996 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001997 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001998 &needRendering, &recycled,
vbuzinovdded6962015-06-12 08:59:45 -07001999 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07002000
2001 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002002
robertphillips64bf7672014-08-21 13:07:35 -07002003 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08002004 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
halcanary96fcdcc2015-08-27 07:41:13 -07002005 initialMatrix, nullptr);
robertphillips64bf7672014-08-21 13:07:35 -07002006
robertphillipsfd61ed02014-10-28 07:21:44 -07002007 GrLayerHoister::UnlockLayers(fContext, needRendering);
2008 GrLayerHoister::UnlockLayers(fContext, recycled);
2009 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
2010 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07002011
2012 return true;
robertphillips63242d72014-12-04 08:31:02 -08002013#else
2014 return false;
2015#endif
robertphillips64bf7672014-08-21 13:07:35 -07002016}
2017
reed13ccbf82015-10-20 09:56:52 -07002018SkImageFilter::Cache* SkGpuDevice::NewImageFilterCache() {
2019 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
2020}
2021
senorblancobe129b22014-08-08 07:14:35 -07002022SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07002023 // We always return a transient cache, so it is freed after each
2024 // filter traversal.
reed13ccbf82015-10-20 09:56:52 -07002025 return SkGpuDevice::NewImageFilterCache();
senorblanco55b6d8b2014-07-30 11:26:46 -07002026}
reedf037e0b2014-10-30 11:34:15 -07002027
2028#endif