blob: a3f00c1434f43f504d1cae0acf27cb77971169a7 [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"
herb11a7f7f2015-11-24 12:41:00 -080012#include "SkDraw.h"
kkinnunenabcfab42015-02-22 22:53:44 -080013#include "GrGpu.h"
14#include "GrGpuResourcePriv.h"
bsalomonc55271f2015-11-09 11:55:57 -080015#include "GrImageIDTextureAdjuster.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"
egdanielbbcb38d2014-06-19 10:19:29 -070019#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080020#include "SkCanvasPriv.h"
kkinnunenabcfab42015-02-22 22:53:44 -080021#include "SkErrorInternals.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000022#include "SkGlyphCache.h"
kkinnunenabcfab42015-02-22 22:53:44 -080023#include "SkGrTexturePixelRef.h"
bsalomonf276ac52015-10-09 13:36:42 -070024#include "SkGr.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070025#include "SkGrPriv.h"
reeda85d4d02015-05-06 12:56:48 -070026#include "SkImage_Base.h"
bsalomon1cf6f9b2015-12-08 10:53:43 -080027#include "SkImageCacherator.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080029#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000030#include "SkMaskFilter.h"
joshualitt33a5fce2015-11-18 13:28:51 -080031#include "SkNinePatchIter.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"
joshualitte8042922015-12-11 06:11:21 -080049#include "text/GrTextContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000050
reedf037e0b2014-10-30 11:34:15 -070051#if SK_SUPPORT_GPU
52
joshualittce894002016-01-11 13:29:31 -080053#define ASSERT_SINGLE_OWNER \
54 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->debugSingleOwner());)
55
senorblanco55b6d8b2014-07-30 11:26:46 -070056enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
57
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058#if 0
59 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080060 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000061 do { \
62 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080063 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000064 } while (0)
65#else
joshualitt5531d512014-12-17 15:50:11 -080066 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000067#endif
68
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000069#define DO_DEFERRED_CLEAR() \
70 do { \
bsalomonafe30052015-01-16 07:32:33 -080071 if (fNeedClear) { \
72 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000073 } \
74 } while (false) \
75
76///////////////////////////////////////////////////////////////////////////////
77
78#define CHECK_FOR_ANNOTATION(paint) \
79 do { if (paint.getAnnotation()) { return; } } while (0)
80
81///////////////////////////////////////////////////////////////////////////////
82
bsalomonbcf0a522014-10-08 08:40:09 -070083// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
84// just accesses the backing GrTexture. Otherwise, it creates a cached texture
85// representation and releases it in the destructor.
86class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040087public:
bsalomonbcf0a522014-10-08 08:40:09 -070088 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070089
bsalomonbcf0a522014-10-08 08:40:09 -070090 AutoBitmapTexture(GrContext* context,
91 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -070092 const GrTextureParams& params,
bsalomonbcf0a522014-10-08 08:40:09 -070093 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040094 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070095 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040096 }
97
bsalomonbcf0a522014-10-08 08:40:09 -070098 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040099 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -0700100 const GrTextureParams& params) {
bsalomonbcf0a522014-10-08 08:40:09 -0700101 // Either get the texture directly from the bitmap, or else use the cache and
102 // remember to unref it.
103 if (GrTexture* bmpTexture = bitmap.getTexture()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700104 fTexture.reset(nullptr);
bsalomonbcf0a522014-10-08 08:40:09 -0700105 return bmpTexture;
106 } else {
107 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
108 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400109 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400110 }
111
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000112private:
bsalomonbcf0a522014-10-08 08:40:09 -0700113 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000114};
115
116///////////////////////////////////////////////////////////////////////////////
117
bsalomon74f681d2015-06-23 14:38:48 -0700118/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
119 should fail. */
120bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
121 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
122 *flags = 0;
123 if (info) {
124 switch (info->alphaType()) {
125 case kPremul_SkAlphaType:
126 break;
127 case kOpaque_SkAlphaType:
128 *flags |= SkGpuDevice::kIsOpaque_Flag;
129 break;
130 default: // If it is unpremul or unknown don't try to render
131 return false;
132 }
133 }
134 if (kClear_InitContents == init) {
135 *flags |= kNeedClear_Flag;
136 }
137 return true;
138}
139
140SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props,
141 InitContents init) {
142 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, init);
senorblancod0d37ca2015-04-02 04:54:56 -0700143}
144
145SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
bsalomon74f681d2015-06-23 14:38:48 -0700146 const SkSurfaceProps* props, InitContents init) {
bsalomonafe30052015-01-16 07:32:33 -0800147 if (!rt || rt->wasDestroyed()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700148 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000149 }
bsalomon74f681d2015-06-23 14:38:48 -0700150 unsigned flags;
halcanary96fcdcc2015-08-27 07:41:13 -0700151 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
152 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700153 }
halcanary385fe4d2015-08-26 13:07:48 -0700154 return new SkGpuDevice(rt, width, height, props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000155}
156
bsalomon74f681d2015-06-23 14:38:48 -0700157SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
158 const SkImageInfo& info, int sampleCount,
159 const SkSurfaceProps* props, InitContents init) {
160 unsigned flags;
161 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700162 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700163 }
164
165 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
halcanary96fcdcc2015-08-27 07:41:13 -0700166 if (nullptr == rt) {
167 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700168 }
169
halcanary385fe4d2015-08-26 13:07:48 -0700170 return new SkGpuDevice(rt, info.width(), info.height(), props, flags);
bsalomon74f681d2015-06-23 14:38:48 -0700171}
172
senorblancod0d37ca2015-04-02 04:54:56 -0700173SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
174 const SkSurfaceProps* props, unsigned flags)
robertphillipsfcf78292015-06-19 11:49:52 -0700175 : INHERITED(SkSurfacePropsCopyOrDefault(props))
joshualitt06dd0a82016-01-08 06:32:31 -0800176 , fContext(SkRef(rt->getContext()))
177 , fRenderTarget(SkRef(rt)) {
bsalomon74f681d2015-06-23 14:38:48 -0700178 fNeedClear = SkToBool(flags & kNeedClear_Flag);
179 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000180
bsalomon74f681d2015-06-23 14:38:48 -0700181 SkAlphaType at = fOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
182 SkImageInfo info = rt->surfacePriv().info(at).makeWH(width, height);
halcanary385fe4d2015-08-26 13:07:48 -0700183 SkPixelRef* pr = new SkGrPixelRef(info, rt);
bsalomonafbf2d62014-09-30 12:18:44 -0700184 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700185 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700186
robertphillips77a2e522015-10-17 07:43:27 -0700187 fDrawContext.reset(this->context()->drawContext(rt, &this->surfaceProps()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000188}
189
kkinnunenabcfab42015-02-22 22:53:44 -0800190GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
191 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000192 if (kUnknown_SkColorType == origInfo.colorType() ||
193 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700194 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000195 }
196
bsalomonafe30052015-01-16 07:32:33 -0800197 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700198 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800199 }
200
reede5ea5002014-09-03 11:54:58 -0700201 SkColorType ct = origInfo.colorType();
202 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700203 if (kRGB_565_SkColorType == ct) {
204 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800205 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
206 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700207 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800208 }
209 if (kOpaque_SkAlphaType != at) {
210 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000211 }
reede5ea5002014-09-03 11:54:58 -0700212 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000213
bsalomonf2703d82014-10-28 14:33:06 -0700214 GrSurfaceDesc desc;
215 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000216 desc.fWidth = info.width();
217 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000218 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000219 desc.fSampleCnt = sampleCount;
bsalomond309e7a2015-04-30 14:18:54 -0700220 GrTexture* texture = context->textureProvider()->createTexture(
halcanary96fcdcc2015-08-27 07:41:13 -0700221 desc, SkToBool(budgeted), nullptr, 0);
222 if (nullptr == texture) {
223 return nullptr;
kkinnunenabcfab42015-02-22 22:53:44 -0800224 }
halcanary96fcdcc2015-08-27 07:41:13 -0700225 SkASSERT(nullptr != texture->asRenderTarget());
kkinnunenabcfab42015-02-22 22:53:44 -0800226 return texture->asRenderTarget();
227}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000228
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000229///////////////////////////////////////////////////////////////////////////////
230
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000231bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
232 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800233 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000234 DO_DEFERRED_CLEAR();
235
236 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000237 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000238 if (kUnknown_GrPixelConfig == config) {
239 return false;
240 }
241
242 uint32_t flags = 0;
243 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
244 flags = GrContext::kUnpremul_PixelOpsFlag;
245 }
bsalomon74f681d2015-06-23 14:38:48 -0700246 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
247 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000248}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000249
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000250bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
251 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800252 ASSERT_SINGLE_OWNER
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000253 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000254 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000255 if (kUnknown_GrPixelConfig == config) {
256 return false;
257 }
258 uint32_t flags = 0;
259 if (kUnpremul_SkAlphaType == info.alphaType()) {
260 flags = GrContext::kUnpremul_PixelOpsFlag;
261 }
262 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
263
264 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700265 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000266
267 return true;
268}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000269
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000270const SkBitmap& SkGpuDevice::onAccessBitmap() {
joshualittce894002016-01-11 13:29:31 -0800271 ASSERT_SINGLE_OWNER
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000272 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700273 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000274}
275
reed41e010c2015-06-09 12:16:53 -0700276bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800277 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700278 DO_DEFERRED_CLEAR();
279 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
280 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
281 // ... ugh.
282 fLegacyBitmap.notifyPixelsChanged();
283 return false;
284}
285
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000286void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
joshualittce894002016-01-11 13:29:31 -0800287 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000288 INHERITED::onAttachToCanvas(canvas);
289
290 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800291 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000292}
293
294void SkGpuDevice::onDetachFromCanvas() {
joshualittce894002016-01-11 13:29:31 -0800295 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000296 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800297 fClip.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700298 fClipStack.reset(nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000299}
300
301// call this every draw call, to ensure that the context reflects our state,
302// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800303void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800304 ASSERT_SINGLE_OWNER
joshualitt44701df2015-02-23 14:44:57 -0800305 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000306
joshualitt44701df2015-02-23 14:44:57 -0800307 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000308
joshualitt570d2f82015-02-25 13:19:48 -0800309 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000310
311 DO_DEFERRED_CLEAR();
312}
313
314GrRenderTarget* SkGpuDevice::accessRenderTarget() {
joshualittce894002016-01-11 13:29:31 -0800315 ASSERT_SINGLE_OWNER
robertphillips7156b092015-05-14 08:54:12 -0700316 DO_DEFERRED_CLEAR();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000317 return fRenderTarget;
318}
319
reed8eddfb52014-12-04 07:50:14 -0800320void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800321 ASSERT_SINGLE_OWNER
reed8eddfb52014-12-04 07:50:14 -0800322 GrColor color = 0;
joshualitt5651ee62016-01-11 10:39:11 -0800323 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext);
reed8eddfb52014-12-04 07:50:14 -0800324 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700325 fDrawContext->clear(&rect, color, true);
bsalomonafe30052015-01-16 07:32:33 -0800326 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800327}
328
kkinnunenabcfab42015-02-22 22:53:44 -0800329void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800330 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800331 // Caller must have accessed the render target, because it knows the rt must be replaced.
332 SkASSERT(!fNeedClear);
333
334 SkSurface::Budgeted budgeted =
335 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
336 : SkSurface::kNo_Budgeted;
337
338 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
robertphillips77a2e522015-10-17 07:43:27 -0700339 this->context(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt));
kkinnunenabcfab42015-02-22 22:53:44 -0800340
halcanary96fcdcc2015-08-27 07:41:13 -0700341 if (nullptr == newRT) {
kkinnunenabcfab42015-02-22 22:53:44 -0800342 return;
343 }
344
345 if (shouldRetainContent) {
346 if (fRenderTarget->wasDestroyed()) {
347 return;
348 }
349 this->context()->copySurface(newRT, fRenderTarget);
350 }
351
352 SkASSERT(fRenderTarget != newRT);
353
joshualitt06dd0a82016-01-08 06:32:31 -0800354 fRenderTarget.reset(newRT.detach());
kkinnunenabcfab42015-02-22 22:53:44 -0800355
bsalomon74f681d2015-06-23 14:38:48 -0700356#ifdef SK_DEBUG
357 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
358 kPremul_SkAlphaType);
359 SkASSERT(info == fLegacyBitmap.info());
360#endif
halcanary385fe4d2015-08-26 13:07:48 -0700361 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget);
kkinnunenabcfab42015-02-22 22:53:44 -0800362 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700363
robertphillips77a2e522015-10-17 07:43:27 -0700364 fDrawContext.reset(this->context()->drawContext(fRenderTarget, &this->surfaceProps()));
kkinnunenabcfab42015-02-22 22:53:44 -0800365}
366
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000367///////////////////////////////////////////////////////////////////////////////
368
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000369void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800370 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800371 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800372 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000373
374 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700375 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700376 return;
377 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000378
robertphillips2e1e51f2015-10-15 08:01:48 -0700379 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000380}
381
382// must be in SkCanvas::PointMode order
383static const GrPrimitiveType gPointMode2PrimtiveType[] = {
384 kPoints_GrPrimitiveType,
385 kLines_GrPrimitiveType,
386 kLineStrip_GrPrimitiveType
387};
388
ethannicholas330bb952015-07-17 06:44:02 -0700389// suppress antialiasing on axis-aligned integer-coordinate lines
390static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
391 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
392 return false;
393 }
394 if (count == 2) {
395 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
396 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800397 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700398 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800399 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700400 // the two end pixels as being the lesser of two evils.
401 if (pts[0].fX == pts[1].fX) {
402 return ((int) pts[0].fX) != pts[0].fX;
403 }
404 if (pts[0].fY == pts[1].fY) {
405 return ((int) pts[0].fY) != pts[0].fY;
406 }
407 }
408 return true;
409}
410
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000411void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
412 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800413 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800414 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000415 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800416 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000417
418 SkScalar width = paint.getStrokeWidth();
419 if (width < 0) {
420 return;
421 }
422
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000423 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700424 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
425 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700426 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700427 return;
428 }
egdaniele61c4112014-06-12 10:24:21 -0700429 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700430 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700431 path.moveTo(pts[0]);
432 path.lineTo(pts[1]);
robertphillips2e1e51f2015-10-15 08:01:48 -0700433 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700434 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000435 }
436
ethannicholas330bb952015-07-17 06:44:02 -0700437 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000438 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800439 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700440 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000441 draw.drawPoints(mode, count, pts, paint, true);
442 return;
443 }
444
445 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700446 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700447 return;
448 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000449
robertphillips2e1e51f2015-10-15 08:01:48 -0700450 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700451 grPaint,
452 *draw.fMatrix,
453 gPointMode2PrimtiveType[mode],
454 SkToS32(count),
455 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700456 nullptr,
457 nullptr,
458 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700459 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000460}
461
462///////////////////////////////////////////////////////////////////////////////
463
robertphillipsff55b492015-11-24 07:56:59 -0800464void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800465 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800466 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000467 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800468 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000469
470 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
471 SkScalar width = paint.getStrokeWidth();
472
473 /*
474 We have special code for hairline strokes, miter-strokes, bevel-stroke
475 and fills. Anything else we just call our path code.
476 */
477 bool usePath = doStroke && width > 0 &&
478 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
479 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
egdanield58a0ba2014-06-11 10:30:05 -0700480
robertphillipsd8aa59d2015-08-05 09:07:12 -0700481 // a few other reasons we might need to call drawPath...
robertphillipsff55b492015-11-24 07:56:59 -0800482 if (paint.getMaskFilter() || paint.getPathEffect() ||
robertphillipsd8aa59d2015-08-05 09:07:12 -0700483 paint.getStyle() == SkPaint::kStrokeAndFill_Style) { // we can't both stroke and fill rects
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000484 usePath = true;
485 }
egdanield58a0ba2014-06-11 10:30:05 -0700486
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000487 if (usePath) {
488 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700489 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000490 path.addRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800491 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800492 fClip, path, paint,
493 *draw.fMatrix, nullptr,
494 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000495 return;
496 }
497
498 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700499 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700500 return;
501 }
Mike Klein744fb732014-06-23 15:13:26 -0400502
robertphillipsff55b492015-11-24 07:56:59 -0800503 GrStrokeInfo strokeInfo(paint);
504
robertphillips2e1e51f2015-10-15 08:01:48 -0700505 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000506}
507
508///////////////////////////////////////////////////////////////////////////////
509
510void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800511 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800512 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800513 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000514 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800515 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000516
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000517 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700518 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700519 return;
520 }
Mike Klein744fb732014-06-23 15:13:26 -0400521
egdanield58a0ba2014-06-11 10:30:05 -0700522 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000523 if (paint.getMaskFilter()) {
524 // try to hit the fast path for drawing filtered round rects
525
526 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800527 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000528 if (devRRect.allCornersCircular()) {
529 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700530 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
joshualitt5531d512014-12-17 15:50:11 -0800531 draw.fClip->getBounds(),
532 *draw.fMatrix,
533 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000534 SkIRect finalIRect;
535 maskRect.roundOut(&finalIRect);
536 if (draw.fClip->quickReject(finalIRect)) {
537 // clipped out
538 return;
539 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700540 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
541 fDrawContext,
joshualitt25d9c152015-02-18 12:29:52 -0800542 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800543 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800544 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700545 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700546 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000547 return;
548 }
549 }
550
551 }
552 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000553 }
554
robertphillips514450c2015-11-24 05:36:02 -0800555 if (paint.getMaskFilter() || paint.getPathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800556 // The only mask filter the native rrect drawing code could've handle was taken
557 // care of above.
558 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000559 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700560 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000561 path.addRRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800562 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800563 fClip, path, paint,
564 *draw.fMatrix, nullptr,
565 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000566 return;
567 }
Mike Klein744fb732014-06-23 15:13:26 -0400568
robertphillips514450c2015-11-24 05:36:02 -0800569 SkASSERT(!strokeInfo.isDashed());
570
robertphillips2e1e51f2015-10-15 08:01:48 -0700571 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000572}
573
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000574void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800575 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800576 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800577 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800578 CHECK_FOR_ANNOTATION(paint);
579 CHECK_SHOULD_DRAW(draw);
580
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000581 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000582
robertphillips0e7029e2015-11-30 05:45:06 -0800583 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000584 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700585 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700586 return;
587 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000588
robertphillips0e7029e2015-11-30 05:45:06 -0800589 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
590 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000591 }
592
593 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700594 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000595 path.addRRect(outer);
596 path.addRRect(inner);
597 path.setFillType(SkPath::kEvenOdd_FillType);
598
robertphillips7bceedc2015-12-01 12:51:26 -0800599 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillips0e7029e2015-11-30 05:45:06 -0800600 fClip, path, paint,
601 *draw.fMatrix, nullptr,
602 draw.fClip->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000603}
604
605
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000606/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000607
robertphillips514450c2015-11-24 05:36:02 -0800608void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800609 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800610 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000611 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800612 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000613
robertphillips514450c2015-11-24 05:36:02 -0800614 // Presumably the path effect warps this to something other than an oval
615 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000616 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700617 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000618 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700619 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000620 return;
herb11a7f7f2015-11-24 12:41:00 -0800621 }
622
robertphillips514450c2015-11-24 05:36:02 -0800623 if (paint.getMaskFilter()) {
624 // The RRect path can handle special case blurring
625 SkRRect rr = SkRRect::MakeOval(oval);
626 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000627 }
628
629 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700630 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700631 return;
632 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000633
robertphillips514450c2015-11-24 05:36:02 -0800634 GrStrokeInfo strokeInfo(paint);
635 SkASSERT(!strokeInfo.isDashed());
636
robertphillips2e1e51f2015-10-15 08:01:48 -0700637 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000638}
639
640#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000641
642///////////////////////////////////////////////////////////////////////////////
643
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000644void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
645 const SkPaint& paint, const SkMatrix* prePathMatrix,
646 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800647 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800648 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
649 bool isClosed;
650 SkRect rect;
651 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
652 this->drawRect(draw, rect, paint);
653 return;
654 }
655 if (origSrcPath.isOval(&rect)) {
656 this->drawOval(draw, rect, paint);
657 return;
658 }
659 SkRRect rrect;
660 if (origSrcPath.isRRect(&rrect)) {
661 this->drawRRect(draw, rrect, paint);
662 return;
663 }
664 }
665
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000666 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800667 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800668 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000669
robertphillips7bceedc2015-12-01 12:51:26 -0800670 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsccb1b572015-05-27 11:02:55 -0700671 fClip, origSrcPath, paint,
672 *draw.fMatrix, prePathMatrix,
673 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000674}
675
676static const int kBmpSmallTileSize = 1 << 10;
677
678static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
679 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
680 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
681 return tilesX * tilesY;
682}
683
reed85d91782015-09-10 14:33:38 -0700684static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000685 if (maxTileSize <= kBmpSmallTileSize) {
686 return maxTileSize;
687 }
688
689 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
690 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
691
692 maxTileTotalTileSize *= maxTileSize * maxTileSize;
693 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
694
695 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
696 return kBmpSmallTileSize;
697 } else {
698 return maxTileSize;
699 }
700}
701
702// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
703// pixels from the bitmap are necessary.
bsalomon74f681d2015-06-23 14:38:48 -0700704static void determine_clipped_src_rect(const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800705 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800706 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700707 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000708 const SkRect* srcRectPtr,
709 SkIRect* clippedSrcIRect) {
robertphillips7bceedc2015-12-01 12:51:26 -0800710 clip.getConservativeBounds(rt->width(), rt->height(), clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000711 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800712 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000713 clippedSrcIRect->setEmpty();
714 return;
715 }
716 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
717 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700718 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000719 // we've setup src space 0,0 to map to the top left of the src rect.
720 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000721 if (!clippedSrcRect.intersect(*srcRectPtr)) {
722 clippedSrcIRect->setEmpty();
723 return;
724 }
725 }
726 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700727 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000728 if (!clippedSrcIRect->intersect(bmpBounds)) {
729 clippedSrcIRect->setEmpty();
730 }
731}
732
reed85d91782015-09-10 14:33:38 -0700733bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
734 const SkMatrix& viewMatrix,
735 const GrTextureParams& params,
736 const SkRect* srcRectPtr,
737 int maxTileSize,
738 int* tileSize,
739 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800740 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700741 // if it's larger than the max tile size, then we have no choice but tiling.
742 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
743 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(),
744 srcRectPtr, clippedSubset);
745 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
746 return true;
747 }
748
bsalomon1a1d0b82015-10-16 07:49:42 -0700749 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700750 const size_t area = imageRect.width() * imageRect.height();
751 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
752 return false;
753 }
754
reed85d91782015-09-10 14:33:38 -0700755 // At this point we know we could do the draw by uploading the entire bitmap
756 // as a texture. However, if the texture would be large compared to the
757 // cache size and we don't require most of it for this draw then tile to
758 // reduce the amount of upload and cache spill.
759
760 // assumption here is that sw bitmap size is a good proxy for its size as
761 // a texture
762 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
763 size_t cacheSize;
764 fContext->getResourceCacheLimits(nullptr, &cacheSize);
765 if (bmpSize < cacheSize / 2) {
766 return false;
767 }
768
bsalomon1a1d0b82015-10-16 07:49:42 -0700769 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
770 // tiling memory savings would be < 50%.
reed85d91782015-09-10 14:33:38 -0700771 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(), srcRectPtr,
772 clippedSubset);
773 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
774 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
775 kBmpSmallTileSize * kBmpSmallTileSize;
776
777 return usedTileBytes < 2 * bmpSize;
778}
779
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000780bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800781 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000782 const GrTextureParams& params,
783 const SkRect* srcRectPtr,
784 int maxTileSize,
785 int* tileSize,
786 SkIRect* clippedSrcRect) const {
joshualittce894002016-01-11 13:29:31 -0800787 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000788 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700789 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000790 return false;
791 }
792
reed85d91782015-09-10 14:33:38 -0700793 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
794 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
795}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000796
reed85d91782015-09-10 14:33:38 -0700797bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
798 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
799 const SkMatrix& viewMatrix) const {
joshualittce894002016-01-11 13:29:31 -0800800 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700801 // if image is explictly texture backed then just use the texture
802 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000803 return false;
804 }
805
reed85d91782015-09-10 14:33:38 -0700806 GrTextureParams params;
807 bool doBicubic;
808 GrTextureParams::FilterMode textureFilterMode =
809 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
810
811 int tileFilterPad;
812 if (doBicubic) {
813 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
814 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
815 tileFilterPad = 0;
816 } else {
817 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000818 }
reed85d91782015-09-10 14:33:38 -0700819 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000820
bsalomon8c07b7a2015-11-02 11:36:52 -0800821 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000822
reed85d91782015-09-10 14:33:38 -0700823 // these are output, which we safely ignore, as we just want to know the predicate
824 int outTileSize;
825 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000826
reed85d91782015-09-10 14:33:38 -0700827 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
828 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000829}
830
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000831void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000832 const SkBitmap& bitmap,
833 const SkMatrix& m,
834 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800835 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800836 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800837 SkMatrix viewMatrix;
838 viewMatrix.setConcat(*origDraw.fMatrix, m);
839 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800840 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonb1b01992015-11-18 10:56:08 -0800841 // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
bsalomonf1ecd212015-12-09 17:06:02 -0800842 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
843 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800844 return;
845 }
bsalomonb1b01992015-11-18 10:56:08 -0800846 int maxTileSize = fContext->caps()->maxTileSize();
847
848 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
849 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
850 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
851 paint.isAntiAlias() &&
852 bitmap.width() <= maxTileSize &&
853 bitmap.height() <= maxTileSize;
854
855 bool skipTileCheck = drawAA || paint.getMaskFilter();
856
857 if (!skipTileCheck) {
858 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
859 int tileSize;
860 SkIRect clippedSrcRect;
861
862 GrTextureParams params;
863 bool doBicubic;
864 GrTextureParams::FilterMode textureFilterMode =
865 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
866 &doBicubic);
867
868 int tileFilterPad;
869
870 if (doBicubic) {
871 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
872 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
873 tileFilterPad = 0;
874 } else {
875 tileFilterPad = 1;
876 }
877 params.setFilterMode(textureFilterMode);
878
879 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
880 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
881 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
882 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
883 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
884 return;
885 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000886 }
bsalomonb1b01992015-11-18 10:56:08 -0800887 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800888 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
889 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000890}
891
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000892// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000893// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
894// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000895static inline void clamped_outset_with_offset(SkIRect* iRect,
896 int outset,
897 SkPoint* offset,
898 const SkIRect& clamp) {
899 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000900
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000901 int leftClampDelta = clamp.fLeft - iRect->fLeft;
902 if (leftClampDelta > 0) {
903 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000904 iRect->fLeft = clamp.fLeft;
905 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000906 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000907 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000908
909 int topClampDelta = clamp.fTop - iRect->fTop;
910 if (topClampDelta > 0) {
911 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000912 iRect->fTop = clamp.fTop;
913 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000914 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000915 }
916
917 if (iRect->fRight > clamp.fRight) {
918 iRect->fRight = clamp.fRight;
919 }
920 if (iRect->fBottom > clamp.fBottom) {
921 iRect->fBottom = clamp.fBottom;
922 }
923}
924
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000925// Break 'bitmap' into several tiles to draw it since it has already
926// been determined to be too large to fit in VRAM
927void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800928 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000929 const SkRect& srcRect,
930 const SkIRect& clippedSrcIRect,
931 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800932 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700933 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000934 int tileSize,
935 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800936 ASSERT_SINGLE_OWNER
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000937 // The following pixel lock is technically redundant, but it is desirable
938 // to lock outside of the tile loop to prevent redecoding the whole image
939 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
940 // is larger than the limit of the discardable memory pool.
941 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800942
943 const SkPaint* paint = &origPaint;
944 SkPaint tempPaint;
945 if (origPaint.isAntiAlias() && !fRenderTarget->isUnifiedMultisampled()) {
946 // Drop antialiasing to avoid seams at tile boundaries.
947 tempPaint = origPaint;
948 tempPaint.setAntiAlias(false);
949 paint = &tempPaint;
950 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000951 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
952
953 int nx = bitmap.width() / tileSize;
954 int ny = bitmap.height() / tileSize;
955 for (int x = 0; x <= nx; x++) {
956 for (int y = 0; y <= ny; y++) {
957 SkRect tileR;
958 tileR.set(SkIntToScalar(x * tileSize),
959 SkIntToScalar(y * tileSize),
960 SkIntToScalar((x + 1) * tileSize),
961 SkIntToScalar((y + 1) * tileSize));
962
963 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
964 continue;
965 }
966
967 if (!tileR.intersect(srcRect)) {
968 continue;
969 }
970
971 SkBitmap tmpB;
972 SkIRect iTileR;
973 tileR.roundOut(&iTileR);
974 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
975 SkIntToScalar(iTileR.fTop));
976
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000977 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -0800978 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000979 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000980 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -0800981 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000982
robertphillipsec8bb942014-11-21 10:16:25 -0800983 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000984 SkIRect iClampRect;
985
reeda5517e22015-07-14 10:54:12 -0700986 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000987 // In bleed mode we want to always expand the tile on all edges
988 // but stay within the bitmap bounds
989 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
990 } else {
991 // In texture-domain/clamp mode we only want to expand the
992 // tile on edges interior to "srcRect" (i.e., we want to
993 // not bleed across the original clamped edges)
994 srcRect.roundOut(&iClampRect);
995 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000996 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
997 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000998 }
999
1000 if (bitmap.extractSubset(&tmpB, iTileR)) {
1001 // now offset it to make it "local" to our tmp bitmap
1002 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001003 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001004 // de-optimized this determination
1005 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001006 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001007 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001008 tileR,
1009 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -08001010 *paint,
reeda5517e22015-07-14 10:54:12 -07001011 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001012 bicubic,
1013 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001014 }
1015 }
1016 }
1017}
1018
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001019/*
1020 * This is called by drawBitmap(), which has to handle images that may be too
1021 * large to be represented by a single texture.
1022 *
1023 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1024 * and that non-texture portion of the GrPaint has already been setup.
1025 */
1026void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001027 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001028 const SkRect& srcRect,
1029 const GrTextureParams& params,
1030 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001031 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001032 bool bicubic,
1033 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001034 // We should have already handled bitmaps larger than the max texture size.
1035 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1036 bitmap.height() <= fContext->caps()->maxTextureSize());
1037 // Unless the bitmap is inherently texture-backed, we should be respecting the max tile size
1038 // by the time we get here.
1039 SkASSERT(bitmap.getTexture() ||
1040 (bitmap.width() <= fContext->caps()->maxTileSize() &&
1041 bitmap.height() <= fContext->caps()->maxTileSize()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001042
1043 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001044 AutoBitmapTexture abt(fContext, bitmap, params, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001045 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001046 return;
1047 }
1048
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001049 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001050 SkRect paintRect;
1051 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1052 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1053 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1054 SkScalarMul(srcRect.fTop, hInv),
1055 SkScalarMul(srcRect.fRight, wInv),
1056 SkScalarMul(srcRect.fBottom, hInv));
1057
egdaniel79da63f2015-10-09 10:55:16 -07001058 SkMatrix texMatrix;
1059 texMatrix.reset();
1060 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1061 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1062 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1063 // Thus we need to pass in the transform matrix directly to the texture processor used for
1064 // the bitmap draw.
1065 texMatrix.setScale(wInv, hInv);
1066 }
1067
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001068 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001069
1070 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1071 // the rest from the SkPaint.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001072 SkAutoTUnref<const GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001073
reeda5517e22015-07-14 10:54:12 -07001074 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001075 // Use a constrained texture domain to avoid color bleeding
1076 SkScalar left, top, right, bottom;
1077 if (srcRect.width() > SK_Scalar1) {
1078 SkScalar border = SK_ScalarHalf / texture->width();
1079 left = paintRect.left() + border;
1080 right = paintRect.right() - border;
1081 } else {
1082 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1083 }
1084 if (srcRect.height() > SK_Scalar1) {
1085 SkScalar border = SK_ScalarHalf / texture->height();
1086 top = paintRect.top() + border;
1087 bottom = paintRect.bottom() - border;
1088 } else {
1089 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1090 }
1091 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001092 if (bicubic) {
egdaniel79da63f2015-10-09 10:55:16 -07001093 fp.reset(GrBicubicEffect::Create(texture, texMatrix, textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001094 } else {
bsalomon4a339522015-10-06 08:40:50 -07001095 fp.reset(GrTextureDomainEffect::Create(texture,
egdaniel79da63f2015-10-09 10:55:16 -07001096 texMatrix,
joshualitt5531d512014-12-17 15:50:11 -08001097 textureDomain,
1098 GrTextureDomain::kClamp_Mode,
1099 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001100 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001101 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001102 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1103 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
egdaniel79da63f2015-10-09 10:55:16 -07001104 fp.reset(GrBicubicEffect::Create(texture, texMatrix, tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001105 } else {
egdaniel79da63f2015-10-09 10:55:16 -07001106 fp.reset(GrSimpleTextureEffect::Create(texture, texMatrix, params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001107 }
1108
joshualitt33a5fce2015-11-18 13:28:51 -08001109 GrPaint grPaint;
1110 if (!SkPaintToGrPaintWithTexture(this->context(), paint, viewMatrix, fp,
1111 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001112 return;
1113 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001114
egdaniel79da63f2015-10-09 10:55:16 -07001115 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1116 // We don't have local coords in this case and have previously set the transform
1117 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001118 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001119 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001120 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001121 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001122}
1123
fmalita2d97bc12014-11-20 10:44:58 -08001124bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001125 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001126 const SkImageFilter* filter,
1127 const SkImageFilter::Context& ctx,
1128 SkBitmap* result, SkIPoint* offset) {
joshualittce894002016-01-11 13:29:31 -08001129 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001130 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001131
reed88d064d2015-10-12 11:30:02 -07001132 SkImageFilter::DeviceProxy proxy(this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001133
1134 if (filter->canFilterImageGPU()) {
robertphillips1de87df2016-01-14 06:03:29 -08001135 SkBitmap bm;
1136 GrWrapTextureInBitmap(texture, width, height, false, &bm);
1137 return filter->filterImageGPU(&proxy, bm, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001138 } else {
1139 return false;
1140 }
1141}
1142
1143void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1144 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001145 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001146 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001147 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001148
1149 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1150 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1151 return;
1152 }
1153
1154 int w = bitmap.width();
1155 int h = bitmap.height();
1156
1157 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001158 // draw sprite neither filters nor tiles.
1159 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001160 if (!texture) {
1161 return;
1162 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001163
bsalomonf1b7a1d2015-09-28 06:26:28 -07001164 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
1165
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001166 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167 // This bitmap will own the filtered result as a texture.
1168 SkBitmap filteredBitmap;
1169
bsalomon49f085d2014-09-05 13:34:00 -07001170 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001171 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001172 SkMatrix matrix(*draw.fMatrix);
1173 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblancodb64af32015-12-09 10:11:43 -08001174#ifdef SK_SUPPORT_SRC_BOUNDS_BLOAT_FOR_IMAGEFILTERS
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001175 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancodb64af32015-12-09 10:11:43 -08001176#else
1177 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-left, -top);
1178#endif
senorblancobe129b22014-08-08 07:14:35 -07001179 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001180 // This cache is transient, and is freed (along with all its contained
1181 // textures) when it goes out of scope.
reed4e23cda2016-01-11 10:56:59 -08001182 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001183 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001184 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001185 texture = (GrTexture*) filteredBitmap.getTexture();
1186 w = filteredBitmap.width();
1187 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001188 left += offset.x();
1189 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001190 } else {
1191 return;
1192 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001193 SkASSERT(!GrPixelConfigIsAlphaOnly(texture->config()));
1194 alphaOnly = false;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195 }
1196
1197 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001198 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001199 GrSimpleTextureEffect::Create(texture, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001200 if (alphaOnly) {
1201 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1202 } else {
1203 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1204 }
1205 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001206 return;
1207 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001208
bsalomona2e69fc2015-11-05 10:41:43 -08001209 fDrawContext->fillRectToRect(fClip,
1210 grPaint,
1211 SkMatrix::I(),
1212 SkRect::MakeXYWH(SkIntToScalar(left),
1213 SkIntToScalar(top),
1214 SkIntToScalar(w),
1215 SkIntToScalar(h)),
1216 SkRect::MakeXYWH(0,
1217 0,
1218 SK_Scalar1 * w / texture->width(),
1219 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001220}
1221
bsalomonb1b01992015-11-18 10:56:08 -08001222void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1223 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001224 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001225 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -08001226 if (bitmap.getTexture()) {
1227 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001228 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001229 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
1230 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001231 return;
1232 }
bsalomonb1b01992015-11-18 10:56:08 -08001233 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1234 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1235 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1236 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1237 const SkRect* dst = &origDst;
1238 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001239 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001240 if (!src) {
1241 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001242 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001243
bsalomonb1b01992015-11-18 10:56:08 -08001244 SkMatrix srcToDstMatrix;
1245 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1246 return;
1247 }
1248 SkRect tmpSrc, tmpDst;
1249 if (src != &bmpBounds) {
1250 if (!bmpBounds.contains(*src)) {
1251 tmpSrc = *src;
1252 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001253 return; // nothing to draw
1254 }
bsalomonb1b01992015-11-18 10:56:08 -08001255 src = &tmpSrc;
1256 srcToDstMatrix.mapRect(&tmpDst, *src);
1257 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001258 }
1259 }
1260
bsalomonb1b01992015-11-18 10:56:08 -08001261 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001262
bsalomonb1b01992015-11-18 10:56:08 -08001263 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1264 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
1265 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
1266 paint.isAntiAlias() &&
1267 bitmap.width() <= maxTileSize &&
1268 bitmap.height() <= maxTileSize;
1269
1270 bool skipTileCheck = drawAA || paint.getMaskFilter();
1271
1272 if (!skipTileCheck) {
1273 int tileSize;
1274 SkIRect clippedSrcRect;
1275
1276 GrTextureParams params;
1277 bool doBicubic;
1278 GrTextureParams::FilterMode textureFilterMode =
1279 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1280 &doBicubic);
1281
1282 int tileFilterPad;
1283
1284 if (doBicubic) {
1285 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1286 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1287 tileFilterPad = 0;
1288 } else {
1289 tileFilterPad = 1;
1290 }
1291 params.setFilterMode(textureFilterMode);
1292
1293 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1294 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1295 // we have a mask filter.
1296 SkMatrix viewMatrix = *draw.fMatrix;
1297 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1298 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1299 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1300 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1301 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1302 constraint, tileSize, doBicubic);
1303 return;
1304 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001305 }
bsalomonb1b01992015-11-18 10:56:08 -08001306 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001307 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001308}
1309
1310void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1311 int x, int y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001312 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001313 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001314 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001315 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001316
1317 // TODO: If the source device covers the whole of this device, we could
1318 // omit fNeedsClear -related flushing.
1319 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001320
1321 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001322 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001323
1324 GrRenderTarget* devRT = dev->accessRenderTarget();
1325 GrTexture* devTex;
halcanary96fcdcc2015-08-27 07:41:13 -07001326 if (nullptr == (devTex = devRT->asTexture())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001327 return;
1328 }
1329
robertphillips7b9e8a42014-12-11 08:20:31 -08001330 const SkImageInfo ii = dev->imageInfo();
1331 int w = ii.width();
1332 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001333
1334 SkImageFilter* filter = paint.getImageFilter();
1335 // This bitmap will own the filtered result as a texture.
1336 SkBitmap filteredBitmap;
1337
bsalomon49f085d2014-09-05 13:34:00 -07001338 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001339 SkIPoint offset = SkIPoint::Make(0, 0);
1340 SkMatrix matrix(*draw.fMatrix);
1341 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblancodb64af32015-12-09 10:11:43 -08001342#ifdef SK_SUPPORT_SRC_BOUNDS_BLOAT_FOR_IMAGEFILTERS
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001343 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblancodb64af32015-12-09 10:11:43 -08001344#else
1345 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-x, -y);
1346#endif
senorblanco55b6d8b2014-07-30 11:26:46 -07001347 // This cache is transient, and is freed (along with all its contained
1348 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001349 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
reed4e23cda2016-01-11 10:56:59 -08001350 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001351 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1352 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001353 devTex = filteredBitmap.getTexture();
1354 w = filteredBitmap.width();
1355 h = filteredBitmap.height();
1356 x += offset.fX;
1357 y += offset.fY;
1358 } else {
1359 return;
1360 }
1361 }
1362
1363 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001364 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001365 GrSimpleTextureEffect::Create(devTex, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001366 if (GrPixelConfigIsAlphaOnly(devTex->config())) {
1367 // Can this happen?
1368 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1369 } else {
1370 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1371 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001372
bsalomonf1b7a1d2015-09-28 06:26:28 -07001373 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001374 return;
1375 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001376
1377 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1378 SkIntToScalar(y),
1379 SkIntToScalar(w),
1380 SkIntToScalar(h));
1381
1382 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1383 // scratch texture).
1384 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1385 SK_Scalar1 * h / devTex->height());
1386
bsalomona2e69fc2015-11-05 10:41:43 -08001387 fDrawContext->fillRectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001388}
1389
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001390bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
joshualittce894002016-01-11 13:29:31 -08001391 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001392 return filter->canFilterImageGPU();
1393}
1394
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001395bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001396 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001397 SkBitmap* result, SkIPoint* offset) {
joshualittce894002016-01-11 13:29:31 -08001398 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001399 // want explicitly our impl, so guard against a subclass of us overriding it
1400 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1401 return false;
1402 }
1403
1404 SkAutoLockPixels alp(src, !src.getTexture());
1405 if (!src.getTexture() && !src.readyToDraw()) {
1406 return false;
1407 }
1408
1409 GrTexture* texture;
1410 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1411 // must be pushed upstack.
bsalomonafa95e22015-10-12 10:39:46 -07001412 AutoBitmapTexture abt(fContext, src, GrTextureParams::ClampNoFilter(), &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001413 if (!texture) {
1414 return false;
1415 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001416
senorblancod0d37ca2015-04-02 04:54:56 -07001417 return this->filterTexture(fContext, texture, src.width(), src.height(),
1418 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001419}
1420
reeda85d4d02015-05-06 12:56:48 -07001421void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1422 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001423 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001424 SkMatrix viewMatrix = *draw.fMatrix;
1425 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001426 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001427 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001428 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001429 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1430 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001431 return;
reed85d91782015-09-10 14:33:38 -07001432 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001433 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001434 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1435 paint.getFilterQuality(), *draw.fMatrix)) {
1436 // only support tiling as bitmap at the moment, so force raster-version
1437 if (!as_IB(image)->getROPixels(&bm)) {
1438 return;
1439 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001440 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1441 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1442 CHECK_SHOULD_DRAW(draw);
1443 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001444 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1445 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001446 } else if (as_IB(image)->getROPixels(&bm)) {
1447 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001448 }
reeda85d4d02015-05-06 12:56:48 -07001449 }
1450}
1451
1452void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001453 const SkRect& dst, const SkPaint& paint,
1454 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001455 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001456 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001457 CHECK_SHOULD_DRAW(draw);
1458 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001459 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001460 return;
1461 }
1462 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001463 SkMatrix totalMatrix = *draw.fMatrix;
1464 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1465 dst.height() / (src ? src->height() : image->height()));
1466 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001467 // only support tiling as bitmap at the moment, so force raster-version
1468 if (!as_IB(image)->getROPixels(&bm)) {
1469 return;
1470 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001471 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1472 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1473 CHECK_SHOULD_DRAW(draw);
1474 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001475 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001476 } else if (as_IB(image)->getROPixels(&bm)) {
1477 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001478 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001479}
1480
bsalomonf1ecd212015-12-09 17:06:02 -08001481void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001482 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001483 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001484
1485 CHECK_FOR_ANNOTATION(paint);
1486 CHECK_SHOULD_DRAW(draw);
1487
joshualittedb36442015-11-19 14:29:30 -08001488 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
1489 fRenderTarget->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001490 bool doBicubic;
1491 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001492 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1493 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001494 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001495 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001496
1497 SkRect srcR, dstR;
1498 while (iter.next(&srcR, &dstR)) {
bsalomonf1ecd212015-12-09 17:06:02 -08001499 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
1500 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001501 }
1502 return;
1503 }
1504
bsalomon2bbd0c62015-12-09 12:50:56 -08001505 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
1506 SkAutoTUnref<const GrFragmentProcessor> fp(
1507 producer->createFragmentProcessor(SkMatrix::I(),
1508 SkRect::MakeIWH(producer->width(), producer->height()),
1509 GrTextureProducer::kNo_FilterConstraint, true,
1510 &kMode));
joshualitt33a5fce2015-11-18 13:28:51 -08001511 GrPaint grPaint;
1512 if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, fp,
bsalomonf1ecd212015-12-09 17:06:02 -08001513 producer->isAlphaOnly(), &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001514 return;
1515 }
1516
bsalomon2bbd0c62015-12-09 12:50:56 -08001517 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1518 producer->height(), center, dst);
1519}
1520
1521void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1522 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001523 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001524 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001525 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001526 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001527 } else {
1528 SkBitmap bm;
1529 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1530 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001531 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001532 } else if (as_IB(image)->getROPixels(&bm)) {
1533 this->drawBitmapNine(draw, bm, center, dst, paint);
1534 }
1535 }
1536}
1537
1538void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1539 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001540 ASSERT_SINGLE_OWNER
bsalomon2bbd0c62015-12-09 12:50:56 -08001541 if (bitmap.getTexture()) {
1542 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001543 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001544 } else {
1545 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001546 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001547 }
joshualitt33a5fce2015-11-18 13:28:51 -08001548}
1549
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001550///////////////////////////////////////////////////////////////////////////////
1551
1552// must be in SkCanvas::VertexMode order
1553static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1554 kTriangles_GrPrimitiveType,
1555 kTriangleStrip_GrPrimitiveType,
1556 kTriangleFan_GrPrimitiveType,
1557};
1558
1559void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1560 int vertexCount, const SkPoint vertices[],
1561 const SkPoint texs[], const SkColor colors[],
1562 SkXfermode* xmode,
1563 const uint16_t indices[], int indexCount,
1564 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001565 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001566 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001567 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001568
halcanary96fcdcc2015-08-27 07:41:13 -07001569 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1570 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001571
halcanary96fcdcc2015-08-27 07:41:13 -07001572 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001573
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001574 SkPaint copy(paint);
1575 copy.setStyle(SkPaint::kStroke_Style);
1576 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001577
bsalomonf1b7a1d2015-09-28 06:26:28 -07001578 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001579 // we ignore the shader if texs is null.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001580 if (!SkPaintToGrPaintNoShader(this->context(), copy, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001581 return;
1582 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001583
dandov32a311b2014-07-15 19:46:26 -07001584 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001585 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001586 switch (vmode) {
1587 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001588 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001589 break;
1590 case SkCanvas::kTriangleStrip_VertexMode:
1591 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001592 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001593 break;
1594 }
mtklein533eb782014-08-27 10:39:42 -07001595
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001596 VertState state(vertexCount, indices, indexCount);
1597 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001598
dandov32a311b2014-07-15 19:46:26 -07001599 //number of indices for lines per triangle with kLines
1600 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001601
bsalomonf1b7a1d2015-09-28 06:26:28 -07001602 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001603 int i = 0;
1604 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001605 lineIndices[i] = state.f0;
1606 lineIndices[i + 1] = state.f1;
1607 lineIndices[i + 2] = state.f1;
1608 lineIndices[i + 3] = state.f2;
1609 lineIndices[i + 4] = state.f2;
1610 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001611 i += 6;
1612 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001613 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001614 grPaint,
1615 *draw.fMatrix,
1616 kLines_GrPrimitiveType,
1617 vertexCount,
1618 vertices,
1619 texs,
1620 colors,
1621 lineIndices.get(),
1622 indexCount);
1623 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001624 }
1625
bsalomonf1b7a1d2015-09-28 06:26:28 -07001626 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001627
1628 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001629 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001630 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1631 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001632 convertedColors.reset(vertexCount);
1633 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001634 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001635 }
1636 colors = convertedColors.get();
1637 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001638 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001639 if (texs && paint.getShader()) {
1640 if (colors) {
1641 // When there are texs and colors the shader and colors are combined using xmode. A null
1642 // xmode is defined to mean modulate.
1643 SkXfermode::Mode colorMode;
1644 if (xmode) {
1645 if (!xmode->asMode(&colorMode)) {
1646 return;
1647 }
1648 } else {
1649 colorMode = SkXfermode::kModulate_Mode;
1650 }
1651 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
1652 false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001653 return;
1654 }
1655 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001656 // We have a shader, but no colors to blend it against.
1657 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
1658 return;
1659 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001660 }
bsalomonaa48d362015-10-01 08:34:17 -07001661 } else {
1662 if (colors) {
1663 // We have colors, but either have no shader or no texture coords (which implies that
1664 // we should ignore the shader).
1665 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint, &grPaint)) {
1666 return;
1667 }
1668 } else {
1669 // No colors and no shaders. Just draw with the paint color.
1670 if (!SkPaintToGrPaintNoShader(this->context(), paint, &grPaint)) {
1671 return;
1672 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001673 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001674 }
1675
robertphillips2e1e51f2015-10-15 08:01:48 -07001676 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001677 grPaint,
1678 *draw.fMatrix,
1679 primType,
1680 vertexCount,
1681 vertices,
1682 texs,
1683 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001684 indices,
robertphillipsea461502015-05-26 11:38:03 -07001685 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001686}
1687
1688///////////////////////////////////////////////////////////////////////////////
1689
jvanverth31ff7622015-08-07 10:09:28 -07001690void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001691 const SkRect texRect[], const SkColor colors[], int count,
1692 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001693 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001694 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001695 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001696 return;
1697 }
1698
jvanverth31ff7622015-08-07 10:09:28 -07001699 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001700 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001701
reedca109532015-06-25 16:25:25 -07001702 SkPaint p(paint);
1703 p.setShader(atlas->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
1704
jvanverth31ff7622015-08-07 10:09:28 -07001705 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001706 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001707 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
1708 &grPaint)) {
1709 return;
1710 }
1711 } else {
1712 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix, &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001713 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001714 }
1715 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001716
1717 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001718 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001719}
1720
1721///////////////////////////////////////////////////////////////////////////////
1722
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001723void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001724 size_t byteLength, SkScalar x, SkScalar y,
1725 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001726 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001727 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001728 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001729
jvanverth8c27a182014-10-14 08:45:50 -07001730 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001731 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001732 return;
1733 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001734
jvanverth8c27a182014-10-14 08:45:50 -07001735 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001736
robertphillips2e1e51f2015-10-15 08:01:48 -07001737 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001738 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001739}
1740
fmalita05c4a432014-09-29 06:29:53 -07001741void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1742 const SkScalar pos[], int scalarsPerPos,
1743 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001744 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001745 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001746 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001747
jvanverth8c27a182014-10-14 08:45:50 -07001748 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001749 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001750 return;
1751 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001752
jvanverth8c27a182014-10-14 08:45:50 -07001753 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001754
robertphillips2e1e51f2015-10-15 08:01:48 -07001755 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001756 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1757 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001758}
1759
joshualitt9c328182015-03-23 08:13:04 -07001760void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1761 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001762 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001763 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001764 CHECK_SHOULD_DRAW(draw);
1765
1766 SkDEBUGCODE(this->validate();)
1767
robertphillips2e1e51f2015-10-15 08:01:48 -07001768 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001769 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001770}
1771
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001772///////////////////////////////////////////////////////////////////////////////
1773
reedb2db8982014-11-13 12:41:02 -08001774bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
robertphillips9c240a12015-05-28 07:45:59 -07001775 return GrTextContext::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001776}
1777
1778void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001779 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001780 DO_DEFERRED_CLEAR();
joshualittbc907352016-01-13 06:45:40 -08001781
1782 // Clear batch debugging output
1783 // TODO not exactly sure where this should live
1784 if (GR_BATCH_DEBUGGING_OUTPUT) {
1785 SkDebugf("%s\n", fContext->getAuditTrail()->toJson().c_str());
1786 // TODO This currently crashes because not all ops are accounted for
1787 GR_AUDIT_TRAIL_RESET(fContext->getAuditTrail());
1788 }
bsalomonc49e8682015-06-30 11:37:35 -07001789 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001790}
1791
1792///////////////////////////////////////////////////////////////////////////////
1793
reed76033be2015-03-14 10:54:31 -07001794SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001795 ASSERT_SINGLE_OWNER
bsalomonf2703d82014-10-28 14:33:06 -07001796 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001797 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001798 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001799 desc.fWidth = cinfo.fInfo.width();
1800 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001801 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001802
1803 SkAutoTUnref<GrTexture> texture;
1804 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001805 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001806
hcm4396fa52014-10-27 21:43:30 -07001807 // layers are never draw in repeat modes, so we can request an approx
1808 // match and ignore any padding.
bsalomoneae62002015-07-31 13:59:30 -07001809 if (kNever_TileUsage == cinfo.fTileUsage) {
1810 texture.reset(fContext->textureProvider()->createApproxTexture(desc));
1811 } else {
1812 texture.reset(fContext->textureProvider()->createTexture(desc, true));
1813 }
bsalomonafe30052015-01-16 07:32:33 -08001814
1815 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001816 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001817 return SkGpuDevice::Create(
bsalomon74f681d2015-06-23 14:38:48 -07001818 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, init);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001819 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001820 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001821 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001822 cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary96fcdcc2015-08-27 07:41:13 -07001823 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001824 }
1825}
1826
reed4a8126e2014-09-22 07:29:03 -07001827SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001828 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001829 // TODO: Change the signature of newSurface to take a budgeted parameter.
1830 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
vbuzinovdded6962015-06-12 08:59:45 -07001831 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
bsalomonafe30052015-01-16 07:32:33 -08001832 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001833}
1834
robertphillips30d2cc62014-09-24 08:52:18 -07001835bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001836 const SkMatrix* matrix, const SkPaint* paint) {
joshualittce894002016-01-11 13:29:31 -08001837 ASSERT_SINGLE_OWNER
robertphillips63242d72014-12-04 08:31:02 -08001838#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001839 // todo: should handle this natively
1840 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001841 return false;
1842 }
1843
halcanary96fcdcc2015-08-27 07:41:13 -07001844 const SkBigPicture::AccelData* data = nullptr;
mtklein9db912c2015-05-19 11:11:26 -07001845 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1846 data = bp->accelData();
1847 }
robertphillips81f71b62014-11-11 04:54:49 -08001848 if (!data) {
1849 return false;
1850 }
1851
robertphillipse5524cd2015-02-20 12:30:26 -08001852 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1853 if (0 == gpuData->numBlocks()) {
1854 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001855 }
1856
robertphillipsfd61ed02014-10-28 07:21:44 -07001857 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001858
robertphillipse5524cd2015-02-20 12:30:26 -08001859 SkIRect iBounds;
1860 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1861 return false;
1862 }
1863
1864 SkRect clipBounds = SkRect::Make(iBounds);
1865
1866 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1867
robertphillips60029a52015-11-09 13:51:06 -08001868 GrLayerHoister::Begin(fContext);
1869
robertphillipsfd61ed02014-10-28 07:21:44 -07001870 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001871 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001872 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001873 &atlasedNeedRendering, &atlasedRecycled,
vbuzinovdded6962015-06-12 08:59:45 -07001874 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001875
1876 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1877
1878 SkTDArray<GrHoistedLayer> needRendering, recycled;
1879
robertphillipse5524cd2015-02-20 12:30:26 -08001880 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1881
robertphillipsfd61ed02014-10-28 07:21:44 -07001882 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001883 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001884 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001885 &needRendering, &recycled,
vbuzinovdded6962015-06-12 08:59:45 -07001886 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001887
1888 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001889
robertphillips64bf7672014-08-21 13:07:35 -07001890 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001891 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
halcanary96fcdcc2015-08-27 07:41:13 -07001892 initialMatrix, nullptr);
robertphillips64bf7672014-08-21 13:07:35 -07001893
robertphillipsfd61ed02014-10-28 07:21:44 -07001894 GrLayerHoister::UnlockLayers(fContext, needRendering);
1895 GrLayerHoister::UnlockLayers(fContext, recycled);
1896 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1897 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips60029a52015-11-09 13:51:06 -08001898 GrLayerHoister::End(fContext);
robertphillips64bf7672014-08-21 13:07:35 -07001899
1900 return true;
robertphillips63242d72014-12-04 08:31:02 -08001901#else
1902 return false;
1903#endif
robertphillips64bf7672014-08-21 13:07:35 -07001904}
1905
reed13ccbf82015-10-20 09:56:52 -07001906SkImageFilter::Cache* SkGpuDevice::NewImageFilterCache() {
1907 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
1908}
1909
senorblancobe129b22014-08-08 07:14:35 -07001910SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001911 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001912 // We always return a transient cache, so it is freed after each
1913 // filter traversal.
reed13ccbf82015-10-20 09:56:52 -07001914 return SkGpuDevice::NewImageFilterCache();
senorblanco55b6d8b2014-07-30 11:26:46 -07001915}
reedf037e0b2014-10-30 11:34:15 -07001916
1917#endif