blob: 54ed413549d8ec83cf5c091fb28d4c7f87fa0206 [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
9
robertphillipsccb1b572015-05-27 11:02:55 -070010#include "GrBlurUtils.h"
kkinnunenabcfab42015-02-22 22:53:44 -080011#include "GrContext.h"
herb11a7f7f2015-11-24 12:41:00 -080012#include "SkDraw.h"
kkinnunenabcfab42015-02-22 22:53:44 -080013#include "GrGpu.h"
14#include "GrGpuResourcePriv.h"
bsalomonc55271f2015-11-09 11:55:57 -080015#include "GrImageIDTextureAdjuster.h"
robertphillips98d709b2014-09-02 10:20:50 -070016#include "GrLayerHoister.h"
robertphillips274b4ba2014-09-04 07:24:18 -070017#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070018#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070019#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080020#include "SkCanvasPriv.h"
kkinnunenabcfab42015-02-22 22:53:44 -080021#include "SkErrorInternals.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000022#include "SkGlyphCache.h"
kkinnunenabcfab42015-02-22 22:53:44 -080023#include "SkGrTexturePixelRef.h"
bsalomonf276ac52015-10-09 13:36:42 -070024#include "SkGr.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070025#include "SkGrPriv.h"
reeda85d4d02015-05-06 12:56:48 -070026#include "SkImage_Base.h"
bsalomon1cf6f9b2015-12-08 10:53:43 -080027#include "SkImageCacherator.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkImageFilter.h"
senorblanco900c3672016-04-27 11:31:23 -070029#include "SkImageFilterCache.h"
robertphillips82365912014-11-12 09:32:34 -080030#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000031#include "SkMaskFilter.h"
joshualitt33a5fce2015-11-18 13:28:51 -080032#include "SkNinePatchIter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000034#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070035#include "SkPictureData.h"
reed1e7f5e72016-04-27 07:49:17 -070036#include "SkRasterClip.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080038#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000039#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000040#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080041#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000042#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000043#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000044#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070045#include "SkXfermode.h"
joshualitta61c8172015-08-17 10:51:22 -070046#include "batches/GrRectBatchFactory.h"
kkinnunenabcfab42015-02-22 22:53:44 -080047#include "effects/GrBicubicEffect.h"
48#include "effects/GrDashingEffect.h"
49#include "effects/GrSimpleTextureEffect.h"
50#include "effects/GrTextureDomain.h"
joshualitt8e84a1e2016-02-16 11:09:25 -080051#include "text/GrTextUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000052
reedf037e0b2014-10-30 11:34:15 -070053#if SK_SUPPORT_GPU
54
joshualittce894002016-01-11 13:29:31 -080055#define ASSERT_SINGLE_OWNER \
56 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fContext->debugSingleOwner());)
57
senorblanco55b6d8b2014-07-30 11:26:46 -070058enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
59
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000060#if 0
61 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080062 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000063 do { \
64 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080065 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000066 } while (0)
67#else
joshualitt5531d512014-12-17 15:50:11 -080068 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000069#endif
70
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000071///////////////////////////////////////////////////////////////////////////////
72
bsalomonbcf0a522014-10-08 08:40:09 -070073// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
74// just accesses the backing GrTexture. Otherwise, it creates a cached texture
75// representation and releases it in the destructor.
76class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040077public:
bsalomonbcf0a522014-10-08 08:40:09 -070078 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070079
bsalomonbcf0a522014-10-08 08:40:09 -070080 AutoBitmapTexture(GrContext* context,
81 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -070082 const GrTextureParams& params,
bsalomonbcf0a522014-10-08 08:40:09 -070083 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040084 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070085 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040086 }
87
bsalomonbcf0a522014-10-08 08:40:09 -070088 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040089 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -070090 const GrTextureParams& params) {
bsalomonbcf0a522014-10-08 08:40:09 -070091 // Either get the texture directly from the bitmap, or else use the cache and
92 // remember to unref it.
93 if (GrTexture* bmpTexture = bitmap.getTexture()) {
halcanary96fcdcc2015-08-27 07:41:13 -070094 fTexture.reset(nullptr);
bsalomonbcf0a522014-10-08 08:40:09 -070095 return bmpTexture;
96 } else {
97 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
98 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -040099 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400100 }
101
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000102private:
bsalomonbcf0a522014-10-08 08:40:09 -0700103 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000104};
105
106///////////////////////////////////////////////////////////////////////////////
107
bsalomon74f681d2015-06-23 14:38:48 -0700108/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
109 should fail. */
110bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
111 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
112 *flags = 0;
113 if (info) {
114 switch (info->alphaType()) {
115 case kPremul_SkAlphaType:
116 break;
117 case kOpaque_SkAlphaType:
118 *flags |= SkGpuDevice::kIsOpaque_Flag;
119 break;
120 default: // If it is unpremul or unknown don't try to render
121 return false;
122 }
123 }
124 if (kClear_InitContents == init) {
125 *flags |= kNeedClear_Flag;
126 }
127 return true;
128}
129
robertphillips24e91282016-04-29 06:46:36 -0700130sk_sp<SkGpuDevice> SkGpuDevice::Make(sk_sp<GrRenderTarget> rt, const SkSurfaceProps* props,
131 InitContents init) {
132 const int width = rt->width();
133 const int height = rt->height();
134 return SkGpuDevice::Make(std::move(rt), width, height, props, init);
senorblancod0d37ca2015-04-02 04:54:56 -0700135}
136
robertphillips24e91282016-04-29 06:46:36 -0700137sk_sp<SkGpuDevice> SkGpuDevice::Make(sk_sp<GrRenderTarget> rt, int width, int height,
138 const SkSurfaceProps* props, InitContents init) {
bsalomonafe30052015-01-16 07:32:33 -0800139 if (!rt || rt->wasDestroyed()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700140 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000141 }
bsalomon74f681d2015-06-23 14:38:48 -0700142 unsigned flags;
halcanary96fcdcc2015-08-27 07:41:13 -0700143 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
144 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700145 }
robertphillips24e91282016-04-29 06:46:36 -0700146 return sk_sp<SkGpuDevice>(new SkGpuDevice(rt.get(), width, height, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000147}
148
robertphillips24e91282016-04-29 06:46:36 -0700149sk_sp<SkGpuDevice> SkGpuDevice::Make(GrContext* context, SkBudgeted budgeted,
150 const SkImageInfo& info, int sampleCount,
151 const SkSurfaceProps* props, InitContents init) {
bsalomon74f681d2015-06-23 14:38:48 -0700152 unsigned flags;
153 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700154 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700155 }
156
bsalomon7e68ab72016-04-13 14:29:25 -0700157 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
robertphillips24e91282016-04-29 06:46:36 -0700158 if (!rt) {
halcanary96fcdcc2015-08-27 07:41:13 -0700159 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700160 }
161
robertphillips24e91282016-04-29 06:46:36 -0700162 return sk_sp<SkGpuDevice>(new SkGpuDevice(rt, info.width(), info.height(), props, flags));
bsalomon74f681d2015-06-23 14:38:48 -0700163}
164
senorblancod0d37ca2015-04-02 04:54:56 -0700165SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
166 const SkSurfaceProps* props, unsigned flags)
robertphillipsfcf78292015-06-19 11:49:52 -0700167 : INHERITED(SkSurfacePropsCopyOrDefault(props))
joshualitt06dd0a82016-01-08 06:32:31 -0800168 , fContext(SkRef(rt->getContext()))
169 , fRenderTarget(SkRef(rt)) {
bsalomon74f681d2015-06-23 14:38:48 -0700170 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000171
bsalomon74f681d2015-06-23 14:38:48 -0700172 SkAlphaType at = fOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
173 SkImageInfo info = rt->surfacePriv().info(at).makeWH(width, height);
halcanary385fe4d2015-08-26 13:07:48 -0700174 SkPixelRef* pr = new SkGrPixelRef(info, rt);
bsalomonafbf2d62014-09-30 12:18:44 -0700175 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700176 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700177
robertphillips6c7e3252016-04-27 10:47:51 -0700178 fDrawContext = this->context()->drawContext(sk_ref_sp(rt), &this->surfaceProps());
bsalomone63ffef2016-02-05 07:17:34 -0800179 if (flags & kNeedClear_Flag) {
180 this->clearAll();
181 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000182}
183
bsalomon7e68ab72016-04-13 14:29:25 -0700184GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkBudgeted budgeted,
185 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000186 if (kUnknown_SkColorType == origInfo.colorType() ||
187 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700188 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000189 }
190
bsalomonafe30052015-01-16 07:32:33 -0800191 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700192 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800193 }
194
reede5ea5002014-09-03 11:54:58 -0700195 SkColorType ct = origInfo.colorType();
196 SkAlphaType at = origInfo.alphaType();
brianosmana6359362016-03-21 06:55:37 -0700197 SkColorProfileType pt = origInfo.profileType();
brianosman6b086522016-04-14 12:39:00 -0700198 if (kRGB_565_SkColorType == ct || kGray_8_SkColorType == ct) {
reede5ea5002014-09-03 11:54:58 -0700199 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800200 }
201 if (kOpaque_SkAlphaType != at) {
202 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000203 }
brianosman6b086522016-04-14 12:39:00 -0700204
205 GrPixelConfig origConfig = SkImageInfo2GrPixelConfig(ct, at, pt, *context->caps());
206 if (!context->caps()->isConfigRenderable(origConfig, sampleCount > 0)) {
207 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
208 ct = kN32_SkColorType;
209 }
210
brianosmana6359362016-03-21 06:55:37 -0700211 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at, pt);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000212
bsalomonf2703d82014-10-28 14:33:06 -0700213 GrSurfaceDesc desc;
214 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000215 desc.fWidth = info.width();
216 desc.fHeight = info.height();
brianosmana6359362016-03-21 06:55:37 -0700217 desc.fConfig = SkImageInfo2GrPixelConfig(info, *context->caps());
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000218 desc.fSampleCnt = sampleCount;
cblume55f2d2d2016-02-26 13:20:48 -0800219 desc.fIsMipMapped = false;
bsalomon5ec26ae2016-02-25 08:33:02 -0800220 GrTexture* texture = context->textureProvider()->createTexture(desc, budgeted, nullptr, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700221 if (nullptr == texture) {
222 return nullptr;
kkinnunenabcfab42015-02-22 22:53:44 -0800223 }
halcanary96fcdcc2015-08-27 07:41:13 -0700224 SkASSERT(nullptr != texture->asRenderTarget());
kkinnunenabcfab42015-02-22 22:53:44 -0800225 return texture->asRenderTarget();
226}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000227
robertphillips1579e3c2016-03-24 05:01:23 -0700228// This method ensures that we always have a texture-backed "bitmap" when we finally
229// call through to the base impl so that the image filtering code will take the
230// gpu-specific paths. This mirrors SkCanvas::internalDrawDevice (the other
231// use of SkImageFilter::filterImage) in that the source and dest will have
232// homogenous backing (e.g., raster or gpu).
robertphillips2302de92016-03-24 07:26:32 -0700233void SkGpuDevice::drawSpriteWithFilter(const SkDraw& draw, const SkBitmap& bitmap,
234 int x, int y, const SkPaint& paint) {
robertphillips090b7622016-03-28 11:07:43 -0700235 ASSERT_SINGLE_OWNER
236 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawSpriteWithFilter", fContext);
237
238 if (fContext->abandoned()) {
239 return;
240 }
241
robertphillips1579e3c2016-03-24 05:01:23 -0700242 if (bitmap.getTexture()) {
robertphillips2302de92016-03-24 07:26:32 -0700243 INHERITED::drawSpriteWithFilter(draw, bitmap, x, y, paint);
robertphillips1579e3c2016-03-24 05:01:23 -0700244 return;
245 }
246
247 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
248 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
249 return;
250 }
251
252 GrTexture* texture;
253 // draw sprite neither filters nor tiles.
254 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
255 if (!texture) {
256 return;
257 }
258
259 SkBitmap newBitmap;
260
261 GrWrapTextureInBitmap(texture, texture->width(), texture->height(),
262 bitmap.isOpaque(), &newBitmap);
263
robertphillips2302de92016-03-24 07:26:32 -0700264 INHERITED::drawSpriteWithFilter(draw, newBitmap, x, y, paint);
robertphillips1579e3c2016-03-24 05:01:23 -0700265}
266
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000267///////////////////////////////////////////////////////////////////////////////
268
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000269bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
270 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800271 ASSERT_SINGLE_OWNER
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000272
273 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700274 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps());
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000275 if (kUnknown_GrPixelConfig == config) {
276 return false;
277 }
278
279 uint32_t flags = 0;
280 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
281 flags = GrContext::kUnpremul_PixelOpsFlag;
282 }
bsalomon74f681d2015-06-23 14:38:48 -0700283 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
284 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000285}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000286
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000287bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
288 int x, int y) {
joshualittce894002016-01-11 13:29:31 -0800289 ASSERT_SINGLE_OWNER
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000290 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
brianosmana6359362016-03-21 06:55:37 -0700291 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fContext->caps());
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000292 if (kUnknown_GrPixelConfig == config) {
293 return false;
294 }
295 uint32_t flags = 0;
296 if (kUnpremul_SkAlphaType == info.alphaType()) {
297 flags = GrContext::kUnpremul_PixelOpsFlag;
298 }
299 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
300
301 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700302 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000303
304 return true;
305}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000306
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000307const SkBitmap& SkGpuDevice::onAccessBitmap() {
joshualittce894002016-01-11 13:29:31 -0800308 ASSERT_SINGLE_OWNER
reed89443ab2014-06-27 11:34:19 -0700309 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000310}
311
reed41e010c2015-06-09 12:16:53 -0700312bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
joshualittce894002016-01-11 13:29:31 -0800313 ASSERT_SINGLE_OWNER
reed41e010c2015-06-09 12:16:53 -0700314 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
315 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
316 // ... ugh.
317 fLegacyBitmap.notifyPixelsChanged();
318 return false;
319}
320
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000321void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
joshualittce894002016-01-11 13:29:31 -0800322 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323 INHERITED::onAttachToCanvas(canvas);
324
325 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800326 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000327}
328
329void SkGpuDevice::onDetachFromCanvas() {
joshualittce894002016-01-11 13:29:31 -0800330 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000331 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800332 fClip.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700333 fClipStack.reset(nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000334}
335
336// call this every draw call, to ensure that the context reflects our state,
337// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800338void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualittce894002016-01-11 13:29:31 -0800339 ASSERT_SINGLE_OWNER
joshualitt44701df2015-02-23 14:44:57 -0800340 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000341
joshualitt44701df2015-02-23 14:44:57 -0800342 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000343
joshualitt570d2f82015-02-25 13:19:48 -0800344 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000345}
346
347GrRenderTarget* SkGpuDevice::accessRenderTarget() {
joshualittce894002016-01-11 13:29:31 -0800348 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000349 return fRenderTarget;
350}
351
robertphillips175dd9b2016-04-28 14:32:04 -0700352GrDrawContext* SkGpuDevice::accessDrawContext() {
353 ASSERT_SINGLE_OWNER
354 return fDrawContext.get();
355}
356
reed8eddfb52014-12-04 07:50:14 -0800357void SkGpuDevice::clearAll() {
joshualittce894002016-01-11 13:29:31 -0800358 ASSERT_SINGLE_OWNER
reed8eddfb52014-12-04 07:50:14 -0800359 GrColor color = 0;
joshualitt5651ee62016-01-11 10:39:11 -0800360 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext);
reed8eddfb52014-12-04 07:50:14 -0800361 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700362 fDrawContext->clear(&rect, color, true);
reed8eddfb52014-12-04 07:50:14 -0800363}
364
kkinnunenabcfab42015-02-22 22:53:44 -0800365void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
joshualittce894002016-01-11 13:29:31 -0800366 ASSERT_SINGLE_OWNER
kkinnunenabcfab42015-02-22 22:53:44 -0800367
bsalomon5ec26ae2016-02-25 08:33:02 -0800368 SkBudgeted budgeted = fRenderTarget->resourcePriv().isBudgeted();
kkinnunenabcfab42015-02-22 22:53:44 -0800369
370 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
bsalomon7e68ab72016-04-13 14:29:25 -0700371 this->context(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt));
kkinnunenabcfab42015-02-22 22:53:44 -0800372
halcanary96fcdcc2015-08-27 07:41:13 -0700373 if (nullptr == newRT) {
kkinnunenabcfab42015-02-22 22:53:44 -0800374 return;
375 }
376
377 if (shouldRetainContent) {
378 if (fRenderTarget->wasDestroyed()) {
379 return;
380 }
381 this->context()->copySurface(newRT, fRenderTarget);
382 }
383
384 SkASSERT(fRenderTarget != newRT);
385
mtklein18300a32016-03-16 13:53:35 -0700386 fRenderTarget.reset(newRT.release());
kkinnunenabcfab42015-02-22 22:53:44 -0800387
bsalomon74f681d2015-06-23 14:38:48 -0700388#ifdef SK_DEBUG
389 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
390 kPremul_SkAlphaType);
391 SkASSERT(info == fLegacyBitmap.info());
392#endif
halcanary385fe4d2015-08-26 13:07:48 -0700393 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget);
kkinnunenabcfab42015-02-22 22:53:44 -0800394 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700395
robertphillips6c7e3252016-04-27 10:47:51 -0700396 fDrawContext = this->context()->drawContext(sk_ref_sp(fRenderTarget.get()),
397 &this->surfaceProps());
kkinnunenabcfab42015-02-22 22:53:44 -0800398}
399
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000400///////////////////////////////////////////////////////////////////////////////
401
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000402void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800403 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -0800404 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800405 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000406
407 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700408 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700409 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700410 return;
411 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000412
robertphillips2e1e51f2015-10-15 08:01:48 -0700413 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000414}
415
416// must be in SkCanvas::PointMode order
417static const GrPrimitiveType gPointMode2PrimtiveType[] = {
418 kPoints_GrPrimitiveType,
419 kLines_GrPrimitiveType,
420 kLineStrip_GrPrimitiveType
421};
422
ethannicholas330bb952015-07-17 06:44:02 -0700423// suppress antialiasing on axis-aligned integer-coordinate lines
424static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
425 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
426 return false;
427 }
428 if (count == 2) {
429 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
430 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800431 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700432 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800433 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700434 // the two end pixels as being the lesser of two evils.
435 if (pts[0].fX == pts[1].fX) {
436 return ((int) pts[0].fX) != pts[0].fX;
437 }
438 if (pts[0].fY == pts[1].fY) {
439 return ((int) pts[0].fY) != pts[0].fY;
440 }
441 }
442 return true;
443}
444
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000445void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
446 size_t count, const SkPoint pts[], const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800447 ASSERT_SINGLE_OWNER
joshualitt086cee12016-01-12 06:45:24 -0800448 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPoints", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800449 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000450
451 SkScalar width = paint.getStrokeWidth();
452 if (width < 0) {
453 return;
454 }
455
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000456 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700457 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
458 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700459 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700460 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700461 return;
462 }
egdaniele61c4112014-06-12 10:24:21 -0700463 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700464 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700465 path.moveTo(pts[0]);
466 path.lineTo(pts[1]);
robertphillips2e1e51f2015-10-15 08:01:48 -0700467 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700468 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000469 }
470
ethannicholas330bb952015-07-17 06:44:02 -0700471 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000472 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800473 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700474 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000475 draw.drawPoints(mode, count, pts, paint, true);
476 return;
477 }
478
479 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700480 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700481 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700482 return;
483 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000484
robertphillips2e1e51f2015-10-15 08:01:48 -0700485 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700486 grPaint,
487 *draw.fMatrix,
488 gPointMode2PrimtiveType[mode],
489 SkToS32(count),
490 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700491 nullptr,
492 nullptr,
493 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700494 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000495}
496
497///////////////////////////////////////////////////////////////////////////////
498
robertphillipsff55b492015-11-24 07:56:59 -0800499void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800500 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800501 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800502 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000503
504 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
505 SkScalar width = paint.getStrokeWidth();
506
507 /*
508 We have special code for hairline strokes, miter-strokes, bevel-stroke
509 and fills. Anything else we just call our path code.
510 */
511 bool usePath = doStroke && width > 0 &&
512 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
513 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
egdanield58a0ba2014-06-11 10:30:05 -0700514
robertphillipsd8aa59d2015-08-05 09:07:12 -0700515 // a few other reasons we might need to call drawPath...
robertphillipsff55b492015-11-24 07:56:59 -0800516 if (paint.getMaskFilter() || paint.getPathEffect() ||
robertphillipsd8aa59d2015-08-05 09:07:12 -0700517 paint.getStyle() == SkPaint::kStrokeAndFill_Style) { // we can't both stroke and fill rects
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000518 usePath = true;
519 }
egdanield58a0ba2014-06-11 10:30:05 -0700520
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000521 if (usePath) {
522 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700523 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000524 path.addRect(rect);
robertphillips6c7e3252016-04-27 10:47:51 -0700525 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800526 fClip, path, paint,
527 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700528 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000529 return;
530 }
531
532 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700533 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700534 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700535 return;
536 }
Mike Klein744fb732014-06-23 15:13:26 -0400537
robertphillipsff55b492015-11-24 07:56:59 -0800538 GrStrokeInfo strokeInfo(paint);
539
robertphillips2e1e51f2015-10-15 08:01:48 -0700540 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541}
542
543///////////////////////////////////////////////////////////////////////////////
544
545void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800546 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800547 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800548 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800549 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000551 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700552 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700553 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700554 return;
555 }
Mike Klein744fb732014-06-23 15:13:26 -0400556
egdanield58a0ba2014-06-11 10:30:05 -0700557 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000558 if (paint.getMaskFilter()) {
559 // try to hit the fast path for drawing filtered round rects
560
561 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800562 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000563 if (devRRect.allCornersCircular()) {
564 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700565 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
reed1e7f5e72016-04-27 07:49:17 -0700566 draw.fRC->getBounds(),
joshualitt5531d512014-12-17 15:50:11 -0800567 *draw.fMatrix,
568 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000569 SkIRect finalIRect;
570 maskRect.roundOut(&finalIRect);
reed1e7f5e72016-04-27 07:49:17 -0700571 if (draw.fRC->quickReject(finalIRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000572 // clipped out
573 return;
574 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700575 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
robertphillips6c7e3252016-04-27 10:47:51 -0700576 fDrawContext.get(),
joshualitt25d9c152015-02-18 12:29:52 -0800577 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800578 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800579 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700580 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700581 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000582 return;
583 }
584 }
585
586 }
587 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000588 }
589
robertphillips514450c2015-11-24 05:36:02 -0800590 if (paint.getMaskFilter() || paint.getPathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800591 // The only mask filter the native rrect drawing code could've handle was taken
592 // care of above.
593 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000594 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700595 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000596 path.addRRect(rect);
robertphillips6c7e3252016-04-27 10:47:51 -0700597 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsff55b492015-11-24 07:56:59 -0800598 fClip, path, paint,
599 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700600 draw.fRC->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000601 return;
602 }
Mike Klein744fb732014-06-23 15:13:26 -0400603
robertphillips514450c2015-11-24 05:36:02 -0800604 SkASSERT(!strokeInfo.isDashed());
605
robertphillips2e1e51f2015-10-15 08:01:48 -0700606 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000607}
608
robertphillipsd7706102016-02-25 09:28:08 -0800609
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000610void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800611 const SkRRect& inner, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800612 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800613 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800614 CHECK_SHOULD_DRAW(draw);
615
robertphillipsd7706102016-02-25 09:28:08 -0800616 if (outer.isEmpty()) {
617 return;
618 }
619
620 if (inner.isEmpty()) {
621 return this->drawRRect(draw, outer, paint);
622 }
623
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000624 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000625
robertphillips0e7029e2015-11-30 05:45:06 -0800626 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
robertphillips00095892016-02-29 13:50:40 -0800627 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700628 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700629 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700630 return;
631 }
robertphillips00095892016-02-29 13:50:40 -0800632
633 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
634 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000635 }
636
637 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700638 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000639 path.addRRect(outer);
640 path.addRRect(inner);
641 path.setFillType(SkPath::kEvenOdd_FillType);
642
robertphillips6c7e3252016-04-27 10:47:51 -0700643 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillips0e7029e2015-11-30 05:45:06 -0800644 fClip, path, paint,
645 *draw.fMatrix, nullptr,
reed1e7f5e72016-04-27 07:49:17 -0700646 draw.fRC->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000647}
648
649
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000650/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000651
robertphillips514450c2015-11-24 05:36:02 -0800652void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800653 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -0800654 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
joshualitt5531d512014-12-17 15:50:11 -0800655 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000656
robertphillips514450c2015-11-24 05:36:02 -0800657 // Presumably the path effect warps this to something other than an oval
658 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000659 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700660 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000661 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700662 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000663 return;
herb11a7f7f2015-11-24 12:41:00 -0800664 }
665
robertphillips514450c2015-11-24 05:36:02 -0800666 if (paint.getMaskFilter()) {
667 // The RRect path can handle special case blurring
668 SkRRect rr = SkRRect::MakeOval(oval);
669 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000670 }
671
672 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -0700673 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -0700674 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700675 return;
676 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000677
robertphillips514450c2015-11-24 05:36:02 -0800678 GrStrokeInfo strokeInfo(paint);
679 SkASSERT(!strokeInfo.isDashed());
680
robertphillips2e1e51f2015-10-15 08:01:48 -0700681 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000682}
683
684#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000685
686///////////////////////////////////////////////////////////////////////////////
687
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000688void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
689 const SkPaint& paint, const SkMatrix* prePathMatrix,
690 bool pathIsMutable) {
joshualittce894002016-01-11 13:29:31 -0800691 ASSERT_SINGLE_OWNER
robertphillipsff55b492015-11-24 07:56:59 -0800692 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
693 bool isClosed;
694 SkRect rect;
695 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
696 this->drawRect(draw, rect, paint);
697 return;
698 }
699 if (origSrcPath.isOval(&rect)) {
700 this->drawOval(draw, rect, paint);
701 return;
702 }
703 SkRRect rrect;
704 if (origSrcPath.isRRect(&rrect)) {
705 this->drawRRect(draw, rrect, paint);
706 return;
707 }
708 }
709
joshualitt5531d512014-12-17 15:50:11 -0800710 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800711 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000712
robertphillips6c7e3252016-04-27 10:47:51 -0700713 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext.get(),
robertphillipsccb1b572015-05-27 11:02:55 -0700714 fClip, origSrcPath, paint,
715 *draw.fMatrix, prePathMatrix,
reed1e7f5e72016-04-27 07:49:17 -0700716 draw.fRC->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000717}
718
719static const int kBmpSmallTileSize = 1 << 10;
720
721static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
722 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
723 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
724 return tilesX * tilesY;
725}
726
reed85d91782015-09-10 14:33:38 -0700727static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000728 if (maxTileSize <= kBmpSmallTileSize) {
729 return maxTileSize;
730 }
731
732 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
733 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
734
735 maxTileTotalTileSize *= maxTileSize * maxTileSize;
736 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
737
738 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
739 return kBmpSmallTileSize;
740 } else {
741 return maxTileSize;
742 }
743}
744
745// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
746// pixels from the bitmap are necessary.
robertphillipsd215a952016-05-05 12:03:29 -0700747static void determine_clipped_src_rect(int width, int height,
joshualitt570d2f82015-02-25 13:19:48 -0800748 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800749 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700750 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000751 const SkRect* srcRectPtr,
752 SkIRect* clippedSrcIRect) {
robertphillipsd215a952016-05-05 12:03:29 -0700753 clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000754 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800755 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000756 clippedSrcIRect->setEmpty();
757 return;
758 }
759 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
760 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700761 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000762 // we've setup src space 0,0 to map to the top left of the src rect.
763 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764 if (!clippedSrcRect.intersect(*srcRectPtr)) {
765 clippedSrcIRect->setEmpty();
766 return;
767 }
768 }
769 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700770 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000771 if (!clippedSrcIRect->intersect(bmpBounds)) {
772 clippedSrcIRect->setEmpty();
773 }
774}
775
reed85d91782015-09-10 14:33:38 -0700776bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
777 const SkMatrix& viewMatrix,
778 const GrTextureParams& params,
779 const SkRect* srcRectPtr,
780 int maxTileSize,
781 int* tileSize,
782 SkIRect* clippedSubset) const {
joshualittce894002016-01-11 13:29:31 -0800783 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700784 // if it's larger than the max tile size, then we have no choice but tiling.
785 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
robertphillipsd215a952016-05-05 12:03:29 -0700786 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(),
787 fClip, viewMatrix, imageRect.size(),
reed85d91782015-09-10 14:33:38 -0700788 srcRectPtr, clippedSubset);
789 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
790 return true;
791 }
792
bsalomon1a1d0b82015-10-16 07:49:42 -0700793 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700794 const size_t area = imageRect.width() * imageRect.height();
795 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
796 return false;
797 }
798
reed85d91782015-09-10 14:33:38 -0700799 // At this point we know we could do the draw by uploading the entire bitmap
800 // as a texture. However, if the texture would be large compared to the
801 // cache size and we don't require most of it for this draw then tile to
802 // reduce the amount of upload and cache spill.
803
804 // assumption here is that sw bitmap size is a good proxy for its size as
805 // a texture
806 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
807 size_t cacheSize;
808 fContext->getResourceCacheLimits(nullptr, &cacheSize);
809 if (bmpSize < cacheSize / 2) {
810 return false;
811 }
812
bsalomon1a1d0b82015-10-16 07:49:42 -0700813 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
814 // tiling memory savings would be < 50%.
robertphillipsd215a952016-05-05 12:03:29 -0700815 determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(),
816 fClip, viewMatrix, imageRect.size(), srcRectPtr,
reed85d91782015-09-10 14:33:38 -0700817 clippedSubset);
818 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
819 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
820 kBmpSmallTileSize * kBmpSmallTileSize;
821
822 return usedTileBytes < 2 * bmpSize;
823}
824
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000825bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800826 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000827 const GrTextureParams& params,
828 const SkRect* srcRectPtr,
829 int maxTileSize,
830 int* tileSize,
831 SkIRect* clippedSrcRect) const {
joshualittce894002016-01-11 13:29:31 -0800832 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000833 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700834 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000835 return false;
836 }
837
reed85d91782015-09-10 14:33:38 -0700838 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
839 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
840}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000841
reed85d91782015-09-10 14:33:38 -0700842bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
843 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
844 const SkMatrix& viewMatrix) const {
joshualittce894002016-01-11 13:29:31 -0800845 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700846 // if image is explictly texture backed then just use the texture
847 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000848 return false;
849 }
850
reed85d91782015-09-10 14:33:38 -0700851 GrTextureParams params;
852 bool doBicubic;
853 GrTextureParams::FilterMode textureFilterMode =
854 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
855
856 int tileFilterPad;
857 if (doBicubic) {
858 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
859 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
860 tileFilterPad = 0;
861 } else {
862 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000863 }
reed85d91782015-09-10 14:33:38 -0700864 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000865
bsalomon8c07b7a2015-11-02 11:36:52 -0800866 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000867
reed85d91782015-09-10 14:33:38 -0700868 // these are output, which we safely ignore, as we just want to know the predicate
869 int outTileSize;
870 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000871
reed85d91782015-09-10 14:33:38 -0700872 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
873 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000874}
875
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000876void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000877 const SkBitmap& bitmap,
878 const SkMatrix& m,
879 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800880 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800881 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800882 SkMatrix viewMatrix;
883 viewMatrix.setConcat(*origDraw.fMatrix, m);
884 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800885 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonb1b01992015-11-18 10:56:08 -0800886 // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
bsalomonf1ecd212015-12-09 17:06:02 -0800887 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
888 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800889 return;
890 }
bsalomonb1b01992015-11-18 10:56:08 -0800891 int maxTileSize = fContext->caps()->maxTileSize();
892
893 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
894 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
895 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
896 paint.isAntiAlias() &&
897 bitmap.width() <= maxTileSize &&
898 bitmap.height() <= maxTileSize;
899
900 bool skipTileCheck = drawAA || paint.getMaskFilter();
901
902 if (!skipTileCheck) {
903 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
904 int tileSize;
905 SkIRect clippedSrcRect;
906
907 GrTextureParams params;
908 bool doBicubic;
909 GrTextureParams::FilterMode textureFilterMode =
910 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
911 &doBicubic);
912
913 int tileFilterPad;
914
915 if (doBicubic) {
916 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
917 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
918 tileFilterPad = 0;
919 } else {
920 tileFilterPad = 1;
921 }
922 params.setFilterMode(textureFilterMode);
923
924 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
925 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
926 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
927 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
928 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
929 return;
930 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000931 }
bsalomonb1b01992015-11-18 10:56:08 -0800932 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800933 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
934 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000935}
936
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000937// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000938// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
939// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000940static inline void clamped_outset_with_offset(SkIRect* iRect,
941 int outset,
942 SkPoint* offset,
943 const SkIRect& clamp) {
944 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000945
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000946 int leftClampDelta = clamp.fLeft - iRect->fLeft;
947 if (leftClampDelta > 0) {
948 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000949 iRect->fLeft = clamp.fLeft;
950 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000951 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000952 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000953
954 int topClampDelta = clamp.fTop - iRect->fTop;
955 if (topClampDelta > 0) {
956 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000957 iRect->fTop = clamp.fTop;
958 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000959 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000960 }
961
962 if (iRect->fRight > clamp.fRight) {
963 iRect->fRight = clamp.fRight;
964 }
965 if (iRect->fBottom > clamp.fBottom) {
966 iRect->fBottom = clamp.fBottom;
967 }
968}
969
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000970// Break 'bitmap' into several tiles to draw it since it has already
971// been determined to be too large to fit in VRAM
972void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800973 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000974 const SkRect& srcRect,
975 const SkIRect& clippedSrcIRect,
976 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800977 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700978 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000979 int tileSize,
980 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800981 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800982
ericrk983294f2016-04-18 09:14:00 -0700983 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800984 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700985 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800986
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000987 // The following pixel lock is technically redundant, but it is desirable
988 // to lock outside of the tile loop to prevent redecoding the whole image
989 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
990 // is larger than the limit of the discardable memory pool.
991 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800992
993 const SkPaint* paint = &origPaint;
994 SkPaint tempPaint;
995 if (origPaint.isAntiAlias() && !fRenderTarget->isUnifiedMultisampled()) {
996 // Drop antialiasing to avoid seams at tile boundaries.
997 tempPaint = origPaint;
998 tempPaint.setAntiAlias(false);
999 paint = &tempPaint;
1000 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001001 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1002
1003 int nx = bitmap.width() / tileSize;
1004 int ny = bitmap.height() / tileSize;
1005 for (int x = 0; x <= nx; x++) {
1006 for (int y = 0; y <= ny; y++) {
1007 SkRect tileR;
1008 tileR.set(SkIntToScalar(x * tileSize),
1009 SkIntToScalar(y * tileSize),
1010 SkIntToScalar((x + 1) * tileSize),
1011 SkIntToScalar((y + 1) * tileSize));
1012
1013 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1014 continue;
1015 }
1016
1017 if (!tileR.intersect(srcRect)) {
1018 continue;
1019 }
1020
1021 SkBitmap tmpB;
1022 SkIRect iTileR;
1023 tileR.roundOut(&iTileR);
1024 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1025 SkIntToScalar(iTileR.fTop));
1026
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001027 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001028 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001029 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001030 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001031 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001032
robertphillipsec8bb942014-11-21 10:16:25 -08001033 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001034 SkIRect iClampRect;
1035
reeda5517e22015-07-14 10:54:12 -07001036 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001037 // In bleed mode we want to always expand the tile on all edges
1038 // but stay within the bitmap bounds
1039 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1040 } else {
1041 // In texture-domain/clamp mode we only want to expand the
1042 // tile on edges interior to "srcRect" (i.e., we want to
1043 // not bleed across the original clamped edges)
1044 srcRect.roundOut(&iClampRect);
1045 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001046 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1047 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001048 }
1049
1050 if (bitmap.extractSubset(&tmpB, iTileR)) {
1051 // now offset it to make it "local" to our tmp bitmap
1052 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001053 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001054 // de-optimized this determination
1055 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001056 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001057 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001058 tileR,
1059 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -08001060 *paint,
reeda5517e22015-07-14 10:54:12 -07001061 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001062 bicubic,
1063 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001064 }
1065 }
1066 }
1067}
1068
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001069/*
1070 * This is called by drawBitmap(), which has to handle images that may be too
1071 * large to be represented by a single texture.
1072 *
1073 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1074 * and that non-texture portion of the GrPaint has already been setup.
1075 */
1076void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001077 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001078 const SkRect& srcRect,
1079 const GrTextureParams& params,
1080 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001081 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001082 bool bicubic,
1083 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001084 // We should have already handled bitmaps larger than the max texture size.
1085 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1086 bitmap.height() <= fContext->caps()->maxTextureSize());
1087 // Unless the bitmap is inherently texture-backed, we should be respecting the max tile size
1088 // by the time we get here.
1089 SkASSERT(bitmap.getTexture() ||
1090 (bitmap.width() <= fContext->caps()->maxTileSize() &&
1091 bitmap.height() <= fContext->caps()->maxTileSize()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001092
1093 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001094 AutoBitmapTexture abt(fContext, bitmap, params, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001095 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001096 return;
1097 }
1098
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001099 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001100 SkRect paintRect;
1101 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1102 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1103 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1104 SkScalarMul(srcRect.fTop, hInv),
1105 SkScalarMul(srcRect.fRight, wInv),
1106 SkScalarMul(srcRect.fBottom, hInv));
1107
egdaniel79da63f2015-10-09 10:55:16 -07001108 SkMatrix texMatrix;
1109 texMatrix.reset();
1110 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1111 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1112 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1113 // Thus we need to pass in the transform matrix directly to the texture processor used for
1114 // the bitmap draw.
1115 texMatrix.setScale(wInv, hInv);
1116 }
1117
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001118 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001119
1120 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1121 // the rest from the SkPaint.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001122 SkAutoTUnref<const GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001123
reeda5517e22015-07-14 10:54:12 -07001124 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001125 // Use a constrained texture domain to avoid color bleeding
1126 SkScalar left, top, right, bottom;
1127 if (srcRect.width() > SK_Scalar1) {
1128 SkScalar border = SK_ScalarHalf / texture->width();
1129 left = paintRect.left() + border;
1130 right = paintRect.right() - border;
1131 } else {
1132 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1133 }
1134 if (srcRect.height() > SK_Scalar1) {
1135 SkScalar border = SK_ScalarHalf / texture->height();
1136 top = paintRect.top() + border;
1137 bottom = paintRect.bottom() - border;
1138 } else {
1139 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1140 }
1141 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001142 if (bicubic) {
egdaniel79da63f2015-10-09 10:55:16 -07001143 fp.reset(GrBicubicEffect::Create(texture, texMatrix, textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001144 } else {
bsalomon4a339522015-10-06 08:40:50 -07001145 fp.reset(GrTextureDomainEffect::Create(texture,
egdaniel79da63f2015-10-09 10:55:16 -07001146 texMatrix,
joshualitt5531d512014-12-17 15:50:11 -08001147 textureDomain,
1148 GrTextureDomain::kClamp_Mode,
1149 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001150 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001151 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001152 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1153 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
egdaniel79da63f2015-10-09 10:55:16 -07001154 fp.reset(GrBicubicEffect::Create(texture, texMatrix, tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001155 } else {
egdaniel79da63f2015-10-09 10:55:16 -07001156 fp.reset(GrSimpleTextureEffect::Create(texture, texMatrix, params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001157 }
1158
joshualitt33a5fce2015-11-18 13:28:51 -08001159 GrPaint grPaint;
1160 if (!SkPaintToGrPaintWithTexture(this->context(), paint, viewMatrix, fp,
brianosman898235c2016-04-06 07:38:23 -07001161 kAlpha_8_SkColorType == bitmap.colorType(),
brianosmanb461d342016-04-13 13:10:14 -07001162 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001163 return;
1164 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001165
egdaniel79da63f2015-10-09 10:55:16 -07001166 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1167 // We don't have local coords in this case and have previously set the transform
1168 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001169 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001170 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001171 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001172 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001173}
1174
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001175void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1176 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001177 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001178 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001179 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001180
1181 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1182 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1183 return;
1184 }
1185
robertphillipse1849d12016-03-17 08:26:38 -07001186 int offX = bitmap.pixelRefOrigin().fX;
1187 int offY = bitmap.pixelRefOrigin().fY;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001188 int w = bitmap.width();
1189 int h = bitmap.height();
1190
1191 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001192 // draw sprite neither filters nor tiles.
1193 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001194 if (!texture) {
1195 return;
1196 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001197
bsalomonf1b7a1d2015-09-28 06:26:28 -07001198 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
1199
senorblancof35566e2016-04-18 10:32:02 -07001200 SkASSERT(!paint.getImageFilter());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001201
1202 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001203 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001204 GrSimpleTextureEffect::Create(texture, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001205 if (alphaOnly) {
1206 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1207 } else {
1208 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1209 }
brianosman898235c2016-04-06 07:38:23 -07001210 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp,
brianosmanb461d342016-04-13 13:10:14 -07001211 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001212 return;
1213 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001214
bsalomona2e69fc2015-11-05 10:41:43 -08001215 fDrawContext->fillRectToRect(fClip,
1216 grPaint,
1217 SkMatrix::I(),
1218 SkRect::MakeXYWH(SkIntToScalar(left),
1219 SkIntToScalar(top),
1220 SkIntToScalar(w),
1221 SkIntToScalar(h)),
robertphillipse1849d12016-03-17 08:26:38 -07001222 SkRect::MakeXYWH(SkIntToScalar(offX) / texture->width(),
1223 SkIntToScalar(offY) / texture->height(),
1224 SkIntToScalar(w) / texture->width(),
1225 SkIntToScalar(h) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001226}
1227
bsalomonb1b01992015-11-18 10:56:08 -08001228void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1229 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001230 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001231 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001232 CHECK_SHOULD_DRAW(draw);
bsalomonb1b01992015-11-18 10:56:08 -08001233 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001234 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001235 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
1236 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001237 return;
1238 }
bsalomonb1b01992015-11-18 10:56:08 -08001239 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1240 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1241 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1242 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1243 const SkRect* dst = &origDst;
1244 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001245 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001246 if (!src) {
1247 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001248 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001249
bsalomonb1b01992015-11-18 10:56:08 -08001250 SkMatrix srcToDstMatrix;
1251 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1252 return;
1253 }
1254 SkRect tmpSrc, tmpDst;
1255 if (src != &bmpBounds) {
1256 if (!bmpBounds.contains(*src)) {
1257 tmpSrc = *src;
1258 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001259 return; // nothing to draw
1260 }
bsalomonb1b01992015-11-18 10:56:08 -08001261 src = &tmpSrc;
1262 srcToDstMatrix.mapRect(&tmpDst, *src);
1263 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001264 }
1265 }
1266
bsalomonb1b01992015-11-18 10:56:08 -08001267 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001268
bsalomonb1b01992015-11-18 10:56:08 -08001269 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1270 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
1271 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
1272 paint.isAntiAlias() &&
1273 bitmap.width() <= maxTileSize &&
1274 bitmap.height() <= maxTileSize;
1275
1276 bool skipTileCheck = drawAA || paint.getMaskFilter();
1277
1278 if (!skipTileCheck) {
1279 int tileSize;
1280 SkIRect clippedSrcRect;
1281
1282 GrTextureParams params;
1283 bool doBicubic;
1284 GrTextureParams::FilterMode textureFilterMode =
1285 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1286 &doBicubic);
1287
1288 int tileFilterPad;
1289
1290 if (doBicubic) {
1291 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1292 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1293 tileFilterPad = 0;
1294 } else {
1295 tileFilterPad = 1;
1296 }
1297 params.setFilterMode(textureFilterMode);
1298
1299 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1300 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1301 // we have a mask filter.
1302 SkMatrix viewMatrix = *draw.fMatrix;
1303 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1304 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1305 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1306 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1307 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1308 constraint, tileSize, doBicubic);
1309 return;
1310 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001311 }
bsalomonb1b01992015-11-18 10:56:08 -08001312 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001313 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001314}
1315
1316void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1317 int x, int y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001318 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001319 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001320 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001321 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001322
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001323 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001324 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001325
1326 GrRenderTarget* devRT = dev->accessRenderTarget();
1327 GrTexture* devTex;
halcanary96fcdcc2015-08-27 07:41:13 -07001328 if (nullptr == (devTex = devRT->asTexture())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001329 return;
1330 }
1331
robertphillips7b9e8a42014-12-11 08:20:31 -08001332 const SkImageInfo ii = dev->imageInfo();
1333 int w = ii.width();
1334 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001335
senorblancof35566e2016-04-18 10:32:02 -07001336 SkASSERT(!paint.getImageFilter());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001337
1338 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001339 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001340 GrSimpleTextureEffect::Create(devTex, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001341 if (GrPixelConfigIsAlphaOnly(devTex->config())) {
1342 // Can this happen?
1343 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1344 } else {
1345 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1346 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001347
brianosman898235c2016-04-06 07:38:23 -07001348 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp,
brianosmanb461d342016-04-13 13:10:14 -07001349 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001350 return;
1351 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001352
1353 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1354 SkIntToScalar(y),
1355 SkIntToScalar(w),
1356 SkIntToScalar(h));
1357
1358 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1359 // scratch texture).
1360 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1361 SK_Scalar1 * h / devTex->height());
1362
bsalomona2e69fc2015-11-05 10:41:43 -08001363 fDrawContext->fillRectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001364}
1365
reeda85d4d02015-05-06 12:56:48 -07001366void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1367 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001368 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001369 SkMatrix viewMatrix = *draw.fMatrix;
1370 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001371 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001372 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001373 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001374 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1375 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001376 return;
reed85d91782015-09-10 14:33:38 -07001377 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001378 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001379 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1380 paint.getFilterQuality(), *draw.fMatrix)) {
1381 // only support tiling as bitmap at the moment, so force raster-version
1382 if (!as_IB(image)->getROPixels(&bm)) {
1383 return;
1384 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001385 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1386 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1387 CHECK_SHOULD_DRAW(draw);
1388 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001389 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1390 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001391 } else if (as_IB(image)->getROPixels(&bm)) {
1392 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001393 }
reeda85d4d02015-05-06 12:56:48 -07001394 }
1395}
1396
1397void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001398 const SkRect& dst, const SkPaint& paint,
1399 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001400 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001401 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001402 CHECK_SHOULD_DRAW(draw);
1403 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001404 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001405 return;
1406 }
1407 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001408 SkMatrix totalMatrix = *draw.fMatrix;
1409 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1410 dst.height() / (src ? src->height() : image->height()));
1411 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001412 // only support tiling as bitmap at the moment, so force raster-version
1413 if (!as_IB(image)->getROPixels(&bm)) {
1414 return;
1415 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001416 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1417 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1418 CHECK_SHOULD_DRAW(draw);
1419 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001420 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001421 } else if (as_IB(image)->getROPixels(&bm)) {
1422 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001423 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001424}
1425
bsalomonf1ecd212015-12-09 17:06:02 -08001426void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001427 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001428 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001429
joshualitt33a5fce2015-11-18 13:28:51 -08001430 CHECK_SHOULD_DRAW(draw);
1431
joshualittedb36442015-11-19 14:29:30 -08001432 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
1433 fRenderTarget->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001434 bool doBicubic;
1435 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001436 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1437 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001438 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001439 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001440
1441 SkRect srcR, dstR;
1442 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001443 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001444 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001445 }
1446 return;
1447 }
1448
bsalomon2bbd0c62015-12-09 12:50:56 -08001449 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
1450 SkAutoTUnref<const GrFragmentProcessor> fp(
1451 producer->createFragmentProcessor(SkMatrix::I(),
1452 SkRect::MakeIWH(producer->width(), producer->height()),
1453 GrTextureProducer::kNo_FilterConstraint, true,
1454 &kMode));
joshualitt33a5fce2015-11-18 13:28:51 -08001455 GrPaint grPaint;
1456 if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, fp,
brianosman898235c2016-04-06 07:38:23 -07001457 producer->isAlphaOnly(),
brianosmanb461d342016-04-13 13:10:14 -07001458 this->surfaceProps().isGammaCorrect(), &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001459 return;
1460 }
1461
bsalomon2bbd0c62015-12-09 12:50:56 -08001462 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1463 producer->height(), center, dst);
1464}
1465
1466void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1467 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001468 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001469 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001470 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001471 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001472 } else {
1473 SkBitmap bm;
1474 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1475 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001476 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001477 } else if (as_IB(image)->getROPixels(&bm)) {
1478 this->drawBitmapNine(draw, bm, center, dst, paint);
1479 }
1480 }
1481}
1482
1483void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1484 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001485 ASSERT_SINGLE_OWNER
bsalomon2bbd0c62015-12-09 12:50:56 -08001486 if (bitmap.getTexture()) {
1487 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001488 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001489 } else {
1490 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001491 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001492 }
joshualitt33a5fce2015-11-18 13:28:51 -08001493}
1494
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001495///////////////////////////////////////////////////////////////////////////////
1496
1497// must be in SkCanvas::VertexMode order
1498static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1499 kTriangles_GrPrimitiveType,
1500 kTriangleStrip_GrPrimitiveType,
1501 kTriangleFan_GrPrimitiveType,
1502};
1503
1504void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1505 int vertexCount, const SkPoint vertices[],
1506 const SkPoint texs[], const SkColor colors[],
1507 SkXfermode* xmode,
1508 const uint16_t indices[], int indexCount,
1509 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001510 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001511 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001512 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001513
halcanary96fcdcc2015-08-27 07:41:13 -07001514 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1515 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001516
halcanary96fcdcc2015-08-27 07:41:13 -07001517 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001518
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001519 SkPaint copy(paint);
1520 copy.setStyle(SkPaint::kStroke_Style);
1521 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001522
bsalomonf1b7a1d2015-09-28 06:26:28 -07001523 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001524 // we ignore the shader if texs is null.
brianosman898235c2016-04-06 07:38:23 -07001525 if (!SkPaintToGrPaintNoShader(this->context(), copy,
brianosmanb461d342016-04-13 13:10:14 -07001526 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001527 return;
1528 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001529
dandov32a311b2014-07-15 19:46:26 -07001530 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001531 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001532 switch (vmode) {
1533 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001534 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001535 break;
1536 case SkCanvas::kTriangleStrip_VertexMode:
1537 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001538 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001539 break;
1540 }
mtklein533eb782014-08-27 10:39:42 -07001541
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001542 VertState state(vertexCount, indices, indexCount);
1543 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001544
dandov32a311b2014-07-15 19:46:26 -07001545 //number of indices for lines per triangle with kLines
1546 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001547
bsalomonf1b7a1d2015-09-28 06:26:28 -07001548 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001549 int i = 0;
1550 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001551 lineIndices[i] = state.f0;
1552 lineIndices[i + 1] = state.f1;
1553 lineIndices[i + 2] = state.f1;
1554 lineIndices[i + 3] = state.f2;
1555 lineIndices[i + 4] = state.f2;
1556 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001557 i += 6;
1558 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001559 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001560 grPaint,
1561 *draw.fMatrix,
1562 kLines_GrPrimitiveType,
1563 vertexCount,
1564 vertices,
1565 texs,
1566 colors,
1567 lineIndices.get(),
1568 indexCount);
1569 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001570 }
1571
bsalomonf1b7a1d2015-09-28 06:26:28 -07001572 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001573
1574 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001575 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001576 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1577 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001578 convertedColors.reset(vertexCount);
1579 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001580 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001581 }
1582 colors = convertedColors.get();
1583 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001584 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001585 if (texs && paint.getShader()) {
1586 if (colors) {
1587 // When there are texs and colors the shader and colors are combined using xmode. A null
1588 // xmode is defined to mean modulate.
1589 SkXfermode::Mode colorMode;
1590 if (xmode) {
1591 if (!xmode->asMode(&colorMode)) {
1592 return;
1593 }
1594 } else {
1595 colorMode = SkXfermode::kModulate_Mode;
1596 }
1597 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
brianosmanb461d342016-04-13 13:10:14 -07001598 false, this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001599 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001600 return;
1601 }
1602 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001603 // We have a shader, but no colors to blend it against.
brianosman898235c2016-04-06 07:38:23 -07001604 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001605 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001606 return;
1607 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001608 }
bsalomonaa48d362015-10-01 08:34:17 -07001609 } else {
1610 if (colors) {
1611 // We have colors, but either have no shader or no texture coords (which implies that
1612 // we should ignore the shader).
brianosman898235c2016-04-06 07:38:23 -07001613 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001614 this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001615 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001616 return;
1617 }
1618 } else {
1619 // No colors and no shaders. Just draw with the paint color.
brianosman898235c2016-04-06 07:38:23 -07001620 if (!SkPaintToGrPaintNoShader(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001621 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001622 return;
1623 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001624 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001625 }
1626
robertphillips2e1e51f2015-10-15 08:01:48 -07001627 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001628 grPaint,
1629 *draw.fMatrix,
1630 primType,
1631 vertexCount,
1632 vertices,
1633 texs,
1634 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001635 indices,
robertphillipsea461502015-05-26 11:38:03 -07001636 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001637}
1638
1639///////////////////////////////////////////////////////////////////////////////
1640
jvanverth31ff7622015-08-07 10:09:28 -07001641void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001642 const SkRect texRect[], const SkColor colors[], int count,
1643 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001644 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001645 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001646 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001647 return;
1648 }
1649
jvanverth31ff7622015-08-07 10:09:28 -07001650 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001651 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001652
reedca109532015-06-25 16:25:25 -07001653 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001654 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001655
jvanverth31ff7622015-08-07 10:09:28 -07001656 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001657 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001658 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
brianosmanb461d342016-04-13 13:10:14 -07001659 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001660 return;
1661 }
1662 } else {
brianosman898235c2016-04-06 07:38:23 -07001663 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001664 this->surfaceProps().isGammaCorrect(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001665 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001666 }
1667 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001668
1669 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001670 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001671}
1672
1673///////////////////////////////////////////////////////////////////////////////
1674
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001675void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001676 size_t byteLength, SkScalar x, SkScalar y,
1677 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001678 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001679 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001680 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001681
jvanverth8c27a182014-10-14 08:45:50 -07001682 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001683 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001684 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001685 return;
1686 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001687
jvanverth8c27a182014-10-14 08:45:50 -07001688 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001689
robertphillips2e1e51f2015-10-15 08:01:48 -07001690 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001691 (const char *)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001692}
1693
fmalita05c4a432014-09-29 06:29:53 -07001694void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1695 const SkScalar pos[], int scalarsPerPos,
1696 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001697 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001698 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001699 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001700
jvanverth8c27a182014-10-14 08:45:50 -07001701 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001702 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001703 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001704 return;
1705 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001706
jvanverth8c27a182014-10-14 08:45:50 -07001707 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001708
robertphillips2e1e51f2015-10-15 08:01:48 -07001709 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001710 (const char *)text, byteLength, pos, scalarsPerPos, offset,
reed1e7f5e72016-04-27 07:49:17 -07001711 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001712}
1713
joshualitt9c328182015-03-23 08:13:04 -07001714void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1715 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001716 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001717 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001718 CHECK_SHOULD_DRAW(draw);
1719
1720 SkDEBUGCODE(this->validate();)
1721
robertphillips2e1e51f2015-10-15 08:01:48 -07001722 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001723 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001724}
1725
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001726///////////////////////////////////////////////////////////////////////////////
1727
reedb2db8982014-11-13 12:41:02 -08001728bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001729 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001730}
1731
1732void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001733 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001734
bsalomonc49e8682015-06-30 11:37:35 -07001735 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001736}
1737
1738///////////////////////////////////////////////////////////////////////////////
1739
reed76033be2015-03-14 10:54:31 -07001740SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001741 ASSERT_SINGLE_OWNER
bsalomonf2703d82014-10-28 14:33:06 -07001742 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001743 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001744 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001745 desc.fWidth = cinfo.fInfo.width();
1746 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001747 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001748
1749 SkAutoTUnref<GrTexture> texture;
1750 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001751 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001752
hcm4396fa52014-10-27 21:43:30 -07001753 // layers are never draw in repeat modes, so we can request an approx
1754 // match and ignore any padding.
bsalomoneae62002015-07-31 13:59:30 -07001755 if (kNever_TileUsage == cinfo.fTileUsage) {
1756 texture.reset(fContext->textureProvider()->createApproxTexture(desc));
1757 } else {
bsalomon5ec26ae2016-02-25 08:33:02 -08001758 texture.reset(fContext->textureProvider()->createTexture(desc, SkBudgeted::kYes));
bsalomoneae62002015-07-31 13:59:30 -07001759 }
bsalomonafe30052015-01-16 07:32:33 -08001760
1761 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001762 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
robertphillips24e91282016-04-29 06:46:36 -07001763 return SkGpuDevice::Make(sk_ref_sp(texture->asRenderTarget()),
1764 cinfo.fInfo.width(), cinfo.fInfo.height(),
1765 &props, init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001766 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001767 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001768 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001769 cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary96fcdcc2015-08-27 07:41:13 -07001770 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001771 }
1772}
1773
reede8f30622016-03-23 18:59:25 -07001774sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001775 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001776 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001777 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
robertphillipsd215a952016-05-05 12:03:29 -07001778 return SkSurface::MakeRenderTarget(fContext, kBudgeted, info, fDrawContext->numColorSamples(),
reede8f30622016-03-23 18:59:25 -07001779 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001780}
1781
robertphillips30d2cc62014-09-24 08:52:18 -07001782bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001783 const SkMatrix* matrix, const SkPaint* paint) {
joshualittce894002016-01-11 13:29:31 -08001784 ASSERT_SINGLE_OWNER
robertphillips63242d72014-12-04 08:31:02 -08001785#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001786 // todo: should handle this natively
robertphillips6b53b9d2016-05-06 07:16:18 -07001787 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001788 return false;
1789 }
1790
halcanary96fcdcc2015-08-27 07:41:13 -07001791 const SkBigPicture::AccelData* data = nullptr;
mtklein9db912c2015-05-19 11:11:26 -07001792 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1793 data = bp->accelData();
1794 }
robertphillips81f71b62014-11-11 04:54:49 -08001795 if (!data) {
1796 return false;
1797 }
1798
robertphillipse5524cd2015-02-20 12:30:26 -08001799 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1800 if (0 == gpuData->numBlocks()) {
1801 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001802 }
1803
robertphillipsfd61ed02014-10-28 07:21:44 -07001804 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001805
robertphillipse5524cd2015-02-20 12:30:26 -08001806 SkIRect iBounds;
1807 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1808 return false;
1809 }
1810
1811 SkRect clipBounds = SkRect::Make(iBounds);
1812
1813 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1814
robertphillips60029a52015-11-09 13:51:06 -08001815 GrLayerHoister::Begin(fContext);
1816
robertphillipsfd61ed02014-10-28 07:21:44 -07001817 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001818 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001819 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001820 &atlasedNeedRendering, &atlasedRecycled,
robertphillipsd215a952016-05-05 12:03:29 -07001821 fDrawContext->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001822
1823 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1824
1825 SkTDArray<GrHoistedLayer> needRendering, recycled;
1826
robertphillipse5524cd2015-02-20 12:30:26 -08001827 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1828
robertphillipsfd61ed02014-10-28 07:21:44 -07001829 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001830 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001831 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001832 &needRendering, &recycled,
robertphillipsd215a952016-05-05 12:03:29 -07001833 fDrawContext->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001834
1835 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001836
robertphillips64bf7672014-08-21 13:07:35 -07001837 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001838 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
halcanary96fcdcc2015-08-27 07:41:13 -07001839 initialMatrix, nullptr);
robertphillips64bf7672014-08-21 13:07:35 -07001840
robertphillipsfd61ed02014-10-28 07:21:44 -07001841 GrLayerHoister::UnlockLayers(fContext, needRendering);
1842 GrLayerHoister::UnlockLayers(fContext, recycled);
1843 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1844 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips60029a52015-11-09 13:51:06 -08001845 GrLayerHoister::End(fContext);
robertphillips64bf7672014-08-21 13:07:35 -07001846
1847 return true;
robertphillips63242d72014-12-04 08:31:02 -08001848#else
1849 return false;
1850#endif
robertphillips64bf7672014-08-21 13:07:35 -07001851}
1852
senorblanco900c3672016-04-27 11:31:23 -07001853SkImageFilterCache* SkGpuDevice::getImageFilterCache() {
joshualittce894002016-01-11 13:29:31 -08001854 ASSERT_SINGLE_OWNER
senorblanco55b6d8b2014-07-30 11:26:46 -07001855 // We always return a transient cache, so it is freed after each
1856 // filter traversal.
senorblanco900c3672016-04-27 11:31:23 -07001857 return SkImageFilterCache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001858}
reedf037e0b2014-10-30 11:34:15 -07001859
1860#endif