blob: d0c526b1700aab2727466da756410591d8c17e99 [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;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700399 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700400 return;
401 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000402
robertphillips2e1e51f2015-10-15 08:01:48 -0700403 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000404}
405
406// must be in SkCanvas::PointMode order
407static const GrPrimitiveType gPointMode2PrimtiveType[] = {
408 kPoints_GrPrimitiveType,
409 kLines_GrPrimitiveType,
410 kLineStrip_GrPrimitiveType
411};
412
ethannicholas330bb952015-07-17 06:44:02 -0700413// suppress antialiasing on axis-aligned integer-coordinate lines
414static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
415 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
416 return false;
417 }
418 if (count == 2) {
419 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
420 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800421 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700422 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800423 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700424 // the two end pixels as being the lesser of two evils.
425 if (pts[0].fX == pts[1].fX) {
426 return ((int) pts[0].fX) != pts[0].fX;
427 }
428 if (pts[0].fY == pts[1].fY) {
429 return ((int) pts[0].fY) != pts[0].fY;
430 }
431 }
432 return true;
433}
434
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000435void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
436 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800437 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800438 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800439 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000440
441 SkScalar width = paint.getStrokeWidth();
442 if (width < 0) {
443 return;
444 }
445
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000446 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700447 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
448 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700449 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700450 return;
451 }
egdaniele61c4112014-06-12 10:24:21 -0700452 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700453 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700454 path.moveTo(pts[0]);
455 path.lineTo(pts[1]);
robertphillips2e1e51f2015-10-15 08:01:48 -0700456 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700457 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000458 }
459
ethannicholas330bb952015-07-17 06:44:02 -0700460 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000461 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800462 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700463 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000464 draw.drawPoints(mode, count, pts, paint, true);
465 return;
466 }
467
468 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700469 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700470 return;
471 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000472
robertphillips2e1e51f2015-10-15 08:01:48 -0700473 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700474 grPaint,
475 *draw.fMatrix,
476 gPointMode2PrimtiveType[mode],
477 SkToS32(count),
478 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700479 nullptr,
480 nullptr,
481 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700482 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000483}
484
485///////////////////////////////////////////////////////////////////////////////
486
robertphillipsff55b492015-11-24 07:56:59 -0800487void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800488 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800489 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800490 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000491
492 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
493 SkScalar width = paint.getStrokeWidth();
494
495 /*
496 We have special code for hairline strokes, miter-strokes, bevel-stroke
497 and fills. Anything else we just call our path code.
498 */
499 bool usePath = doStroke && width > 0 &&
500 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
501 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
egdanield58a0ba2014-06-11 10:30:05 -0700502
robertphillipsd8aa59d2015-08-05 09:07:12 -0700503 // a few other reasons we might need to call drawPath...
robertphillipsff55b492015-11-24 07:56:59 -0800504 if (paint.getMaskFilter() || paint.getPathEffect() ||
robertphillipsd8aa59d2015-08-05 09:07:12 -0700505 paint.getStyle() == SkPaint::kStrokeAndFill_Style) { // we can't both stroke and fill rects
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000506 usePath = true;
507 }
egdanield58a0ba2014-06-11 10:30:05 -0700508
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000509 if (usePath) {
510 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700511 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000512 path.addRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800513 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800514 fClip, path, paint,
515 *draw.fMatrix, nullptr,
516 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000517 return;
518 }
519
520 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700521 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700522 return;
523 }
Mike Klein744fb732014-06-23 15:13:26 -0400524
robertphillipsff55b492015-11-24 07:56:59 -0800525 GrStrokeInfo strokeInfo(paint);
526
robertphillips2e1e51f2015-10-15 08:01:48 -0700527 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000528}
529
530///////////////////////////////////////////////////////////////////////////////
531
532void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800533 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800534 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800535 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800536 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000537
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000538 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700539 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700540 return;
541 }
Mike Klein744fb732014-06-23 15:13:26 -0400542
egdanield58a0ba2014-06-11 10:30:05 -0700543 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000544 if (paint.getMaskFilter()) {
545 // try to hit the fast path for drawing filtered round rects
546
547 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800548 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000549 if (devRRect.allCornersCircular()) {
550 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700551 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
joshualitt5531d512014-12-17 15:50:11 -0800552 draw.fClip->getBounds(),
553 *draw.fMatrix,
554 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000555 SkIRect finalIRect;
556 maskRect.roundOut(&finalIRect);
557 if (draw.fClip->quickReject(finalIRect)) {
558 // clipped out
559 return;
560 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700561 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
562 fDrawContext,
joshualitt25d9c152015-02-18 12:29:52 -0800563 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800564 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800565 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700566 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700567 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000568 return;
569 }
570 }
571
572 }
573 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000574 }
575
robertphillips514450c2015-11-24 05:36:02 -0800576 if (paint.getMaskFilter() || paint.getPathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800577 // The only mask filter the native rrect drawing code could've handle was taken
578 // care of above.
579 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000580 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700581 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000582 path.addRRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800583 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800584 fClip, path, paint,
585 *draw.fMatrix, nullptr,
586 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000587 return;
588 }
Mike Klein744fb732014-06-23 15:13:26 -0400589
robertphillips514450c2015-11-24 05:36:02 -0800590 SkASSERT(!strokeInfo.isDashed());
591
robertphillips2e1e51f2015-10-15 08:01:48 -0700592 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000593}
594
robertphillipsd7706102016-02-25 09:28:08 -0800595
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000596void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800597 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800598 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800599 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800600 CHECK_SHOULD_DRAW(draw);
601
robertphillipsd7706102016-02-25 09:28:08 -0800602 if (outer.isEmpty()) {
603 return;
604 }
605
606 if (inner.isEmpty()) {
607 return this->drawRRect(draw, outer, paint);
608 }
609
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000610 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000611
robertphillips0e7029e2015-11-30 05:45:06 -0800612 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800613 GrPaint grPaint;
614 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700615 return;
616 }
robertphillips00095892016-02-29 13:50:40 -0800617
618 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
619 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000620 }
621
622 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700623 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000624 path.addRRect(outer);
625 path.addRRect(inner);
626 path.setFillType(SkPath::kEvenOdd_FillType);
627
robertphillips7bceedc2015-12-01 12:51:26 -0800628 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillips0e7029e2015-11-30 05:45:06 -0800629 fClip, path, paint,
630 *draw.fMatrix, nullptr,
631 draw.fClip->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000632}
633
634
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000635/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000636
robertphillips514450c2015-11-24 05:36:02 -0800637void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800638 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800639 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800640 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000641
robertphillips514450c2015-11-24 05:36:02 -0800642 // Presumably the path effect warps this to something other than an oval
643 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000644 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700645 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000646 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700647 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000648 return;
herb11a7f7f2015-11-24 12:41:00 -0800649 }
650
robertphillips514450c2015-11-24 05:36:02 -0800651 if (paint.getMaskFilter()) {
652 // The RRect path can handle special case blurring
653 SkRRect rr = SkRRect::MakeOval(oval);
654 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000655 }
656
657 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700658 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700659 return;
660 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000661
robertphillips514450c2015-11-24 05:36:02 -0800662 GrStrokeInfo strokeInfo(paint);
663 SkASSERT(!strokeInfo.isDashed());
664
robertphillips2e1e51f2015-10-15 08:01:48 -0700665 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000666}
667
668#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000669
670///////////////////////////////////////////////////////////////////////////////
671
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000672void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
673 const SkPaint& paint, const SkMatrix* prePathMatrix,
674 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800675 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800676 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
677 bool isClosed;
678 SkRect rect;
679 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
680 this->drawRect(draw, rect, paint);
681 return;
682 }
683 if (origSrcPath.isOval(&rect)) {
684 this->drawOval(draw, rect, paint);
685 return;
686 }
687 SkRRect rrect;
688 if (origSrcPath.isRRect(&rrect)) {
689 this->drawRRect(draw, rrect, paint);
690 return;
691 }
692 }
693
joshualitt5531d512014-12-17 15:50:11 -0800694 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800695 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000696
robertphillips7bceedc2015-12-01 12:51:26 -0800697 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsccb1b572015-05-27 11:02:55 -0700698 fClip, origSrcPath, paint,
699 *draw.fMatrix, prePathMatrix,
700 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000701}
702
703static const int kBmpSmallTileSize = 1 << 10;
704
705static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
706 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
707 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
708 return tilesX * tilesY;
709}
710
reed85d91782015-09-10 14:33:38 -0700711static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000712 if (maxTileSize <= kBmpSmallTileSize) {
713 return maxTileSize;
714 }
715
716 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
717 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
718
719 maxTileTotalTileSize *= maxTileSize * maxTileSize;
720 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
721
722 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
723 return kBmpSmallTileSize;
724 } else {
725 return maxTileSize;
726 }
727}
728
729// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
730// pixels from the bitmap are necessary.
bsalomon74f681d2015-06-23 14:38:48 -0700731static void determine_clipped_src_rect(const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800732 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800733 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700734 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000735 const SkRect* srcRectPtr,
736 SkIRect* clippedSrcIRect) {
robertphillips7bceedc2015-12-01 12:51:26 -0800737 clip.getConservativeBounds(rt->width(), rt->height(), clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000738 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800739 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000740 clippedSrcIRect->setEmpty();
741 return;
742 }
743 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
744 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700745 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000746 // we've setup src space 0,0 to map to the top left of the src rect.
747 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000748 if (!clippedSrcRect.intersect(*srcRectPtr)) {
749 clippedSrcIRect->setEmpty();
750 return;
751 }
752 }
753 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700754 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000755 if (!clippedSrcIRect->intersect(bmpBounds)) {
756 clippedSrcIRect->setEmpty();
757 }
758}
759
reed85d91782015-09-10 14:33:38 -0700760bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
761 const SkMatrix& viewMatrix,
762 const GrTextureParams& params,
763 const SkRect* srcRectPtr,
764 int maxTileSize,
765 int* tileSize,
766 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800767 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700768 // if it's larger than the max tile size, then we have no choice but tiling.
769 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
770 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(),
771 srcRectPtr, clippedSubset);
772 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
773 return true;
774 }
775
bsalomon1a1d0b82015-10-16 07:49:42 -0700776 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700777 const size_t area = imageRect.width() * imageRect.height();
778 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
779 return false;
780 }
781
reed85d91782015-09-10 14:33:38 -0700782 // At this point we know we could do the draw by uploading the entire bitmap
783 // as a texture. However, if the texture would be large compared to the
784 // cache size and we don't require most of it for this draw then tile to
785 // reduce the amount of upload and cache spill.
786
787 // assumption here is that sw bitmap size is a good proxy for its size as
788 // a texture
789 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
790 size_t cacheSize;
791 fContext->getResourceCacheLimits(nullptr, &cacheSize);
792 if (bmpSize < cacheSize / 2) {
793 return false;
794 }
795
bsalomon1a1d0b82015-10-16 07:49:42 -0700796 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
797 // tiling memory savings would be < 50%.
reed85d91782015-09-10 14:33:38 -0700798 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(), srcRectPtr,
799 clippedSubset);
800 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
801 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
802 kBmpSmallTileSize * kBmpSmallTileSize;
803
804 return usedTileBytes < 2 * bmpSize;
805}
806
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000807bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800808 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000809 const GrTextureParams& params,
810 const SkRect* srcRectPtr,
811 int maxTileSize,
812 int* tileSize,
813 SkIRect* clippedSrcRect) const {
joshualittce894002016-01-11 13:29:31 -0800814 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000815 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700816 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000817 return false;
818 }
819
reed85d91782015-09-10 14:33:38 -0700820 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
821 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
822}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000823
reed85d91782015-09-10 14:33:38 -0700824bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
825 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
826 const SkMatrix& viewMatrix) const {
joshualittce894002016-01-11 13:29:31 -0800827 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700828 // if image is explictly texture backed then just use the texture
829 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000830 return false;
831 }
832
reed85d91782015-09-10 14:33:38 -0700833 GrTextureParams params;
834 bool doBicubic;
835 GrTextureParams::FilterMode textureFilterMode =
836 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
837
838 int tileFilterPad;
839 if (doBicubic) {
840 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
841 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
842 tileFilterPad = 0;
843 } else {
844 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000845 }
reed85d91782015-09-10 14:33:38 -0700846 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000847
bsalomon8c07b7a2015-11-02 11:36:52 -0800848 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000849
reed85d91782015-09-10 14:33:38 -0700850 // these are output, which we safely ignore, as we just want to know the predicate
851 int outTileSize;
852 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000853
reed85d91782015-09-10 14:33:38 -0700854 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
855 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000856}
857
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000858void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000859 const SkBitmap& bitmap,
860 const SkMatrix& m,
861 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800862 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800863 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800864 SkMatrix viewMatrix;
865 viewMatrix.setConcat(*origDraw.fMatrix, m);
866 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800867 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonb1b01992015-11-18 10:56:08 -0800868 // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
bsalomonf1ecd212015-12-09 17:06:02 -0800869 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
870 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800871 return;
872 }
bsalomonb1b01992015-11-18 10:56:08 -0800873 int maxTileSize = fContext->caps()->maxTileSize();
874
875 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
876 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
877 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
878 paint.isAntiAlias() &&
879 bitmap.width() <= maxTileSize &&
880 bitmap.height() <= maxTileSize;
881
882 bool skipTileCheck = drawAA || paint.getMaskFilter();
883
884 if (!skipTileCheck) {
885 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
886 int tileSize;
887 SkIRect clippedSrcRect;
888
889 GrTextureParams params;
890 bool doBicubic;
891 GrTextureParams::FilterMode textureFilterMode =
892 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
893 &doBicubic);
894
895 int tileFilterPad;
896
897 if (doBicubic) {
898 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
899 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
900 tileFilterPad = 0;
901 } else {
902 tileFilterPad = 1;
903 }
904 params.setFilterMode(textureFilterMode);
905
906 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
907 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
908 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
909 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
910 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
911 return;
912 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000913 }
bsalomonb1b01992015-11-18 10:56:08 -0800914 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800915 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
916 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000917}
918
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000919// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000920// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
921// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000922static inline void clamped_outset_with_offset(SkIRect* iRect,
923 int outset,
924 SkPoint* offset,
925 const SkIRect& clamp) {
926 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000927
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000928 int leftClampDelta = clamp.fLeft - iRect->fLeft;
929 if (leftClampDelta > 0) {
930 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000931 iRect->fLeft = clamp.fLeft;
932 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000933 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000934 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000935
936 int topClampDelta = clamp.fTop - iRect->fTop;
937 if (topClampDelta > 0) {
938 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000939 iRect->fTop = clamp.fTop;
940 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000941 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000942 }
943
944 if (iRect->fRight > clamp.fRight) {
945 iRect->fRight = clamp.fRight;
946 }
947 if (iRect->fBottom > clamp.fBottom) {
948 iRect->fBottom = clamp.fBottom;
949 }
950}
951
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000952// Break 'bitmap' into several tiles to draw it since it has already
953// been determined to be too large to fit in VRAM
954void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800955 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000956 const SkRect& srcRect,
957 const SkIRect& clippedSrcIRect,
958 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800959 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700960 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000961 int tileSize,
962 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800963 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800964
ericrk82e26bf2016-02-25 22:15:29 -0800965 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entry.
ericrk369e9372016-02-05 15:32:36 -0800966 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
967
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000968 // The following pixel lock is technically redundant, but it is desirable
969 // to lock outside of the tile loop to prevent redecoding the whole image
970 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
971 // is larger than the limit of the discardable memory pool.
972 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800973
974 const SkPaint* paint = &origPaint;
975 SkPaint tempPaint;
976 if (origPaint.isAntiAlias() && !fRenderTarget->isUnifiedMultisampled()) {
977 // Drop antialiasing to avoid seams at tile boundaries.
978 tempPaint = origPaint;
979 tempPaint.setAntiAlias(false);
980 paint = &tempPaint;
981 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
983
984 int nx = bitmap.width() / tileSize;
985 int ny = bitmap.height() / tileSize;
986 for (int x = 0; x <= nx; x++) {
987 for (int y = 0; y <= ny; y++) {
988 SkRect tileR;
989 tileR.set(SkIntToScalar(x * tileSize),
990 SkIntToScalar(y * tileSize),
991 SkIntToScalar((x + 1) * tileSize),
992 SkIntToScalar((y + 1) * tileSize));
993
994 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
995 continue;
996 }
997
998 if (!tileR.intersect(srcRect)) {
999 continue;
1000 }
1001
1002 SkBitmap tmpB;
1003 SkIRect iTileR;
1004 tileR.roundOut(&iTileR);
1005 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1006 SkIntToScalar(iTileR.fTop));
1007
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001008 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001009 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001010 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001011 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001012 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001013
robertphillipsec8bb942014-11-21 10:16:25 -08001014 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001015 SkIRect iClampRect;
1016
reeda5517e22015-07-14 10:54:12 -07001017 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001018 // In bleed mode we want to always expand the tile on all edges
1019 // but stay within the bitmap bounds
1020 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1021 } else {
1022 // In texture-domain/clamp mode we only want to expand the
1023 // tile on edges interior to "srcRect" (i.e., we want to
1024 // not bleed across the original clamped edges)
1025 srcRect.roundOut(&iClampRect);
1026 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001027 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1028 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001029 }
1030
1031 if (bitmap.extractSubset(&tmpB, iTileR)) {
1032 // now offset it to make it "local" to our tmp bitmap
1033 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001034 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001035 // de-optimized this determination
1036 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001037 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001038 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001039 tileR,
1040 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -08001041 *paint,
reeda5517e22015-07-14 10:54:12 -07001042 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001043 bicubic,
1044 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045 }
1046 }
1047 }
1048}
1049
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001050/*
1051 * This is called by drawBitmap(), which has to handle images that may be too
1052 * large to be represented by a single texture.
1053 *
1054 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1055 * and that non-texture portion of the GrPaint has already been setup.
1056 */
1057void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001058 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001059 const SkRect& srcRect,
1060 const GrTextureParams& params,
1061 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001062 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001063 bool bicubic,
1064 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001065 // We should have already handled bitmaps larger than the max texture size.
1066 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1067 bitmap.height() <= fContext->caps()->maxTextureSize());
1068 // Unless the bitmap is inherently texture-backed, we should be respecting the max tile size
1069 // by the time we get here.
1070 SkASSERT(bitmap.getTexture() ||
1071 (bitmap.width() <= fContext->caps()->maxTileSize() &&
1072 bitmap.height() <= fContext->caps()->maxTileSize()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001073
1074 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001075 AutoBitmapTexture abt(fContext, bitmap, params, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001076 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001077 return;
1078 }
1079
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001080 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001081 SkRect paintRect;
1082 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1083 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1084 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1085 SkScalarMul(srcRect.fTop, hInv),
1086 SkScalarMul(srcRect.fRight, wInv),
1087 SkScalarMul(srcRect.fBottom, hInv));
1088
egdaniel79da63f2015-10-09 10:55:16 -07001089 SkMatrix texMatrix;
1090 texMatrix.reset();
1091 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1092 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1093 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1094 // Thus we need to pass in the transform matrix directly to the texture processor used for
1095 // the bitmap draw.
1096 texMatrix.setScale(wInv, hInv);
1097 }
1098
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001099 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001100
1101 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1102 // the rest from the SkPaint.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001103 SkAutoTUnref<const GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001104
reeda5517e22015-07-14 10:54:12 -07001105 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001106 // Use a constrained texture domain to avoid color bleeding
1107 SkScalar left, top, right, bottom;
1108 if (srcRect.width() > SK_Scalar1) {
1109 SkScalar border = SK_ScalarHalf / texture->width();
1110 left = paintRect.left() + border;
1111 right = paintRect.right() - border;
1112 } else {
1113 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1114 }
1115 if (srcRect.height() > SK_Scalar1) {
1116 SkScalar border = SK_ScalarHalf / texture->height();
1117 top = paintRect.top() + border;
1118 bottom = paintRect.bottom() - border;
1119 } else {
1120 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1121 }
1122 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001123 if (bicubic) {
egdaniel79da63f2015-10-09 10:55:16 -07001124 fp.reset(GrBicubicEffect::Create(texture, texMatrix, textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001125 } else {
bsalomon4a339522015-10-06 08:40:50 -07001126 fp.reset(GrTextureDomainEffect::Create(texture,
egdaniel79da63f2015-10-09 10:55:16 -07001127 texMatrix,
joshualitt5531d512014-12-17 15:50:11 -08001128 textureDomain,
1129 GrTextureDomain::kClamp_Mode,
1130 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001131 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001132 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001133 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1134 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
egdaniel79da63f2015-10-09 10:55:16 -07001135 fp.reset(GrBicubicEffect::Create(texture, texMatrix, tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001136 } else {
egdaniel79da63f2015-10-09 10:55:16 -07001137 fp.reset(GrSimpleTextureEffect::Create(texture, texMatrix, params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001138 }
1139
joshualitt33a5fce2015-11-18 13:28:51 -08001140 GrPaint grPaint;
1141 if (!SkPaintToGrPaintWithTexture(this->context(), paint, viewMatrix, fp,
1142 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001143 return;
1144 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001145
egdaniel79da63f2015-10-09 10:55:16 -07001146 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1147 // We don't have local coords in this case and have previously set the transform
1148 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001149 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001150 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001151 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001152 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001153}
1154
fmalita2d97bc12014-11-20 10:44:58 -08001155bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001156 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001157 const SkImageFilter* filter,
1158 const SkImageFilter::Context& ctx,
1159 SkBitmap* result, SkIPoint* offset) {
joshualittce894002016-01-11 13:29:31 -08001160 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001161 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001162
reed88d064d2015-10-12 11:30:02 -07001163 SkImageFilter::DeviceProxy proxy(this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001164
1165 if (filter->canFilterImageGPU()) {
robertphillips1de87df2016-01-14 06:03:29 -08001166 SkBitmap bm;
1167 GrWrapTextureInBitmap(texture, width, height, false, &bm);
robertphillips48e78462016-02-17 13:57:16 -08001168 return filter->filterImageGPUDeprecated(&proxy, bm, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001169 } else {
1170 return false;
1171 }
1172}
1173
1174void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1175 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001176 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001177 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001178 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001179
1180 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1181 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1182 return;
1183 }
1184
robertphillipse1849d12016-03-17 08:26:38 -07001185 int offX = bitmap.pixelRefOrigin().fX;
1186 int offY = bitmap.pixelRefOrigin().fY;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001187 int w = bitmap.width();
1188 int h = bitmap.height();
1189
1190 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001191 // draw sprite neither filters nor tiles.
1192 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001193 if (!texture) {
1194 return;
1195 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001196
bsalomonf1b7a1d2015-09-28 06:26:28 -07001197 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
1198
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001199 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001200 // This bitmap will own the filtered result as a texture.
1201 SkBitmap filteredBitmap;
1202
bsalomon49f085d2014-09-05 13:34:00 -07001203 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001204 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001205 SkMatrix matrix(*draw.fMatrix);
1206 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblancodb64af32015-12-09 10:11:43 -08001207 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-left, -top);
senorblancobe129b22014-08-08 07:14:35 -07001208 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001209 // This cache is transient, and is freed (along with all its contained
1210 // textures) when it goes out of scope.
reed4e23cda2016-01-11 10:56:59 -08001211 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001212 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001213 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001214 texture = (GrTexture*) filteredBitmap.getTexture();
robertphillipse1849d12016-03-17 08:26:38 -07001215 offX = filteredBitmap.pixelRefOrigin().fX;
1216 offY = filteredBitmap.pixelRefOrigin().fY;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001217 w = filteredBitmap.width();
1218 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001219 left += offset.x();
1220 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001221 } else {
1222 return;
1223 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001224 SkASSERT(!GrPixelConfigIsAlphaOnly(texture->config()));
1225 alphaOnly = false;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001226 }
1227
1228 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001229 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001230 GrSimpleTextureEffect::Create(texture, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001231 if (alphaOnly) {
1232 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1233 } else {
1234 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1235 }
1236 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001237 return;
1238 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001239
bsalomona2e69fc2015-11-05 10:41:43 -08001240 fDrawContext->fillRectToRect(fClip,
1241 grPaint,
1242 SkMatrix::I(),
1243 SkRect::MakeXYWH(SkIntToScalar(left),
1244 SkIntToScalar(top),
1245 SkIntToScalar(w),
1246 SkIntToScalar(h)),
robertphillipse1849d12016-03-17 08:26:38 -07001247 SkRect::MakeXYWH(SkIntToScalar(offX) / texture->width(),
1248 SkIntToScalar(offY) / texture->height(),
1249 SkIntToScalar(w) / texture->width(),
1250 SkIntToScalar(h) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001251}
1252
bsalomonb1b01992015-11-18 10:56:08 -08001253void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1254 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001255 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001256 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001257 CHECK_SHOULD_DRAW(draw);
bsalomonb1b01992015-11-18 10:56:08 -08001258 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001259 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001260 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
1261 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001262 return;
1263 }
bsalomonb1b01992015-11-18 10:56:08 -08001264 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1265 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1266 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1267 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1268 const SkRect* dst = &origDst;
1269 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001270 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001271 if (!src) {
1272 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001273 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001274
bsalomonb1b01992015-11-18 10:56:08 -08001275 SkMatrix srcToDstMatrix;
1276 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1277 return;
1278 }
1279 SkRect tmpSrc, tmpDst;
1280 if (src != &bmpBounds) {
1281 if (!bmpBounds.contains(*src)) {
1282 tmpSrc = *src;
1283 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001284 return; // nothing to draw
1285 }
bsalomonb1b01992015-11-18 10:56:08 -08001286 src = &tmpSrc;
1287 srcToDstMatrix.mapRect(&tmpDst, *src);
1288 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001289 }
1290 }
1291
bsalomonb1b01992015-11-18 10:56:08 -08001292 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001293
bsalomonb1b01992015-11-18 10:56:08 -08001294 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1295 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
1296 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
1297 paint.isAntiAlias() &&
1298 bitmap.width() <= maxTileSize &&
1299 bitmap.height() <= maxTileSize;
1300
1301 bool skipTileCheck = drawAA || paint.getMaskFilter();
1302
1303 if (!skipTileCheck) {
1304 int tileSize;
1305 SkIRect clippedSrcRect;
1306
1307 GrTextureParams params;
1308 bool doBicubic;
1309 GrTextureParams::FilterMode textureFilterMode =
1310 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1311 &doBicubic);
1312
1313 int tileFilterPad;
1314
1315 if (doBicubic) {
1316 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1317 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1318 tileFilterPad = 0;
1319 } else {
1320 tileFilterPad = 1;
1321 }
1322 params.setFilterMode(textureFilterMode);
1323
1324 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1325 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1326 // we have a mask filter.
1327 SkMatrix viewMatrix = *draw.fMatrix;
1328 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1329 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1330 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1331 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1332 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1333 constraint, tileSize, doBicubic);
1334 return;
1335 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001336 }
bsalomonb1b01992015-11-18 10:56:08 -08001337 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001338 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001339}
1340
1341void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1342 int x, int y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001343 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001344 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001345 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001346 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001347
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001348 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001349 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001350
1351 GrRenderTarget* devRT = dev->accessRenderTarget();
1352 GrTexture* devTex;
halcanary96fcdcc2015-08-27 07:41:13 -07001353 if (nullptr == (devTex = devRT->asTexture())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001354 return;
1355 }
1356
robertphillips7b9e8a42014-12-11 08:20:31 -08001357 const SkImageInfo ii = dev->imageInfo();
1358 int w = ii.width();
1359 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001360
1361 SkImageFilter* filter = paint.getImageFilter();
1362 // This bitmap will own the filtered result as a texture.
1363 SkBitmap filteredBitmap;
1364
bsalomon49f085d2014-09-05 13:34:00 -07001365 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001366 SkIPoint offset = SkIPoint::Make(0, 0);
1367 SkMatrix matrix(*draw.fMatrix);
1368 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblancodb64af32015-12-09 10:11:43 -08001369 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-x, -y);
senorblanco55b6d8b2014-07-30 11:26:46 -07001370 // This cache is transient, and is freed (along with all its contained
1371 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001372 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
reed4e23cda2016-01-11 10:56:59 -08001373 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001374 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1375 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001376 devTex = filteredBitmap.getTexture();
1377 w = filteredBitmap.width();
1378 h = filteredBitmap.height();
1379 x += offset.fX;
1380 y += offset.fY;
1381 } else {
1382 return;
1383 }
1384 }
1385
1386 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001387 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001388 GrSimpleTextureEffect::Create(devTex, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001389 if (GrPixelConfigIsAlphaOnly(devTex->config())) {
1390 // Can this happen?
1391 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1392 } else {
1393 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1394 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001395
bsalomonf1b7a1d2015-09-28 06:26:28 -07001396 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001397 return;
1398 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001399
1400 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1401 SkIntToScalar(y),
1402 SkIntToScalar(w),
1403 SkIntToScalar(h));
1404
1405 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1406 // scratch texture).
1407 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1408 SK_Scalar1 * h / devTex->height());
1409
bsalomona2e69fc2015-11-05 10:41:43 -08001410 fDrawContext->fillRectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001411}
1412
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001413bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
joshualittce894002016-01-11 13:29:31 -08001414 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001415 return filter->canFilterImageGPU();
1416}
1417
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001418bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001419 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001420 SkBitmap* result, SkIPoint* offset) {
joshualittce894002016-01-11 13:29:31 -08001421 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001422 // want explicitly our impl, so guard against a subclass of us overriding it
1423 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1424 return false;
1425 }
1426
1427 SkAutoLockPixels alp(src, !src.getTexture());
1428 if (!src.getTexture() && !src.readyToDraw()) {
1429 return false;
1430 }
1431
1432 GrTexture* texture;
1433 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1434 // must be pushed upstack.
bsalomonafa95e22015-10-12 10:39:46 -07001435 AutoBitmapTexture abt(fContext, src, GrTextureParams::ClampNoFilter(), &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001436 if (!texture) {
1437 return false;
1438 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001439
senorblancod0d37ca2015-04-02 04:54:56 -07001440 return this->filterTexture(fContext, texture, src.width(), src.height(),
1441 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001442}
1443
reeda85d4d02015-05-06 12:56:48 -07001444void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1445 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001446 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001447 SkMatrix viewMatrix = *draw.fMatrix;
1448 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001449 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001450 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001451 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001452 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1453 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001454 return;
reed85d91782015-09-10 14:33:38 -07001455 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001456 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001457 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1458 paint.getFilterQuality(), *draw.fMatrix)) {
1459 // only support tiling as bitmap at the moment, so force raster-version
1460 if (!as_IB(image)->getROPixels(&bm)) {
1461 return;
1462 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001463 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1464 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1465 CHECK_SHOULD_DRAW(draw);
1466 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001467 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1468 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001469 } else if (as_IB(image)->getROPixels(&bm)) {
1470 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001471 }
reeda85d4d02015-05-06 12:56:48 -07001472 }
1473}
1474
1475void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001476 const SkRect& dst, const SkPaint& paint,
1477 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001478 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001479 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001480 CHECK_SHOULD_DRAW(draw);
1481 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001482 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001483 return;
1484 }
1485 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001486 SkMatrix totalMatrix = *draw.fMatrix;
1487 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1488 dst.height() / (src ? src->height() : image->height()));
1489 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001490 // only support tiling as bitmap at the moment, so force raster-version
1491 if (!as_IB(image)->getROPixels(&bm)) {
1492 return;
1493 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001494 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1495 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1496 CHECK_SHOULD_DRAW(draw);
1497 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001498 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001499 } else if (as_IB(image)->getROPixels(&bm)) {
1500 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001501 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001502}
1503
bsalomonf1ecd212015-12-09 17:06:02 -08001504void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001505 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001506 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001507
joshualitt33a5fce2015-11-18 13:28:51 -08001508 CHECK_SHOULD_DRAW(draw);
1509
joshualittedb36442015-11-19 14:29:30 -08001510 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
1511 fRenderTarget->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001512 bool doBicubic;
1513 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001514 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1515 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001516 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001517 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001518
1519 SkRect srcR, dstR;
1520 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001521 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001522 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001523 }
1524 return;
1525 }
1526
bsalomon2bbd0c62015-12-09 12:50:56 -08001527 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
1528 SkAutoTUnref<const GrFragmentProcessor> fp(
1529 producer->createFragmentProcessor(SkMatrix::I(),
1530 SkRect::MakeIWH(producer->width(), producer->height()),
1531 GrTextureProducer::kNo_FilterConstraint, true,
1532 &kMode));
joshualitt33a5fce2015-11-18 13:28:51 -08001533 GrPaint grPaint;
1534 if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, fp,
bsalomonf1ecd212015-12-09 17:06:02 -08001535 producer->isAlphaOnly(), &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001536 return;
1537 }
1538
bsalomon2bbd0c62015-12-09 12:50:56 -08001539 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1540 producer->height(), center, dst);
1541}
1542
1543void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1544 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001545 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001546 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001547 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001548 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001549 } else {
1550 SkBitmap bm;
1551 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1552 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001553 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001554 } else if (as_IB(image)->getROPixels(&bm)) {
1555 this->drawBitmapNine(draw, bm, center, dst, paint);
1556 }
1557 }
1558}
1559
1560void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1561 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001562 ASSERT_SINGLE_OWNER
bsalomon2bbd0c62015-12-09 12:50:56 -08001563 if (bitmap.getTexture()) {
1564 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001565 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001566 } else {
1567 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001568 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001569 }
joshualitt33a5fce2015-11-18 13:28:51 -08001570}
1571
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001572///////////////////////////////////////////////////////////////////////////////
1573
1574// must be in SkCanvas::VertexMode order
1575static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1576 kTriangles_GrPrimitiveType,
1577 kTriangleStrip_GrPrimitiveType,
1578 kTriangleFan_GrPrimitiveType,
1579};
1580
1581void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1582 int vertexCount, const SkPoint vertices[],
1583 const SkPoint texs[], const SkColor colors[],
1584 SkXfermode* xmode,
1585 const uint16_t indices[], int indexCount,
1586 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001587 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001588 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001589 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001590
halcanary96fcdcc2015-08-27 07:41:13 -07001591 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1592 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001593
halcanary96fcdcc2015-08-27 07:41:13 -07001594 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001595
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001596 SkPaint copy(paint);
1597 copy.setStyle(SkPaint::kStroke_Style);
1598 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001599
bsalomonf1b7a1d2015-09-28 06:26:28 -07001600 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001601 // we ignore the shader if texs is null.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001602 if (!SkPaintToGrPaintNoShader(this->context(), copy, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001603 return;
1604 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001605
dandov32a311b2014-07-15 19:46:26 -07001606 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001607 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001608 switch (vmode) {
1609 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001610 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001611 break;
1612 case SkCanvas::kTriangleStrip_VertexMode:
1613 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001614 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001615 break;
1616 }
mtklein533eb782014-08-27 10:39:42 -07001617
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001618 VertState state(vertexCount, indices, indexCount);
1619 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001620
dandov32a311b2014-07-15 19:46:26 -07001621 //number of indices for lines per triangle with kLines
1622 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001623
bsalomonf1b7a1d2015-09-28 06:26:28 -07001624 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001625 int i = 0;
1626 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001627 lineIndices[i] = state.f0;
1628 lineIndices[i + 1] = state.f1;
1629 lineIndices[i + 2] = state.f1;
1630 lineIndices[i + 3] = state.f2;
1631 lineIndices[i + 4] = state.f2;
1632 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001633 i += 6;
1634 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001635 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001636 grPaint,
1637 *draw.fMatrix,
1638 kLines_GrPrimitiveType,
1639 vertexCount,
1640 vertices,
1641 texs,
1642 colors,
1643 lineIndices.get(),
1644 indexCount);
1645 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001646 }
1647
bsalomonf1b7a1d2015-09-28 06:26:28 -07001648 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001649
1650 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001651 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001652 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1653 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001654 convertedColors.reset(vertexCount);
1655 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001656 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001657 }
1658 colors = convertedColors.get();
1659 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001660 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001661 if (texs && paint.getShader()) {
1662 if (colors) {
1663 // When there are texs and colors the shader and colors are combined using xmode. A null
1664 // xmode is defined to mean modulate.
1665 SkXfermode::Mode colorMode;
1666 if (xmode) {
1667 if (!xmode->asMode(&colorMode)) {
1668 return;
1669 }
1670 } else {
1671 colorMode = SkXfermode::kModulate_Mode;
1672 }
1673 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
1674 false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001675 return;
1676 }
1677 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001678 // We have a shader, but no colors to blend it against.
1679 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
1680 return;
1681 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001682 }
bsalomonaa48d362015-10-01 08:34:17 -07001683 } else {
1684 if (colors) {
1685 // We have colors, but either have no shader or no texture coords (which implies that
1686 // we should ignore the shader).
1687 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint, &grPaint)) {
1688 return;
1689 }
1690 } else {
1691 // No colors and no shaders. Just draw with the paint color.
1692 if (!SkPaintToGrPaintNoShader(this->context(), paint, &grPaint)) {
1693 return;
1694 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001695 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001696 }
1697
robertphillips2e1e51f2015-10-15 08:01:48 -07001698 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001699 grPaint,
1700 *draw.fMatrix,
1701 primType,
1702 vertexCount,
1703 vertices,
1704 texs,
1705 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001706 indices,
robertphillipsea461502015-05-26 11:38:03 -07001707 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001708}
1709
1710///////////////////////////////////////////////////////////////////////////////
1711
jvanverth31ff7622015-08-07 10:09:28 -07001712void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001713 const SkRect texRect[], const SkColor colors[], int count,
1714 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001715 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001716 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001717 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001718 return;
1719 }
1720
jvanverth31ff7622015-08-07 10:09:28 -07001721 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001722 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001723
reedca109532015-06-25 16:25:25 -07001724 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001725 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001726
jvanverth31ff7622015-08-07 10:09:28 -07001727 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001728 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001729 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
1730 &grPaint)) {
1731 return;
1732 }
1733 } else {
1734 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix, &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001735 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001736 }
1737 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001738
1739 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001740 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001741}
1742
1743///////////////////////////////////////////////////////////////////////////////
1744
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001745void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001746 size_t byteLength, SkScalar x, SkScalar y,
1747 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001748 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001749 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001750 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001751
jvanverth8c27a182014-10-14 08:45:50 -07001752 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001753 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001754 return;
1755 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001756
jvanverth8c27a182014-10-14 08:45:50 -07001757 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001758
robertphillips2e1e51f2015-10-15 08:01:48 -07001759 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001760 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001761}
1762
fmalita05c4a432014-09-29 06:29:53 -07001763void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1764 const SkScalar pos[], int scalarsPerPos,
1765 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001766 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001767 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001768 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001769
jvanverth8c27a182014-10-14 08:45:50 -07001770 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001771 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001772 return;
1773 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001774
jvanverth8c27a182014-10-14 08:45:50 -07001775 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001776
robertphillips2e1e51f2015-10-15 08:01:48 -07001777 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001778 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1779 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001780}
1781
joshualitt9c328182015-03-23 08:13:04 -07001782void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1783 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001784 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001785 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001786 CHECK_SHOULD_DRAW(draw);
1787
1788 SkDEBUGCODE(this->validate();)
1789
robertphillips2e1e51f2015-10-15 08:01:48 -07001790 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001791 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001792}
1793
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001794///////////////////////////////////////////////////////////////////////////////
1795
reedb2db8982014-11-13 12:41:02 -08001796bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001797 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001798}
1799
1800void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001801 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001802
bsalomonc49e8682015-06-30 11:37:35 -07001803 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001804}
1805
1806///////////////////////////////////////////////////////////////////////////////
1807
reed76033be2015-03-14 10:54:31 -07001808SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001809 ASSERT_SINGLE_OWNER
bsalomonf2703d82014-10-28 14:33:06 -07001810 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001811 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001812 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001813 desc.fWidth = cinfo.fInfo.width();
1814 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001815 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001816
1817 SkAutoTUnref<GrTexture> texture;
1818 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001819 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001820
hcm4396fa52014-10-27 21:43:30 -07001821 // layers are never draw in repeat modes, so we can request an approx
1822 // match and ignore any padding.
bsalomoneae62002015-07-31 13:59:30 -07001823 if (kNever_TileUsage == cinfo.fTileUsage) {
1824 texture.reset(fContext->textureProvider()->createApproxTexture(desc));
1825 } else {
bsalomon5ec26ae2016-02-25 08:33:02 -08001826 texture.reset(fContext->textureProvider()->createTexture(desc, SkBudgeted::kYes));
bsalomoneae62002015-07-31 13:59:30 -07001827 }
bsalomonafe30052015-01-16 07:32:33 -08001828
1829 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001830 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001831 return SkGpuDevice::Create(
bsalomon74f681d2015-06-23 14:38:48 -07001832 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, init);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001833 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001834 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001835 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001836 cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary96fcdcc2015-08-27 07:41:13 -07001837 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001838 }
1839}
1840
reede8f30622016-03-23 18:59:25 -07001841sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001842 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001843 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001844 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
reede8f30622016-03-23 18:59:25 -07001845 return SkSurface::MakeRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
1846 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001847}
1848
robertphillips30d2cc62014-09-24 08:52:18 -07001849bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001850 const SkMatrix* matrix, const SkPaint* paint) {
joshualittce894002016-01-11 13:29:31 -08001851 ASSERT_SINGLE_OWNER
robertphillips63242d72014-12-04 08:31:02 -08001852#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001853 // todo: should handle this natively
1854 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001855 return false;
1856 }
1857
halcanary96fcdcc2015-08-27 07:41:13 -07001858 const SkBigPicture::AccelData* data = nullptr;
mtklein9db912c2015-05-19 11:11:26 -07001859 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1860 data = bp->accelData();
1861 }
robertphillips81f71b62014-11-11 04:54:49 -08001862 if (!data) {
1863 return false;
1864 }
1865
robertphillipse5524cd2015-02-20 12:30:26 -08001866 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1867 if (0 == gpuData->numBlocks()) {
1868 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001869 }
1870
robertphillipsfd61ed02014-10-28 07:21:44 -07001871 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001872
robertphillipse5524cd2015-02-20 12:30:26 -08001873 SkIRect iBounds;
1874 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1875 return false;
1876 }
1877
1878 SkRect clipBounds = SkRect::Make(iBounds);
1879
1880 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1881
robertphillips60029a52015-11-09 13:51:06 -08001882 GrLayerHoister::Begin(fContext);
1883
robertphillipsfd61ed02014-10-28 07:21:44 -07001884 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001885 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001886 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001887 &atlasedNeedRendering, &atlasedRecycled,
vbuzinovdded6962015-06-12 08:59:45 -07001888 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001889
1890 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1891
1892 SkTDArray<GrHoistedLayer> needRendering, recycled;
1893
robertphillipse5524cd2015-02-20 12:30:26 -08001894 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1895
robertphillipsfd61ed02014-10-28 07:21:44 -07001896 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001897 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001898 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001899 &needRendering, &recycled,
vbuzinovdded6962015-06-12 08:59:45 -07001900 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001901
1902 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001903
robertphillips64bf7672014-08-21 13:07:35 -07001904 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001905 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
halcanary96fcdcc2015-08-27 07:41:13 -07001906 initialMatrix, nullptr);
robertphillips64bf7672014-08-21 13:07:35 -07001907
robertphillipsfd61ed02014-10-28 07:21:44 -07001908 GrLayerHoister::UnlockLayers(fContext, needRendering);
1909 GrLayerHoister::UnlockLayers(fContext, recycled);
1910 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1911 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips60029a52015-11-09 13:51:06 -08001912 GrLayerHoister::End(fContext);
robertphillips64bf7672014-08-21 13:07:35 -07001913
1914 return true;
robertphillips63242d72014-12-04 08:31:02 -08001915#else
1916 return false;
1917#endif
robertphillips64bf7672014-08-21 13:07:35 -07001918}
1919
reed13ccbf82015-10-20 09:56:52 -07001920SkImageFilter::Cache* SkGpuDevice::NewImageFilterCache() {
1921 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
1922}
1923
senorblancobe129b22014-08-08 07:14:35 -07001924SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001925 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001926 // We always return a transient cache, so it is freed after each
1927 // filter traversal.
reed13ccbf82015-10-20 09:56:52 -07001928 return SkGpuDevice::NewImageFilterCache();
senorblanco55b6d8b2014-07-30 11:26:46 -07001929}
reedf037e0b2014-10-30 11:34:15 -07001930
1931#endif