blob: 10bcecf2ba350c3b08e002f22815835d166288bd [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
9
robertphillipsccb1b572015-05-27 11:02:55 -070010#include "GrBlurUtils.h"
kkinnunenabcfab42015-02-22 22:53:44 -080011#include "GrContext.h"
herb11a7f7f2015-11-24 12:41:00 -080012#include "SkDraw.h"
kkinnunenabcfab42015-02-22 22:53:44 -080013#include "GrGpu.h"
14#include "GrGpuResourcePriv.h"
bsalomonc55271f2015-11-09 11:55:57 -080015#include "GrImageIDTextureAdjuster.h"
robertphillips98d709b2014-09-02 10:20:50 -070016#include "GrLayerHoister.h"
robertphillips274b4ba2014-09-04 07:24:18 -070017#include "GrRecordReplaceDraw.h"
bsalomon6663acf2016-05-10 09:14:17 -070018#include "GrStyle.h"
egdanielbbcb38d2014-06-19 10:19:29 -070019#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080020#include "SkCanvasPriv.h"
kkinnunenabcfab42015-02-22 22:53:44 -080021#include "SkErrorInternals.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000022#include "SkGlyphCache.h"
kkinnunenabcfab42015-02-22 22:53:44 -080023#include "SkGrTexturePixelRef.h"
bsalomonf276ac52015-10-09 13:36:42 -070024#include "SkGr.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070025#include "SkGrPriv.h"
reeda85d4d02015-05-06 12:56:48 -070026#include "SkImage_Base.h"
bsalomon1cf6f9b2015-12-08 10:53:43 -080027#include "SkImageCacherator.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070029#include "SkImageFilterCache.h"
robertphillips82365912014-11-12 09:32:34 -080030#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000031#include "SkMaskFilter.h"
joshualitt33a5fce2015-11-18 13:28:51 -080032#include "SkNinePatchIter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000034#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070035#include "SkPictureData.h"
reed1e7f5e72016-04-27 07:49:17 -070036#include "SkRasterClip.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080038#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000039#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000040#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080041#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000042#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000043#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000044#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070045#include "SkXfermode.h"
joshualitta61c8172015-08-17 10:51:22 -070046#include "batches/GrRectBatchFactory.h"
kkinnunenabcfab42015-02-22 22:53:44 -080047#include "effects/GrBicubicEffect.h"
48#include "effects/GrDashingEffect.h"
49#include "effects/GrSimpleTextureEffect.h"
50#include "effects/GrTextureDomain.h"
joshualitt8e84a1e2016-02-16 11:09:25 -080051#include "text/GrTextUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000052
reedf037e0b2014-10-30 11:34:15 -070053#if SK_SUPPORT_GPU
54
joshualittce894002016-01-11 13:29:31 -080055#define ASSERT_SINGLE_OWNER \
56 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->debugSingleOwner());)
57
senorblanco55b6d8b2014-07-30 11:26:46 -070058enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
59
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000060#if 0
61 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080062 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000063 do { \
64 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080065 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000066 } while (0)
67#else
joshualitt5531d512014-12-17 15:50:11 -080068 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000069#endif
70
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000071///////////////////////////////////////////////////////////////////////////////
72
bsalomonbcf0a522014-10-08 08:40:09 -070073// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
74// just accesses the backing GrTexture. Otherwise, it creates a cached texture
75// representation and releases it in the destructor.
76class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040077public:
bsalomonbcf0a522014-10-08 08:40:09 -070078 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070079
bsalomonbcf0a522014-10-08 08:40:09 -070080 AutoBitmapTexture(GrContext* context,
81 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -070082 const GrTextureParams& params,
bsalomonbcf0a522014-10-08 08:40:09 -070083 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040084 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070085 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040086 }
87
bsalomonbcf0a522014-10-08 08:40:09 -070088 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040089 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -070090 const GrTextureParams& params) {
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 {
97 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
98 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();
brianosmana6359362016-03-21 06:55:37 -0700214 SkColorProfileType pt = origInfo.profileType();
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
222 GrPixelConfig origConfig = SkImageInfo2GrPixelConfig(ct, at, pt, *context->caps());
223 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
robertphillipsca6eafc2016-05-17 09:57:46 -0700228 GrPixelConfig config = SkImageInfo2GrPixelConfig(ct, at, pt, *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);
234
kkinnunenabcfab42015-02-22 22:53:44 -0800235}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000236
robertphillips1579e3c2016-03-24 05:01:23 -0700237// This method ensures that we always have a texture-backed "bitmap" when we finally
238// call through to the base impl so that the image filtering code will take the
239// gpu-specific paths. This mirrors SkCanvas::internalDrawDevice (the other
240// use of SkImageFilter::filterImage) in that the source and dest will have
241// homogenous backing (e.g., raster or gpu).
robertphillips2302de92016-03-24 07:26:32 -0700242void SkGpuDevice::drawSpriteWithFilter(const SkDraw& draw, const SkBitmap& bitmap,
243 int x, int y, const SkPaint& paint) {
robertphillips090b7622016-03-28 11:07:43 -0700244 ASSERT_SINGLE_OWNER
245 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpriteWithFilter", fContext);
246
247 if (fContext->abandoned()) {
248 return;
249 }
250
robertphillips1579e3c2016-03-24 05:01:23 -0700251 if (bitmap.getTexture()) {
robertphillips2302de92016-03-24 07:26:32 -0700252 INHERITED::drawSpriteWithFilter(draw, bitmap, x, y, paint);
robertphillips1579e3c2016-03-24 05:01:23 -0700253 return;
254 }
255
256 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
257 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
258 return;
259 }
260
261 GrTexture* texture;
262 // draw sprite neither filters nor tiles.
263 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
264 if (!texture) {
265 return;
266 }
267
268 SkBitmap newBitmap;
269
270 GrWrapTextureInBitmap(texture, texture->width(), texture->height(),
271 bitmap.isOpaque(), &newBitmap);
272
robertphillips2302de92016-03-24 07:26:32 -0700273 INHERITED::drawSpriteWithFilter(draw, newBitmap, x, y, paint);
robertphillips1579e3c2016-03-24 05:01:23 -0700274}
275
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000276///////////////////////////////////////////////////////////////////////////////
277
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000278bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
279 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800280 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000281
282 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700283 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps());
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000284 if (kUnknown_GrPixelConfig == config) {
285 return false;
286 }
287
288 uint32_t flags = 0;
289 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
290 flags = GrContext::kUnpremul_PixelOpsFlag;
291 }
bsalomon74f681d2015-06-23 14:38:48 -0700292 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
293 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000294}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000295
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000296bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
297 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800298 ASSERT_SINGLE_OWNER
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000299 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700300 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fContext->caps());
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000301 if (kUnknown_GrPixelConfig == config) {
302 return false;
303 }
304 uint32_t flags = 0;
305 if (kUnpremul_SkAlphaType == info.alphaType()) {
306 flags = GrContext::kUnpremul_PixelOpsFlag;
307 }
308 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
309
310 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700311 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000312
313 return true;
314}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000315
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000316const SkBitmap& SkGpuDevice::onAccessBitmap() {
joshualittce894002016-01-11 13:29:31 -0800317 ASSERT_SINGLE_OWNER
reed89443ab2014-06-27 11:34:19 -0700318 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000319}
320
reed41e010c2015-06-09 12:16:53 -0700321bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800322 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700323 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
324 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
325 // ... ugh.
326 fLegacyBitmap.notifyPixelsChanged();
327 return false;
328}
329
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000330void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
joshualittce894002016-01-11 13:29:31 -0800331 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000332 INHERITED::onAttachToCanvas(canvas);
333
334 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800335 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000336}
337
338void SkGpuDevice::onDetachFromCanvas() {
joshualittce894002016-01-11 13:29:31 -0800339 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000340 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800341 fClip.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700342 fClipStack.reset(nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000343}
344
345// call this every draw call, to ensure that the context reflects our state,
346// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800347void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800348 ASSERT_SINGLE_OWNER
joshualitt44701df2015-02-23 14:44:57 -0800349 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000350
joshualitt44701df2015-02-23 14:44:57 -0800351 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000352
cdalton846c0512016-05-13 10:25:00 -0700353 fClip.reset(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000354}
355
356GrRenderTarget* SkGpuDevice::accessRenderTarget() {
joshualittce894002016-01-11 13:29:31 -0800357 ASSERT_SINGLE_OWNER
robertphillipsca6eafc2016-05-17 09:57:46 -0700358 return fRenderTarget.get();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000359}
360
robertphillips175dd9b2016-04-28 14:32:04 -0700361GrDrawContext* SkGpuDevice::accessDrawContext() {
362 ASSERT_SINGLE_OWNER
363 return fDrawContext.get();
364}
365
reed8eddfb52014-12-04 07:50:14 -0800366void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800367 ASSERT_SINGLE_OWNER
reed8eddfb52014-12-04 07:50:14 -0800368 GrColor color = 0;
joshualitt5651ee62016-01-11 10:39:11 -0800369 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext);
reed8eddfb52014-12-04 07:50:14 -0800370 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700371 fDrawContext->clear(&rect, color, true);
reed8eddfb52014-12-04 07:50:14 -0800372}
373
robertphillipsca6eafc2016-05-17 09:57:46 -0700374void SkGpuDevice::replaceDrawContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800375 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800376
bsalomon5ec26ae2016-02-25 08:33:02 -0800377 SkBudgeted budgeted = fRenderTarget->resourcePriv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800378
robertphillipsca6eafc2016-05-17 09:57:46 -0700379 sk_sp<GrDrawContext> newDC(CreateDrawContext(this->context(),
380 budgeted,
381 this->imageInfo(),
382 fDrawContext->numColorSamples(),
383 &this->surfaceProps()));
384 if (!newDC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800385 return;
386 }
387
388 if (shouldRetainContent) {
389 if (fRenderTarget->wasDestroyed()) {
390 return;
391 }
robertphillipsca6eafc2016-05-17 09:57:46 -0700392 newDC->copySurface(fDrawContext->asTexture().get(),
393 SkIRect::MakeWH(this->width(), this->height()),
394 SkIPoint::Make(0, 0));
kkinnunenabcfab42015-02-22 22:53:44 -0800395 }
396
robertphillipsca6eafc2016-05-17 09:57:46 -0700397 SkASSERT(fDrawContext->accessRenderTarget() != newDC->accessRenderTarget());
kkinnunenabcfab42015-02-22 22:53:44 -0800398
robertphillipsca6eafc2016-05-17 09:57:46 -0700399 fRenderTarget = newDC->renderTarget();
kkinnunenabcfab42015-02-22 22:53:44 -0800400
bsalomon74f681d2015-06-23 14:38:48 -0700401#ifdef SK_DEBUG
402 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
403 kPremul_SkAlphaType);
404 SkASSERT(info == fLegacyBitmap.info());
405#endif
robertphillipsca6eafc2016-05-17 09:57:46 -0700406 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget.get());
kkinnunenabcfab42015-02-22 22:53:44 -0800407 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700408
robertphillipsca6eafc2016-05-17 09:57:46 -0700409 fDrawContext = newDC;
kkinnunenabcfab42015-02-22 22:53:44 -0800410}
411
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000412///////////////////////////////////////////////////////////////////////////////
413
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000414void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800415 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800416 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800417 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000418
419 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700420 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700421 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700422 return;
423 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000424
robertphillips2e1e51f2015-10-15 08:01:48 -0700425 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000426}
427
428// must be in SkCanvas::PointMode order
429static const GrPrimitiveType gPointMode2PrimtiveType[] = {
430 kPoints_GrPrimitiveType,
431 kLines_GrPrimitiveType,
432 kLineStrip_GrPrimitiveType
433};
434
ethannicholas330bb952015-07-17 06:44:02 -0700435// suppress antialiasing on axis-aligned integer-coordinate lines
436static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
437 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
438 return false;
439 }
440 if (count == 2) {
441 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
442 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800443 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700444 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800445 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700446 // the two end pixels as being the lesser of two evils.
447 if (pts[0].fX == pts[1].fX) {
448 return ((int) pts[0].fX) != pts[0].fX;
449 }
450 if (pts[0].fY == pts[1].fY) {
451 return ((int) pts[0].fY) != pts[0].fY;
452 }
453 }
454 return true;
455}
456
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000457void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
458 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800459 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800460 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800461 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000462
463 SkScalar width = paint.getStrokeWidth();
464 if (width < 0) {
465 return;
466 }
467
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000468 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700469 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700470 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700471 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700472 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700473 return;
474 }
egdaniele61c4112014-06-12 10:24:21 -0700475 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700476 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700477 path.moveTo(pts[0]);
478 path.lineTo(pts[1]);
bsalomon6663acf2016-05-10 09:14:17 -0700479 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, style);
egdaniele61c4112014-06-12 10:24:21 -0700480 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000481 }
482
ethannicholas330bb952015-07-17 06:44:02 -0700483 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000484 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800485 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700486 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000487 draw.drawPoints(mode, count, pts, paint, true);
488 return;
489 }
490
491 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700492 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700493 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700494 return;
495 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000496
robertphillips2e1e51f2015-10-15 08:01:48 -0700497 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700498 grPaint,
499 *draw.fMatrix,
500 gPointMode2PrimtiveType[mode],
501 SkToS32(count),
502 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700503 nullptr,
504 nullptr,
505 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700506 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000507}
508
509///////////////////////////////////////////////////////////////////////////////
510
robertphillipsff55b492015-11-24 07:56:59 -0800511void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800512 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800513 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800514 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000515
516 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
517 SkScalar width = paint.getStrokeWidth();
518
519 /*
520 We have special code for hairline strokes, miter-strokes, bevel-stroke
521 and fills. Anything else we just call our path code.
522 */
523 bool usePath = doStroke && width > 0 &&
524 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
525 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
egdanield58a0ba2014-06-11 10:30:05 -0700526
robertphillipsd8aa59d2015-08-05 09:07:12 -0700527 // a few other reasons we might need to call drawPath...
robertphillipsff55b492015-11-24 07:56:59 -0800528 if (paint.getMaskFilter() || paint.getPathEffect() ||
robertphillipsd8aa59d2015-08-05 09:07:12 -0700529 paint.getStyle() == SkPaint::kStrokeAndFill_Style) { // we can't both stroke and fill rects
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000530 usePath = true;
531 }
egdanield58a0ba2014-06-11 10:30:05 -0700532
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000533 if (usePath) {
534 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700535 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000536 path.addRect(rect);
robertphillips6c7e3252016-04-27 10:47:51 -0700537 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800538 fClip, path, paint,
539 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700540 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541 return;
542 }
543
544 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700545 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700546 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700547 return;
548 }
Mike Klein744fb732014-06-23 15:13:26 -0400549
bsalomon6663acf2016-05-10 09:14:17 -0700550 GrStyle style(paint);
551 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000552}
553
554///////////////////////////////////////////////////////////////////////////////
555
556void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800557 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800558 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800559 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800560 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000561
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000562 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700563 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700564 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700565 return;
566 }
Mike Klein744fb732014-06-23 15:13:26 -0400567
bsalomon6663acf2016-05-10 09:14:17 -0700568 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000569 if (paint.getMaskFilter()) {
570 // try to hit the fast path for drawing filtered round rects
571
572 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800573 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000574 if (devRRect.allCornersCircular()) {
575 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700576 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
reed1e7f5e72016-04-27 07:49:17 -0700577 draw.fRC->getBounds(),
joshualitt5531d512014-12-17 15:50:11 -0800578 *draw.fMatrix,
579 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000580 SkIRect finalIRect;
581 maskRect.roundOut(&finalIRect);
reed1e7f5e72016-04-27 07:49:17 -0700582 if (draw.fRC->quickReject(finalIRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000583 // clipped out
584 return;
585 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700586 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
robertphillips6c7e3252016-04-27 10:47:51 -0700587 fDrawContext.get(),
joshualitt25d9c152015-02-18 12:29:52 -0800588 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800589 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800590 *draw.fMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700591 style.strokeRec(),
egdanield58a0ba2014-06-11 10:30:05 -0700592 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000593 return;
594 }
595 }
596
597 }
598 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000599 }
600
bsalomon6663acf2016-05-10 09:14:17 -0700601 if (paint.getMaskFilter() || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800602 // The only mask filter the native rrect drawing code could've handle was taken
603 // care of above.
604 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000605 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700606 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000607 path.addRRect(rect);
robertphillips6c7e3252016-04-27 10:47:51 -0700608 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800609 fClip, path, paint,
610 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700611 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000612 return;
613 }
Mike Klein744fb732014-06-23 15:13:26 -0400614
bsalomon6663acf2016-05-10 09:14:17 -0700615 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800616
bsalomon6663acf2016-05-10 09:14:17 -0700617 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000618}
619
robertphillipsd7706102016-02-25 09:28:08 -0800620
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000621void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800622 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800623 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800624 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800625 CHECK_SHOULD_DRAW(draw);
626
robertphillipsd7706102016-02-25 09:28:08 -0800627 if (outer.isEmpty()) {
628 return;
629 }
630
631 if (inner.isEmpty()) {
632 return this->drawRRect(draw, outer, paint);
633 }
634
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000635 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000636
robertphillips0e7029e2015-11-30 05:45:06 -0800637 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800638 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700639 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700640 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700641 return;
642 }
robertphillips00095892016-02-29 13:50:40 -0800643
644 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
645 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000646 }
647
648 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700649 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000650 path.addRRect(outer);
651 path.addRRect(inner);
652 path.setFillType(SkPath::kEvenOdd_FillType);
653
robertphillips6c7e3252016-04-27 10:47:51 -0700654 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillips0e7029e2015-11-30 05:45:06 -0800655 fClip, path, paint,
656 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700657 draw.fRC->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000658}
659
660
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000661/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000662
robertphillips514450c2015-11-24 05:36:02 -0800663void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800664 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800665 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800666 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000667
robertphillips514450c2015-11-24 05:36:02 -0800668 // Presumably the path effect warps this to something other than an oval
669 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000670 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700671 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000672 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700673 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000674 return;
herb11a7f7f2015-11-24 12:41:00 -0800675 }
676
robertphillips514450c2015-11-24 05:36:02 -0800677 if (paint.getMaskFilter()) {
678 // The RRect path can handle special case blurring
679 SkRRect rr = SkRRect::MakeOval(oval);
680 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000681 }
682
683 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700684 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700685 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700686 return;
687 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000688
bsalomon6663acf2016-05-10 09:14:17 -0700689 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000690}
691
692#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000693
694///////////////////////////////////////////////////////////////////////////////
695
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000696void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
697 const SkPaint& paint, const SkMatrix* prePathMatrix,
698 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800699 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800700 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
701 bool isClosed;
702 SkRect rect;
703 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
704 this->drawRect(draw, rect, paint);
705 return;
706 }
707 if (origSrcPath.isOval(&rect)) {
708 this->drawOval(draw, rect, paint);
709 return;
710 }
711 SkRRect rrect;
712 if (origSrcPath.isRRect(&rrect)) {
713 this->drawRRect(draw, rrect, paint);
714 return;
715 }
716 }
717
joshualitt5531d512014-12-17 15:50:11 -0800718 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800719 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000720
robertphillips6c7e3252016-04-27 10:47:51 -0700721 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsccb1b572015-05-27 11:02:55 -0700722 fClip, origSrcPath, paint,
723 *draw.fMatrix, prePathMatrix,
reed1e7f5e72016-04-27 07:49:17 -0700724 draw.fRC->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000725}
726
727static const int kBmpSmallTileSize = 1 << 10;
728
729static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
730 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
731 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
732 return tilesX * tilesY;
733}
734
reed85d91782015-09-10 14:33:38 -0700735static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000736 if (maxTileSize <= kBmpSmallTileSize) {
737 return maxTileSize;
738 }
739
740 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
741 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
742
743 maxTileTotalTileSize *= maxTileSize * maxTileSize;
744 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
745
746 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
747 return kBmpSmallTileSize;
748 } else {
749 return maxTileSize;
750 }
751}
752
753// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
754// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700755static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800756 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800757 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700758 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000759 const SkRect* srcRectPtr,
760 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700761 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000762 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800763 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764 clippedSrcIRect->setEmpty();
765 return;
766 }
767 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
768 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700769 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000770 // we've setup src space 0,0 to map to the top left of the src rect.
771 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000772 if (!clippedSrcRect.intersect(*srcRectPtr)) {
773 clippedSrcIRect->setEmpty();
774 return;
775 }
776 }
777 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700778 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000779 if (!clippedSrcIRect->intersect(bmpBounds)) {
780 clippedSrcIRect->setEmpty();
781 }
782}
783
reed85d91782015-09-10 14:33:38 -0700784bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
785 const SkMatrix& viewMatrix,
786 const GrTextureParams& params,
787 const SkRect* srcRectPtr,
788 int maxTileSize,
789 int* tileSize,
790 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800791 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700792 // if it's larger than the max tile size, then we have no choice but tiling.
793 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
robertphillipse5768742016-05-13 11:20:46 -0700794 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(),
795 fClip, viewMatrix, imageRect.size(),
reed85d91782015-09-10 14:33:38 -0700796 srcRectPtr, clippedSubset);
797 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
798 return true;
799 }
800
bsalomon1a1d0b82015-10-16 07:49:42 -0700801 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700802 const size_t area = imageRect.width() * imageRect.height();
803 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
804 return false;
805 }
806
reed85d91782015-09-10 14:33:38 -0700807 // At this point we know we could do the draw by uploading the entire bitmap
808 // as a texture. However, if the texture would be large compared to the
809 // cache size and we don't require most of it for this draw then tile to
810 // reduce the amount of upload and cache spill.
811
812 // assumption here is that sw bitmap size is a good proxy for its size as
813 // a texture
814 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
815 size_t cacheSize;
816 fContext->getResourceCacheLimits(nullptr, &cacheSize);
817 if (bmpSize < cacheSize / 2) {
818 return false;
819 }
820
bsalomon1a1d0b82015-10-16 07:49:42 -0700821 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
822 // tiling memory savings would be < 50%.
robertphillipse5768742016-05-13 11:20:46 -0700823 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(),
824 fClip, viewMatrix, imageRect.size(), srcRectPtr,
reed85d91782015-09-10 14:33:38 -0700825 clippedSubset);
826 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
827 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
828 kBmpSmallTileSize * kBmpSmallTileSize;
829
830 return usedTileBytes < 2 * bmpSize;
831}
832
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000833bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800834 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000835 const GrTextureParams& params,
836 const SkRect* srcRectPtr,
837 int maxTileSize,
838 int* tileSize,
839 SkIRect* clippedSrcRect) const {
joshualittce894002016-01-11 13:29:31 -0800840 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000841 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700842 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000843 return false;
844 }
845
reed85d91782015-09-10 14:33:38 -0700846 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
847 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
848}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000849
reed85d91782015-09-10 14:33:38 -0700850bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
851 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
852 const SkMatrix& viewMatrix) const {
joshualittce894002016-01-11 13:29:31 -0800853 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700854 // if image is explictly texture backed then just use the texture
855 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000856 return false;
857 }
858
reed85d91782015-09-10 14:33:38 -0700859 GrTextureParams params;
860 bool doBicubic;
861 GrTextureParams::FilterMode textureFilterMode =
862 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
863
864 int tileFilterPad;
865 if (doBicubic) {
866 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
867 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
868 tileFilterPad = 0;
869 } else {
870 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000871 }
reed85d91782015-09-10 14:33:38 -0700872 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000873
bsalomon8c07b7a2015-11-02 11:36:52 -0800874 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000875
reed85d91782015-09-10 14:33:38 -0700876 // these are output, which we safely ignore, as we just want to know the predicate
877 int outTileSize;
878 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000879
reed85d91782015-09-10 14:33:38 -0700880 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
881 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000882}
883
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000884void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000885 const SkBitmap& bitmap,
886 const SkMatrix& m,
887 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800888 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800889 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800890 SkMatrix viewMatrix;
891 viewMatrix.setConcat(*origDraw.fMatrix, m);
892 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800893 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonb1b01992015-11-18 10:56:08 -0800894 // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
bsalomonf1ecd212015-12-09 17:06:02 -0800895 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
896 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800897 return;
898 }
bsalomonb1b01992015-11-18 10:56:08 -0800899 int maxTileSize = fContext->caps()->maxTileSize();
900
901 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
902 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
robertphillips071457b2016-05-16 09:50:03 -0700903 bool drawAA = !fDrawContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -0800904 paint.isAntiAlias() &&
905 bitmap.width() <= maxTileSize &&
906 bitmap.height() <= maxTileSize;
907
908 bool skipTileCheck = drawAA || paint.getMaskFilter();
909
910 if (!skipTileCheck) {
911 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
912 int tileSize;
913 SkIRect clippedSrcRect;
914
915 GrTextureParams params;
916 bool doBicubic;
917 GrTextureParams::FilterMode textureFilterMode =
918 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
919 &doBicubic);
920
921 int tileFilterPad;
922
923 if (doBicubic) {
924 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
925 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
926 tileFilterPad = 0;
927 } else {
928 tileFilterPad = 1;
929 }
930 params.setFilterMode(textureFilterMode);
931
932 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
933 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
934 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
935 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
936 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
937 return;
938 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000939 }
bsalomonb1b01992015-11-18 10:56:08 -0800940 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800941 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
942 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000943}
944
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000945// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000946// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
947// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000948static inline void clamped_outset_with_offset(SkIRect* iRect,
949 int outset,
950 SkPoint* offset,
951 const SkIRect& clamp) {
952 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000953
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000954 int leftClampDelta = clamp.fLeft - iRect->fLeft;
955 if (leftClampDelta > 0) {
956 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000957 iRect->fLeft = clamp.fLeft;
958 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000959 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000960 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000961
962 int topClampDelta = clamp.fTop - iRect->fTop;
963 if (topClampDelta > 0) {
964 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000965 iRect->fTop = clamp.fTop;
966 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000967 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000968 }
969
970 if (iRect->fRight > clamp.fRight) {
971 iRect->fRight = clamp.fRight;
972 }
973 if (iRect->fBottom > clamp.fBottom) {
974 iRect->fBottom = clamp.fBottom;
975 }
976}
977
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000978// Break 'bitmap' into several tiles to draw it since it has already
979// been determined to be too large to fit in VRAM
980void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800981 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982 const SkRect& srcRect,
983 const SkIRect& clippedSrcIRect,
984 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800985 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700986 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000987 int tileSize,
988 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800989 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800990
ericrk983294f2016-04-18 09:14:00 -0700991 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800992 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700993 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800994
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000995 // The following pixel lock is technically redundant, but it is desirable
996 // to lock outside of the tile loop to prevent redecoding the whole image
997 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
998 // is larger than the limit of the discardable memory pool.
999 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -08001000
1001 const SkPaint* paint = &origPaint;
1002 SkPaint tempPaint;
robertphillips071457b2016-05-16 09:50:03 -07001003 if (origPaint.isAntiAlias() && !fDrawContext->isUnifiedMultisampled()) {
bsalomonc55271f2015-11-09 11:55:57 -08001004 // Drop antialiasing to avoid seams at tile boundaries.
1005 tempPaint = origPaint;
1006 tempPaint.setAntiAlias(false);
1007 paint = &tempPaint;
1008 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001009 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1010
1011 int nx = bitmap.width() / tileSize;
1012 int ny = bitmap.height() / tileSize;
1013 for (int x = 0; x <= nx; x++) {
1014 for (int y = 0; y <= ny; y++) {
1015 SkRect tileR;
1016 tileR.set(SkIntToScalar(x * tileSize),
1017 SkIntToScalar(y * tileSize),
1018 SkIntToScalar((x + 1) * tileSize),
1019 SkIntToScalar((y + 1) * tileSize));
1020
1021 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1022 continue;
1023 }
1024
1025 if (!tileR.intersect(srcRect)) {
1026 continue;
1027 }
1028
1029 SkBitmap tmpB;
1030 SkIRect iTileR;
1031 tileR.roundOut(&iTileR);
1032 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1033 SkIntToScalar(iTileR.fTop));
1034
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001035 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001036 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001037 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001038 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001039 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001040
robertphillipsec8bb942014-11-21 10:16:25 -08001041 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001042 SkIRect iClampRect;
1043
reeda5517e22015-07-14 10:54:12 -07001044 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045 // In bleed mode we want to always expand the tile on all edges
1046 // but stay within the bitmap bounds
1047 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1048 } else {
1049 // In texture-domain/clamp mode we only want to expand the
1050 // tile on edges interior to "srcRect" (i.e., we want to
1051 // not bleed across the original clamped edges)
1052 srcRect.roundOut(&iClampRect);
1053 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001054 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1055 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001056 }
1057
1058 if (bitmap.extractSubset(&tmpB, iTileR)) {
1059 // now offset it to make it "local" to our tmp bitmap
1060 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001061 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001062 // de-optimized this determination
1063 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001064 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001065 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001066 tileR,
1067 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -08001068 *paint,
reeda5517e22015-07-14 10:54:12 -07001069 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001070 bicubic,
1071 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001072 }
1073 }
1074 }
1075}
1076
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001077/*
1078 * This is called by drawBitmap(), which has to handle images that may be too
1079 * large to be represented by a single texture.
1080 *
1081 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1082 * and that non-texture portion of the GrPaint has already been setup.
1083 */
1084void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001085 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001086 const SkRect& srcRect,
1087 const GrTextureParams& params,
1088 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001089 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001090 bool bicubic,
1091 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001092 // We should have already handled bitmaps larger than the max texture size.
1093 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1094 bitmap.height() <= fContext->caps()->maxTextureSize());
1095 // Unless the bitmap is inherently texture-backed, we should be respecting the max tile size
1096 // by the time we get here.
1097 SkASSERT(bitmap.getTexture() ||
1098 (bitmap.width() <= fContext->caps()->maxTileSize() &&
1099 bitmap.height() <= fContext->caps()->maxTileSize()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001100
1101 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001102 AutoBitmapTexture abt(fContext, bitmap, params, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001103 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001104 return;
1105 }
1106
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001107 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001108 SkRect paintRect;
1109 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1110 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1111 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1112 SkScalarMul(srcRect.fTop, hInv),
1113 SkScalarMul(srcRect.fRight, wInv),
1114 SkScalarMul(srcRect.fBottom, hInv));
1115
egdaniel79da63f2015-10-09 10:55:16 -07001116 SkMatrix texMatrix;
1117 texMatrix.reset();
1118 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1119 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1120 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1121 // Thus we need to pass in the transform matrix directly to the texture processor used for
1122 // the bitmap draw.
1123 texMatrix.setScale(wInv, hInv);
1124 }
1125
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001126 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001127
1128 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1129 // the rest from the SkPaint.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001130 SkAutoTUnref<const GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001131
reeda5517e22015-07-14 10:54:12 -07001132 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001133 // Use a constrained texture domain to avoid color bleeding
1134 SkScalar left, top, right, bottom;
1135 if (srcRect.width() > SK_Scalar1) {
1136 SkScalar border = SK_ScalarHalf / texture->width();
1137 left = paintRect.left() + border;
1138 right = paintRect.right() - border;
1139 } else {
1140 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1141 }
1142 if (srcRect.height() > SK_Scalar1) {
1143 SkScalar border = SK_ScalarHalf / texture->height();
1144 top = paintRect.top() + border;
1145 bottom = paintRect.bottom() - border;
1146 } else {
1147 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1148 }
1149 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001150 if (bicubic) {
egdaniel79da63f2015-10-09 10:55:16 -07001151 fp.reset(GrBicubicEffect::Create(texture, texMatrix, textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001152 } else {
bsalomon4a339522015-10-06 08:40:50 -07001153 fp.reset(GrTextureDomainEffect::Create(texture,
egdaniel79da63f2015-10-09 10:55:16 -07001154 texMatrix,
joshualitt5531d512014-12-17 15:50:11 -08001155 textureDomain,
1156 GrTextureDomain::kClamp_Mode,
1157 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001158 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001159 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001160 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1161 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
egdaniel79da63f2015-10-09 10:55:16 -07001162 fp.reset(GrBicubicEffect::Create(texture, texMatrix, tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001163 } else {
egdaniel79da63f2015-10-09 10:55:16 -07001164 fp.reset(GrSimpleTextureEffect::Create(texture, texMatrix, params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001165 }
1166
joshualitt33a5fce2015-11-18 13:28:51 -08001167 GrPaint grPaint;
1168 if (!SkPaintToGrPaintWithTexture(this->context(), paint, viewMatrix, fp,
brianosman898235c2016-04-06 07:38:23 -07001169 kAlpha_8_SkColorType == bitmap.colorType(),
brianosmanb461d342016-04-13 13:10:14 -07001170 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001171 return;
1172 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001173
egdaniel79da63f2015-10-09 10:55:16 -07001174 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1175 // We don't have local coords in this case and have previously set the transform
1176 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001177 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001178 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001179 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001180 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001181}
1182
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001183void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1184 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001185 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001186 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001187 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001188
1189 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1190 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1191 return;
1192 }
1193
robertphillipse1849d12016-03-17 08:26:38 -07001194 int offX = bitmap.pixelRefOrigin().fX;
1195 int offY = bitmap.pixelRefOrigin().fY;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001196 int w = bitmap.width();
1197 int h = bitmap.height();
1198
1199 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001200 // draw sprite neither filters nor tiles.
1201 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001202 if (!texture) {
1203 return;
1204 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001205
bsalomonf1b7a1d2015-09-28 06:26:28 -07001206 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
1207
senorblancof35566e2016-04-18 10:32:02 -07001208 SkASSERT(!paint.getImageFilter());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001209
1210 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001211 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001212 GrSimpleTextureEffect::Create(texture, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001213 if (alphaOnly) {
1214 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1215 } else {
1216 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1217 }
brianosman898235c2016-04-06 07:38:23 -07001218 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp,
brianosmanb461d342016-04-13 13:10:14 -07001219 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001220 return;
1221 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001222
bsalomona2e69fc2015-11-05 10:41:43 -08001223 fDrawContext->fillRectToRect(fClip,
1224 grPaint,
1225 SkMatrix::I(),
1226 SkRect::MakeXYWH(SkIntToScalar(left),
1227 SkIntToScalar(top),
1228 SkIntToScalar(w),
1229 SkIntToScalar(h)),
robertphillipse1849d12016-03-17 08:26:38 -07001230 SkRect::MakeXYWH(SkIntToScalar(offX) / texture->width(),
1231 SkIntToScalar(offY) / texture->height(),
1232 SkIntToScalar(w) / texture->width(),
1233 SkIntToScalar(h) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001234}
1235
bsalomonb1b01992015-11-18 10:56:08 -08001236void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1237 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001238 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001239 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001240 CHECK_SHOULD_DRAW(draw);
bsalomonb1b01992015-11-18 10:56:08 -08001241 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001242 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001243 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
1244 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001245 return;
1246 }
bsalomonb1b01992015-11-18 10:56:08 -08001247 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1248 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1249 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1250 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1251 const SkRect* dst = &origDst;
1252 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001253 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001254 if (!src) {
1255 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001256 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001257
bsalomonb1b01992015-11-18 10:56:08 -08001258 SkMatrix srcToDstMatrix;
1259 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1260 return;
1261 }
1262 SkRect tmpSrc, tmpDst;
1263 if (src != &bmpBounds) {
1264 if (!bmpBounds.contains(*src)) {
1265 tmpSrc = *src;
1266 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001267 return; // nothing to draw
1268 }
bsalomonb1b01992015-11-18 10:56:08 -08001269 src = &tmpSrc;
1270 srcToDstMatrix.mapRect(&tmpDst, *src);
1271 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001272 }
1273 }
1274
bsalomonb1b01992015-11-18 10:56:08 -08001275 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001276
bsalomonb1b01992015-11-18 10:56:08 -08001277 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1278 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
robertphillips071457b2016-05-16 09:50:03 -07001279 bool drawAA = !fDrawContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -08001280 paint.isAntiAlias() &&
1281 bitmap.width() <= maxTileSize &&
1282 bitmap.height() <= maxTileSize;
1283
1284 bool skipTileCheck = drawAA || paint.getMaskFilter();
1285
1286 if (!skipTileCheck) {
1287 int tileSize;
1288 SkIRect clippedSrcRect;
1289
1290 GrTextureParams params;
1291 bool doBicubic;
1292 GrTextureParams::FilterMode textureFilterMode =
1293 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1294 &doBicubic);
1295
1296 int tileFilterPad;
1297
1298 if (doBicubic) {
1299 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1300 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1301 tileFilterPad = 0;
1302 } else {
1303 tileFilterPad = 1;
1304 }
1305 params.setFilterMode(textureFilterMode);
1306
1307 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1308 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1309 // we have a mask filter.
1310 SkMatrix viewMatrix = *draw.fMatrix;
1311 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1312 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1313 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1314 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1315 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1316 constraint, tileSize, doBicubic);
1317 return;
1318 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001319 }
bsalomonb1b01992015-11-18 10:56:08 -08001320 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001321 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001322}
1323
1324void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1325 int x, int y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001326 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001327 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001328 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001329 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001330
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001331 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001332 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001333
robertphillipsca6eafc2016-05-17 09:57:46 -07001334 sk_sp<GrTexture> devTex(dev->accessDrawContext()->asTexture());
1335 if (!devTex) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001336 return;
1337 }
1338
robertphillips7b9e8a42014-12-11 08:20:31 -08001339 const SkImageInfo ii = dev->imageInfo();
1340 int w = ii.width();
1341 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001342
senorblancof35566e2016-04-18 10:32:02 -07001343 SkASSERT(!paint.getImageFilter());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001344
1345 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001346 SkAutoTUnref<const GrFragmentProcessor> fp(
robertphillipsca6eafc2016-05-17 09:57:46 -07001347 GrSimpleTextureEffect::Create(devTex.get(), SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001348 if (GrPixelConfigIsAlphaOnly(devTex->config())) {
1349 // Can this happen?
1350 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1351 } else {
1352 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1353 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001354
brianosman898235c2016-04-06 07:38:23 -07001355 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp,
brianosmanb461d342016-04-13 13:10:14 -07001356 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001357 return;
1358 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001359
1360 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1361 SkIntToScalar(y),
1362 SkIntToScalar(w),
1363 SkIntToScalar(h));
1364
1365 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1366 // scratch texture).
1367 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1368 SK_Scalar1 * h / devTex->height());
1369
bsalomona2e69fc2015-11-05 10:41:43 -08001370 fDrawContext->fillRectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001371}
1372
reeda85d4d02015-05-06 12:56:48 -07001373void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1374 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001375 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001376 SkMatrix viewMatrix = *draw.fMatrix;
1377 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001378 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001379 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001380 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001381 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1382 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001383 return;
reed85d91782015-09-10 14:33:38 -07001384 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001385 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001386 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1387 paint.getFilterQuality(), *draw.fMatrix)) {
1388 // only support tiling as bitmap at the moment, so force raster-version
1389 if (!as_IB(image)->getROPixels(&bm)) {
1390 return;
1391 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001392 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1393 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1394 CHECK_SHOULD_DRAW(draw);
1395 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001396 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1397 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001398 } else if (as_IB(image)->getROPixels(&bm)) {
1399 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001400 }
reeda85d4d02015-05-06 12:56:48 -07001401 }
1402}
1403
1404void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001405 const SkRect& dst, const SkPaint& paint,
1406 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001407 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001408 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001409 CHECK_SHOULD_DRAW(draw);
1410 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001411 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001412 return;
1413 }
1414 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001415 SkMatrix totalMatrix = *draw.fMatrix;
1416 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1417 dst.height() / (src ? src->height() : image->height()));
1418 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001419 // only support tiling as bitmap at the moment, so force raster-version
1420 if (!as_IB(image)->getROPixels(&bm)) {
1421 return;
1422 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001423 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1424 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1425 CHECK_SHOULD_DRAW(draw);
1426 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001427 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001428 } else if (as_IB(image)->getROPixels(&bm)) {
1429 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001430 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001431}
1432
bsalomonf1ecd212015-12-09 17:06:02 -08001433void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001434 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001435 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001436
joshualitt33a5fce2015-11-18 13:28:51 -08001437 CHECK_SHOULD_DRAW(draw);
1438
joshualittedb36442015-11-19 14:29:30 -08001439 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
robertphillips071457b2016-05-16 09:50:03 -07001440 fDrawContext->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001441 bool doBicubic;
1442 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001443 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1444 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001445 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001446 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001447
1448 SkRect srcR, dstR;
1449 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001450 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001451 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001452 }
1453 return;
1454 }
1455
bsalomon2bbd0c62015-12-09 12:50:56 -08001456 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
1457 SkAutoTUnref<const GrFragmentProcessor> fp(
1458 producer->createFragmentProcessor(SkMatrix::I(),
1459 SkRect::MakeIWH(producer->width(), producer->height()),
1460 GrTextureProducer::kNo_FilterConstraint, true,
1461 &kMode));
joshualitt33a5fce2015-11-18 13:28:51 -08001462 GrPaint grPaint;
1463 if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, fp,
brianosman898235c2016-04-06 07:38:23 -07001464 producer->isAlphaOnly(),
brianosmanb461d342016-04-13 13:10:14 -07001465 this->surfaceProps().isGammaCorrect(), &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001466 return;
1467 }
1468
bsalomon2bbd0c62015-12-09 12:50:56 -08001469 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1470 producer->height(), center, dst);
1471}
1472
1473void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1474 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001475 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001476 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001477 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001478 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001479 } else {
1480 SkBitmap bm;
1481 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1482 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001483 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001484 } else if (as_IB(image)->getROPixels(&bm)) {
1485 this->drawBitmapNine(draw, bm, center, dst, paint);
1486 }
1487 }
1488}
1489
1490void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1491 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001492 ASSERT_SINGLE_OWNER
bsalomon2bbd0c62015-12-09 12:50:56 -08001493 if (bitmap.getTexture()) {
1494 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001495 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001496 } else {
1497 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001498 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001499 }
joshualitt33a5fce2015-11-18 13:28:51 -08001500}
1501
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001502///////////////////////////////////////////////////////////////////////////////
1503
1504// must be in SkCanvas::VertexMode order
1505static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1506 kTriangles_GrPrimitiveType,
1507 kTriangleStrip_GrPrimitiveType,
1508 kTriangleFan_GrPrimitiveType,
1509};
1510
1511void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1512 int vertexCount, const SkPoint vertices[],
1513 const SkPoint texs[], const SkColor colors[],
1514 SkXfermode* xmode,
1515 const uint16_t indices[], int indexCount,
1516 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001517 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001518 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001519 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001520
halcanary96fcdcc2015-08-27 07:41:13 -07001521 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1522 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001523
halcanary96fcdcc2015-08-27 07:41:13 -07001524 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001525
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001526 SkPaint copy(paint);
1527 copy.setStyle(SkPaint::kStroke_Style);
1528 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001529
bsalomonf1b7a1d2015-09-28 06:26:28 -07001530 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001531 // we ignore the shader if texs is null.
brianosman898235c2016-04-06 07:38:23 -07001532 if (!SkPaintToGrPaintNoShader(this->context(), copy,
brianosmanb461d342016-04-13 13:10:14 -07001533 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001534 return;
1535 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001536
dandov32a311b2014-07-15 19:46:26 -07001537 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001538 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001539 switch (vmode) {
1540 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001541 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001542 break;
1543 case SkCanvas::kTriangleStrip_VertexMode:
1544 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001545 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001546 break;
1547 }
mtklein533eb782014-08-27 10:39:42 -07001548
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001549 VertState state(vertexCount, indices, indexCount);
1550 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001551
dandov32a311b2014-07-15 19:46:26 -07001552 //number of indices for lines per triangle with kLines
1553 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001554
bsalomonf1b7a1d2015-09-28 06:26:28 -07001555 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001556 int i = 0;
1557 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001558 lineIndices[i] = state.f0;
1559 lineIndices[i + 1] = state.f1;
1560 lineIndices[i + 2] = state.f1;
1561 lineIndices[i + 3] = state.f2;
1562 lineIndices[i + 4] = state.f2;
1563 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001564 i += 6;
1565 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001566 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001567 grPaint,
1568 *draw.fMatrix,
1569 kLines_GrPrimitiveType,
1570 vertexCount,
1571 vertices,
1572 texs,
1573 colors,
1574 lineIndices.get(),
1575 indexCount);
1576 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001577 }
1578
bsalomonf1b7a1d2015-09-28 06:26:28 -07001579 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001580
1581 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001582 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001583 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1584 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001585 convertedColors.reset(vertexCount);
1586 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001587 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001588 }
1589 colors = convertedColors.get();
1590 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001591 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001592 if (texs && paint.getShader()) {
1593 if (colors) {
1594 // When there are texs and colors the shader and colors are combined using xmode. A null
1595 // xmode is defined to mean modulate.
1596 SkXfermode::Mode colorMode;
1597 if (xmode) {
1598 if (!xmode->asMode(&colorMode)) {
1599 return;
1600 }
1601 } else {
1602 colorMode = SkXfermode::kModulate_Mode;
1603 }
1604 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
brianosmanb461d342016-04-13 13:10:14 -07001605 false, this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001606 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001607 return;
1608 }
1609 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001610 // We have a shader, but no colors to blend it against.
brianosman898235c2016-04-06 07:38:23 -07001611 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001612 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001613 return;
1614 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001615 }
bsalomonaa48d362015-10-01 08:34:17 -07001616 } else {
1617 if (colors) {
1618 // We have colors, but either have no shader or no texture coords (which implies that
1619 // we should ignore the shader).
brianosman898235c2016-04-06 07:38:23 -07001620 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001621 this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001622 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001623 return;
1624 }
1625 } else {
1626 // No colors and no shaders. Just draw with the paint color.
brianosman898235c2016-04-06 07:38:23 -07001627 if (!SkPaintToGrPaintNoShader(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001628 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001629 return;
1630 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001631 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001632 }
1633
robertphillips2e1e51f2015-10-15 08:01:48 -07001634 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001635 grPaint,
1636 *draw.fMatrix,
1637 primType,
1638 vertexCount,
1639 vertices,
1640 texs,
1641 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001642 indices,
robertphillipsea461502015-05-26 11:38:03 -07001643 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001644}
1645
1646///////////////////////////////////////////////////////////////////////////////
1647
jvanverth31ff7622015-08-07 10:09:28 -07001648void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001649 const SkRect texRect[], const SkColor colors[], int count,
1650 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001651 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001652 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001653 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001654 return;
1655 }
1656
jvanverth31ff7622015-08-07 10:09:28 -07001657 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001658 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001659
reedca109532015-06-25 16:25:25 -07001660 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001661 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001662
jvanverth31ff7622015-08-07 10:09:28 -07001663 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001664 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001665 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
brianosmanb461d342016-04-13 13:10:14 -07001666 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001667 return;
1668 }
1669 } else {
brianosman898235c2016-04-06 07:38:23 -07001670 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001671 this->surfaceProps().isGammaCorrect(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001672 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001673 }
1674 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001675
1676 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001677 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001678}
1679
1680///////////////////////////////////////////////////////////////////////////////
1681
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001682void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001683 size_t byteLength, SkScalar x, SkScalar y,
1684 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001685 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001686 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001687 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001688
jvanverth8c27a182014-10-14 08:45:50 -07001689 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001690 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001691 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001692 return;
1693 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001694
jvanverth8c27a182014-10-14 08:45:50 -07001695 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001696
robertphillips2e1e51f2015-10-15 08:01:48 -07001697 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001698 (const char *)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001699}
1700
fmalita05c4a432014-09-29 06:29:53 -07001701void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1702 const SkScalar pos[], int scalarsPerPos,
1703 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001704 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001705 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001706 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001707
jvanverth8c27a182014-10-14 08:45:50 -07001708 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001709 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001710 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001711 return;
1712 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001713
jvanverth8c27a182014-10-14 08:45:50 -07001714 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001715
robertphillips2e1e51f2015-10-15 08:01:48 -07001716 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001717 (const char *)text, byteLength, pos, scalarsPerPos, offset,
reed1e7f5e72016-04-27 07:49:17 -07001718 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001719}
1720
joshualitt9c328182015-03-23 08:13:04 -07001721void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1722 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001723 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001724 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001725 CHECK_SHOULD_DRAW(draw);
1726
1727 SkDEBUGCODE(this->validate();)
1728
robertphillips2e1e51f2015-10-15 08:01:48 -07001729 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001730 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001731}
1732
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001733///////////////////////////////////////////////////////////////////////////////
1734
reedb2db8982014-11-13 12:41:02 -08001735bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001736 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001737}
1738
1739void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001740 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001741
bsalomonc49e8682015-06-30 11:37:35 -07001742 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001743}
1744
1745///////////////////////////////////////////////////////////////////////////////
1746
reed76033be2015-03-14 10:54:31 -07001747SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001748 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001749
robertphillipsca6eafc2016-05-17 09:57:46 -07001750 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001751
robertphillipsca6eafc2016-05-17 09:57:46 -07001752 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001753 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001754 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1755 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001756
robertphillipsca6eafc2016-05-17 09:57:46 -07001757 sk_sp<GrDrawContext> dc(fContext->newDrawContext(fit,
1758 cinfo.fInfo.width(), cinfo.fInfo.height(),
1759 fDrawContext->config(),
1760 fDrawContext->desc().fSampleCnt,
1761 kDefault_GrSurfaceOrigin,
1762 &props));
1763 if (!dc) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001764 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001765 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001766 cinfo.fInfo.width(), cinfo.fInfo.height());
robertphillipsca6eafc2016-05-17 09:57:46 -07001767 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001768 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001769
1770 // Skia's convention is to only clear a device if it is non-opaque.
1771 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1772
1773 return SkGpuDevice::Make(std::move(dc),
1774 cinfo.fInfo.width(), cinfo.fInfo.height(),
1775 init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001776}
1777
reede8f30622016-03-23 18:59:25 -07001778sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001779 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001780 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001781 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
robertphillipse5768742016-05-13 11:20:46 -07001782 return SkSurface::MakeRenderTarget(fContext, kBudgeted, info, fDrawContext->desc().fSampleCnt,
reede8f30622016-03-23 18:59:25 -07001783 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001784}
1785
robertphillips30d2cc62014-09-24 08:52:18 -07001786bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001787 const SkMatrix* matrix, const SkPaint* paint) {
joshualittce894002016-01-11 13:29:31 -08001788 ASSERT_SINGLE_OWNER
robertphillips63242d72014-12-04 08:31:02 -08001789#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001790 // todo: should handle this natively
robertphillips74e9a4d2016-05-09 05:12:18 -07001791 if (paint ||
1792 (kRGBA_8888_SkColorType != mainCanvas->imageInfo().colorType() &&
1793 kBGRA_8888_SkColorType != mainCanvas->imageInfo().colorType())) {
reedd5fa1a42014-08-09 11:08:05 -07001794 return false;
1795 }
1796
halcanary96fcdcc2015-08-27 07:41:13 -07001797 const SkBigPicture::AccelData* data = nullptr;
mtklein9db912c2015-05-19 11:11:26 -07001798 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1799 data = bp->accelData();
1800 }
robertphillips81f71b62014-11-11 04:54:49 -08001801 if (!data) {
1802 return false;
1803 }
1804
robertphillipse5524cd2015-02-20 12:30:26 -08001805 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1806 if (0 == gpuData->numBlocks()) {
1807 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001808 }
1809
robertphillipsfd61ed02014-10-28 07:21:44 -07001810 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001811
robertphillipse5524cd2015-02-20 12:30:26 -08001812 SkIRect iBounds;
1813 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1814 return false;
1815 }
1816
1817 SkRect clipBounds = SkRect::Make(iBounds);
1818
1819 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1820
robertphillips60029a52015-11-09 13:51:06 -08001821 GrLayerHoister::Begin(fContext);
1822
robertphillipsfd61ed02014-10-28 07:21:44 -07001823 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001824 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001825 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001826 &atlasedNeedRendering, &atlasedRecycled,
robertphillipse5768742016-05-13 11:20:46 -07001827 fDrawContext->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001828
1829 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1830
1831 SkTDArray<GrHoistedLayer> needRendering, recycled;
1832
robertphillipse5524cd2015-02-20 12:30:26 -08001833 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1834
robertphillipsfd61ed02014-10-28 07:21:44 -07001835 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001836 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001837 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001838 &needRendering, &recycled,
robertphillipse5768742016-05-13 11:20:46 -07001839 fDrawContext->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001840
1841 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001842
robertphillips64bf7672014-08-21 13:07:35 -07001843 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001844 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
halcanary96fcdcc2015-08-27 07:41:13 -07001845 initialMatrix, nullptr);
robertphillips64bf7672014-08-21 13:07:35 -07001846
robertphillipsfd61ed02014-10-28 07:21:44 -07001847 GrLayerHoister::UnlockLayers(fContext, needRendering);
1848 GrLayerHoister::UnlockLayers(fContext, recycled);
1849 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1850 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips60029a52015-11-09 13:51:06 -08001851 GrLayerHoister::End(fContext);
robertphillips64bf7672014-08-21 13:07:35 -07001852
1853 return true;
robertphillips63242d72014-12-04 08:31:02 -08001854#else
1855 return false;
1856#endif
robertphillips64bf7672014-08-21 13:07:35 -07001857}
1858
senorblanco900c3672016-04-27 11:31:23 -07001859SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001860 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001861 // We always return a transient cache, so it is freed after each
1862 // filter traversal.
senorblanco900c3672016-04-27 11:31:23 -07001863 return SkImageFilterCache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001864}
reedf037e0b2014-10-30 11:34:15 -07001865
1866#endif