blob: b3761e2d7105e9db8797c74de096305435cdc5d8 [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"
robertphillipsd7706102016-02-25 09:28:08 -080047#include "effects/GrRRectEffect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080048#include "effects/GrSimpleTextureEffect.h"
49#include "effects/GrTextureDomain.h"
joshualitt8e84a1e2016-02-16 11:09:25 -080050#include "text/GrTextUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000051
reedf037e0b2014-10-30 11:34:15 -070052#if SK_SUPPORT_GPU
53
joshualittce894002016-01-11 13:29:31 -080054#define ASSERT_SINGLE_OWNER \
55 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->debugSingleOwner());)
56
senorblanco55b6d8b2014-07-30 11:26:46 -070057enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
58
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000059#if 0
60 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080061 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000062 do { \
63 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080064 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000065 } while (0)
66#else
joshualitt5531d512014-12-17 15:50:11 -080067 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000068#endif
69
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000070///////////////////////////////////////////////////////////////////////////////
71
72#define CHECK_FOR_ANNOTATION(paint) \
73 do { if (paint.getAnnotation()) { return; } } while (0)
74
75///////////////////////////////////////////////////////////////////////////////
76
bsalomonbcf0a522014-10-08 08:40:09 -070077// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
78// just accesses the backing GrTexture. Otherwise, it creates a cached texture
79// representation and releases it in the destructor.
80class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040081public:
bsalomonbcf0a522014-10-08 08:40:09 -070082 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070083
bsalomonbcf0a522014-10-08 08:40:09 -070084 AutoBitmapTexture(GrContext* context,
85 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -070086 const GrTextureParams& params,
bsalomonbcf0a522014-10-08 08:40:09 -070087 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040088 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070089 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040090 }
91
bsalomonbcf0a522014-10-08 08:40:09 -070092 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040093 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -070094 const GrTextureParams& params) {
bsalomonbcf0a522014-10-08 08:40:09 -070095 // Either get the texture directly from the bitmap, or else use the cache and
96 // remember to unref it.
97 if (GrTexture* bmpTexture = bitmap.getTexture()) {
halcanary96fcdcc2015-08-27 07:41:13 -070098 fTexture.reset(nullptr);
bsalomonbcf0a522014-10-08 08:40:09 -070099 return bmpTexture;
100 } else {
101 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
102 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400103 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400104 }
105
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000106private:
bsalomonbcf0a522014-10-08 08:40:09 -0700107 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000108};
109
110///////////////////////////////////////////////////////////////////////////////
111
bsalomon74f681d2015-06-23 14:38:48 -0700112/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
113 should fail. */
114bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
115 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
116 *flags = 0;
117 if (info) {
118 switch (info->alphaType()) {
119 case kPremul_SkAlphaType:
120 break;
121 case kOpaque_SkAlphaType:
122 *flags |= SkGpuDevice::kIsOpaque_Flag;
123 break;
124 default: // If it is unpremul or unknown don't try to render
125 return false;
126 }
127 }
128 if (kClear_InitContents == init) {
129 *flags |= kNeedClear_Flag;
130 }
131 return true;
132}
133
134SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props,
135 InitContents init) {
136 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, init);
senorblancod0d37ca2015-04-02 04:54:56 -0700137}
138
139SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
bsalomon74f681d2015-06-23 14:38:48 -0700140 const SkSurfaceProps* props, InitContents init) {
bsalomonafe30052015-01-16 07:32:33 -0800141 if (!rt || rt->wasDestroyed()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700142 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000143 }
bsalomon74f681d2015-06-23 14:38:48 -0700144 unsigned flags;
halcanary96fcdcc2015-08-27 07:41:13 -0700145 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
146 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700147 }
halcanary385fe4d2015-08-26 13:07:48 -0700148 return new SkGpuDevice(rt, width, height, props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000149}
150
bsalomon5ec26ae2016-02-25 08:33:02 -0800151SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkBudgeted budgeted,
bsalomon74f681d2015-06-23 14:38:48 -0700152 const SkImageInfo& info, int sampleCount,
erikchen9a1ed5d2016-02-10 16:32:34 -0800153 const SkSurfaceProps* props, InitContents init,
154 GrTextureStorageAllocator customAllocator) {
bsalomon74f681d2015-06-23 14:38:48 -0700155 unsigned flags;
156 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700157 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700158 }
159
erikchen9a1ed5d2016-02-10 16:32:34 -0800160 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(
161 context, budgeted, info, sampleCount, customAllocator));
halcanary96fcdcc2015-08-27 07:41:13 -0700162 if (nullptr == rt) {
163 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700164 }
165
halcanary385fe4d2015-08-26 13:07:48 -0700166 return new SkGpuDevice(rt, info.width(), info.height(), props, flags);
bsalomon74f681d2015-06-23 14:38:48 -0700167}
168
senorblancod0d37ca2015-04-02 04:54:56 -0700169SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
170 const SkSurfaceProps* props, unsigned flags)
robertphillipsfcf78292015-06-19 11:49:52 -0700171 : INHERITED(SkSurfacePropsCopyOrDefault(props))
joshualitt06dd0a82016-01-08 06:32:31 -0800172 , fContext(SkRef(rt->getContext()))
173 , fRenderTarget(SkRef(rt)) {
bsalomon74f681d2015-06-23 14:38:48 -0700174 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000175
bsalomon74f681d2015-06-23 14:38:48 -0700176 SkAlphaType at = fOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
177 SkImageInfo info = rt->surfacePriv().info(at).makeWH(width, height);
halcanary385fe4d2015-08-26 13:07:48 -0700178 SkPixelRef* pr = new SkGrPixelRef(info, rt);
bsalomonafbf2d62014-09-30 12:18:44 -0700179 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700180 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700181
robertphillips77a2e522015-10-17 07:43:27 -0700182 fDrawContext.reset(this->context()->drawContext(rt, &this->surfaceProps()));
bsalomone63ffef2016-02-05 07:17:34 -0800183 if (flags & kNeedClear_Flag) {
184 this->clearAll();
185 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000186}
187
erikchen9a1ed5d2016-02-10 16:32:34 -0800188GrRenderTarget* SkGpuDevice::CreateRenderTarget(
bsalomon5ec26ae2016-02-25 08:33:02 -0800189 GrContext* context, SkBudgeted budgeted, const SkImageInfo& origInfo,
erikchen9a1ed5d2016-02-10 16:32:34 -0800190 int sampleCount, GrTextureStorageAllocator textureStorageAllocator) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000191 if (kUnknown_SkColorType == origInfo.colorType() ||
192 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700193 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000194 }
195
bsalomonafe30052015-01-16 07:32:33 -0800196 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700197 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800198 }
199
reede5ea5002014-09-03 11:54:58 -0700200 SkColorType ct = origInfo.colorType();
201 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700202 if (kRGB_565_SkColorType == ct) {
203 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800204 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
205 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700206 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800207 }
208 if (kOpaque_SkAlphaType != at) {
209 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000210 }
reede5ea5002014-09-03 11:54:58 -0700211 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000212
bsalomonf2703d82014-10-28 14:33:06 -0700213 GrSurfaceDesc desc;
214 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000215 desc.fWidth = info.width();
216 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000217 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000218 desc.fSampleCnt = sampleCount;
erikchen9a1ed5d2016-02-10 16:32:34 -0800219 desc.fTextureStorageAllocator = textureStorageAllocator;
bsalomon5ec26ae2016-02-25 08:33:02 -0800220 GrTexture* texture = context->textureProvider()->createTexture(desc, budgeted, nullptr, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700221 if (nullptr == texture) {
222 return nullptr;
kkinnunenabcfab42015-02-22 22:53:44 -0800223 }
halcanary96fcdcc2015-08-27 07:41:13 -0700224 SkASSERT(nullptr != texture->asRenderTarget());
kkinnunenabcfab42015-02-22 22:53:44 -0800225 return texture->asRenderTarget();
226}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000227
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000228///////////////////////////////////////////////////////////////////////////////
229
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000230bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
231 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800232 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000233
234 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000235 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000236 if (kUnknown_GrPixelConfig == config) {
237 return false;
238 }
239
240 uint32_t flags = 0;
241 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
242 flags = GrContext::kUnpremul_PixelOpsFlag;
243 }
bsalomon74f681d2015-06-23 14:38:48 -0700244 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
245 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000246}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000247
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000248bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
249 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800250 ASSERT_SINGLE_OWNER
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000251 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000252 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000253 if (kUnknown_GrPixelConfig == config) {
254 return false;
255 }
256 uint32_t flags = 0;
257 if (kUnpremul_SkAlphaType == info.alphaType()) {
258 flags = GrContext::kUnpremul_PixelOpsFlag;
259 }
260 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
261
262 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700263 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000264
265 return true;
266}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000267
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000268const SkBitmap& SkGpuDevice::onAccessBitmap() {
joshualittce894002016-01-11 13:29:31 -0800269 ASSERT_SINGLE_OWNER
reed89443ab2014-06-27 11:34:19 -0700270 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000271}
272
reed41e010c2015-06-09 12:16:53 -0700273bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800274 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700275 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
276 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
277 // ... ugh.
278 fLegacyBitmap.notifyPixelsChanged();
279 return false;
280}
281
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000282void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
joshualittce894002016-01-11 13:29:31 -0800283 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000284 INHERITED::onAttachToCanvas(canvas);
285
286 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800287 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000288}
289
290void SkGpuDevice::onDetachFromCanvas() {
joshualittce894002016-01-11 13:29:31 -0800291 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000292 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800293 fClip.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700294 fClipStack.reset(nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000295}
296
297// call this every draw call, to ensure that the context reflects our state,
298// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800299void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800300 ASSERT_SINGLE_OWNER
joshualitt44701df2015-02-23 14:44:57 -0800301 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000302
joshualitt44701df2015-02-23 14:44:57 -0800303 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000304
joshualitt570d2f82015-02-25 13:19:48 -0800305 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000306}
307
308GrRenderTarget* SkGpuDevice::accessRenderTarget() {
joshualittce894002016-01-11 13:29:31 -0800309 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000310 return fRenderTarget;
311}
312
reed8eddfb52014-12-04 07:50:14 -0800313void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800314 ASSERT_SINGLE_OWNER
reed8eddfb52014-12-04 07:50:14 -0800315 GrColor color = 0;
joshualitt5651ee62016-01-11 10:39:11 -0800316 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext);
reed8eddfb52014-12-04 07:50:14 -0800317 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700318 fDrawContext->clear(&rect, color, true);
reed8eddfb52014-12-04 07:50:14 -0800319}
320
kkinnunenabcfab42015-02-22 22:53:44 -0800321void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800322 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800323
bsalomon5ec26ae2016-02-25 08:33:02 -0800324 SkBudgeted budgeted = fRenderTarget->resourcePriv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800325
326 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
erikchen9a1ed5d2016-02-10 16:32:34 -0800327 this->context(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt,
328 fRenderTarget->desc().fTextureStorageAllocator));
kkinnunenabcfab42015-02-22 22:53:44 -0800329
halcanary96fcdcc2015-08-27 07:41:13 -0700330 if (nullptr == newRT) {
kkinnunenabcfab42015-02-22 22:53:44 -0800331 return;
332 }
333
334 if (shouldRetainContent) {
335 if (fRenderTarget->wasDestroyed()) {
336 return;
337 }
338 this->context()->copySurface(newRT, fRenderTarget);
339 }
340
341 SkASSERT(fRenderTarget != newRT);
342
joshualitt06dd0a82016-01-08 06:32:31 -0800343 fRenderTarget.reset(newRT.detach());
kkinnunenabcfab42015-02-22 22:53:44 -0800344
bsalomon74f681d2015-06-23 14:38:48 -0700345#ifdef SK_DEBUG
346 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
347 kPremul_SkAlphaType);
348 SkASSERT(info == fLegacyBitmap.info());
349#endif
halcanary385fe4d2015-08-26 13:07:48 -0700350 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget);
kkinnunenabcfab42015-02-22 22:53:44 -0800351 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700352
robertphillips77a2e522015-10-17 07:43:27 -0700353 fDrawContext.reset(this->context()->drawContext(fRenderTarget, &this->surfaceProps()));
kkinnunenabcfab42015-02-22 22:53:44 -0800354}
355
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356///////////////////////////////////////////////////////////////////////////////
357
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000358void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800359 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800360 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800361 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000362
363 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700364 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700365 return;
366 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000367
robertphillips2e1e51f2015-10-15 08:01:48 -0700368 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000369}
370
371// must be in SkCanvas::PointMode order
372static const GrPrimitiveType gPointMode2PrimtiveType[] = {
373 kPoints_GrPrimitiveType,
374 kLines_GrPrimitiveType,
375 kLineStrip_GrPrimitiveType
376};
377
ethannicholas330bb952015-07-17 06:44:02 -0700378// suppress antialiasing on axis-aligned integer-coordinate lines
379static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
380 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
381 return false;
382 }
383 if (count == 2) {
384 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
385 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800386 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700387 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800388 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700389 // the two end pixels as being the lesser of two evils.
390 if (pts[0].fX == pts[1].fX) {
391 return ((int) pts[0].fX) != pts[0].fX;
392 }
393 if (pts[0].fY == pts[1].fY) {
394 return ((int) pts[0].fY) != pts[0].fY;
395 }
396 }
397 return true;
398}
399
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000400void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
401 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800402 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800403 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000404 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800405 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000406
407 SkScalar width = paint.getStrokeWidth();
408 if (width < 0) {
409 return;
410 }
411
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000412 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700413 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
414 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700415 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700416 return;
417 }
egdaniele61c4112014-06-12 10:24:21 -0700418 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700419 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700420 path.moveTo(pts[0]);
421 path.lineTo(pts[1]);
robertphillips2e1e51f2015-10-15 08:01:48 -0700422 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700423 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000424 }
425
ethannicholas330bb952015-07-17 06:44:02 -0700426 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000427 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800428 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700429 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000430 draw.drawPoints(mode, count, pts, paint, true);
431 return;
432 }
433
434 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700435 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700436 return;
437 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000438
robertphillips2e1e51f2015-10-15 08:01:48 -0700439 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700440 grPaint,
441 *draw.fMatrix,
442 gPointMode2PrimtiveType[mode],
443 SkToS32(count),
444 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700445 nullptr,
446 nullptr,
447 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700448 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000449}
450
451///////////////////////////////////////////////////////////////////////////////
452
robertphillipsff55b492015-11-24 07:56:59 -0800453void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800454 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800455 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000456 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800457 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000458
459 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
460 SkScalar width = paint.getStrokeWidth();
461
462 /*
463 We have special code for hairline strokes, miter-strokes, bevel-stroke
464 and fills. Anything else we just call our path code.
465 */
466 bool usePath = doStroke && width > 0 &&
467 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
468 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
egdanield58a0ba2014-06-11 10:30:05 -0700469
robertphillipsd8aa59d2015-08-05 09:07:12 -0700470 // a few other reasons we might need to call drawPath...
robertphillipsff55b492015-11-24 07:56:59 -0800471 if (paint.getMaskFilter() || paint.getPathEffect() ||
robertphillipsd8aa59d2015-08-05 09:07:12 -0700472 paint.getStyle() == SkPaint::kStrokeAndFill_Style) { // we can't both stroke and fill rects
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000473 usePath = true;
474 }
egdanield58a0ba2014-06-11 10:30:05 -0700475
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000476 if (usePath) {
477 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700478 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000479 path.addRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800480 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800481 fClip, path, paint,
482 *draw.fMatrix, nullptr,
483 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000484 return;
485 }
486
487 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700488 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700489 return;
490 }
Mike Klein744fb732014-06-23 15:13:26 -0400491
robertphillipsff55b492015-11-24 07:56:59 -0800492 GrStrokeInfo strokeInfo(paint);
493
robertphillips2e1e51f2015-10-15 08:01:48 -0700494 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000495}
496
497///////////////////////////////////////////////////////////////////////////////
498
499void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800500 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800501 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800502 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000503 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800504 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000505
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000506 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700507 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700508 return;
509 }
Mike Klein744fb732014-06-23 15:13:26 -0400510
egdanield58a0ba2014-06-11 10:30:05 -0700511 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000512 if (paint.getMaskFilter()) {
513 // try to hit the fast path for drawing filtered round rects
514
515 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800516 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000517 if (devRRect.allCornersCircular()) {
518 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700519 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
joshualitt5531d512014-12-17 15:50:11 -0800520 draw.fClip->getBounds(),
521 *draw.fMatrix,
522 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000523 SkIRect finalIRect;
524 maskRect.roundOut(&finalIRect);
525 if (draw.fClip->quickReject(finalIRect)) {
526 // clipped out
527 return;
528 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700529 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
530 fDrawContext,
joshualitt25d9c152015-02-18 12:29:52 -0800531 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800532 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800533 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700534 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700535 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000536 return;
537 }
538 }
539
540 }
541 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000542 }
543
robertphillips514450c2015-11-24 05:36:02 -0800544 if (paint.getMaskFilter() || paint.getPathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800545 // The only mask filter the native rrect drawing code could've handle was taken
546 // care of above.
547 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000548 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700549 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550 path.addRRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800551 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800552 fClip, path, paint,
553 *draw.fMatrix, nullptr,
554 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000555 return;
556 }
Mike Klein744fb732014-06-23 15:13:26 -0400557
robertphillips514450c2015-11-24 05:36:02 -0800558 SkASSERT(!strokeInfo.isDashed());
559
robertphillips2e1e51f2015-10-15 08:01:48 -0700560 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000561}
562
robertphillipsd7706102016-02-25 09:28:08 -0800563bool SkGpuDevice::drawFilledDRRect(const SkMatrix& viewMatrix, const SkRRect& origOuter,
564 const SkRRect& origInner, const SkPaint& paint) {
565 SkASSERT(!origInner.isEmpty());
566 SkASSERT(!origOuter.isEmpty());
567
568 bool applyAA = paint.isAntiAlias() && !fRenderTarget->isUnifiedMultisampled();
569
570 GrPrimitiveEdgeType innerEdgeType = applyAA ? kInverseFillAA_GrProcessorEdgeType :
571 kInverseFillBW_GrProcessorEdgeType;
572 GrPrimitiveEdgeType outerEdgeType = applyAA ? kFillAA_GrProcessorEdgeType :
573 kFillBW_GrProcessorEdgeType;
574
575 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
576 SkMatrix inverseVM;
577 if (!viewMatrix.isIdentity()) {
578 if (!origInner.transform(viewMatrix, inner.writable())) {
579 return false;
580 }
581 if (!origOuter.transform(viewMatrix, outer.writable())) {
582 return false;
583 }
584 if (!viewMatrix.invert(&inverseVM)) {
585 return false;
586 }
587 } else {
588 inverseVM.reset();
589 }
590
591 GrPaint grPaint;
592
593 if (!SkPaintToGrPaint(this->context(), paint, viewMatrix, &grPaint)) {
594 return false;
595 }
596
597 grPaint.setAntiAlias(false);
598
599 // TODO these need to be a geometry processors
600 SkAutoTUnref<GrFragmentProcessor> innerEffect(GrRRectEffect::Create(innerEdgeType, *inner));
601 if (!innerEffect) {
602 return false;
603 }
604
605 SkAutoTUnref<GrFragmentProcessor> outerEffect(GrRRectEffect::Create(outerEdgeType, *outer));
606 if (!outerEffect) {
607 return false;
608 }
609
610 grPaint.addCoverageFragmentProcessor(innerEffect);
611 grPaint.addCoverageFragmentProcessor(outerEffect);
612
613 SkRect bounds = outer->getBounds();
614 if (applyAA) {
615 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
616 }
617
618 fDrawContext->fillRectWithLocalMatrix(fClip, grPaint, SkMatrix::I(), bounds, inverseVM);
619 return true;
620}
621
622
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000623void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800624 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800625 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800626 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800627 CHECK_FOR_ANNOTATION(paint);
628 CHECK_SHOULD_DRAW(draw);
629
robertphillipsd7706102016-02-25 09:28:08 -0800630 if (outer.isEmpty()) {
631 return;
632 }
633
634 if (inner.isEmpty()) {
635 return this->drawRRect(draw, outer, paint);
636 }
637
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000638 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000639
robertphillips0e7029e2015-11-30 05:45:06 -0800640 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillipsd7706102016-02-25 09:28:08 -0800641 if (this->drawFilledDRRect(*draw.fMatrix, outer, inner, paint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700642 return;
643 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000644 }
645
646 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700647 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000648 path.addRRect(outer);
649 path.addRRect(inner);
650 path.setFillType(SkPath::kEvenOdd_FillType);
651
robertphillips7bceedc2015-12-01 12:51:26 -0800652 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillips0e7029e2015-11-30 05:45:06 -0800653 fClip, path, paint,
654 *draw.fMatrix, nullptr,
655 draw.fClip->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000656}
657
658
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000659/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660
robertphillips514450c2015-11-24 05:36:02 -0800661void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800662 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800663 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000664 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800665 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000666
robertphillips514450c2015-11-24 05:36:02 -0800667 // Presumably the path effect warps this to something other than an oval
668 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000669 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700670 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000671 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700672 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000673 return;
herb11a7f7f2015-11-24 12:41:00 -0800674 }
675
robertphillips514450c2015-11-24 05:36:02 -0800676 if (paint.getMaskFilter()) {
677 // The RRect path can handle special case blurring
678 SkRRect rr = SkRRect::MakeOval(oval);
679 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000680 }
681
682 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700683 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700684 return;
685 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000686
robertphillips514450c2015-11-24 05:36:02 -0800687 GrStrokeInfo strokeInfo(paint);
688 SkASSERT(!strokeInfo.isDashed());
689
robertphillips2e1e51f2015-10-15 08:01:48 -0700690 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000691}
692
693#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000694
695///////////////////////////////////////////////////////////////////////////////
696
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000697void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
698 const SkPaint& paint, const SkMatrix* prePathMatrix,
699 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800700 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800701 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
702 bool isClosed;
703 SkRect rect;
704 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
705 this->drawRect(draw, rect, paint);
706 return;
707 }
708 if (origSrcPath.isOval(&rect)) {
709 this->drawOval(draw, rect, paint);
710 return;
711 }
712 SkRRect rrect;
713 if (origSrcPath.isRRect(&rrect)) {
714 this->drawRRect(draw, rrect, paint);
715 return;
716 }
717 }
718
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000719 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800720 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800721 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000722
robertphillips7bceedc2015-12-01 12:51:26 -0800723 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsccb1b572015-05-27 11:02:55 -0700724 fClip, origSrcPath, paint,
725 *draw.fMatrix, prePathMatrix,
726 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000727}
728
729static const int kBmpSmallTileSize = 1 << 10;
730
731static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
732 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
733 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
734 return tilesX * tilesY;
735}
736
reed85d91782015-09-10 14:33:38 -0700737static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000738 if (maxTileSize <= kBmpSmallTileSize) {
739 return maxTileSize;
740 }
741
742 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
743 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
744
745 maxTileTotalTileSize *= maxTileSize * maxTileSize;
746 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
747
748 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
749 return kBmpSmallTileSize;
750 } else {
751 return maxTileSize;
752 }
753}
754
755// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
756// pixels from the bitmap are necessary.
bsalomon74f681d2015-06-23 14:38:48 -0700757static void determine_clipped_src_rect(const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800758 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800759 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700760 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000761 const SkRect* srcRectPtr,
762 SkIRect* clippedSrcIRect) {
robertphillips7bceedc2015-12-01 12:51:26 -0800763 clip.getConservativeBounds(rt->width(), rt->height(), clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800765 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000766 clippedSrcIRect->setEmpty();
767 return;
768 }
769 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
770 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700771 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000772 // we've setup src space 0,0 to map to the top left of the src rect.
773 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000774 if (!clippedSrcRect.intersect(*srcRectPtr)) {
775 clippedSrcIRect->setEmpty();
776 return;
777 }
778 }
779 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700780 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000781 if (!clippedSrcIRect->intersect(bmpBounds)) {
782 clippedSrcIRect->setEmpty();
783 }
784}
785
reed85d91782015-09-10 14:33:38 -0700786bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
787 const SkMatrix& viewMatrix,
788 const GrTextureParams& params,
789 const SkRect* srcRectPtr,
790 int maxTileSize,
791 int* tileSize,
792 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800793 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700794 // if it's larger than the max tile size, then we have no choice but tiling.
795 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
796 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(),
797 srcRectPtr, clippedSubset);
798 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
799 return true;
800 }
801
bsalomon1a1d0b82015-10-16 07:49:42 -0700802 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700803 const size_t area = imageRect.width() * imageRect.height();
804 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
805 return false;
806 }
807
reed85d91782015-09-10 14:33:38 -0700808 // At this point we know we could do the draw by uploading the entire bitmap
809 // as a texture. However, if the texture would be large compared to the
810 // cache size and we don't require most of it for this draw then tile to
811 // reduce the amount of upload and cache spill.
812
813 // assumption here is that sw bitmap size is a good proxy for its size as
814 // a texture
815 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
816 size_t cacheSize;
817 fContext->getResourceCacheLimits(nullptr, &cacheSize);
818 if (bmpSize < cacheSize / 2) {
819 return false;
820 }
821
bsalomon1a1d0b82015-10-16 07:49:42 -0700822 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
823 // tiling memory savings would be < 50%.
reed85d91782015-09-10 14:33:38 -0700824 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(), srcRectPtr,
825 clippedSubset);
826 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
827 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
828 kBmpSmallTileSize * kBmpSmallTileSize;
829
830 return usedTileBytes < 2 * bmpSize;
831}
832
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000833bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800834 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000835 const GrTextureParams& params,
836 const SkRect* srcRectPtr,
837 int maxTileSize,
838 int* tileSize,
839 SkIRect* clippedSrcRect) const {
joshualittce894002016-01-11 13:29:31 -0800840 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000841 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700842 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000843 return false;
844 }
845
reed85d91782015-09-10 14:33:38 -0700846 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
847 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
848}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000849
reed85d91782015-09-10 14:33:38 -0700850bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
851 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
852 const SkMatrix& viewMatrix) const {
joshualittce894002016-01-11 13:29:31 -0800853 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700854 // if image is explictly texture backed then just use the texture
855 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000856 return false;
857 }
858
reed85d91782015-09-10 14:33:38 -0700859 GrTextureParams params;
860 bool doBicubic;
861 GrTextureParams::FilterMode textureFilterMode =
862 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
863
864 int tileFilterPad;
865 if (doBicubic) {
866 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
867 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
868 tileFilterPad = 0;
869 } else {
870 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000871 }
reed85d91782015-09-10 14:33:38 -0700872 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000873
bsalomon8c07b7a2015-11-02 11:36:52 -0800874 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000875
reed85d91782015-09-10 14:33:38 -0700876 // these are output, which we safely ignore, as we just want to know the predicate
877 int outTileSize;
878 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000879
reed85d91782015-09-10 14:33:38 -0700880 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
881 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000882}
883
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000884void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000885 const SkBitmap& bitmap,
886 const SkMatrix& m,
887 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800888 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800889 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800890 SkMatrix viewMatrix;
891 viewMatrix.setConcat(*origDraw.fMatrix, m);
892 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800893 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonb1b01992015-11-18 10:56:08 -0800894 // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
bsalomonf1ecd212015-12-09 17:06:02 -0800895 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
896 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800897 return;
898 }
bsalomonb1b01992015-11-18 10:56:08 -0800899 int maxTileSize = fContext->caps()->maxTileSize();
900
901 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
902 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
903 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
904 paint.isAntiAlias() &&
905 bitmap.width() <= maxTileSize &&
906 bitmap.height() <= maxTileSize;
907
908 bool skipTileCheck = drawAA || paint.getMaskFilter();
909
910 if (!skipTileCheck) {
911 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
912 int tileSize;
913 SkIRect clippedSrcRect;
914
915 GrTextureParams params;
916 bool doBicubic;
917 GrTextureParams::FilterMode textureFilterMode =
918 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
919 &doBicubic);
920
921 int tileFilterPad;
922
923 if (doBicubic) {
924 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
925 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
926 tileFilterPad = 0;
927 } else {
928 tileFilterPad = 1;
929 }
930 params.setFilterMode(textureFilterMode);
931
932 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
933 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
934 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
935 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
936 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
937 return;
938 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000939 }
bsalomonb1b01992015-11-18 10:56:08 -0800940 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800941 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
942 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000943}
944
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000945// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000946// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
947// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000948static inline void clamped_outset_with_offset(SkIRect* iRect,
949 int outset,
950 SkPoint* offset,
951 const SkIRect& clamp) {
952 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000953
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000954 int leftClampDelta = clamp.fLeft - iRect->fLeft;
955 if (leftClampDelta > 0) {
956 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000957 iRect->fLeft = clamp.fLeft;
958 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000959 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000960 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000961
962 int topClampDelta = clamp.fTop - iRect->fTop;
963 if (topClampDelta > 0) {
964 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000965 iRect->fTop = clamp.fTop;
966 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000967 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000968 }
969
970 if (iRect->fRight > clamp.fRight) {
971 iRect->fRight = clamp.fRight;
972 }
973 if (iRect->fBottom > clamp.fBottom) {
974 iRect->fBottom = clamp.fBottom;
975 }
976}
977
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000978// Break 'bitmap' into several tiles to draw it since it has already
979// been determined to be too large to fit in VRAM
980void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800981 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982 const SkRect& srcRect,
983 const SkIRect& clippedSrcIRect,
984 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800985 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700986 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000987 int tileSize,
988 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800989 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800990
ericrk82e26bf2016-02-25 22:15:29 -0800991 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entry.
ericrk369e9372016-02-05 15:32:36 -0800992 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
993
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000994 // The following pixel lock is technically redundant, but it is desirable
995 // to lock outside of the tile loop to prevent redecoding the whole image
996 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
997 // is larger than the limit of the discardable memory pool.
998 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800999
1000 const SkPaint* paint = &origPaint;
1001 SkPaint tempPaint;
1002 if (origPaint.isAntiAlias() && !fRenderTarget->isUnifiedMultisampled()) {
1003 // Drop antialiasing to avoid seams at tile boundaries.
1004 tempPaint = origPaint;
1005 tempPaint.setAntiAlias(false);
1006 paint = &tempPaint;
1007 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001008 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1009
1010 int nx = bitmap.width() / tileSize;
1011 int ny = bitmap.height() / tileSize;
1012 for (int x = 0; x <= nx; x++) {
1013 for (int y = 0; y <= ny; y++) {
1014 SkRect tileR;
1015 tileR.set(SkIntToScalar(x * tileSize),
1016 SkIntToScalar(y * tileSize),
1017 SkIntToScalar((x + 1) * tileSize),
1018 SkIntToScalar((y + 1) * tileSize));
1019
1020 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1021 continue;
1022 }
1023
1024 if (!tileR.intersect(srcRect)) {
1025 continue;
1026 }
1027
1028 SkBitmap tmpB;
1029 SkIRect iTileR;
1030 tileR.roundOut(&iTileR);
1031 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1032 SkIntToScalar(iTileR.fTop));
1033
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001034 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001035 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001036 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001037 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001038 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001039
robertphillipsec8bb942014-11-21 10:16:25 -08001040 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001041 SkIRect iClampRect;
1042
reeda5517e22015-07-14 10:54:12 -07001043 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001044 // In bleed mode we want to always expand the tile on all edges
1045 // but stay within the bitmap bounds
1046 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1047 } else {
1048 // In texture-domain/clamp mode we only want to expand the
1049 // tile on edges interior to "srcRect" (i.e., we want to
1050 // not bleed across the original clamped edges)
1051 srcRect.roundOut(&iClampRect);
1052 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001053 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1054 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001055 }
1056
1057 if (bitmap.extractSubset(&tmpB, iTileR)) {
1058 // now offset it to make it "local" to our tmp bitmap
1059 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001060 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001061 // de-optimized this determination
1062 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001063 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001064 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001065 tileR,
1066 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -08001067 *paint,
reeda5517e22015-07-14 10:54:12 -07001068 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001069 bicubic,
1070 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001071 }
1072 }
1073 }
1074}
1075
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001076/*
1077 * This is called by drawBitmap(), which has to handle images that may be too
1078 * large to be represented by a single texture.
1079 *
1080 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1081 * and that non-texture portion of the GrPaint has already been setup.
1082 */
1083void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001084 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001085 const SkRect& srcRect,
1086 const GrTextureParams& params,
1087 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001088 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001089 bool bicubic,
1090 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001091 // We should have already handled bitmaps larger than the max texture size.
1092 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1093 bitmap.height() <= fContext->caps()->maxTextureSize());
1094 // Unless the bitmap is inherently texture-backed, we should be respecting the max tile size
1095 // by the time we get here.
1096 SkASSERT(bitmap.getTexture() ||
1097 (bitmap.width() <= fContext->caps()->maxTileSize() &&
1098 bitmap.height() <= fContext->caps()->maxTileSize()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001099
1100 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001101 AutoBitmapTexture abt(fContext, bitmap, params, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001102 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001103 return;
1104 }
1105
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001106 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001107 SkRect paintRect;
1108 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1109 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1110 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1111 SkScalarMul(srcRect.fTop, hInv),
1112 SkScalarMul(srcRect.fRight, wInv),
1113 SkScalarMul(srcRect.fBottom, hInv));
1114
egdaniel79da63f2015-10-09 10:55:16 -07001115 SkMatrix texMatrix;
1116 texMatrix.reset();
1117 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1118 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1119 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1120 // Thus we need to pass in the transform matrix directly to the texture processor used for
1121 // the bitmap draw.
1122 texMatrix.setScale(wInv, hInv);
1123 }
1124
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001125 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001126
1127 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1128 // the rest from the SkPaint.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001129 SkAutoTUnref<const GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001130
reeda5517e22015-07-14 10:54:12 -07001131 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001132 // Use a constrained texture domain to avoid color bleeding
1133 SkScalar left, top, right, bottom;
1134 if (srcRect.width() > SK_Scalar1) {
1135 SkScalar border = SK_ScalarHalf / texture->width();
1136 left = paintRect.left() + border;
1137 right = paintRect.right() - border;
1138 } else {
1139 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1140 }
1141 if (srcRect.height() > SK_Scalar1) {
1142 SkScalar border = SK_ScalarHalf / texture->height();
1143 top = paintRect.top() + border;
1144 bottom = paintRect.bottom() - border;
1145 } else {
1146 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1147 }
1148 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001149 if (bicubic) {
egdaniel79da63f2015-10-09 10:55:16 -07001150 fp.reset(GrBicubicEffect::Create(texture, texMatrix, textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001151 } else {
bsalomon4a339522015-10-06 08:40:50 -07001152 fp.reset(GrTextureDomainEffect::Create(texture,
egdaniel79da63f2015-10-09 10:55:16 -07001153 texMatrix,
joshualitt5531d512014-12-17 15:50:11 -08001154 textureDomain,
1155 GrTextureDomain::kClamp_Mode,
1156 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001157 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001158 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001159 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1160 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
egdaniel79da63f2015-10-09 10:55:16 -07001161 fp.reset(GrBicubicEffect::Create(texture, texMatrix, tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001162 } else {
egdaniel79da63f2015-10-09 10:55:16 -07001163 fp.reset(GrSimpleTextureEffect::Create(texture, texMatrix, params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001164 }
1165
joshualitt33a5fce2015-11-18 13:28:51 -08001166 GrPaint grPaint;
1167 if (!SkPaintToGrPaintWithTexture(this->context(), paint, viewMatrix, fp,
1168 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001169 return;
1170 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001171
egdaniel79da63f2015-10-09 10:55:16 -07001172 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1173 // We don't have local coords in this case and have previously set the transform
1174 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001175 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001176 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001177 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001178 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001179}
1180
fmalita2d97bc12014-11-20 10:44:58 -08001181bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001182 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001183 const SkImageFilter* filter,
1184 const SkImageFilter::Context& ctx,
1185 SkBitmap* result, SkIPoint* offset) {
joshualittce894002016-01-11 13:29:31 -08001186 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001187 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001188
reed88d064d2015-10-12 11:30:02 -07001189 SkImageFilter::DeviceProxy proxy(this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001190
1191 if (filter->canFilterImageGPU()) {
robertphillips1de87df2016-01-14 06:03:29 -08001192 SkBitmap bm;
1193 GrWrapTextureInBitmap(texture, width, height, false, &bm);
robertphillips48e78462016-02-17 13:57:16 -08001194 return filter->filterImageGPUDeprecated(&proxy, bm, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195 } else {
1196 return false;
1197 }
1198}
1199
1200void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1201 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001202 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001203 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001204 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001205
1206 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1207 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1208 return;
1209 }
1210
1211 int w = bitmap.width();
1212 int h = bitmap.height();
1213
1214 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001215 // draw sprite neither filters nor tiles.
1216 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001217 if (!texture) {
1218 return;
1219 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001220
bsalomonf1b7a1d2015-09-28 06:26:28 -07001221 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
1222
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001223 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001224 // This bitmap will own the filtered result as a texture.
1225 SkBitmap filteredBitmap;
1226
bsalomon49f085d2014-09-05 13:34:00 -07001227 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001228 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001229 SkMatrix matrix(*draw.fMatrix);
1230 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblancodb64af32015-12-09 10:11:43 -08001231 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-left, -top);
senorblancobe129b22014-08-08 07:14:35 -07001232 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001233 // This cache is transient, and is freed (along with all its contained
1234 // textures) when it goes out of scope.
reed4e23cda2016-01-11 10:56:59 -08001235 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001236 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001237 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001238 texture = (GrTexture*) filteredBitmap.getTexture();
1239 w = filteredBitmap.width();
1240 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001241 left += offset.x();
1242 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001243 } else {
1244 return;
1245 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001246 SkASSERT(!GrPixelConfigIsAlphaOnly(texture->config()));
1247 alphaOnly = false;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001248 }
1249
1250 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001251 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001252 GrSimpleTextureEffect::Create(texture, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001253 if (alphaOnly) {
1254 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1255 } else {
1256 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1257 }
1258 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001259 return;
1260 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001261
bsalomona2e69fc2015-11-05 10:41:43 -08001262 fDrawContext->fillRectToRect(fClip,
1263 grPaint,
1264 SkMatrix::I(),
1265 SkRect::MakeXYWH(SkIntToScalar(left),
1266 SkIntToScalar(top),
1267 SkIntToScalar(w),
1268 SkIntToScalar(h)),
1269 SkRect::MakeXYWH(0,
1270 0,
1271 SK_Scalar1 * w / texture->width(),
1272 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001273}
1274
bsalomonb1b01992015-11-18 10:56:08 -08001275void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1276 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001277 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001278 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001279 CHECK_SHOULD_DRAW(draw);
bsalomonb1b01992015-11-18 10:56:08 -08001280 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001281 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001282 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
1283 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001284 return;
1285 }
bsalomonb1b01992015-11-18 10:56:08 -08001286 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1287 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1288 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1289 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1290 const SkRect* dst = &origDst;
1291 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001292 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001293 if (!src) {
1294 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001295 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001296
bsalomonb1b01992015-11-18 10:56:08 -08001297 SkMatrix srcToDstMatrix;
1298 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1299 return;
1300 }
1301 SkRect tmpSrc, tmpDst;
1302 if (src != &bmpBounds) {
1303 if (!bmpBounds.contains(*src)) {
1304 tmpSrc = *src;
1305 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001306 return; // nothing to draw
1307 }
bsalomonb1b01992015-11-18 10:56:08 -08001308 src = &tmpSrc;
1309 srcToDstMatrix.mapRect(&tmpDst, *src);
1310 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001311 }
1312 }
1313
bsalomonb1b01992015-11-18 10:56:08 -08001314 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001315
bsalomonb1b01992015-11-18 10:56:08 -08001316 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1317 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
1318 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
1319 paint.isAntiAlias() &&
1320 bitmap.width() <= maxTileSize &&
1321 bitmap.height() <= maxTileSize;
1322
1323 bool skipTileCheck = drawAA || paint.getMaskFilter();
1324
1325 if (!skipTileCheck) {
1326 int tileSize;
1327 SkIRect clippedSrcRect;
1328
1329 GrTextureParams params;
1330 bool doBicubic;
1331 GrTextureParams::FilterMode textureFilterMode =
1332 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1333 &doBicubic);
1334
1335 int tileFilterPad;
1336
1337 if (doBicubic) {
1338 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1339 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1340 tileFilterPad = 0;
1341 } else {
1342 tileFilterPad = 1;
1343 }
1344 params.setFilterMode(textureFilterMode);
1345
1346 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1347 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1348 // we have a mask filter.
1349 SkMatrix viewMatrix = *draw.fMatrix;
1350 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1351 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1352 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1353 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1354 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1355 constraint, tileSize, doBicubic);
1356 return;
1357 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001358 }
bsalomonb1b01992015-11-18 10:56:08 -08001359 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001360 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001361}
1362
1363void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1364 int x, int y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001365 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001366 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001367 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001368 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001369
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001370 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001371 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001372
1373 GrRenderTarget* devRT = dev->accessRenderTarget();
1374 GrTexture* devTex;
halcanary96fcdcc2015-08-27 07:41:13 -07001375 if (nullptr == (devTex = devRT->asTexture())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001376 return;
1377 }
1378
robertphillips7b9e8a42014-12-11 08:20:31 -08001379 const SkImageInfo ii = dev->imageInfo();
1380 int w = ii.width();
1381 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001382
1383 SkImageFilter* filter = paint.getImageFilter();
1384 // This bitmap will own the filtered result as a texture.
1385 SkBitmap filteredBitmap;
1386
bsalomon49f085d2014-09-05 13:34:00 -07001387 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001388 SkIPoint offset = SkIPoint::Make(0, 0);
1389 SkMatrix matrix(*draw.fMatrix);
1390 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblancodb64af32015-12-09 10:11:43 -08001391 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-x, -y);
senorblanco55b6d8b2014-07-30 11:26:46 -07001392 // This cache is transient, and is freed (along with all its contained
1393 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001394 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
reed4e23cda2016-01-11 10:56:59 -08001395 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001396 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1397 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001398 devTex = filteredBitmap.getTexture();
1399 w = filteredBitmap.width();
1400 h = filteredBitmap.height();
1401 x += offset.fX;
1402 y += offset.fY;
1403 } else {
1404 return;
1405 }
1406 }
1407
1408 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001409 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001410 GrSimpleTextureEffect::Create(devTex, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001411 if (GrPixelConfigIsAlphaOnly(devTex->config())) {
1412 // Can this happen?
1413 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1414 } else {
1415 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1416 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001417
bsalomonf1b7a1d2015-09-28 06:26:28 -07001418 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001419 return;
1420 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001421
1422 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1423 SkIntToScalar(y),
1424 SkIntToScalar(w),
1425 SkIntToScalar(h));
1426
1427 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1428 // scratch texture).
1429 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1430 SK_Scalar1 * h / devTex->height());
1431
bsalomona2e69fc2015-11-05 10:41:43 -08001432 fDrawContext->fillRectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001433}
1434
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001435bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
joshualittce894002016-01-11 13:29:31 -08001436 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001437 return filter->canFilterImageGPU();
1438}
1439
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001440bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001441 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001442 SkBitmap* result, SkIPoint* offset) {
joshualittce894002016-01-11 13:29:31 -08001443 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001444 // want explicitly our impl, so guard against a subclass of us overriding it
1445 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1446 return false;
1447 }
1448
1449 SkAutoLockPixels alp(src, !src.getTexture());
1450 if (!src.getTexture() && !src.readyToDraw()) {
1451 return false;
1452 }
1453
1454 GrTexture* texture;
1455 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1456 // must be pushed upstack.
bsalomonafa95e22015-10-12 10:39:46 -07001457 AutoBitmapTexture abt(fContext, src, GrTextureParams::ClampNoFilter(), &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001458 if (!texture) {
1459 return false;
1460 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001461
senorblancod0d37ca2015-04-02 04:54:56 -07001462 return this->filterTexture(fContext, texture, src.width(), src.height(),
1463 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001464}
1465
reeda85d4d02015-05-06 12:56:48 -07001466void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1467 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001468 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001469 SkMatrix viewMatrix = *draw.fMatrix;
1470 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001471 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001472 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001473 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001474 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1475 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001476 return;
reed85d91782015-09-10 14:33:38 -07001477 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001478 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001479 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1480 paint.getFilterQuality(), *draw.fMatrix)) {
1481 // only support tiling as bitmap at the moment, so force raster-version
1482 if (!as_IB(image)->getROPixels(&bm)) {
1483 return;
1484 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001485 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1486 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1487 CHECK_SHOULD_DRAW(draw);
1488 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001489 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1490 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001491 } else if (as_IB(image)->getROPixels(&bm)) {
1492 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001493 }
reeda85d4d02015-05-06 12:56:48 -07001494 }
1495}
1496
1497void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001498 const SkRect& dst, const SkPaint& paint,
1499 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001500 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001501 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001502 CHECK_SHOULD_DRAW(draw);
1503 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001504 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001505 return;
1506 }
1507 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001508 SkMatrix totalMatrix = *draw.fMatrix;
1509 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1510 dst.height() / (src ? src->height() : image->height()));
1511 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001512 // only support tiling as bitmap at the moment, so force raster-version
1513 if (!as_IB(image)->getROPixels(&bm)) {
1514 return;
1515 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001516 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1517 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1518 CHECK_SHOULD_DRAW(draw);
1519 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001520 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001521 } else if (as_IB(image)->getROPixels(&bm)) {
1522 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001523 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001524}
1525
bsalomonf1ecd212015-12-09 17:06:02 -08001526void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001527 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001528 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001529
1530 CHECK_FOR_ANNOTATION(paint);
1531 CHECK_SHOULD_DRAW(draw);
1532
joshualittedb36442015-11-19 14:29:30 -08001533 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
1534 fRenderTarget->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001535 bool doBicubic;
1536 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001537 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1538 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001539 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001540 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001541
1542 SkRect srcR, dstR;
1543 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001544 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001545 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001546 }
1547 return;
1548 }
1549
bsalomon2bbd0c62015-12-09 12:50:56 -08001550 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
1551 SkAutoTUnref<const GrFragmentProcessor> fp(
1552 producer->createFragmentProcessor(SkMatrix::I(),
1553 SkRect::MakeIWH(producer->width(), producer->height()),
1554 GrTextureProducer::kNo_FilterConstraint, true,
1555 &kMode));
joshualitt33a5fce2015-11-18 13:28:51 -08001556 GrPaint grPaint;
1557 if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, fp,
bsalomonf1ecd212015-12-09 17:06:02 -08001558 producer->isAlphaOnly(), &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001559 return;
1560 }
1561
bsalomon2bbd0c62015-12-09 12:50:56 -08001562 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1563 producer->height(), center, dst);
1564}
1565
1566void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1567 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001568 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001569 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001570 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001571 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001572 } else {
1573 SkBitmap bm;
1574 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1575 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001576 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001577 } else if (as_IB(image)->getROPixels(&bm)) {
1578 this->drawBitmapNine(draw, bm, center, dst, paint);
1579 }
1580 }
1581}
1582
1583void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1584 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001585 ASSERT_SINGLE_OWNER
bsalomon2bbd0c62015-12-09 12:50:56 -08001586 if (bitmap.getTexture()) {
1587 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001588 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001589 } else {
1590 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001591 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001592 }
joshualitt33a5fce2015-11-18 13:28:51 -08001593}
1594
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001595///////////////////////////////////////////////////////////////////////////////
1596
1597// must be in SkCanvas::VertexMode order
1598static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1599 kTriangles_GrPrimitiveType,
1600 kTriangleStrip_GrPrimitiveType,
1601 kTriangleFan_GrPrimitiveType,
1602};
1603
1604void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1605 int vertexCount, const SkPoint vertices[],
1606 const SkPoint texs[], const SkColor colors[],
1607 SkXfermode* xmode,
1608 const uint16_t indices[], int indexCount,
1609 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001610 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001611 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001612 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001613
halcanary96fcdcc2015-08-27 07:41:13 -07001614 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1615 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001616
halcanary96fcdcc2015-08-27 07:41:13 -07001617 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001618
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001619 SkPaint copy(paint);
1620 copy.setStyle(SkPaint::kStroke_Style);
1621 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001622
bsalomonf1b7a1d2015-09-28 06:26:28 -07001623 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001624 // we ignore the shader if texs is null.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001625 if (!SkPaintToGrPaintNoShader(this->context(), copy, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001626 return;
1627 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001628
dandov32a311b2014-07-15 19:46:26 -07001629 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001630 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001631 switch (vmode) {
1632 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001633 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001634 break;
1635 case SkCanvas::kTriangleStrip_VertexMode:
1636 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001637 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001638 break;
1639 }
mtklein533eb782014-08-27 10:39:42 -07001640
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001641 VertState state(vertexCount, indices, indexCount);
1642 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001643
dandov32a311b2014-07-15 19:46:26 -07001644 //number of indices for lines per triangle with kLines
1645 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001646
bsalomonf1b7a1d2015-09-28 06:26:28 -07001647 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001648 int i = 0;
1649 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001650 lineIndices[i] = state.f0;
1651 lineIndices[i + 1] = state.f1;
1652 lineIndices[i + 2] = state.f1;
1653 lineIndices[i + 3] = state.f2;
1654 lineIndices[i + 4] = state.f2;
1655 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001656 i += 6;
1657 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001658 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001659 grPaint,
1660 *draw.fMatrix,
1661 kLines_GrPrimitiveType,
1662 vertexCount,
1663 vertices,
1664 texs,
1665 colors,
1666 lineIndices.get(),
1667 indexCount);
1668 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001669 }
1670
bsalomonf1b7a1d2015-09-28 06:26:28 -07001671 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001672
1673 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001674 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001675 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1676 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001677 convertedColors.reset(vertexCount);
1678 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001679 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001680 }
1681 colors = convertedColors.get();
1682 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001683 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001684 if (texs && paint.getShader()) {
1685 if (colors) {
1686 // When there are texs and colors the shader and colors are combined using xmode. A null
1687 // xmode is defined to mean modulate.
1688 SkXfermode::Mode colorMode;
1689 if (xmode) {
1690 if (!xmode->asMode(&colorMode)) {
1691 return;
1692 }
1693 } else {
1694 colorMode = SkXfermode::kModulate_Mode;
1695 }
1696 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
1697 false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001698 return;
1699 }
1700 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001701 // We have a shader, but no colors to blend it against.
1702 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
1703 return;
1704 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001705 }
bsalomonaa48d362015-10-01 08:34:17 -07001706 } else {
1707 if (colors) {
1708 // We have colors, but either have no shader or no texture coords (which implies that
1709 // we should ignore the shader).
1710 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint, &grPaint)) {
1711 return;
1712 }
1713 } else {
1714 // No colors and no shaders. Just draw with the paint color.
1715 if (!SkPaintToGrPaintNoShader(this->context(), paint, &grPaint)) {
1716 return;
1717 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001718 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001719 }
1720
robertphillips2e1e51f2015-10-15 08:01:48 -07001721 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001722 grPaint,
1723 *draw.fMatrix,
1724 primType,
1725 vertexCount,
1726 vertices,
1727 texs,
1728 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001729 indices,
robertphillipsea461502015-05-26 11:38:03 -07001730 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001731}
1732
1733///////////////////////////////////////////////////////////////////////////////
1734
jvanverth31ff7622015-08-07 10:09:28 -07001735void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001736 const SkRect texRect[], const SkColor colors[], int count,
1737 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001738 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001739 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001740 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001741 return;
1742 }
1743
jvanverth31ff7622015-08-07 10:09:28 -07001744 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001745 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001746
reedca109532015-06-25 16:25:25 -07001747 SkPaint p(paint);
1748 p.setShader(atlas->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
1749
jvanverth31ff7622015-08-07 10:09:28 -07001750 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001751 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001752 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
1753 &grPaint)) {
1754 return;
1755 }
1756 } else {
1757 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix, &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001758 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001759 }
1760 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001761
1762 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001763 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001764}
1765
1766///////////////////////////////////////////////////////////////////////////////
1767
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001768void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001769 size_t byteLength, SkScalar x, SkScalar y,
1770 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001771 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001772 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001773 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001774
jvanverth8c27a182014-10-14 08:45:50 -07001775 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001776 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001777 return;
1778 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001779
jvanverth8c27a182014-10-14 08:45:50 -07001780 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001781
robertphillips2e1e51f2015-10-15 08:01:48 -07001782 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001783 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001784}
1785
fmalita05c4a432014-09-29 06:29:53 -07001786void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1787 const SkScalar pos[], int scalarsPerPos,
1788 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001789 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001790 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001791 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001792
jvanverth8c27a182014-10-14 08:45:50 -07001793 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001794 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001795 return;
1796 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001797
jvanverth8c27a182014-10-14 08:45:50 -07001798 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001799
robertphillips2e1e51f2015-10-15 08:01:48 -07001800 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001801 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1802 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001803}
1804
joshualitt9c328182015-03-23 08:13:04 -07001805void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1806 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001807 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001808 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001809 CHECK_SHOULD_DRAW(draw);
1810
1811 SkDEBUGCODE(this->validate();)
1812
robertphillips2e1e51f2015-10-15 08:01:48 -07001813 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001814 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001815}
1816
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001817///////////////////////////////////////////////////////////////////////////////
1818
reedb2db8982014-11-13 12:41:02 -08001819bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001820 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001821}
1822
1823void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001824 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001825
bsalomonc49e8682015-06-30 11:37:35 -07001826 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001827}
1828
1829///////////////////////////////////////////////////////////////////////////////
1830
reed76033be2015-03-14 10:54:31 -07001831SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001832 ASSERT_SINGLE_OWNER
bsalomonf2703d82014-10-28 14:33:06 -07001833 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001834 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001835 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001836 desc.fWidth = cinfo.fInfo.width();
1837 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001838 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001839
1840 SkAutoTUnref<GrTexture> texture;
1841 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001842 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001843
hcm4396fa52014-10-27 21:43:30 -07001844 // layers are never draw in repeat modes, so we can request an approx
1845 // match and ignore any padding.
bsalomoneae62002015-07-31 13:59:30 -07001846 if (kNever_TileUsage == cinfo.fTileUsage) {
1847 texture.reset(fContext->textureProvider()->createApproxTexture(desc));
1848 } else {
bsalomon5ec26ae2016-02-25 08:33:02 -08001849 texture.reset(fContext->textureProvider()->createTexture(desc, SkBudgeted::kYes));
bsalomoneae62002015-07-31 13:59:30 -07001850 }
bsalomonafe30052015-01-16 07:32:33 -08001851
1852 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001853 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001854 return SkGpuDevice::Create(
bsalomon74f681d2015-06-23 14:38:48 -07001855 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, init);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001856 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001857 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001858 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001859 cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary96fcdcc2015-08-27 07:41:13 -07001860 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001861 }
1862}
1863
reed4a8126e2014-09-22 07:29:03 -07001864SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001865 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001866 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001867 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
vbuzinovdded6962015-06-12 08:59:45 -07001868 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
bsalomonafe30052015-01-16 07:32:33 -08001869 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001870}
1871
robertphillips30d2cc62014-09-24 08:52:18 -07001872bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001873 const SkMatrix* matrix, const SkPaint* paint) {
joshualittce894002016-01-11 13:29:31 -08001874 ASSERT_SINGLE_OWNER
robertphillips63242d72014-12-04 08:31:02 -08001875#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001876 // todo: should handle this natively
1877 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001878 return false;
1879 }
1880
halcanary96fcdcc2015-08-27 07:41:13 -07001881 const SkBigPicture::AccelData* data = nullptr;
mtklein9db912c2015-05-19 11:11:26 -07001882 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1883 data = bp->accelData();
1884 }
robertphillips81f71b62014-11-11 04:54:49 -08001885 if (!data) {
1886 return false;
1887 }
1888
robertphillipse5524cd2015-02-20 12:30:26 -08001889 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1890 if (0 == gpuData->numBlocks()) {
1891 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001892 }
1893
robertphillipsfd61ed02014-10-28 07:21:44 -07001894 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001895
robertphillipse5524cd2015-02-20 12:30:26 -08001896 SkIRect iBounds;
1897 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1898 return false;
1899 }
1900
1901 SkRect clipBounds = SkRect::Make(iBounds);
1902
1903 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1904
robertphillips60029a52015-11-09 13:51:06 -08001905 GrLayerHoister::Begin(fContext);
1906
robertphillipsfd61ed02014-10-28 07:21:44 -07001907 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001908 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001909 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001910 &atlasedNeedRendering, &atlasedRecycled,
vbuzinovdded6962015-06-12 08:59:45 -07001911 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001912
1913 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1914
1915 SkTDArray<GrHoistedLayer> needRendering, recycled;
1916
robertphillipse5524cd2015-02-20 12:30:26 -08001917 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1918
robertphillipsfd61ed02014-10-28 07:21:44 -07001919 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001920 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001921 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001922 &needRendering, &recycled,
vbuzinovdded6962015-06-12 08:59:45 -07001923 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001924
1925 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001926
robertphillips64bf7672014-08-21 13:07:35 -07001927 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001928 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
halcanary96fcdcc2015-08-27 07:41:13 -07001929 initialMatrix, nullptr);
robertphillips64bf7672014-08-21 13:07:35 -07001930
robertphillipsfd61ed02014-10-28 07:21:44 -07001931 GrLayerHoister::UnlockLayers(fContext, needRendering);
1932 GrLayerHoister::UnlockLayers(fContext, recycled);
1933 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1934 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips60029a52015-11-09 13:51:06 -08001935 GrLayerHoister::End(fContext);
robertphillips64bf7672014-08-21 13:07:35 -07001936
1937 return true;
robertphillips63242d72014-12-04 08:31:02 -08001938#else
1939 return false;
1940#endif
robertphillips64bf7672014-08-21 13:07:35 -07001941}
1942
reed13ccbf82015-10-20 09:56:52 -07001943SkImageFilter::Cache* SkGpuDevice::NewImageFilterCache() {
1944 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
1945}
1946
senorblancobe129b22014-08-08 07:14:35 -07001947SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001948 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001949 // We always return a transient cache, so it is freed after each
1950 // filter traversal.
reed13ccbf82015-10-20 09:56:52 -07001951 return SkGpuDevice::NewImageFilterCache();
senorblanco55b6d8b2014-07-30 11:26:46 -07001952}
reedf037e0b2014-10-30 11:34:15 -07001953
1954#endif