blob: df5c7a73a11a18b68df092008608db8e6b5b91f0 [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
9
robertphillipsccb1b572015-05-27 11:02:55 -070010#include "GrBlurUtils.h"
kkinnunenabcfab42015-02-22 22:53:44 -080011#include "GrContext.h"
herb11a7f7f2015-11-24 12:41:00 -080012#include "SkDraw.h"
kkinnunenabcfab42015-02-22 22:53:44 -080013#include "GrGpu.h"
14#include "GrGpuResourcePriv.h"
bsalomonc55271f2015-11-09 11:55:57 -080015#include "GrImageIDTextureAdjuster.h"
robertphillips98d709b2014-09-02 10:20:50 -070016#include "GrLayerHoister.h"
robertphillips274b4ba2014-09-04 07:24:18 -070017#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070018#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070019#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080020#include "SkCanvasPriv.h"
kkinnunenabcfab42015-02-22 22:53:44 -080021#include "SkErrorInternals.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000022#include "SkGlyphCache.h"
kkinnunenabcfab42015-02-22 22:53:44 -080023#include "SkGrTexturePixelRef.h"
bsalomonf276ac52015-10-09 13:36:42 -070024#include "SkGr.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070025#include "SkGrPriv.h"
reeda85d4d02015-05-06 12:56:48 -070026#include "SkImage_Base.h"
bsalomon1cf6f9b2015-12-08 10:53:43 -080027#include "SkImageCacherator.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080029#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000030#include "SkMaskFilter.h"
joshualitt33a5fce2015-11-18 13:28:51 -080031#include "SkNinePatchIter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000032#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000033#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070034#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080036#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000038#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080039#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000040#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000041#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000042#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070043#include "SkXfermode.h"
joshualitta61c8172015-08-17 10:51:22 -070044#include "batches/GrRectBatchFactory.h"
kkinnunenabcfab42015-02-22 22:53:44 -080045#include "effects/GrBicubicEffect.h"
46#include "effects/GrDashingEffect.h"
47#include "effects/GrSimpleTextureEffect.h"
48#include "effects/GrTextureDomain.h"
joshualitt8e84a1e2016-02-16 11:09:25 -080049#include "text/GrTextUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000050
reedf037e0b2014-10-30 11:34:15 -070051#if SK_SUPPORT_GPU
52
joshualittce894002016-01-11 13:29:31 -080053#define ASSERT_SINGLE_OWNER \
54 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->debugSingleOwner());)
55
senorblanco55b6d8b2014-07-30 11:26:46 -070056enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
57
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058#if 0
59 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080060 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000061 do { \
62 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080063 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000064 } while (0)
65#else
joshualitt5531d512014-12-17 15:50:11 -080066 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000067#endif
68
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000069///////////////////////////////////////////////////////////////////////////////
70
71#define CHECK_FOR_ANNOTATION(paint) \
72 do { if (paint.getAnnotation()) { return; } } while (0)
73
74///////////////////////////////////////////////////////////////////////////////
75
bsalomonbcf0a522014-10-08 08:40:09 -070076// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
77// just accesses the backing GrTexture. Otherwise, it creates a cached texture
78// representation and releases it in the destructor.
79class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040080public:
bsalomonbcf0a522014-10-08 08:40:09 -070081 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070082
bsalomonbcf0a522014-10-08 08:40:09 -070083 AutoBitmapTexture(GrContext* context,
84 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -070085 const GrTextureParams& params,
bsalomonbcf0a522014-10-08 08:40:09 -070086 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040087 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070088 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040089 }
90
bsalomonbcf0a522014-10-08 08:40:09 -070091 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040092 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -070093 const GrTextureParams& params) {
bsalomonbcf0a522014-10-08 08:40:09 -070094 // Either get the texture directly from the bitmap, or else use the cache and
95 // remember to unref it.
96 if (GrTexture* bmpTexture = bitmap.getTexture()) {
halcanary96fcdcc2015-08-27 07:41:13 -070097 fTexture.reset(nullptr);
bsalomonbcf0a522014-10-08 08:40:09 -070098 return bmpTexture;
99 } else {
100 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
101 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400102 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400103 }
104
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000105private:
bsalomonbcf0a522014-10-08 08:40:09 -0700106 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000107};
108
109///////////////////////////////////////////////////////////////////////////////
110
bsalomon74f681d2015-06-23 14:38:48 -0700111/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
112 should fail. */
113bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
114 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
115 *flags = 0;
116 if (info) {
117 switch (info->alphaType()) {
118 case kPremul_SkAlphaType:
119 break;
120 case kOpaque_SkAlphaType:
121 *flags |= SkGpuDevice::kIsOpaque_Flag;
122 break;
123 default: // If it is unpremul or unknown don't try to render
124 return false;
125 }
126 }
127 if (kClear_InitContents == init) {
128 *flags |= kNeedClear_Flag;
129 }
130 return true;
131}
132
133SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props,
134 InitContents init) {
135 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, init);
senorblancod0d37ca2015-04-02 04:54:56 -0700136}
137
138SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
bsalomon74f681d2015-06-23 14:38:48 -0700139 const SkSurfaceProps* props, InitContents init) {
bsalomonafe30052015-01-16 07:32:33 -0800140 if (!rt || rt->wasDestroyed()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700141 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000142 }
bsalomon74f681d2015-06-23 14:38:48 -0700143 unsigned flags;
halcanary96fcdcc2015-08-27 07:41:13 -0700144 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
145 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700146 }
halcanary385fe4d2015-08-26 13:07:48 -0700147 return new SkGpuDevice(rt, width, height, props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000148}
149
bsalomon74f681d2015-06-23 14:38:48 -0700150SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
151 const SkImageInfo& info, int sampleCount,
erikchen9a1ed5d2016-02-10 16:32:34 -0800152 const SkSurfaceProps* props, InitContents init,
153 GrTextureStorageAllocator customAllocator) {
bsalomon74f681d2015-06-23 14:38:48 -0700154 unsigned flags;
155 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700156 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700157 }
158
erikchen9a1ed5d2016-02-10 16:32:34 -0800159 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(
160 context, budgeted, info, sampleCount, customAllocator));
halcanary96fcdcc2015-08-27 07:41:13 -0700161 if (nullptr == rt) {
162 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700163 }
164
halcanary385fe4d2015-08-26 13:07:48 -0700165 return new SkGpuDevice(rt, info.width(), info.height(), props, flags);
bsalomon74f681d2015-06-23 14:38:48 -0700166}
167
senorblancod0d37ca2015-04-02 04:54:56 -0700168SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
169 const SkSurfaceProps* props, unsigned flags)
robertphillipsfcf78292015-06-19 11:49:52 -0700170 : INHERITED(SkSurfacePropsCopyOrDefault(props))
joshualitt06dd0a82016-01-08 06:32:31 -0800171 , fContext(SkRef(rt->getContext()))
172 , fRenderTarget(SkRef(rt)) {
bsalomon74f681d2015-06-23 14:38:48 -0700173 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000174
bsalomon74f681d2015-06-23 14:38:48 -0700175 SkAlphaType at = fOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
176 SkImageInfo info = rt->surfacePriv().info(at).makeWH(width, height);
halcanary385fe4d2015-08-26 13:07:48 -0700177 SkPixelRef* pr = new SkGrPixelRef(info, rt);
bsalomonafbf2d62014-09-30 12:18:44 -0700178 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700179 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700180
robertphillips77a2e522015-10-17 07:43:27 -0700181 fDrawContext.reset(this->context()->drawContext(rt, &this->surfaceProps()));
bsalomone63ffef2016-02-05 07:17:34 -0800182 if (flags & kNeedClear_Flag) {
183 this->clearAll();
184 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000185}
186
erikchen9a1ed5d2016-02-10 16:32:34 -0800187GrRenderTarget* SkGpuDevice::CreateRenderTarget(
188 GrContext* context, SkSurface::Budgeted budgeted, const SkImageInfo& origInfo,
189 int sampleCount, GrTextureStorageAllocator textureStorageAllocator) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000190 if (kUnknown_SkColorType == origInfo.colorType() ||
191 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700192 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000193 }
194
bsalomonafe30052015-01-16 07:32:33 -0800195 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700196 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800197 }
198
reede5ea5002014-09-03 11:54:58 -0700199 SkColorType ct = origInfo.colorType();
200 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700201 if (kRGB_565_SkColorType == ct) {
202 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800203 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
204 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700205 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800206 }
207 if (kOpaque_SkAlphaType != at) {
208 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000209 }
reede5ea5002014-09-03 11:54:58 -0700210 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000211
bsalomonf2703d82014-10-28 14:33:06 -0700212 GrSurfaceDesc desc;
213 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000214 desc.fWidth = info.width();
215 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000216 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000217 desc.fSampleCnt = sampleCount;
erikchen9a1ed5d2016-02-10 16:32:34 -0800218 desc.fTextureStorageAllocator = textureStorageAllocator;
bsalomond309e7a2015-04-30 14:18:54 -0700219 GrTexture* texture = context->textureProvider()->createTexture(
halcanary96fcdcc2015-08-27 07:41:13 -0700220 desc, SkToBool(budgeted), nullptr, 0);
221 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
324 SkSurface::Budgeted budgeted =
325 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
326 : SkSurface::kNo_Budgeted;
327
328 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
erikchen9a1ed5d2016-02-10 16:32:34 -0800329 this->context(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt,
330 fRenderTarget->desc().fTextureStorageAllocator));
kkinnunenabcfab42015-02-22 22:53:44 -0800331
halcanary96fcdcc2015-08-27 07:41:13 -0700332 if (nullptr == newRT) {
kkinnunenabcfab42015-02-22 22:53:44 -0800333 return;
334 }
335
336 if (shouldRetainContent) {
337 if (fRenderTarget->wasDestroyed()) {
338 return;
339 }
340 this->context()->copySurface(newRT, fRenderTarget);
341 }
342
343 SkASSERT(fRenderTarget != newRT);
344
joshualitt06dd0a82016-01-08 06:32:31 -0800345 fRenderTarget.reset(newRT.detach());
kkinnunenabcfab42015-02-22 22:53:44 -0800346
bsalomon74f681d2015-06-23 14:38:48 -0700347#ifdef SK_DEBUG
348 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
349 kPremul_SkAlphaType);
350 SkASSERT(info == fLegacyBitmap.info());
351#endif
halcanary385fe4d2015-08-26 13:07:48 -0700352 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget);
kkinnunenabcfab42015-02-22 22:53:44 -0800353 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700354
robertphillips77a2e522015-10-17 07:43:27 -0700355 fDrawContext.reset(this->context()->drawContext(fRenderTarget, &this->surfaceProps()));
kkinnunenabcfab42015-02-22 22:53:44 -0800356}
357
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000358///////////////////////////////////////////////////////////////////////////////
359
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000360void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800361 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800362 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800363 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000364
365 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700366 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700367 return;
368 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000369
robertphillips2e1e51f2015-10-15 08:01:48 -0700370 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000371}
372
373// must be in SkCanvas::PointMode order
374static const GrPrimitiveType gPointMode2PrimtiveType[] = {
375 kPoints_GrPrimitiveType,
376 kLines_GrPrimitiveType,
377 kLineStrip_GrPrimitiveType
378};
379
ethannicholas330bb952015-07-17 06:44:02 -0700380// suppress antialiasing on axis-aligned integer-coordinate lines
381static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
382 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
383 return false;
384 }
385 if (count == 2) {
386 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
387 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800388 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700389 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800390 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700391 // the two end pixels as being the lesser of two evils.
392 if (pts[0].fX == pts[1].fX) {
393 return ((int) pts[0].fX) != pts[0].fX;
394 }
395 if (pts[0].fY == pts[1].fY) {
396 return ((int) pts[0].fY) != pts[0].fY;
397 }
398 }
399 return true;
400}
401
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000402void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
403 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800404 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800405 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000406 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800407 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000408
409 SkScalar width = paint.getStrokeWidth();
410 if (width < 0) {
411 return;
412 }
413
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000414 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700415 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
416 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700417 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700418 return;
419 }
egdaniele61c4112014-06-12 10:24:21 -0700420 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700421 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700422 path.moveTo(pts[0]);
423 path.lineTo(pts[1]);
robertphillips2e1e51f2015-10-15 08:01:48 -0700424 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700425 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000426 }
427
ethannicholas330bb952015-07-17 06:44:02 -0700428 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000429 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800430 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700431 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000432 draw.drawPoints(mode, count, pts, paint, true);
433 return;
434 }
435
436 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700437 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700438 return;
439 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000440
robertphillips2e1e51f2015-10-15 08:01:48 -0700441 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700442 grPaint,
443 *draw.fMatrix,
444 gPointMode2PrimtiveType[mode],
445 SkToS32(count),
446 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700447 nullptr,
448 nullptr,
449 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700450 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000451}
452
453///////////////////////////////////////////////////////////////////////////////
454
robertphillipsff55b492015-11-24 07:56:59 -0800455void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800456 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800457 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000458 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800459 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000460
461 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
462 SkScalar width = paint.getStrokeWidth();
463
464 /*
465 We have special code for hairline strokes, miter-strokes, bevel-stroke
466 and fills. Anything else we just call our path code.
467 */
468 bool usePath = doStroke && width > 0 &&
469 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
470 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
egdanield58a0ba2014-06-11 10:30:05 -0700471
robertphillipsd8aa59d2015-08-05 09:07:12 -0700472 // a few other reasons we might need to call drawPath...
robertphillipsff55b492015-11-24 07:56:59 -0800473 if (paint.getMaskFilter() || paint.getPathEffect() ||
robertphillipsd8aa59d2015-08-05 09:07:12 -0700474 paint.getStyle() == SkPaint::kStrokeAndFill_Style) { // we can't both stroke and fill rects
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000475 usePath = true;
476 }
egdanield58a0ba2014-06-11 10:30:05 -0700477
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000478 if (usePath) {
479 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700480 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000481 path.addRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800482 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800483 fClip, path, paint,
484 *draw.fMatrix, nullptr,
485 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000486 return;
487 }
488
489 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700490 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700491 return;
492 }
Mike Klein744fb732014-06-23 15:13:26 -0400493
robertphillipsff55b492015-11-24 07:56:59 -0800494 GrStrokeInfo strokeInfo(paint);
495
robertphillips2e1e51f2015-10-15 08:01:48 -0700496 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000497}
498
499///////////////////////////////////////////////////////////////////////////////
500
501void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800502 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800503 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800504 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000505 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800506 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000507
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000508 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700509 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700510 return;
511 }
Mike Klein744fb732014-06-23 15:13:26 -0400512
egdanield58a0ba2014-06-11 10:30:05 -0700513 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000514 if (paint.getMaskFilter()) {
515 // try to hit the fast path for drawing filtered round rects
516
517 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800518 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000519 if (devRRect.allCornersCircular()) {
520 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700521 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
joshualitt5531d512014-12-17 15:50:11 -0800522 draw.fClip->getBounds(),
523 *draw.fMatrix,
524 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000525 SkIRect finalIRect;
526 maskRect.roundOut(&finalIRect);
527 if (draw.fClip->quickReject(finalIRect)) {
528 // clipped out
529 return;
530 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700531 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
532 fDrawContext,
joshualitt25d9c152015-02-18 12:29:52 -0800533 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800534 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800535 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700536 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700537 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000538 return;
539 }
540 }
541
542 }
543 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000544 }
545
robertphillips514450c2015-11-24 05:36:02 -0800546 if (paint.getMaskFilter() || paint.getPathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800547 // The only mask filter the native rrect drawing code could've handle was taken
548 // care of above.
549 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700551 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000552 path.addRRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800553 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800554 fClip, path, paint,
555 *draw.fMatrix, nullptr,
556 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000557 return;
558 }
Mike Klein744fb732014-06-23 15:13:26 -0400559
robertphillips514450c2015-11-24 05:36:02 -0800560 SkASSERT(!strokeInfo.isDashed());
561
robertphillips2e1e51f2015-10-15 08:01:48 -0700562 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000563}
564
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000565void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800566 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800567 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800568 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800569 CHECK_FOR_ANNOTATION(paint);
570 CHECK_SHOULD_DRAW(draw);
571
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000572 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000573
robertphillips0e7029e2015-11-30 05:45:06 -0800574 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000575 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700576 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700577 return;
578 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000579
robertphillips0e7029e2015-11-30 05:45:06 -0800580 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
581 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000582 }
583
584 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700585 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000586 path.addRRect(outer);
587 path.addRRect(inner);
588 path.setFillType(SkPath::kEvenOdd_FillType);
589
robertphillips7bceedc2015-12-01 12:51:26 -0800590 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillips0e7029e2015-11-30 05:45:06 -0800591 fClip, path, paint,
592 *draw.fMatrix, nullptr,
593 draw.fClip->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000594}
595
596
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000597/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000598
robertphillips514450c2015-11-24 05:36:02 -0800599void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800600 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800601 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000602 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800603 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000604
robertphillips514450c2015-11-24 05:36:02 -0800605 // Presumably the path effect warps this to something other than an oval
606 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000607 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700608 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000609 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700610 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000611 return;
herb11a7f7f2015-11-24 12:41:00 -0800612 }
613
robertphillips514450c2015-11-24 05:36:02 -0800614 if (paint.getMaskFilter()) {
615 // The RRect path can handle special case blurring
616 SkRRect rr = SkRRect::MakeOval(oval);
617 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000618 }
619
620 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700621 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700622 return;
623 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000624
robertphillips514450c2015-11-24 05:36:02 -0800625 GrStrokeInfo strokeInfo(paint);
626 SkASSERT(!strokeInfo.isDashed());
627
robertphillips2e1e51f2015-10-15 08:01:48 -0700628 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000629}
630
631#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000632
633///////////////////////////////////////////////////////////////////////////////
634
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000635void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
636 const SkPaint& paint, const SkMatrix* prePathMatrix,
637 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800638 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800639 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
640 bool isClosed;
641 SkRect rect;
642 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
643 this->drawRect(draw, rect, paint);
644 return;
645 }
646 if (origSrcPath.isOval(&rect)) {
647 this->drawOval(draw, rect, paint);
648 return;
649 }
650 SkRRect rrect;
651 if (origSrcPath.isRRect(&rrect)) {
652 this->drawRRect(draw, rrect, paint);
653 return;
654 }
655 }
656
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000657 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800658 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800659 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660
robertphillips7bceedc2015-12-01 12:51:26 -0800661 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsccb1b572015-05-27 11:02:55 -0700662 fClip, origSrcPath, paint,
663 *draw.fMatrix, prePathMatrix,
664 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000665}
666
667static const int kBmpSmallTileSize = 1 << 10;
668
669static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
670 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
671 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
672 return tilesX * tilesY;
673}
674
reed85d91782015-09-10 14:33:38 -0700675static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000676 if (maxTileSize <= kBmpSmallTileSize) {
677 return maxTileSize;
678 }
679
680 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
681 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
682
683 maxTileTotalTileSize *= maxTileSize * maxTileSize;
684 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
685
686 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
687 return kBmpSmallTileSize;
688 } else {
689 return maxTileSize;
690 }
691}
692
693// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
694// pixels from the bitmap are necessary.
bsalomon74f681d2015-06-23 14:38:48 -0700695static void determine_clipped_src_rect(const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800696 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800697 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700698 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000699 const SkRect* srcRectPtr,
700 SkIRect* clippedSrcIRect) {
robertphillips7bceedc2015-12-01 12:51:26 -0800701 clip.getConservativeBounds(rt->width(), rt->height(), clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000702 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800703 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000704 clippedSrcIRect->setEmpty();
705 return;
706 }
707 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
708 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700709 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000710 // we've setup src space 0,0 to map to the top left of the src rect.
711 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000712 if (!clippedSrcRect.intersect(*srcRectPtr)) {
713 clippedSrcIRect->setEmpty();
714 return;
715 }
716 }
717 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700718 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000719 if (!clippedSrcIRect->intersect(bmpBounds)) {
720 clippedSrcIRect->setEmpty();
721 }
722}
723
reed85d91782015-09-10 14:33:38 -0700724bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
725 const SkMatrix& viewMatrix,
726 const GrTextureParams& params,
727 const SkRect* srcRectPtr,
728 int maxTileSize,
729 int* tileSize,
730 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800731 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700732 // if it's larger than the max tile size, then we have no choice but tiling.
733 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
734 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(),
735 srcRectPtr, clippedSubset);
736 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
737 return true;
738 }
739
bsalomon1a1d0b82015-10-16 07:49:42 -0700740 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700741 const size_t area = imageRect.width() * imageRect.height();
742 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
743 return false;
744 }
745
reed85d91782015-09-10 14:33:38 -0700746 // At this point we know we could do the draw by uploading the entire bitmap
747 // as a texture. However, if the texture would be large compared to the
748 // cache size and we don't require most of it for this draw then tile to
749 // reduce the amount of upload and cache spill.
750
751 // assumption here is that sw bitmap size is a good proxy for its size as
752 // a texture
753 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
754 size_t cacheSize;
755 fContext->getResourceCacheLimits(nullptr, &cacheSize);
756 if (bmpSize < cacheSize / 2) {
757 return false;
758 }
759
bsalomon1a1d0b82015-10-16 07:49:42 -0700760 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
761 // tiling memory savings would be < 50%.
reed85d91782015-09-10 14:33:38 -0700762 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(), srcRectPtr,
763 clippedSubset);
764 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
765 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
766 kBmpSmallTileSize * kBmpSmallTileSize;
767
768 return usedTileBytes < 2 * bmpSize;
769}
770
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000771bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800772 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773 const GrTextureParams& params,
774 const SkRect* srcRectPtr,
775 int maxTileSize,
776 int* tileSize,
777 SkIRect* clippedSrcRect) const {
joshualittce894002016-01-11 13:29:31 -0800778 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000779 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700780 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000781 return false;
782 }
783
reed85d91782015-09-10 14:33:38 -0700784 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
785 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
786}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000787
reed85d91782015-09-10 14:33:38 -0700788bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
789 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
790 const SkMatrix& viewMatrix) const {
joshualittce894002016-01-11 13:29:31 -0800791 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700792 // if image is explictly texture backed then just use the texture
793 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000794 return false;
795 }
796
reed85d91782015-09-10 14:33:38 -0700797 GrTextureParams params;
798 bool doBicubic;
799 GrTextureParams::FilterMode textureFilterMode =
800 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
801
802 int tileFilterPad;
803 if (doBicubic) {
804 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
805 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
806 tileFilterPad = 0;
807 } else {
808 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000809 }
reed85d91782015-09-10 14:33:38 -0700810 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000811
bsalomon8c07b7a2015-11-02 11:36:52 -0800812 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000813
reed85d91782015-09-10 14:33:38 -0700814 // these are output, which we safely ignore, as we just want to know the predicate
815 int outTileSize;
816 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000817
reed85d91782015-09-10 14:33:38 -0700818 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
819 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000820}
821
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000822void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000823 const SkBitmap& bitmap,
824 const SkMatrix& m,
825 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800826 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800827 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800828 SkMatrix viewMatrix;
829 viewMatrix.setConcat(*origDraw.fMatrix, m);
830 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800831 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonb1b01992015-11-18 10:56:08 -0800832 // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
bsalomonf1ecd212015-12-09 17:06:02 -0800833 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
834 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800835 return;
836 }
bsalomonb1b01992015-11-18 10:56:08 -0800837 int maxTileSize = fContext->caps()->maxTileSize();
838
839 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
840 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
841 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
842 paint.isAntiAlias() &&
843 bitmap.width() <= maxTileSize &&
844 bitmap.height() <= maxTileSize;
845
846 bool skipTileCheck = drawAA || paint.getMaskFilter();
847
848 if (!skipTileCheck) {
849 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
850 int tileSize;
851 SkIRect clippedSrcRect;
852
853 GrTextureParams params;
854 bool doBicubic;
855 GrTextureParams::FilterMode textureFilterMode =
856 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
857 &doBicubic);
858
859 int tileFilterPad;
860
861 if (doBicubic) {
862 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
863 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
864 tileFilterPad = 0;
865 } else {
866 tileFilterPad = 1;
867 }
868 params.setFilterMode(textureFilterMode);
869
870 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
871 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
872 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
873 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
874 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
875 return;
876 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000877 }
bsalomonb1b01992015-11-18 10:56:08 -0800878 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800879 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
880 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000881}
882
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000883// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000884// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
885// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000886static inline void clamped_outset_with_offset(SkIRect* iRect,
887 int outset,
888 SkPoint* offset,
889 const SkIRect& clamp) {
890 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000891
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000892 int leftClampDelta = clamp.fLeft - iRect->fLeft;
893 if (leftClampDelta > 0) {
894 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000895 iRect->fLeft = clamp.fLeft;
896 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000897 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000898 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000899
900 int topClampDelta = clamp.fTop - iRect->fTop;
901 if (topClampDelta > 0) {
902 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000903 iRect->fTop = clamp.fTop;
904 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000905 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000906 }
907
908 if (iRect->fRight > clamp.fRight) {
909 iRect->fRight = clamp.fRight;
910 }
911 if (iRect->fBottom > clamp.fBottom) {
912 iRect->fBottom = clamp.fBottom;
913 }
914}
915
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000916// Break 'bitmap' into several tiles to draw it since it has already
917// been determined to be too large to fit in VRAM
918void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800919 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000920 const SkRect& srcRect,
921 const SkIRect& clippedSrcIRect,
922 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800923 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700924 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000925 int tileSize,
926 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800927 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800928
929 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entry.
930 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
931
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000932 // The following pixel lock is technically redundant, but it is desirable
933 // to lock outside of the tile loop to prevent redecoding the whole image
934 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
935 // is larger than the limit of the discardable memory pool.
936 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800937
938 const SkPaint* paint = &origPaint;
939 SkPaint tempPaint;
940 if (origPaint.isAntiAlias() && !fRenderTarget->isUnifiedMultisampled()) {
941 // Drop antialiasing to avoid seams at tile boundaries.
942 tempPaint = origPaint;
943 tempPaint.setAntiAlias(false);
944 paint = &tempPaint;
945 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000946 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
947
948 int nx = bitmap.width() / tileSize;
949 int ny = bitmap.height() / tileSize;
950 for (int x = 0; x <= nx; x++) {
951 for (int y = 0; y <= ny; y++) {
952 SkRect tileR;
953 tileR.set(SkIntToScalar(x * tileSize),
954 SkIntToScalar(y * tileSize),
955 SkIntToScalar((x + 1) * tileSize),
956 SkIntToScalar((y + 1) * tileSize));
957
958 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
959 continue;
960 }
961
962 if (!tileR.intersect(srcRect)) {
963 continue;
964 }
965
966 SkBitmap tmpB;
967 SkIRect iTileR;
968 tileR.roundOut(&iTileR);
969 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
970 SkIntToScalar(iTileR.fTop));
971
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000972 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -0800973 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000974 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000975 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -0800976 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000977
robertphillipsec8bb942014-11-21 10:16:25 -0800978 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000979 SkIRect iClampRect;
980
reeda5517e22015-07-14 10:54:12 -0700981 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982 // In bleed mode we want to always expand the tile on all edges
983 // but stay within the bitmap bounds
984 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
985 } else {
986 // In texture-domain/clamp mode we only want to expand the
987 // tile on edges interior to "srcRect" (i.e., we want to
988 // not bleed across the original clamped edges)
989 srcRect.roundOut(&iClampRect);
990 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000991 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
992 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000993 }
994
995 if (bitmap.extractSubset(&tmpB, iTileR)) {
996 // now offset it to make it "local" to our tmp bitmap
997 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000998 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -0800999 // de-optimized this determination
1000 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001001 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001002 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001003 tileR,
1004 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -08001005 *paint,
reeda5517e22015-07-14 10:54:12 -07001006 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001007 bicubic,
1008 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001009 }
1010 }
1011 }
1012}
1013
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001014/*
1015 * This is called by drawBitmap(), which has to handle images that may be too
1016 * large to be represented by a single texture.
1017 *
1018 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1019 * and that non-texture portion of the GrPaint has already been setup.
1020 */
1021void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001022 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001023 const SkRect& srcRect,
1024 const GrTextureParams& params,
1025 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001026 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001027 bool bicubic,
1028 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001029 // We should have already handled bitmaps larger than the max texture size.
1030 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1031 bitmap.height() <= fContext->caps()->maxTextureSize());
1032 // Unless the bitmap is inherently texture-backed, we should be respecting the max tile size
1033 // by the time we get here.
1034 SkASSERT(bitmap.getTexture() ||
1035 (bitmap.width() <= fContext->caps()->maxTileSize() &&
1036 bitmap.height() <= fContext->caps()->maxTileSize()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001037
1038 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001039 AutoBitmapTexture abt(fContext, bitmap, params, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001040 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001041 return;
1042 }
1043
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001044 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045 SkRect paintRect;
1046 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1047 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1048 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1049 SkScalarMul(srcRect.fTop, hInv),
1050 SkScalarMul(srcRect.fRight, wInv),
1051 SkScalarMul(srcRect.fBottom, hInv));
1052
egdaniel79da63f2015-10-09 10:55:16 -07001053 SkMatrix texMatrix;
1054 texMatrix.reset();
1055 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1056 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1057 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1058 // Thus we need to pass in the transform matrix directly to the texture processor used for
1059 // the bitmap draw.
1060 texMatrix.setScale(wInv, hInv);
1061 }
1062
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001063 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001064
1065 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1066 // the rest from the SkPaint.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001067 SkAutoTUnref<const GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001068
reeda5517e22015-07-14 10:54:12 -07001069 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001070 // Use a constrained texture domain to avoid color bleeding
1071 SkScalar left, top, right, bottom;
1072 if (srcRect.width() > SK_Scalar1) {
1073 SkScalar border = SK_ScalarHalf / texture->width();
1074 left = paintRect.left() + border;
1075 right = paintRect.right() - border;
1076 } else {
1077 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1078 }
1079 if (srcRect.height() > SK_Scalar1) {
1080 SkScalar border = SK_ScalarHalf / texture->height();
1081 top = paintRect.top() + border;
1082 bottom = paintRect.bottom() - border;
1083 } else {
1084 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1085 }
1086 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001087 if (bicubic) {
egdaniel79da63f2015-10-09 10:55:16 -07001088 fp.reset(GrBicubicEffect::Create(texture, texMatrix, textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001089 } else {
bsalomon4a339522015-10-06 08:40:50 -07001090 fp.reset(GrTextureDomainEffect::Create(texture,
egdaniel79da63f2015-10-09 10:55:16 -07001091 texMatrix,
joshualitt5531d512014-12-17 15:50:11 -08001092 textureDomain,
1093 GrTextureDomain::kClamp_Mode,
1094 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001095 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001096 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001097 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1098 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
egdaniel79da63f2015-10-09 10:55:16 -07001099 fp.reset(GrBicubicEffect::Create(texture, texMatrix, tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001100 } else {
egdaniel79da63f2015-10-09 10:55:16 -07001101 fp.reset(GrSimpleTextureEffect::Create(texture, texMatrix, params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001102 }
1103
joshualitt33a5fce2015-11-18 13:28:51 -08001104 GrPaint grPaint;
1105 if (!SkPaintToGrPaintWithTexture(this->context(), paint, viewMatrix, fp,
1106 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001107 return;
1108 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001109
egdaniel79da63f2015-10-09 10:55:16 -07001110 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1111 // We don't have local coords in this case and have previously set the transform
1112 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001113 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001114 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001115 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001116 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001117}
1118
fmalita2d97bc12014-11-20 10:44:58 -08001119bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001120 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001121 const SkImageFilter* filter,
1122 const SkImageFilter::Context& ctx,
1123 SkBitmap* result, SkIPoint* offset) {
joshualittce894002016-01-11 13:29:31 -08001124 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001125 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001126
reed88d064d2015-10-12 11:30:02 -07001127 SkImageFilter::DeviceProxy proxy(this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001128
1129 if (filter->canFilterImageGPU()) {
robertphillips1de87df2016-01-14 06:03:29 -08001130 SkBitmap bm;
1131 GrWrapTextureInBitmap(texture, width, height, false, &bm);
robertphillips48e78462016-02-17 13:57:16 -08001132 return filter->filterImageGPUDeprecated(&proxy, bm, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001133 } else {
1134 return false;
1135 }
1136}
1137
1138void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1139 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001140 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001141 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001142 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001143
1144 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1145 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1146 return;
1147 }
1148
1149 int w = bitmap.width();
1150 int h = bitmap.height();
1151
1152 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001153 // draw sprite neither filters nor tiles.
1154 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001155 if (!texture) {
1156 return;
1157 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001158
bsalomonf1b7a1d2015-09-28 06:26:28 -07001159 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
1160
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001161 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001162 // This bitmap will own the filtered result as a texture.
1163 SkBitmap filteredBitmap;
1164
bsalomon49f085d2014-09-05 13:34:00 -07001165 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001166 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167 SkMatrix matrix(*draw.fMatrix);
1168 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblancodb64af32015-12-09 10:11:43 -08001169 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-left, -top);
senorblancobe129b22014-08-08 07:14:35 -07001170 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001171 // This cache is transient, and is freed (along with all its contained
1172 // textures) when it goes out of scope.
reed4e23cda2016-01-11 10:56:59 -08001173 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001174 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001175 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001176 texture = (GrTexture*) filteredBitmap.getTexture();
1177 w = filteredBitmap.width();
1178 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001179 left += offset.x();
1180 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001181 } else {
1182 return;
1183 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001184 SkASSERT(!GrPixelConfigIsAlphaOnly(texture->config()));
1185 alphaOnly = false;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001186 }
1187
1188 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001189 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001190 GrSimpleTextureEffect::Create(texture, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001191 if (alphaOnly) {
1192 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1193 } else {
1194 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1195 }
1196 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001197 return;
1198 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001199
bsalomona2e69fc2015-11-05 10:41:43 -08001200 fDrawContext->fillRectToRect(fClip,
1201 grPaint,
1202 SkMatrix::I(),
1203 SkRect::MakeXYWH(SkIntToScalar(left),
1204 SkIntToScalar(top),
1205 SkIntToScalar(w),
1206 SkIntToScalar(h)),
1207 SkRect::MakeXYWH(0,
1208 0,
1209 SK_Scalar1 * w / texture->width(),
1210 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001211}
1212
bsalomonb1b01992015-11-18 10:56:08 -08001213void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1214 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001215 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001216 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001217 CHECK_SHOULD_DRAW(draw);
bsalomonb1b01992015-11-18 10:56:08 -08001218 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001219 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001220 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
1221 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001222 return;
1223 }
bsalomonb1b01992015-11-18 10:56:08 -08001224 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1225 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1226 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1227 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1228 const SkRect* dst = &origDst;
1229 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001230 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001231 if (!src) {
1232 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001233 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001234
bsalomonb1b01992015-11-18 10:56:08 -08001235 SkMatrix srcToDstMatrix;
1236 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1237 return;
1238 }
1239 SkRect tmpSrc, tmpDst;
1240 if (src != &bmpBounds) {
1241 if (!bmpBounds.contains(*src)) {
1242 tmpSrc = *src;
1243 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001244 return; // nothing to draw
1245 }
bsalomonb1b01992015-11-18 10:56:08 -08001246 src = &tmpSrc;
1247 srcToDstMatrix.mapRect(&tmpDst, *src);
1248 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001249 }
1250 }
1251
bsalomonb1b01992015-11-18 10:56:08 -08001252 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001253
bsalomonb1b01992015-11-18 10:56:08 -08001254 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1255 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
1256 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
1257 paint.isAntiAlias() &&
1258 bitmap.width() <= maxTileSize &&
1259 bitmap.height() <= maxTileSize;
1260
1261 bool skipTileCheck = drawAA || paint.getMaskFilter();
1262
1263 if (!skipTileCheck) {
1264 int tileSize;
1265 SkIRect clippedSrcRect;
1266
1267 GrTextureParams params;
1268 bool doBicubic;
1269 GrTextureParams::FilterMode textureFilterMode =
1270 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1271 &doBicubic);
1272
1273 int tileFilterPad;
1274
1275 if (doBicubic) {
1276 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1277 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1278 tileFilterPad = 0;
1279 } else {
1280 tileFilterPad = 1;
1281 }
1282 params.setFilterMode(textureFilterMode);
1283
1284 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1285 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1286 // we have a mask filter.
1287 SkMatrix viewMatrix = *draw.fMatrix;
1288 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1289 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1290 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1291 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1292 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1293 constraint, tileSize, doBicubic);
1294 return;
1295 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001296 }
bsalomonb1b01992015-11-18 10:56:08 -08001297 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001298 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001299}
1300
1301void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1302 int x, int y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001303 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001304 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001305 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001306 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001307
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001308 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001309 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001310
1311 GrRenderTarget* devRT = dev->accessRenderTarget();
1312 GrTexture* devTex;
halcanary96fcdcc2015-08-27 07:41:13 -07001313 if (nullptr == (devTex = devRT->asTexture())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001314 return;
1315 }
1316
robertphillips7b9e8a42014-12-11 08:20:31 -08001317 const SkImageInfo ii = dev->imageInfo();
1318 int w = ii.width();
1319 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001320
1321 SkImageFilter* filter = paint.getImageFilter();
1322 // This bitmap will own the filtered result as a texture.
1323 SkBitmap filteredBitmap;
1324
bsalomon49f085d2014-09-05 13:34:00 -07001325 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001326 SkIPoint offset = SkIPoint::Make(0, 0);
1327 SkMatrix matrix(*draw.fMatrix);
1328 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblancodb64af32015-12-09 10:11:43 -08001329 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-x, -y);
senorblanco55b6d8b2014-07-30 11:26:46 -07001330 // This cache is transient, and is freed (along with all its contained
1331 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001332 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
reed4e23cda2016-01-11 10:56:59 -08001333 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001334 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1335 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001336 devTex = filteredBitmap.getTexture();
1337 w = filteredBitmap.width();
1338 h = filteredBitmap.height();
1339 x += offset.fX;
1340 y += offset.fY;
1341 } else {
1342 return;
1343 }
1344 }
1345
1346 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001347 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001348 GrSimpleTextureEffect::Create(devTex, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001349 if (GrPixelConfigIsAlphaOnly(devTex->config())) {
1350 // Can this happen?
1351 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1352 } else {
1353 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1354 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001355
bsalomonf1b7a1d2015-09-28 06:26:28 -07001356 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001357 return;
1358 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001359
1360 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1361 SkIntToScalar(y),
1362 SkIntToScalar(w),
1363 SkIntToScalar(h));
1364
1365 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1366 // scratch texture).
1367 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1368 SK_Scalar1 * h / devTex->height());
1369
bsalomona2e69fc2015-11-05 10:41:43 -08001370 fDrawContext->fillRectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001371}
1372
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001373bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
joshualittce894002016-01-11 13:29:31 -08001374 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001375 return filter->canFilterImageGPU();
1376}
1377
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001378bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001379 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001380 SkBitmap* result, SkIPoint* offset) {
joshualittce894002016-01-11 13:29:31 -08001381 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001382 // want explicitly our impl, so guard against a subclass of us overriding it
1383 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1384 return false;
1385 }
1386
1387 SkAutoLockPixels alp(src, !src.getTexture());
1388 if (!src.getTexture() && !src.readyToDraw()) {
1389 return false;
1390 }
1391
1392 GrTexture* texture;
1393 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1394 // must be pushed upstack.
bsalomonafa95e22015-10-12 10:39:46 -07001395 AutoBitmapTexture abt(fContext, src, GrTextureParams::ClampNoFilter(), &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001396 if (!texture) {
1397 return false;
1398 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001399
senorblancod0d37ca2015-04-02 04:54:56 -07001400 return this->filterTexture(fContext, texture, src.width(), src.height(),
1401 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001402}
1403
reeda85d4d02015-05-06 12:56:48 -07001404void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1405 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001406 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001407 SkMatrix viewMatrix = *draw.fMatrix;
1408 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001409 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001410 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001411 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001412 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1413 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001414 return;
reed85d91782015-09-10 14:33:38 -07001415 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001416 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001417 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1418 paint.getFilterQuality(), *draw.fMatrix)) {
1419 // only support tiling as bitmap at the moment, so force raster-version
1420 if (!as_IB(image)->getROPixels(&bm)) {
1421 return;
1422 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001423 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1424 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1425 CHECK_SHOULD_DRAW(draw);
1426 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001427 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1428 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001429 } else if (as_IB(image)->getROPixels(&bm)) {
1430 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001431 }
reeda85d4d02015-05-06 12:56:48 -07001432 }
1433}
1434
1435void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001436 const SkRect& dst, const SkPaint& paint,
1437 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001438 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001439 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001440 CHECK_SHOULD_DRAW(draw);
1441 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001442 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001443 return;
1444 }
1445 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001446 SkMatrix totalMatrix = *draw.fMatrix;
1447 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1448 dst.height() / (src ? src->height() : image->height()));
1449 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001450 // only support tiling as bitmap at the moment, so force raster-version
1451 if (!as_IB(image)->getROPixels(&bm)) {
1452 return;
1453 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001454 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1455 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1456 CHECK_SHOULD_DRAW(draw);
1457 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001458 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001459 } else if (as_IB(image)->getROPixels(&bm)) {
1460 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001461 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001462}
1463
bsalomonf1ecd212015-12-09 17:06:02 -08001464void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001465 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001466 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001467
1468 CHECK_FOR_ANNOTATION(paint);
1469 CHECK_SHOULD_DRAW(draw);
1470
joshualittedb36442015-11-19 14:29:30 -08001471 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
1472 fRenderTarget->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001473 bool doBicubic;
1474 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001475 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1476 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001477 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001478 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001479
1480 SkRect srcR, dstR;
1481 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001482 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001483 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001484 }
1485 return;
1486 }
1487
bsalomon2bbd0c62015-12-09 12:50:56 -08001488 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
1489 SkAutoTUnref<const GrFragmentProcessor> fp(
1490 producer->createFragmentProcessor(SkMatrix::I(),
1491 SkRect::MakeIWH(producer->width(), producer->height()),
1492 GrTextureProducer::kNo_FilterConstraint, true,
1493 &kMode));
joshualitt33a5fce2015-11-18 13:28:51 -08001494 GrPaint grPaint;
1495 if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, fp,
bsalomonf1ecd212015-12-09 17:06:02 -08001496 producer->isAlphaOnly(), &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001497 return;
1498 }
1499
bsalomon2bbd0c62015-12-09 12:50:56 -08001500 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1501 producer->height(), center, dst);
1502}
1503
1504void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1505 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001506 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001507 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001508 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001509 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001510 } else {
1511 SkBitmap bm;
1512 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1513 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001514 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001515 } else if (as_IB(image)->getROPixels(&bm)) {
1516 this->drawBitmapNine(draw, bm, center, dst, paint);
1517 }
1518 }
1519}
1520
1521void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1522 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001523 ASSERT_SINGLE_OWNER
bsalomon2bbd0c62015-12-09 12:50:56 -08001524 if (bitmap.getTexture()) {
1525 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001526 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001527 } else {
1528 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001529 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001530 }
joshualitt33a5fce2015-11-18 13:28:51 -08001531}
1532
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001533///////////////////////////////////////////////////////////////////////////////
1534
1535// must be in SkCanvas::VertexMode order
1536static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1537 kTriangles_GrPrimitiveType,
1538 kTriangleStrip_GrPrimitiveType,
1539 kTriangleFan_GrPrimitiveType,
1540};
1541
1542void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1543 int vertexCount, const SkPoint vertices[],
1544 const SkPoint texs[], const SkColor colors[],
1545 SkXfermode* xmode,
1546 const uint16_t indices[], int indexCount,
1547 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001548 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001549 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001550 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001551
halcanary96fcdcc2015-08-27 07:41:13 -07001552 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1553 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001554
halcanary96fcdcc2015-08-27 07:41:13 -07001555 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001556
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001557 SkPaint copy(paint);
1558 copy.setStyle(SkPaint::kStroke_Style);
1559 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001560
bsalomonf1b7a1d2015-09-28 06:26:28 -07001561 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001562 // we ignore the shader if texs is null.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001563 if (!SkPaintToGrPaintNoShader(this->context(), copy, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001564 return;
1565 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001566
dandov32a311b2014-07-15 19:46:26 -07001567 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001568 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001569 switch (vmode) {
1570 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001571 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001572 break;
1573 case SkCanvas::kTriangleStrip_VertexMode:
1574 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001575 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001576 break;
1577 }
mtklein533eb782014-08-27 10:39:42 -07001578
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001579 VertState state(vertexCount, indices, indexCount);
1580 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001581
dandov32a311b2014-07-15 19:46:26 -07001582 //number of indices for lines per triangle with kLines
1583 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001584
bsalomonf1b7a1d2015-09-28 06:26:28 -07001585 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001586 int i = 0;
1587 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001588 lineIndices[i] = state.f0;
1589 lineIndices[i + 1] = state.f1;
1590 lineIndices[i + 2] = state.f1;
1591 lineIndices[i + 3] = state.f2;
1592 lineIndices[i + 4] = state.f2;
1593 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001594 i += 6;
1595 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001596 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001597 grPaint,
1598 *draw.fMatrix,
1599 kLines_GrPrimitiveType,
1600 vertexCount,
1601 vertices,
1602 texs,
1603 colors,
1604 lineIndices.get(),
1605 indexCount);
1606 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001607 }
1608
bsalomonf1b7a1d2015-09-28 06:26:28 -07001609 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001610
1611 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001612 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001613 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1614 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001615 convertedColors.reset(vertexCount);
1616 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001617 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001618 }
1619 colors = convertedColors.get();
1620 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001621 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001622 if (texs && paint.getShader()) {
1623 if (colors) {
1624 // When there are texs and colors the shader and colors are combined using xmode. A null
1625 // xmode is defined to mean modulate.
1626 SkXfermode::Mode colorMode;
1627 if (xmode) {
1628 if (!xmode->asMode(&colorMode)) {
1629 return;
1630 }
1631 } else {
1632 colorMode = SkXfermode::kModulate_Mode;
1633 }
1634 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
1635 false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001636 return;
1637 }
1638 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001639 // We have a shader, but no colors to blend it against.
1640 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
1641 return;
1642 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001643 }
bsalomonaa48d362015-10-01 08:34:17 -07001644 } else {
1645 if (colors) {
1646 // We have colors, but either have no shader or no texture coords (which implies that
1647 // we should ignore the shader).
1648 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint, &grPaint)) {
1649 return;
1650 }
1651 } else {
1652 // No colors and no shaders. Just draw with the paint color.
1653 if (!SkPaintToGrPaintNoShader(this->context(), paint, &grPaint)) {
1654 return;
1655 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001656 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001657 }
1658
robertphillips2e1e51f2015-10-15 08:01:48 -07001659 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001660 grPaint,
1661 *draw.fMatrix,
1662 primType,
1663 vertexCount,
1664 vertices,
1665 texs,
1666 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001667 indices,
robertphillipsea461502015-05-26 11:38:03 -07001668 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001669}
1670
1671///////////////////////////////////////////////////////////////////////////////
1672
jvanverth31ff7622015-08-07 10:09:28 -07001673void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001674 const SkRect texRect[], const SkColor colors[], int count,
1675 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001676 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001677 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001678 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001679 return;
1680 }
1681
jvanverth31ff7622015-08-07 10:09:28 -07001682 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001683 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001684
reedca109532015-06-25 16:25:25 -07001685 SkPaint p(paint);
1686 p.setShader(atlas->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
1687
jvanverth31ff7622015-08-07 10:09:28 -07001688 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001689 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001690 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
1691 &grPaint)) {
1692 return;
1693 }
1694 } else {
1695 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix, &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001696 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001697 }
1698 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001699
1700 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001701 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001702}
1703
1704///////////////////////////////////////////////////////////////////////////////
1705
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001706void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001707 size_t byteLength, SkScalar x, SkScalar y,
1708 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001709 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001710 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001711 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001712
jvanverth8c27a182014-10-14 08:45:50 -07001713 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001714 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001715 return;
1716 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001717
jvanverth8c27a182014-10-14 08:45:50 -07001718 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001719
robertphillips2e1e51f2015-10-15 08:01:48 -07001720 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001721 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001722}
1723
fmalita05c4a432014-09-29 06:29:53 -07001724void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1725 const SkScalar pos[], int scalarsPerPos,
1726 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001727 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001728 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001729 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001730
jvanverth8c27a182014-10-14 08:45:50 -07001731 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001732 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001733 return;
1734 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001735
jvanverth8c27a182014-10-14 08:45:50 -07001736 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001737
robertphillips2e1e51f2015-10-15 08:01:48 -07001738 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001739 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1740 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001741}
1742
joshualitt9c328182015-03-23 08:13:04 -07001743void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1744 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001745 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001746 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001747 CHECK_SHOULD_DRAW(draw);
1748
1749 SkDEBUGCODE(this->validate();)
1750
robertphillips2e1e51f2015-10-15 08:01:48 -07001751 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001752 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001753}
1754
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001755///////////////////////////////////////////////////////////////////////////////
1756
reedb2db8982014-11-13 12:41:02 -08001757bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001758 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001759}
1760
1761void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001762 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001763
1764 // Clear batch debugging output
1765 // TODO not exactly sure where this should live
1766 if (GR_BATCH_DEBUGGING_OUTPUT) {
1767 SkDebugf("%s\n", fContext->getAuditTrail()->toJson().c_str());
1768 // TODO This currently crashes because not all ops are accounted for
1769 GR_AUDIT_TRAIL_RESET(fContext->getAuditTrail());
1770 }
bsalomonc49e8682015-06-30 11:37:35 -07001771 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001772}
1773
1774///////////////////////////////////////////////////////////////////////////////
1775
reed76033be2015-03-14 10:54:31 -07001776SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001777 ASSERT_SINGLE_OWNER
bsalomonf2703d82014-10-28 14:33:06 -07001778 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001779 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001780 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001781 desc.fWidth = cinfo.fInfo.width();
1782 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001783 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001784
1785 SkAutoTUnref<GrTexture> texture;
1786 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001787 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001788
hcm4396fa52014-10-27 21:43:30 -07001789 // layers are never draw in repeat modes, so we can request an approx
1790 // match and ignore any padding.
bsalomoneae62002015-07-31 13:59:30 -07001791 if (kNever_TileUsage == cinfo.fTileUsage) {
1792 texture.reset(fContext->textureProvider()->createApproxTexture(desc));
1793 } else {
1794 texture.reset(fContext->textureProvider()->createTexture(desc, true));
1795 }
bsalomonafe30052015-01-16 07:32:33 -08001796
1797 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001798 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001799 return SkGpuDevice::Create(
bsalomon74f681d2015-06-23 14:38:48 -07001800 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, init);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001801 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001802 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001803 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001804 cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary96fcdcc2015-08-27 07:41:13 -07001805 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001806 }
1807}
1808
reed4a8126e2014-09-22 07:29:03 -07001809SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001810 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001811 // TODO: Change the signature of newSurface to take a budgeted parameter.
1812 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
vbuzinovdded6962015-06-12 08:59:45 -07001813 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
bsalomonafe30052015-01-16 07:32:33 -08001814 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001815}
1816
robertphillips30d2cc62014-09-24 08:52:18 -07001817bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001818 const SkMatrix* matrix, const SkPaint* paint) {
joshualittce894002016-01-11 13:29:31 -08001819 ASSERT_SINGLE_OWNER
robertphillips63242d72014-12-04 08:31:02 -08001820#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001821 // todo: should handle this natively
1822 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001823 return false;
1824 }
1825
halcanary96fcdcc2015-08-27 07:41:13 -07001826 const SkBigPicture::AccelData* data = nullptr;
mtklein9db912c2015-05-19 11:11:26 -07001827 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1828 data = bp->accelData();
1829 }
robertphillips81f71b62014-11-11 04:54:49 -08001830 if (!data) {
1831 return false;
1832 }
1833
robertphillipse5524cd2015-02-20 12:30:26 -08001834 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1835 if (0 == gpuData->numBlocks()) {
1836 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001837 }
1838
robertphillipsfd61ed02014-10-28 07:21:44 -07001839 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001840
robertphillipse5524cd2015-02-20 12:30:26 -08001841 SkIRect iBounds;
1842 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1843 return false;
1844 }
1845
1846 SkRect clipBounds = SkRect::Make(iBounds);
1847
1848 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1849
robertphillips60029a52015-11-09 13:51:06 -08001850 GrLayerHoister::Begin(fContext);
1851
robertphillipsfd61ed02014-10-28 07:21:44 -07001852 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001853 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001854 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001855 &atlasedNeedRendering, &atlasedRecycled,
vbuzinovdded6962015-06-12 08:59:45 -07001856 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001857
1858 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1859
1860 SkTDArray<GrHoistedLayer> needRendering, recycled;
1861
robertphillipse5524cd2015-02-20 12:30:26 -08001862 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1863
robertphillipsfd61ed02014-10-28 07:21:44 -07001864 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001865 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001866 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001867 &needRendering, &recycled,
vbuzinovdded6962015-06-12 08:59:45 -07001868 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001869
1870 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001871
robertphillips64bf7672014-08-21 13:07:35 -07001872 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001873 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
halcanary96fcdcc2015-08-27 07:41:13 -07001874 initialMatrix, nullptr);
robertphillips64bf7672014-08-21 13:07:35 -07001875
robertphillipsfd61ed02014-10-28 07:21:44 -07001876 GrLayerHoister::UnlockLayers(fContext, needRendering);
1877 GrLayerHoister::UnlockLayers(fContext, recycled);
1878 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1879 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips60029a52015-11-09 13:51:06 -08001880 GrLayerHoister::End(fContext);
robertphillips64bf7672014-08-21 13:07:35 -07001881
1882 return true;
robertphillips63242d72014-12-04 08:31:02 -08001883#else
1884 return false;
1885#endif
robertphillips64bf7672014-08-21 13:07:35 -07001886}
1887
reed13ccbf82015-10-20 09:56:52 -07001888SkImageFilter::Cache* SkGpuDevice::NewImageFilterCache() {
1889 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
1890}
1891
senorblancobe129b22014-08-08 07:14:35 -07001892SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001893 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001894 // We always return a transient cache, so it is freed after each
1895 // filter traversal.
reed13ccbf82015-10-20 09:56:52 -07001896 return SkGpuDevice::NewImageFilterCache();
senorblanco55b6d8b2014-07-30 11:26:46 -07001897}
reedf037e0b2014-10-30 11:34:15 -07001898
1899#endif