blob: ba5848b4b11996b40e3fe974f27802b76b85bc81 [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.
robertphillips524484b2016-05-06 07:55:19 -0700747static void determine_clipped_src_rect(const GrRenderTarget* rt,
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) {
robertphillips524484b2016-05-06 07:55:19 -0700753 clip.getConservativeBounds(rt->width(), rt->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) {
robertphillips524484b2016-05-06 07:55:19 -0700786 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(),
reed85d91782015-09-10 14:33:38 -0700787 srcRectPtr, clippedSubset);
788 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
789 return true;
790 }
791
bsalomon1a1d0b82015-10-16 07:49:42 -0700792 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700793 const size_t area = imageRect.width() * imageRect.height();
794 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
795 return false;
796 }
797
reed85d91782015-09-10 14:33:38 -0700798 // At this point we know we could do the draw by uploading the entire bitmap
799 // as a texture. However, if the texture would be large compared to the
800 // cache size and we don't require most of it for this draw then tile to
801 // reduce the amount of upload and cache spill.
802
803 // assumption here is that sw bitmap size is a good proxy for its size as
804 // a texture
805 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
806 size_t cacheSize;
807 fContext->getResourceCacheLimits(nullptr, &cacheSize);
808 if (bmpSize < cacheSize / 2) {
809 return false;
810 }
811
bsalomon1a1d0b82015-10-16 07:49:42 -0700812 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
813 // tiling memory savings would be < 50%.
robertphillips524484b2016-05-06 07:55:19 -0700814 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(), srcRectPtr,
reed85d91782015-09-10 14:33:38 -0700815 clippedSubset);
816 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
817 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
818 kBmpSmallTileSize * kBmpSmallTileSize;
819
820 return usedTileBytes < 2 * bmpSize;
821}
822
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000823bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800824 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000825 const GrTextureParams& params,
826 const SkRect* srcRectPtr,
827 int maxTileSize,
828 int* tileSize,
829 SkIRect* clippedSrcRect) const {
joshualittce894002016-01-11 13:29:31 -0800830 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000831 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700832 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000833 return false;
834 }
835
reed85d91782015-09-10 14:33:38 -0700836 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
837 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
838}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000839
reed85d91782015-09-10 14:33:38 -0700840bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
841 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
842 const SkMatrix& viewMatrix) const {
joshualittce894002016-01-11 13:29:31 -0800843 ASSERT_SINGLE_OWNER
reed85d91782015-09-10 14:33:38 -0700844 // if image is explictly texture backed then just use the texture
845 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000846 return false;
847 }
848
reed85d91782015-09-10 14:33:38 -0700849 GrTextureParams params;
850 bool doBicubic;
851 GrTextureParams::FilterMode textureFilterMode =
852 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
853
854 int tileFilterPad;
855 if (doBicubic) {
856 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
857 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
858 tileFilterPad = 0;
859 } else {
860 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000861 }
reed85d91782015-09-10 14:33:38 -0700862 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000863
bsalomon8c07b7a2015-11-02 11:36:52 -0800864 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000865
reed85d91782015-09-10 14:33:38 -0700866 // these are output, which we safely ignore, as we just want to know the predicate
867 int outTileSize;
868 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000869
reed85d91782015-09-10 14:33:38 -0700870 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
871 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000872}
873
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000874void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000875 const SkBitmap& bitmap,
876 const SkMatrix& m,
877 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -0800878 ASSERT_SINGLE_OWNER
bsalomonb1b01992015-11-18 10:56:08 -0800879 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800880 SkMatrix viewMatrix;
881 viewMatrix.setConcat(*origDraw.fMatrix, m);
882 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800883 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonb1b01992015-11-18 10:56:08 -0800884 // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
bsalomonf1ecd212015-12-09 17:06:02 -0800885 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
886 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800887 return;
888 }
bsalomonb1b01992015-11-18 10:56:08 -0800889 int maxTileSize = fContext->caps()->maxTileSize();
890
891 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
892 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
893 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
894 paint.isAntiAlias() &&
895 bitmap.width() <= maxTileSize &&
896 bitmap.height() <= maxTileSize;
897
898 bool skipTileCheck = drawAA || paint.getMaskFilter();
899
900 if (!skipTileCheck) {
901 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
902 int tileSize;
903 SkIRect clippedSrcRect;
904
905 GrTextureParams params;
906 bool doBicubic;
907 GrTextureParams::FilterMode textureFilterMode =
908 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
909 &doBicubic);
910
911 int tileFilterPad;
912
913 if (doBicubic) {
914 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
915 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
916 tileFilterPad = 0;
917 } else {
918 tileFilterPad = 1;
919 }
920 params.setFilterMode(textureFilterMode);
921
922 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
923 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
924 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
925 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
926 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
927 return;
928 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000929 }
bsalomonb1b01992015-11-18 10:56:08 -0800930 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800931 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
932 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933}
934
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000935// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000936// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
937// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000938static inline void clamped_outset_with_offset(SkIRect* iRect,
939 int outset,
940 SkPoint* offset,
941 const SkIRect& clamp) {
942 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000943
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000944 int leftClampDelta = clamp.fLeft - iRect->fLeft;
945 if (leftClampDelta > 0) {
946 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000947 iRect->fLeft = clamp.fLeft;
948 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000949 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000950 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000951
952 int topClampDelta = clamp.fTop - iRect->fTop;
953 if (topClampDelta > 0) {
954 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000955 iRect->fTop = clamp.fTop;
956 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000957 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958 }
959
960 if (iRect->fRight > clamp.fRight) {
961 iRect->fRight = clamp.fRight;
962 }
963 if (iRect->fBottom > clamp.fBottom) {
964 iRect->fBottom = clamp.fBottom;
965 }
966}
967
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000968// Break 'bitmap' into several tiles to draw it since it has already
969// been determined to be too large to fit in VRAM
970void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800971 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000972 const SkRect& srcRect,
973 const SkIRect& clippedSrcIRect,
974 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800975 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700976 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000977 int tileSize,
978 bool bicubic) {
joshualittce894002016-01-11 13:29:31 -0800979 ASSERT_SINGLE_OWNER
ericrk369e9372016-02-05 15:32:36 -0800980
ericrk983294f2016-04-18 09:14:00 -0700981 // This is the funnel for all paths that draw tiled bitmaps/images. Log histogram entries.
ericrk369e9372016-02-05 15:32:36 -0800982 SK_HISTOGRAM_BOOLEAN("DrawTiled", true);
ericrk983294f2016-04-18 09:14:00 -0700983 LogDrawScaleFactor(viewMatrix, origPaint.getFilterQuality());
ericrk369e9372016-02-05 15:32:36 -0800984
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000985 // The following pixel lock is technically redundant, but it is desirable
986 // to lock outside of the tile loop to prevent redecoding the whole image
987 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
988 // is larger than the limit of the discardable memory pool.
989 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800990
991 const SkPaint* paint = &origPaint;
992 SkPaint tempPaint;
993 if (origPaint.isAntiAlias() && !fRenderTarget->isUnifiedMultisampled()) {
994 // Drop antialiasing to avoid seams at tile boundaries.
995 tempPaint = origPaint;
996 tempPaint.setAntiAlias(false);
997 paint = &tempPaint;
998 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000999 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1000
1001 int nx = bitmap.width() / tileSize;
1002 int ny = bitmap.height() / tileSize;
1003 for (int x = 0; x <= nx; x++) {
1004 for (int y = 0; y <= ny; y++) {
1005 SkRect tileR;
1006 tileR.set(SkIntToScalar(x * tileSize),
1007 SkIntToScalar(y * tileSize),
1008 SkIntToScalar((x + 1) * tileSize),
1009 SkIntToScalar((y + 1) * tileSize));
1010
1011 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1012 continue;
1013 }
1014
1015 if (!tileR.intersect(srcRect)) {
1016 continue;
1017 }
1018
1019 SkBitmap tmpB;
1020 SkIRect iTileR;
1021 tileR.roundOut(&iTileR);
1022 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1023 SkIntToScalar(iTileR.fTop));
1024
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001025 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001026 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001027 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001028 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001029 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001030
robertphillipsec8bb942014-11-21 10:16:25 -08001031 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001032 SkIRect iClampRect;
1033
reeda5517e22015-07-14 10:54:12 -07001034 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001035 // In bleed mode we want to always expand the tile on all edges
1036 // but stay within the bitmap bounds
1037 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1038 } else {
1039 // In texture-domain/clamp mode we only want to expand the
1040 // tile on edges interior to "srcRect" (i.e., we want to
1041 // not bleed across the original clamped edges)
1042 srcRect.roundOut(&iClampRect);
1043 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001044 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1045 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001046 }
1047
1048 if (bitmap.extractSubset(&tmpB, iTileR)) {
1049 // now offset it to make it "local" to our tmp bitmap
1050 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001051 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -08001052 // de-optimized this determination
1053 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001054 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001055 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001056 tileR,
1057 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -08001058 *paint,
reeda5517e22015-07-14 10:54:12 -07001059 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001060 bicubic,
1061 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001062 }
1063 }
1064 }
1065}
1066
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001067/*
1068 * This is called by drawBitmap(), which has to handle images that may be too
1069 * large to be represented by a single texture.
1070 *
1071 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1072 * and that non-texture portion of the GrPaint has already been setup.
1073 */
1074void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001075 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001076 const SkRect& srcRect,
1077 const GrTextureParams& params,
1078 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001079 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001080 bool bicubic,
1081 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001082 // We should have already handled bitmaps larger than the max texture size.
1083 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1084 bitmap.height() <= fContext->caps()->maxTextureSize());
1085 // Unless the bitmap is inherently texture-backed, we should be respecting the max tile size
1086 // by the time we get here.
1087 SkASSERT(bitmap.getTexture() ||
1088 (bitmap.width() <= fContext->caps()->maxTileSize() &&
1089 bitmap.height() <= fContext->caps()->maxTileSize()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001090
1091 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001092 AutoBitmapTexture abt(fContext, bitmap, params, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001093 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001094 return;
1095 }
1096
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001097 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001098 SkRect paintRect;
1099 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1100 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1101 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1102 SkScalarMul(srcRect.fTop, hInv),
1103 SkScalarMul(srcRect.fRight, wInv),
1104 SkScalarMul(srcRect.fBottom, hInv));
1105
egdaniel79da63f2015-10-09 10:55:16 -07001106 SkMatrix texMatrix;
1107 texMatrix.reset();
1108 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1109 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1110 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1111 // Thus we need to pass in the transform matrix directly to the texture processor used for
1112 // the bitmap draw.
1113 texMatrix.setScale(wInv, hInv);
1114 }
1115
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001116 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001117
1118 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1119 // the rest from the SkPaint.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001120 SkAutoTUnref<const GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001121
reeda5517e22015-07-14 10:54:12 -07001122 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001123 // Use a constrained texture domain to avoid color bleeding
1124 SkScalar left, top, right, bottom;
1125 if (srcRect.width() > SK_Scalar1) {
1126 SkScalar border = SK_ScalarHalf / texture->width();
1127 left = paintRect.left() + border;
1128 right = paintRect.right() - border;
1129 } else {
1130 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1131 }
1132 if (srcRect.height() > SK_Scalar1) {
1133 SkScalar border = SK_ScalarHalf / texture->height();
1134 top = paintRect.top() + border;
1135 bottom = paintRect.bottom() - border;
1136 } else {
1137 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1138 }
1139 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001140 if (bicubic) {
egdaniel79da63f2015-10-09 10:55:16 -07001141 fp.reset(GrBicubicEffect::Create(texture, texMatrix, textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001142 } else {
bsalomon4a339522015-10-06 08:40:50 -07001143 fp.reset(GrTextureDomainEffect::Create(texture,
egdaniel79da63f2015-10-09 10:55:16 -07001144 texMatrix,
joshualitt5531d512014-12-17 15:50:11 -08001145 textureDomain,
1146 GrTextureDomain::kClamp_Mode,
1147 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001148 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001149 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001150 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1151 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
egdaniel79da63f2015-10-09 10:55:16 -07001152 fp.reset(GrBicubicEffect::Create(texture, texMatrix, tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001153 } else {
egdaniel79da63f2015-10-09 10:55:16 -07001154 fp.reset(GrSimpleTextureEffect::Create(texture, texMatrix, params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001155 }
1156
joshualitt33a5fce2015-11-18 13:28:51 -08001157 GrPaint grPaint;
1158 if (!SkPaintToGrPaintWithTexture(this->context(), paint, viewMatrix, fp,
brianosman898235c2016-04-06 07:38:23 -07001159 kAlpha_8_SkColorType == bitmap.colorType(),
brianosmanb461d342016-04-13 13:10:14 -07001160 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001161 return;
1162 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001163
egdaniel79da63f2015-10-09 10:55:16 -07001164 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1165 // We don't have local coords in this case and have previously set the transform
1166 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001167 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001168 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001169 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001170 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001171}
1172
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001173void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1174 int left, int top, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001175 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001176 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001177 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001178
1179 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1180 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1181 return;
1182 }
1183
robertphillipse1849d12016-03-17 08:26:38 -07001184 int offX = bitmap.pixelRefOrigin().fX;
1185 int offY = bitmap.pixelRefOrigin().fY;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001186 int w = bitmap.width();
1187 int h = bitmap.height();
1188
1189 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001190 // draw sprite neither filters nor tiles.
1191 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001192 if (!texture) {
1193 return;
1194 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195
bsalomonf1b7a1d2015-09-28 06:26:28 -07001196 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
1197
senorblancof35566e2016-04-18 10:32:02 -07001198 SkASSERT(!paint.getImageFilter());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001199
1200 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001201 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001202 GrSimpleTextureEffect::Create(texture, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001203 if (alphaOnly) {
1204 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1205 } else {
1206 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1207 }
brianosman898235c2016-04-06 07:38:23 -07001208 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp,
brianosmanb461d342016-04-13 13:10:14 -07001209 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001210 return;
1211 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001212
bsalomona2e69fc2015-11-05 10:41:43 -08001213 fDrawContext->fillRectToRect(fClip,
1214 grPaint,
1215 SkMatrix::I(),
1216 SkRect::MakeXYWH(SkIntToScalar(left),
1217 SkIntToScalar(top),
1218 SkIntToScalar(w),
1219 SkIntToScalar(h)),
robertphillipse1849d12016-03-17 08:26:38 -07001220 SkRect::MakeXYWH(SkIntToScalar(offX) / texture->width(),
1221 SkIntToScalar(offY) / texture->height(),
1222 SkIntToScalar(w) / texture->width(),
1223 SkIntToScalar(h) / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001224}
1225
bsalomonb1b01992015-11-18 10:56:08 -08001226void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1227 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001228 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001229 ASSERT_SINGLE_OWNER
lsalzman973ed242016-01-14 13:06:41 -08001230 CHECK_SHOULD_DRAW(draw);
bsalomonb1b01992015-11-18 10:56:08 -08001231 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001232 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001233 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
1234 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001235 return;
1236 }
bsalomonb1b01992015-11-18 10:56:08 -08001237 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1238 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1239 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1240 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1241 const SkRect* dst = &origDst;
1242 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001243 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001244 if (!src) {
1245 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001246 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001247
bsalomonb1b01992015-11-18 10:56:08 -08001248 SkMatrix srcToDstMatrix;
1249 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1250 return;
1251 }
1252 SkRect tmpSrc, tmpDst;
1253 if (src != &bmpBounds) {
1254 if (!bmpBounds.contains(*src)) {
1255 tmpSrc = *src;
1256 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001257 return; // nothing to draw
1258 }
bsalomonb1b01992015-11-18 10:56:08 -08001259 src = &tmpSrc;
1260 srcToDstMatrix.mapRect(&tmpDst, *src);
1261 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001262 }
1263 }
1264
bsalomonb1b01992015-11-18 10:56:08 -08001265 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001266
bsalomonb1b01992015-11-18 10:56:08 -08001267 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1268 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
1269 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
1270 paint.isAntiAlias() &&
1271 bitmap.width() <= maxTileSize &&
1272 bitmap.height() <= maxTileSize;
1273
1274 bool skipTileCheck = drawAA || paint.getMaskFilter();
1275
1276 if (!skipTileCheck) {
1277 int tileSize;
1278 SkIRect clippedSrcRect;
1279
1280 GrTextureParams params;
1281 bool doBicubic;
1282 GrTextureParams::FilterMode textureFilterMode =
1283 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1284 &doBicubic);
1285
1286 int tileFilterPad;
1287
1288 if (doBicubic) {
1289 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1290 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1291 tileFilterPad = 0;
1292 } else {
1293 tileFilterPad = 1;
1294 }
1295 params.setFilterMode(textureFilterMode);
1296
1297 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1298 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1299 // we have a mask filter.
1300 SkMatrix viewMatrix = *draw.fMatrix;
1301 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1302 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1303 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1304 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1305 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1306 constraint, tileSize, doBicubic);
1307 return;
1308 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001309 }
bsalomonb1b01992015-11-18 10:56:08 -08001310 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001311 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001312}
1313
1314void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1315 int x, int y, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001316 ASSERT_SINGLE_OWNER
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001317 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001318 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001319 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001320
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001321 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001322 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001323
1324 GrRenderTarget* devRT = dev->accessRenderTarget();
1325 GrTexture* devTex;
halcanary96fcdcc2015-08-27 07:41:13 -07001326 if (nullptr == (devTex = devRT->asTexture())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001327 return;
1328 }
1329
robertphillips7b9e8a42014-12-11 08:20:31 -08001330 const SkImageInfo ii = dev->imageInfo();
1331 int w = ii.width();
1332 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001333
senorblancof35566e2016-04-18 10:32:02 -07001334 SkASSERT(!paint.getImageFilter());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001335
1336 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001337 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001338 GrSimpleTextureEffect::Create(devTex, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001339 if (GrPixelConfigIsAlphaOnly(devTex->config())) {
1340 // Can this happen?
1341 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1342 } else {
1343 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1344 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001345
brianosman898235c2016-04-06 07:38:23 -07001346 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp,
brianosmanb461d342016-04-13 13:10:14 -07001347 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001348 return;
1349 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001350
1351 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1352 SkIntToScalar(y),
1353 SkIntToScalar(w),
1354 SkIntToScalar(h));
1355
1356 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1357 // scratch texture).
1358 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1359 SK_Scalar1 * h / devTex->height());
1360
bsalomona2e69fc2015-11-05 10:41:43 -08001361 fDrawContext->fillRectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001362}
1363
reeda85d4d02015-05-06 12:56:48 -07001364void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1365 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001366 ASSERT_SINGLE_OWNER
bsalomon1cf6f9b2015-12-08 10:53:43 -08001367 SkMatrix viewMatrix = *draw.fMatrix;
1368 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001369 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001370 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001371 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001372 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1373 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001374 return;
reed85d91782015-09-10 14:33:38 -07001375 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001376 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001377 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1378 paint.getFilterQuality(), *draw.fMatrix)) {
1379 // only support tiling as bitmap at the moment, so force raster-version
1380 if (!as_IB(image)->getROPixels(&bm)) {
1381 return;
1382 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001383 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1384 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1385 CHECK_SHOULD_DRAW(draw);
1386 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001387 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1388 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001389 } else if (as_IB(image)->getROPixels(&bm)) {
1390 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001391 }
reeda85d4d02015-05-06 12:56:48 -07001392 }
1393}
1394
1395void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001396 const SkRect& dst, const SkPaint& paint,
1397 SkCanvas::SrcRectConstraint constraint) {
joshualittce894002016-01-11 13:29:31 -08001398 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001399 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001400 CHECK_SHOULD_DRAW(draw);
1401 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001402 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001403 return;
1404 }
1405 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001406 SkMatrix totalMatrix = *draw.fMatrix;
1407 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1408 dst.height() / (src ? src->height() : image->height()));
1409 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001410 // only support tiling as bitmap at the moment, so force raster-version
1411 if (!as_IB(image)->getROPixels(&bm)) {
1412 return;
1413 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001414 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1415 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1416 CHECK_SHOULD_DRAW(draw);
1417 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001418 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001419 } else if (as_IB(image)->getROPixels(&bm)) {
1420 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001421 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001422}
1423
bsalomonf1ecd212015-12-09 17:06:02 -08001424void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001425 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001426 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001427
joshualitt33a5fce2015-11-18 13:28:51 -08001428 CHECK_SHOULD_DRAW(draw);
1429
joshualittedb36442015-11-19 14:29:30 -08001430 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
1431 fRenderTarget->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001432 bool doBicubic;
1433 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001434 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1435 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001436 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001437 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001438
1439 SkRect srcR, dstR;
1440 while (iter.next(&srcR, &dstR)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001441 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
bsalomonf1ecd212015-12-09 17:06:02 -08001442 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001443 }
1444 return;
1445 }
1446
bsalomon2bbd0c62015-12-09 12:50:56 -08001447 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
1448 SkAutoTUnref<const GrFragmentProcessor> fp(
1449 producer->createFragmentProcessor(SkMatrix::I(),
1450 SkRect::MakeIWH(producer->width(), producer->height()),
1451 GrTextureProducer::kNo_FilterConstraint, true,
1452 &kMode));
joshualitt33a5fce2015-11-18 13:28:51 -08001453 GrPaint grPaint;
1454 if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, fp,
brianosman898235c2016-04-06 07:38:23 -07001455 producer->isAlphaOnly(),
brianosmanb461d342016-04-13 13:10:14 -07001456 this->surfaceProps().isGammaCorrect(), &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001457 return;
1458 }
1459
bsalomon2bbd0c62015-12-09 12:50:56 -08001460 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1461 producer->height(), center, dst);
1462}
1463
1464void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1465 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001466 ASSERT_SINGLE_OWNER
bsalomonf1ecd212015-12-09 17:06:02 -08001467 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001468 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001469 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001470 } else {
1471 SkBitmap bm;
1472 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1473 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001474 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001475 } else if (as_IB(image)->getROPixels(&bm)) {
1476 this->drawBitmapNine(draw, bm, center, dst, paint);
1477 }
1478 }
1479}
1480
1481void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1482 const SkRect& dst, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001483 ASSERT_SINGLE_OWNER
bsalomon2bbd0c62015-12-09 12:50:56 -08001484 if (bitmap.getTexture()) {
1485 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001486 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001487 } else {
1488 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001489 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001490 }
joshualitt33a5fce2015-11-18 13:28:51 -08001491}
1492
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001493///////////////////////////////////////////////////////////////////////////////
1494
1495// must be in SkCanvas::VertexMode order
1496static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1497 kTriangles_GrPrimitiveType,
1498 kTriangleStrip_GrPrimitiveType,
1499 kTriangleFan_GrPrimitiveType,
1500};
1501
1502void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1503 int vertexCount, const SkPoint vertices[],
1504 const SkPoint texs[], const SkColor colors[],
1505 SkXfermode* xmode,
1506 const uint16_t indices[], int indexCount,
1507 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001508 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001509 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001510 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001511
halcanary96fcdcc2015-08-27 07:41:13 -07001512 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1513 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001514
halcanary96fcdcc2015-08-27 07:41:13 -07001515 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001516
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001517 SkPaint copy(paint);
1518 copy.setStyle(SkPaint::kStroke_Style);
1519 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001520
bsalomonf1b7a1d2015-09-28 06:26:28 -07001521 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001522 // we ignore the shader if texs is null.
brianosman898235c2016-04-06 07:38:23 -07001523 if (!SkPaintToGrPaintNoShader(this->context(), copy,
brianosmanb461d342016-04-13 13:10:14 -07001524 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001525 return;
1526 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001527
dandov32a311b2014-07-15 19:46:26 -07001528 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001529 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001530 switch (vmode) {
1531 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001532 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001533 break;
1534 case SkCanvas::kTriangleStrip_VertexMode:
1535 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001536 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001537 break;
1538 }
mtklein533eb782014-08-27 10:39:42 -07001539
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001540 VertState state(vertexCount, indices, indexCount);
1541 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001542
dandov32a311b2014-07-15 19:46:26 -07001543 //number of indices for lines per triangle with kLines
1544 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001545
bsalomonf1b7a1d2015-09-28 06:26:28 -07001546 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001547 int i = 0;
1548 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001549 lineIndices[i] = state.f0;
1550 lineIndices[i + 1] = state.f1;
1551 lineIndices[i + 2] = state.f1;
1552 lineIndices[i + 3] = state.f2;
1553 lineIndices[i + 4] = state.f2;
1554 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001555 i += 6;
1556 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001557 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001558 grPaint,
1559 *draw.fMatrix,
1560 kLines_GrPrimitiveType,
1561 vertexCount,
1562 vertices,
1563 texs,
1564 colors,
1565 lineIndices.get(),
1566 indexCount);
1567 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001568 }
1569
bsalomonf1b7a1d2015-09-28 06:26:28 -07001570 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001571
1572 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001573 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001574 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1575 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001576 convertedColors.reset(vertexCount);
1577 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001578 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001579 }
1580 colors = convertedColors.get();
1581 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001582 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001583 if (texs && paint.getShader()) {
1584 if (colors) {
1585 // When there are texs and colors the shader and colors are combined using xmode. A null
1586 // xmode is defined to mean modulate.
1587 SkXfermode::Mode colorMode;
1588 if (xmode) {
1589 if (!xmode->asMode(&colorMode)) {
1590 return;
1591 }
1592 } else {
1593 colorMode = SkXfermode::kModulate_Mode;
1594 }
1595 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
brianosmanb461d342016-04-13 13:10:14 -07001596 false, this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001597 &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001598 return;
1599 }
1600 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001601 // We have a shader, but no colors to blend it against.
brianosman898235c2016-04-06 07:38:23 -07001602 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001603 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001604 return;
1605 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001606 }
bsalomonaa48d362015-10-01 08:34:17 -07001607 } else {
1608 if (colors) {
1609 // We have colors, but either have no shader or no texture coords (which implies that
1610 // we should ignore the shader).
brianosman898235c2016-04-06 07:38:23 -07001611 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001612 this->surfaceProps().isGammaCorrect(),
brianosman898235c2016-04-06 07:38:23 -07001613 &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001614 return;
1615 }
1616 } else {
1617 // No colors and no shaders. Just draw with the paint color.
brianosman898235c2016-04-06 07:38:23 -07001618 if (!SkPaintToGrPaintNoShader(this->context(), paint,
brianosmanb461d342016-04-13 13:10:14 -07001619 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonaa48d362015-10-01 08:34:17 -07001620 return;
1621 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001622 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001623 }
1624
robertphillips2e1e51f2015-10-15 08:01:48 -07001625 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001626 grPaint,
1627 *draw.fMatrix,
1628 primType,
1629 vertexCount,
1630 vertices,
1631 texs,
1632 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001633 indices,
robertphillipsea461502015-05-26 11:38:03 -07001634 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001635}
1636
1637///////////////////////////////////////////////////////////////////////////////
1638
jvanverth31ff7622015-08-07 10:09:28 -07001639void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001640 const SkRect texRect[], const SkColor colors[], int count,
1641 SkXfermode::Mode mode, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001642 ASSERT_SINGLE_OWNER
reedca109532015-06-25 16:25:25 -07001643 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001644 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001645 return;
1646 }
1647
jvanverth31ff7622015-08-07 10:09:28 -07001648 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001649 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001650
reedca109532015-06-25 16:25:25 -07001651 SkPaint p(paint);
reed5671c5b2016-03-09 14:47:34 -08001652 p.setShader(atlas->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
reedca109532015-06-25 16:25:25 -07001653
jvanverth31ff7622015-08-07 10:09:28 -07001654 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001655 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001656 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
brianosmanb461d342016-04-13 13:10:14 -07001657 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001658 return;
1659 }
1660 } else {
brianosman898235c2016-04-06 07:38:23 -07001661 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001662 this->surfaceProps().isGammaCorrect(), &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001663 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001664 }
1665 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001666
1667 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001668 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001669}
1670
1671///////////////////////////////////////////////////////////////////////////////
1672
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001673void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001674 size_t byteLength, SkScalar x, SkScalar y,
1675 const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001676 ASSERT_SINGLE_OWNER
joshualitt5531d512014-12-17 15:50:11 -08001677 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001678 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001679
jvanverth8c27a182014-10-14 08:45:50 -07001680 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001681 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001682 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001683 return;
1684 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001685
jvanverth8c27a182014-10-14 08:45:50 -07001686 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001687
robertphillips2e1e51f2015-10-15 08:01:48 -07001688 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001689 (const char *)text, byteLength, x, y, draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001690}
1691
fmalita05c4a432014-09-29 06:29:53 -07001692void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1693 const SkScalar pos[], int scalarsPerPos,
1694 const SkPoint& offset, const SkPaint& paint) {
joshualittce894002016-01-11 13:29:31 -08001695 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001696 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001697 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001698
jvanverth8c27a182014-10-14 08:45:50 -07001699 GrPaint grPaint;
brianosman898235c2016-04-06 07:38:23 -07001700 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix,
brianosmanb461d342016-04-13 13:10:14 -07001701 this->surfaceProps().isGammaCorrect(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001702 return;
1703 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001704
jvanverth8c27a182014-10-14 08:45:50 -07001705 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001706
robertphillips2e1e51f2015-10-15 08:01:48 -07001707 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001708 (const char *)text, byteLength, pos, scalarsPerPos, offset,
reed1e7f5e72016-04-27 07:49:17 -07001709 draw.fRC->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001710}
1711
joshualitt9c328182015-03-23 08:13:04 -07001712void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1713 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualittce894002016-01-11 13:29:31 -08001714 ASSERT_SINGLE_OWNER
joshualitt5651ee62016-01-11 10:39:11 -08001715 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001716 CHECK_SHOULD_DRAW(draw);
1717
1718 SkDEBUGCODE(this->validate();)
1719
robertphillips2e1e51f2015-10-15 08:01:48 -07001720 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
reed1e7f5e72016-04-27 07:49:17 -07001721 blob, x, y, drawFilter, draw.fRC->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001722}
1723
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001724///////////////////////////////////////////////////////////////////////////////
1725
reedb2db8982014-11-13 12:41:02 -08001726bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
joshualitt8e84a1e2016-02-16 11:09:25 -08001727 return GrTextUtils::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001728}
1729
1730void SkGpuDevice::flush() {
joshualittce894002016-01-11 13:29:31 -08001731 ASSERT_SINGLE_OWNER
joshualittbc907352016-01-13 06:45:40 -08001732
bsalomonc49e8682015-06-30 11:37:35 -07001733 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001734}
1735
1736///////////////////////////////////////////////////////////////////////////////
1737
reed76033be2015-03-14 10:54:31 -07001738SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
joshualittce894002016-01-11 13:29:31 -08001739 ASSERT_SINGLE_OWNER
bsalomonf2703d82014-10-28 14:33:06 -07001740 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001741 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001742 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001743 desc.fWidth = cinfo.fInfo.width();
1744 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001745 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001746
1747 SkAutoTUnref<GrTexture> texture;
1748 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001749 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001750
hcm4396fa52014-10-27 21:43:30 -07001751 // layers are never draw in repeat modes, so we can request an approx
1752 // match and ignore any padding.
bsalomoneae62002015-07-31 13:59:30 -07001753 if (kNever_TileUsage == cinfo.fTileUsage) {
1754 texture.reset(fContext->textureProvider()->createApproxTexture(desc));
1755 } else {
bsalomon5ec26ae2016-02-25 08:33:02 -08001756 texture.reset(fContext->textureProvider()->createTexture(desc, SkBudgeted::kYes));
bsalomoneae62002015-07-31 13:59:30 -07001757 }
bsalomonafe30052015-01-16 07:32:33 -08001758
1759 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001760 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
robertphillips24e91282016-04-29 06:46:36 -07001761 return SkGpuDevice::Make(sk_ref_sp(texture->asRenderTarget()),
1762 cinfo.fInfo.width(), cinfo.fInfo.height(),
1763 &props, init).release();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001764 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001765 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001766 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001767 cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary96fcdcc2015-08-27 07:41:13 -07001768 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001769 }
1770}
1771
reede8f30622016-03-23 18:59:25 -07001772sk_sp<SkSurface> SkGpuDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
joshualittce894002016-01-11 13:29:31 -08001773 ASSERT_SINGLE_OWNER
bsalomonafe30052015-01-16 07:32:33 -08001774 // TODO: Change the signature of newSurface to take a budgeted parameter.
bsalomon5ec26ae2016-02-25 08:33:02 -08001775 static const SkBudgeted kBudgeted = SkBudgeted::kNo;
robertphillips524484b2016-05-06 07:55:19 -07001776 return SkSurface::MakeRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
reede8f30622016-03-23 18:59:25 -07001777 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001778}
1779
robertphillips30d2cc62014-09-24 08:52:18 -07001780bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001781 const SkMatrix* matrix, const SkPaint* paint) {
joshualittce894002016-01-11 13:29:31 -08001782 ASSERT_SINGLE_OWNER
robertphillips63242d72014-12-04 08:31:02 -08001783#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001784 // todo: should handle this natively
robertphillips74e9a4d2016-05-09 05:12:18 -07001785 if (paint ||
1786 (kRGBA_8888_SkColorType != mainCanvas->imageInfo().colorType() &&
1787 kBGRA_8888_SkColorType != mainCanvas->imageInfo().colorType())) {
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,
robertphillips524484b2016-05-06 07:55:19 -07001821 fRenderTarget->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,
robertphillips524484b2016-05-06 07:55:19 -07001833 fRenderTarget->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