blob: 81e955d702a1b55e0e13e06fde8f0113e4bed03e [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,
brianosman982eb7f2016-06-06 13:10:58 -070083 SkSourceGammaTreatment gammaTreatment,
bsalomonbcf0a522014-10-08 08:40:09 -070084 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040085 SkASSERT(texture);
brianosman982eb7f2016-06-06 13:10:58 -070086 *texture = this->set(context, bitmap, params, gammaTreatment);
Brian Salomon9323b8b2014-10-07 15:07:38 -040087 }
88
bsalomonbcf0a522014-10-08 08:40:09 -070089 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040090 const SkBitmap& bitmap,
brianosman982eb7f2016-06-06 13:10:58 -070091 const GrTextureParams& params,
92 SkSourceGammaTreatment gammaTreatment) {
bsalomonbcf0a522014-10-08 08:40:09 -070093 // Either get the texture directly from the bitmap, or else use the cache and
94 // remember to unref it.
95 if (GrTexture* bmpTexture = bitmap.getTexture()) {
halcanary96fcdcc2015-08-27 07:41:13 -070096 fTexture.reset(nullptr);
bsalomonbcf0a522014-10-08 08:40:09 -070097 return bmpTexture;
98 } else {
brianosman982eb7f2016-06-06 13:10:58 -070099 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params, gammaTreatment));
bsalomonbcf0a522014-10-08 08:40:09 -0700100 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400101 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400102 }
103
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000104private:
bsalomonbcf0a522014-10-08 08:40:09 -0700105 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000106};
107
108///////////////////////////////////////////////////////////////////////////////
109
bsalomon74f681d2015-06-23 14:38:48 -0700110/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
111 should fail. */
112bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
113 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
114 *flags = 0;
115 if (info) {
116 switch (info->alphaType()) {
117 case kPremul_SkAlphaType:
118 break;
119 case kOpaque_SkAlphaType:
120 *flags |= SkGpuDevice::kIsOpaque_Flag;
121 break;
122 default: // If it is unpremul or unknown don't try to render
123 return false;
124 }
125 }
126 if (kClear_InitContents == init) {
127 *flags |= kNeedClear_Flag;
128 }
129 return true;
130}
131
robertphillips24e91282016-04-29 06:46:36 -0700132sk_sp<SkGpuDevice> SkGpuDevice::Make(sk_sp<GrRenderTarget> rt, const SkSurfaceProps* props,
133 InitContents init) {
robertphillipsca6eafc2016-05-17 09:57:46 -0700134 if (!rt || rt->wasDestroyed() || !rt->getContext()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700135 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000136 }
bsalomon74f681d2015-06-23 14:38:48 -0700137 unsigned flags;
halcanary96fcdcc2015-08-27 07:41:13 -0700138 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
139 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700140 }
robertphillipsca6eafc2016-05-17 09:57:46 -0700141
142 const int width = rt->width();
143 const int height = rt->height();
144
145 GrContext* context = rt->getContext();
146
147 sk_sp<GrDrawContext> drawContext(context->drawContext(std::move(rt), props));
148 return sk_sp<SkGpuDevice>(new SkGpuDevice(std::move(drawContext), width, height, flags));
149}
150
151sk_sp<SkBaseDevice> SkGpuDevice::Make(sk_sp<GrDrawContext> drawContext,
152 int width, int height,
153 InitContents init) {
154 if (!drawContext || drawContext->wasAbandoned()) {
155 return nullptr;
156 }
157 unsigned flags;
158 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
159 return nullptr;
160 }
161 return sk_sp<SkBaseDevice>(new SkGpuDevice(std::move(drawContext), width, height, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000162}
163
robertphillips24e91282016-04-29 06:46:36 -0700164sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
165 const SkImageInfo& info, int sampleCount,
166 const SkSurfaceProps* props, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700167 unsigned flags;
168 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700169 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700170 }
171
robertphillipsca6eafc2016-05-17 09:57:46 -0700172 sk_sp<GrDrawContext> drawContext(CreateDrawContext(context, budgeted, info,
173 sampleCount, props));
174 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700175 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700176 }
177
robertphillipsca6eafc2016-05-17 09:57:46 -0700178 return sk_sp<SkGpuDevice>(new SkGpuDevice(std::move(drawContext),
179 info.width(), info.height(), flags));
bsalomon74f681d2015-06-23 14:38:48 -0700180}
181
robertphillipsca6eafc2016-05-17 09:57:46 -0700182SkGpuDevice::SkGpuDevice(sk_sp<GrDrawContext> drawContext, int width, int height, unsigned flags)
183 : INHERITED(drawContext->surfaceProps())
184 , fContext(SkRef(drawContext->accessRenderTarget()->getContext()))
185 , fRenderTarget(drawContext->renderTarget())
186 , fDrawContext(std::move(drawContext)) {
bsalomon74f681d2015-06-23 14:38:48 -0700187 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000188
bsalomon74f681d2015-06-23 14:38:48 -0700189 SkAlphaType at = fOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
robertphillipsca6eafc2016-05-17 09:57:46 -0700190 SkImageInfo info = fRenderTarget->surfacePriv().info(at).makeWH(width, height);
191 SkPixelRef* pr = new SkGrPixelRef(info, fRenderTarget.get());
bsalomonafbf2d62014-09-30 12:18:44 -0700192 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700193 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700194
bsalomone63ffef2016-02-05 07:17:34 -0800195 if (flags & kNeedClear_Flag) {
196 this->clearAll();
197 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000198}
199
robertphillipsca6eafc2016-05-17 09:57:46 -0700200sk_sp<GrDrawContext> SkGpuDevice::CreateDrawContext(GrContext* context,
201 SkBudgeted budgeted,
202 const SkImageInfo& origInfo,
203 int sampleCount,
204 const SkSurfaceProps* surfaceProps) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000205 if (kUnknown_SkColorType == origInfo.colorType() ||
206 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700207 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000208 }
209
bsalomonafe30052015-01-16 07:32:33 -0800210 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700211 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800212 }
213
reede5ea5002014-09-03 11:54:58 -0700214 SkColorType ct = origInfo.colorType();
215 SkAlphaType at = origInfo.alphaType();
brianosmana6359362016-03-21 06:55:37 -0700216 SkColorProfileType pt = origInfo.profileType();
brianosman6b086522016-04-14 12:39:00 -0700217 if (kRGB_565_SkColorType == ct || kGray_8_SkColorType == ct) {
reede5ea5002014-09-03 11:54:58 -0700218 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800219 }
220 if (kOpaque_SkAlphaType != at) {
221 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000222 }
brianosman6b086522016-04-14 12:39:00 -0700223
224 GrPixelConfig origConfig = SkImageInfo2GrPixelConfig(ct, at, pt, *context->caps());
225 if (!context->caps()->isConfigRenderable(origConfig, sampleCount > 0)) {
226 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
227 ct = kN32_SkColorType;
228 }
229
robertphillipsca6eafc2016-05-17 09:57:46 -0700230 GrPixelConfig config = SkImageInfo2GrPixelConfig(ct, at, pt, *context->caps());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000231
robertphillipsca6eafc2016-05-17 09:57:46 -0700232 return context->newDrawContext(SkBackingFit::kExact, // Why exact?
233 origInfo.width(), origInfo.height(),
234 config, sampleCount,
235 kDefault_GrSurfaceOrigin, surfaceProps, budgeted);
236
kkinnunenabcfab42015-02-22 22:53:44 -0800237}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000238
robertphillips1579e3c2016-03-24 05:01:23 -0700239// This method ensures that we always have a texture-backed "bitmap" when we finally
240// call through to the base impl so that the image filtering code will take the
241// gpu-specific paths. This mirrors SkCanvas::internalDrawDevice (the other
242// use of SkImageFilter::filterImage) in that the source and dest will have
243// homogenous backing (e.g., raster or gpu).
robertphillips2302de92016-03-24 07:26:32 -0700244void SkGpuDevice::drawSpriteWithFilter(const SkDraw& draw, const SkBitmap& bitmap,
245 int x, int y, const SkPaint& paint) {
robertphillips090b7622016-03-28 11:07:43 -0700246 ASSERT_SINGLE_OWNER
247 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpriteWithFilter", fContext);
248
249 if (fContext->abandoned()) {
250 return;
251 }
252
robertphillips1579e3c2016-03-24 05:01:23 -0700253 if (bitmap.getTexture()) {
robertphillips2302de92016-03-24 07:26:32 -0700254 INHERITED::drawSpriteWithFilter(draw, bitmap, x, y, paint);
robertphillips1579e3c2016-03-24 05:01:23 -0700255 return;
256 }
257
258 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
259 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
260 return;
261 }
262
263 GrTexture* texture;
264 // draw sprite neither filters nor tiles.
brianosman982eb7f2016-06-06 13:10:58 -0700265 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(),
266 SkSourceGammaTreatment::kRespect, &texture);
robertphillips1579e3c2016-03-24 05:01:23 -0700267 if (!texture) {
268 return;
269 }
270
271 SkBitmap newBitmap;
272
273 GrWrapTextureInBitmap(texture, texture->width(), texture->height(),
274 bitmap.isOpaque(), &newBitmap);
275
robertphillips2302de92016-03-24 07:26:32 -0700276 INHERITED::drawSpriteWithFilter(draw, newBitmap, x, y, paint);
robertphillips1579e3c2016-03-24 05:01:23 -0700277}
278
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000279///////////////////////////////////////////////////////////////////////////////
280
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000281bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
282 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800283 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000284
285 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700286 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps());
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000287 if (kUnknown_GrPixelConfig == config) {
288 return false;
289 }
290
291 uint32_t flags = 0;
292 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
293 flags = GrContext::kUnpremul_PixelOpsFlag;
294 }
bsalomon74f681d2015-06-23 14:38:48 -0700295 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
296 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000297}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000298
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000299bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
300 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800301 ASSERT_SINGLE_OWNER
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000302 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700303 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fContext->caps());
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000304 if (kUnknown_GrPixelConfig == config) {
305 return false;
306 }
307 uint32_t flags = 0;
308 if (kUnpremul_SkAlphaType == info.alphaType()) {
309 flags = GrContext::kUnpremul_PixelOpsFlag;
310 }
311 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
312
313 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700314 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000315
316 return true;
317}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000318
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000319const SkBitmap& SkGpuDevice::onAccessBitmap() {
joshualittce894002016-01-11 13:29:31 -0800320 ASSERT_SINGLE_OWNER
reed89443ab2014-06-27 11:34:19 -0700321 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000322}
323
reed41e010c2015-06-09 12:16:53 -0700324bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800325 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700326 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
327 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
328 // ... ugh.
329 fLegacyBitmap.notifyPixelsChanged();
330 return false;
331}
332
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000333void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
joshualittce894002016-01-11 13:29:31 -0800334 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000335 INHERITED::onAttachToCanvas(canvas);
336
337 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800338 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000339}
340
341void SkGpuDevice::onDetachFromCanvas() {
joshualittce894002016-01-11 13:29:31 -0800342 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000343 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800344 fClip.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700345 fClipStack.reset(nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000346}
347
348// call this every draw call, to ensure that the context reflects our state,
349// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800350void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800351 ASSERT_SINGLE_OWNER
joshualitt44701df2015-02-23 14:44:57 -0800352 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000353
joshualitt44701df2015-02-23 14:44:57 -0800354 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000355
cdalton846c0512016-05-13 10:25:00 -0700356 fClip.reset(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000357}
358
359GrRenderTarget* SkGpuDevice::accessRenderTarget() {
joshualittce894002016-01-11 13:29:31 -0800360 ASSERT_SINGLE_OWNER
robertphillipsca6eafc2016-05-17 09:57:46 -0700361 return fRenderTarget.get();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000362}
363
robertphillips175dd9b2016-04-28 14:32:04 -0700364GrDrawContext* SkGpuDevice::accessDrawContext() {
365 ASSERT_SINGLE_OWNER
366 return fDrawContext.get();
367}
368
reed8eddfb52014-12-04 07:50:14 -0800369void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800370 ASSERT_SINGLE_OWNER
reed8eddfb52014-12-04 07:50:14 -0800371 GrColor color = 0;
joshualitt5651ee62016-01-11 10:39:11 -0800372 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext);
reed8eddfb52014-12-04 07:50:14 -0800373 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700374 fDrawContext->clear(&rect, color, true);
reed8eddfb52014-12-04 07:50:14 -0800375}
376
robertphillipsca6eafc2016-05-17 09:57:46 -0700377void SkGpuDevice::replaceDrawContext(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800378 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800379
bsalomon5ec26ae2016-02-25 08:33:02 -0800380 SkBudgeted budgeted = fRenderTarget->resourcePriv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800381
robertphillipsca6eafc2016-05-17 09:57:46 -0700382 sk_sp<GrDrawContext> newDC(CreateDrawContext(this->context(),
383 budgeted,
384 this->imageInfo(),
385 fDrawContext->numColorSamples(),
386 &this->surfaceProps()));
387 if (!newDC) {
kkinnunenabcfab42015-02-22 22:53:44 -0800388 return;
389 }
390
391 if (shouldRetainContent) {
392 if (fRenderTarget->wasDestroyed()) {
393 return;
394 }
robertphillipsca6eafc2016-05-17 09:57:46 -0700395 newDC->copySurface(fDrawContext->asTexture().get(),
396 SkIRect::MakeWH(this->width(), this->height()),
397 SkIPoint::Make(0, 0));
kkinnunenabcfab42015-02-22 22:53:44 -0800398 }
399
robertphillipsca6eafc2016-05-17 09:57:46 -0700400 SkASSERT(fDrawContext->accessRenderTarget() != newDC->accessRenderTarget());
kkinnunenabcfab42015-02-22 22:53:44 -0800401
robertphillipsca6eafc2016-05-17 09:57:46 -0700402 fRenderTarget = newDC->renderTarget();
kkinnunenabcfab42015-02-22 22:53:44 -0800403
bsalomon74f681d2015-06-23 14:38:48 -0700404#ifdef SK_DEBUG
405 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
406 kPremul_SkAlphaType);
407 SkASSERT(info == fLegacyBitmap.info());
408#endif
robertphillipsca6eafc2016-05-17 09:57:46 -0700409 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget.get());
kkinnunenabcfab42015-02-22 22:53:44 -0800410 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700411
robertphillipsca6eafc2016-05-17 09:57:46 -0700412 fDrawContext = newDC;
kkinnunenabcfab42015-02-22 22:53:44 -0800413}
414
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000415///////////////////////////////////////////////////////////////////////////////
416
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000417void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800418 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800419 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800420 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000421
422 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700423 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700424 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700425 return;
426 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000427
robertphillips2e1e51f2015-10-15 08:01:48 -0700428 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000429}
430
431// must be in SkCanvas::PointMode order
432static const GrPrimitiveType gPointMode2PrimtiveType[] = {
433 kPoints_GrPrimitiveType,
434 kLines_GrPrimitiveType,
435 kLineStrip_GrPrimitiveType
436};
437
ethannicholas330bb952015-07-17 06:44:02 -0700438// suppress antialiasing on axis-aligned integer-coordinate lines
439static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
440 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
441 return false;
442 }
443 if (count == 2) {
444 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
445 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800446 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700447 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800448 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700449 // the two end pixels as being the lesser of two evils.
450 if (pts[0].fX == pts[1].fX) {
451 return ((int) pts[0].fX) != pts[0].fX;
452 }
453 if (pts[0].fY == pts[1].fY) {
454 return ((int) pts[0].fY) != pts[0].fY;
455 }
456 }
457 return true;
458}
459
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000460void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
461 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800462 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800463 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800464 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000465
466 SkScalar width = paint.getStrokeWidth();
467 if (width < 0) {
468 return;
469 }
470
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000471 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
bsalomon6663acf2016-05-10 09:14:17 -0700472 GrStyle style(paint, SkPaint::kStroke_Style);
egdaniele61c4112014-06-12 10:24:21 -0700473 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700474 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700475 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700476 return;
477 }
egdaniele61c4112014-06-12 10:24:21 -0700478 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700479 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700480 path.moveTo(pts[0]);
481 path.lineTo(pts[1]);
bsalomon6663acf2016-05-10 09:14:17 -0700482 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, style);
egdaniele61c4112014-06-12 10:24:21 -0700483 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000484 }
485
ethannicholas330bb952015-07-17 06:44:02 -0700486 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000487 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800488 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700489 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000490 draw.drawPoints(mode, count, pts, paint, true);
491 return;
492 }
493
494 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700495 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700496 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700497 return;
498 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000499
robertphillips2e1e51f2015-10-15 08:01:48 -0700500 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700501 grPaint,
502 *draw.fMatrix,
503 gPointMode2PrimtiveType[mode],
504 SkToS32(count),
505 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700506 nullptr,
507 nullptr,
508 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700509 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000510}
511
512///////////////////////////////////////////////////////////////////////////////
513
robertphillipsff55b492015-11-24 07:56:59 -0800514void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800515 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800516 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800517 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000518
519 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
520 SkScalar width = paint.getStrokeWidth();
521
522 /*
523 We have special code for hairline strokes, miter-strokes, bevel-stroke
524 and fills. Anything else we just call our path code.
525 */
526 bool usePath = doStroke && width > 0 &&
527 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
528 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
egdanield58a0ba2014-06-11 10:30:05 -0700529
robertphillipsd8aa59d2015-08-05 09:07:12 -0700530 // a few other reasons we might need to call drawPath...
robertphillipsff55b492015-11-24 07:56:59 -0800531 if (paint.getMaskFilter() || paint.getPathEffect() ||
robertphillipsd8aa59d2015-08-05 09:07:12 -0700532 paint.getStyle() == SkPaint::kStrokeAndFill_Style) { // we can't both stroke and fill rects
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000533 usePath = true;
534 }
egdanield58a0ba2014-06-11 10:30:05 -0700535
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000536 if (usePath) {
537 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700538 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000539 path.addRect(rect);
robertphillips6c7e3252016-04-27 10:47:51 -0700540 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800541 fClip, path, paint,
542 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700543 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000544 return;
545 }
546
547 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700548 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700549 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700550 return;
551 }
Mike Klein744fb732014-06-23 15:13:26 -0400552
bsalomon6663acf2016-05-10 09:14:17 -0700553 GrStyle style(paint);
554 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000555}
556
557///////////////////////////////////////////////////////////////////////////////
558
559void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800560 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800561 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800562 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800563 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000564
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000565 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700566 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700567 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700568 return;
569 }
Mike Klein744fb732014-06-23 15:13:26 -0400570
bsalomon6663acf2016-05-10 09:14:17 -0700571 GrStyle style(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000572 if (paint.getMaskFilter()) {
573 // try to hit the fast path for drawing filtered round rects
574
575 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800576 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000577 if (devRRect.allCornersCircular()) {
578 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700579 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
reed1e7f5e72016-04-27 07:49:17 -0700580 draw.fRC->getBounds(),
joshualitt5531d512014-12-17 15:50:11 -0800581 *draw.fMatrix,
582 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000583 SkIRect finalIRect;
584 maskRect.roundOut(&finalIRect);
reed1e7f5e72016-04-27 07:49:17 -0700585 if (draw.fRC->quickReject(finalIRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000586 // clipped out
587 return;
588 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700589 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
robertphillips6c7e3252016-04-27 10:47:51 -0700590 fDrawContext.get(),
joshualitt25d9c152015-02-18 12:29:52 -0800591 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800592 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800593 *draw.fMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700594 style.strokeRec(),
egdanield58a0ba2014-06-11 10:30:05 -0700595 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000596 return;
597 }
598 }
599
600 }
601 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000602 }
603
bsalomon6663acf2016-05-10 09:14:17 -0700604 if (paint.getMaskFilter() || style.pathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800605 // The only mask filter the native rrect drawing code could've handle was taken
606 // care of above.
607 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000608 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700609 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000610 path.addRRect(rect);
robertphillips6c7e3252016-04-27 10:47:51 -0700611 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800612 fClip, path, paint,
613 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700614 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000615 return;
616 }
Mike Klein744fb732014-06-23 15:13:26 -0400617
bsalomon6663acf2016-05-10 09:14:17 -0700618 SkASSERT(!style.pathEffect());
robertphillips514450c2015-11-24 05:36:02 -0800619
bsalomon6663acf2016-05-10 09:14:17 -0700620 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000621}
622
robertphillipsd7706102016-02-25 09:28:08 -0800623
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000624void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800625 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800626 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800627 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800628 CHECK_SHOULD_DRAW(draw);
629
robertphillipsd7706102016-02-25 09:28:08 -0800630 if (outer.isEmpty()) {
631 return;
632 }
633
634 if (inner.isEmpty()) {
635 return this->drawRRect(draw, outer, paint);
636 }
637
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000638 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000639
robertphillips0e7029e2015-11-30 05:45:06 -0800640 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800641 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700642 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700643 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700644 return;
645 }
robertphillips00095892016-02-29 13:50:40 -0800646
647 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
648 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000649 }
650
651 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700652 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000653 path.addRRect(outer);
654 path.addRRect(inner);
655 path.setFillType(SkPath::kEvenOdd_FillType);
656
robertphillips6c7e3252016-04-27 10:47:51 -0700657 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillips0e7029e2015-11-30 05:45:06 -0800658 fClip, path, paint,
659 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700660 draw.fRC->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000661}
662
663
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000664/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000665
robertphillips514450c2015-11-24 05:36:02 -0800666void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800667 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800668 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800669 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000670
robertphillips514450c2015-11-24 05:36:02 -0800671 // Presumably the path effect warps this to something other than an oval
672 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000673 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700674 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000675 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700676 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000677 return;
herb11a7f7f2015-11-24 12:41:00 -0800678 }
679
robertphillips514450c2015-11-24 05:36:02 -0800680 if (paint.getMaskFilter()) {
681 // The RRect path can handle special case blurring
682 SkRRect rr = SkRRect::MakeOval(oval);
683 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000684 }
685
686 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700687 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700688 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700689 return;
690 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000691
bsalomon6663acf2016-05-10 09:14:17 -0700692 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, GrStyle(paint));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000693}
694
695#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000696
697///////////////////////////////////////////////////////////////////////////////
robertphillips0851d2d2016-06-02 05:21:34 -0700698void SkGpuDevice::drawStrokedLine(const SkPoint points[2],
699 const SkDraw& draw,
700 const SkPaint& origPaint) {
701 ASSERT_SINGLE_OWNER
702 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawStrokedLine", fContext);
703 CHECK_SHOULD_DRAW(draw);
704
705 // Adding support for round capping would require a GrDrawContext::fillRRectWithLocalMatrix
706 // entry point
707 SkASSERT(SkPaint::kRound_Cap != origPaint.getStrokeCap());
708 SkASSERT(SkPaint::kStroke_Style == origPaint.getStyle());
709 SkASSERT(!origPaint.getPathEffect());
710 SkASSERT(!origPaint.getMaskFilter());
711
712 const SkScalar halfWidth = 0.5f * origPaint.getStrokeWidth();
713 SkASSERT(halfWidth > 0);
714
715 SkVector v = points[1] - points[0];
716
717 SkScalar length = SkPoint::Normalize(&v);
718 if (!length) {
719 v.fX = 1.0f;
720 v.fY = 0.0f;
721 }
722
723 SkPaint newPaint(origPaint);
724 newPaint.setStyle(SkPaint::kFill_Style);
725
726 SkScalar xtraLength = 0.0f;
727 if (SkPaint::kButt_Cap != origPaint.getStrokeCap()) {
728 xtraLength = halfWidth;
729 }
730
731 SkPoint mid = points[0] + points[1];
732 mid.scale(0.5f);
733
734 SkRect rect = SkRect::MakeLTRB(mid.fX-halfWidth, mid.fY - 0.5f*length - xtraLength,
735 mid.fX+halfWidth, mid.fY + 0.5f*length + xtraLength);
736 SkMatrix m;
737 m.setSinCos(v.fX, -v.fY, mid.fX, mid.fY);
738
739 SkMatrix local = m;
740
741 m.postConcat(*draw.fMatrix);
742
743 GrPaint grPaint;
744 if (!SkPaintToGrPaint(this->context(), newPaint, m,
745 this->surfaceProps().isGammaCorrect(), &grPaint)) {
746 return;
747 }
748
749 fDrawContext->fillRectWithLocalMatrix(fClip, grPaint, m, rect, local);
750}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000751
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000752void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
753 const SkPaint& paint, const SkMatrix* prePathMatrix,
754 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800755 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800756 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
robertphillips0851d2d2016-06-02 05:21:34 -0700757 SkPoint points[2];
758 if (SkPaint::kStroke_Style == paint.getStyle() && paint.getStrokeWidth() > 0 &&
759 !paint.getMaskFilter() && SkPaint::kRound_Cap != paint.getStrokeCap() &&
760 draw.fMatrix->preservesRightAngles() && origSrcPath.isLine(points)) {
761 // Path-based stroking looks better for thin rects
762 SkScalar strokeWidth = draw.fMatrix->getMaxScale() * paint.getStrokeWidth();
robertphillipsf2204c92016-06-02 10:57:59 -0700763 if (strokeWidth >= 1.0f) {
robertphillips0851d2d2016-06-02 05:21:34 -0700764 // Round capping support is currently disabled b.c. it would require
765 // a RRect batch that takes a localMatrix.
766 this->drawStrokedLine(points, draw, paint);
767 return;
768 }
769 }
robertphillipsff55b492015-11-24 07:56:59 -0800770 bool isClosed;
771 SkRect rect;
772 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
773 this->drawRect(draw, rect, paint);
774 return;
775 }
776 if (origSrcPath.isOval(&rect)) {
777 this->drawOval(draw, rect, paint);
778 return;
779 }
780 SkRRect rrect;
781 if (origSrcPath.isRRect(&rrect)) {
782 this->drawRRect(draw, rrect, paint);
783 return;
784 }
785 }
786
joshualitt5531d512014-12-17 15:50:11 -0800787 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800788 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000789
robertphillips6c7e3252016-04-27 10:47:51 -0700790 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsccb1b572015-05-27 11:02:55 -0700791 fClip, origSrcPath, paint,
792 *draw.fMatrix, prePathMatrix,
reed1e7f5e72016-04-27 07:49:17 -0700793 draw.fRC->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000794}
795
796static const int kBmpSmallTileSize = 1 << 10;
797
798static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
799 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
800 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
801 return tilesX * tilesY;
802}
803
reed85d91782015-09-10 14:33:38 -0700804static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000805 if (maxTileSize <= kBmpSmallTileSize) {
806 return maxTileSize;
807 }
808
809 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
810 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
811
812 maxTileTotalTileSize *= maxTileSize * maxTileSize;
813 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
814
815 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
816 return kBmpSmallTileSize;
817 } else {
818 return maxTileSize;
819 }
820}
821
822// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
823// pixels from the bitmap are necessary.
robertphillipse5768742016-05-13 11:20:46 -0700824static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800825 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800826 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700827 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000828 const SkRect* srcRectPtr,
829 SkIRect* clippedSrcIRect) {
robertphillipse5768742016-05-13 11:20:46 -0700830 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000831 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800832 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000833 clippedSrcIRect->setEmpty();
834 return;
835 }
836 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
837 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700838 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000839 // we've setup src space 0,0 to map to the top left of the src rect.
840 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000841 if (!clippedSrcRect.intersect(*srcRectPtr)) {
842 clippedSrcIRect->setEmpty();
843 return;
844 }
845 }
846 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700847 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000848 if (!clippedSrcIRect->intersect(bmpBounds)) {
849 clippedSrcIRect->setEmpty();
850 }
851}
852
reed85d91782015-09-10 14:33:38 -0700853bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
854 const SkMatrix& viewMatrix,
855 const GrTextureParams& params,
856 const SkRect* srcRectPtr,
857 int maxTileSize,
858 int* tileSize,
859 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800860 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700861 // if it's larger than the max tile size, then we have no choice but tiling.
862 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
robertphillipse5768742016-05-13 11:20:46 -0700863 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(),
864 fClip, viewMatrix, imageRect.size(),
reed85d91782015-09-10 14:33:38 -0700865 srcRectPtr, clippedSubset);
866 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
867 return true;
868 }
869
bsalomon1a1d0b82015-10-16 07:49:42 -0700870 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700871 const size_t area = imageRect.width() * imageRect.height();
872 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
873 return false;
874 }
875
reed85d91782015-09-10 14:33:38 -0700876 // At this point we know we could do the draw by uploading the entire bitmap
877 // as a texture. However, if the texture would be large compared to the
878 // cache size and we don't require most of it for this draw then tile to
879 // reduce the amount of upload and cache spill.
880
881 // assumption here is that sw bitmap size is a good proxy for its size as
882 // a texture
883 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
884 size_t cacheSize;
885 fContext->getResourceCacheLimits(nullptr, &cacheSize);
886 if (bmpSize < cacheSize / 2) {
887 return false;
888 }
889
bsalomon1a1d0b82015-10-16 07:49:42 -0700890 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
891 // tiling memory savings would be < 50%.
robertphillipse5768742016-05-13 11:20:46 -0700892 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(),
893 fClip, viewMatrix, imageRect.size(), srcRectPtr,
reed85d91782015-09-10 14:33:38 -0700894 clippedSubset);
895 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
896 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
897 kBmpSmallTileSize * kBmpSmallTileSize;
898
899 return usedTileBytes < 2 * bmpSize;
900}
901
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000902bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800903 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000904 const GrTextureParams& params,
905 const SkRect* srcRectPtr,
906 int maxTileSize,
907 int* tileSize,
908 SkIRect* clippedSrcRect) const {
joshualittce894002016-01-11 13:29:31 -0800909 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000910 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700911 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000912 return false;
913 }
914
reed85d91782015-09-10 14:33:38 -0700915 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
916 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
917}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000918
reed85d91782015-09-10 14:33:38 -0700919bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
920 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
921 const SkMatrix& viewMatrix) const {
joshualittce894002016-01-11 13:29:31 -0800922 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700923 // if image is explictly texture backed then just use the texture
924 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000925 return false;
926 }
927
reed85d91782015-09-10 14:33:38 -0700928 GrTextureParams params;
929 bool doBicubic;
930 GrTextureParams::FilterMode textureFilterMode =
931 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
932
933 int tileFilterPad;
934 if (doBicubic) {
935 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
936 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
937 tileFilterPad = 0;
938 } else {
939 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000940 }
reed85d91782015-09-10 14:33:38 -0700941 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000942
bsalomon8c07b7a2015-11-02 11:36:52 -0800943 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000944
reed85d91782015-09-10 14:33:38 -0700945 // these are output, which we safely ignore, as we just want to know the predicate
946 int outTileSize;
947 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000948
reed85d91782015-09-10 14:33:38 -0700949 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
950 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000951}
952
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000953void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000954 const SkBitmap& bitmap,
955 const SkMatrix& m,
956 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800957 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800958 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800959 SkMatrix viewMatrix;
960 viewMatrix.setConcat(*origDraw.fMatrix, m);
961 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800962 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonb1b01992015-11-18 10:56:08 -0800963 // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
bsalomonf1ecd212015-12-09 17:06:02 -0800964 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
965 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800966 return;
967 }
bsalomonb1b01992015-11-18 10:56:08 -0800968 int maxTileSize = fContext->caps()->maxTileSize();
969
970 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
971 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
robertphillips071457b2016-05-16 09:50:03 -0700972 bool drawAA = !fDrawContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -0800973 paint.isAntiAlias() &&
974 bitmap.width() <= maxTileSize &&
975 bitmap.height() <= maxTileSize;
976
977 bool skipTileCheck = drawAA || paint.getMaskFilter();
978
979 if (!skipTileCheck) {
980 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
981 int tileSize;
982 SkIRect clippedSrcRect;
983
984 GrTextureParams params;
985 bool doBicubic;
986 GrTextureParams::FilterMode textureFilterMode =
987 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
988 &doBicubic);
989
990 int tileFilterPad;
991
992 if (doBicubic) {
993 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
994 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
995 tileFilterPad = 0;
996 } else {
997 tileFilterPad = 1;
998 }
999 params.setFilterMode(textureFilterMode);
1000
1001 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1002 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
1003 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1004 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
1005 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
1006 return;
1007 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001008 }
bsalomonb1b01992015-11-18 10:56:08 -08001009 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001010 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
1011 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001012}
1013
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001014// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001015// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1016// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001017static inline void clamped_outset_with_offset(SkIRect* iRect,
1018 int outset,
1019 SkPoint* offset,
1020 const SkIRect& clamp) {
1021 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001022
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001023 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1024 if (leftClampDelta > 0) {
1025 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001026 iRect->fLeft = clamp.fLeft;
1027 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001028 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001029 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001030
1031 int topClampDelta = clamp.fTop - iRect->fTop;
1032 if (topClampDelta > 0) {
1033 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001034 iRect->fTop = clamp.fTop;
1035 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001036 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001037 }
1038
1039 if (iRect->fRight > clamp.fRight) {
1040 iRect->fRight = clamp.fRight;
1041 }
1042 if (iRect->fBottom > clamp.fBottom) {
1043 iRect->fBottom = clamp.fBottom;
1044 }
1045}
1046
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001047// Break 'bitmap' into several tiles to draw it since it has already
1048// been determined to be too large to fit in VRAM
1049void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001050 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001051 const SkRect& srcRect,
1052 const SkIRect& clippedSrcIRect,
1053 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -08001054 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -07001055 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001056 int tileSize,
1057 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -08001058 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -08001059
ericrk983294f2016-04-18 09:14:00 -07001060 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -08001061 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -07001062 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -08001063
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001064 // The following pixel lock is technically redundant, but it is desirable
1065 // to lock outside of the tile loop to prevent redecoding the whole image
1066 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1067 // is larger than the limit of the discardable memory pool.
1068 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -08001069
1070 const SkPaint* paint = &origPaint;
1071 SkPaint tempPaint;
robertphillips071457b2016-05-16 09:50:03 -07001072 if (origPaint.isAntiAlias() && !fDrawContext->isUnifiedMultisampled()) {
bsalomonc55271f2015-11-09 11:55:57 -08001073 // Drop antialiasing to avoid seams at tile boundaries.
1074 tempPaint = origPaint;
1075 tempPaint.setAntiAlias(false);
1076 paint = &tempPaint;
1077 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001078 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1079
1080 int nx = bitmap.width() / tileSize;
1081 int ny = bitmap.height() / tileSize;
1082 for (int x = 0; x <= nx; x++) {
1083 for (int y = 0; y <= ny; y++) {
1084 SkRect tileR;
1085 tileR.set(SkIntToScalar(x * tileSize),
1086 SkIntToScalar(y * tileSize),
1087 SkIntToScalar((x + 1) * tileSize),
1088 SkIntToScalar((y + 1) * tileSize));
1089
1090 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1091 continue;
1092 }
1093
1094 if (!tileR.intersect(srcRect)) {
1095 continue;
1096 }
1097
1098 SkBitmap tmpB;
1099 SkIRect iTileR;
1100 tileR.roundOut(&iTileR);
1101 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1102 SkIntToScalar(iTileR.fTop));
1103
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001104 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001105 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001106 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001107 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001108 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001109
robertphillipsec8bb942014-11-21 10:16:25 -08001110 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001111 SkIRect iClampRect;
1112
reeda5517e22015-07-14 10:54:12 -07001113 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001114 // In bleed mode we want to always expand the tile on all edges
1115 // but stay within the bitmap bounds
1116 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1117 } else {
1118 // In texture-domain/clamp mode we only want to expand the
1119 // tile on edges interior to "srcRect" (i.e., we want to
1120 // not bleed across the original clamped edges)
1121 srcRect.roundOut(&iClampRect);
1122 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001123 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1124 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001125 }
1126
1127 if (bitmap.extractSubset(&tmpB, iTileR)) {
1128 // now offset it to make it "local" to our tmp bitmap
1129 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001130 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001131 // de-optimized this determination
1132 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001133 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001134 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001135 tileR,
1136 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -08001137 *paint,
reeda5517e22015-07-14 10:54:12 -07001138 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001139 bicubic,
1140 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001141 }
1142 }
1143 }
1144}
1145
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001146/*
1147 * This is called by drawBitmap(), which has to handle images that may be too
1148 * large to be represented by a single texture.
1149 *
1150 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1151 * and that non-texture portion of the GrPaint has already been setup.
1152 */
1153void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001154 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001155 const SkRect& srcRect,
1156 const GrTextureParams& params,
1157 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001158 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001159 bool bicubic,
1160 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001161 // We should have already handled bitmaps larger than the max texture size.
1162 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1163 bitmap.height() <= fContext->caps()->maxTextureSize());
1164 // Unless the bitmap is inherently texture-backed, we should be respecting the max tile size
1165 // by the time we get here.
1166 SkASSERT(bitmap.getTexture() ||
1167 (bitmap.width() <= fContext->caps()->maxTileSize() &&
1168 bitmap.height() <= fContext->caps()->maxTileSize()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001169
1170 GrTexture* texture;
brianosman982eb7f2016-06-06 13:10:58 -07001171 SkSourceGammaTreatment gammaTreatment = this->surfaceProps().isGammaCorrect()
1172 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIgnore;
1173 AutoBitmapTexture abt(fContext, bitmap, params, gammaTreatment, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001174 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001175 return;
1176 }
1177
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001178 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001179 SkRect paintRect;
1180 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1181 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1182 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1183 SkScalarMul(srcRect.fTop, hInv),
1184 SkScalarMul(srcRect.fRight, wInv),
1185 SkScalarMul(srcRect.fBottom, hInv));
1186
egdaniel79da63f2015-10-09 10:55:16 -07001187 SkMatrix texMatrix;
1188 texMatrix.reset();
1189 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1190 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1191 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1192 // Thus we need to pass in the transform matrix directly to the texture processor used for
1193 // the bitmap draw.
1194 texMatrix.setScale(wInv, hInv);
1195 }
1196
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001197 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001198
1199 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1200 // the rest from the SkPaint.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001201 SkAutoTUnref<const GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001202
reeda5517e22015-07-14 10:54:12 -07001203 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001204 // Use a constrained texture domain to avoid color bleeding
1205 SkScalar left, top, right, bottom;
1206 if (srcRect.width() > SK_Scalar1) {
1207 SkScalar border = SK_ScalarHalf / texture->width();
1208 left = paintRect.left() + border;
1209 right = paintRect.right() - border;
1210 } else {
1211 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1212 }
1213 if (srcRect.height() > SK_Scalar1) {
1214 SkScalar border = SK_ScalarHalf / texture->height();
1215 top = paintRect.top() + border;
1216 bottom = paintRect.bottom() - border;
1217 } else {
1218 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1219 }
1220 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001221 if (bicubic) {
egdaniel79da63f2015-10-09 10:55:16 -07001222 fp.reset(GrBicubicEffect::Create(texture, texMatrix, textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001223 } else {
bsalomon4a339522015-10-06 08:40:50 -07001224 fp.reset(GrTextureDomainEffect::Create(texture,
egdaniel79da63f2015-10-09 10:55:16 -07001225 texMatrix,
joshualitt5531d512014-12-17 15:50:11 -08001226 textureDomain,
1227 GrTextureDomain::kClamp_Mode,
1228 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001229 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001230 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001231 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1232 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
egdaniel79da63f2015-10-09 10:55:16 -07001233 fp.reset(GrBicubicEffect::Create(texture, texMatrix, tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001234 } else {
egdaniel79da63f2015-10-09 10:55:16 -07001235 fp.reset(GrSimpleTextureEffect::Create(texture, texMatrix, params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001236 }
1237
joshualitt33a5fce2015-11-18 13:28:51 -08001238 GrPaint grPaint;
1239 if (!SkPaintToGrPaintWithTexture(this->context(), paint, viewMatrix, fp,
brianosman898235c2016-04-06 07:38:23 -07001240 kAlpha_8_SkColorType == bitmap.colorType(),
brianosmanb461d342016-04-13 13:10:14 -07001241 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001242 return;
1243 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001244
egdaniel79da63f2015-10-09 10:55:16 -07001245 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1246 // We don't have local coords in this case and have previously set the transform
1247 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001248 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001249 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001250 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001251 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001252}
1253
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001254void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1255 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001256 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001257 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001258 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001259
1260 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1261 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1262 return;
1263 }
1264
robertphillipse1849d12016-03-17 08:26:38 -07001265 int offX = bitmap.pixelRefOrigin().fX;
1266 int offY = bitmap.pixelRefOrigin().fY;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001267 int w = bitmap.width();
1268 int h = bitmap.height();
1269
1270 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001271 // draw sprite neither filters nor tiles.
brianosman982eb7f2016-06-06 13:10:58 -07001272 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(),
1273 SkSourceGammaTreatment::kRespect, &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001274 if (!texture) {
1275 return;
1276 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001277
bsalomonf1b7a1d2015-09-28 06:26:28 -07001278 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
1279
senorblancof35566e2016-04-18 10:32:02 -07001280 SkASSERT(!paint.getImageFilter());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001281
1282 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001283 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001284 GrSimpleTextureEffect::Create(texture, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001285 if (alphaOnly) {
1286 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1287 } else {
1288 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1289 }
brianosman898235c2016-04-06 07:38:23 -07001290 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp,
brianosmanb461d342016-04-13 13:10:14 -07001291 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001292 return;
1293 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001294
bsalomona2e69fc2015-11-05 10:41:43 -08001295 fDrawContext->fillRectToRect(fClip,
1296 grPaint,
1297 SkMatrix::I(),
1298 SkRect::MakeXYWH(SkIntToScalar(left),
1299 SkIntToScalar(top),
1300 SkIntToScalar(w),
1301 SkIntToScalar(h)),
robertphillipse1849d12016-03-17 08:26:38 -07001302 SkRect::MakeXYWH(SkIntToScalar(offX) / texture->width(),
1303 SkIntToScalar(offY) / texture->height(),
1304 SkIntToScalar(w) / texture->width(),
1305 SkIntToScalar(h) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001306}
1307
bsalomonb1b01992015-11-18 10:56:08 -08001308void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1309 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001310 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001311 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001312 CHECK_SHOULD_DRAW(draw);
bsalomonb1b01992015-11-18 10:56:08 -08001313 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001314 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001315 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
1316 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001317 return;
1318 }
bsalomonb1b01992015-11-18 10:56:08 -08001319 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1320 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1321 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1322 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1323 const SkRect* dst = &origDst;
1324 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001325 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001326 if (!src) {
1327 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001328 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001329
bsalomonb1b01992015-11-18 10:56:08 -08001330 SkMatrix srcToDstMatrix;
1331 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1332 return;
1333 }
1334 SkRect tmpSrc, tmpDst;
1335 if (src != &bmpBounds) {
1336 if (!bmpBounds.contains(*src)) {
1337 tmpSrc = *src;
1338 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001339 return; // nothing to draw
1340 }
bsalomonb1b01992015-11-18 10:56:08 -08001341 src = &tmpSrc;
1342 srcToDstMatrix.mapRect(&tmpDst, *src);
1343 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001344 }
1345 }
1346
bsalomonb1b01992015-11-18 10:56:08 -08001347 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001348
bsalomonb1b01992015-11-18 10:56:08 -08001349 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1350 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
robertphillips071457b2016-05-16 09:50:03 -07001351 bool drawAA = !fDrawContext->isUnifiedMultisampled() &&
bsalomonb1b01992015-11-18 10:56:08 -08001352 paint.isAntiAlias() &&
1353 bitmap.width() <= maxTileSize &&
1354 bitmap.height() <= maxTileSize;
1355
1356 bool skipTileCheck = drawAA || paint.getMaskFilter();
1357
1358 if (!skipTileCheck) {
1359 int tileSize;
1360 SkIRect clippedSrcRect;
1361
1362 GrTextureParams params;
1363 bool doBicubic;
1364 GrTextureParams::FilterMode textureFilterMode =
1365 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1366 &doBicubic);
1367
1368 int tileFilterPad;
1369
1370 if (doBicubic) {
1371 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1372 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1373 tileFilterPad = 0;
1374 } else {
1375 tileFilterPad = 1;
1376 }
1377 params.setFilterMode(textureFilterMode);
1378
1379 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1380 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1381 // we have a mask filter.
1382 SkMatrix viewMatrix = *draw.fMatrix;
1383 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1384 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1385 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1386 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1387 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1388 constraint, tileSize, doBicubic);
1389 return;
1390 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001391 }
bsalomonb1b01992015-11-18 10:56:08 -08001392 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001393 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001394}
1395
1396void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1397 int x, int y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001398 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001399 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001400 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001401 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001402
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001403 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001404 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001405
robertphillipsca6eafc2016-05-17 09:57:46 -07001406 sk_sp<GrTexture> devTex(dev->accessDrawContext()->asTexture());
1407 if (!devTex) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001408 return;
1409 }
1410
robertphillips7b9e8a42014-12-11 08:20:31 -08001411 const SkImageInfo ii = dev->imageInfo();
1412 int w = ii.width();
1413 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001414
senorblancof35566e2016-04-18 10:32:02 -07001415 SkASSERT(!paint.getImageFilter());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001416
1417 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001418 SkAutoTUnref<const GrFragmentProcessor> fp(
robertphillipsca6eafc2016-05-17 09:57:46 -07001419 GrSimpleTextureEffect::Create(devTex.get(), SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001420 if (GrPixelConfigIsAlphaOnly(devTex->config())) {
1421 // Can this happen?
1422 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1423 } else {
1424 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1425 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001426
brianosman898235c2016-04-06 07:38:23 -07001427 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp,
brianosmanb461d342016-04-13 13:10:14 -07001428 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001429 return;
1430 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001431
1432 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1433 SkIntToScalar(y),
1434 SkIntToScalar(w),
1435 SkIntToScalar(h));
1436
1437 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1438 // scratch texture).
1439 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1440 SK_Scalar1 * h / devTex->height());
1441
bsalomona2e69fc2015-11-05 10:41:43 -08001442 fDrawContext->fillRectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001443}
1444
reeda85d4d02015-05-06 12:56:48 -07001445void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1446 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001447 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001448 SkMatrix viewMatrix = *draw.fMatrix;
1449 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001450 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001451 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001452 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001453 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1454 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001455 return;
reed85d91782015-09-10 14:33:38 -07001456 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001457 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001458 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1459 paint.getFilterQuality(), *draw.fMatrix)) {
1460 // only support tiling as bitmap at the moment, so force raster-version
1461 if (!as_IB(image)->getROPixels(&bm)) {
1462 return;
1463 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001464 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1465 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1466 CHECK_SHOULD_DRAW(draw);
1467 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001468 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1469 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001470 } else if (as_IB(image)->getROPixels(&bm)) {
1471 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001472 }
reeda85d4d02015-05-06 12:56:48 -07001473 }
1474}
1475
1476void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001477 const SkRect& dst, const SkPaint& paint,
1478 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001479 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001480 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001481 CHECK_SHOULD_DRAW(draw);
1482 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001483 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001484 return;
1485 }
1486 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001487 SkMatrix totalMatrix = *draw.fMatrix;
1488 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1489 dst.height() / (src ? src->height() : image->height()));
1490 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001491 // only support tiling as bitmap at the moment, so force raster-version
1492 if (!as_IB(image)->getROPixels(&bm)) {
1493 return;
1494 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001495 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1496 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1497 CHECK_SHOULD_DRAW(draw);
1498 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001499 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001500 } else if (as_IB(image)->getROPixels(&bm)) {
1501 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001502 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001503}
1504
bsalomonf1ecd212015-12-09 17:06:02 -08001505void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001506 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001507 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001508
joshualitt33a5fce2015-11-18 13:28:51 -08001509 CHECK_SHOULD_DRAW(draw);
1510
joshualittedb36442015-11-19 14:29:30 -08001511 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
robertphillips071457b2016-05-16 09:50:03 -07001512 fDrawContext->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001513 bool doBicubic;
1514 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001515 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1516 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001517 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001518 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001519
1520 SkRect srcR, dstR;
1521 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001522 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001523 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001524 }
1525 return;
1526 }
1527
bsalomon2bbd0c62015-12-09 12:50:56 -08001528 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
brianosman982eb7f2016-06-06 13:10:58 -07001529 bool gammaCorrect = this->surfaceProps().isGammaCorrect();
1530 SkSourceGammaTreatment gammaTreatment = gammaCorrect
1531 ? SkSourceGammaTreatment::kRespect : SkSourceGammaTreatment::kIgnore;
bsalomon2bbd0c62015-12-09 12:50:56 -08001532 SkAutoTUnref<const GrFragmentProcessor> fp(
1533 producer->createFragmentProcessor(SkMatrix::I(),
1534 SkRect::MakeIWH(producer->width(), producer->height()),
1535 GrTextureProducer::kNo_FilterConstraint, true,
brianosman982eb7f2016-06-06 13:10:58 -07001536 &kMode, gammaTreatment));
joshualitt33a5fce2015-11-18 13:28:51 -08001537 GrPaint grPaint;
1538 if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, fp,
brianosman982eb7f2016-06-06 13:10:58 -07001539 producer->isAlphaOnly(), gammaCorrect, &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001540 return;
1541 }
1542
bsalomon2bbd0c62015-12-09 12:50:56 -08001543 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1544 producer->height(), center, dst);
1545}
1546
1547void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1548 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001549 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001550 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001551 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001552 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001553 } else {
1554 SkBitmap bm;
1555 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1556 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001557 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001558 } else if (as_IB(image)->getROPixels(&bm)) {
1559 this->drawBitmapNine(draw, bm, center, dst, paint);
1560 }
1561 }
1562}
1563
1564void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1565 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001566 ASSERT_SINGLE_OWNER
bsalomon2bbd0c62015-12-09 12:50:56 -08001567 if (bitmap.getTexture()) {
1568 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001569 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001570 } else {
1571 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001572 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001573 }
joshualitt33a5fce2015-11-18 13:28:51 -08001574}
1575
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001576///////////////////////////////////////////////////////////////////////////////
1577
1578// must be in SkCanvas::VertexMode order
1579static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1580 kTriangles_GrPrimitiveType,
1581 kTriangleStrip_GrPrimitiveType,
1582 kTriangleFan_GrPrimitiveType,
1583};
1584
1585void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1586 int vertexCount, const SkPoint vertices[],
1587 const SkPoint texs[], const SkColor colors[],
1588 SkXfermode* xmode,
1589 const uint16_t indices[], int indexCount,
1590 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001591 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001592 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001593 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001594
halcanary96fcdcc2015-08-27 07:41:13 -07001595 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1596 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001597
halcanary96fcdcc2015-08-27 07:41:13 -07001598 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001599
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001600 SkPaint copy(paint);
1601 copy.setStyle(SkPaint::kStroke_Style);
1602 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001603
bsalomonf1b7a1d2015-09-28 06:26:28 -07001604 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001605 // we ignore the shader if texs is null.
brianosman898235c2016-04-06 07:38:23 -07001606 if (!SkPaintToGrPaintNoShader(this->context(), copy,
brianosmanb461d342016-04-13 13:10:14 -07001607 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001608 return;
1609 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001610
dandov32a311b2014-07-15 19:46:26 -07001611 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001612 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001613 switch (vmode) {
1614 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001615 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001616 break;
1617 case SkCanvas::kTriangleStrip_VertexMode:
1618 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001619 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001620 break;
1621 }
mtklein533eb782014-08-27 10:39:42 -07001622
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001623 VertState state(vertexCount, indices, indexCount);
1624 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001625
dandov32a311b2014-07-15 19:46:26 -07001626 //number of indices for lines per triangle with kLines
1627 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001628
bsalomonf1b7a1d2015-09-28 06:26:28 -07001629 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001630 int i = 0;
1631 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001632 lineIndices[i] = state.f0;
1633 lineIndices[i + 1] = state.f1;
1634 lineIndices[i + 2] = state.f1;
1635 lineIndices[i + 3] = state.f2;
1636 lineIndices[i + 4] = state.f2;
1637 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001638 i += 6;
1639 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001640 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001641 grPaint,
1642 *draw.fMatrix,
1643 kLines_GrPrimitiveType,
1644 vertexCount,
1645 vertices,
1646 texs,
1647 colors,
1648 lineIndices.get(),
1649 indexCount);
1650 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001651 }
1652
bsalomonf1b7a1d2015-09-28 06:26:28 -07001653 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001654
1655 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001656 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001657 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1658 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001659 convertedColors.reset(vertexCount);
1660 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001661 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001662 }
1663 colors = convertedColors.get();
1664 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001665 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001666 if (texs && paint.getShader()) {
1667 if (colors) {
1668 // When there are texs and colors the shader and colors are combined using xmode. A null
1669 // xmode is defined to mean modulate.
1670 SkXfermode::Mode colorMode;
1671 if (xmode) {
1672 if (!xmode->asMode(&colorMode)) {
1673 return;
1674 }
1675 } else {
1676 colorMode = SkXfermode::kModulate_Mode;
1677 }
1678 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
brianosmanb461d342016-04-13 13:10:14 -07001679 false, this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001680 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001681 return;
1682 }
1683 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001684 // We have a shader, but no colors to blend it against.
brianosman898235c2016-04-06 07:38:23 -07001685 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001686 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001687 return;
1688 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001689 }
bsalomonaa48d362015-10-01 08:34:17 -07001690 } else {
1691 if (colors) {
1692 // We have colors, but either have no shader or no texture coords (which implies that
1693 // we should ignore the shader).
brianosman898235c2016-04-06 07:38:23 -07001694 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001695 this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001696 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001697 return;
1698 }
1699 } else {
1700 // No colors and no shaders. Just draw with the paint color.
brianosman898235c2016-04-06 07:38:23 -07001701 if (!SkPaintToGrPaintNoShader(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001702 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001703 return;
1704 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001705 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001706 }
1707
robertphillips2e1e51f2015-10-15 08:01:48 -07001708 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001709 grPaint,
1710 *draw.fMatrix,
1711 primType,
1712 vertexCount,
1713 vertices,
1714 texs,
1715 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001716 indices,
robertphillipsea461502015-05-26 11:38:03 -07001717 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001718}
1719
1720///////////////////////////////////////////////////////////////////////////////
1721
jvanverth31ff7622015-08-07 10:09:28 -07001722void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001723 const SkRect texRect[], const SkColor colors[], int count,
1724 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001725 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001726 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001727 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001728 return;
1729 }
1730
jvanverth31ff7622015-08-07 10:09:28 -07001731 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001732 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001733
reedca109532015-06-25 16:25:25 -07001734 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001735 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001736
jvanverth31ff7622015-08-07 10:09:28 -07001737 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001738 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001739 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
brianosmanb461d342016-04-13 13:10:14 -07001740 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001741 return;
1742 }
1743 } else {
brianosman898235c2016-04-06 07:38:23 -07001744 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001745 this->surfaceProps().isGammaCorrect(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001746 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001747 }
1748 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001749
1750 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001751 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001752}
1753
1754///////////////////////////////////////////////////////////////////////////////
1755
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001756void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001757 size_t byteLength, SkScalar x, SkScalar y,
1758 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001759 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001760 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001761 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001762
jvanverth8c27a182014-10-14 08:45:50 -07001763 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001764 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001765 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001766 return;
1767 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001768
jvanverth8c27a182014-10-14 08:45:50 -07001769 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001770
robertphillips2e1e51f2015-10-15 08:01:48 -07001771 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001772 (const char *)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001773}
1774
fmalita05c4a432014-09-29 06:29:53 -07001775void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1776 const SkScalar pos[], int scalarsPerPos,
1777 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001778 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001779 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001780 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001781
jvanverth8c27a182014-10-14 08:45:50 -07001782 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001783 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001784 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001785 return;
1786 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001787
jvanverth8c27a182014-10-14 08:45:50 -07001788 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001789
robertphillips2e1e51f2015-10-15 08:01:48 -07001790 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001791 (const char *)text, byteLength, pos, scalarsPerPos, offset,
reed1e7f5e72016-04-27 07:49:17 -07001792 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001793}
1794
joshualitt9c328182015-03-23 08:13:04 -07001795void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1796 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001797 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001798 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001799 CHECK_SHOULD_DRAW(draw);
1800
1801 SkDEBUGCODE(this->validate();)
1802
robertphillips2e1e51f2015-10-15 08:01:48 -07001803 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001804 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001805}
1806
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001807///////////////////////////////////////////////////////////////////////////////
1808
reedb2db8982014-11-13 12:41:02 -08001809bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001810 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001811}
1812
1813void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001814 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001815
bsalomonc49e8682015-06-30 11:37:35 -07001816 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001817}
1818
1819///////////////////////////////////////////////////////////////////////////////
1820
reed76033be2015-03-14 10:54:31 -07001821SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001822 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001823
robertphillipsca6eafc2016-05-17 09:57:46 -07001824 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001825
robertphillipsca6eafc2016-05-17 09:57:46 -07001826 // layers are never drawn in repeat modes, so we can request an approx
hcm4396fa52014-10-27 21:43:30 -07001827 // match and ignore any padding.
robertphillipsca6eafc2016-05-17 09:57:46 -07001828 SkBackingFit fit = kNever_TileUsage == cinfo.fTileUsage ? SkBackingFit::kApprox
1829 : SkBackingFit::kExact;
bsalomonafe30052015-01-16 07:32:33 -08001830
robertphillipsca6eafc2016-05-17 09:57:46 -07001831 sk_sp<GrDrawContext> dc(fContext->newDrawContext(fit,
1832 cinfo.fInfo.width(), cinfo.fInfo.height(),
1833 fDrawContext->config(),
1834 fDrawContext->desc().fSampleCnt,
1835 kDefault_GrSurfaceOrigin,
1836 &props));
1837 if (!dc) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001838 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001839 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001840 cinfo.fInfo.width(), cinfo.fInfo.height());
robertphillipsca6eafc2016-05-17 09:57:46 -07001841 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001842 }
robertphillipsca6eafc2016-05-17 09:57:46 -07001843
1844 // Skia's convention is to only clear a device if it is non-opaque.
1845 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
1846
1847 return SkGpuDevice::Make(std::move(dc),
1848 cinfo.fInfo.width(), cinfo.fInfo.height(),
1849 init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001850}
1851
reede8f30622016-03-23 18:59:25 -07001852sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001853 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001854 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001855 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
robertphillipse5768742016-05-13 11:20:46 -07001856 return SkSurface::MakeRenderTarget(fContext, kBudgeted, info, fDrawContext->desc().fSampleCnt,
reede8f30622016-03-23 18:59:25 -07001857 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001858}
1859
robertphillips30d2cc62014-09-24 08:52:18 -07001860bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001861 const SkMatrix* matrix, const SkPaint* paint) {
joshualittce894002016-01-11 13:29:31 -08001862 ASSERT_SINGLE_OWNER
robertphillips63242d72014-12-04 08:31:02 -08001863#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001864 // todo: should handle this natively
robertphillips74e9a4d2016-05-09 05:12:18 -07001865 if (paint ||
1866 (kRGBA_8888_SkColorType != mainCanvas->imageInfo().colorType() &&
1867 kBGRA_8888_SkColorType != mainCanvas->imageInfo().colorType())) {
reedd5fa1a42014-08-09 11:08:05 -07001868 return false;
1869 }
1870
halcanary96fcdcc2015-08-27 07:41:13 -07001871 const SkBigPicture::AccelData* data = nullptr;
mtklein9db912c2015-05-19 11:11:26 -07001872 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1873 data = bp->accelData();
1874 }
robertphillips81f71b62014-11-11 04:54:49 -08001875 if (!data) {
1876 return false;
1877 }
1878
robertphillipse5524cd2015-02-20 12:30:26 -08001879 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1880 if (0 == gpuData->numBlocks()) {
1881 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001882 }
1883
robertphillipsfd61ed02014-10-28 07:21:44 -07001884 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001885
robertphillipse5524cd2015-02-20 12:30:26 -08001886 SkIRect iBounds;
1887 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1888 return false;
1889 }
1890
1891 SkRect clipBounds = SkRect::Make(iBounds);
1892
1893 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1894
robertphillips60029a52015-11-09 13:51:06 -08001895 GrLayerHoister::Begin(fContext);
1896
robertphillipsfd61ed02014-10-28 07:21:44 -07001897 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001898 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001899 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001900 &atlasedNeedRendering, &atlasedRecycled,
robertphillipse5768742016-05-13 11:20:46 -07001901 fDrawContext->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001902
1903 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1904
1905 SkTDArray<GrHoistedLayer> needRendering, recycled;
1906
robertphillipse5524cd2015-02-20 12:30:26 -08001907 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1908
robertphillipsfd61ed02014-10-28 07:21:44 -07001909 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001910 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001911 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001912 &needRendering, &recycled,
robertphillipse5768742016-05-13 11:20:46 -07001913 fDrawContext->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001914
1915 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001916
robertphillips64bf7672014-08-21 13:07:35 -07001917 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001918 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
halcanary96fcdcc2015-08-27 07:41:13 -07001919 initialMatrix, nullptr);
robertphillips64bf7672014-08-21 13:07:35 -07001920
robertphillipsfd61ed02014-10-28 07:21:44 -07001921 GrLayerHoister::UnlockLayers(fContext, needRendering);
1922 GrLayerHoister::UnlockLayers(fContext, recycled);
1923 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1924 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips60029a52015-11-09 13:51:06 -08001925 GrLayerHoister::End(fContext);
robertphillips64bf7672014-08-21 13:07:35 -07001926
1927 return true;
robertphillips63242d72014-12-04 08:31:02 -08001928#else
1929 return false;
1930#endif
robertphillips64bf7672014-08-21 13:07:35 -07001931}
1932
senorblanco900c3672016-04-27 11:31:23 -07001933SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001934 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001935 // We always return a transient cache, so it is freed after each
1936 // filter traversal.
senorblanco900c3672016-04-27 11:31:23 -07001937 return SkImageFilterCache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001938}
reedf037e0b2014-10-30 11:34:15 -07001939
1940#endif