blob: 4d82b201e74374db6a3ac9f2683e448fe4ecde11 [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();
reede5ea5002014-09-03 11:54:58 -0700194 if (kRGB_565_SkColorType == ct) {
195 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800196 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
197 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700198 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800199 }
200 if (kOpaque_SkAlphaType != at) {
201 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000202 }
brianosmana6359362016-03-21 06:55:37 -0700203 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at, pt);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000204
bsalomonf2703d82014-10-28 14:33:06 -0700205 GrSurfaceDesc desc;
206 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000207 desc.fWidth = info.width();
208 desc.fHeight = info.height();
brianosmana6359362016-03-21 06:55:37 -0700209 desc.fConfig = SkImageInfo2GrPixelConfig(info, *context->caps());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000210 desc.fSampleCnt = sampleCount;
cblume55f2d2d2016-02-26 13:20:48 -0800211 desc.fIsMipMapped = false;
bsalomon5ec26ae2016-02-25 08:33:02 -0800212 GrTexture* texture = context->textureProvider()->createTexture(desc, budgeted, nullptr, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700213 if (nullptr == texture) {
214 return nullptr;
kkinnunenabcfab42015-02-22 22:53:44 -0800215 }
halcanary96fcdcc2015-08-27 07:41:13 -0700216 SkASSERT(nullptr != texture->asRenderTarget());
kkinnunenabcfab42015-02-22 22:53:44 -0800217 return texture->asRenderTarget();
218}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000219
robertphillips1579e3c2016-03-24 05:01:23 -0700220// This method ensures that we always have a texture-backed "bitmap" when we finally
221// call through to the base impl so that the image filtering code will take the
222// gpu-specific paths. This mirrors SkCanvas::internalDrawDevice (the other
223// use of SkImageFilter::filterImage) in that the source and dest will have
224// homogenous backing (e.g., raster or gpu).
robertphillips2302de92016-03-24 07:26:32 -0700225void SkGpuDevice::drawSpriteWithFilter(const SkDraw& draw, const SkBitmap& bitmap,
226 int x, int y, const SkPaint& paint) {
robertphillips090b7622016-03-28 11:07:43 -0700227 ASSERT_SINGLE_OWNER
228 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpriteWithFilter", fContext);
229
230 if (fContext->abandoned()) {
231 return;
232 }
233
robertphillips1579e3c2016-03-24 05:01:23 -0700234 if (bitmap.getTexture()) {
robertphillips2302de92016-03-24 07:26:32 -0700235 INHERITED::drawSpriteWithFilter(draw, bitmap, x, y, paint);
robertphillips1579e3c2016-03-24 05:01:23 -0700236 return;
237 }
238
239 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
240 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
241 return;
242 }
243
244 GrTexture* texture;
245 // draw sprite neither filters nor tiles.
246 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
247 if (!texture) {
248 return;
249 }
250
251 SkBitmap newBitmap;
252
253 GrWrapTextureInBitmap(texture, texture->width(), texture->height(),
254 bitmap.isOpaque(), &newBitmap);
255
robertphillips2302de92016-03-24 07:26:32 -0700256 INHERITED::drawSpriteWithFilter(draw, newBitmap, x, y, paint);
robertphillips1579e3c2016-03-24 05:01:23 -0700257}
258
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000259///////////////////////////////////////////////////////////////////////////////
260
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000261bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
262 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800263 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000264
265 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700266 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps());
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000267 if (kUnknown_GrPixelConfig == config) {
268 return false;
269 }
270
271 uint32_t flags = 0;
272 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
273 flags = GrContext::kUnpremul_PixelOpsFlag;
274 }
bsalomon74f681d2015-06-23 14:38:48 -0700275 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
276 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000277}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000278
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000279bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
280 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800281 ASSERT_SINGLE_OWNER
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000282 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700283 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fContext->caps());
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000284 if (kUnknown_GrPixelConfig == config) {
285 return false;
286 }
287 uint32_t flags = 0;
288 if (kUnpremul_SkAlphaType == info.alphaType()) {
289 flags = GrContext::kUnpremul_PixelOpsFlag;
290 }
291 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
292
293 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700294 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000295
296 return true;
297}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000298
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000299const SkBitmap& SkGpuDevice::onAccessBitmap() {
joshualittce894002016-01-11 13:29:31 -0800300 ASSERT_SINGLE_OWNER
reed89443ab2014-06-27 11:34:19 -0700301 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000302}
303
reed41e010c2015-06-09 12:16:53 -0700304bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800305 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700306 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
307 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
308 // ... ugh.
309 fLegacyBitmap.notifyPixelsChanged();
310 return false;
311}
312
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000313void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
joshualittce894002016-01-11 13:29:31 -0800314 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000315 INHERITED::onAttachToCanvas(canvas);
316
317 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800318 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000319}
320
321void SkGpuDevice::onDetachFromCanvas() {
joshualittce894002016-01-11 13:29:31 -0800322 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800324 fClip.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700325 fClipStack.reset(nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000326}
327
328// call this every draw call, to ensure that the context reflects our state,
329// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800330void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800331 ASSERT_SINGLE_OWNER
joshualitt44701df2015-02-23 14:44:57 -0800332 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000333
joshualitt44701df2015-02-23 14:44:57 -0800334 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000335
joshualitt570d2f82015-02-25 13:19:48 -0800336 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000337}
338
339GrRenderTarget* SkGpuDevice::accessRenderTarget() {
joshualittce894002016-01-11 13:29:31 -0800340 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000341 return fRenderTarget;
342}
343
reed8eddfb52014-12-04 07:50:14 -0800344void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800345 ASSERT_SINGLE_OWNER
reed8eddfb52014-12-04 07:50:14 -0800346 GrColor color = 0;
joshualitt5651ee62016-01-11 10:39:11 -0800347 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext);
reed8eddfb52014-12-04 07:50:14 -0800348 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700349 fDrawContext->clear(&rect, color, true);
reed8eddfb52014-12-04 07:50:14 -0800350}
351
kkinnunenabcfab42015-02-22 22:53:44 -0800352void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800353 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800354
bsalomon5ec26ae2016-02-25 08:33:02 -0800355 SkBudgeted budgeted = fRenderTarget->resourcePriv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800356
357 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
bsalomon7e68ab72016-04-13 14:29:25 -0700358 this->context(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt));
kkinnunenabcfab42015-02-22 22:53:44 -0800359
halcanary96fcdcc2015-08-27 07:41:13 -0700360 if (nullptr == newRT) {
kkinnunenabcfab42015-02-22 22:53:44 -0800361 return;
362 }
363
364 if (shouldRetainContent) {
365 if (fRenderTarget->wasDestroyed()) {
366 return;
367 }
368 this->context()->copySurface(newRT, fRenderTarget);
369 }
370
371 SkASSERT(fRenderTarget != newRT);
372
mtklein18300a32016-03-16 13:53:35 -0700373 fRenderTarget.reset(newRT.release());
kkinnunenabcfab42015-02-22 22:53:44 -0800374
bsalomon74f681d2015-06-23 14:38:48 -0700375#ifdef SK_DEBUG
376 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
377 kPremul_SkAlphaType);
378 SkASSERT(info == fLegacyBitmap.info());
379#endif
halcanary385fe4d2015-08-26 13:07:48 -0700380 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget);
kkinnunenabcfab42015-02-22 22:53:44 -0800381 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700382
robertphillips77a2e522015-10-17 07:43:27 -0700383 fDrawContext.reset(this->context()->drawContext(fRenderTarget, &this->surfaceProps()));
kkinnunenabcfab42015-02-22 22:53:44 -0800384}
385
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000386///////////////////////////////////////////////////////////////////////////////
387
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000388void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800389 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800390 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800391 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000392
393 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700394 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700395 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700396 return;
397 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000398
robertphillips2e1e51f2015-10-15 08:01:48 -0700399 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000400}
401
402// must be in SkCanvas::PointMode order
403static const GrPrimitiveType gPointMode2PrimtiveType[] = {
404 kPoints_GrPrimitiveType,
405 kLines_GrPrimitiveType,
406 kLineStrip_GrPrimitiveType
407};
408
ethannicholas330bb952015-07-17 06:44:02 -0700409// suppress antialiasing on axis-aligned integer-coordinate lines
410static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
411 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
412 return false;
413 }
414 if (count == 2) {
415 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
416 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800417 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700418 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800419 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700420 // the two end pixels as being the lesser of two evils.
421 if (pts[0].fX == pts[1].fX) {
422 return ((int) pts[0].fX) != pts[0].fX;
423 }
424 if (pts[0].fY == pts[1].fY) {
425 return ((int) pts[0].fY) != pts[0].fY;
426 }
427 }
428 return true;
429}
430
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000431void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
432 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800433 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800434 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800435 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000436
437 SkScalar width = paint.getStrokeWidth();
438 if (width < 0) {
439 return;
440 }
441
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000442 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700443 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
444 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700445 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700446 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700447 return;
448 }
egdaniele61c4112014-06-12 10:24:21 -0700449 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700450 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700451 path.moveTo(pts[0]);
452 path.lineTo(pts[1]);
robertphillips2e1e51f2015-10-15 08:01:48 -0700453 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700454 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000455 }
456
ethannicholas330bb952015-07-17 06:44:02 -0700457 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000458 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800459 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700460 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000461 draw.drawPoints(mode, count, pts, paint, true);
462 return;
463 }
464
465 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700466 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700467 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700468 return;
469 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000470
robertphillips2e1e51f2015-10-15 08:01:48 -0700471 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700472 grPaint,
473 *draw.fMatrix,
474 gPointMode2PrimtiveType[mode],
475 SkToS32(count),
476 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700477 nullptr,
478 nullptr,
479 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700480 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000481}
482
483///////////////////////////////////////////////////////////////////////////////
484
robertphillipsff55b492015-11-24 07:56:59 -0800485void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800486 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800487 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800488 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000489
490 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
491 SkScalar width = paint.getStrokeWidth();
492
493 /*
494 We have special code for hairline strokes, miter-strokes, bevel-stroke
495 and fills. Anything else we just call our path code.
496 */
497 bool usePath = doStroke && width > 0 &&
498 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
499 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
egdanield58a0ba2014-06-11 10:30:05 -0700500
robertphillipsd8aa59d2015-08-05 09:07:12 -0700501 // a few other reasons we might need to call drawPath...
robertphillipsff55b492015-11-24 07:56:59 -0800502 if (paint.getMaskFilter() || paint.getPathEffect() ||
robertphillipsd8aa59d2015-08-05 09:07:12 -0700503 paint.getStyle() == SkPaint::kStrokeAndFill_Style) { // we can't both stroke and fill rects
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000504 usePath = true;
505 }
egdanield58a0ba2014-06-11 10:30:05 -0700506
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000507 if (usePath) {
508 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700509 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000510 path.addRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800511 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800512 fClip, path, paint,
513 *draw.fMatrix, nullptr,
514 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000515 return;
516 }
517
518 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700519 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700520 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700521 return;
522 }
Mike Klein744fb732014-06-23 15:13:26 -0400523
robertphillipsff55b492015-11-24 07:56:59 -0800524 GrStrokeInfo strokeInfo(paint);
525
robertphillips2e1e51f2015-10-15 08:01:48 -0700526 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000527}
528
529///////////////////////////////////////////////////////////////////////////////
530
531void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800532 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800533 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800534 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800535 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000536
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000537 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700538 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700539 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700540 return;
541 }
Mike Klein744fb732014-06-23 15:13:26 -0400542
egdanield58a0ba2014-06-11 10:30:05 -0700543 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000544 if (paint.getMaskFilter()) {
545 // try to hit the fast path for drawing filtered round rects
546
547 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800548 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000549 if (devRRect.allCornersCircular()) {
550 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700551 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
joshualitt5531d512014-12-17 15:50:11 -0800552 draw.fClip->getBounds(),
553 *draw.fMatrix,
554 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000555 SkIRect finalIRect;
556 maskRect.roundOut(&finalIRect);
557 if (draw.fClip->quickReject(finalIRect)) {
558 // clipped out
559 return;
560 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700561 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
562 fDrawContext,
joshualitt25d9c152015-02-18 12:29:52 -0800563 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800564 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800565 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700566 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700567 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000568 return;
569 }
570 }
571
572 }
573 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000574 }
575
robertphillips514450c2015-11-24 05:36:02 -0800576 if (paint.getMaskFilter() || paint.getPathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800577 // The only mask filter the native rrect drawing code could've handle was taken
578 // care of above.
579 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000580 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700581 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000582 path.addRRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800583 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800584 fClip, path, paint,
585 *draw.fMatrix, nullptr,
586 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000587 return;
588 }
Mike Klein744fb732014-06-23 15:13:26 -0400589
robertphillips514450c2015-11-24 05:36:02 -0800590 SkASSERT(!strokeInfo.isDashed());
591
robertphillips2e1e51f2015-10-15 08:01:48 -0700592 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000593}
594
robertphillipsd7706102016-02-25 09:28:08 -0800595
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000596void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800597 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800598 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800599 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800600 CHECK_SHOULD_DRAW(draw);
601
robertphillipsd7706102016-02-25 09:28:08 -0800602 if (outer.isEmpty()) {
603 return;
604 }
605
606 if (inner.isEmpty()) {
607 return this->drawRRect(draw, outer, paint);
608 }
609
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000610 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000611
robertphillips0e7029e2015-11-30 05:45:06 -0800612 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800613 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700614 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700615 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700616 return;
617 }
robertphillips00095892016-02-29 13:50:40 -0800618
619 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
620 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000621 }
622
623 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700624 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000625 path.addRRect(outer);
626 path.addRRect(inner);
627 path.setFillType(SkPath::kEvenOdd_FillType);
628
robertphillips7bceedc2015-12-01 12:51:26 -0800629 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillips0e7029e2015-11-30 05:45:06 -0800630 fClip, path, paint,
631 *draw.fMatrix, nullptr,
632 draw.fClip->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000633}
634
635
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000636/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000637
robertphillips514450c2015-11-24 05:36:02 -0800638void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800639 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800640 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800641 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000642
robertphillips514450c2015-11-24 05:36:02 -0800643 // Presumably the path effect warps this to something other than an oval
644 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000645 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700646 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000647 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700648 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000649 return;
herb11a7f7f2015-11-24 12:41:00 -0800650 }
651
robertphillips514450c2015-11-24 05:36:02 -0800652 if (paint.getMaskFilter()) {
653 // The RRect path can handle special case blurring
654 SkRRect rr = SkRRect::MakeOval(oval);
655 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000656 }
657
658 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700659 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700660 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700661 return;
662 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000663
robertphillips514450c2015-11-24 05:36:02 -0800664 GrStrokeInfo strokeInfo(paint);
665 SkASSERT(!strokeInfo.isDashed());
666
robertphillips2e1e51f2015-10-15 08:01:48 -0700667 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000668}
669
670#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000671
672///////////////////////////////////////////////////////////////////////////////
673
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000674void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
675 const SkPaint& paint, const SkMatrix* prePathMatrix,
676 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800677 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800678 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
679 bool isClosed;
680 SkRect rect;
681 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
682 this->drawRect(draw, rect, paint);
683 return;
684 }
685 if (origSrcPath.isOval(&rect)) {
686 this->drawOval(draw, rect, paint);
687 return;
688 }
689 SkRRect rrect;
690 if (origSrcPath.isRRect(&rrect)) {
691 this->drawRRect(draw, rrect, paint);
692 return;
693 }
694 }
695
joshualitt5531d512014-12-17 15:50:11 -0800696 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800697 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000698
robertphillips7bceedc2015-12-01 12:51:26 -0800699 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsccb1b572015-05-27 11:02:55 -0700700 fClip, origSrcPath, paint,
701 *draw.fMatrix, prePathMatrix,
702 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000703}
704
705static const int kBmpSmallTileSize = 1 << 10;
706
707static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
708 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
709 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
710 return tilesX * tilesY;
711}
712
reed85d91782015-09-10 14:33:38 -0700713static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000714 if (maxTileSize <= kBmpSmallTileSize) {
715 return maxTileSize;
716 }
717
718 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
719 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
720
721 maxTileTotalTileSize *= maxTileSize * maxTileSize;
722 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
723
724 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
725 return kBmpSmallTileSize;
726 } else {
727 return maxTileSize;
728 }
729}
730
731// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
732// pixels from the bitmap are necessary.
bsalomon74f681d2015-06-23 14:38:48 -0700733static void determine_clipped_src_rect(const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800734 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800735 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700736 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000737 const SkRect* srcRectPtr,
738 SkIRect* clippedSrcIRect) {
robertphillips7bceedc2015-12-01 12:51:26 -0800739 clip.getConservativeBounds(rt->width(), rt->height(), clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000740 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800741 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742 clippedSrcIRect->setEmpty();
743 return;
744 }
745 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
746 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700747 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000748 // we've setup src space 0,0 to map to the top left of the src rect.
749 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000750 if (!clippedSrcRect.intersect(*srcRectPtr)) {
751 clippedSrcIRect->setEmpty();
752 return;
753 }
754 }
755 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700756 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000757 if (!clippedSrcIRect->intersect(bmpBounds)) {
758 clippedSrcIRect->setEmpty();
759 }
760}
761
reed85d91782015-09-10 14:33:38 -0700762bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
763 const SkMatrix& viewMatrix,
764 const GrTextureParams& params,
765 const SkRect* srcRectPtr,
766 int maxTileSize,
767 int* tileSize,
768 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800769 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700770 // if it's larger than the max tile size, then we have no choice but tiling.
771 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
772 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(),
773 srcRectPtr, clippedSubset);
774 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
775 return true;
776 }
777
bsalomon1a1d0b82015-10-16 07:49:42 -0700778 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700779 const size_t area = imageRect.width() * imageRect.height();
780 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
781 return false;
782 }
783
reed85d91782015-09-10 14:33:38 -0700784 // At this point we know we could do the draw by uploading the entire bitmap
785 // as a texture. However, if the texture would be large compared to the
786 // cache size and we don't require most of it for this draw then tile to
787 // reduce the amount of upload and cache spill.
788
789 // assumption here is that sw bitmap size is a good proxy for its size as
790 // a texture
791 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
792 size_t cacheSize;
793 fContext->getResourceCacheLimits(nullptr, &cacheSize);
794 if (bmpSize < cacheSize / 2) {
795 return false;
796 }
797
bsalomon1a1d0b82015-10-16 07:49:42 -0700798 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
799 // tiling memory savings would be < 50%.
reed85d91782015-09-10 14:33:38 -0700800 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(), srcRectPtr,
801 clippedSubset);
802 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
803 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
804 kBmpSmallTileSize * kBmpSmallTileSize;
805
806 return usedTileBytes < 2 * bmpSize;
807}
808
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000809bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800810 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000811 const GrTextureParams& params,
812 const SkRect* srcRectPtr,
813 int maxTileSize,
814 int* tileSize,
815 SkIRect* clippedSrcRect) const {
joshualittce894002016-01-11 13:29:31 -0800816 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000817 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700818 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000819 return false;
820 }
821
reed85d91782015-09-10 14:33:38 -0700822 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
823 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
824}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000825
reed85d91782015-09-10 14:33:38 -0700826bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
827 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
828 const SkMatrix& viewMatrix) const {
joshualittce894002016-01-11 13:29:31 -0800829 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700830 // if image is explictly texture backed then just use the texture
831 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000832 return false;
833 }
834
reed85d91782015-09-10 14:33:38 -0700835 GrTextureParams params;
836 bool doBicubic;
837 GrTextureParams::FilterMode textureFilterMode =
838 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
839
840 int tileFilterPad;
841 if (doBicubic) {
842 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
843 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
844 tileFilterPad = 0;
845 } else {
846 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000847 }
reed85d91782015-09-10 14:33:38 -0700848 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000849
bsalomon8c07b7a2015-11-02 11:36:52 -0800850 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000851
reed85d91782015-09-10 14:33:38 -0700852 // these are output, which we safely ignore, as we just want to know the predicate
853 int outTileSize;
854 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000855
reed85d91782015-09-10 14:33:38 -0700856 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
857 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000858}
859
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000860void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000861 const SkBitmap& bitmap,
862 const SkMatrix& m,
863 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800864 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800865 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800866 SkMatrix viewMatrix;
867 viewMatrix.setConcat(*origDraw.fMatrix, m);
868 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800869 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonb1b01992015-11-18 10:56:08 -0800870 // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
bsalomonf1ecd212015-12-09 17:06:02 -0800871 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
872 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800873 return;
874 }
bsalomonb1b01992015-11-18 10:56:08 -0800875 int maxTileSize = fContext->caps()->maxTileSize();
876
877 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
878 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
879 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
880 paint.isAntiAlias() &&
881 bitmap.width() <= maxTileSize &&
882 bitmap.height() <= maxTileSize;
883
884 bool skipTileCheck = drawAA || paint.getMaskFilter();
885
886 if (!skipTileCheck) {
887 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
888 int tileSize;
889 SkIRect clippedSrcRect;
890
891 GrTextureParams params;
892 bool doBicubic;
893 GrTextureParams::FilterMode textureFilterMode =
894 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
895 &doBicubic);
896
897 int tileFilterPad;
898
899 if (doBicubic) {
900 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
901 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
902 tileFilterPad = 0;
903 } else {
904 tileFilterPad = 1;
905 }
906 params.setFilterMode(textureFilterMode);
907
908 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
909 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
910 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
911 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
912 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
913 return;
914 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000915 }
bsalomonb1b01992015-11-18 10:56:08 -0800916 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800917 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
918 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000919}
920
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000921// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000922// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
923// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000924static inline void clamped_outset_with_offset(SkIRect* iRect,
925 int outset,
926 SkPoint* offset,
927 const SkIRect& clamp) {
928 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000929
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000930 int leftClampDelta = clamp.fLeft - iRect->fLeft;
931 if (leftClampDelta > 0) {
932 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933 iRect->fLeft = clamp.fLeft;
934 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000935 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000936 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000937
938 int topClampDelta = clamp.fTop - iRect->fTop;
939 if (topClampDelta > 0) {
940 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000941 iRect->fTop = clamp.fTop;
942 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000943 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000944 }
945
946 if (iRect->fRight > clamp.fRight) {
947 iRect->fRight = clamp.fRight;
948 }
949 if (iRect->fBottom > clamp.fBottom) {
950 iRect->fBottom = clamp.fBottom;
951 }
952}
953
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000954// Break 'bitmap' into several tiles to draw it since it has already
955// been determined to be too large to fit in VRAM
956void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800957 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958 const SkRect& srcRect,
959 const SkIRect& clippedSrcIRect,
960 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800961 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700962 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000963 int tileSize,
964 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800965 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800966
ericrk82e26bf2016-02-25 22:15:29 -0800967 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entry.
ericrk369e9372016-02-05 15:32:36 -0800968 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
969
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000970 // The following pixel lock is technically redundant, but it is desirable
971 // to lock outside of the tile loop to prevent redecoding the whole image
972 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
973 // is larger than the limit of the discardable memory pool.
974 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800975
976 const SkPaint* paint = &origPaint;
977 SkPaint tempPaint;
978 if (origPaint.isAntiAlias() && !fRenderTarget->isUnifiedMultisampled()) {
979 // Drop antialiasing to avoid seams at tile boundaries.
980 tempPaint = origPaint;
981 tempPaint.setAntiAlias(false);
982 paint = &tempPaint;
983 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000984 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
985
986 int nx = bitmap.width() / tileSize;
987 int ny = bitmap.height() / tileSize;
988 for (int x = 0; x <= nx; x++) {
989 for (int y = 0; y <= ny; y++) {
990 SkRect tileR;
991 tileR.set(SkIntToScalar(x * tileSize),
992 SkIntToScalar(y * tileSize),
993 SkIntToScalar((x + 1) * tileSize),
994 SkIntToScalar((y + 1) * tileSize));
995
996 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
997 continue;
998 }
999
1000 if (!tileR.intersect(srcRect)) {
1001 continue;
1002 }
1003
1004 SkBitmap tmpB;
1005 SkIRect iTileR;
1006 tileR.roundOut(&iTileR);
1007 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1008 SkIntToScalar(iTileR.fTop));
1009
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001010 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001011 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001012 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001013 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001014 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001015
robertphillipsec8bb942014-11-21 10:16:25 -08001016 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001017 SkIRect iClampRect;
1018
reeda5517e22015-07-14 10:54:12 -07001019 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001020 // In bleed mode we want to always expand the tile on all edges
1021 // but stay within the bitmap bounds
1022 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1023 } else {
1024 // In texture-domain/clamp mode we only want to expand the
1025 // tile on edges interior to "srcRect" (i.e., we want to
1026 // not bleed across the original clamped edges)
1027 srcRect.roundOut(&iClampRect);
1028 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001029 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1030 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001031 }
1032
1033 if (bitmap.extractSubset(&tmpB, iTileR)) {
1034 // now offset it to make it "local" to our tmp bitmap
1035 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001036 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001037 // de-optimized this determination
1038 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001039 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001040 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001041 tileR,
1042 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -08001043 *paint,
reeda5517e22015-07-14 10:54:12 -07001044 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001045 bicubic,
1046 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001047 }
1048 }
1049 }
1050}
1051
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001052/*
1053 * This is called by drawBitmap(), which has to handle images that may be too
1054 * large to be represented by a single texture.
1055 *
1056 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1057 * and that non-texture portion of the GrPaint has already been setup.
1058 */
1059void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001060 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001061 const SkRect& srcRect,
1062 const GrTextureParams& params,
1063 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001064 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001065 bool bicubic,
1066 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001067 // We should have already handled bitmaps larger than the max texture size.
1068 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1069 bitmap.height() <= fContext->caps()->maxTextureSize());
1070 // Unless the bitmap is inherently texture-backed, we should be respecting the max tile size
1071 // by the time we get here.
1072 SkASSERT(bitmap.getTexture() ||
1073 (bitmap.width() <= fContext->caps()->maxTileSize() &&
1074 bitmap.height() <= fContext->caps()->maxTileSize()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001075
1076 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001077 AutoBitmapTexture abt(fContext, bitmap, params, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001078 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001079 return;
1080 }
1081
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001082 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001083 SkRect paintRect;
1084 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1085 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1086 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1087 SkScalarMul(srcRect.fTop, hInv),
1088 SkScalarMul(srcRect.fRight, wInv),
1089 SkScalarMul(srcRect.fBottom, hInv));
1090
egdaniel79da63f2015-10-09 10:55:16 -07001091 SkMatrix texMatrix;
1092 texMatrix.reset();
1093 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1094 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1095 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1096 // Thus we need to pass in the transform matrix directly to the texture processor used for
1097 // the bitmap draw.
1098 texMatrix.setScale(wInv, hInv);
1099 }
1100
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001101 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001102
1103 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1104 // the rest from the SkPaint.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001105 SkAutoTUnref<const GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001106
reeda5517e22015-07-14 10:54:12 -07001107 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001108 // Use a constrained texture domain to avoid color bleeding
1109 SkScalar left, top, right, bottom;
1110 if (srcRect.width() > SK_Scalar1) {
1111 SkScalar border = SK_ScalarHalf / texture->width();
1112 left = paintRect.left() + border;
1113 right = paintRect.right() - border;
1114 } else {
1115 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1116 }
1117 if (srcRect.height() > SK_Scalar1) {
1118 SkScalar border = SK_ScalarHalf / texture->height();
1119 top = paintRect.top() + border;
1120 bottom = paintRect.bottom() - border;
1121 } else {
1122 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1123 }
1124 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001125 if (bicubic) {
egdaniel79da63f2015-10-09 10:55:16 -07001126 fp.reset(GrBicubicEffect::Create(texture, texMatrix, textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001127 } else {
bsalomon4a339522015-10-06 08:40:50 -07001128 fp.reset(GrTextureDomainEffect::Create(texture,
egdaniel79da63f2015-10-09 10:55:16 -07001129 texMatrix,
joshualitt5531d512014-12-17 15:50:11 -08001130 textureDomain,
1131 GrTextureDomain::kClamp_Mode,
1132 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001133 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001134 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001135 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1136 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
egdaniel79da63f2015-10-09 10:55:16 -07001137 fp.reset(GrBicubicEffect::Create(texture, texMatrix, tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001138 } else {
egdaniel79da63f2015-10-09 10:55:16 -07001139 fp.reset(GrSimpleTextureEffect::Create(texture, texMatrix, params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001140 }
1141
joshualitt33a5fce2015-11-18 13:28:51 -08001142 GrPaint grPaint;
1143 if (!SkPaintToGrPaintWithTexture(this->context(), paint, viewMatrix, fp,
brianosman898235c2016-04-06 07:38:23 -07001144 kAlpha_8_SkColorType == bitmap.colorType(),
brianosmanb461d342016-04-13 13:10:14 -07001145 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001146 return;
1147 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001148
egdaniel79da63f2015-10-09 10:55:16 -07001149 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1150 // We don't have local coords in this case and have previously set the transform
1151 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001152 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001153 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001154 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001155 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001156}
1157
fmalita2d97bc12014-11-20 10:44:58 -08001158bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001159 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001160 const SkImageFilter* filter,
1161 const SkImageFilter::Context& ctx,
1162 SkBitmap* result, SkIPoint* offset) {
joshualittce894002016-01-11 13:29:31 -08001163 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001164 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001165
reed88d064d2015-10-12 11:30:02 -07001166 SkImageFilter::DeviceProxy proxy(this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167
1168 if (filter->canFilterImageGPU()) {
robertphillips1de87df2016-01-14 06:03:29 -08001169 SkBitmap bm;
1170 GrWrapTextureInBitmap(texture, width, height, false, &bm);
robertphillips48e78462016-02-17 13:57:16 -08001171 return filter->filterImageGPUDeprecated(&proxy, bm, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001172 } else {
1173 return false;
1174 }
1175}
1176
1177void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1178 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001179 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001180 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001181 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001182
1183 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1184 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1185 return;
1186 }
1187
robertphillipse1849d12016-03-17 08:26:38 -07001188 int offX = bitmap.pixelRefOrigin().fX;
1189 int offY = bitmap.pixelRefOrigin().fY;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001190 int w = bitmap.width();
1191 int h = bitmap.height();
1192
1193 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001194 // draw sprite neither filters nor tiles.
1195 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001196 if (!texture) {
1197 return;
1198 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001199
bsalomonf1b7a1d2015-09-28 06:26:28 -07001200 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
1201
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001202 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001203 // This bitmap will own the filtered result as a texture.
1204 SkBitmap filteredBitmap;
1205
bsalomon49f085d2014-09-05 13:34:00 -07001206 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001207 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001208 SkMatrix matrix(*draw.fMatrix);
1209 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblancodb64af32015-12-09 10:11:43 -08001210 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-left, -top);
senorblancobe129b22014-08-08 07:14:35 -07001211 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001212 // This cache is transient, and is freed (along with all its contained
1213 // textures) when it goes out of scope.
reed4e23cda2016-01-11 10:56:59 -08001214 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001215 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001216 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001217 texture = (GrTexture*) filteredBitmap.getTexture();
robertphillipse1849d12016-03-17 08:26:38 -07001218 offX = filteredBitmap.pixelRefOrigin().fX;
1219 offY = filteredBitmap.pixelRefOrigin().fY;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001220 w = filteredBitmap.width();
1221 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001222 left += offset.x();
1223 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001224 } else {
1225 return;
1226 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001227 SkASSERT(!GrPixelConfigIsAlphaOnly(texture->config()));
1228 alphaOnly = false;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001229 }
1230
1231 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001232 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001233 GrSimpleTextureEffect::Create(texture, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001234 if (alphaOnly) {
1235 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1236 } else {
1237 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1238 }
brianosman898235c2016-04-06 07:38:23 -07001239 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp,
brianosmanb461d342016-04-13 13:10:14 -07001240 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001241 return;
1242 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001243
bsalomona2e69fc2015-11-05 10:41:43 -08001244 fDrawContext->fillRectToRect(fClip,
1245 grPaint,
1246 SkMatrix::I(),
1247 SkRect::MakeXYWH(SkIntToScalar(left),
1248 SkIntToScalar(top),
1249 SkIntToScalar(w),
1250 SkIntToScalar(h)),
robertphillipse1849d12016-03-17 08:26:38 -07001251 SkRect::MakeXYWH(SkIntToScalar(offX) / texture->width(),
1252 SkIntToScalar(offY) / texture->height(),
1253 SkIntToScalar(w) / texture->width(),
1254 SkIntToScalar(h) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001255}
1256
bsalomonb1b01992015-11-18 10:56:08 -08001257void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1258 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001259 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001260 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001261 CHECK_SHOULD_DRAW(draw);
bsalomonb1b01992015-11-18 10:56:08 -08001262 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001263 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001264 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
1265 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001266 return;
1267 }
bsalomonb1b01992015-11-18 10:56:08 -08001268 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1269 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1270 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1271 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1272 const SkRect* dst = &origDst;
1273 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001274 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001275 if (!src) {
1276 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001277 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001278
bsalomonb1b01992015-11-18 10:56:08 -08001279 SkMatrix srcToDstMatrix;
1280 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1281 return;
1282 }
1283 SkRect tmpSrc, tmpDst;
1284 if (src != &bmpBounds) {
1285 if (!bmpBounds.contains(*src)) {
1286 tmpSrc = *src;
1287 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001288 return; // nothing to draw
1289 }
bsalomonb1b01992015-11-18 10:56:08 -08001290 src = &tmpSrc;
1291 srcToDstMatrix.mapRect(&tmpDst, *src);
1292 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001293 }
1294 }
1295
bsalomonb1b01992015-11-18 10:56:08 -08001296 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001297
bsalomonb1b01992015-11-18 10:56:08 -08001298 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1299 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
1300 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
1301 paint.isAntiAlias() &&
1302 bitmap.width() <= maxTileSize &&
1303 bitmap.height() <= maxTileSize;
1304
1305 bool skipTileCheck = drawAA || paint.getMaskFilter();
1306
1307 if (!skipTileCheck) {
1308 int tileSize;
1309 SkIRect clippedSrcRect;
1310
1311 GrTextureParams params;
1312 bool doBicubic;
1313 GrTextureParams::FilterMode textureFilterMode =
1314 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1315 &doBicubic);
1316
1317 int tileFilterPad;
1318
1319 if (doBicubic) {
1320 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1321 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1322 tileFilterPad = 0;
1323 } else {
1324 tileFilterPad = 1;
1325 }
1326 params.setFilterMode(textureFilterMode);
1327
1328 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1329 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1330 // we have a mask filter.
1331 SkMatrix viewMatrix = *draw.fMatrix;
1332 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1333 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1334 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1335 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1336 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1337 constraint, tileSize, doBicubic);
1338 return;
1339 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001340 }
bsalomonb1b01992015-11-18 10:56:08 -08001341 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001342 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001343}
1344
1345void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1346 int x, int y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001347 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001348 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001349 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001350 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001351
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001352 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001353 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001354
1355 GrRenderTarget* devRT = dev->accessRenderTarget();
1356 GrTexture* devTex;
halcanary96fcdcc2015-08-27 07:41:13 -07001357 if (nullptr == (devTex = devRT->asTexture())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001358 return;
1359 }
1360
robertphillips7b9e8a42014-12-11 08:20:31 -08001361 const SkImageInfo ii = dev->imageInfo();
1362 int w = ii.width();
1363 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001364
1365 SkImageFilter* filter = paint.getImageFilter();
1366 // This bitmap will own the filtered result as a texture.
1367 SkBitmap filteredBitmap;
1368
bsalomon49f085d2014-09-05 13:34:00 -07001369 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001370 SkIPoint offset = SkIPoint::Make(0, 0);
1371 SkMatrix matrix(*draw.fMatrix);
1372 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblancodb64af32015-12-09 10:11:43 -08001373 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-x, -y);
senorblanco55b6d8b2014-07-30 11:26:46 -07001374 // This cache is transient, and is freed (along with all its contained
1375 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001376 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
reed4e23cda2016-01-11 10:56:59 -08001377 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001378 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1379 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001380 devTex = filteredBitmap.getTexture();
1381 w = filteredBitmap.width();
1382 h = filteredBitmap.height();
1383 x += offset.fX;
1384 y += offset.fY;
1385 } else {
1386 return;
1387 }
1388 }
1389
1390 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001391 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001392 GrSimpleTextureEffect::Create(devTex, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001393 if (GrPixelConfigIsAlphaOnly(devTex->config())) {
1394 // Can this happen?
1395 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1396 } else {
1397 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1398 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001399
brianosman898235c2016-04-06 07:38:23 -07001400 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp,
brianosmanb461d342016-04-13 13:10:14 -07001401 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001402 return;
1403 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001404
1405 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1406 SkIntToScalar(y),
1407 SkIntToScalar(w),
1408 SkIntToScalar(h));
1409
1410 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1411 // scratch texture).
1412 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1413 SK_Scalar1 * h / devTex->height());
1414
bsalomona2e69fc2015-11-05 10:41:43 -08001415 fDrawContext->fillRectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001416}
1417
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001418bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
joshualittce894002016-01-11 13:29:31 -08001419 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001420 return filter->canFilterImageGPU();
1421}
1422
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001423bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001424 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001425 SkBitmap* result, SkIPoint* offset) {
joshualittce894002016-01-11 13:29:31 -08001426 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001427 // want explicitly our impl, so guard against a subclass of us overriding it
1428 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1429 return false;
1430 }
1431
1432 SkAutoLockPixels alp(src, !src.getTexture());
1433 if (!src.getTexture() && !src.readyToDraw()) {
1434 return false;
1435 }
1436
1437 GrTexture* texture;
1438 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1439 // must be pushed upstack.
bsalomonafa95e22015-10-12 10:39:46 -07001440 AutoBitmapTexture abt(fContext, src, GrTextureParams::ClampNoFilter(), &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001441 if (!texture) {
1442 return false;
1443 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001444
senorblancod0d37ca2015-04-02 04:54:56 -07001445 return this->filterTexture(fContext, texture, src.width(), src.height(),
1446 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001447}
1448
reeda85d4d02015-05-06 12:56:48 -07001449void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1450 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001451 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001452 SkMatrix viewMatrix = *draw.fMatrix;
1453 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001454 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001455 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001456 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001457 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1458 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001459 return;
reed85d91782015-09-10 14:33:38 -07001460 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001461 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001462 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1463 paint.getFilterQuality(), *draw.fMatrix)) {
1464 // only support tiling as bitmap at the moment, so force raster-version
1465 if (!as_IB(image)->getROPixels(&bm)) {
1466 return;
1467 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001468 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1469 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1470 CHECK_SHOULD_DRAW(draw);
1471 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001472 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1473 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001474 } else if (as_IB(image)->getROPixels(&bm)) {
1475 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001476 }
reeda85d4d02015-05-06 12:56:48 -07001477 }
1478}
1479
1480void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001481 const SkRect& dst, const SkPaint& paint,
1482 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001483 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001484 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001485 CHECK_SHOULD_DRAW(draw);
1486 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001487 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001488 return;
1489 }
1490 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001491 SkMatrix totalMatrix = *draw.fMatrix;
1492 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1493 dst.height() / (src ? src->height() : image->height()));
1494 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001495 // only support tiling as bitmap at the moment, so force raster-version
1496 if (!as_IB(image)->getROPixels(&bm)) {
1497 return;
1498 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001499 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1500 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1501 CHECK_SHOULD_DRAW(draw);
1502 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001503 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001504 } else if (as_IB(image)->getROPixels(&bm)) {
1505 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001506 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001507}
1508
bsalomonf1ecd212015-12-09 17:06:02 -08001509void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001510 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001511 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001512
joshualitt33a5fce2015-11-18 13:28:51 -08001513 CHECK_SHOULD_DRAW(draw);
1514
joshualittedb36442015-11-19 14:29:30 -08001515 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
1516 fRenderTarget->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001517 bool doBicubic;
1518 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001519 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1520 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001521 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001522 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001523
1524 SkRect srcR, dstR;
1525 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001526 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001527 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001528 }
1529 return;
1530 }
1531
bsalomon2bbd0c62015-12-09 12:50:56 -08001532 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
1533 SkAutoTUnref<const GrFragmentProcessor> fp(
1534 producer->createFragmentProcessor(SkMatrix::I(),
1535 SkRect::MakeIWH(producer->width(), producer->height()),
1536 GrTextureProducer::kNo_FilterConstraint, true,
1537 &kMode));
joshualitt33a5fce2015-11-18 13:28:51 -08001538 GrPaint grPaint;
1539 if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, fp,
brianosman898235c2016-04-06 07:38:23 -07001540 producer->isAlphaOnly(),
brianosmanb461d342016-04-13 13:10:14 -07001541 this->surfaceProps().isGammaCorrect(), &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001542 return;
1543 }
1544
bsalomon2bbd0c62015-12-09 12:50:56 -08001545 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1546 producer->height(), center, dst);
1547}
1548
1549void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1550 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001551 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001552 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001553 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001554 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001555 } else {
1556 SkBitmap bm;
1557 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1558 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001559 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001560 } else if (as_IB(image)->getROPixels(&bm)) {
1561 this->drawBitmapNine(draw, bm, center, dst, paint);
1562 }
1563 }
1564}
1565
1566void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1567 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001568 ASSERT_SINGLE_OWNER
bsalomon2bbd0c62015-12-09 12:50:56 -08001569 if (bitmap.getTexture()) {
1570 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001571 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001572 } else {
1573 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001574 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001575 }
joshualitt33a5fce2015-11-18 13:28:51 -08001576}
1577
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001578///////////////////////////////////////////////////////////////////////////////
1579
1580// must be in SkCanvas::VertexMode order
1581static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1582 kTriangles_GrPrimitiveType,
1583 kTriangleStrip_GrPrimitiveType,
1584 kTriangleFan_GrPrimitiveType,
1585};
1586
1587void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1588 int vertexCount, const SkPoint vertices[],
1589 const SkPoint texs[], const SkColor colors[],
1590 SkXfermode* xmode,
1591 const uint16_t indices[], int indexCount,
1592 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001593 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001594 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001595 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001596
halcanary96fcdcc2015-08-27 07:41:13 -07001597 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1598 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001599
halcanary96fcdcc2015-08-27 07:41:13 -07001600 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001601
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001602 SkPaint copy(paint);
1603 copy.setStyle(SkPaint::kStroke_Style);
1604 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001605
bsalomonf1b7a1d2015-09-28 06:26:28 -07001606 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001607 // we ignore the shader if texs is null.
brianosman898235c2016-04-06 07:38:23 -07001608 if (!SkPaintToGrPaintNoShader(this->context(), copy,
brianosmanb461d342016-04-13 13:10:14 -07001609 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001610 return;
1611 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001612
dandov32a311b2014-07-15 19:46:26 -07001613 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001614 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001615 switch (vmode) {
1616 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001617 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001618 break;
1619 case SkCanvas::kTriangleStrip_VertexMode:
1620 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001621 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001622 break;
1623 }
mtklein533eb782014-08-27 10:39:42 -07001624
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001625 VertState state(vertexCount, indices, indexCount);
1626 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001627
dandov32a311b2014-07-15 19:46:26 -07001628 //number of indices for lines per triangle with kLines
1629 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001630
bsalomonf1b7a1d2015-09-28 06:26:28 -07001631 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001632 int i = 0;
1633 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001634 lineIndices[i] = state.f0;
1635 lineIndices[i + 1] = state.f1;
1636 lineIndices[i + 2] = state.f1;
1637 lineIndices[i + 3] = state.f2;
1638 lineIndices[i + 4] = state.f2;
1639 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001640 i += 6;
1641 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001642 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001643 grPaint,
1644 *draw.fMatrix,
1645 kLines_GrPrimitiveType,
1646 vertexCount,
1647 vertices,
1648 texs,
1649 colors,
1650 lineIndices.get(),
1651 indexCount);
1652 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001653 }
1654
bsalomonf1b7a1d2015-09-28 06:26:28 -07001655 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001656
1657 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001658 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001659 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1660 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001661 convertedColors.reset(vertexCount);
1662 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001663 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001664 }
1665 colors = convertedColors.get();
1666 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001667 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001668 if (texs && paint.getShader()) {
1669 if (colors) {
1670 // When there are texs and colors the shader and colors are combined using xmode. A null
1671 // xmode is defined to mean modulate.
1672 SkXfermode::Mode colorMode;
1673 if (xmode) {
1674 if (!xmode->asMode(&colorMode)) {
1675 return;
1676 }
1677 } else {
1678 colorMode = SkXfermode::kModulate_Mode;
1679 }
1680 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
brianosmanb461d342016-04-13 13:10:14 -07001681 false, this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001682 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001683 return;
1684 }
1685 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001686 // We have a shader, but no colors to blend it against.
brianosman898235c2016-04-06 07:38:23 -07001687 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001688 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001689 return;
1690 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001691 }
bsalomonaa48d362015-10-01 08:34:17 -07001692 } else {
1693 if (colors) {
1694 // We have colors, but either have no shader or no texture coords (which implies that
1695 // we should ignore the shader).
brianosman898235c2016-04-06 07:38:23 -07001696 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001697 this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001698 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001699 return;
1700 }
1701 } else {
1702 // No colors and no shaders. Just draw with the paint color.
brianosman898235c2016-04-06 07:38:23 -07001703 if (!SkPaintToGrPaintNoShader(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001704 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001705 return;
1706 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001707 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001708 }
1709
robertphillips2e1e51f2015-10-15 08:01:48 -07001710 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001711 grPaint,
1712 *draw.fMatrix,
1713 primType,
1714 vertexCount,
1715 vertices,
1716 texs,
1717 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001718 indices,
robertphillipsea461502015-05-26 11:38:03 -07001719 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001720}
1721
1722///////////////////////////////////////////////////////////////////////////////
1723
jvanverth31ff7622015-08-07 10:09:28 -07001724void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001725 const SkRect texRect[], const SkColor colors[], int count,
1726 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001727 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001728 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001729 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001730 return;
1731 }
1732
jvanverth31ff7622015-08-07 10:09:28 -07001733 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001734 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001735
reedca109532015-06-25 16:25:25 -07001736 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001737 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001738
jvanverth31ff7622015-08-07 10:09:28 -07001739 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001740 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001741 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
brianosmanb461d342016-04-13 13:10:14 -07001742 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001743 return;
1744 }
1745 } else {
brianosman898235c2016-04-06 07:38:23 -07001746 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001747 this->surfaceProps().isGammaCorrect(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001748 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001749 }
1750 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001751
1752 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001753 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001754}
1755
1756///////////////////////////////////////////////////////////////////////////////
1757
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001758void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001759 size_t byteLength, SkScalar x, SkScalar y,
1760 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001761 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001762 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001763 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001764
jvanverth8c27a182014-10-14 08:45:50 -07001765 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001766 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001767 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001768 return;
1769 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001770
jvanverth8c27a182014-10-14 08:45:50 -07001771 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001772
robertphillips2e1e51f2015-10-15 08:01:48 -07001773 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001774 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001775}
1776
fmalita05c4a432014-09-29 06:29:53 -07001777void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1778 const SkScalar pos[], int scalarsPerPos,
1779 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001780 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001781 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001782 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001783
jvanverth8c27a182014-10-14 08:45:50 -07001784 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001785 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001786 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001787 return;
1788 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001789
jvanverth8c27a182014-10-14 08:45:50 -07001790 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001791
robertphillips2e1e51f2015-10-15 08:01:48 -07001792 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001793 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1794 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001795}
1796
joshualitt9c328182015-03-23 08:13:04 -07001797void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1798 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001799 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001800 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001801 CHECK_SHOULD_DRAW(draw);
1802
1803 SkDEBUGCODE(this->validate();)
1804
robertphillips2e1e51f2015-10-15 08:01:48 -07001805 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001806 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001807}
1808
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001809///////////////////////////////////////////////////////////////////////////////
1810
reedb2db8982014-11-13 12:41:02 -08001811bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001812 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001813}
1814
1815void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001816 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001817
bsalomonc49e8682015-06-30 11:37:35 -07001818 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001819}
1820
1821///////////////////////////////////////////////////////////////////////////////
1822
reed76033be2015-03-14 10:54:31 -07001823SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001824 ASSERT_SINGLE_OWNER
bsalomonf2703d82014-10-28 14:33:06 -07001825 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001826 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001827 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001828 desc.fWidth = cinfo.fInfo.width();
1829 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001830 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001831
1832 SkAutoTUnref<GrTexture> texture;
1833 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001834 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001835
hcm4396fa52014-10-27 21:43:30 -07001836 // layers are never draw in repeat modes, so we can request an approx
1837 // match and ignore any padding.
bsalomoneae62002015-07-31 13:59:30 -07001838 if (kNever_TileUsage == cinfo.fTileUsage) {
1839 texture.reset(fContext->textureProvider()->createApproxTexture(desc));
1840 } else {
bsalomon5ec26ae2016-02-25 08:33:02 -08001841 texture.reset(fContext->textureProvider()->createTexture(desc, SkBudgeted::kYes));
bsalomoneae62002015-07-31 13:59:30 -07001842 }
bsalomonafe30052015-01-16 07:32:33 -08001843
1844 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001845 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001846 return SkGpuDevice::Create(
bsalomon74f681d2015-06-23 14:38:48 -07001847 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, init);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001848 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001849 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001850 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001851 cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary96fcdcc2015-08-27 07:41:13 -07001852 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001853 }
1854}
1855
reede8f30622016-03-23 18:59:25 -07001856sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001857 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001858 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001859 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
reede8f30622016-03-23 18:59:25 -07001860 return SkSurface::MakeRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
1861 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001862}
1863
robertphillips30d2cc62014-09-24 08:52:18 -07001864bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001865 const SkMatrix* matrix, const SkPaint* paint) {
joshualittce894002016-01-11 13:29:31 -08001866 ASSERT_SINGLE_OWNER
robertphillips63242d72014-12-04 08:31:02 -08001867#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001868 // todo: should handle this natively
1869 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001870 return false;
1871 }
1872
halcanary96fcdcc2015-08-27 07:41:13 -07001873 const SkBigPicture::AccelData* data = nullptr;
mtklein9db912c2015-05-19 11:11:26 -07001874 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1875 data = bp->accelData();
1876 }
robertphillips81f71b62014-11-11 04:54:49 -08001877 if (!data) {
1878 return false;
1879 }
1880
robertphillipse5524cd2015-02-20 12:30:26 -08001881 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1882 if (0 == gpuData->numBlocks()) {
1883 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001884 }
1885
robertphillipsfd61ed02014-10-28 07:21:44 -07001886 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001887
robertphillipse5524cd2015-02-20 12:30:26 -08001888 SkIRect iBounds;
1889 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1890 return false;
1891 }
1892
1893 SkRect clipBounds = SkRect::Make(iBounds);
1894
1895 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1896
robertphillips60029a52015-11-09 13:51:06 -08001897 GrLayerHoister::Begin(fContext);
1898
robertphillipsfd61ed02014-10-28 07:21:44 -07001899 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001900 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001901 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001902 &atlasedNeedRendering, &atlasedRecycled,
vbuzinovdded6962015-06-12 08:59:45 -07001903 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001904
1905 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1906
1907 SkTDArray<GrHoistedLayer> needRendering, recycled;
1908
robertphillipse5524cd2015-02-20 12:30:26 -08001909 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1910
robertphillipsfd61ed02014-10-28 07:21:44 -07001911 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001912 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001913 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001914 &needRendering, &recycled,
vbuzinovdded6962015-06-12 08:59:45 -07001915 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001916
1917 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001918
robertphillips64bf7672014-08-21 13:07:35 -07001919 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001920 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
halcanary96fcdcc2015-08-27 07:41:13 -07001921 initialMatrix, nullptr);
robertphillips64bf7672014-08-21 13:07:35 -07001922
robertphillipsfd61ed02014-10-28 07:21:44 -07001923 GrLayerHoister::UnlockLayers(fContext, needRendering);
1924 GrLayerHoister::UnlockLayers(fContext, recycled);
1925 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1926 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips60029a52015-11-09 13:51:06 -08001927 GrLayerHoister::End(fContext);
robertphillips64bf7672014-08-21 13:07:35 -07001928
1929 return true;
robertphillips63242d72014-12-04 08:31:02 -08001930#else
1931 return false;
1932#endif
robertphillips64bf7672014-08-21 13:07:35 -07001933}
1934
reed13ccbf82015-10-20 09:56:52 -07001935SkImageFilter::Cache* SkGpuDevice::NewImageFilterCache() {
1936 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
1937}
1938
senorblancobe129b22014-08-08 07:14:35 -07001939SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001940 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001941 // We always return a transient cache, so it is freed after each
1942 // filter traversal.
reed13ccbf82015-10-20 09:56:52 -07001943 return SkGpuDevice::NewImageFilterCache();
senorblanco55b6d8b2014-07-30 11:26:46 -07001944}
reedf037e0b2014-10-30 11:34:15 -07001945
1946#endif