blob: 9357568117bebfcc6ed5900a110e6ad0dc13f0f3 [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
robertphillips175dd9b2016-04-28 14:32:04 -0700344GrDrawContext* SkGpuDevice::accessDrawContext() {
345 ASSERT_SINGLE_OWNER
346 return fDrawContext.get();
347}
348
reed8eddfb52014-12-04 07:50:14 -0800349void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800350 ASSERT_SINGLE_OWNER
reed8eddfb52014-12-04 07:50:14 -0800351 GrColor color = 0;
joshualitt5651ee62016-01-11 10:39:11 -0800352 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext);
reed8eddfb52014-12-04 07:50:14 -0800353 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700354 fDrawContext->clear(&rect, color, true);
reed8eddfb52014-12-04 07:50:14 -0800355}
356
robertphillipsca6eafc2016-05-17 09:57:46 -0700357void SkGpuDevice::replaceDrawContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800358 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800359
bsalomon5ec26ae2016-02-25 08:33:02 -0800360 SkBudgeted budgeted = fRenderTarget->resourcePriv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800361
robertphillipsca6eafc2016-05-17 09:57:46 -0700362 sk_sp<GrDrawContext> newDC(CreateDrawContext(this->context(),
363 budgeted,
364 this->imageInfo(),
365 fDrawContext->numColorSamples(),
366 &this->surfaceProps()));
367 if (!newDC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800368 return;
369 }
370
371 if (shouldRetainContent) {
372 if (fRenderTarget->wasDestroyed()) {
373 return;
374 }
robertphillipsca6eafc2016-05-17 09:57:46 -0700375 newDC->copySurface(fDrawContext->asTexture().get(),
376 SkIRect::MakeWH(this->width(), this->height()),
377 SkIPoint::Make(0, 0));
kkinnunenabcfab42015-02-22 22:53:44 -0800378 }
379
robertphillipsca6eafc2016-05-17 09:57:46 -0700380 SkASSERT(fDrawContext->accessRenderTarget() != newDC->accessRenderTarget());
kkinnunenabcfab42015-02-22 22:53:44 -0800381
robertphillipsca6eafc2016-05-17 09:57:46 -0700382 fRenderTarget = newDC->renderTarget();
kkinnunenabcfab42015-02-22 22:53:44 -0800383
bsalomon74f681d2015-06-23 14:38:48 -0700384#ifdef SK_DEBUG
385 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
386 kPremul_SkAlphaType);
387 SkASSERT(info == fLegacyBitmap.info());
388#endif
robertphillipsca6eafc2016-05-17 09:57:46 -0700389 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget.get());
kkinnunenabcfab42015-02-22 22:53:44 -0800390 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700391
robertphillipsca6eafc2016-05-17 09:57:46 -0700392 fDrawContext = newDC;
kkinnunenabcfab42015-02-22 22:53:44 -0800393}
394
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000395///////////////////////////////////////////////////////////////////////////////
396
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000397void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800398 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800399 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800400 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000401
402 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700403 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700404 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700405 return;
406 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000407
robertphillips2e1e51f2015-10-15 08:01:48 -0700408 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000409}
410
411// must be in SkCanvas::PointMode order
412static const GrPrimitiveType gPointMode2PrimtiveType[] = {
413 kPoints_GrPrimitiveType,
414 kLines_GrPrimitiveType,
415 kLineStrip_GrPrimitiveType
416};
417
ethannicholas330bb952015-07-17 06:44:02 -0700418// suppress antialiasing on axis-aligned integer-coordinate lines
419static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
420 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
421 return false;
422 }
423 if (count == 2) {
424 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
425 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800426 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700427 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800428 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700429 // the two end pixels as being the lesser of two evils.
430 if (pts[0].fX == pts[1].fX) {
431 return ((int) pts[0].fX) != pts[0].fX;
432 }
433 if (pts[0].fY == pts[1].fY) {
434 return ((int) pts[0].fY) != pts[0].fY;
435 }
436 }
437 return true;
438}
439
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000440void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
441 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800442 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800443 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800444 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000445
446 SkScalar width = paint.getStrokeWidth();
447 if (width < 0) {
448 return;
449 }
450
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000451 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700452 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700453 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700454 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700455 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700456 return;
457 }
egdaniele61c4112014-06-12 10:24:21 -0700458 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700459 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700460 path.moveTo(pts[0]);
461 path.lineTo(pts[1]);
bsalomon6663acf2016-05-10 09:14:17 -0700462 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, style);
egdaniele61c4112014-06-12 10:24:21 -0700463 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000464 }
465
ethannicholas330bb952015-07-17 06:44:02 -0700466 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000467 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800468 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700469 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000470 draw.drawPoints(mode, count, pts, paint, true);
471 return;
472 }
473
474 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700475 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700476 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700477 return;
478 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000479
robertphillips2e1e51f2015-10-15 08:01:48 -0700480 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700481 grPaint,
482 *draw.fMatrix,
483 gPointMode2PrimtiveType[mode],
484 SkToS32(count),
485 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700486 nullptr,
487 nullptr,
488 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700489 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000490}
491
492///////////////////////////////////////////////////////////////////////////////
493
robertphillipsff55b492015-11-24 07:56:59 -0800494void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800495 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800496 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800497 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000498
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000499
bsalomona7d85ba2016-07-06 11:54:59 -0700500 // A couple reasons we might need to call drawPath.
501 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000502 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700503 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000504 path.addRect(rect);
robertphillips6c7e3252016-04-27 10:47:51 -0700505 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800506 fClip, path, paint,
507 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700508 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000509 return;
510 }
511
512 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700513 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700514 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700515 return;
516 }
Mike Klein744fb732014-06-23 15:13:26 -0400517
bsalomon6663acf2016-05-10 09:14:17 -0700518 GrStyle style(paint);
519 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000520}
521
522///////////////////////////////////////////////////////////////////////////////
523
524void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800525 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800526 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800527 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800528 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000529
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000530 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700531 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700532 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700533 return;
534 }
Mike Klein744fb732014-06-23 15:13:26 -0400535
bsalomon6663acf2016-05-10 09:14:17 -0700536 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000537 if (paint.getMaskFilter()) {
538 // try to hit the fast path for drawing filtered round rects
539
540 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800541 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000542 if (devRRect.allCornersCircular()) {
543 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700544 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
reed1e7f5e72016-04-27 07:49:17 -0700545 draw.fRC->getBounds(),
joshualitt5531d512014-12-17 15:50:11 -0800546 *draw.fMatrix,
547 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000548 SkIRect finalIRect;
549 maskRect.roundOut(&finalIRect);
reed1e7f5e72016-04-27 07:49:17 -0700550 if (draw.fRC->quickReject(finalIRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000551 // clipped out
552 return;
553 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700554 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
robertphillips6c7e3252016-04-27 10:47:51 -0700555 fDrawContext.get(),
joshualitt25d9c152015-02-18 12:29:52 -0800556 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800557 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800558 *draw.fMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700559 style.strokeRec(),
egdanield58a0ba2014-06-11 10:30:05 -0700560 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000561 return;
562 }
563 }
564
565 }
566 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000567 }
568
bsalomon6663acf2016-05-10 09:14:17 -0700569 if (paint.getMaskFilter() || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800570 // The only mask filter the native rrect drawing code could've handle was taken
571 // care of above.
572 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000573 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700574 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000575 path.addRRect(rect);
robertphillips6c7e3252016-04-27 10:47:51 -0700576 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800577 fClip, path, paint,
578 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700579 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000580 return;
581 }
Mike Klein744fb732014-06-23 15:13:26 -0400582
bsalomon6663acf2016-05-10 09:14:17 -0700583 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800584
bsalomon6663acf2016-05-10 09:14:17 -0700585 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000586}
587
robertphillipsd7706102016-02-25 09:28:08 -0800588
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000589void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800590 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800591 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800592 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800593 CHECK_SHOULD_DRAW(draw);
594
robertphillipsd7706102016-02-25 09:28:08 -0800595 if (outer.isEmpty()) {
596 return;
597 }
598
599 if (inner.isEmpty()) {
600 return this->drawRRect(draw, outer, paint);
601 }
602
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000603 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000604
robertphillips0e7029e2015-11-30 05:45:06 -0800605 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800606 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700607 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700608 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700609 return;
610 }
robertphillips00095892016-02-29 13:50:40 -0800611
612 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
613 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000614 }
615
616 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700617 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000618 path.addRRect(outer);
619 path.addRRect(inner);
620 path.setFillType(SkPath::kEvenOdd_FillType);
621
robertphillips6c7e3252016-04-27 10:47:51 -0700622 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillips0e7029e2015-11-30 05:45:06 -0800623 fClip, path, paint,
624 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700625 draw.fRC->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000626}
627
628
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000629/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000630
robertphillips514450c2015-11-24 05:36:02 -0800631void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800632 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800633 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800634 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000635
robertphillips514450c2015-11-24 05:36:02 -0800636 // Presumably the path effect warps this to something other than an oval
637 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000638 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700639 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000640 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700641 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000642 return;
herb11a7f7f2015-11-24 12:41:00 -0800643 }
644
robertphillips514450c2015-11-24 05:36:02 -0800645 if (paint.getMaskFilter()) {
646 // The RRect path can handle special case blurring
647 SkRRect rr = SkRRect::MakeOval(oval);
648 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000649 }
650
651 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700652 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700653 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700654 return;
655 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000656
bsalomon6663acf2016-05-10 09:14:17 -0700657 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000658}
659
660#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000661
662///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700663void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
664 const SkDraw& draw,
665 const SkPaint& origPaint) {
666 ASSERT_SINGLE_OWNER
667 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext);
668 CHECK_SHOULD_DRAW(draw);
669
670 // Adding support for round capping would require a GrDrawContext::fillRRectWithLocalMatrix
671 // entry point
672 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
673 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
674 SkASSERT(!origPaint.getPathEffect());
675 SkASSERT(!origPaint.getMaskFilter());
676
677 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
678 SkASSERT(halfWidth > 0);
679
680 SkVector v = points[1] - points[0];
681
682 SkScalar length = SkPoint::Normalize(&v);
683 if (!length) {
684 v.fX = 1.0f;
685 v.fY = 0.0f;
686 }
687
688 SkPaint newPaint(origPaint);
689 newPaint.setStyle(SkPaint::kFill_Style);
690
691 SkScalar xtraLength = 0.0f;
692 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
693 xtraLength = halfWidth;
694 }
695
696 SkPoint mid = points[0] + points[1];
697 mid.scale(0.5f);
698
699 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
700 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
701 SkMatrix m;
702 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
703
704 SkMatrix local = m;
705
706 m.postConcat(*draw.fMatrix);
707
708 GrPaint grPaint;
709 if (!SkPaintToGrPaint(this->context(), newPaint, m,
710 this->surfaceProps().isGammaCorrect(), &grPaint)) {
711 return;
712 }
713
714 fDrawContext->fillRectWithLocalMatrix(fClip, grPaint, m, rect, local);
715}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000716
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000717void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
718 const SkPaint& paint, const SkMatrix* prePathMatrix,
719 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800720 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800721 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700722 SkPoint points[2];
723 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
724 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
725 draw.fMatrix->preservesRightAngles() && origSrcPath.isLine(points)) {
726 // Path-based stroking looks better for thin rects
727 SkScalar strokeWidth = draw.fMatrix->getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700728 if (strokeWidth >= 1.0f) {
robertphillips0851d2d2016-06-02 05:21:34 -0700729 // Round capping support is currently disabled b.c. it would require
730 // a RRect batch that takes a localMatrix.
731 this->drawStrokedLine(points, draw, paint);
732 return;
733 }
734 }
robertphillipsff55b492015-11-24 07:56:59 -0800735 bool isClosed;
736 SkRect rect;
737 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
738 this->drawRect(draw, rect, paint);
739 return;
740 }
741 if (origSrcPath.isOval(&rect)) {
742 this->drawOval(draw, rect, paint);
743 return;
744 }
745 SkRRect rrect;
746 if (origSrcPath.isRRect(&rrect)) {
747 this->drawRRect(draw, rrect, paint);
748 return;
749 }
750 }
751
joshualitt5531d512014-12-17 15:50:11 -0800752 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800753 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000754
robertphillips6c7e3252016-04-27 10:47:51 -0700755 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsccb1b572015-05-27 11:02:55 -0700756 fClip, origSrcPath, paint,
757 *draw.fMatrix, prePathMatrix,
reed1e7f5e72016-04-27 07:49:17 -0700758 draw.fRC->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000759}
760
761static const int kBmpSmallTileSize = 1 << 10;
762
763static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
764 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
765 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
766 return tilesX * tilesY;
767}
768
reed85d91782015-09-10 14:33:38 -0700769static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000770 if (maxTileSize <= kBmpSmallTileSize) {
771 return maxTileSize;
772 }
773
774 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
775 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
776
777 maxTileTotalTileSize *= maxTileSize * maxTileSize;
778 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
779
780 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
781 return kBmpSmallTileSize;
782 } else {
783 return maxTileSize;
784 }
785}
786
787// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
788// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700789static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800790 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800791 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700792 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000793 const SkRect* srcRectPtr,
794 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700795 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000796 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800797 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000798 clippedSrcIRect->setEmpty();
799 return;
800 }
801 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
802 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700803 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000804 // we've setup src space 0,0 to map to the top left of the src rect.
805 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000806 if (!clippedSrcRect.intersect(*srcRectPtr)) {
807 clippedSrcIRect->setEmpty();
808 return;
809 }
810 }
811 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700812 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000813 if (!clippedSrcIRect->intersect(bmpBounds)) {
814 clippedSrcIRect->setEmpty();
815 }
816}
817
reed85d91782015-09-10 14:33:38 -0700818bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
819 const SkMatrix& viewMatrix,
820 const GrTextureParams& params,
821 const SkRect* srcRectPtr,
822 int maxTileSize,
823 int* tileSize,
824 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800825 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700826 // if it's larger than the max tile size, then we have no choice but tiling.
827 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
robertphillipse5768742016-05-13 11:20:46 -0700828 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(),
829 fClip, viewMatrix, imageRect.size(),
reed85d91782015-09-10 14:33:38 -0700830 srcRectPtr, clippedSubset);
831 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
832 return true;
833 }
834
bsalomon1a1d0b82015-10-16 07:49:42 -0700835 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700836 const size_t area = imageRect.width() * imageRect.height();
837 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
838 return false;
839 }
840
reed85d91782015-09-10 14:33:38 -0700841 // At this point we know we could do the draw by uploading the entire bitmap
842 // as a texture. However, if the texture would be large compared to the
843 // cache size and we don't require most of it for this draw then tile to
844 // reduce the amount of upload and cache spill.
845
846 // assumption here is that sw bitmap size is a good proxy for its size as
847 // a texture
848 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
849 size_t cacheSize;
850 fContext->getResourceCacheLimits(nullptr, &cacheSize);
851 if (bmpSize < cacheSize / 2) {
852 return false;
853 }
854
bsalomon1a1d0b82015-10-16 07:49:42 -0700855 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
856 // tiling memory savings would be < 50%.
robertphillipse5768742016-05-13 11:20:46 -0700857 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(),
858 fClip, viewMatrix, imageRect.size(), srcRectPtr,
reed85d91782015-09-10 14:33:38 -0700859 clippedSubset);
860 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
861 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
862 kBmpSmallTileSize * kBmpSmallTileSize;
863
864 return usedTileBytes < 2 * bmpSize;
865}
866
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000867bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800868 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000869 const GrTextureParams& params,
870 const SkRect* srcRectPtr,
871 int maxTileSize,
872 int* tileSize,
873 SkIRect* clippedSrcRect) const {
joshualittce894002016-01-11 13:29:31 -0800874 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000875 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700876 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000877 return false;
878 }
879
reed85d91782015-09-10 14:33:38 -0700880 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
881 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
882}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000883
reed85d91782015-09-10 14:33:38 -0700884bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
885 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
886 const SkMatrix& viewMatrix) const {
joshualittce894002016-01-11 13:29:31 -0800887 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700888 // if image is explictly texture backed then just use the texture
889 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000890 return false;
891 }
892
reed85d91782015-09-10 14:33:38 -0700893 GrTextureParams params;
894 bool doBicubic;
895 GrTextureParams::FilterMode textureFilterMode =
896 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
897
898 int tileFilterPad;
899 if (doBicubic) {
900 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
901 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
902 tileFilterPad = 0;
903 } else {
904 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000905 }
reed85d91782015-09-10 14:33:38 -0700906 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000907
bsalomon8c07b7a2015-11-02 11:36:52 -0800908 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000909
reed85d91782015-09-10 14:33:38 -0700910 // these are output, which we safely ignore, as we just want to know the predicate
911 int outTileSize;
912 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000913
reed85d91782015-09-10 14:33:38 -0700914 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
915 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000916}
917
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000918void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000919 const SkBitmap& bitmap,
920 const SkMatrix& m,
921 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800922 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800923 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800924 SkMatrix viewMatrix;
925 viewMatrix.setConcat(*origDraw.fMatrix, m);
926 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800927 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonb1b01992015-11-18 10:56:08 -0800928 // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
bsalomonf1ecd212015-12-09 17:06:02 -0800929 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
930 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800931 return;
932 }
bsalomonb1b01992015-11-18 10:56:08 -0800933 int maxTileSize = fContext->caps()->maxTileSize();
934
935 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
936 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
robertphillips071457b2016-05-16 09:50:03 -0700937 bool drawAA = !fDrawContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -0800938 paint.isAntiAlias() &&
939 bitmap.width() <= maxTileSize &&
940 bitmap.height() <= maxTileSize;
941
942 bool skipTileCheck = drawAA || paint.getMaskFilter();
943
944 if (!skipTileCheck) {
945 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
946 int tileSize;
947 SkIRect clippedSrcRect;
948
949 GrTextureParams params;
950 bool doBicubic;
951 GrTextureParams::FilterMode textureFilterMode =
952 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
953 &doBicubic);
954
955 int tileFilterPad;
956
957 if (doBicubic) {
958 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
959 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
960 tileFilterPad = 0;
961 } else {
962 tileFilterPad = 1;
963 }
964 params.setFilterMode(textureFilterMode);
965
966 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
967 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
968 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
969 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
970 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
971 return;
972 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000973 }
bsalomonb1b01992015-11-18 10:56:08 -0800974 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800975 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
976 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000977}
978
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000979// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000980// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
981// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000982static inline void clamped_outset_with_offset(SkIRect* iRect,
983 int outset,
984 SkPoint* offset,
985 const SkIRect& clamp) {
986 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000987
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000988 int leftClampDelta = clamp.fLeft - iRect->fLeft;
989 if (leftClampDelta > 0) {
990 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000991 iRect->fLeft = clamp.fLeft;
992 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000993 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000994 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000995
996 int topClampDelta = clamp.fTop - iRect->fTop;
997 if (topClampDelta > 0) {
998 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000999 iRect->fTop = clamp.fTop;
1000 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001001 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001002 }
1003
1004 if (iRect->fRight > clamp.fRight) {
1005 iRect->fRight = clamp.fRight;
1006 }
1007 if (iRect->fBottom > clamp.fBottom) {
1008 iRect->fBottom = clamp.fBottom;
1009 }
1010}
1011
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001012// Break 'bitmap' into several tiles to draw it since it has already
1013// been determined to be too large to fit in VRAM
1014void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001015 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001016 const SkRect& srcRect,
1017 const SkIRect& clippedSrcIRect,
1018 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -08001019 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -07001020 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001021 int tileSize,
1022 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -08001023 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -08001024
ericrk983294f2016-04-18 09:14:00 -07001025 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -08001026 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -07001027 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -08001028
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001029 // The following pixel lock is technically redundant, but it is desirable
1030 // to lock outside of the tile loop to prevent redecoding the whole image
1031 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1032 // is larger than the limit of the discardable memory pool.
1033 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -08001034
1035 const SkPaint* paint = &origPaint;
1036 SkPaint tempPaint;
robertphillips071457b2016-05-16 09:50:03 -07001037 if (origPaint.isAntiAlias() && !fDrawContext->isUnifiedMultisampled()) {
bsalomonc55271f2015-11-09 11:55:57 -08001038 // Drop antialiasing to avoid seams at tile boundaries.
1039 tempPaint = origPaint;
1040 tempPaint.setAntiAlias(false);
1041 paint = &tempPaint;
1042 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001043 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1044
1045 int nx = bitmap.width() / tileSize;
1046 int ny = bitmap.height() / tileSize;
1047 for (int x = 0; x <= nx; x++) {
1048 for (int y = 0; y <= ny; y++) {
1049 SkRect tileR;
1050 tileR.set(SkIntToScalar(x * tileSize),
1051 SkIntToScalar(y * tileSize),
1052 SkIntToScalar((x + 1) * tileSize),
1053 SkIntToScalar((y + 1) * tileSize));
1054
1055 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1056 continue;
1057 }
1058
1059 if (!tileR.intersect(srcRect)) {
1060 continue;
1061 }
1062
1063 SkBitmap tmpB;
1064 SkIRect iTileR;
1065 tileR.roundOut(&iTileR);
1066 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1067 SkIntToScalar(iTileR.fTop));
1068
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001069 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001070 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001071 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001072 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001073 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001074
robertphillipsec8bb942014-11-21 10:16:25 -08001075 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001076 SkIRect iClampRect;
1077
reeda5517e22015-07-14 10:54:12 -07001078 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001079 // In bleed mode we want to always expand the tile on all edges
1080 // but stay within the bitmap bounds
1081 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1082 } else {
1083 // In texture-domain/clamp mode we only want to expand the
1084 // tile on edges interior to "srcRect" (i.e., we want to
1085 // not bleed across the original clamped edges)
1086 srcRect.roundOut(&iClampRect);
1087 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001088 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1089 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001090 }
1091
1092 if (bitmap.extractSubset(&tmpB, iTileR)) {
1093 // now offset it to make it "local" to our tmp bitmap
1094 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001095 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001096 // de-optimized this determination
1097 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001098 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001099 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001100 tileR,
1101 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -08001102 *paint,
reeda5517e22015-07-14 10:54:12 -07001103 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001104 bicubic,
1105 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001106 }
1107 }
1108 }
1109}
1110
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001111/*
1112 * This is called by drawBitmap(), which has to handle images that may be too
1113 * large to be represented by a single texture.
1114 *
1115 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1116 * and that non-texture portion of the GrPaint has already been setup.
1117 */
1118void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001119 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001120 const SkRect& srcRect,
1121 const GrTextureParams& params,
1122 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001123 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001124 bool bicubic,
1125 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001126 // We should have already handled bitmaps larger than the max texture size.
1127 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1128 bitmap.height() <= fContext->caps()->maxTextureSize());
1129 // Unless the bitmap is inherently texture-backed, we should be respecting the max tile size
1130 // by the time we get here.
1131 SkASSERT(bitmap.getTexture() ||
1132 (bitmap.width() <= fContext->caps()->maxTileSize() &&
1133 bitmap.height() <= fContext->caps()->maxTileSize()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001134
1135 GrTexture* texture;
brianosman982eb7f2016-06-06 13:10:58 -07001136 SkSourceGammaTreatment gammaTreatment = this->surfaceProps().isGammaCorrect()
1137 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIgnore;
1138 AutoBitmapTexture abt(fContext, bitmap, params, gammaTreatment, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001139 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001140 return;
1141 }
1142
brianosman54f30c12016-07-18 10:53:52 -07001143 SkColorSpace* dstColorSpace = nullptr; // XFORMTODO
1144 sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(bitmap.colorSpace(),
1145 dstColorSpace);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001146 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001147 SkRect paintRect;
1148 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1149 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1150 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1151 SkScalarMul(srcRect.fTop, hInv),
1152 SkScalarMul(srcRect.fRight, wInv),
1153 SkScalarMul(srcRect.fBottom, hInv));
1154
egdaniel79da63f2015-10-09 10:55:16 -07001155 SkMatrix texMatrix;
1156 texMatrix.reset();
1157 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1158 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1159 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1160 // Thus we need to pass in the transform matrix directly to the texture processor used for
1161 // the bitmap draw.
1162 texMatrix.setScale(wInv, hInv);
1163 }
1164
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001165 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001166
1167 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1168 // the rest from the SkPaint.
bungeman06ca8ec2016-06-09 08:01:03 -07001169 sk_sp<GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001170
reeda5517e22015-07-14 10:54:12 -07001171 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001172 // Use a constrained texture domain to avoid color bleeding
1173 SkScalar left, top, right, bottom;
1174 if (srcRect.width() > SK_Scalar1) {
1175 SkScalar border = SK_ScalarHalf / texture->width();
1176 left = paintRect.left() + border;
1177 right = paintRect.right() - border;
1178 } else {
1179 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1180 }
1181 if (srcRect.height() > SK_Scalar1) {
1182 SkScalar border = SK_ScalarHalf / texture->height();
1183 top = paintRect.top() + border;
1184 bottom = paintRect.bottom() - border;
1185 } else {
1186 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1187 }
1188 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001189 if (bicubic) {
brianosman54f30c12016-07-18 10:53:52 -07001190 fp = GrBicubicEffect::Make(texture, std::move(colorSpaceXform), texMatrix,
1191 textureDomain);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001192 } else {
brianosman54f30c12016-07-18 10:53:52 -07001193 fp = GrTextureDomainEffect::Make(texture, std::move(colorSpaceXform), texMatrix,
1194 textureDomain, GrTextureDomain::kClamp_Mode,
1195 params.filterMode());
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001196 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001197 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001198 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1199 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
brianosman54f30c12016-07-18 10:53:52 -07001200 fp = GrBicubicEffect::Make(texture, std::move(colorSpaceXform), texMatrix, tileModes);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001201 } else {
brianosman54f30c12016-07-18 10:53:52 -07001202 fp = GrSimpleTextureEffect::Make(texture, std::move(colorSpaceXform), texMatrix, params);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001203 }
1204
joshualitt33a5fce2015-11-18 13:28:51 -08001205 GrPaint grPaint;
bungeman06ca8ec2016-06-09 08:01:03 -07001206 if (!SkPaintToGrPaintWithTexture(this->context(), paint, viewMatrix, std::move(fp),
brianosman898235c2016-04-06 07:38:23 -07001207 kAlpha_8_SkColorType == bitmap.colorType(),
brianosmanb461d342016-04-13 13:10:14 -07001208 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001209 return;
1210 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001211
egdaniel79da63f2015-10-09 10:55:16 -07001212 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1213 // We don't have local coords in this case and have previously set the transform
1214 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001215 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001216 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001217 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001218 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001219}
1220
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001221void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1222 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001223 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001224 CHECK_SHOULD_DRAW(draw);
robertphillips970587b2016-07-14 14:12:55 -07001225 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSprite", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001226
robertphillips970587b2016-07-14 14:12:55 -07001227 if (fContext->abandoned()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001228 return;
1229 }
1230
robertphillips970587b2016-07-14 14:12:55 -07001231 sk_sp<GrTexture> texture = sk_ref_sp(bitmap.getTexture());
joshualitt5f5a8d72015-02-25 14:09:45 -08001232 if (!texture) {
robertphillips970587b2016-07-14 14:12:55 -07001233 SkAutoLockPixels alp(bitmap, true);
1234 if (!bitmap.readyToDraw()) {
1235 return;
1236 }
1237
1238 // draw sprite neither filters nor tiles.
1239 texture.reset(GrRefCachedBitmapTexture(fContext, bitmap,
1240 GrTextureParams::ClampNoFilter(),
1241 SkSourceGammaTreatment::kRespect));
1242 if (!texture) {
1243 return;
1244 }
joshualitt5f5a8d72015-02-25 14:09:45 -08001245 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001246
robertphillips970587b2016-07-14 14:12:55 -07001247 SkIRect srcRect = SkIRect::MakeXYWH(bitmap.pixelRefOrigin().fX,
1248 bitmap.pixelRefOrigin().fY,
1249 bitmap.width(),
1250 bitmap.height());
bsalomonf1b7a1d2015-09-28 06:26:28 -07001251
robertphillips970587b2016-07-14 14:12:55 -07001252 sk_sp<SkSpecialImage> srcImg(SkSpecialImage::MakeFromGpu(srcRect,
1253 bitmap.getGenerationID(),
1254 std::move(texture),
robertphillips1b5f9682016-07-15 08:01:12 -07001255 &this->surfaceProps()));
robertphillips970587b2016-07-14 14:12:55 -07001256
1257 this->drawSpecial(draw, srcImg.get(), left, top, paint);
1258}
1259
1260
1261void SkGpuDevice::drawSpecial(const SkDraw& draw,
1262 SkSpecialImage* special1,
1263 int left, int top,
1264 const SkPaint& paint) {
robertphillips1b5f9682016-07-15 08:01:12 -07001265 ASSERT_SINGLE_OWNER
1266 CHECK_SHOULD_DRAW(draw);
1267 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpecial", fContext);
robertphillips970587b2016-07-14 14:12:55 -07001268
1269 SkIPoint offset = { 0, 0 };
1270
1271 sk_sp<SkSpecialImage> result;
1272 if (paint.getImageFilter()) {
1273 result = this->filterTexture(draw, special1, left, top,
1274 &offset,
1275 paint.getImageFilter());
1276 if (!result) {
1277 return;
1278 }
1279 } else {
1280 result = sk_ref_sp(special1);
1281 }
1282
1283 SkASSERT(result->isTextureBacked());
1284 sk_sp<GrTexture> texture = result->asTextureRef(fContext);
1285
1286 SkPaint tmpUnfiltered(paint);
1287 tmpUnfiltered.setImageFilter(nullptr);
1288
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001289 GrPaint grPaint;
brianosman54f30c12016-07-18 10:53:52 -07001290 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture.get(), nullptr,
1291 SkMatrix::I()));
robertphillips1b5f9682016-07-15 08:01:12 -07001292 if (GrPixelConfigIsAlphaOnly(texture->config())) {
bungeman06ca8ec2016-06-09 08:01:03 -07001293 fp = GrFragmentProcessor::MulOutputByInputUnpremulColor(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001294 } else {
bungeman06ca8ec2016-06-09 08:01:03 -07001295 fp = GrFragmentProcessor::MulOutputByInputAlpha(std::move(fp));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001296 }
robertphillips970587b2016-07-14 14:12:55 -07001297 if (!SkPaintToGrPaintReplaceShader(this->context(), tmpUnfiltered, std::move(fp),
brianosmanb461d342016-04-13 13:10:14 -07001298 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001299 return;
1300 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001301
robertphillips970587b2016-07-14 14:12:55 -07001302 const SkIRect& subset = result->subset();
1303
bsalomona2e69fc2015-11-05 10:41:43 -08001304 fDrawContext->fillRectToRect(fClip,
1305 grPaint,
1306 SkMatrix::I(),
robertphillips970587b2016-07-14 14:12:55 -07001307 SkRect::Make(SkIRect::MakeXYWH(left + offset.fX, top + offset.fY,
1308 subset.width(), subset.height())),
1309 SkRect::MakeXYWH(SkIntToScalar(subset.fLeft) / texture->width(),
1310 SkIntToScalar(subset.fTop) / texture->height(),
1311 SkIntToScalar(subset.width()) / texture->width(),
1312 SkIntToScalar(subset.height()) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001313}
1314
bsalomonb1b01992015-11-18 10:56:08 -08001315void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1316 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001317 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001318 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001319 CHECK_SHOULD_DRAW(draw);
bsalomonb1b01992015-11-18 10:56:08 -08001320 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001321 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001322 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
1323 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001324 return;
1325 }
bsalomonb1b01992015-11-18 10:56:08 -08001326 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1327 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1328 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1329 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1330 const SkRect* dst = &origDst;
1331 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001332 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001333 if (!src) {
1334 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001335 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001336
bsalomonb1b01992015-11-18 10:56:08 -08001337 SkMatrix srcToDstMatrix;
1338 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1339 return;
1340 }
1341 SkRect tmpSrc, tmpDst;
1342 if (src != &bmpBounds) {
1343 if (!bmpBounds.contains(*src)) {
1344 tmpSrc = *src;
1345 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001346 return; // nothing to draw
1347 }
bsalomonb1b01992015-11-18 10:56:08 -08001348 src = &tmpSrc;
1349 srcToDstMatrix.mapRect(&tmpDst, *src);
1350 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001351 }
1352 }
1353
bsalomonb1b01992015-11-18 10:56:08 -08001354 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001355
bsalomonb1b01992015-11-18 10:56:08 -08001356 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1357 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
robertphillips071457b2016-05-16 09:50:03 -07001358 bool drawAA = !fDrawContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -08001359 paint.isAntiAlias() &&
1360 bitmap.width() <= maxTileSize &&
1361 bitmap.height() <= maxTileSize;
1362
1363 bool skipTileCheck = drawAA || paint.getMaskFilter();
1364
1365 if (!skipTileCheck) {
1366 int tileSize;
1367 SkIRect clippedSrcRect;
1368
1369 GrTextureParams params;
1370 bool doBicubic;
1371 GrTextureParams::FilterMode textureFilterMode =
1372 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1373 &doBicubic);
1374
1375 int tileFilterPad;
1376
1377 if (doBicubic) {
1378 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1379 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1380 tileFilterPad = 0;
1381 } else {
1382 tileFilterPad = 1;
1383 }
1384 params.setFilterMode(textureFilterMode);
1385
1386 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1387 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1388 // we have a mask filter.
1389 SkMatrix viewMatrix = *draw.fMatrix;
1390 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1391 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1392 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1393 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1394 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1395 constraint, tileSize, doBicubic);
1396 return;
1397 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001398 }
bsalomonb1b01992015-11-18 10:56:08 -08001399 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001400 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001401}
1402
robertphillips6451a0c2016-07-18 08:31:31 -07001403sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkBitmap& bitmap) {
1404 SkASSERT(!bitmap.getTexture());
1405
1406 SkAutoLockPixels alp(bitmap, true);
1407 if (!bitmap.readyToDraw()) {
1408 return nullptr;
1409 }
1410
1411 GrTexture* texture;
1412 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(),
1413 SkSourceGammaTreatment::kRespect, &texture);
1414 if (!texture) {
1415 return nullptr;
1416 }
1417
1418 return SkSpecialImage::MakeFromGpu(bitmap.bounds(),
1419 bitmap.getGenerationID(),
1420 sk_ref_sp(texture),
1421 &this->surfaceProps());
1422}
1423
reede51c3562016-07-19 14:33:20 -07001424sk_sp<SkSpecialImage> SkGpuDevice::makeSpecial(const SkImage* image) {
robertphillips6451a0c2016-07-18 08:31:31 -07001425 SkPixmap pm;
1426 if (image->isTextureBacked()) {
1427 GrTexture* texture = as_IB(image)->peekTexture();
1428
1429 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(image->width(), image->height()),
1430 image->uniqueID(),
1431 sk_ref_sp(texture),
1432 &this->surfaceProps());
1433 } else if (image->peekPixels(&pm)) {
1434 SkBitmap bm;
1435
1436 bm.installPixels(pm);
1437 return this->makeSpecial(bm);
1438 } else {
1439 return nullptr;
1440 }
1441}
1442
1443sk_sp<SkSpecialImage> SkGpuDevice::snapSpecial() {
robertphillips1b5f9682016-07-15 08:01:12 -07001444 sk_sp<GrTexture> texture(this->accessDrawContext()->asTexture());
1445 if (!texture) {
robertphillips04d62182016-07-15 12:21:33 -07001446 // When the device doesn't have a texture, we create a temporary texture.
1447 // TODO: we should actually only copy the portion of the source needed to apply the image
1448 // filter
1449 texture.reset(fContext->textureProvider()->createTexture(this->accessDrawContext()->desc(),
1450 SkBudgeted::kYes));
1451 if (!texture) {
1452 return nullptr;
1453 }
1454
reede51c3562016-07-19 14:33:20 -07001455 if (!fContext->copySurface(texture.get(), this->accessDrawContext()->accessRenderTarget())){
robertphillips04d62182016-07-15 12:21:33 -07001456 return nullptr;
1457 }
robertphillips1b5f9682016-07-15 08:01:12 -07001458 }
1459
1460 const SkImageInfo ii = this->imageInfo();
1461 const SkIRect srcRect = SkIRect::MakeWH(ii.width(), ii.height());
1462
1463 return SkSpecialImage::MakeFromGpu(srcRect,
1464 kNeedNewImageUniqueID_SpecialImage,
1465 std::move(texture),
1466 &this->surfaceProps());
1467}
1468
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001469void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
robertphillips1b5f9682016-07-15 08:01:12 -07001470 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001471 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001472 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001473 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
kkinnunen2e4414e2015-02-19 07:20:40 -08001474
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001475 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001476 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001477
robertphillips1b5f9682016-07-15 08:01:12 -07001478 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
robertphillips6451a0c2016-07-18 08:31:31 -07001479 sk_sp<SkSpecialImage> srcImg(dev->snapSpecial());
robertphillips1b5f9682016-07-15 08:01:12 -07001480 if (!srcImg) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001481 return;
1482 }
1483
robertphillips1b5f9682016-07-15 08:01:12 -07001484 this->drawSpecial(draw, srcImg.get(), left, top, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001485}
1486
reeda85d4d02015-05-06 12:56:48 -07001487void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1488 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001489 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001490 SkMatrix viewMatrix = *draw.fMatrix;
1491 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001492 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001493 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001494 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001495 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1496 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001497 return;
reed85d91782015-09-10 14:33:38 -07001498 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001499 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001500 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1501 paint.getFilterQuality(), *draw.fMatrix)) {
1502 // only support tiling as bitmap at the moment, so force raster-version
1503 if (!as_IB(image)->getROPixels(&bm)) {
1504 return;
1505 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001506 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1507 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1508 CHECK_SHOULD_DRAW(draw);
1509 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001510 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1511 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001512 } else if (as_IB(image)->getROPixels(&bm)) {
1513 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001514 }
reeda85d4d02015-05-06 12:56:48 -07001515 }
1516}
1517
1518void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001519 const SkRect& dst, const SkPaint& paint,
1520 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001521 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001522 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001523 CHECK_SHOULD_DRAW(draw);
1524 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001525 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001526 return;
1527 }
1528 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001529 SkMatrix totalMatrix = *draw.fMatrix;
1530 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1531 dst.height() / (src ? src->height() : image->height()));
1532 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001533 // only support tiling as bitmap at the moment, so force raster-version
1534 if (!as_IB(image)->getROPixels(&bm)) {
1535 return;
1536 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001537 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1538 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1539 CHECK_SHOULD_DRAW(draw);
1540 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001541 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001542 } else if (as_IB(image)->getROPixels(&bm)) {
1543 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001544 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001545}
1546
bsalomonf1ecd212015-12-09 17:06:02 -08001547void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001548 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001549 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001550
joshualitt33a5fce2015-11-18 13:28:51 -08001551 CHECK_SHOULD_DRAW(draw);
1552
joshualittedb36442015-11-19 14:29:30 -08001553 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
robertphillips071457b2016-05-16 09:50:03 -07001554 fDrawContext->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001555 bool doBicubic;
1556 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001557 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1558 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001559 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001560 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001561
1562 SkRect srcR, dstR;
1563 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001564 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001565 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001566 }
1567 return;
1568 }
1569
bsalomon2bbd0c62015-12-09 12:50:56 -08001570 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
brianosman982eb7f2016-06-06 13:10:58 -07001571 bool gammaCorrect = this->surfaceProps().isGammaCorrect();
1572 SkSourceGammaTreatment gammaTreatment = gammaCorrect
1573 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIgnore;
bungeman06ca8ec2016-06-09 08:01:03 -07001574 sk_sp<GrFragmentProcessor> fp(
bsalomon2bbd0c62015-12-09 12:50:56 -08001575 producer->createFragmentProcessor(SkMatrix::I(),
1576 SkRect::MakeIWH(producer->width(), producer->height()),
1577 GrTextureProducer::kNo_FilterConstraint, true,
brianosman982eb7f2016-06-06 13:10:58 -07001578 &kMode, gammaTreatment));
joshualitt33a5fce2015-11-18 13:28:51 -08001579 GrPaint grPaint;
bungeman06ca8ec2016-06-09 08:01:03 -07001580 if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, std::move(fp),
brianosman982eb7f2016-06-06 13:10:58 -07001581 producer->isAlphaOnly(), gammaCorrect, &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001582 return;
1583 }
1584
bsalomon2bbd0c62015-12-09 12:50:56 -08001585 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1586 producer->height(), center, dst);
1587}
1588
1589void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1590 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001591 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001592 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001593 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001594 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001595 } else {
1596 SkBitmap bm;
1597 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1598 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001599 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001600 } else if (as_IB(image)->getROPixels(&bm)) {
1601 this->drawBitmapNine(draw, bm, center, dst, paint);
1602 }
1603 }
1604}
1605
1606void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1607 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001608 ASSERT_SINGLE_OWNER
bsalomon2bbd0c62015-12-09 12:50:56 -08001609 if (bitmap.getTexture()) {
1610 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001611 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001612 } else {
1613 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001614 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001615 }
joshualitt33a5fce2015-11-18 13:28:51 -08001616}
1617
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001618///////////////////////////////////////////////////////////////////////////////
1619
1620// must be in SkCanvas::VertexMode order
1621static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1622 kTriangles_GrPrimitiveType,
1623 kTriangleStrip_GrPrimitiveType,
1624 kTriangleFan_GrPrimitiveType,
1625};
1626
1627void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1628 int vertexCount, const SkPoint vertices[],
1629 const SkPoint texs[], const SkColor colors[],
1630 SkXfermode* xmode,
1631 const uint16_t indices[], int indexCount,
1632 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001633 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001634 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001635 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001636
halcanary96fcdcc2015-08-27 07:41:13 -07001637 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1638 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001639
halcanary96fcdcc2015-08-27 07:41:13 -07001640 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001641
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001642 SkPaint copy(paint);
1643 copy.setStyle(SkPaint::kStroke_Style);
1644 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001645
bsalomonf1b7a1d2015-09-28 06:26:28 -07001646 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001647 // we ignore the shader if texs is null.
brianosman898235c2016-04-06 07:38:23 -07001648 if (!SkPaintToGrPaintNoShader(this->context(), copy,
brianosmanb461d342016-04-13 13:10:14 -07001649 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001650 return;
1651 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001652
dandov32a311b2014-07-15 19:46:26 -07001653 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001654 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001655 switch (vmode) {
1656 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001657 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001658 break;
1659 case SkCanvas::kTriangleStrip_VertexMode:
1660 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001661 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001662 break;
1663 }
mtklein533eb782014-08-27 10:39:42 -07001664
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001665 VertState state(vertexCount, indices, indexCount);
1666 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001667
dandov32a311b2014-07-15 19:46:26 -07001668 //number of indices for lines per triangle with kLines
1669 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001670
bsalomonf1b7a1d2015-09-28 06:26:28 -07001671 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001672 int i = 0;
1673 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001674 lineIndices[i] = state.f0;
1675 lineIndices[i + 1] = state.f1;
1676 lineIndices[i + 2] = state.f1;
1677 lineIndices[i + 3] = state.f2;
1678 lineIndices[i + 4] = state.f2;
1679 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001680 i += 6;
1681 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001682 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001683 grPaint,
1684 *draw.fMatrix,
1685 kLines_GrPrimitiveType,
1686 vertexCount,
1687 vertices,
1688 texs,
1689 colors,
1690 lineIndices.get(),
1691 indexCount);
1692 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001693 }
1694
bsalomonf1b7a1d2015-09-28 06:26:28 -07001695 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001696
1697 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001698 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001699 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1700 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001701 convertedColors.reset(vertexCount);
1702 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001703 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001704 }
1705 colors = convertedColors.get();
1706 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001707 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001708 if (texs && paint.getShader()) {
1709 if (colors) {
1710 // When there are texs and colors the shader and colors are combined using xmode. A null
1711 // xmode is defined to mean modulate.
1712 SkXfermode::Mode colorMode;
1713 if (xmode) {
1714 if (!xmode->asMode(&colorMode)) {
1715 return;
1716 }
1717 } else {
1718 colorMode = SkXfermode::kModulate_Mode;
1719 }
1720 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
brianosmanb461d342016-04-13 13:10:14 -07001721 false, this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001722 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001723 return;
1724 }
1725 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001726 // We have a shader, but no colors to blend it against.
brianosman898235c2016-04-06 07:38:23 -07001727 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001728 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001729 return;
1730 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001731 }
bsalomonaa48d362015-10-01 08:34:17 -07001732 } else {
1733 if (colors) {
1734 // We have colors, but either have no shader or no texture coords (which implies that
1735 // we should ignore the shader).
brianosman898235c2016-04-06 07:38:23 -07001736 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001737 this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001738 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001739 return;
1740 }
1741 } else {
1742 // No colors and no shaders. Just draw with the paint color.
brianosman898235c2016-04-06 07:38:23 -07001743 if (!SkPaintToGrPaintNoShader(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001744 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001745 return;
1746 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001747 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001748 }
1749
robertphillips2e1e51f2015-10-15 08:01:48 -07001750 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001751 grPaint,
1752 *draw.fMatrix,
1753 primType,
1754 vertexCount,
1755 vertices,
1756 texs,
1757 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001758 indices,
robertphillipsea461502015-05-26 11:38:03 -07001759 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001760}
1761
1762///////////////////////////////////////////////////////////////////////////////
1763
jvanverth31ff7622015-08-07 10:09:28 -07001764void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001765 const SkRect texRect[], const SkColor colors[], int count,
1766 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001767 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001768 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001769 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001770 return;
1771 }
1772
jvanverth31ff7622015-08-07 10:09:28 -07001773 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001774 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001775
reedca109532015-06-25 16:25:25 -07001776 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001777 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001778
jvanverth31ff7622015-08-07 10:09:28 -07001779 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001780 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001781 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
brianosmanb461d342016-04-13 13:10:14 -07001782 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001783 return;
1784 }
1785 } else {
brianosman898235c2016-04-06 07:38:23 -07001786 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001787 this->surfaceProps().isGammaCorrect(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001788 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001789 }
1790 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001791
1792 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001793 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001794}
1795
1796///////////////////////////////////////////////////////////////////////////////
1797
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001798void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001799 size_t byteLength, SkScalar x, SkScalar y,
1800 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001801 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001802 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001803 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001804
jvanverth8c27a182014-10-14 08:45:50 -07001805 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001806 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001807 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001808 return;
1809 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001810
jvanverth8c27a182014-10-14 08:45:50 -07001811 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001812
robertphillips2e1e51f2015-10-15 08:01:48 -07001813 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001814 (const char *)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001815}
1816
fmalita05c4a432014-09-29 06:29:53 -07001817void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1818 const SkScalar pos[], int scalarsPerPos,
1819 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001820 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001821 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001822 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001823
jvanverth8c27a182014-10-14 08:45:50 -07001824 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001825 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001826 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001827 return;
1828 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001829
jvanverth8c27a182014-10-14 08:45:50 -07001830 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001831
robertphillips2e1e51f2015-10-15 08:01:48 -07001832 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001833 (const char *)text, byteLength, pos, scalarsPerPos, offset,
reed1e7f5e72016-04-27 07:49:17 -07001834 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001835}
1836
joshualitt9c328182015-03-23 08:13:04 -07001837void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1838 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001839 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001840 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001841 CHECK_SHOULD_DRAW(draw);
1842
1843 SkDEBUGCODE(this->validate();)
1844
robertphillips2e1e51f2015-10-15 08:01:48 -07001845 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001846 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001847}
1848
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001849///////////////////////////////////////////////////////////////////////////////
1850
reedb2db8982014-11-13 12:41:02 -08001851bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001852 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001853}
1854
1855void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001856 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001857
bsalomonc49e8682015-06-30 11:37:35 -07001858 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001859}
1860
1861///////////////////////////////////////////////////////////////////////////////
1862
reed76033be2015-03-14 10:54:31 -07001863SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001864 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001865
robertphillipsca6eafc2016-05-17 09:57:46 -07001866 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001867
robertphillipsca6eafc2016-05-17 09:57:46 -07001868 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001869 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001870 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1871 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001872
robertphillipsca6eafc2016-05-17 09:57:46 -07001873 sk_sp<GrDrawContext> dc(fContext->newDrawContext(fit,
1874 cinfo.fInfo.width(), cinfo.fInfo.height(),
1875 fDrawContext->config(),
1876 fDrawContext->desc().fSampleCnt,
1877 kDefault_GrSurfaceOrigin,
1878 &props));
1879 if (!dc) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001880 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001881 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001882 cinfo.fInfo.width(), cinfo.fInfo.height());
robertphillipsca6eafc2016-05-17 09:57:46 -07001883 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001884 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001885
1886 // Skia's convention is to only clear a device if it is non-opaque.
1887 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1888
1889 return SkGpuDevice::Make(std::move(dc),
1890 cinfo.fInfo.width(), cinfo.fInfo.height(),
1891 init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001892}
1893
reede8f30622016-03-23 18:59:25 -07001894sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001895 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001896 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001897 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
robertphillipse5768742016-05-13 11:20:46 -07001898 return SkSurface::MakeRenderTarget(fContext, kBudgeted, info, fDrawContext->desc().fSampleCnt,
reede8f30622016-03-23 18:59:25 -07001899 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001900}
1901
senorblanco900c3672016-04-27 11:31:23 -07001902SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001903 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001904 // We always return a transient cache, so it is freed after each
1905 // filter traversal.
senorblanco900c3672016-04-27 11:31:23 -07001906 return SkImageFilterCache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001907}
reedf037e0b2014-10-30 11:34:15 -07001908
1909#endif