blob: 8ce4b30ff70a160a58abe7dce0cbe798a25a61da [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"
bsalomon6663acf2016-05-10 09:14:17 -070016#include "GrStyle.h"
egdanielbbcb38d2014-06-19 10:19:29 -070017#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080018#include "SkCanvasPriv.h"
kkinnunenabcfab42015-02-22 22:53:44 -080019#include "SkErrorInternals.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000020#include "SkGlyphCache.h"
bsalomonf276ac52015-10-09 13:36:42 -070021#include "SkGr.h"
bsalomon1433c672016-07-14 07:53:30 -070022#include "SkGrPixelRef.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070023#include "SkGrPriv.h"
reeda85d4d02015-05-06 12:56:48 -070024#include "SkImage_Base.h"
bsalomon1cf6f9b2015-12-08 10:53:43 -080025#include "SkImageCacherator.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000026#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070027#include "SkImageFilterCache.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000028#include "SkMaskFilter.h"
joshualitt33a5fce2015-11-18 13:28:51 -080029#include "SkNinePatchIter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000030#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000031#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070032#include "SkPictureData.h"
reed1e7f5e72016-04-27 07:49:17 -070033#include "SkRasterClip.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000034#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080035#include "SkRecord.h"
robertphillips970587b2016-07-14 14:12:55 -070036#include "SkSpecialImage.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,
brianosman982eb7f2016-06-06 13:10:58 -070081 SkSourceGammaTreatment gammaTreatment,
bsalomonbcf0a522014-10-08 08:40:09 -070082 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040083 SkASSERT(texture);
brianosman982eb7f2016-06-06 13:10:58 -070084 *texture = this->set(context, bitmap, params, gammaTreatment);
Brian Salomon9323b8b2014-10-07 15:07:38 -040085 }
86
bsalomonbcf0a522014-10-08 08:40:09 -070087 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040088 const SkBitmap& bitmap,
brianosman982eb7f2016-06-06 13:10:58 -070089 const GrTextureParams& params,
90 SkSourceGammaTreatment gammaTreatment) {
bsalomonbcf0a522014-10-08 08:40:09 -070091 // Either get the texture directly from the bitmap, or else use the cache and
92 // remember to unref it.
93 if (GrTexture* bmpTexture = bitmap.getTexture()) {
halcanary96fcdcc2015-08-27 07:41:13 -070094 fTexture.reset(nullptr);
bsalomonbcf0a522014-10-08 08:40:09 -070095 return bmpTexture;
96 } else {
brianosman982eb7f2016-06-06 13:10:58 -070097 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params, gammaTreatment));
bsalomonbcf0a522014-10-08 08:40:09 -070098 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -040099 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400100 }
101
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000102private:
bsalomonbcf0a522014-10-08 08:40:09 -0700103 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000104};
105
106///////////////////////////////////////////////////////////////////////////////
107
bsalomon74f681d2015-06-23 14:38:48 -0700108/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
109 should fail. */
110bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
111 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
112 *flags = 0;
113 if (info) {
114 switch (info->alphaType()) {
115 case kPremul_SkAlphaType:
116 break;
117 case kOpaque_SkAlphaType:
118 *flags |= SkGpuDevice::kIsOpaque_Flag;
119 break;
120 default: // If it is unpremul or unknown don't try to render
121 return false;
122 }
123 }
124 if (kClear_InitContents == init) {
125 *flags |= kNeedClear_Flag;
126 }
127 return true;
128}
129
robertphillips24e91282016-04-29 06:46:36 -0700130sk_sp<SkGpuDevice> SkGpuDevice::Make(sk_sp<GrRenderTarget> rt, const SkSurfaceProps* props,
131 InitContents init) {
robertphillipsca6eafc2016-05-17 09:57:46 -0700132 if (!rt || rt->wasDestroyed() || !rt->getContext()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700133 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000134 }
bsalomon74f681d2015-06-23 14:38:48 -0700135 unsigned flags;
halcanary96fcdcc2015-08-27 07:41:13 -0700136 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
137 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700138 }
robertphillipsca6eafc2016-05-17 09:57:46 -0700139
140 const int width = rt->width();
141 const int height = rt->height();
142
143 GrContext* context = rt->getContext();
144
145 sk_sp<GrDrawContext> drawContext(context->drawContext(std::move(rt), props));
146 return sk_sp<SkGpuDevice>(new SkGpuDevice(std::move(drawContext), width, height, flags));
147}
148
149sk_sp<SkBaseDevice> SkGpuDevice::Make(sk_sp<GrDrawContext> drawContext,
150 int width, int height,
151 InitContents init) {
152 if (!drawContext || drawContext->wasAbandoned()) {
153 return nullptr;
154 }
155 unsigned flags;
156 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
157 return nullptr;
158 }
159 return sk_sp<SkBaseDevice>(new SkGpuDevice(std::move(drawContext), width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000160}
161
robertphillips24e91282016-04-29 06:46:36 -0700162sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
163 const SkImageInfo& info, int sampleCount,
164 const SkSurfaceProps* props, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700165 unsigned flags;
166 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700167 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700168 }
169
robertphillipsca6eafc2016-05-17 09:57:46 -0700170 sk_sp<GrDrawContext> drawContext(CreateDrawContext(context, budgeted, info,
171 sampleCount, props));
172 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700173 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700174 }
175
robertphillipsca6eafc2016-05-17 09:57:46 -0700176 return sk_sp<SkGpuDevice>(new SkGpuDevice(std::move(drawContext),
177 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700178}
179
robertphillipsca6eafc2016-05-17 09:57:46 -0700180SkGpuDevice::SkGpuDevice(sk_sp<GrDrawContext> drawContext, int width, int height, unsigned flags)
181 : INHERITED(drawContext->surfaceProps())
182 , fContext(SkRef(drawContext->accessRenderTarget()->getContext()))
183 , fRenderTarget(drawContext->renderTarget())
184 , fDrawContext(std::move(drawContext)) {
bsalomon74f681d2015-06-23 14:38:48 -0700185 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000186
bsalomon74f681d2015-06-23 14:38:48 -0700187 SkAlphaType at = fOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
robertphillipsca6eafc2016-05-17 09:57:46 -0700188 SkImageInfo info = fRenderTarget->surfacePriv().info(at).makeWH(width, height);
189 SkPixelRef* pr = new SkGrPixelRef(info, fRenderTarget.get());
bsalomonafbf2d62014-09-30 12:18:44 -0700190 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700191 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700192
bsalomone63ffef2016-02-05 07:17:34 -0800193 if (flags & kNeedClear_Flag) {
194 this->clearAll();
195 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000196}
197
robertphillipsca6eafc2016-05-17 09:57:46 -0700198sk_sp<GrDrawContext> SkGpuDevice::CreateDrawContext(GrContext* context,
199 SkBudgeted budgeted,
200 const SkImageInfo& origInfo,
201 int sampleCount,
202 const SkSurfaceProps* surfaceProps) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000203 if (kUnknown_SkColorType == origInfo.colorType() ||
204 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700205 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000206 }
207
bsalomonafe30052015-01-16 07:32:33 -0800208 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700209 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800210 }
211
reede5ea5002014-09-03 11:54:58 -0700212 SkColorType ct = origInfo.colorType();
213 SkAlphaType at = origInfo.alphaType();
brianosmanb109b8c2016-06-16 13:03:24 -0700214 SkColorSpace* cs = origInfo.colorSpace();
brianosman6b086522016-04-14 12:39:00 -0700215 if (kRGB_565_SkColorType == ct || kGray_8_SkColorType == ct) {
reede5ea5002014-09-03 11:54:58 -0700216 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800217 }
218 if (kOpaque_SkAlphaType != at) {
219 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000220 }
brianosman6b086522016-04-14 12:39:00 -0700221
brianosmanb109b8c2016-06-16 13:03:24 -0700222 GrPixelConfig origConfig = SkImageInfo2GrPixelConfig(ct, at, cs, *context->caps());
brianosman6b086522016-04-14 12:39:00 -0700223 if (!context->caps()->isConfigRenderable(origConfig, sampleCount > 0)) {
224 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
225 ct = kN32_SkColorType;
226 }
227
brianosmanb109b8c2016-06-16 13:03:24 -0700228 GrPixelConfig config = SkImageInfo2GrPixelConfig(ct, at, cs, *context->caps());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000229
robertphillipsca6eafc2016-05-17 09:57:46 -0700230 return context->newDrawContext(SkBackingFit::kExact, // Why exact?
231 origInfo.width(), origInfo.height(),
232 config, sampleCount,
233 kDefault_GrSurfaceOrigin, surfaceProps, budgeted);
kkinnunenabcfab42015-02-22 22:53:44 -0800234}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000235
robertphillips970587b2016-07-14 14:12:55 -0700236sk_sp<SkSpecialImage> SkGpuDevice::filterTexture(const SkDraw& draw,
237 SkSpecialImage* srcImg,
238 int left, int top,
239 SkIPoint* offset,
240 const SkImageFilter* filter) {
241 SkASSERT(srcImg->isTextureBacked());
242 SkASSERT(filter);
243
244 SkMatrix matrix = *draw.fMatrix;
245 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
246 const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-left, -top);
247 SkAutoTUnref<SkImageFilterCache> cache(this->getImageFilterCache());
248 SkImageFilter::Context ctx(matrix, clipBounds, cache.get());
249
250 return filter->filterImage(srcImg, ctx, offset);
251}
252
253
robertphillips2302de92016-03-24 07:26:32 -0700254void SkGpuDevice::drawSpriteWithFilter(const SkDraw& draw, const SkBitmap& bitmap,
robertphillips970587b2016-07-14 14:12:55 -0700255 int left, int top, const SkPaint& paint) {
robertphillips090b7622016-03-28 11:07:43 -0700256 ASSERT_SINGLE_OWNER
robertphillips970587b2016-07-14 14:12:55 -0700257 CHECK_SHOULD_DRAW(draw);
robertphillips090b7622016-03-28 11:07:43 -0700258 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpriteWithFilter", fContext);
259
robertphillips970587b2016-07-14 14:12:55 -0700260 SkASSERT(paint.getImageFilter());
261 this->drawSprite(draw, bitmap, left, top, paint);
robertphillips1579e3c2016-03-24 05:01:23 -0700262}
263
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000264///////////////////////////////////////////////////////////////////////////////
265
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000266bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
267 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800268 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000269
270 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700271 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps());
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000272 if (kUnknown_GrPixelConfig == config) {
273 return false;
274 }
275
276 uint32_t flags = 0;
277 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
278 flags = GrContext::kUnpremul_PixelOpsFlag;
279 }
bsalomon74f681d2015-06-23 14:38:48 -0700280 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
281 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000282}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000283
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000284bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
285 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800286 ASSERT_SINGLE_OWNER
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000287 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700288 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fContext->caps());
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000289 if (kUnknown_GrPixelConfig == config) {
290 return false;
291 }
292 uint32_t flags = 0;
293 if (kUnpremul_SkAlphaType == info.alphaType()) {
294 flags = GrContext::kUnpremul_PixelOpsFlag;
295 }
296 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
297
298 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700299 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000300
301 return true;
302}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000303
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000304const SkBitmap& SkGpuDevice::onAccessBitmap() {
joshualittce894002016-01-11 13:29:31 -0800305 ASSERT_SINGLE_OWNER
reed89443ab2014-06-27 11:34:19 -0700306 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000307}
308
reed41e010c2015-06-09 12:16:53 -0700309bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800310 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700311 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
312 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
313 // ... ugh.
314 fLegacyBitmap.notifyPixelsChanged();
315 return false;
316}
317
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000318void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
joshualittce894002016-01-11 13:29:31 -0800319 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000320 INHERITED::onAttachToCanvas(canvas);
321
322 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800323 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000324}
325
326void SkGpuDevice::onDetachFromCanvas() {
joshualittce894002016-01-11 13:29:31 -0800327 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000328 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800329 fClip.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700330 fClipStack.reset(nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000331}
332
333// call this every draw call, to ensure that the context reflects our state,
334// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800335void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800336 ASSERT_SINGLE_OWNER
joshualitt44701df2015-02-23 14:44:57 -0800337 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000338
joshualitt44701df2015-02-23 14:44:57 -0800339 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000340
cdalton846c0512016-05-13 10:25:00 -0700341 fClip.reset(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000342}
343
344GrRenderTarget* SkGpuDevice::accessRenderTarget() {
joshualittce894002016-01-11 13:29:31 -0800345 ASSERT_SINGLE_OWNER
robertphillipsca6eafc2016-05-17 09:57:46 -0700346 return fRenderTarget.get();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000347}
348
robertphillips175dd9b2016-04-28 14:32:04 -0700349GrDrawContext* SkGpuDevice::accessDrawContext() {
350 ASSERT_SINGLE_OWNER
351 return fDrawContext.get();
352}
353
reed8eddfb52014-12-04 07:50:14 -0800354void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800355 ASSERT_SINGLE_OWNER
reed8eddfb52014-12-04 07:50:14 -0800356 GrColor color = 0;
joshualitt5651ee62016-01-11 10:39:11 -0800357 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext);
reed8eddfb52014-12-04 07:50:14 -0800358 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700359 fDrawContext->clear(&rect, color, true);
reed8eddfb52014-12-04 07:50:14 -0800360}
361
robertphillipsca6eafc2016-05-17 09:57:46 -0700362void SkGpuDevice::replaceDrawContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800363 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800364
bsalomon5ec26ae2016-02-25 08:33:02 -0800365 SkBudgeted budgeted = fRenderTarget->resourcePriv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800366
robertphillipsca6eafc2016-05-17 09:57:46 -0700367 sk_sp<GrDrawContext> newDC(CreateDrawContext(this->context(),
368 budgeted,
369 this->imageInfo(),
370 fDrawContext->numColorSamples(),
371 &this->surfaceProps()));
372 if (!newDC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800373 return;
374 }
375
376 if (shouldRetainContent) {
377 if (fRenderTarget->wasDestroyed()) {
378 return;
379 }
robertphillipsca6eafc2016-05-17 09:57:46 -0700380 newDC->copySurface(fDrawContext->asTexture().get(),
381 SkIRect::MakeWH(this->width(), this->height()),
382 SkIPoint::Make(0, 0));
kkinnunenabcfab42015-02-22 22:53:44 -0800383 }
384
robertphillipsca6eafc2016-05-17 09:57:46 -0700385 SkASSERT(fDrawContext->accessRenderTarget() != newDC->accessRenderTarget());
kkinnunenabcfab42015-02-22 22:53:44 -0800386
robertphillipsca6eafc2016-05-17 09:57:46 -0700387 fRenderTarget = newDC->renderTarget();
kkinnunenabcfab42015-02-22 22:53:44 -0800388
bsalomon74f681d2015-06-23 14:38:48 -0700389#ifdef SK_DEBUG
390 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
391 kPremul_SkAlphaType);
392 SkASSERT(info == fLegacyBitmap.info());
393#endif
robertphillipsca6eafc2016-05-17 09:57:46 -0700394 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget.get());
kkinnunenabcfab42015-02-22 22:53:44 -0800395 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700396
robertphillipsca6eafc2016-05-17 09:57:46 -0700397 fDrawContext = newDC;
kkinnunenabcfab42015-02-22 22:53:44 -0800398}
399
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000400///////////////////////////////////////////////////////////////////////////////
401
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000402void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800403 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800404 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800405 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000406
407 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700408 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700409 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700410 return;
411 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000412
robertphillips2e1e51f2015-10-15 08:01:48 -0700413 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000414}
415
416// must be in SkCanvas::PointMode order
417static const GrPrimitiveType gPointMode2PrimtiveType[] = {
418 kPoints_GrPrimitiveType,
419 kLines_GrPrimitiveType,
420 kLineStrip_GrPrimitiveType
421};
422
ethannicholas330bb952015-07-17 06:44:02 -0700423// suppress antialiasing on axis-aligned integer-coordinate lines
424static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
425 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
426 return false;
427 }
428 if (count == 2) {
429 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
430 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800431 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700432 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800433 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700434 // the two end pixels as being the lesser of two evils.
435 if (pts[0].fX == pts[1].fX) {
436 return ((int) pts[0].fX) != pts[0].fX;
437 }
438 if (pts[0].fY == pts[1].fY) {
439 return ((int) pts[0].fY) != pts[0].fY;
440 }
441 }
442 return true;
443}
444
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000445void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
446 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800447 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800448 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800449 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000450
451 SkScalar width = paint.getStrokeWidth();
452 if (width < 0) {
453 return;
454 }
455
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000456 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700457 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700458 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700459 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700460 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700461 return;
462 }
egdaniele61c4112014-06-12 10:24:21 -0700463 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700464 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700465 path.moveTo(pts[0]);
466 path.lineTo(pts[1]);
bsalomon6663acf2016-05-10 09:14:17 -0700467 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, style);
egdaniele61c4112014-06-12 10:24:21 -0700468 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000469 }
470
ethannicholas330bb952015-07-17 06:44:02 -0700471 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000472 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800473 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700474 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000475 draw.drawPoints(mode, count, pts, paint, true);
476 return;
477 }
478
479 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700480 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700481 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700482 return;
483 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000484
robertphillips2e1e51f2015-10-15 08:01:48 -0700485 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700486 grPaint,
487 *draw.fMatrix,
488 gPointMode2PrimtiveType[mode],
489 SkToS32(count),
490 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700491 nullptr,
492 nullptr,
493 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700494 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000495}
496
497///////////////////////////////////////////////////////////////////////////////
498
robertphillipsff55b492015-11-24 07:56:59 -0800499void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800500 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800501 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800502 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000503
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000504
bsalomona7d85ba2016-07-06 11:54:59 -0700505 // A couple reasons we might need to call drawPath.
506 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000507 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700508 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000509 path.addRect(rect);
robertphillips6c7e3252016-04-27 10:47:51 -0700510 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800511 fClip, path, paint,
512 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700513 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000514 return;
515 }
516
517 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700518 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700519 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700520 return;
521 }
Mike Klein744fb732014-06-23 15:13:26 -0400522
bsalomon6663acf2016-05-10 09:14:17 -0700523 GrStyle style(paint);
524 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000525}
526
527///////////////////////////////////////////////////////////////////////////////
528
529void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800530 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800531 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800532 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800533 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000534
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000535 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700536 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700537 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700538 return;
539 }
Mike Klein744fb732014-06-23 15:13:26 -0400540
bsalomon6663acf2016-05-10 09:14:17 -0700541 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000542 if (paint.getMaskFilter()) {
543 // try to hit the fast path for drawing filtered round rects
544
545 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800546 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000547 if (devRRect.allCornersCircular()) {
548 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700549 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
reed1e7f5e72016-04-27 07:49:17 -0700550 draw.fRC->getBounds(),
joshualitt5531d512014-12-17 15:50:11 -0800551 *draw.fMatrix,
552 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000553 SkIRect finalIRect;
554 maskRect.roundOut(&finalIRect);
reed1e7f5e72016-04-27 07:49:17 -0700555 if (draw.fRC->quickReject(finalIRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000556 // clipped out
557 return;
558 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700559 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
robertphillips6c7e3252016-04-27 10:47:51 -0700560 fDrawContext.get(),
joshualitt25d9c152015-02-18 12:29:52 -0800561 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800562 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800563 *draw.fMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700564 style.strokeRec(),
egdanield58a0ba2014-06-11 10:30:05 -0700565 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000566 return;
567 }
568 }
569
570 }
571 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000572 }
573
bsalomon6663acf2016-05-10 09:14:17 -0700574 if (paint.getMaskFilter() || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800575 // The only mask filter the native rrect drawing code could've handle was taken
576 // care of above.
577 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000578 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700579 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000580 path.addRRect(rect);
robertphillips6c7e3252016-04-27 10:47:51 -0700581 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800582 fClip, path, paint,
583 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700584 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000585 return;
586 }
Mike Klein744fb732014-06-23 15:13:26 -0400587
bsalomon6663acf2016-05-10 09:14:17 -0700588 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800589
bsalomon6663acf2016-05-10 09:14:17 -0700590 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000591}
592
robertphillipsd7706102016-02-25 09:28:08 -0800593
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000594void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800595 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800596 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800597 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800598 CHECK_SHOULD_DRAW(draw);
599
robertphillipsd7706102016-02-25 09:28:08 -0800600 if (outer.isEmpty()) {
601 return;
602 }
603
604 if (inner.isEmpty()) {
605 return this->drawRRect(draw, outer, paint);
606 }
607
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000608 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000609
robertphillips0e7029e2015-11-30 05:45:06 -0800610 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800611 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700612 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700613 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700614 return;
615 }
robertphillips00095892016-02-29 13:50:40 -0800616
617 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
618 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000619 }
620
621 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700622 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000623 path.addRRect(outer);
624 path.addRRect(inner);
625 path.setFillType(SkPath::kEvenOdd_FillType);
626
robertphillips6c7e3252016-04-27 10:47:51 -0700627 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillips0e7029e2015-11-30 05:45:06 -0800628 fClip, path, paint,
629 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700630 draw.fRC->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000631}
632
633
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000634/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000635
robertphillips514450c2015-11-24 05:36:02 -0800636void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800637 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800638 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800639 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000640
robertphillips514450c2015-11-24 05:36:02 -0800641 // Presumably the path effect warps this to something other than an oval
642 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000643 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700644 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000645 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700646 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000647 return;
herb11a7f7f2015-11-24 12:41:00 -0800648 }
649
robertphillips514450c2015-11-24 05:36:02 -0800650 if (paint.getMaskFilter()) {
651 // The RRect path can handle special case blurring
652 SkRRect rr = SkRRect::MakeOval(oval);
653 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000654 }
655
656 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700657 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700658 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700659 return;
660 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000661
bsalomon6663acf2016-05-10 09:14:17 -0700662 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000663}
664
665#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000666
667///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700668void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
669 const SkDraw& draw,
670 const SkPaint& origPaint) {
671 ASSERT_SINGLE_OWNER
672 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext);
673 CHECK_SHOULD_DRAW(draw);
674
675 // Adding support for round capping would require a GrDrawContext::fillRRectWithLocalMatrix
676 // entry point
677 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
678 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
679 SkASSERT(!origPaint.getPathEffect());
680 SkASSERT(!origPaint.getMaskFilter());
681
682 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
683 SkASSERT(halfWidth > 0);
684
685 SkVector v = points[1] - points[0];
686
687 SkScalar length = SkPoint::Normalize(&v);
688 if (!length) {
689 v.fX = 1.0f;
690 v.fY = 0.0f;
691 }
692
693 SkPaint newPaint(origPaint);
694 newPaint.setStyle(SkPaint::kFill_Style);
695
696 SkScalar xtraLength = 0.0f;
697 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
698 xtraLength = halfWidth;
699 }
700
701 SkPoint mid = points[0] + points[1];
702 mid.scale(0.5f);
703
704 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
705 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
706 SkMatrix m;
707 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
708
709 SkMatrix local = m;
710
711 m.postConcat(*draw.fMatrix);
712
713 GrPaint grPaint;
714 if (!SkPaintToGrPaint(this->context(), newPaint, m,
715 this->surfaceProps().isGammaCorrect(), &grPaint)) {
716 return;
717 }
718
719 fDrawContext->fillRectWithLocalMatrix(fClip, grPaint, m, rect, local);
720}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000721
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000722void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
723 const SkPaint& paint, const SkMatrix* prePathMatrix,
724 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800725 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800726 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700727 SkPoint points[2];
728 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
729 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
730 draw.fMatrix->preservesRightAngles() && origSrcPath.isLine(points)) {
731 // Path-based stroking looks better for thin rects
732 SkScalar strokeWidth = draw.fMatrix->getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700733 if (strokeWidth >= 1.0f) {
robertphillips0851d2d2016-06-02 05:21:34 -0700734 // Round capping support is currently disabled b.c. it would require
735 // a RRect batch that takes a localMatrix.
736 this->drawStrokedLine(points, draw, paint);
737 return;
738 }
739 }
robertphillipsff55b492015-11-24 07:56:59 -0800740 bool isClosed;
741 SkRect rect;
742 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
743 this->drawRect(draw, rect, paint);
744 return;
745 }
746 if (origSrcPath.isOval(&rect)) {
747 this->drawOval(draw, rect, paint);
748 return;
749 }
750 SkRRect rrect;
751 if (origSrcPath.isRRect(&rrect)) {
752 this->drawRRect(draw, rrect, paint);
753 return;
754 }
755 }
756
joshualitt5531d512014-12-17 15:50:11 -0800757 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800758 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000759
robertphillips6c7e3252016-04-27 10:47:51 -0700760 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsccb1b572015-05-27 11:02:55 -0700761 fClip, origSrcPath, paint,
762 *draw.fMatrix, prePathMatrix,
reed1e7f5e72016-04-27 07:49:17 -0700763 draw.fRC->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764}
765
766static const int kBmpSmallTileSize = 1 << 10;
767
768static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
769 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
770 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
771 return tilesX * tilesY;
772}
773
reed85d91782015-09-10 14:33:38 -0700774static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000775 if (maxTileSize <= kBmpSmallTileSize) {
776 return maxTileSize;
777 }
778
779 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
780 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
781
782 maxTileTotalTileSize *= maxTileSize * maxTileSize;
783 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
784
785 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
786 return kBmpSmallTileSize;
787 } else {
788 return maxTileSize;
789 }
790}
791
792// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
793// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700794static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800795 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800796 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700797 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000798 const SkRect* srcRectPtr,
799 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700800 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000801 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800802 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000803 clippedSrcIRect->setEmpty();
804 return;
805 }
806 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
807 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700808 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000809 // we've setup src space 0,0 to map to the top left of the src rect.
810 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000811 if (!clippedSrcRect.intersect(*srcRectPtr)) {
812 clippedSrcIRect->setEmpty();
813 return;
814 }
815 }
816 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700817 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000818 if (!clippedSrcIRect->intersect(bmpBounds)) {
819 clippedSrcIRect->setEmpty();
820 }
821}
822
reed85d91782015-09-10 14:33:38 -0700823bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
824 const SkMatrix& viewMatrix,
825 const GrTextureParams& params,
826 const SkRect* srcRectPtr,
827 int maxTileSize,
828 int* tileSize,
829 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800830 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700831 // if it's larger than the max tile size, then we have no choice but tiling.
832 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
robertphillipse5768742016-05-13 11:20:46 -0700833 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(),
834 fClip, viewMatrix, imageRect.size(),
reed85d91782015-09-10 14:33:38 -0700835 srcRectPtr, clippedSubset);
836 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
837 return true;
838 }
839
bsalomon1a1d0b82015-10-16 07:49:42 -0700840 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700841 const size_t area = imageRect.width() * imageRect.height();
842 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
843 return false;
844 }
845
reed85d91782015-09-10 14:33:38 -0700846 // At this point we know we could do the draw by uploading the entire bitmap
847 // as a texture. However, if the texture would be large compared to the
848 // cache size and we don't require most of it for this draw then tile to
849 // reduce the amount of upload and cache spill.
850
851 // assumption here is that sw bitmap size is a good proxy for its size as
852 // a texture
853 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
854 size_t cacheSize;
855 fContext->getResourceCacheLimits(nullptr, &cacheSize);
856 if (bmpSize < cacheSize / 2) {
857 return false;
858 }
859
bsalomon1a1d0b82015-10-16 07:49:42 -0700860 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
861 // tiling memory savings would be < 50%.
robertphillipse5768742016-05-13 11:20:46 -0700862 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(),
863 fClip, viewMatrix, imageRect.size(), srcRectPtr,
reed85d91782015-09-10 14:33:38 -0700864 clippedSubset);
865 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
866 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
867 kBmpSmallTileSize * kBmpSmallTileSize;
868
869 return usedTileBytes < 2 * bmpSize;
870}
871
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000872bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800873 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000874 const GrTextureParams& params,
875 const SkRect* srcRectPtr,
876 int maxTileSize,
877 int* tileSize,
878 SkIRect* clippedSrcRect) const {
joshualittce894002016-01-11 13:29:31 -0800879 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000880 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700881 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000882 return false;
883 }
884
reed85d91782015-09-10 14:33:38 -0700885 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
886 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
887}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000888
reed85d91782015-09-10 14:33:38 -0700889bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
890 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
891 const SkMatrix& viewMatrix) const {
joshualittce894002016-01-11 13:29:31 -0800892 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700893 // if image is explictly texture backed then just use the texture
894 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000895 return false;
896 }
897
reed85d91782015-09-10 14:33:38 -0700898 GrTextureParams params;
899 bool doBicubic;
900 GrTextureParams::FilterMode textureFilterMode =
901 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
902
903 int tileFilterPad;
904 if (doBicubic) {
905 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
906 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
907 tileFilterPad = 0;
908 } else {
909 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000910 }
reed85d91782015-09-10 14:33:38 -0700911 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000912
bsalomon8c07b7a2015-11-02 11:36:52 -0800913 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000914
reed85d91782015-09-10 14:33:38 -0700915 // these are output, which we safely ignore, as we just want to know the predicate
916 int outTileSize;
917 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000918
reed85d91782015-09-10 14:33:38 -0700919 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
920 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000921}
922
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000923void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000924 const SkBitmap& bitmap,
925 const SkMatrix& m,
926 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800927 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800928 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800929 SkMatrix viewMatrix;
930 viewMatrix.setConcat(*origDraw.fMatrix, m);
931 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800932 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonb1b01992015-11-18 10:56:08 -0800933 // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
bsalomonf1ecd212015-12-09 17:06:02 -0800934 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
935 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800936 return;
937 }
bsalomonb1b01992015-11-18 10:56:08 -0800938 int maxTileSize = fContext->caps()->maxTileSize();
939
940 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
941 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
robertphillips071457b2016-05-16 09:50:03 -0700942 bool drawAA = !fDrawContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -0800943 paint.isAntiAlias() &&
944 bitmap.width() <= maxTileSize &&
945 bitmap.height() <= maxTileSize;
946
947 bool skipTileCheck = drawAA || paint.getMaskFilter();
948
949 if (!skipTileCheck) {
950 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
951 int tileSize;
952 SkIRect clippedSrcRect;
953
954 GrTextureParams params;
955 bool doBicubic;
956 GrTextureParams::FilterMode textureFilterMode =
957 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
958 &doBicubic);
959
960 int tileFilterPad;
961
962 if (doBicubic) {
963 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
964 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
965 tileFilterPad = 0;
966 } else {
967 tileFilterPad = 1;
968 }
969 params.setFilterMode(textureFilterMode);
970
971 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
972 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
973 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
974 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
975 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
976 return;
977 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000978 }
bsalomonb1b01992015-11-18 10:56:08 -0800979 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800980 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
981 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982}
983
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000984// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000985// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
986// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000987static inline void clamped_outset_with_offset(SkIRect* iRect,
988 int outset,
989 SkPoint* offset,
990 const SkIRect& clamp) {
991 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000992
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000993 int leftClampDelta = clamp.fLeft - iRect->fLeft;
994 if (leftClampDelta > 0) {
995 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000996 iRect->fLeft = clamp.fLeft;
997 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000998 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000999 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001000
1001 int topClampDelta = clamp.fTop - iRect->fTop;
1002 if (topClampDelta > 0) {
1003 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001004 iRect->fTop = clamp.fTop;
1005 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001006 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001007 }
1008
1009 if (iRect->fRight > clamp.fRight) {
1010 iRect->fRight = clamp.fRight;
1011 }
1012 if (iRect->fBottom > clamp.fBottom) {
1013 iRect->fBottom = clamp.fBottom;
1014 }
1015}
1016
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001017// Break 'bitmap' into several tiles to draw it since it has already
1018// been determined to be too large to fit in VRAM
1019void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001020 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001021 const SkRect& srcRect,
1022 const SkIRect& clippedSrcIRect,
1023 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -08001024 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -07001025 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001026 int tileSize,
1027 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -08001028 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -08001029
ericrk983294f2016-04-18 09:14:00 -07001030 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -08001031 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -07001032 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -08001033
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001034 // The following pixel lock is technically redundant, but it is desirable
1035 // to lock outside of the tile loop to prevent redecoding the whole image
1036 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1037 // is larger than the limit of the discardable memory pool.
1038 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -08001039
1040 const SkPaint* paint = &origPaint;
1041 SkPaint tempPaint;
robertphillips071457b2016-05-16 09:50:03 -07001042 if (origPaint.isAntiAlias() && !fDrawContext->isUnifiedMultisampled()) {
bsalomonc55271f2015-11-09 11:55:57 -08001043 // Drop antialiasing to avoid seams at tile boundaries.
1044 tempPaint = origPaint;
1045 tempPaint.setAntiAlias(false);
1046 paint = &tempPaint;
1047 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001048 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1049
1050 int nx = bitmap.width() / tileSize;
1051 int ny = bitmap.height() / tileSize;
1052 for (int x = 0; x <= nx; x++) {
1053 for (int y = 0; y <= ny; y++) {
1054 SkRect tileR;
1055 tileR.set(SkIntToScalar(x * tileSize),
1056 SkIntToScalar(y * tileSize),
1057 SkIntToScalar((x + 1) * tileSize),
1058 SkIntToScalar((y + 1) * tileSize));
1059
1060 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1061 continue;
1062 }
1063
1064 if (!tileR.intersect(srcRect)) {
1065 continue;
1066 }
1067
1068 SkBitmap tmpB;
1069 SkIRect iTileR;
1070 tileR.roundOut(&iTileR);
1071 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1072 SkIntToScalar(iTileR.fTop));
1073
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001074 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001075 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001076 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001077 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001078 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001079
robertphillipsec8bb942014-11-21 10:16:25 -08001080 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001081 SkIRect iClampRect;
1082
reeda5517e22015-07-14 10:54:12 -07001083 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001084 // In bleed mode we want to always expand the tile on all edges
1085 // but stay within the bitmap bounds
1086 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1087 } else {
1088 // In texture-domain/clamp mode we only want to expand the
1089 // tile on edges interior to "srcRect" (i.e., we want to
1090 // not bleed across the original clamped edges)
1091 srcRect.roundOut(&iClampRect);
1092 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001093 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1094 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001095 }
1096
1097 if (bitmap.extractSubset(&tmpB, iTileR)) {
1098 // now offset it to make it "local" to our tmp bitmap
1099 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001100 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001101 // de-optimized this determination
1102 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001103 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001104 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001105 tileR,
1106 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -08001107 *paint,
reeda5517e22015-07-14 10:54:12 -07001108 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001109 bicubic,
1110 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001111 }
1112 }
1113 }
1114}
1115
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001116/*
1117 * This is called by drawBitmap(), which has to handle images that may be too
1118 * large to be represented by a single texture.
1119 *
1120 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1121 * and that non-texture portion of the GrPaint has already been setup.
1122 */
1123void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001124 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001125 const SkRect& srcRect,
1126 const GrTextureParams& params,
1127 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001128 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001129 bool bicubic,
1130 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001131 // We should have already handled bitmaps larger than the max texture size.
1132 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1133 bitmap.height() <= fContext->caps()->maxTextureSize());
1134 // Unless the bitmap is inherently texture-backed, we should be respecting the max tile size
1135 // by the time we get here.
1136 SkASSERT(bitmap.getTexture() ||
1137 (bitmap.width() <= fContext->caps()->maxTileSize() &&
1138 bitmap.height() <= fContext->caps()->maxTileSize()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001139
1140 GrTexture* texture;
brianosman982eb7f2016-06-06 13:10:58 -07001141 SkSourceGammaTreatment gammaTreatment = this->surfaceProps().isGammaCorrect()
1142 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIgnore;
1143 AutoBitmapTexture abt(fContext, bitmap, params, gammaTreatment, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001144 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001145 return;
1146 }
1147
brianosman54f30c12016-07-18 10:53:52 -07001148 SkColorSpace* dstColorSpace = nullptr; // XFORMTODO
1149 sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(bitmap.colorSpace(),
1150 dstColorSpace);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001151 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001152 SkRect paintRect;
1153 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1154 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1155 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1156 SkScalarMul(srcRect.fTop, hInv),
1157 SkScalarMul(srcRect.fRight, wInv),
1158 SkScalarMul(srcRect.fBottom, hInv));
1159
egdaniel79da63f2015-10-09 10:55:16 -07001160 SkMatrix texMatrix;
1161 texMatrix.reset();
1162 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1163 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1164 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1165 // Thus we need to pass in the transform matrix directly to the texture processor used for
1166 // the bitmap draw.
1167 texMatrix.setScale(wInv, hInv);
1168 }
1169
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001170 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001171
1172 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1173 // the rest from the SkPaint.
bungeman06ca8ec2016-06-09 08:01:03 -07001174 sk_sp<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001175
reeda5517e22015-07-14 10:54:12 -07001176 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001177 // Use a constrained texture domain to avoid color bleeding
1178 SkScalar left, top, right, bottom;
1179 if (srcRect.width() > SK_Scalar1) {
1180 SkScalar border = SK_ScalarHalf / texture->width();
1181 left = paintRect.left() + border;
1182 right = paintRect.right() - border;
1183 } else {
1184 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1185 }
1186 if (srcRect.height() > SK_Scalar1) {
1187 SkScalar border = SK_ScalarHalf / texture->height();
1188 top = paintRect.top() + border;
1189 bottom = paintRect.bottom() - border;
1190 } else {
1191 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1192 }
1193 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001194 if (bicubic) {
brianosman54f30c12016-07-18 10:53:52 -07001195 fp = GrBicubicEffect::Make(texture, std::move(colorSpaceXform), texMatrix,
1196 textureDomain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001197 } else {
brianosman54f30c12016-07-18 10:53:52 -07001198 fp = GrTextureDomainEffect::Make(texture, std::move(colorSpaceXform), texMatrix,
1199 textureDomain, GrTextureDomain::kClamp_Mode,
1200 params.filterMode());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001201 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001202 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001203 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1204 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
brianosman54f30c12016-07-18 10:53:52 -07001205 fp = GrBicubicEffect::Make(texture, std::move(colorSpaceXform), texMatrix, tileModes);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001206 } else {
brianosman54f30c12016-07-18 10:53:52 -07001207 fp = GrSimpleTextureEffect::Make(texture, std::move(colorSpaceXform), texMatrix, params);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001208 }
1209
joshualitt33a5fce2015-11-18 13:28:51 -08001210 GrPaint grPaint;
bungeman06ca8ec2016-06-09 08:01:03 -07001211 if (!SkPaintToGrPaintWithTexture(this->context(), paint, viewMatrix, std::move(fp),
brianosman898235c2016-04-06 07:38:23 -07001212 kAlpha_8_SkColorType == bitmap.colorType(),
brianosmanb461d342016-04-13 13:10:14 -07001213 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001214 return;
1215 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001216
egdaniel79da63f2015-10-09 10:55:16 -07001217 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1218 // We don't have local coords in this case and have previously set the transform
1219 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001220 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001221 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001222 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001223 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001224}
1225
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001226void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1227 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001228 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001229 CHECK_SHOULD_DRAW(draw);
robertphillips970587b2016-07-14 14:12:55 -07001230 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001231
robertphillips970587b2016-07-14 14:12:55 -07001232 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001233 return;
1234 }
1235
robertphillips970587b2016-07-14 14:12:55 -07001236 sk_sp<GrTexture> texture = sk_ref_sp(bitmap.getTexture());
joshualitt5f5a8d72015-02-25 14:09:45 -08001237 if (!texture) {
robertphillips970587b2016-07-14 14:12:55 -07001238 SkAutoLockPixels alp(bitmap, true);
1239 if (!bitmap.readyToDraw()) {
1240 return;
1241 }
1242
1243 // draw sprite neither filters nor tiles.
1244 texture.reset(GrRefCachedBitmapTexture(fContext, bitmap,
1245 GrTextureParams::ClampNoFilter(),
1246 SkSourceGammaTreatment::kRespect));
1247 if (!texture) {
1248 return;
1249 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001250 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001251
robertphillips970587b2016-07-14 14:12:55 -07001252 SkIRect srcRect = SkIRect::MakeXYWH(bitmap.pixelRefOrigin().fX,
1253 bitmap.pixelRefOrigin().fY,
1254 bitmap.width(),
1255 bitmap.height());
bsalomonf1b7a1d2015-09-28 06:26:28 -07001256
robertphillips970587b2016-07-14 14:12:55 -07001257 sk_sp<SkSpecialImage> srcImg(SkSpecialImage::MakeFromGpu(srcRect,
1258 bitmap.getGenerationID(),
1259 std::move(texture),
robertphillips1b5f9682016-07-15 08:01:12 -07001260 &this->surfaceProps()));
robertphillips970587b2016-07-14 14:12:55 -07001261
1262 this->drawSpecial(draw, srcImg.get(), left, top, paint);
1263}
1264
1265
1266void SkGpuDevice::drawSpecial(const SkDraw& draw,
1267 SkSpecialImage* special1,
1268 int left, int top,
1269 const SkPaint& paint) {
robertphillips1b5f9682016-07-15 08:01:12 -07001270 ASSERT_SINGLE_OWNER
1271 CHECK_SHOULD_DRAW(draw);
1272 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext);
robertphillips970587b2016-07-14 14:12:55 -07001273
1274 SkIPoint offset = { 0, 0 };
1275
1276 sk_sp<SkSpecialImage> result;
1277 if (paint.getImageFilter()) {
1278 result = this->filterTexture(draw, special1, left, top,
1279 &offset,
1280 paint.getImageFilter());
1281 if (!result) {
1282 return;
1283 }
1284 } else {
1285 result = sk_ref_sp(special1);
1286 }
1287
1288 SkASSERT(result->isTextureBacked());
1289 sk_sp<GrTexture> texture = result->asTextureRef(fContext);
1290
1291 SkPaint tmpUnfiltered(paint);
1292 tmpUnfiltered.setImageFilter(nullptr);
1293
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001294 GrPaint grPaint;
brianosman54f30c12016-07-18 10:53:52 -07001295 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture.get(), nullptr,
1296 SkMatrix::I()));
robertphillips1b5f9682016-07-15 08:01:12 -07001297 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bungeman06ca8ec2016-06-09 08:01:03 -07001298 fp = GrFragmentProcessor::MulOutputByInputUnpremulColor(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001299 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001300 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001301 }
robertphillips970587b2016-07-14 14:12:55 -07001302 if (!SkPaintToGrPaintReplaceShader(this->context(), tmpUnfiltered, std::move(fp),
brianosmanb461d342016-04-13 13:10:14 -07001303 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001304 return;
1305 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001306
robertphillips970587b2016-07-14 14:12:55 -07001307 const SkIRect& subset = result->subset();
1308
bsalomona2e69fc2015-11-05 10:41:43 -08001309 fDrawContext->fillRectToRect(fClip,
1310 grPaint,
1311 SkMatrix::I(),
robertphillips970587b2016-07-14 14:12:55 -07001312 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX, top + offset.fY,
1313 subset.width(), subset.height())),
1314 SkRect::MakeXYWH(SkIntToScalar(subset.fLeft) / texture->width(),
1315 SkIntToScalar(subset.fTop) / texture->height(),
1316 SkIntToScalar(subset.width()) / texture->width(),
1317 SkIntToScalar(subset.height()) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001318}
1319
bsalomonb1b01992015-11-18 10:56:08 -08001320void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1321 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001322 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001323 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001324 CHECK_SHOULD_DRAW(draw);
bsalomonb1b01992015-11-18 10:56:08 -08001325 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001326 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001327 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
1328 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001329 return;
1330 }
bsalomonb1b01992015-11-18 10:56:08 -08001331 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1332 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1333 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1334 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1335 const SkRect* dst = &origDst;
1336 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001337 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001338 if (!src) {
1339 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001340 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001341
bsalomonb1b01992015-11-18 10:56:08 -08001342 SkMatrix srcToDstMatrix;
1343 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1344 return;
1345 }
1346 SkRect tmpSrc, tmpDst;
1347 if (src != &bmpBounds) {
1348 if (!bmpBounds.contains(*src)) {
1349 tmpSrc = *src;
1350 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001351 return; // nothing to draw
1352 }
bsalomonb1b01992015-11-18 10:56:08 -08001353 src = &tmpSrc;
1354 srcToDstMatrix.mapRect(&tmpDst, *src);
1355 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001356 }
1357 }
1358
bsalomonb1b01992015-11-18 10:56:08 -08001359 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001360
bsalomonb1b01992015-11-18 10:56:08 -08001361 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1362 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
robertphillips071457b2016-05-16 09:50:03 -07001363 bool drawAA = !fDrawContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -08001364 paint.isAntiAlias() &&
1365 bitmap.width() <= maxTileSize &&
1366 bitmap.height() <= maxTileSize;
1367
1368 bool skipTileCheck = drawAA || paint.getMaskFilter();
1369
1370 if (!skipTileCheck) {
1371 int tileSize;
1372 SkIRect clippedSrcRect;
1373
1374 GrTextureParams params;
1375 bool doBicubic;
1376 GrTextureParams::FilterMode textureFilterMode =
1377 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1378 &doBicubic);
1379
1380 int tileFilterPad;
1381
1382 if (doBicubic) {
1383 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1384 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1385 tileFilterPad = 0;
1386 } else {
1387 tileFilterPad = 1;
1388 }
1389 params.setFilterMode(textureFilterMode);
1390
1391 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1392 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1393 // we have a mask filter.
1394 SkMatrix viewMatrix = *draw.fMatrix;
1395 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1396 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1397 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1398 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1399 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1400 constraint, tileSize, doBicubic);
1401 return;
1402 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001403 }
bsalomonb1b01992015-11-18 10:56:08 -08001404 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001405 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001406}
1407
robertphillips6451a0c2016-07-18 08:31:31 -07001408sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
1409 SkASSERT(!bitmap.getTexture());
1410
1411 SkAutoLockPixels alp(bitmap, true);
1412 if (!bitmap.readyToDraw()) {
1413 return nullptr;
1414 }
1415
1416 GrTexture* texture;
1417 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(),
1418 SkSourceGammaTreatment::kRespect, &texture);
1419 if (!texture) {
1420 return nullptr;
1421 }
1422
1423 return SkSpecialImage::MakeFromGpu(bitmap.bounds(),
1424 bitmap.getGenerationID(),
1425 sk_ref_sp(texture),
1426 &this->surfaceProps());
1427}
1428
reede51c3562016-07-19 14:33:20 -07001429sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001430 SkPixmap pm;
1431 if (image->isTextureBacked()) {
1432 GrTexture* texture = as_IB(image)->peekTexture();
1433
1434 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(image->width(), image->height()),
1435 image->uniqueID(),
1436 sk_ref_sp(texture),
1437 &this->surfaceProps());
1438 } else if (image->peekPixels(&pm)) {
1439 SkBitmap bm;
1440
1441 bm.installPixels(pm);
1442 return this->makeSpecial(bm);
1443 } else {
1444 return nullptr;
1445 }
1446}
1447
1448sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
robertphillips1b5f9682016-07-15 08:01:12 -07001449 sk_sp<GrTexture> texture(this->accessDrawContext()->asTexture());
1450 if (!texture) {
robertphillips04d62182016-07-15 12:21:33 -07001451 // When the device doesn't have a texture, we create a temporary texture.
1452 // TODO: we should actually only copy the portion of the source needed to apply the image
1453 // filter
1454 texture.reset(fContext->textureProvider()->createTexture(this->accessDrawContext()->desc(),
1455 SkBudgeted::kYes));
1456 if (!texture) {
1457 return nullptr;
1458 }
1459
reede51c3562016-07-19 14:33:20 -07001460 if (!fContext->copySurface(texture.get(), this->accessDrawContext()->accessRenderTarget())){
robertphillips04d62182016-07-15 12:21:33 -07001461 return nullptr;
1462 }
robertphillips1b5f9682016-07-15 08:01:12 -07001463 }
1464
1465 const SkImageInfo ii = this->imageInfo();
1466 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1467
1468 return SkSpecialImage::MakeFromGpu(srcRect,
1469 kNeedNewImageUniqueID_SpecialImage,
1470 std::move(texture),
1471 &this->surfaceProps());
1472}
1473
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001474void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001475 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001476 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001477 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001478 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
kkinnunen2e4414e2015-02-19 07:20:40 -08001479
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001480 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001481 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001482
robertphillips1b5f9682016-07-15 08:01:12 -07001483 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001484 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001485 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001486 return;
1487 }
1488
robertphillips1b5f9682016-07-15 08:01:12 -07001489 this->drawSpecial(draw, srcImg.get(), left, top, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001490}
1491
reeda85d4d02015-05-06 12:56:48 -07001492void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1493 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001494 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001495 SkMatrix viewMatrix = *draw.fMatrix;
1496 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001497 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001498 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001499 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001500 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1501 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001502 return;
reed85d91782015-09-10 14:33:38 -07001503 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001504 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001505 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1506 paint.getFilterQuality(), *draw.fMatrix)) {
1507 // only support tiling as bitmap at the moment, so force raster-version
1508 if (!as_IB(image)->getROPixels(&bm)) {
1509 return;
1510 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001511 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1512 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1513 CHECK_SHOULD_DRAW(draw);
1514 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001515 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1516 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001517 } else if (as_IB(image)->getROPixels(&bm)) {
1518 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001519 }
reeda85d4d02015-05-06 12:56:48 -07001520 }
1521}
1522
1523void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001524 const SkRect& dst, const SkPaint& paint,
1525 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001526 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001527 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001528 CHECK_SHOULD_DRAW(draw);
1529 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001530 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001531 return;
1532 }
1533 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001534 SkMatrix totalMatrix = *draw.fMatrix;
1535 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1536 dst.height() / (src ? src->height() : image->height()));
1537 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001538 // only support tiling as bitmap at the moment, so force raster-version
1539 if (!as_IB(image)->getROPixels(&bm)) {
1540 return;
1541 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001542 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1543 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1544 CHECK_SHOULD_DRAW(draw);
1545 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001546 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001547 } else if (as_IB(image)->getROPixels(&bm)) {
1548 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001549 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001550}
1551
bsalomonf1ecd212015-12-09 17:06:02 -08001552void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001553 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001554 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001555
joshualitt33a5fce2015-11-18 13:28:51 -08001556 CHECK_SHOULD_DRAW(draw);
1557
joshualittedb36442015-11-19 14:29:30 -08001558 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
robertphillips071457b2016-05-16 09:50:03 -07001559 fDrawContext->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001560 bool doBicubic;
1561 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001562 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1563 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001564 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001565 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001566
1567 SkRect srcR, dstR;
1568 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001569 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001570 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001571 }
1572 return;
1573 }
1574
bsalomon2bbd0c62015-12-09 12:50:56 -08001575 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
brianosman982eb7f2016-06-06 13:10:58 -07001576 bool gammaCorrect = this->surfaceProps().isGammaCorrect();
1577 SkSourceGammaTreatment gammaTreatment = gammaCorrect
1578 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIgnore;
bungeman06ca8ec2016-06-09 08:01:03 -07001579 sk_sp<GrFragmentProcessor> fp(
bsalomon2bbd0c62015-12-09 12:50:56 -08001580 producer->createFragmentProcessor(SkMatrix::I(),
1581 SkRect::MakeIWH(producer->width(), producer->height()),
1582 GrTextureProducer::kNo_FilterConstraint, true,
brianosman982eb7f2016-06-06 13:10:58 -07001583 &kMode, gammaTreatment));
joshualitt33a5fce2015-11-18 13:28:51 -08001584 GrPaint grPaint;
bungeman06ca8ec2016-06-09 08:01:03 -07001585 if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, std::move(fp),
brianosman982eb7f2016-06-06 13:10:58 -07001586 producer->isAlphaOnly(), gammaCorrect, &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001587 return;
1588 }
1589
bsalomon2bbd0c62015-12-09 12:50:56 -08001590 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1591 producer->height(), center, dst);
1592}
1593
1594void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1595 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001596 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001597 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001598 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001599 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001600 } else {
1601 SkBitmap bm;
1602 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1603 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001604 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001605 } else if (as_IB(image)->getROPixels(&bm)) {
1606 this->drawBitmapNine(draw, bm, center, dst, paint);
1607 }
1608 }
1609}
1610
1611void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1612 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001613 ASSERT_SINGLE_OWNER
bsalomon2bbd0c62015-12-09 12:50:56 -08001614 if (bitmap.getTexture()) {
1615 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001616 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001617 } else {
1618 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001619 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001620 }
joshualitt33a5fce2015-11-18 13:28:51 -08001621}
1622
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001623///////////////////////////////////////////////////////////////////////////////
1624
1625// must be in SkCanvas::VertexMode order
1626static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1627 kTriangles_GrPrimitiveType,
1628 kTriangleStrip_GrPrimitiveType,
1629 kTriangleFan_GrPrimitiveType,
1630};
1631
1632void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1633 int vertexCount, const SkPoint vertices[],
1634 const SkPoint texs[], const SkColor colors[],
1635 SkXfermode* xmode,
1636 const uint16_t indices[], int indexCount,
1637 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001638 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001639 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001640 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001641
halcanary96fcdcc2015-08-27 07:41:13 -07001642 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1643 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001644
halcanary96fcdcc2015-08-27 07:41:13 -07001645 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001646
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001647 SkPaint copy(paint);
1648 copy.setStyle(SkPaint::kStroke_Style);
1649 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001650
bsalomonf1b7a1d2015-09-28 06:26:28 -07001651 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001652 // we ignore the shader if texs is null.
brianosman898235c2016-04-06 07:38:23 -07001653 if (!SkPaintToGrPaintNoShader(this->context(), copy,
brianosmanb461d342016-04-13 13:10:14 -07001654 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001655 return;
1656 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001657
dandov32a311b2014-07-15 19:46:26 -07001658 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001659 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001660 switch (vmode) {
1661 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001662 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001663 break;
1664 case SkCanvas::kTriangleStrip_VertexMode:
1665 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001666 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001667 break;
1668 }
mtklein533eb782014-08-27 10:39:42 -07001669
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001670 VertState state(vertexCount, indices, indexCount);
1671 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001672
dandov32a311b2014-07-15 19:46:26 -07001673 //number of indices for lines per triangle with kLines
1674 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001675
bsalomonf1b7a1d2015-09-28 06:26:28 -07001676 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001677 int i = 0;
1678 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001679 lineIndices[i] = state.f0;
1680 lineIndices[i + 1] = state.f1;
1681 lineIndices[i + 2] = state.f1;
1682 lineIndices[i + 3] = state.f2;
1683 lineIndices[i + 4] = state.f2;
1684 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001685 i += 6;
1686 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001687 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001688 grPaint,
1689 *draw.fMatrix,
1690 kLines_GrPrimitiveType,
1691 vertexCount,
1692 vertices,
1693 texs,
1694 colors,
1695 lineIndices.get(),
1696 indexCount);
1697 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001698 }
1699
bsalomonf1b7a1d2015-09-28 06:26:28 -07001700 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001701
1702 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001703 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001704 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1705 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001706 convertedColors.reset(vertexCount);
1707 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001708 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001709 }
1710 colors = convertedColors.get();
1711 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001712 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001713 if (texs && paint.getShader()) {
1714 if (colors) {
1715 // When there are texs and colors the shader and colors are combined using xmode. A null
1716 // xmode is defined to mean modulate.
1717 SkXfermode::Mode colorMode;
1718 if (xmode) {
1719 if (!xmode->asMode(&colorMode)) {
1720 return;
1721 }
1722 } else {
1723 colorMode = SkXfermode::kModulate_Mode;
1724 }
1725 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
brianosmanb461d342016-04-13 13:10:14 -07001726 false, this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001727 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001728 return;
1729 }
1730 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001731 // We have a shader, but no colors to blend it against.
brianosman898235c2016-04-06 07:38:23 -07001732 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001733 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001734 return;
1735 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001736 }
bsalomonaa48d362015-10-01 08:34:17 -07001737 } else {
1738 if (colors) {
1739 // We have colors, but either have no shader or no texture coords (which implies that
1740 // we should ignore the shader).
brianosman898235c2016-04-06 07:38:23 -07001741 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001742 this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001743 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001744 return;
1745 }
1746 } else {
1747 // No colors and no shaders. Just draw with the paint color.
brianosman898235c2016-04-06 07:38:23 -07001748 if (!SkPaintToGrPaintNoShader(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001749 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001750 return;
1751 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001752 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001753 }
1754
robertphillips2e1e51f2015-10-15 08:01:48 -07001755 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001756 grPaint,
1757 *draw.fMatrix,
1758 primType,
1759 vertexCount,
1760 vertices,
1761 texs,
1762 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001763 indices,
robertphillipsea461502015-05-26 11:38:03 -07001764 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001765}
1766
1767///////////////////////////////////////////////////////////////////////////////
1768
jvanverth31ff7622015-08-07 10:09:28 -07001769void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001770 const SkRect texRect[], const SkColor colors[], int count,
1771 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001772 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001773 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001774 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001775 return;
1776 }
1777
jvanverth31ff7622015-08-07 10:09:28 -07001778 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001779 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001780
reedca109532015-06-25 16:25:25 -07001781 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001782 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001783
jvanverth31ff7622015-08-07 10:09:28 -07001784 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001785 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001786 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
brianosmanb461d342016-04-13 13:10:14 -07001787 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001788 return;
1789 }
1790 } else {
brianosman898235c2016-04-06 07:38:23 -07001791 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001792 this->surfaceProps().isGammaCorrect(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001793 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001794 }
1795 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001796
1797 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001798 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001799}
1800
1801///////////////////////////////////////////////////////////////////////////////
1802
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001803void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001804 size_t byteLength, SkScalar x, SkScalar y,
1805 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001806 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001807 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001808 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001809
jvanverth8c27a182014-10-14 08:45:50 -07001810 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001811 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001812 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001813 return;
1814 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001815
jvanverth8c27a182014-10-14 08:45:50 -07001816 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001817
robertphillips2e1e51f2015-10-15 08:01:48 -07001818 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001819 (const char *)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001820}
1821
fmalita05c4a432014-09-29 06:29:53 -07001822void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1823 const SkScalar pos[], int scalarsPerPos,
1824 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001825 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001826 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001827 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001828
jvanverth8c27a182014-10-14 08:45:50 -07001829 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001830 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001831 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001832 return;
1833 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001834
jvanverth8c27a182014-10-14 08:45:50 -07001835 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001836
robertphillips2e1e51f2015-10-15 08:01:48 -07001837 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001838 (const char *)text, byteLength, pos, scalarsPerPos, offset,
reed1e7f5e72016-04-27 07:49:17 -07001839 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001840}
1841
joshualitt9c328182015-03-23 08:13:04 -07001842void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1843 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001844 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001845 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001846 CHECK_SHOULD_DRAW(draw);
1847
1848 SkDEBUGCODE(this->validate();)
1849
robertphillips2e1e51f2015-10-15 08:01:48 -07001850 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001851 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001852}
1853
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001854///////////////////////////////////////////////////////////////////////////////
1855
reedb2db8982014-11-13 12:41:02 -08001856bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001857 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001858}
1859
1860void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001861 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001862
bsalomonc49e8682015-06-30 11:37:35 -07001863 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001864}
1865
1866///////////////////////////////////////////////////////////////////////////////
1867
reed76033be2015-03-14 10:54:31 -07001868SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001869 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001870
robertphillipsca6eafc2016-05-17 09:57:46 -07001871 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001872
robertphillipsca6eafc2016-05-17 09:57:46 -07001873 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001874 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001875 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1876 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001877
robertphillipsca6eafc2016-05-17 09:57:46 -07001878 sk_sp<GrDrawContext> dc(fContext->newDrawContext(fit,
1879 cinfo.fInfo.width(), cinfo.fInfo.height(),
1880 fDrawContext->config(),
1881 fDrawContext->desc().fSampleCnt,
1882 kDefault_GrSurfaceOrigin,
1883 &props));
1884 if (!dc) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001885 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001886 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001887 cinfo.fInfo.width(), cinfo.fInfo.height());
robertphillipsca6eafc2016-05-17 09:57:46 -07001888 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001889 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001890
1891 // Skia's convention is to only clear a device if it is non-opaque.
1892 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1893
1894 return SkGpuDevice::Make(std::move(dc),
1895 cinfo.fInfo.width(), cinfo.fInfo.height(),
1896 init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001897}
1898
reede8f30622016-03-23 18:59:25 -07001899sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001900 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001901 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001902 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
robertphillipse5768742016-05-13 11:20:46 -07001903 return SkSurface::MakeRenderTarget(fContext, kBudgeted, info, fDrawContext->desc().fSampleCnt,
reede8f30622016-03-23 18:59:25 -07001904 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001905}
1906
senorblanco900c3672016-04-27 11:31:23 -07001907SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001908 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001909 // We always return a transient cache, so it is freed after each
1910 // filter traversal.
senorblanco900c3672016-04-27 11:31:23 -07001911 return SkImageFilterCache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001912}
reedf037e0b2014-10-30 11:34:15 -07001913
1914#endif