blob: 84e6a79803f13c6617f3354b1d3e9cef904cedda [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,
bsalomon7e68ab72016-04-13 14:29:25 -0700147 const SkSurfaceProps* props, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700148 unsigned flags;
149 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700150 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700151 }
152
bsalomon7e68ab72016-04-13 14:29:25 -0700153 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
halcanary96fcdcc2015-08-27 07:41:13 -0700154 if (nullptr == rt) {
155 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700156 }
157
halcanary385fe4d2015-08-26 13:07:48 -0700158 return new SkGpuDevice(rt, info.width(), info.height(), props, flags);
bsalomon74f681d2015-06-23 14:38:48 -0700159}
160
senorblancod0d37ca2015-04-02 04:54:56 -0700161SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
162 const SkSurfaceProps* props, unsigned flags)
robertphillipsfcf78292015-06-19 11:49:52 -0700163 : INHERITED(SkSurfacePropsCopyOrDefault(props))
joshualitt06dd0a82016-01-08 06:32:31 -0800164 , fContext(SkRef(rt->getContext()))
165 , fRenderTarget(SkRef(rt)) {
bsalomon74f681d2015-06-23 14:38:48 -0700166 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000167
bsalomon74f681d2015-06-23 14:38:48 -0700168 SkAlphaType at = fOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
169 SkImageInfo info = rt->surfacePriv().info(at).makeWH(width, height);
halcanary385fe4d2015-08-26 13:07:48 -0700170 SkPixelRef* pr = new SkGrPixelRef(info, rt);
bsalomonafbf2d62014-09-30 12:18:44 -0700171 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700172 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700173
robertphillips77a2e522015-10-17 07:43:27 -0700174 fDrawContext.reset(this->context()->drawContext(rt, &this->surfaceProps()));
bsalomone63ffef2016-02-05 07:17:34 -0800175 if (flags & kNeedClear_Flag) {
176 this->clearAll();
177 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000178}
179
bsalomon7e68ab72016-04-13 14:29:25 -0700180GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkBudgeted budgeted,
181 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000182 if (kUnknown_SkColorType == origInfo.colorType() ||
183 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700184 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000185 }
186
bsalomonafe30052015-01-16 07:32:33 -0800187 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700188 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800189 }
190
reede5ea5002014-09-03 11:54:58 -0700191 SkColorType ct = origInfo.colorType();
192 SkAlphaType at = origInfo.alphaType();
brianosmana6359362016-03-21 06:55:37 -0700193 SkColorProfileType pt = origInfo.profileType();
brianosman6b086522016-04-14 12:39:00 -0700194 if (kRGB_565_SkColorType == ct || kGray_8_SkColorType == ct) {
reede5ea5002014-09-03 11:54:58 -0700195 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800196 }
197 if (kOpaque_SkAlphaType != at) {
198 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000199 }
brianosman6b086522016-04-14 12:39:00 -0700200
201 GrPixelConfig origConfig = SkImageInfo2GrPixelConfig(ct, at, pt, *context->caps());
202 if (!context->caps()->isConfigRenderable(origConfig, sampleCount > 0)) {
203 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
204 ct = kN32_SkColorType;
205 }
206
brianosmana6359362016-03-21 06:55:37 -0700207 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at, pt);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000208
bsalomonf2703d82014-10-28 14:33:06 -0700209 GrSurfaceDesc desc;
210 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000211 desc.fWidth = info.width();
212 desc.fHeight = info.height();
brianosmana6359362016-03-21 06:55:37 -0700213 desc.fConfig = SkImageInfo2GrPixelConfig(info, *context->caps());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000214 desc.fSampleCnt = sampleCount;
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(
bsalomon7e68ab72016-04-13 14:29:25 -0700362 this->context(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt));
kkinnunenabcfab42015-02-22 22:53:44 -0800363
halcanary96fcdcc2015-08-27 07:41:13 -0700364 if (nullptr == newRT) {
kkinnunenabcfab42015-02-22 22:53:44 -0800365 return;
366 }
367
368 if (shouldRetainContent) {
369 if (fRenderTarget->wasDestroyed()) {
370 return;
371 }
372 this->context()->copySurface(newRT, fRenderTarget);
373 }
374
375 SkASSERT(fRenderTarget != newRT);
376
mtklein18300a32016-03-16 13:53:35 -0700377 fRenderTarget.reset(newRT.release());
kkinnunenabcfab42015-02-22 22:53:44 -0800378
bsalomon74f681d2015-06-23 14:38:48 -0700379#ifdef SK_DEBUG
380 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
381 kPremul_SkAlphaType);
382 SkASSERT(info == fLegacyBitmap.info());
383#endif
halcanary385fe4d2015-08-26 13:07:48 -0700384 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget);
kkinnunenabcfab42015-02-22 22:53:44 -0800385 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700386
robertphillips77a2e522015-10-17 07:43:27 -0700387 fDrawContext.reset(this->context()->drawContext(fRenderTarget, &this->surfaceProps()));
kkinnunenabcfab42015-02-22 22:53:44 -0800388}
389
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000390///////////////////////////////////////////////////////////////////////////////
391
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000392void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800393 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800394 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800395 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000396
397 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700398 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700399 this->surfaceProps().isGammaCorrect(), &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;
brianosman898235c2016-04-06 07:38:23 -0700449 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700450 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700451 return;
452 }
egdaniele61c4112014-06-12 10:24:21 -0700453 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700454 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700455 path.moveTo(pts[0]);
456 path.lineTo(pts[1]);
robertphillips2e1e51f2015-10-15 08:01:48 -0700457 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700458 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000459 }
460
ethannicholas330bb952015-07-17 06:44:02 -0700461 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000462 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800463 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700464 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000465 draw.drawPoints(mode, count, pts, paint, true);
466 return;
467 }
468
469 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700470 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700471 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700472 return;
473 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000474
robertphillips2e1e51f2015-10-15 08:01:48 -0700475 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700476 grPaint,
477 *draw.fMatrix,
478 gPointMode2PrimtiveType[mode],
479 SkToS32(count),
480 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700481 nullptr,
482 nullptr,
483 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700484 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000485}
486
487///////////////////////////////////////////////////////////////////////////////
488
robertphillipsff55b492015-11-24 07:56:59 -0800489void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800490 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800491 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800492 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000493
494 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
495 SkScalar width = paint.getStrokeWidth();
496
497 /*
498 We have special code for hairline strokes, miter-strokes, bevel-stroke
499 and fills. Anything else we just call our path code.
500 */
501 bool usePath = doStroke && width > 0 &&
502 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
503 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
egdanield58a0ba2014-06-11 10:30:05 -0700504
robertphillipsd8aa59d2015-08-05 09:07:12 -0700505 // a few other reasons we might need to call drawPath...
robertphillipsff55b492015-11-24 07:56:59 -0800506 if (paint.getMaskFilter() || paint.getPathEffect() ||
robertphillipsd8aa59d2015-08-05 09:07:12 -0700507 paint.getStyle() == SkPaint::kStrokeAndFill_Style) { // we can't both stroke and fill rects
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000508 usePath = true;
509 }
egdanield58a0ba2014-06-11 10:30:05 -0700510
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000511 if (usePath) {
512 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700513 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000514 path.addRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800515 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800516 fClip, path, paint,
517 *draw.fMatrix, nullptr,
518 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000519 return;
520 }
521
522 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700523 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700524 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700525 return;
526 }
Mike Klein744fb732014-06-23 15:13:26 -0400527
robertphillipsff55b492015-11-24 07:56:59 -0800528 GrStrokeInfo strokeInfo(paint);
529
robertphillips2e1e51f2015-10-15 08:01:48 -0700530 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000531}
532
533///////////////////////////////////////////////////////////////////////////////
534
535void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800536 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800537 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800538 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800539 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000540
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000541 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700542 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700543 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700544 return;
545 }
Mike Klein744fb732014-06-23 15:13:26 -0400546
egdanield58a0ba2014-06-11 10:30:05 -0700547 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000548 if (paint.getMaskFilter()) {
549 // try to hit the fast path for drawing filtered round rects
550
551 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800552 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000553 if (devRRect.allCornersCircular()) {
554 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700555 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
joshualitt5531d512014-12-17 15:50:11 -0800556 draw.fClip->getBounds(),
557 *draw.fMatrix,
558 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000559 SkIRect finalIRect;
560 maskRect.roundOut(&finalIRect);
561 if (draw.fClip->quickReject(finalIRect)) {
562 // clipped out
563 return;
564 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700565 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
566 fDrawContext,
joshualitt25d9c152015-02-18 12:29:52 -0800567 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800568 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800569 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700570 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700571 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000572 return;
573 }
574 }
575
576 }
577 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000578 }
579
robertphillips514450c2015-11-24 05:36:02 -0800580 if (paint.getMaskFilter() || paint.getPathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800581 // The only mask filter the native rrect drawing code could've handle was taken
582 // care of above.
583 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000584 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700585 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000586 path.addRRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800587 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800588 fClip, path, paint,
589 *draw.fMatrix, nullptr,
590 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000591 return;
592 }
Mike Klein744fb732014-06-23 15:13:26 -0400593
robertphillips514450c2015-11-24 05:36:02 -0800594 SkASSERT(!strokeInfo.isDashed());
595
robertphillips2e1e51f2015-10-15 08:01:48 -0700596 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000597}
598
robertphillipsd7706102016-02-25 09:28:08 -0800599
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000600void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800601 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800602 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800603 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800604 CHECK_SHOULD_DRAW(draw);
605
robertphillipsd7706102016-02-25 09:28:08 -0800606 if (outer.isEmpty()) {
607 return;
608 }
609
610 if (inner.isEmpty()) {
611 return this->drawRRect(draw, outer, paint);
612 }
613
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000614 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000615
robertphillips0e7029e2015-11-30 05:45:06 -0800616 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800617 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700618 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700619 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700620 return;
621 }
robertphillips00095892016-02-29 13:50:40 -0800622
623 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
624 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000625 }
626
627 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700628 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000629 path.addRRect(outer);
630 path.addRRect(inner);
631 path.setFillType(SkPath::kEvenOdd_FillType);
632
robertphillips7bceedc2015-12-01 12:51:26 -0800633 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillips0e7029e2015-11-30 05:45:06 -0800634 fClip, path, paint,
635 *draw.fMatrix, nullptr,
636 draw.fClip->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000637}
638
639
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000640/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000641
robertphillips514450c2015-11-24 05:36:02 -0800642void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800643 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800644 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800645 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000646
robertphillips514450c2015-11-24 05:36:02 -0800647 // Presumably the path effect warps this to something other than an oval
648 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000649 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700650 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000651 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700652 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000653 return;
herb11a7f7f2015-11-24 12:41:00 -0800654 }
655
robertphillips514450c2015-11-24 05:36:02 -0800656 if (paint.getMaskFilter()) {
657 // The RRect path can handle special case blurring
658 SkRRect rr = SkRRect::MakeOval(oval);
659 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660 }
661
662 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700663 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700664 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700665 return;
666 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000667
robertphillips514450c2015-11-24 05:36:02 -0800668 GrStrokeInfo strokeInfo(paint);
669 SkASSERT(!strokeInfo.isDashed());
670
robertphillips2e1e51f2015-10-15 08:01:48 -0700671 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000672}
673
674#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000675
676///////////////////////////////////////////////////////////////////////////////
677
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000678void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
679 const SkPaint& paint, const SkMatrix* prePathMatrix,
680 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800681 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800682 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
683 bool isClosed;
684 SkRect rect;
685 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
686 this->drawRect(draw, rect, paint);
687 return;
688 }
689 if (origSrcPath.isOval(&rect)) {
690 this->drawOval(draw, rect, paint);
691 return;
692 }
693 SkRRect rrect;
694 if (origSrcPath.isRRect(&rrect)) {
695 this->drawRRect(draw, rrect, paint);
696 return;
697 }
698 }
699
joshualitt5531d512014-12-17 15:50:11 -0800700 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800701 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000702
robertphillips7bceedc2015-12-01 12:51:26 -0800703 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsccb1b572015-05-27 11:02:55 -0700704 fClip, origSrcPath, paint,
705 *draw.fMatrix, prePathMatrix,
706 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000707}
708
709static const int kBmpSmallTileSize = 1 << 10;
710
711static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
712 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
713 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
714 return tilesX * tilesY;
715}
716
reed85d91782015-09-10 14:33:38 -0700717static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000718 if (maxTileSize <= kBmpSmallTileSize) {
719 return maxTileSize;
720 }
721
722 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
723 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
724
725 maxTileTotalTileSize *= maxTileSize * maxTileSize;
726 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
727
728 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
729 return kBmpSmallTileSize;
730 } else {
731 return maxTileSize;
732 }
733}
734
735// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
736// pixels from the bitmap are necessary.
bsalomon74f681d2015-06-23 14:38:48 -0700737static void determine_clipped_src_rect(const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800738 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800739 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700740 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000741 const SkRect* srcRectPtr,
742 SkIRect* clippedSrcIRect) {
robertphillips7bceedc2015-12-01 12:51:26 -0800743 clip.getConservativeBounds(rt->width(), rt->height(), clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000744 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800745 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000746 clippedSrcIRect->setEmpty();
747 return;
748 }
749 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
750 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700751 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000752 // we've setup src space 0,0 to map to the top left of the src rect.
753 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000754 if (!clippedSrcRect.intersect(*srcRectPtr)) {
755 clippedSrcIRect->setEmpty();
756 return;
757 }
758 }
759 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700760 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000761 if (!clippedSrcIRect->intersect(bmpBounds)) {
762 clippedSrcIRect->setEmpty();
763 }
764}
765
reed85d91782015-09-10 14:33:38 -0700766bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
767 const SkMatrix& viewMatrix,
768 const GrTextureParams& params,
769 const SkRect* srcRectPtr,
770 int maxTileSize,
771 int* tileSize,
772 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800773 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700774 // if it's larger than the max tile size, then we have no choice but tiling.
775 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
776 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(),
777 srcRectPtr, clippedSubset);
778 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
779 return true;
780 }
781
bsalomon1a1d0b82015-10-16 07:49:42 -0700782 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700783 const size_t area = imageRect.width() * imageRect.height();
784 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
785 return false;
786 }
787
reed85d91782015-09-10 14:33:38 -0700788 // At this point we know we could do the draw by uploading the entire bitmap
789 // as a texture. However, if the texture would be large compared to the
790 // cache size and we don't require most of it for this draw then tile to
791 // reduce the amount of upload and cache spill.
792
793 // assumption here is that sw bitmap size is a good proxy for its size as
794 // a texture
795 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
796 size_t cacheSize;
797 fContext->getResourceCacheLimits(nullptr, &cacheSize);
798 if (bmpSize < cacheSize / 2) {
799 return false;
800 }
801
bsalomon1a1d0b82015-10-16 07:49:42 -0700802 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
803 // tiling memory savings would be < 50%.
reed85d91782015-09-10 14:33:38 -0700804 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(), srcRectPtr,
805 clippedSubset);
806 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
807 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
808 kBmpSmallTileSize * kBmpSmallTileSize;
809
810 return usedTileBytes < 2 * bmpSize;
811}
812
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000813bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800814 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000815 const GrTextureParams& params,
816 const SkRect* srcRectPtr,
817 int maxTileSize,
818 int* tileSize,
819 SkIRect* clippedSrcRect) const {
joshualittce894002016-01-11 13:29:31 -0800820 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000821 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700822 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000823 return false;
824 }
825
reed85d91782015-09-10 14:33:38 -0700826 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
827 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
828}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000829
reed85d91782015-09-10 14:33:38 -0700830bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
831 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
832 const SkMatrix& viewMatrix) const {
joshualittce894002016-01-11 13:29:31 -0800833 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700834 // if image is explictly texture backed then just use the texture
835 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000836 return false;
837 }
838
reed85d91782015-09-10 14:33:38 -0700839 GrTextureParams params;
840 bool doBicubic;
841 GrTextureParams::FilterMode textureFilterMode =
842 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
843
844 int tileFilterPad;
845 if (doBicubic) {
846 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
847 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
848 tileFilterPad = 0;
849 } else {
850 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000851 }
reed85d91782015-09-10 14:33:38 -0700852 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000853
bsalomon8c07b7a2015-11-02 11:36:52 -0800854 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000855
reed85d91782015-09-10 14:33:38 -0700856 // these are output, which we safely ignore, as we just want to know the predicate
857 int outTileSize;
858 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000859
reed85d91782015-09-10 14:33:38 -0700860 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
861 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000862}
863
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000864void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000865 const SkBitmap& bitmap,
866 const SkMatrix& m,
867 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800868 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800869 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800870 SkMatrix viewMatrix;
871 viewMatrix.setConcat(*origDraw.fMatrix, m);
872 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800873 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonb1b01992015-11-18 10:56:08 -0800874 // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
bsalomonf1ecd212015-12-09 17:06:02 -0800875 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
876 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800877 return;
878 }
bsalomonb1b01992015-11-18 10:56:08 -0800879 int maxTileSize = fContext->caps()->maxTileSize();
880
881 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
882 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
883 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
884 paint.isAntiAlias() &&
885 bitmap.width() <= maxTileSize &&
886 bitmap.height() <= maxTileSize;
887
888 bool skipTileCheck = drawAA || paint.getMaskFilter();
889
890 if (!skipTileCheck) {
891 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
892 int tileSize;
893 SkIRect clippedSrcRect;
894
895 GrTextureParams params;
896 bool doBicubic;
897 GrTextureParams::FilterMode textureFilterMode =
898 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
899 &doBicubic);
900
901 int tileFilterPad;
902
903 if (doBicubic) {
904 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
905 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
906 tileFilterPad = 0;
907 } else {
908 tileFilterPad = 1;
909 }
910 params.setFilterMode(textureFilterMode);
911
912 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
913 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
914 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
915 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
916 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
917 return;
918 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000919 }
bsalomonb1b01992015-11-18 10:56:08 -0800920 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800921 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
922 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000923}
924
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000925// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000926// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
927// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000928static inline void clamped_outset_with_offset(SkIRect* iRect,
929 int outset,
930 SkPoint* offset,
931 const SkIRect& clamp) {
932 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000934 int leftClampDelta = clamp.fLeft - iRect->fLeft;
935 if (leftClampDelta > 0) {
936 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000937 iRect->fLeft = clamp.fLeft;
938 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000939 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000940 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000941
942 int topClampDelta = clamp.fTop - iRect->fTop;
943 if (topClampDelta > 0) {
944 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000945 iRect->fTop = clamp.fTop;
946 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000947 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000948 }
949
950 if (iRect->fRight > clamp.fRight) {
951 iRect->fRight = clamp.fRight;
952 }
953 if (iRect->fBottom > clamp.fBottom) {
954 iRect->fBottom = clamp.fBottom;
955 }
956}
957
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958// Break 'bitmap' into several tiles to draw it since it has already
959// been determined to be too large to fit in VRAM
960void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800961 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000962 const SkRect& srcRect,
963 const SkIRect& clippedSrcIRect,
964 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800965 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700966 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000967 int tileSize,
968 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800969 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800970
ericrk82e26bf2016-02-25 22:15:29 -0800971 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entry.
ericrk369e9372016-02-05 15:32:36 -0800972 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
973
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000974 // The following pixel lock is technically redundant, but it is desirable
975 // to lock outside of the tile loop to prevent redecoding the whole image
976 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
977 // is larger than the limit of the discardable memory pool.
978 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800979
980 const SkPaint* paint = &origPaint;
981 SkPaint tempPaint;
982 if (origPaint.isAntiAlias() && !fRenderTarget->isUnifiedMultisampled()) {
983 // Drop antialiasing to avoid seams at tile boundaries.
984 tempPaint = origPaint;
985 tempPaint.setAntiAlias(false);
986 paint = &tempPaint;
987 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000988 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
989
990 int nx = bitmap.width() / tileSize;
991 int ny = bitmap.height() / tileSize;
992 for (int x = 0; x <= nx; x++) {
993 for (int y = 0; y <= ny; y++) {
994 SkRect tileR;
995 tileR.set(SkIntToScalar(x * tileSize),
996 SkIntToScalar(y * tileSize),
997 SkIntToScalar((x + 1) * tileSize),
998 SkIntToScalar((y + 1) * tileSize));
999
1000 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1001 continue;
1002 }
1003
1004 if (!tileR.intersect(srcRect)) {
1005 continue;
1006 }
1007
1008 SkBitmap tmpB;
1009 SkIRect iTileR;
1010 tileR.roundOut(&iTileR);
1011 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1012 SkIntToScalar(iTileR.fTop));
1013
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001014 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001015 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001016 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001017 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001018 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001019
robertphillipsec8bb942014-11-21 10:16:25 -08001020 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001021 SkIRect iClampRect;
1022
reeda5517e22015-07-14 10:54:12 -07001023 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001024 // In bleed mode we want to always expand the tile on all edges
1025 // but stay within the bitmap bounds
1026 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1027 } else {
1028 // In texture-domain/clamp mode we only want to expand the
1029 // tile on edges interior to "srcRect" (i.e., we want to
1030 // not bleed across the original clamped edges)
1031 srcRect.roundOut(&iClampRect);
1032 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001033 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1034 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001035 }
1036
1037 if (bitmap.extractSubset(&tmpB, iTileR)) {
1038 // now offset it to make it "local" to our tmp bitmap
1039 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001040 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001041 // de-optimized this determination
1042 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001043 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001044 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001045 tileR,
1046 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -08001047 *paint,
reeda5517e22015-07-14 10:54:12 -07001048 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001049 bicubic,
1050 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001051 }
1052 }
1053 }
1054}
1055
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001056/*
1057 * This is called by drawBitmap(), which has to handle images that may be too
1058 * large to be represented by a single texture.
1059 *
1060 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1061 * and that non-texture portion of the GrPaint has already been setup.
1062 */
1063void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001064 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001065 const SkRect& srcRect,
1066 const GrTextureParams& params,
1067 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001068 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001069 bool bicubic,
1070 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001071 // We should have already handled bitmaps larger than the max texture size.
1072 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1073 bitmap.height() <= fContext->caps()->maxTextureSize());
1074 // Unless the bitmap is inherently texture-backed, we should be respecting the max tile size
1075 // by the time we get here.
1076 SkASSERT(bitmap.getTexture() ||
1077 (bitmap.width() <= fContext->caps()->maxTileSize() &&
1078 bitmap.height() <= fContext->caps()->maxTileSize()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001079
1080 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001081 AutoBitmapTexture abt(fContext, bitmap, params, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001082 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001083 return;
1084 }
1085
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001086 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001087 SkRect paintRect;
1088 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1089 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1090 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1091 SkScalarMul(srcRect.fTop, hInv),
1092 SkScalarMul(srcRect.fRight, wInv),
1093 SkScalarMul(srcRect.fBottom, hInv));
1094
egdaniel79da63f2015-10-09 10:55:16 -07001095 SkMatrix texMatrix;
1096 texMatrix.reset();
1097 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1098 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1099 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1100 // Thus we need to pass in the transform matrix directly to the texture processor used for
1101 // the bitmap draw.
1102 texMatrix.setScale(wInv, hInv);
1103 }
1104
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001105 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001106
1107 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1108 // the rest from the SkPaint.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001109 SkAutoTUnref<const GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001110
reeda5517e22015-07-14 10:54:12 -07001111 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001112 // Use a constrained texture domain to avoid color bleeding
1113 SkScalar left, top, right, bottom;
1114 if (srcRect.width() > SK_Scalar1) {
1115 SkScalar border = SK_ScalarHalf / texture->width();
1116 left = paintRect.left() + border;
1117 right = paintRect.right() - border;
1118 } else {
1119 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1120 }
1121 if (srcRect.height() > SK_Scalar1) {
1122 SkScalar border = SK_ScalarHalf / texture->height();
1123 top = paintRect.top() + border;
1124 bottom = paintRect.bottom() - border;
1125 } else {
1126 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1127 }
1128 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001129 if (bicubic) {
egdaniel79da63f2015-10-09 10:55:16 -07001130 fp.reset(GrBicubicEffect::Create(texture, texMatrix, textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001131 } else {
bsalomon4a339522015-10-06 08:40:50 -07001132 fp.reset(GrTextureDomainEffect::Create(texture,
egdaniel79da63f2015-10-09 10:55:16 -07001133 texMatrix,
joshualitt5531d512014-12-17 15:50:11 -08001134 textureDomain,
1135 GrTextureDomain::kClamp_Mode,
1136 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001137 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001138 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001139 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1140 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
egdaniel79da63f2015-10-09 10:55:16 -07001141 fp.reset(GrBicubicEffect::Create(texture, texMatrix, tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001142 } else {
egdaniel79da63f2015-10-09 10:55:16 -07001143 fp.reset(GrSimpleTextureEffect::Create(texture, texMatrix, params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001144 }
1145
joshualitt33a5fce2015-11-18 13:28:51 -08001146 GrPaint grPaint;
1147 if (!SkPaintToGrPaintWithTexture(this->context(), paint, viewMatrix, fp,
brianosman898235c2016-04-06 07:38:23 -07001148 kAlpha_8_SkColorType == bitmap.colorType(),
brianosmanb461d342016-04-13 13:10:14 -07001149 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001150 return;
1151 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001152
egdaniel79da63f2015-10-09 10:55:16 -07001153 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1154 // We don't have local coords in this case and have previously set the transform
1155 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001156 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001157 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001158 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001159 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001160}
1161
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001162void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1163 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001164 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001165 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001166 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167
1168 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1169 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1170 return;
1171 }
1172
robertphillipse1849d12016-03-17 08:26:38 -07001173 int offX = bitmap.pixelRefOrigin().fX;
1174 int offY = bitmap.pixelRefOrigin().fY;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001175 int w = bitmap.width();
1176 int h = bitmap.height();
1177
1178 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001179 // draw sprite neither filters nor tiles.
1180 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001181 if (!texture) {
1182 return;
1183 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001184
bsalomonf1b7a1d2015-09-28 06:26:28 -07001185 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
1186
senorblanco6fb3cd72016-04-17 14:02:04 -07001187 SkASSERT(!paint.getImageFilter());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001188
1189 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001190 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001191 GrSimpleTextureEffect::Create(texture, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001192 if (alphaOnly) {
1193 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1194 } else {
1195 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1196 }
brianosman898235c2016-04-06 07:38:23 -07001197 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp,
brianosmanb461d342016-04-13 13:10:14 -07001198 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001199 return;
1200 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001201
bsalomona2e69fc2015-11-05 10:41:43 -08001202 fDrawContext->fillRectToRect(fClip,
1203 grPaint,
1204 SkMatrix::I(),
1205 SkRect::MakeXYWH(SkIntToScalar(left),
1206 SkIntToScalar(top),
1207 SkIntToScalar(w),
1208 SkIntToScalar(h)),
robertphillipse1849d12016-03-17 08:26:38 -07001209 SkRect::MakeXYWH(SkIntToScalar(offX) / texture->width(),
1210 SkIntToScalar(offY) / texture->height(),
1211 SkIntToScalar(w) / texture->width(),
1212 SkIntToScalar(h) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001213}
1214
bsalomonb1b01992015-11-18 10:56:08 -08001215void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1216 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001217 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001218 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001219 CHECK_SHOULD_DRAW(draw);
bsalomonb1b01992015-11-18 10:56:08 -08001220 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001221 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001222 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
1223 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001224 return;
1225 }
bsalomonb1b01992015-11-18 10:56:08 -08001226 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1227 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1228 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1229 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1230 const SkRect* dst = &origDst;
1231 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001232 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001233 if (!src) {
1234 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001235 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001236
bsalomonb1b01992015-11-18 10:56:08 -08001237 SkMatrix srcToDstMatrix;
1238 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1239 return;
1240 }
1241 SkRect tmpSrc, tmpDst;
1242 if (src != &bmpBounds) {
1243 if (!bmpBounds.contains(*src)) {
1244 tmpSrc = *src;
1245 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001246 return; // nothing to draw
1247 }
bsalomonb1b01992015-11-18 10:56:08 -08001248 src = &tmpSrc;
1249 srcToDstMatrix.mapRect(&tmpDst, *src);
1250 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001251 }
1252 }
1253
bsalomonb1b01992015-11-18 10:56:08 -08001254 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001255
bsalomonb1b01992015-11-18 10:56:08 -08001256 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1257 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
1258 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
1259 paint.isAntiAlias() &&
1260 bitmap.width() <= maxTileSize &&
1261 bitmap.height() <= maxTileSize;
1262
1263 bool skipTileCheck = drawAA || paint.getMaskFilter();
1264
1265 if (!skipTileCheck) {
1266 int tileSize;
1267 SkIRect clippedSrcRect;
1268
1269 GrTextureParams params;
1270 bool doBicubic;
1271 GrTextureParams::FilterMode textureFilterMode =
1272 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1273 &doBicubic);
1274
1275 int tileFilterPad;
1276
1277 if (doBicubic) {
1278 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1279 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1280 tileFilterPad = 0;
1281 } else {
1282 tileFilterPad = 1;
1283 }
1284 params.setFilterMode(textureFilterMode);
1285
1286 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1287 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1288 // we have a mask filter.
1289 SkMatrix viewMatrix = *draw.fMatrix;
1290 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1291 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1292 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1293 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1294 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1295 constraint, tileSize, doBicubic);
1296 return;
1297 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001298 }
bsalomonb1b01992015-11-18 10:56:08 -08001299 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001300 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001301}
1302
1303void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1304 int x, int y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001305 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001306 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001307 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001308 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001309
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001310 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001311 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001312
1313 GrRenderTarget* devRT = dev->accessRenderTarget();
1314 GrTexture* devTex;
halcanary96fcdcc2015-08-27 07:41:13 -07001315 if (nullptr == (devTex = devRT->asTexture())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001316 return;
1317 }
1318
robertphillips7b9e8a42014-12-11 08:20:31 -08001319 const SkImageInfo ii = dev->imageInfo();
1320 int w = ii.width();
1321 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001322
senorblanco6fb3cd72016-04-17 14:02:04 -07001323 SkASSERT(!paint.getImageFilter());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001324
1325 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001326 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001327 GrSimpleTextureEffect::Create(devTex, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001328 if (GrPixelConfigIsAlphaOnly(devTex->config())) {
1329 // Can this happen?
1330 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1331 } else {
1332 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1333 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001334
brianosman898235c2016-04-06 07:38:23 -07001335 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp,
brianosmanb461d342016-04-13 13:10:14 -07001336 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001337 return;
1338 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001339
1340 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1341 SkIntToScalar(y),
1342 SkIntToScalar(w),
1343 SkIntToScalar(h));
1344
1345 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1346 // scratch texture).
1347 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1348 SK_Scalar1 * h / devTex->height());
1349
bsalomona2e69fc2015-11-05 10:41:43 -08001350 fDrawContext->fillRectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001351}
1352
reeda85d4d02015-05-06 12:56:48 -07001353void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1354 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001355 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001356 SkMatrix viewMatrix = *draw.fMatrix;
1357 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001358 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001359 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001360 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001361 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1362 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001363 return;
reed85d91782015-09-10 14:33:38 -07001364 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001365 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001366 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1367 paint.getFilterQuality(), *draw.fMatrix)) {
1368 // only support tiling as bitmap at the moment, so force raster-version
1369 if (!as_IB(image)->getROPixels(&bm)) {
1370 return;
1371 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001372 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1373 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1374 CHECK_SHOULD_DRAW(draw);
1375 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001376 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1377 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001378 } else if (as_IB(image)->getROPixels(&bm)) {
1379 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001380 }
reeda85d4d02015-05-06 12:56:48 -07001381 }
1382}
1383
1384void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001385 const SkRect& dst, const SkPaint& paint,
1386 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001387 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001388 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001389 CHECK_SHOULD_DRAW(draw);
1390 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001391 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001392 return;
1393 }
1394 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001395 SkMatrix totalMatrix = *draw.fMatrix;
1396 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1397 dst.height() / (src ? src->height() : image->height()));
1398 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001399 // only support tiling as bitmap at the moment, so force raster-version
1400 if (!as_IB(image)->getROPixels(&bm)) {
1401 return;
1402 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001403 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1404 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1405 CHECK_SHOULD_DRAW(draw);
1406 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001407 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001408 } else if (as_IB(image)->getROPixels(&bm)) {
1409 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001410 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001411}
1412
bsalomonf1ecd212015-12-09 17:06:02 -08001413void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001414 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001415 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001416
joshualitt33a5fce2015-11-18 13:28:51 -08001417 CHECK_SHOULD_DRAW(draw);
1418
joshualittedb36442015-11-19 14:29:30 -08001419 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
1420 fRenderTarget->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001421 bool doBicubic;
1422 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001423 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1424 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001425 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001426 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001427
1428 SkRect srcR, dstR;
1429 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001430 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001431 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001432 }
1433 return;
1434 }
1435
bsalomon2bbd0c62015-12-09 12:50:56 -08001436 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
1437 SkAutoTUnref<const GrFragmentProcessor> fp(
1438 producer->createFragmentProcessor(SkMatrix::I(),
1439 SkRect::MakeIWH(producer->width(), producer->height()),
1440 GrTextureProducer::kNo_FilterConstraint, true,
1441 &kMode));
joshualitt33a5fce2015-11-18 13:28:51 -08001442 GrPaint grPaint;
1443 if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, fp,
brianosman898235c2016-04-06 07:38:23 -07001444 producer->isAlphaOnly(),
brianosmanb461d342016-04-13 13:10:14 -07001445 this->surfaceProps().isGammaCorrect(), &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001446 return;
1447 }
1448
bsalomon2bbd0c62015-12-09 12:50:56 -08001449 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1450 producer->height(), center, dst);
1451}
1452
1453void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1454 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001455 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001456 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001457 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001458 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001459 } else {
1460 SkBitmap bm;
1461 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1462 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001463 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001464 } else if (as_IB(image)->getROPixels(&bm)) {
1465 this->drawBitmapNine(draw, bm, center, dst, paint);
1466 }
1467 }
1468}
1469
1470void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1471 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001472 ASSERT_SINGLE_OWNER
bsalomon2bbd0c62015-12-09 12:50:56 -08001473 if (bitmap.getTexture()) {
1474 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001475 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001476 } else {
1477 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001478 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001479 }
joshualitt33a5fce2015-11-18 13:28:51 -08001480}
1481
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001482///////////////////////////////////////////////////////////////////////////////
1483
1484// must be in SkCanvas::VertexMode order
1485static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1486 kTriangles_GrPrimitiveType,
1487 kTriangleStrip_GrPrimitiveType,
1488 kTriangleFan_GrPrimitiveType,
1489};
1490
1491void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1492 int vertexCount, const SkPoint vertices[],
1493 const SkPoint texs[], const SkColor colors[],
1494 SkXfermode* xmode,
1495 const uint16_t indices[], int indexCount,
1496 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001497 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001498 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001499 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001500
halcanary96fcdcc2015-08-27 07:41:13 -07001501 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1502 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001503
halcanary96fcdcc2015-08-27 07:41:13 -07001504 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001505
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001506 SkPaint copy(paint);
1507 copy.setStyle(SkPaint::kStroke_Style);
1508 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001509
bsalomonf1b7a1d2015-09-28 06:26:28 -07001510 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001511 // we ignore the shader if texs is null.
brianosman898235c2016-04-06 07:38:23 -07001512 if (!SkPaintToGrPaintNoShader(this->context(), copy,
brianosmanb461d342016-04-13 13:10:14 -07001513 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001514 return;
1515 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001516
dandov32a311b2014-07-15 19:46:26 -07001517 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001518 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001519 switch (vmode) {
1520 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001521 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001522 break;
1523 case SkCanvas::kTriangleStrip_VertexMode:
1524 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001525 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001526 break;
1527 }
mtklein533eb782014-08-27 10:39:42 -07001528
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001529 VertState state(vertexCount, indices, indexCount);
1530 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001531
dandov32a311b2014-07-15 19:46:26 -07001532 //number of indices for lines per triangle with kLines
1533 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001534
bsalomonf1b7a1d2015-09-28 06:26:28 -07001535 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001536 int i = 0;
1537 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001538 lineIndices[i] = state.f0;
1539 lineIndices[i + 1] = state.f1;
1540 lineIndices[i + 2] = state.f1;
1541 lineIndices[i + 3] = state.f2;
1542 lineIndices[i + 4] = state.f2;
1543 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001544 i += 6;
1545 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001546 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001547 grPaint,
1548 *draw.fMatrix,
1549 kLines_GrPrimitiveType,
1550 vertexCount,
1551 vertices,
1552 texs,
1553 colors,
1554 lineIndices.get(),
1555 indexCount);
1556 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001557 }
1558
bsalomonf1b7a1d2015-09-28 06:26:28 -07001559 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001560
1561 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001562 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001563 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1564 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001565 convertedColors.reset(vertexCount);
1566 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001567 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001568 }
1569 colors = convertedColors.get();
1570 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001571 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001572 if (texs && paint.getShader()) {
1573 if (colors) {
1574 // When there are texs and colors the shader and colors are combined using xmode. A null
1575 // xmode is defined to mean modulate.
1576 SkXfermode::Mode colorMode;
1577 if (xmode) {
1578 if (!xmode->asMode(&colorMode)) {
1579 return;
1580 }
1581 } else {
1582 colorMode = SkXfermode::kModulate_Mode;
1583 }
1584 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
brianosmanb461d342016-04-13 13:10:14 -07001585 false, this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001586 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001587 return;
1588 }
1589 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001590 // We have a shader, but no colors to blend it against.
brianosman898235c2016-04-06 07:38:23 -07001591 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001592 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001593 return;
1594 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001595 }
bsalomonaa48d362015-10-01 08:34:17 -07001596 } else {
1597 if (colors) {
1598 // We have colors, but either have no shader or no texture coords (which implies that
1599 // we should ignore the shader).
brianosman898235c2016-04-06 07:38:23 -07001600 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001601 this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001602 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001603 return;
1604 }
1605 } else {
1606 // No colors and no shaders. Just draw with the paint color.
brianosman898235c2016-04-06 07:38:23 -07001607 if (!SkPaintToGrPaintNoShader(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001608 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001609 return;
1610 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001611 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001612 }
1613
robertphillips2e1e51f2015-10-15 08:01:48 -07001614 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001615 grPaint,
1616 *draw.fMatrix,
1617 primType,
1618 vertexCount,
1619 vertices,
1620 texs,
1621 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001622 indices,
robertphillipsea461502015-05-26 11:38:03 -07001623 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001624}
1625
1626///////////////////////////////////////////////////////////////////////////////
1627
jvanverth31ff7622015-08-07 10:09:28 -07001628void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001629 const SkRect texRect[], const SkColor colors[], int count,
1630 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001631 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001632 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001633 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001634 return;
1635 }
1636
jvanverth31ff7622015-08-07 10:09:28 -07001637 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001638 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001639
reedca109532015-06-25 16:25:25 -07001640 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001641 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001642
jvanverth31ff7622015-08-07 10:09:28 -07001643 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001644 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001645 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
brianosmanb461d342016-04-13 13:10:14 -07001646 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001647 return;
1648 }
1649 } else {
brianosman898235c2016-04-06 07:38:23 -07001650 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001651 this->surfaceProps().isGammaCorrect(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001652 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001653 }
1654 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001655
1656 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001657 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001658}
1659
1660///////////////////////////////////////////////////////////////////////////////
1661
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001662void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001663 size_t byteLength, SkScalar x, SkScalar y,
1664 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001665 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001666 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001667 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001668
jvanverth8c27a182014-10-14 08:45:50 -07001669 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001670 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001671 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001672 return;
1673 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001674
jvanverth8c27a182014-10-14 08:45:50 -07001675 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001676
robertphillips2e1e51f2015-10-15 08:01:48 -07001677 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001678 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001679}
1680
fmalita05c4a432014-09-29 06:29:53 -07001681void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1682 const SkScalar pos[], int scalarsPerPos,
1683 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001684 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001685 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001686 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001687
jvanverth8c27a182014-10-14 08:45:50 -07001688 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001689 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001690 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001691 return;
1692 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001693
jvanverth8c27a182014-10-14 08:45:50 -07001694 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001695
robertphillips2e1e51f2015-10-15 08:01:48 -07001696 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001697 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1698 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001699}
1700
joshualitt9c328182015-03-23 08:13:04 -07001701void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1702 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001703 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001704 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001705 CHECK_SHOULD_DRAW(draw);
1706
1707 SkDEBUGCODE(this->validate();)
1708
robertphillips2e1e51f2015-10-15 08:01:48 -07001709 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001710 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001711}
1712
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001713///////////////////////////////////////////////////////////////////////////////
1714
reedb2db8982014-11-13 12:41:02 -08001715bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001716 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001717}
1718
1719void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001720 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001721
bsalomonc49e8682015-06-30 11:37:35 -07001722 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001723}
1724
1725///////////////////////////////////////////////////////////////////////////////
1726
reed76033be2015-03-14 10:54:31 -07001727SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001728 ASSERT_SINGLE_OWNER
bsalomonf2703d82014-10-28 14:33:06 -07001729 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001730 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001731 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001732 desc.fWidth = cinfo.fInfo.width();
1733 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001734 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001735
1736 SkAutoTUnref<GrTexture> texture;
1737 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001738 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001739
hcm4396fa52014-10-27 21:43:30 -07001740 // layers are never draw in repeat modes, so we can request an approx
1741 // match and ignore any padding.
bsalomoneae62002015-07-31 13:59:30 -07001742 if (kNever_TileUsage == cinfo.fTileUsage) {
1743 texture.reset(fContext->textureProvider()->createApproxTexture(desc));
1744 } else {
bsalomon5ec26ae2016-02-25 08:33:02 -08001745 texture.reset(fContext->textureProvider()->createTexture(desc, SkBudgeted::kYes));
bsalomoneae62002015-07-31 13:59:30 -07001746 }
bsalomonafe30052015-01-16 07:32:33 -08001747
1748 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001749 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001750 return SkGpuDevice::Create(
bsalomon74f681d2015-06-23 14:38:48 -07001751 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, init);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001752 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001753 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001754 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001755 cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary96fcdcc2015-08-27 07:41:13 -07001756 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001757 }
1758}
1759
reede8f30622016-03-23 18:59:25 -07001760sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001761 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001762 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001763 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
reede8f30622016-03-23 18:59:25 -07001764 return SkSurface::MakeRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
1765 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001766}
1767
robertphillips30d2cc62014-09-24 08:52:18 -07001768bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001769 const SkMatrix* matrix, const SkPaint* paint) {
joshualittce894002016-01-11 13:29:31 -08001770 ASSERT_SINGLE_OWNER
robertphillips63242d72014-12-04 08:31:02 -08001771#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001772 // todo: should handle this natively
1773 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001774 return false;
1775 }
1776
halcanary96fcdcc2015-08-27 07:41:13 -07001777 const SkBigPicture::AccelData* data = nullptr;
mtklein9db912c2015-05-19 11:11:26 -07001778 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1779 data = bp->accelData();
1780 }
robertphillips81f71b62014-11-11 04:54:49 -08001781 if (!data) {
1782 return false;
1783 }
1784
robertphillipse5524cd2015-02-20 12:30:26 -08001785 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1786 if (0 == gpuData->numBlocks()) {
1787 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001788 }
1789
robertphillipsfd61ed02014-10-28 07:21:44 -07001790 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001791
robertphillipse5524cd2015-02-20 12:30:26 -08001792 SkIRect iBounds;
1793 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1794 return false;
1795 }
1796
1797 SkRect clipBounds = SkRect::Make(iBounds);
1798
1799 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1800
robertphillips60029a52015-11-09 13:51:06 -08001801 GrLayerHoister::Begin(fContext);
1802
robertphillipsfd61ed02014-10-28 07:21:44 -07001803 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001804 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001805 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001806 &atlasedNeedRendering, &atlasedRecycled,
vbuzinovdded6962015-06-12 08:59:45 -07001807 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001808
1809 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1810
1811 SkTDArray<GrHoistedLayer> needRendering, recycled;
1812
robertphillipse5524cd2015-02-20 12:30:26 -08001813 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1814
robertphillipsfd61ed02014-10-28 07:21:44 -07001815 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001816 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001817 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001818 &needRendering, &recycled,
vbuzinovdded6962015-06-12 08:59:45 -07001819 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001820
1821 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001822
robertphillips64bf7672014-08-21 13:07:35 -07001823 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001824 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
halcanary96fcdcc2015-08-27 07:41:13 -07001825 initialMatrix, nullptr);
robertphillips64bf7672014-08-21 13:07:35 -07001826
robertphillipsfd61ed02014-10-28 07:21:44 -07001827 GrLayerHoister::UnlockLayers(fContext, needRendering);
1828 GrLayerHoister::UnlockLayers(fContext, recycled);
1829 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1830 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips60029a52015-11-09 13:51:06 -08001831 GrLayerHoister::End(fContext);
robertphillips64bf7672014-08-21 13:07:35 -07001832
1833 return true;
robertphillips63242d72014-12-04 08:31:02 -08001834#else
1835 return false;
1836#endif
robertphillips64bf7672014-08-21 13:07:35 -07001837}
1838
senorblancobe129b22014-08-08 07:14:35 -07001839SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001840 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001841 // We always return a transient cache, so it is freed after each
1842 // filter traversal.
senorblanco6fb3cd72016-04-17 14:02:04 -07001843 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001844}
reedf037e0b2014-10-30 11:34:15 -07001845
1846#endif