blob: 9bc60c20364a99c95271843fc0bc3f720574eae1 [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
bsalomonbcf0a522014-10-08 08:40:09 -070071// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
72// just accesses the backing GrTexture. Otherwise, it creates a cached texture
73// representation and releases it in the destructor.
74class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040075public:
bsalomonbcf0a522014-10-08 08:40:09 -070076 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070077
bsalomonbcf0a522014-10-08 08:40:09 -070078 AutoBitmapTexture(GrContext* context,
79 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -070080 const GrTextureParams& params,
bsalomonbcf0a522014-10-08 08:40:09 -070081 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040082 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070083 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040084 }
85
bsalomonbcf0a522014-10-08 08:40:09 -070086 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040087 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -070088 const GrTextureParams& params) {
bsalomonbcf0a522014-10-08 08:40:09 -070089 // Either get the texture directly from the bitmap, or else use the cache and
90 // remember to unref it.
91 if (GrTexture* bmpTexture = bitmap.getTexture()) {
halcanary96fcdcc2015-08-27 07:41:13 -070092 fTexture.reset(nullptr);
bsalomonbcf0a522014-10-08 08:40:09 -070093 return bmpTexture;
94 } else {
95 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
96 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -040097 }
Brian Salomon9323b8b2014-10-07 15:07:38 -040098 }
99
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000100private:
bsalomonbcf0a522014-10-08 08:40:09 -0700101 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000102};
103
104///////////////////////////////////////////////////////////////////////////////
105
bsalomon74f681d2015-06-23 14:38:48 -0700106/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
107 should fail. */
108bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
109 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
110 *flags = 0;
111 if (info) {
112 switch (info->alphaType()) {
113 case kPremul_SkAlphaType:
114 break;
115 case kOpaque_SkAlphaType:
116 *flags |= SkGpuDevice::kIsOpaque_Flag;
117 break;
118 default: // If it is unpremul or unknown don't try to render
119 return false;
120 }
121 }
122 if (kClear_InitContents == init) {
123 *flags |= kNeedClear_Flag;
124 }
125 return true;
126}
127
128SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props,
129 InitContents init) {
130 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, init);
senorblancod0d37ca2015-04-02 04:54:56 -0700131}
132
133SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
bsalomon74f681d2015-06-23 14:38:48 -0700134 const SkSurfaceProps* props, InitContents init) {
bsalomonafe30052015-01-16 07:32:33 -0800135 if (!rt || rt->wasDestroyed()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700136 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000137 }
bsalomon74f681d2015-06-23 14:38:48 -0700138 unsigned flags;
halcanary96fcdcc2015-08-27 07:41:13 -0700139 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
140 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700141 }
halcanary385fe4d2015-08-26 13:07:48 -0700142 return new SkGpuDevice(rt, width, height, props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000143}
144
bsalomon5ec26ae2016-02-25 08:33:02 -0800145SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkBudgeted budgeted,
bsalomon74f681d2015-06-23 14:38:48 -0700146 const SkImageInfo& info, int sampleCount,
erikchen9a1ed5d2016-02-10 16:32:34 -0800147 const SkSurfaceProps* props, InitContents init,
148 GrTextureStorageAllocator customAllocator) {
bsalomon74f681d2015-06-23 14:38:48 -0700149 unsigned flags;
150 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700151 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700152 }
153
erikchen9a1ed5d2016-02-10 16:32:34 -0800154 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(
155 context, budgeted, info, sampleCount, customAllocator));
halcanary96fcdcc2015-08-27 07:41:13 -0700156 if (nullptr == rt) {
157 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700158 }
159
halcanary385fe4d2015-08-26 13:07:48 -0700160 return new SkGpuDevice(rt, info.width(), info.height(), props, flags);
bsalomon74f681d2015-06-23 14:38:48 -0700161}
162
senorblancod0d37ca2015-04-02 04:54:56 -0700163SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
164 const SkSurfaceProps* props, unsigned flags)
robertphillipsfcf78292015-06-19 11:49:52 -0700165 : INHERITED(SkSurfacePropsCopyOrDefault(props))
joshualitt06dd0a82016-01-08 06:32:31 -0800166 , fContext(SkRef(rt->getContext()))
167 , fRenderTarget(SkRef(rt)) {
bsalomon74f681d2015-06-23 14:38:48 -0700168 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000169
bsalomon74f681d2015-06-23 14:38:48 -0700170 SkAlphaType at = fOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
171 SkImageInfo info = rt->surfacePriv().info(at).makeWH(width, height);
halcanary385fe4d2015-08-26 13:07:48 -0700172 SkPixelRef* pr = new SkGrPixelRef(info, rt);
bsalomonafbf2d62014-09-30 12:18:44 -0700173 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700174 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700175
robertphillips77a2e522015-10-17 07:43:27 -0700176 fDrawContext.reset(this->context()->drawContext(rt, &this->surfaceProps()));
bsalomone63ffef2016-02-05 07:17:34 -0800177 if (flags & kNeedClear_Flag) {
178 this->clearAll();
179 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000180}
181
erikchen9a1ed5d2016-02-10 16:32:34 -0800182GrRenderTarget* SkGpuDevice::CreateRenderTarget(
bsalomon5ec26ae2016-02-25 08:33:02 -0800183 GrContext* context, SkBudgeted budgeted, const SkImageInfo& origInfo,
erikchen9a1ed5d2016-02-10 16:32:34 -0800184 int sampleCount, GrTextureStorageAllocator textureStorageAllocator) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000185 if (kUnknown_SkColorType == origInfo.colorType() ||
186 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700187 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000188 }
189
bsalomonafe30052015-01-16 07:32:33 -0800190 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700191 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800192 }
193
reede5ea5002014-09-03 11:54:58 -0700194 SkColorType ct = origInfo.colorType();
195 SkAlphaType at = origInfo.alphaType();
brianosmana6359362016-03-21 06:55:37 -0700196 SkColorProfileType pt = origInfo.profileType();
reede5ea5002014-09-03 11:54:58 -0700197 if (kRGB_565_SkColorType == ct) {
198 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800199 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
200 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700201 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800202 }
203 if (kOpaque_SkAlphaType != at) {
204 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000205 }
brianosmana6359362016-03-21 06:55:37 -0700206 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at, pt);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000207
bsalomonf2703d82014-10-28 14:33:06 -0700208 GrSurfaceDesc desc;
209 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000210 desc.fWidth = info.width();
211 desc.fHeight = info.height();
brianosmana6359362016-03-21 06:55:37 -0700212 desc.fConfig = SkImageInfo2GrPixelConfig(info, *context->caps());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000213 desc.fSampleCnt = sampleCount;
erikchen9a1ed5d2016-02-10 16:32:34 -0800214 desc.fTextureStorageAllocator = textureStorageAllocator;
cblume55f2d2d2016-02-26 13:20:48 -0800215 desc.fIsMipMapped = false;
bsalomon5ec26ae2016-02-25 08:33:02 -0800216 GrTexture* texture = context->textureProvider()->createTexture(desc, budgeted, nullptr, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700217 if (nullptr == texture) {
218 return nullptr;
kkinnunenabcfab42015-02-22 22:53:44 -0800219 }
halcanary96fcdcc2015-08-27 07:41:13 -0700220 SkASSERT(nullptr != texture->asRenderTarget());
kkinnunenabcfab42015-02-22 22:53:44 -0800221 return texture->asRenderTarget();
222}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000223
robertphillips1579e3c2016-03-24 05:01:23 -0700224// This method ensures that we always have a texture-backed "bitmap" when we finally
225// call through to the base impl so that the image filtering code will take the
226// gpu-specific paths. This mirrors SkCanvas::internalDrawDevice (the other
227// use of SkImageFilter::filterImage) in that the source and dest will have
228// homogenous backing (e.g., raster or gpu).
robertphillips2302de92016-03-24 07:26:32 -0700229void SkGpuDevice::drawSpriteWithFilter(const SkDraw& draw, const SkBitmap& bitmap,
230 int x, int y, const SkPaint& paint) {
robertphillips090b7622016-03-28 11:07:43 -0700231 ASSERT_SINGLE_OWNER
232 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpriteWithFilter", fContext);
233
234 if (fContext->abandoned()) {
235 return;
236 }
237
robertphillips1579e3c2016-03-24 05:01:23 -0700238 if (bitmap.getTexture()) {
robertphillips2302de92016-03-24 07:26:32 -0700239 INHERITED::drawSpriteWithFilter(draw, bitmap, x, y, paint);
robertphillips1579e3c2016-03-24 05:01:23 -0700240 return;
241 }
242
243 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
244 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
245 return;
246 }
247
248 GrTexture* texture;
249 // draw sprite neither filters nor tiles.
250 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
251 if (!texture) {
252 return;
253 }
254
255 SkBitmap newBitmap;
256
257 GrWrapTextureInBitmap(texture, texture->width(), texture->height(),
258 bitmap.isOpaque(), &newBitmap);
259
robertphillips2302de92016-03-24 07:26:32 -0700260 INHERITED::drawSpriteWithFilter(draw, newBitmap, x, y, paint);
robertphillips1579e3c2016-03-24 05:01:23 -0700261}
262
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000263///////////////////////////////////////////////////////////////////////////////
264
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000265bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
266 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800267 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000268
269 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700270 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps());
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000271 if (kUnknown_GrPixelConfig == config) {
272 return false;
273 }
274
275 uint32_t flags = 0;
276 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
277 flags = GrContext::kUnpremul_PixelOpsFlag;
278 }
bsalomon74f681d2015-06-23 14:38:48 -0700279 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
280 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000281}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000282
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000283bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
284 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800285 ASSERT_SINGLE_OWNER
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000286 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700287 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fContext->caps());
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000288 if (kUnknown_GrPixelConfig == config) {
289 return false;
290 }
291 uint32_t flags = 0;
292 if (kUnpremul_SkAlphaType == info.alphaType()) {
293 flags = GrContext::kUnpremul_PixelOpsFlag;
294 }
295 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
296
297 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700298 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000299
300 return true;
301}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000302
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000303const SkBitmap& SkGpuDevice::onAccessBitmap() {
joshualittce894002016-01-11 13:29:31 -0800304 ASSERT_SINGLE_OWNER
reed89443ab2014-06-27 11:34:19 -0700305 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000306}
307
reed41e010c2015-06-09 12:16:53 -0700308bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800309 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700310 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
311 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
312 // ... ugh.
313 fLegacyBitmap.notifyPixelsChanged();
314 return false;
315}
316
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000317void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
joshualittce894002016-01-11 13:29:31 -0800318 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000319 INHERITED::onAttachToCanvas(canvas);
320
321 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800322 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323}
324
325void SkGpuDevice::onDetachFromCanvas() {
joshualittce894002016-01-11 13:29:31 -0800326 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000327 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800328 fClip.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700329 fClipStack.reset(nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000330}
331
332// call this every draw call, to ensure that the context reflects our state,
333// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800334void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800335 ASSERT_SINGLE_OWNER
joshualitt44701df2015-02-23 14:44:57 -0800336 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000337
joshualitt44701df2015-02-23 14:44:57 -0800338 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000339
joshualitt570d2f82015-02-25 13:19:48 -0800340 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000341}
342
343GrRenderTarget* SkGpuDevice::accessRenderTarget() {
joshualittce894002016-01-11 13:29:31 -0800344 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000345 return fRenderTarget;
346}
347
reed8eddfb52014-12-04 07:50:14 -0800348void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800349 ASSERT_SINGLE_OWNER
reed8eddfb52014-12-04 07:50:14 -0800350 GrColor color = 0;
joshualitt5651ee62016-01-11 10:39:11 -0800351 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext);
reed8eddfb52014-12-04 07:50:14 -0800352 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700353 fDrawContext->clear(&rect, color, true);
reed8eddfb52014-12-04 07:50:14 -0800354}
355
kkinnunenabcfab42015-02-22 22:53:44 -0800356void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800357 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800358
bsalomon5ec26ae2016-02-25 08:33:02 -0800359 SkBudgeted budgeted = fRenderTarget->resourcePriv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800360
361 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
erikchen9a1ed5d2016-02-10 16:32:34 -0800362 this->context(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt,
363 fRenderTarget->desc().fTextureStorageAllocator));
kkinnunenabcfab42015-02-22 22:53:44 -0800364
halcanary96fcdcc2015-08-27 07:41:13 -0700365 if (nullptr == newRT) {
kkinnunenabcfab42015-02-22 22:53:44 -0800366 return;
367 }
368
369 if (shouldRetainContent) {
370 if (fRenderTarget->wasDestroyed()) {
371 return;
372 }
373 this->context()->copySurface(newRT, fRenderTarget);
374 }
375
376 SkASSERT(fRenderTarget != newRT);
377
mtklein18300a32016-03-16 13:53:35 -0700378 fRenderTarget.reset(newRT.release());
kkinnunenabcfab42015-02-22 22:53:44 -0800379
bsalomon74f681d2015-06-23 14:38:48 -0700380#ifdef SK_DEBUG
381 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
382 kPremul_SkAlphaType);
383 SkASSERT(info == fLegacyBitmap.info());
384#endif
halcanary385fe4d2015-08-26 13:07:48 -0700385 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget);
kkinnunenabcfab42015-02-22 22:53:44 -0800386 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700387
robertphillips77a2e522015-10-17 07:43:27 -0700388 fDrawContext.reset(this->context()->drawContext(fRenderTarget, &this->surfaceProps()));
kkinnunenabcfab42015-02-22 22:53:44 -0800389}
390
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000391///////////////////////////////////////////////////////////////////////////////
392
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000393void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800394 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800395 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800396 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000397
398 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700399 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700400 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700401 return;
402 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000403
robertphillips2e1e51f2015-10-15 08:01:48 -0700404 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000405}
406
407// must be in SkCanvas::PointMode order
408static const GrPrimitiveType gPointMode2PrimtiveType[] = {
409 kPoints_GrPrimitiveType,
410 kLines_GrPrimitiveType,
411 kLineStrip_GrPrimitiveType
412};
413
ethannicholas330bb952015-07-17 06:44:02 -0700414// suppress antialiasing on axis-aligned integer-coordinate lines
415static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
416 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
417 return false;
418 }
419 if (count == 2) {
420 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
421 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800422 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700423 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800424 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700425 // the two end pixels as being the lesser of two evils.
426 if (pts[0].fX == pts[1].fX) {
427 return ((int) pts[0].fX) != pts[0].fX;
428 }
429 if (pts[0].fY == pts[1].fY) {
430 return ((int) pts[0].fY) != pts[0].fY;
431 }
432 }
433 return true;
434}
435
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000436void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
437 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800438 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800439 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800440 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000441
442 SkScalar width = paint.getStrokeWidth();
443 if (width < 0) {
444 return;
445 }
446
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000447 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700448 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
449 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700450 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700451 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700452 return;
453 }
egdaniele61c4112014-06-12 10:24:21 -0700454 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700455 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700456 path.moveTo(pts[0]);
457 path.lineTo(pts[1]);
robertphillips2e1e51f2015-10-15 08:01:48 -0700458 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700459 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000460 }
461
ethannicholas330bb952015-07-17 06:44:02 -0700462 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000463 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800464 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700465 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000466 draw.drawPoints(mode, count, pts, paint, true);
467 return;
468 }
469
470 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700471 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700472 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700473 return;
474 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000475
robertphillips2e1e51f2015-10-15 08:01:48 -0700476 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700477 grPaint,
478 *draw.fMatrix,
479 gPointMode2PrimtiveType[mode],
480 SkToS32(count),
481 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700482 nullptr,
483 nullptr,
484 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700485 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000486}
487
488///////////////////////////////////////////////////////////////////////////////
489
robertphillipsff55b492015-11-24 07:56:59 -0800490void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800491 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800492 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800493 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000494
495 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
496 SkScalar width = paint.getStrokeWidth();
497
498 /*
499 We have special code for hairline strokes, miter-strokes, bevel-stroke
500 and fills. Anything else we just call our path code.
501 */
502 bool usePath = doStroke && width > 0 &&
503 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
504 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
egdanield58a0ba2014-06-11 10:30:05 -0700505
robertphillipsd8aa59d2015-08-05 09:07:12 -0700506 // a few other reasons we might need to call drawPath...
robertphillipsff55b492015-11-24 07:56:59 -0800507 if (paint.getMaskFilter() || paint.getPathEffect() ||
robertphillipsd8aa59d2015-08-05 09:07:12 -0700508 paint.getStyle() == SkPaint::kStrokeAndFill_Style) { // we can't both stroke and fill rects
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000509 usePath = true;
510 }
egdanield58a0ba2014-06-11 10:30:05 -0700511
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000512 if (usePath) {
513 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700514 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000515 path.addRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800516 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800517 fClip, path, paint,
518 *draw.fMatrix, nullptr,
519 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000520 return;
521 }
522
523 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700524 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700525 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700526 return;
527 }
Mike Klein744fb732014-06-23 15:13:26 -0400528
robertphillipsff55b492015-11-24 07:56:59 -0800529 GrStrokeInfo strokeInfo(paint);
530
robertphillips2e1e51f2015-10-15 08:01:48 -0700531 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000532}
533
534///////////////////////////////////////////////////////////////////////////////
535
536void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800537 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800538 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800539 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800540 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000542 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700543 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700544 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700545 return;
546 }
Mike Klein744fb732014-06-23 15:13:26 -0400547
egdanield58a0ba2014-06-11 10:30:05 -0700548 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000549 if (paint.getMaskFilter()) {
550 // try to hit the fast path for drawing filtered round rects
551
552 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800553 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000554 if (devRRect.allCornersCircular()) {
555 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700556 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
joshualitt5531d512014-12-17 15:50:11 -0800557 draw.fClip->getBounds(),
558 *draw.fMatrix,
559 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000560 SkIRect finalIRect;
561 maskRect.roundOut(&finalIRect);
562 if (draw.fClip->quickReject(finalIRect)) {
563 // clipped out
564 return;
565 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700566 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
567 fDrawContext,
joshualitt25d9c152015-02-18 12:29:52 -0800568 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800569 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800570 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700571 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700572 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000573 return;
574 }
575 }
576
577 }
578 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000579 }
580
robertphillips514450c2015-11-24 05:36:02 -0800581 if (paint.getMaskFilter() || paint.getPathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800582 // The only mask filter the native rrect drawing code could've handle was taken
583 // care of above.
584 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000585 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700586 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000587 path.addRRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800588 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800589 fClip, path, paint,
590 *draw.fMatrix, nullptr,
591 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000592 return;
593 }
Mike Klein744fb732014-06-23 15:13:26 -0400594
robertphillips514450c2015-11-24 05:36:02 -0800595 SkASSERT(!strokeInfo.isDashed());
596
robertphillips2e1e51f2015-10-15 08:01:48 -0700597 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000598}
599
robertphillipsd7706102016-02-25 09:28:08 -0800600
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000601void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800602 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800603 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800604 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800605 CHECK_SHOULD_DRAW(draw);
606
robertphillipsd7706102016-02-25 09:28:08 -0800607 if (outer.isEmpty()) {
608 return;
609 }
610
611 if (inner.isEmpty()) {
612 return this->drawRRect(draw, outer, paint);
613 }
614
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000615 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000616
robertphillips0e7029e2015-11-30 05:45:06 -0800617 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800618 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700619 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700620 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700621 return;
622 }
robertphillips00095892016-02-29 13:50:40 -0800623
624 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
625 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000626 }
627
628 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700629 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000630 path.addRRect(outer);
631 path.addRRect(inner);
632 path.setFillType(SkPath::kEvenOdd_FillType);
633
robertphillips7bceedc2015-12-01 12:51:26 -0800634 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillips0e7029e2015-11-30 05:45:06 -0800635 fClip, path, paint,
636 *draw.fMatrix, nullptr,
637 draw.fClip->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000638}
639
640
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000641/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000642
robertphillips514450c2015-11-24 05:36:02 -0800643void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800644 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800645 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800646 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000647
robertphillips514450c2015-11-24 05:36:02 -0800648 // Presumably the path effect warps this to something other than an oval
649 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000650 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700651 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000652 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700653 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000654 return;
herb11a7f7f2015-11-24 12:41:00 -0800655 }
656
robertphillips514450c2015-11-24 05:36:02 -0800657 if (paint.getMaskFilter()) {
658 // The RRect path can handle special case blurring
659 SkRRect rr = SkRRect::MakeOval(oval);
660 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000661 }
662
663 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700664 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700665 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700666 return;
667 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000668
robertphillips514450c2015-11-24 05:36:02 -0800669 GrStrokeInfo strokeInfo(paint);
670 SkASSERT(!strokeInfo.isDashed());
671
robertphillips2e1e51f2015-10-15 08:01:48 -0700672 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000673}
674
675#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000676
677///////////////////////////////////////////////////////////////////////////////
678
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000679void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
680 const SkPaint& paint, const SkMatrix* prePathMatrix,
681 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800682 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800683 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
684 bool isClosed;
685 SkRect rect;
686 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
687 this->drawRect(draw, rect, paint);
688 return;
689 }
690 if (origSrcPath.isOval(&rect)) {
691 this->drawOval(draw, rect, paint);
692 return;
693 }
694 SkRRect rrect;
695 if (origSrcPath.isRRect(&rrect)) {
696 this->drawRRect(draw, rrect, paint);
697 return;
698 }
699 }
700
joshualitt5531d512014-12-17 15:50:11 -0800701 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800702 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000703
robertphillips7bceedc2015-12-01 12:51:26 -0800704 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsccb1b572015-05-27 11:02:55 -0700705 fClip, origSrcPath, paint,
706 *draw.fMatrix, prePathMatrix,
707 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000708}
709
710static const int kBmpSmallTileSize = 1 << 10;
711
712static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
713 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
714 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
715 return tilesX * tilesY;
716}
717
reed85d91782015-09-10 14:33:38 -0700718static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000719 if (maxTileSize <= kBmpSmallTileSize) {
720 return maxTileSize;
721 }
722
723 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
724 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
725
726 maxTileTotalTileSize *= maxTileSize * maxTileSize;
727 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
728
729 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
730 return kBmpSmallTileSize;
731 } else {
732 return maxTileSize;
733 }
734}
735
736// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
737// pixels from the bitmap are necessary.
bsalomon74f681d2015-06-23 14:38:48 -0700738static void determine_clipped_src_rect(const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800739 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800740 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700741 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742 const SkRect* srcRectPtr,
743 SkIRect* clippedSrcIRect) {
robertphillips7bceedc2015-12-01 12:51:26 -0800744 clip.getConservativeBounds(rt->width(), rt->height(), clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000745 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800746 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000747 clippedSrcIRect->setEmpty();
748 return;
749 }
750 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
751 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700752 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000753 // we've setup src space 0,0 to map to the top left of the src rect.
754 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000755 if (!clippedSrcRect.intersect(*srcRectPtr)) {
756 clippedSrcIRect->setEmpty();
757 return;
758 }
759 }
760 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700761 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000762 if (!clippedSrcIRect->intersect(bmpBounds)) {
763 clippedSrcIRect->setEmpty();
764 }
765}
766
reed85d91782015-09-10 14:33:38 -0700767bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
768 const SkMatrix& viewMatrix,
769 const GrTextureParams& params,
770 const SkRect* srcRectPtr,
771 int maxTileSize,
772 int* tileSize,
773 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800774 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700775 // if it's larger than the max tile size, then we have no choice but tiling.
776 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
777 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(),
778 srcRectPtr, clippedSubset);
779 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
780 return true;
781 }
782
bsalomon1a1d0b82015-10-16 07:49:42 -0700783 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700784 const size_t area = imageRect.width() * imageRect.height();
785 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
786 return false;
787 }
788
reed85d91782015-09-10 14:33:38 -0700789 // At this point we know we could do the draw by uploading the entire bitmap
790 // as a texture. However, if the texture would be large compared to the
791 // cache size and we don't require most of it for this draw then tile to
792 // reduce the amount of upload and cache spill.
793
794 // assumption here is that sw bitmap size is a good proxy for its size as
795 // a texture
796 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
797 size_t cacheSize;
798 fContext->getResourceCacheLimits(nullptr, &cacheSize);
799 if (bmpSize < cacheSize / 2) {
800 return false;
801 }
802
bsalomon1a1d0b82015-10-16 07:49:42 -0700803 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
804 // tiling memory savings would be < 50%.
reed85d91782015-09-10 14:33:38 -0700805 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(), srcRectPtr,
806 clippedSubset);
807 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
808 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
809 kBmpSmallTileSize * kBmpSmallTileSize;
810
811 return usedTileBytes < 2 * bmpSize;
812}
813
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000814bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800815 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000816 const GrTextureParams& params,
817 const SkRect* srcRectPtr,
818 int maxTileSize,
819 int* tileSize,
820 SkIRect* clippedSrcRect) const {
joshualittce894002016-01-11 13:29:31 -0800821 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000822 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700823 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000824 return false;
825 }
826
reed85d91782015-09-10 14:33:38 -0700827 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
828 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
829}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000830
reed85d91782015-09-10 14:33:38 -0700831bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
832 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
833 const SkMatrix& viewMatrix) const {
joshualittce894002016-01-11 13:29:31 -0800834 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700835 // if image is explictly texture backed then just use the texture
836 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000837 return false;
838 }
839
reed85d91782015-09-10 14:33:38 -0700840 GrTextureParams params;
841 bool doBicubic;
842 GrTextureParams::FilterMode textureFilterMode =
843 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
844
845 int tileFilterPad;
846 if (doBicubic) {
847 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
848 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
849 tileFilterPad = 0;
850 } else {
851 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000852 }
reed85d91782015-09-10 14:33:38 -0700853 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000854
bsalomon8c07b7a2015-11-02 11:36:52 -0800855 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000856
reed85d91782015-09-10 14:33:38 -0700857 // these are output, which we safely ignore, as we just want to know the predicate
858 int outTileSize;
859 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000860
reed85d91782015-09-10 14:33:38 -0700861 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
862 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000863}
864
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000865void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000866 const SkBitmap& bitmap,
867 const SkMatrix& m,
868 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800869 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800870 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800871 SkMatrix viewMatrix;
872 viewMatrix.setConcat(*origDraw.fMatrix, m);
873 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800874 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonb1b01992015-11-18 10:56:08 -0800875 // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
bsalomonf1ecd212015-12-09 17:06:02 -0800876 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
877 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800878 return;
879 }
bsalomonb1b01992015-11-18 10:56:08 -0800880 int maxTileSize = fContext->caps()->maxTileSize();
881
882 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
883 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
884 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
885 paint.isAntiAlias() &&
886 bitmap.width() <= maxTileSize &&
887 bitmap.height() <= maxTileSize;
888
889 bool skipTileCheck = drawAA || paint.getMaskFilter();
890
891 if (!skipTileCheck) {
892 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
893 int tileSize;
894 SkIRect clippedSrcRect;
895
896 GrTextureParams params;
897 bool doBicubic;
898 GrTextureParams::FilterMode textureFilterMode =
899 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
900 &doBicubic);
901
902 int tileFilterPad;
903
904 if (doBicubic) {
905 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
906 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
907 tileFilterPad = 0;
908 } else {
909 tileFilterPad = 1;
910 }
911 params.setFilterMode(textureFilterMode);
912
913 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
914 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
915 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
916 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
917 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
918 return;
919 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000920 }
bsalomonb1b01992015-11-18 10:56:08 -0800921 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800922 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
923 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000924}
925
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000926// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000927// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
928// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000929static inline void clamped_outset_with_offset(SkIRect* iRect,
930 int outset,
931 SkPoint* offset,
932 const SkIRect& clamp) {
933 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000934
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000935 int leftClampDelta = clamp.fLeft - iRect->fLeft;
936 if (leftClampDelta > 0) {
937 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000938 iRect->fLeft = clamp.fLeft;
939 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000940 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000941 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000942
943 int topClampDelta = clamp.fTop - iRect->fTop;
944 if (topClampDelta > 0) {
945 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000946 iRect->fTop = clamp.fTop;
947 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000948 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000949 }
950
951 if (iRect->fRight > clamp.fRight) {
952 iRect->fRight = clamp.fRight;
953 }
954 if (iRect->fBottom > clamp.fBottom) {
955 iRect->fBottom = clamp.fBottom;
956 }
957}
958
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000959// Break 'bitmap' into several tiles to draw it since it has already
960// been determined to be too large to fit in VRAM
961void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800962 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000963 const SkRect& srcRect,
964 const SkIRect& clippedSrcIRect,
965 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800966 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700967 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000968 int tileSize,
969 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800970 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800971
ericrk82e26bf2016-02-25 22:15:29 -0800972 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entry.
ericrk369e9372016-02-05 15:32:36 -0800973 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
974
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000975 // The following pixel lock is technically redundant, but it is desirable
976 // to lock outside of the tile loop to prevent redecoding the whole image
977 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
978 // is larger than the limit of the discardable memory pool.
979 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800980
981 const SkPaint* paint = &origPaint;
982 SkPaint tempPaint;
983 if (origPaint.isAntiAlias() && !fRenderTarget->isUnifiedMultisampled()) {
984 // Drop antialiasing to avoid seams at tile boundaries.
985 tempPaint = origPaint;
986 tempPaint.setAntiAlias(false);
987 paint = &tempPaint;
988 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000989 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
990
991 int nx = bitmap.width() / tileSize;
992 int ny = bitmap.height() / tileSize;
993 for (int x = 0; x <= nx; x++) {
994 for (int y = 0; y <= ny; y++) {
995 SkRect tileR;
996 tileR.set(SkIntToScalar(x * tileSize),
997 SkIntToScalar(y * tileSize),
998 SkIntToScalar((x + 1) * tileSize),
999 SkIntToScalar((y + 1) * tileSize));
1000
1001 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1002 continue;
1003 }
1004
1005 if (!tileR.intersect(srcRect)) {
1006 continue;
1007 }
1008
1009 SkBitmap tmpB;
1010 SkIRect iTileR;
1011 tileR.roundOut(&iTileR);
1012 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1013 SkIntToScalar(iTileR.fTop));
1014
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001015 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001016 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001017 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001018 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001019 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001020
robertphillipsec8bb942014-11-21 10:16:25 -08001021 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001022 SkIRect iClampRect;
1023
reeda5517e22015-07-14 10:54:12 -07001024 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001025 // In bleed mode we want to always expand the tile on all edges
1026 // but stay within the bitmap bounds
1027 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1028 } else {
1029 // In texture-domain/clamp mode we only want to expand the
1030 // tile on edges interior to "srcRect" (i.e., we want to
1031 // not bleed across the original clamped edges)
1032 srcRect.roundOut(&iClampRect);
1033 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001034 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1035 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001036 }
1037
1038 if (bitmap.extractSubset(&tmpB, iTileR)) {
1039 // now offset it to make it "local" to our tmp bitmap
1040 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001041 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001042 // de-optimized this determination
1043 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001044 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001045 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001046 tileR,
1047 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -08001048 *paint,
reeda5517e22015-07-14 10:54:12 -07001049 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001050 bicubic,
1051 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001052 }
1053 }
1054 }
1055}
1056
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001057/*
1058 * This is called by drawBitmap(), which has to handle images that may be too
1059 * large to be represented by a single texture.
1060 *
1061 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1062 * and that non-texture portion of the GrPaint has already been setup.
1063 */
1064void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001065 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001066 const SkRect& srcRect,
1067 const GrTextureParams& params,
1068 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001069 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001070 bool bicubic,
1071 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001072 // We should have already handled bitmaps larger than the max texture size.
1073 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1074 bitmap.height() <= fContext->caps()->maxTextureSize());
1075 // Unless the bitmap is inherently texture-backed, we should be respecting the max tile size
1076 // by the time we get here.
1077 SkASSERT(bitmap.getTexture() ||
1078 (bitmap.width() <= fContext->caps()->maxTileSize() &&
1079 bitmap.height() <= fContext->caps()->maxTileSize()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001080
1081 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001082 AutoBitmapTexture abt(fContext, bitmap, params, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001083 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001084 return;
1085 }
1086
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001087 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001088 SkRect paintRect;
1089 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1090 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1091 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1092 SkScalarMul(srcRect.fTop, hInv),
1093 SkScalarMul(srcRect.fRight, wInv),
1094 SkScalarMul(srcRect.fBottom, hInv));
1095
egdaniel79da63f2015-10-09 10:55:16 -07001096 SkMatrix texMatrix;
1097 texMatrix.reset();
1098 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1099 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1100 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1101 // Thus we need to pass in the transform matrix directly to the texture processor used for
1102 // the bitmap draw.
1103 texMatrix.setScale(wInv, hInv);
1104 }
1105
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001106 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001107
1108 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1109 // the rest from the SkPaint.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001110 SkAutoTUnref<const GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001111
reeda5517e22015-07-14 10:54:12 -07001112 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001113 // Use a constrained texture domain to avoid color bleeding
1114 SkScalar left, top, right, bottom;
1115 if (srcRect.width() > SK_Scalar1) {
1116 SkScalar border = SK_ScalarHalf / texture->width();
1117 left = paintRect.left() + border;
1118 right = paintRect.right() - border;
1119 } else {
1120 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1121 }
1122 if (srcRect.height() > SK_Scalar1) {
1123 SkScalar border = SK_ScalarHalf / texture->height();
1124 top = paintRect.top() + border;
1125 bottom = paintRect.bottom() - border;
1126 } else {
1127 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1128 }
1129 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001130 if (bicubic) {
egdaniel79da63f2015-10-09 10:55:16 -07001131 fp.reset(GrBicubicEffect::Create(texture, texMatrix, textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001132 } else {
bsalomon4a339522015-10-06 08:40:50 -07001133 fp.reset(GrTextureDomainEffect::Create(texture,
egdaniel79da63f2015-10-09 10:55:16 -07001134 texMatrix,
joshualitt5531d512014-12-17 15:50:11 -08001135 textureDomain,
1136 GrTextureDomain::kClamp_Mode,
1137 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001138 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001139 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001140 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1141 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
egdaniel79da63f2015-10-09 10:55:16 -07001142 fp.reset(GrBicubicEffect::Create(texture, texMatrix, tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001143 } else {
egdaniel79da63f2015-10-09 10:55:16 -07001144 fp.reset(GrSimpleTextureEffect::Create(texture, texMatrix, params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001145 }
1146
joshualitt33a5fce2015-11-18 13:28:51 -08001147 GrPaint grPaint;
1148 if (!SkPaintToGrPaintWithTexture(this->context(), paint, viewMatrix, fp,
brianosman898235c2016-04-06 07:38:23 -07001149 kAlpha_8_SkColorType == bitmap.colorType(),
brianosmanb461d342016-04-13 13:10:14 -07001150 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001151 return;
1152 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001153
egdaniel79da63f2015-10-09 10:55:16 -07001154 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1155 // We don't have local coords in this case and have previously set the transform
1156 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001157 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001158 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001159 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001160 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001161}
1162
fmalita2d97bc12014-11-20 10:44:58 -08001163bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001164 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001165 const SkImageFilter* filter,
1166 const SkImageFilter::Context& ctx,
1167 SkBitmap* result, SkIPoint* offset) {
joshualittce894002016-01-11 13:29:31 -08001168 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001169 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001170
reed88d064d2015-10-12 11:30:02 -07001171 SkImageFilter::DeviceProxy proxy(this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001172
1173 if (filter->canFilterImageGPU()) {
robertphillips1de87df2016-01-14 06:03:29 -08001174 SkBitmap bm;
1175 GrWrapTextureInBitmap(texture, width, height, false, &bm);
robertphillips48e78462016-02-17 13:57:16 -08001176 return filter->filterImageGPUDeprecated(&proxy, bm, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001177 } else {
1178 return false;
1179 }
1180}
1181
1182void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1183 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001184 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001185 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001186 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001187
1188 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1189 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1190 return;
1191 }
1192
robertphillipse1849d12016-03-17 08:26:38 -07001193 int offX = bitmap.pixelRefOrigin().fX;
1194 int offY = bitmap.pixelRefOrigin().fY;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195 int w = bitmap.width();
1196 int h = bitmap.height();
1197
1198 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001199 // draw sprite neither filters nor tiles.
1200 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001201 if (!texture) {
1202 return;
1203 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001204
bsalomonf1b7a1d2015-09-28 06:26:28 -07001205 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
1206
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001207 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001208 // This bitmap will own the filtered result as a texture.
1209 SkBitmap filteredBitmap;
1210
bsalomon49f085d2014-09-05 13:34:00 -07001211 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001212 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001213 SkMatrix matrix(*draw.fMatrix);
1214 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblancodb64af32015-12-09 10:11:43 -08001215 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-left, -top);
senorblancobe129b22014-08-08 07:14:35 -07001216 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001217 // This cache is transient, and is freed (along with all its contained
1218 // textures) when it goes out of scope.
reed4e23cda2016-01-11 10:56:59 -08001219 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001220 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001221 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001222 texture = (GrTexture*) filteredBitmap.getTexture();
robertphillipse1849d12016-03-17 08:26:38 -07001223 offX = filteredBitmap.pixelRefOrigin().fX;
1224 offY = filteredBitmap.pixelRefOrigin().fY;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001225 w = filteredBitmap.width();
1226 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001227 left += offset.x();
1228 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001229 } else {
1230 return;
1231 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001232 SkASSERT(!GrPixelConfigIsAlphaOnly(texture->config()));
1233 alphaOnly = false;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001234 }
1235
1236 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001237 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001238 GrSimpleTextureEffect::Create(texture, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001239 if (alphaOnly) {
1240 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1241 } else {
1242 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1243 }
brianosman898235c2016-04-06 07:38:23 -07001244 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp,
brianosmanb461d342016-04-13 13:10:14 -07001245 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001246 return;
1247 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001248
bsalomona2e69fc2015-11-05 10:41:43 -08001249 fDrawContext->fillRectToRect(fClip,
1250 grPaint,
1251 SkMatrix::I(),
1252 SkRect::MakeXYWH(SkIntToScalar(left),
1253 SkIntToScalar(top),
1254 SkIntToScalar(w),
1255 SkIntToScalar(h)),
robertphillipse1849d12016-03-17 08:26:38 -07001256 SkRect::MakeXYWH(SkIntToScalar(offX) / texture->width(),
1257 SkIntToScalar(offY) / texture->height(),
1258 SkIntToScalar(w) / texture->width(),
1259 SkIntToScalar(h) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001260}
1261
bsalomonb1b01992015-11-18 10:56:08 -08001262void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1263 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001264 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001265 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001266 CHECK_SHOULD_DRAW(draw);
bsalomonb1b01992015-11-18 10:56:08 -08001267 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001268 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001269 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
1270 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001271 return;
1272 }
bsalomonb1b01992015-11-18 10:56:08 -08001273 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1274 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1275 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1276 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1277 const SkRect* dst = &origDst;
1278 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001279 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001280 if (!src) {
1281 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001282 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001283
bsalomonb1b01992015-11-18 10:56:08 -08001284 SkMatrix srcToDstMatrix;
1285 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1286 return;
1287 }
1288 SkRect tmpSrc, tmpDst;
1289 if (src != &bmpBounds) {
1290 if (!bmpBounds.contains(*src)) {
1291 tmpSrc = *src;
1292 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001293 return; // nothing to draw
1294 }
bsalomonb1b01992015-11-18 10:56:08 -08001295 src = &tmpSrc;
1296 srcToDstMatrix.mapRect(&tmpDst, *src);
1297 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001298 }
1299 }
1300
bsalomonb1b01992015-11-18 10:56:08 -08001301 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001302
bsalomonb1b01992015-11-18 10:56:08 -08001303 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1304 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
1305 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
1306 paint.isAntiAlias() &&
1307 bitmap.width() <= maxTileSize &&
1308 bitmap.height() <= maxTileSize;
1309
1310 bool skipTileCheck = drawAA || paint.getMaskFilter();
1311
1312 if (!skipTileCheck) {
1313 int tileSize;
1314 SkIRect clippedSrcRect;
1315
1316 GrTextureParams params;
1317 bool doBicubic;
1318 GrTextureParams::FilterMode textureFilterMode =
1319 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1320 &doBicubic);
1321
1322 int tileFilterPad;
1323
1324 if (doBicubic) {
1325 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1326 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1327 tileFilterPad = 0;
1328 } else {
1329 tileFilterPad = 1;
1330 }
1331 params.setFilterMode(textureFilterMode);
1332
1333 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1334 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1335 // we have a mask filter.
1336 SkMatrix viewMatrix = *draw.fMatrix;
1337 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1338 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1339 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1340 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1341 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1342 constraint, tileSize, doBicubic);
1343 return;
1344 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001345 }
bsalomonb1b01992015-11-18 10:56:08 -08001346 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001347 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001348}
1349
1350void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1351 int x, int y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001352 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001353 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001354 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001355 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001356
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001357 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001358 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001359
1360 GrRenderTarget* devRT = dev->accessRenderTarget();
1361 GrTexture* devTex;
halcanary96fcdcc2015-08-27 07:41:13 -07001362 if (nullptr == (devTex = devRT->asTexture())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001363 return;
1364 }
1365
robertphillips7b9e8a42014-12-11 08:20:31 -08001366 const SkImageInfo ii = dev->imageInfo();
1367 int w = ii.width();
1368 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001369
1370 SkImageFilter* filter = paint.getImageFilter();
1371 // This bitmap will own the filtered result as a texture.
1372 SkBitmap filteredBitmap;
1373
bsalomon49f085d2014-09-05 13:34:00 -07001374 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001375 SkIPoint offset = SkIPoint::Make(0, 0);
1376 SkMatrix matrix(*draw.fMatrix);
1377 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblancodb64af32015-12-09 10:11:43 -08001378 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-x, -y);
senorblanco55b6d8b2014-07-30 11:26:46 -07001379 // This cache is transient, and is freed (along with all its contained
1380 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001381 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
reed4e23cda2016-01-11 10:56:59 -08001382 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001383 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1384 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001385 devTex = filteredBitmap.getTexture();
1386 w = filteredBitmap.width();
1387 h = filteredBitmap.height();
1388 x += offset.fX;
1389 y += offset.fY;
1390 } else {
1391 return;
1392 }
1393 }
1394
1395 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001396 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001397 GrSimpleTextureEffect::Create(devTex, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001398 if (GrPixelConfigIsAlphaOnly(devTex->config())) {
1399 // Can this happen?
1400 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1401 } else {
1402 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1403 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001404
brianosman898235c2016-04-06 07:38:23 -07001405 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp,
brianosmanb461d342016-04-13 13:10:14 -07001406 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001407 return;
1408 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001409
1410 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1411 SkIntToScalar(y),
1412 SkIntToScalar(w),
1413 SkIntToScalar(h));
1414
1415 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1416 // scratch texture).
1417 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1418 SK_Scalar1 * h / devTex->height());
1419
bsalomona2e69fc2015-11-05 10:41:43 -08001420 fDrawContext->fillRectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001421}
1422
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001423bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
joshualittce894002016-01-11 13:29:31 -08001424 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001425 return filter->canFilterImageGPU();
1426}
1427
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001428bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001429 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001430 SkBitmap* result, SkIPoint* offset) {
joshualittce894002016-01-11 13:29:31 -08001431 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001432 // want explicitly our impl, so guard against a subclass of us overriding it
1433 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1434 return false;
1435 }
1436
1437 SkAutoLockPixels alp(src, !src.getTexture());
1438 if (!src.getTexture() && !src.readyToDraw()) {
1439 return false;
1440 }
1441
1442 GrTexture* texture;
1443 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1444 // must be pushed upstack.
bsalomonafa95e22015-10-12 10:39:46 -07001445 AutoBitmapTexture abt(fContext, src, GrTextureParams::ClampNoFilter(), &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001446 if (!texture) {
1447 return false;
1448 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001449
senorblancod0d37ca2015-04-02 04:54:56 -07001450 return this->filterTexture(fContext, texture, src.width(), src.height(),
1451 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001452}
1453
reeda85d4d02015-05-06 12:56:48 -07001454void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1455 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001456 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001457 SkMatrix viewMatrix = *draw.fMatrix;
1458 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001459 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001460 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001461 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001462 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1463 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001464 return;
reed85d91782015-09-10 14:33:38 -07001465 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001466 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001467 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1468 paint.getFilterQuality(), *draw.fMatrix)) {
1469 // only support tiling as bitmap at the moment, so force raster-version
1470 if (!as_IB(image)->getROPixels(&bm)) {
1471 return;
1472 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001473 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1474 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1475 CHECK_SHOULD_DRAW(draw);
1476 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001477 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1478 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001479 } else if (as_IB(image)->getROPixels(&bm)) {
1480 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001481 }
reeda85d4d02015-05-06 12:56:48 -07001482 }
1483}
1484
1485void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001486 const SkRect& dst, const SkPaint& paint,
1487 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001488 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001489 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001490 CHECK_SHOULD_DRAW(draw);
1491 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001492 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001493 return;
1494 }
1495 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001496 SkMatrix totalMatrix = *draw.fMatrix;
1497 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1498 dst.height() / (src ? src->height() : image->height()));
1499 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001500 // only support tiling as bitmap at the moment, so force raster-version
1501 if (!as_IB(image)->getROPixels(&bm)) {
1502 return;
1503 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001504 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1505 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1506 CHECK_SHOULD_DRAW(draw);
1507 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001508 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001509 } else if (as_IB(image)->getROPixels(&bm)) {
1510 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001511 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001512}
1513
bsalomonf1ecd212015-12-09 17:06:02 -08001514void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001515 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001516 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001517
joshualitt33a5fce2015-11-18 13:28:51 -08001518 CHECK_SHOULD_DRAW(draw);
1519
joshualittedb36442015-11-19 14:29:30 -08001520 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
1521 fRenderTarget->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001522 bool doBicubic;
1523 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001524 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1525 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001526 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001527 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001528
1529 SkRect srcR, dstR;
1530 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001531 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001532 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001533 }
1534 return;
1535 }
1536
bsalomon2bbd0c62015-12-09 12:50:56 -08001537 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
1538 SkAutoTUnref<const GrFragmentProcessor> fp(
1539 producer->createFragmentProcessor(SkMatrix::I(),
1540 SkRect::MakeIWH(producer->width(), producer->height()),
1541 GrTextureProducer::kNo_FilterConstraint, true,
1542 &kMode));
joshualitt33a5fce2015-11-18 13:28:51 -08001543 GrPaint grPaint;
1544 if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, fp,
brianosman898235c2016-04-06 07:38:23 -07001545 producer->isAlphaOnly(),
brianosmanb461d342016-04-13 13:10:14 -07001546 this->surfaceProps().isGammaCorrect(), &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001547 return;
1548 }
1549
bsalomon2bbd0c62015-12-09 12:50:56 -08001550 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1551 producer->height(), center, dst);
1552}
1553
1554void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1555 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001556 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001557 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001558 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001559 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001560 } else {
1561 SkBitmap bm;
1562 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1563 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001564 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001565 } else if (as_IB(image)->getROPixels(&bm)) {
1566 this->drawBitmapNine(draw, bm, center, dst, paint);
1567 }
1568 }
1569}
1570
1571void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1572 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001573 ASSERT_SINGLE_OWNER
bsalomon2bbd0c62015-12-09 12:50:56 -08001574 if (bitmap.getTexture()) {
1575 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001576 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001577 } else {
1578 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001579 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001580 }
joshualitt33a5fce2015-11-18 13:28:51 -08001581}
1582
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001583///////////////////////////////////////////////////////////////////////////////
1584
1585// must be in SkCanvas::VertexMode order
1586static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1587 kTriangles_GrPrimitiveType,
1588 kTriangleStrip_GrPrimitiveType,
1589 kTriangleFan_GrPrimitiveType,
1590};
1591
1592void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1593 int vertexCount, const SkPoint vertices[],
1594 const SkPoint texs[], const SkColor colors[],
1595 SkXfermode* xmode,
1596 const uint16_t indices[], int indexCount,
1597 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001598 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001599 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001600 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001601
halcanary96fcdcc2015-08-27 07:41:13 -07001602 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1603 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001604
halcanary96fcdcc2015-08-27 07:41:13 -07001605 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001606
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001607 SkPaint copy(paint);
1608 copy.setStyle(SkPaint::kStroke_Style);
1609 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001610
bsalomonf1b7a1d2015-09-28 06:26:28 -07001611 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001612 // we ignore the shader if texs is null.
brianosman898235c2016-04-06 07:38:23 -07001613 if (!SkPaintToGrPaintNoShader(this->context(), copy,
brianosmanb461d342016-04-13 13:10:14 -07001614 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001615 return;
1616 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001617
dandov32a311b2014-07-15 19:46:26 -07001618 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001619 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001620 switch (vmode) {
1621 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001622 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001623 break;
1624 case SkCanvas::kTriangleStrip_VertexMode:
1625 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001626 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001627 break;
1628 }
mtklein533eb782014-08-27 10:39:42 -07001629
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001630 VertState state(vertexCount, indices, indexCount);
1631 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001632
dandov32a311b2014-07-15 19:46:26 -07001633 //number of indices for lines per triangle with kLines
1634 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001635
bsalomonf1b7a1d2015-09-28 06:26:28 -07001636 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001637 int i = 0;
1638 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001639 lineIndices[i] = state.f0;
1640 lineIndices[i + 1] = state.f1;
1641 lineIndices[i + 2] = state.f1;
1642 lineIndices[i + 3] = state.f2;
1643 lineIndices[i + 4] = state.f2;
1644 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001645 i += 6;
1646 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001647 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001648 grPaint,
1649 *draw.fMatrix,
1650 kLines_GrPrimitiveType,
1651 vertexCount,
1652 vertices,
1653 texs,
1654 colors,
1655 lineIndices.get(),
1656 indexCount);
1657 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001658 }
1659
bsalomonf1b7a1d2015-09-28 06:26:28 -07001660 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001661
1662 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001663 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001664 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1665 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001666 convertedColors.reset(vertexCount);
1667 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001668 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001669 }
1670 colors = convertedColors.get();
1671 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001672 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001673 if (texs && paint.getShader()) {
1674 if (colors) {
1675 // When there are texs and colors the shader and colors are combined using xmode. A null
1676 // xmode is defined to mean modulate.
1677 SkXfermode::Mode colorMode;
1678 if (xmode) {
1679 if (!xmode->asMode(&colorMode)) {
1680 return;
1681 }
1682 } else {
1683 colorMode = SkXfermode::kModulate_Mode;
1684 }
1685 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
brianosmanb461d342016-04-13 13:10:14 -07001686 false, this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001687 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001688 return;
1689 }
1690 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001691 // We have a shader, but no colors to blend it against.
brianosman898235c2016-04-06 07:38:23 -07001692 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001693 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001694 return;
1695 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001696 }
bsalomonaa48d362015-10-01 08:34:17 -07001697 } else {
1698 if (colors) {
1699 // We have colors, but either have no shader or no texture coords (which implies that
1700 // we should ignore the shader).
brianosman898235c2016-04-06 07:38:23 -07001701 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001702 this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001703 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001704 return;
1705 }
1706 } else {
1707 // No colors and no shaders. Just draw with the paint color.
brianosman898235c2016-04-06 07:38:23 -07001708 if (!SkPaintToGrPaintNoShader(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001709 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001710 return;
1711 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001712 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001713 }
1714
robertphillips2e1e51f2015-10-15 08:01:48 -07001715 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001716 grPaint,
1717 *draw.fMatrix,
1718 primType,
1719 vertexCount,
1720 vertices,
1721 texs,
1722 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001723 indices,
robertphillipsea461502015-05-26 11:38:03 -07001724 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001725}
1726
1727///////////////////////////////////////////////////////////////////////////////
1728
jvanverth31ff7622015-08-07 10:09:28 -07001729void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001730 const SkRect texRect[], const SkColor colors[], int count,
1731 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001732 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001733 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001734 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001735 return;
1736 }
1737
jvanverth31ff7622015-08-07 10:09:28 -07001738 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001739 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001740
reedca109532015-06-25 16:25:25 -07001741 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001742 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001743
jvanverth31ff7622015-08-07 10:09:28 -07001744 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001745 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001746 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
brianosmanb461d342016-04-13 13:10:14 -07001747 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001748 return;
1749 }
1750 } else {
brianosman898235c2016-04-06 07:38:23 -07001751 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001752 this->surfaceProps().isGammaCorrect(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001753 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001754 }
1755 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001756
1757 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001758 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001759}
1760
1761///////////////////////////////////////////////////////////////////////////////
1762
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001763void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001764 size_t byteLength, SkScalar x, SkScalar y,
1765 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001766 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001767 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001768 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001769
jvanverth8c27a182014-10-14 08:45:50 -07001770 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001771 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001772 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001773 return;
1774 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001775
jvanverth8c27a182014-10-14 08:45:50 -07001776 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001777
robertphillips2e1e51f2015-10-15 08:01:48 -07001778 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001779 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001780}
1781
fmalita05c4a432014-09-29 06:29:53 -07001782void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1783 const SkScalar pos[], int scalarsPerPos,
1784 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001785 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001786 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001787 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001788
jvanverth8c27a182014-10-14 08:45:50 -07001789 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001790 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001791 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001792 return;
1793 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001794
jvanverth8c27a182014-10-14 08:45:50 -07001795 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001796
robertphillips2e1e51f2015-10-15 08:01:48 -07001797 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001798 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1799 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001800}
1801
joshualitt9c328182015-03-23 08:13:04 -07001802void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1803 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001804 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001805 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001806 CHECK_SHOULD_DRAW(draw);
1807
1808 SkDEBUGCODE(this->validate();)
1809
robertphillips2e1e51f2015-10-15 08:01:48 -07001810 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001811 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001812}
1813
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001814///////////////////////////////////////////////////////////////////////////////
1815
reedb2db8982014-11-13 12:41:02 -08001816bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001817 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001818}
1819
1820void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001821 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001822
bsalomonc49e8682015-06-30 11:37:35 -07001823 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001824}
1825
1826///////////////////////////////////////////////////////////////////////////////
1827
reed76033be2015-03-14 10:54:31 -07001828SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001829 ASSERT_SINGLE_OWNER
bsalomonf2703d82014-10-28 14:33:06 -07001830 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001831 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001832 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001833 desc.fWidth = cinfo.fInfo.width();
1834 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001835 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001836
1837 SkAutoTUnref<GrTexture> texture;
1838 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001839 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001840
hcm4396fa52014-10-27 21:43:30 -07001841 // layers are never draw in repeat modes, so we can request an approx
1842 // match and ignore any padding.
bsalomoneae62002015-07-31 13:59:30 -07001843 if (kNever_TileUsage == cinfo.fTileUsage) {
1844 texture.reset(fContext->textureProvider()->createApproxTexture(desc));
1845 } else {
bsalomon5ec26ae2016-02-25 08:33:02 -08001846 texture.reset(fContext->textureProvider()->createTexture(desc, SkBudgeted::kYes));
bsalomoneae62002015-07-31 13:59:30 -07001847 }
bsalomonafe30052015-01-16 07:32:33 -08001848
1849 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001850 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001851 return SkGpuDevice::Create(
bsalomon74f681d2015-06-23 14:38:48 -07001852 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, init);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001853 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001854 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001855 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001856 cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary96fcdcc2015-08-27 07:41:13 -07001857 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001858 }
1859}
1860
reede8f30622016-03-23 18:59:25 -07001861sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001862 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001863 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001864 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
reede8f30622016-03-23 18:59:25 -07001865 return SkSurface::MakeRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
1866 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001867}
1868
robertphillips30d2cc62014-09-24 08:52:18 -07001869bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001870 const SkMatrix* matrix, const SkPaint* paint) {
joshualittce894002016-01-11 13:29:31 -08001871 ASSERT_SINGLE_OWNER
robertphillips63242d72014-12-04 08:31:02 -08001872#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001873 // todo: should handle this natively
1874 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001875 return false;
1876 }
1877
halcanary96fcdcc2015-08-27 07:41:13 -07001878 const SkBigPicture::AccelData* data = nullptr;
mtklein9db912c2015-05-19 11:11:26 -07001879 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1880 data = bp->accelData();
1881 }
robertphillips81f71b62014-11-11 04:54:49 -08001882 if (!data) {
1883 return false;
1884 }
1885
robertphillipse5524cd2015-02-20 12:30:26 -08001886 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1887 if (0 == gpuData->numBlocks()) {
1888 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001889 }
1890
robertphillipsfd61ed02014-10-28 07:21:44 -07001891 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001892
robertphillipse5524cd2015-02-20 12:30:26 -08001893 SkIRect iBounds;
1894 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1895 return false;
1896 }
1897
1898 SkRect clipBounds = SkRect::Make(iBounds);
1899
1900 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1901
robertphillips60029a52015-11-09 13:51:06 -08001902 GrLayerHoister::Begin(fContext);
1903
robertphillipsfd61ed02014-10-28 07:21:44 -07001904 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001905 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001906 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001907 &atlasedNeedRendering, &atlasedRecycled,
vbuzinovdded6962015-06-12 08:59:45 -07001908 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001909
1910 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1911
1912 SkTDArray<GrHoistedLayer> needRendering, recycled;
1913
robertphillipse5524cd2015-02-20 12:30:26 -08001914 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1915
robertphillipsfd61ed02014-10-28 07:21:44 -07001916 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001917 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001918 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001919 &needRendering, &recycled,
vbuzinovdded6962015-06-12 08:59:45 -07001920 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001921
1922 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001923
robertphillips64bf7672014-08-21 13:07:35 -07001924 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001925 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
halcanary96fcdcc2015-08-27 07:41:13 -07001926 initialMatrix, nullptr);
robertphillips64bf7672014-08-21 13:07:35 -07001927
robertphillipsfd61ed02014-10-28 07:21:44 -07001928 GrLayerHoister::UnlockLayers(fContext, needRendering);
1929 GrLayerHoister::UnlockLayers(fContext, recycled);
1930 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1931 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips60029a52015-11-09 13:51:06 -08001932 GrLayerHoister::End(fContext);
robertphillips64bf7672014-08-21 13:07:35 -07001933
1934 return true;
robertphillips63242d72014-12-04 08:31:02 -08001935#else
1936 return false;
1937#endif
robertphillips64bf7672014-08-21 13:07:35 -07001938}
1939
reed13ccbf82015-10-20 09:56:52 -07001940SkImageFilter::Cache* SkGpuDevice::NewImageFilterCache() {
1941 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
1942}
1943
senorblancobe129b22014-08-08 07:14:35 -07001944SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001945 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001946 // We always return a transient cache, so it is freed after each
1947 // filter traversal.
reed13ccbf82015-10-20 09:56:52 -07001948 return SkGpuDevice::NewImageFilterCache();
senorblanco55b6d8b2014-07-30 11:26:46 -07001949}
reedf037e0b2014-10-30 11:34:15 -07001950
1951#endif