blob: 7c77dedecb21a18690de67e6632cbf898136bb1e [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"
robertphillips82365912014-11-12 09:32:34 -080029#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000030#include "SkMaskFilter.h"
joshualitt33a5fce2015-11-18 13:28:51 -080031#include "SkNinePatchIter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000032#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000033#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070034#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080036#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000038#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080039#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000040#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000041#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000042#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070043#include "SkXfermode.h"
joshualitta61c8172015-08-17 10:51:22 -070044#include "batches/GrRectBatchFactory.h"
kkinnunenabcfab42015-02-22 22:53:44 -080045#include "effects/GrBicubicEffect.h"
46#include "effects/GrDashingEffect.h"
47#include "effects/GrSimpleTextureEffect.h"
48#include "effects/GrTextureDomain.h"
joshualitte8042922015-12-11 06:11:21 -080049#include "text/GrTextContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000050
reedf037e0b2014-10-30 11:34:15 -070051#if SK_SUPPORT_GPU
52
senorblanco55b6d8b2014-07-30 11:26:46 -070053enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
54
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000055#if 0
56 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080057 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058 do { \
59 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080060 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000061 } while (0)
62#else
joshualitt5531d512014-12-17 15:50:11 -080063 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000064#endif
65
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000066#define DO_DEFERRED_CLEAR() \
67 do { \
bsalomonafe30052015-01-16 07:32:33 -080068 if (fNeedClear) { \
69 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000070 } \
71 } while (false) \
72
73///////////////////////////////////////////////////////////////////////////////
74
75#define CHECK_FOR_ANNOTATION(paint) \
76 do { if (paint.getAnnotation()) { return; } } while (0)
77
78///////////////////////////////////////////////////////////////////////////////
79
bsalomonbcf0a522014-10-08 08:40:09 -070080// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
81// just accesses the backing GrTexture. Otherwise, it creates a cached texture
82// representation and releases it in the destructor.
83class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040084public:
bsalomonbcf0a522014-10-08 08:40:09 -070085 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070086
bsalomonbcf0a522014-10-08 08:40:09 -070087 AutoBitmapTexture(GrContext* context,
88 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -070089 const GrTextureParams& params,
bsalomonbcf0a522014-10-08 08:40:09 -070090 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040091 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070092 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040093 }
94
bsalomonbcf0a522014-10-08 08:40:09 -070095 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040096 const SkBitmap& bitmap,
bsalomonafa95e22015-10-12 10:39:46 -070097 const GrTextureParams& params) {
bsalomonbcf0a522014-10-08 08:40:09 -070098 // Either get the texture directly from the bitmap, or else use the cache and
99 // remember to unref it.
100 if (GrTexture* bmpTexture = bitmap.getTexture()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700101 fTexture.reset(nullptr);
bsalomonbcf0a522014-10-08 08:40:09 -0700102 return bmpTexture;
103 } else {
104 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
105 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400106 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400107 }
108
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000109private:
bsalomonbcf0a522014-10-08 08:40:09 -0700110 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000111};
112
113///////////////////////////////////////////////////////////////////////////////
114
bsalomon74f681d2015-06-23 14:38:48 -0700115/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
116 should fail. */
117bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
118 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
119 *flags = 0;
120 if (info) {
121 switch (info->alphaType()) {
122 case kPremul_SkAlphaType:
123 break;
124 case kOpaque_SkAlphaType:
125 *flags |= SkGpuDevice::kIsOpaque_Flag;
126 break;
127 default: // If it is unpremul or unknown don't try to render
128 return false;
129 }
130 }
131 if (kClear_InitContents == init) {
132 *flags |= kNeedClear_Flag;
133 }
134 return true;
135}
136
137SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props,
138 InitContents init) {
139 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, init);
senorblancod0d37ca2015-04-02 04:54:56 -0700140}
141
142SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
bsalomon74f681d2015-06-23 14:38:48 -0700143 const SkSurfaceProps* props, InitContents init) {
bsalomonafe30052015-01-16 07:32:33 -0800144 if (!rt || rt->wasDestroyed()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700145 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000146 }
bsalomon74f681d2015-06-23 14:38:48 -0700147 unsigned flags;
halcanary96fcdcc2015-08-27 07:41:13 -0700148 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
149 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700150 }
halcanary385fe4d2015-08-26 13:07:48 -0700151 return new SkGpuDevice(rt, width, height, props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000152}
153
bsalomon74f681d2015-06-23 14:38:48 -0700154SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
155 const SkImageInfo& info, int sampleCount,
156 const SkSurfaceProps* props, InitContents init) {
157 unsigned flags;
158 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700159 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700160 }
161
162 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
halcanary96fcdcc2015-08-27 07:41:13 -0700163 if (nullptr == rt) {
164 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700165 }
166
halcanary385fe4d2015-08-26 13:07:48 -0700167 return new SkGpuDevice(rt, info.width(), info.height(), props, flags);
bsalomon74f681d2015-06-23 14:38:48 -0700168}
169
senorblancod0d37ca2015-04-02 04:54:56 -0700170SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
171 const SkSurfaceProps* props, unsigned flags)
robertphillipsfcf78292015-06-19 11:49:52 -0700172 : INHERITED(SkSurfacePropsCopyOrDefault(props))
joshualitt06dd0a82016-01-08 06:32:31 -0800173 , fContext(SkRef(rt->getContext()))
174 , fRenderTarget(SkRef(rt)) {
bsalomon74f681d2015-06-23 14:38:48 -0700175 fNeedClear = SkToBool(flags & kNeedClear_Flag);
176 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000177
bsalomon74f681d2015-06-23 14:38:48 -0700178 SkAlphaType at = fOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
179 SkImageInfo info = rt->surfacePriv().info(at).makeWH(width, height);
halcanary385fe4d2015-08-26 13:07:48 -0700180 SkPixelRef* pr = new SkGrPixelRef(info, rt);
bsalomonafbf2d62014-09-30 12:18:44 -0700181 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700182 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700183
robertphillips77a2e522015-10-17 07:43:27 -0700184 fDrawContext.reset(this->context()->drawContext(rt, &this->surfaceProps()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000185}
186
kkinnunenabcfab42015-02-22 22:53:44 -0800187GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
188 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000189 if (kUnknown_SkColorType == origInfo.colorType() ||
190 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700191 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000192 }
193
bsalomonafe30052015-01-16 07:32:33 -0800194 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700195 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800196 }
197
reede5ea5002014-09-03 11:54:58 -0700198 SkColorType ct = origInfo.colorType();
199 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700200 if (kRGB_565_SkColorType == ct) {
201 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800202 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
203 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700204 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800205 }
206 if (kOpaque_SkAlphaType != at) {
207 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000208 }
reede5ea5002014-09-03 11:54:58 -0700209 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000210
bsalomonf2703d82014-10-28 14:33:06 -0700211 GrSurfaceDesc desc;
212 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000213 desc.fWidth = info.width();
214 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000215 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000216 desc.fSampleCnt = sampleCount;
bsalomond309e7a2015-04-30 14:18:54 -0700217 GrTexture* texture = context->textureProvider()->createTexture(
halcanary96fcdcc2015-08-27 07:41:13 -0700218 desc, SkToBool(budgeted), nullptr, 0);
219 if (nullptr == texture) {
220 return nullptr;
kkinnunenabcfab42015-02-22 22:53:44 -0800221 }
halcanary96fcdcc2015-08-27 07:41:13 -0700222 SkASSERT(nullptr != texture->asRenderTarget());
kkinnunenabcfab42015-02-22 22:53:44 -0800223 return texture->asRenderTarget();
224}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000225
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000226///////////////////////////////////////////////////////////////////////////////
227
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000228bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
229 int x, int y) {
230 DO_DEFERRED_CLEAR();
231
232 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000233 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000234 if (kUnknown_GrPixelConfig == config) {
235 return false;
236 }
237
238 uint32_t flags = 0;
239 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
240 flags = GrContext::kUnpremul_PixelOpsFlag;
241 }
bsalomon74f681d2015-06-23 14:38:48 -0700242 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
243 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000244}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000245
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000246bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
247 int x, int y) {
248 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000249 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000250 if (kUnknown_GrPixelConfig == config) {
251 return false;
252 }
253 uint32_t flags = 0;
254 if (kUnpremul_SkAlphaType == info.alphaType()) {
255 flags = GrContext::kUnpremul_PixelOpsFlag;
256 }
257 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
258
259 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700260 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000261
262 return true;
263}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000264
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000265const SkBitmap& SkGpuDevice::onAccessBitmap() {
266 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700267 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000268}
269
reed41e010c2015-06-09 12:16:53 -0700270bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
271 DO_DEFERRED_CLEAR();
272 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
273 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
274 // ... ugh.
275 fLegacyBitmap.notifyPixelsChanged();
276 return false;
277}
278
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000279void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
280 INHERITED::onAttachToCanvas(canvas);
281
282 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800283 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000284}
285
286void SkGpuDevice::onDetachFromCanvas() {
287 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800288 fClip.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700289 fClipStack.reset(nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000290}
291
292// call this every draw call, to ensure that the context reflects our state,
293// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800294void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800295 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000296
joshualitt44701df2015-02-23 14:44:57 -0800297 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000298
joshualitt570d2f82015-02-25 13:19:48 -0800299 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000300
301 DO_DEFERRED_CLEAR();
302}
303
304GrRenderTarget* SkGpuDevice::accessRenderTarget() {
robertphillips7156b092015-05-14 08:54:12 -0700305 DO_DEFERRED_CLEAR();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000306 return fRenderTarget;
307}
308
reed8eddfb52014-12-04 07:50:14 -0800309void SkGpuDevice::clearAll() {
310 GrColor color = 0;
joshualitt5651ee62016-01-11 10:39:11 -0800311 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "clearAll", fContext);
reed8eddfb52014-12-04 07:50:14 -0800312 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillips2e1e51f2015-10-15 08:01:48 -0700313 fDrawContext->clear(&rect, color, true);
bsalomonafe30052015-01-16 07:32:33 -0800314 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800315}
316
kkinnunenabcfab42015-02-22 22:53:44 -0800317void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
318 // Caller must have accessed the render target, because it knows the rt must be replaced.
319 SkASSERT(!fNeedClear);
320
321 SkSurface::Budgeted budgeted =
322 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
323 : SkSurface::kNo_Budgeted;
324
325 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
robertphillips77a2e522015-10-17 07:43:27 -0700326 this->context(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt));
kkinnunenabcfab42015-02-22 22:53:44 -0800327
halcanary96fcdcc2015-08-27 07:41:13 -0700328 if (nullptr == newRT) {
kkinnunenabcfab42015-02-22 22:53:44 -0800329 return;
330 }
331
332 if (shouldRetainContent) {
333 if (fRenderTarget->wasDestroyed()) {
334 return;
335 }
336 this->context()->copySurface(newRT, fRenderTarget);
337 }
338
339 SkASSERT(fRenderTarget != newRT);
340
joshualitt06dd0a82016-01-08 06:32:31 -0800341 fRenderTarget.reset(newRT.detach());
kkinnunenabcfab42015-02-22 22:53:44 -0800342
bsalomon74f681d2015-06-23 14:38:48 -0700343#ifdef SK_DEBUG
344 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
345 kPremul_SkAlphaType);
346 SkASSERT(info == fLegacyBitmap.info());
347#endif
halcanary385fe4d2015-08-26 13:07:48 -0700348 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget);
kkinnunenabcfab42015-02-22 22:53:44 -0800349 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700350
robertphillips77a2e522015-10-17 07:43:27 -0700351 fDrawContext.reset(this->context()->drawContext(fRenderTarget, &this->surfaceProps()));
kkinnunenabcfab42015-02-22 22:53:44 -0800352}
353
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000354///////////////////////////////////////////////////////////////////////////////
355
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800357 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800358 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000359
360 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700361 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700362 return;
363 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000364
robertphillips2e1e51f2015-10-15 08:01:48 -0700365 fDrawContext->drawPaint(fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000366}
367
368// must be in SkCanvas::PointMode order
369static const GrPrimitiveType gPointMode2PrimtiveType[] = {
370 kPoints_GrPrimitiveType,
371 kLines_GrPrimitiveType,
372 kLineStrip_GrPrimitiveType
373};
374
ethannicholas330bb952015-07-17 06:44:02 -0700375// suppress antialiasing on axis-aligned integer-coordinate lines
376static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
377 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
378 return false;
379 }
380 if (count == 2) {
381 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
382 // the other coordinates are not. This does mean the two end pixels of the line will be
herb11a7f7f2015-11-24 12:41:00 -0800383 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
ethannicholas330bb952015-07-17 06:44:02 -0700384 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
herb11a7f7f2015-11-24 12:41:00 -0800385 // more complete fix is possible down the road, for the time being we accept the error on
ethannicholas330bb952015-07-17 06:44:02 -0700386 // the two end pixels as being the lesser of two evils.
387 if (pts[0].fX == pts[1].fX) {
388 return ((int) pts[0].fX) != pts[0].fX;
389 }
390 if (pts[0].fY == pts[1].fY) {
391 return ((int) pts[0].fY) != pts[0].fY;
392 }
393 }
394 return true;
395}
396
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000397void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
398 size_t count, const SkPoint pts[], const SkPaint& paint) {
399 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800400 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000401
402 SkScalar width = paint.getStrokeWidth();
403 if (width < 0) {
404 return;
405 }
406
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000407 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700408 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
409 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700410 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700411 return;
412 }
egdaniele61c4112014-06-12 10:24:21 -0700413 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700414 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700415 path.moveTo(pts[0]);
416 path.lineTo(pts[1]);
robertphillips2e1e51f2015-10-15 08:01:48 -0700417 fDrawContext->drawPath(fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700418 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000419 }
420
ethannicholas330bb952015-07-17 06:44:02 -0700421 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000422 // else we let the SkDraw call our drawPath()
herb11a7f7f2015-11-24 12:41:00 -0800423 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
ethannicholas330bb952015-07-17 06:44:02 -0700424 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000425 draw.drawPoints(mode, count, pts, paint, true);
426 return;
427 }
428
429 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700430 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700431 return;
432 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000433
robertphillips2e1e51f2015-10-15 08:01:48 -0700434 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -0700435 grPaint,
436 *draw.fMatrix,
437 gPointMode2PrimtiveType[mode],
438 SkToS32(count),
439 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700440 nullptr,
441 nullptr,
442 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700443 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000444}
445
446///////////////////////////////////////////////////////////////////////////////
447
robertphillipsff55b492015-11-24 07:56:59 -0800448void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -0800449 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000450 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800451 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000452
453 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
454 SkScalar width = paint.getStrokeWidth();
455
456 /*
457 We have special code for hairline strokes, miter-strokes, bevel-stroke
458 and fills. Anything else we just call our path code.
459 */
460 bool usePath = doStroke && width > 0 &&
461 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
462 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
egdanield58a0ba2014-06-11 10:30:05 -0700463
robertphillipsd8aa59d2015-08-05 09:07:12 -0700464 // a few other reasons we might need to call drawPath...
robertphillipsff55b492015-11-24 07:56:59 -0800465 if (paint.getMaskFilter() || paint.getPathEffect() ||
robertphillipsd8aa59d2015-08-05 09:07:12 -0700466 paint.getStyle() == SkPaint::kStrokeAndFill_Style) { // we can't both stroke and fill rects
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000467 usePath = true;
468 }
egdanield58a0ba2014-06-11 10:30:05 -0700469
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000470 if (usePath) {
471 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700472 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000473 path.addRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800474 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800475 fClip, path, paint,
476 *draw.fMatrix, nullptr,
477 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000478 return;
479 }
480
481 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700482 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700483 return;
484 }
Mike Klein744fb732014-06-23 15:13:26 -0400485
robertphillipsff55b492015-11-24 07:56:59 -0800486 GrStrokeInfo strokeInfo(paint);
487
robertphillips2e1e51f2015-10-15 08:01:48 -0700488 fDrawContext->drawRect(fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000489}
490
491///////////////////////////////////////////////////////////////////////////////
492
493void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800494 const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -0800495 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000496 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800497 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000498
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000499 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700500 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700501 return;
502 }
Mike Klein744fb732014-06-23 15:13:26 -0400503
egdanield58a0ba2014-06-11 10:30:05 -0700504 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000505 if (paint.getMaskFilter()) {
506 // try to hit the fast path for drawing filtered round rects
507
508 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800509 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000510 if (devRRect.allCornersCircular()) {
511 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700512 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
joshualitt5531d512014-12-17 15:50:11 -0800513 draw.fClip->getBounds(),
514 *draw.fMatrix,
515 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000516 SkIRect finalIRect;
517 maskRect.roundOut(&finalIRect);
518 if (draw.fClip->quickReject(finalIRect)) {
519 // clipped out
520 return;
521 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700522 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
523 fDrawContext,
joshualitt25d9c152015-02-18 12:29:52 -0800524 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800525 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800526 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700527 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700528 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000529 return;
530 }
531 }
532
533 }
534 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000535 }
536
robertphillips514450c2015-11-24 05:36:02 -0800537 if (paint.getMaskFilter() || paint.getPathEffect()) {
robertphillipsff55b492015-11-24 07:56:59 -0800538 // The only mask filter the native rrect drawing code could've handle was taken
539 // care of above.
540 // A path effect will presumably transform this rrect into something else.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700542 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000543 path.addRRect(rect);
robertphillips7bceedc2015-12-01 12:51:26 -0800544 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsff55b492015-11-24 07:56:59 -0800545 fClip, path, paint,
546 *draw.fMatrix, nullptr,
547 draw.fClip->getBounds(), true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000548 return;
549 }
Mike Klein744fb732014-06-23 15:13:26 -0400550
robertphillips514450c2015-11-24 05:36:02 -0800551 SkASSERT(!strokeInfo.isDashed());
552
robertphillips2e1e51f2015-10-15 08:01:48 -0700553 fDrawContext->drawRRect(fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000554}
555
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000556void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800557 const SkRRect& inner, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -0800558 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDRRect", fContext);
robertphillips0e7029e2015-11-30 05:45:06 -0800559 CHECK_FOR_ANNOTATION(paint);
560 CHECK_SHOULD_DRAW(draw);
561
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000562 SkStrokeRec stroke(paint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000563
robertphillips0e7029e2015-11-30 05:45:06 -0800564 if (stroke.isFillStyle() && !paint.getMaskFilter() && !paint.getPathEffect()) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000565 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700566 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700567 return;
568 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000569
robertphillips0e7029e2015-11-30 05:45:06 -0800570 fDrawContext->drawDRRect(fClip, grPaint, *draw.fMatrix, outer, inner);
571 return;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000572 }
573
574 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700575 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000576 path.addRRect(outer);
577 path.addRRect(inner);
578 path.setFillType(SkPath::kEvenOdd_FillType);
579
robertphillips7bceedc2015-12-01 12:51:26 -0800580 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillips0e7029e2015-11-30 05:45:06 -0800581 fClip, path, paint,
582 *draw.fMatrix, nullptr,
583 draw.fClip->getBounds(), true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000584}
585
586
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000587/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000588
robertphillips514450c2015-11-24 05:36:02 -0800589void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -0800590 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000591 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800592 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000593
robertphillips514450c2015-11-24 05:36:02 -0800594 // Presumably the path effect warps this to something other than an oval
595 if (paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000596 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700597 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000598 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700599 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000600 return;
herb11a7f7f2015-11-24 12:41:00 -0800601 }
602
robertphillips514450c2015-11-24 05:36:02 -0800603 if (paint.getMaskFilter()) {
604 // The RRect path can handle special case blurring
605 SkRRect rr = SkRRect::MakeOval(oval);
606 return this->drawRRect(draw, rr, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000607 }
608
609 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700610 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700611 return;
612 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000613
robertphillips514450c2015-11-24 05:36:02 -0800614 GrStrokeInfo strokeInfo(paint);
615 SkASSERT(!strokeInfo.isDashed());
616
robertphillips2e1e51f2015-10-15 08:01:48 -0700617 fDrawContext->drawOval(fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000618}
619
620#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000621
622///////////////////////////////////////////////////////////////////////////////
623
robertphillipsccb1b572015-05-27 11:02:55 -0700624static SkBitmap wrap_texture(GrTexture* texture, int width, int height) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000625 SkBitmap result;
senorblancod0d37ca2015-04-02 04:54:56 -0700626 result.setInfo(SkImageInfo::MakeN32Premul(width, height));
halcanary385fe4d2015-08-26 13:07:48 -0700627 result.setPixelRef(new SkGrPixelRef(result.info(), texture))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000628 return result;
629}
630
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000631void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
632 const SkPaint& paint, const SkMatrix* prePathMatrix,
633 bool pathIsMutable) {
robertphillipsff55b492015-11-24 07:56:59 -0800634 if (!origSrcPath.isInverseFillType() && !paint.getPathEffect() && !prePathMatrix) {
635 bool isClosed;
636 SkRect rect;
637 if (origSrcPath.isRect(&rect, &isClosed) && isClosed) {
638 this->drawRect(draw, rect, paint);
639 return;
640 }
641 if (origSrcPath.isOval(&rect)) {
642 this->drawOval(draw, rect, paint);
643 return;
644 }
645 SkRRect rrect;
646 if (origSrcPath.isRRect(&rrect)) {
647 this->drawRRect(draw, rrect, paint);
648 return;
649 }
650 }
651
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000652 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800653 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -0800654 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000655
robertphillips7bceedc2015-12-01 12:51:26 -0800656 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext,
robertphillipsccb1b572015-05-27 11:02:55 -0700657 fClip, origSrcPath, paint,
658 *draw.fMatrix, prePathMatrix,
659 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660}
661
662static const int kBmpSmallTileSize = 1 << 10;
663
664static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
665 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
666 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
667 return tilesX * tilesY;
668}
669
reed85d91782015-09-10 14:33:38 -0700670static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000671 if (maxTileSize <= kBmpSmallTileSize) {
672 return maxTileSize;
673 }
674
675 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
676 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
677
678 maxTileTotalTileSize *= maxTileSize * maxTileSize;
679 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
680
681 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
682 return kBmpSmallTileSize;
683 } else {
684 return maxTileSize;
685 }
686}
687
688// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
689// pixels from the bitmap are necessary.
bsalomon74f681d2015-06-23 14:38:48 -0700690static void determine_clipped_src_rect(const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800691 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800692 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700693 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000694 const SkRect* srcRectPtr,
695 SkIRect* clippedSrcIRect) {
robertphillips7bceedc2015-12-01 12:51:26 -0800696 clip.getConservativeBounds(rt->width(), rt->height(), clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000697 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800698 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000699 clippedSrcIRect->setEmpty();
700 return;
701 }
702 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
703 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700704 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000705 // we've setup src space 0,0 to map to the top left of the src rect.
706 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000707 if (!clippedSrcRect.intersect(*srcRectPtr)) {
708 clippedSrcIRect->setEmpty();
709 return;
710 }
711 }
712 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700713 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000714 if (!clippedSrcIRect->intersect(bmpBounds)) {
715 clippedSrcIRect->setEmpty();
716 }
717}
718
reed85d91782015-09-10 14:33:38 -0700719bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
720 const SkMatrix& viewMatrix,
721 const GrTextureParams& params,
722 const SkRect* srcRectPtr,
723 int maxTileSize,
724 int* tileSize,
725 SkIRect* clippedSubset) const {
726 // if it's larger than the max tile size, then we have no choice but tiling.
727 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
728 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(),
729 srcRectPtr, clippedSubset);
730 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
731 return true;
732 }
733
bsalomon1a1d0b82015-10-16 07:49:42 -0700734 // If the image would only produce 4 tiles of the smaller size, don't bother tiling it.
reed85d91782015-09-10 14:33:38 -0700735 const size_t area = imageRect.width() * imageRect.height();
736 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
737 return false;
738 }
739
reed85d91782015-09-10 14:33:38 -0700740 // At this point we know we could do the draw by uploading the entire bitmap
741 // as a texture. However, if the texture would be large compared to the
742 // cache size and we don't require most of it for this draw then tile to
743 // reduce the amount of upload and cache spill.
744
745 // assumption here is that sw bitmap size is a good proxy for its size as
746 // a texture
747 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
748 size_t cacheSize;
749 fContext->getResourceCacheLimits(nullptr, &cacheSize);
750 if (bmpSize < cacheSize / 2) {
751 return false;
752 }
753
bsalomon1a1d0b82015-10-16 07:49:42 -0700754 // Figure out how much of the src we will need based on the src rect and clipping. Reject if
755 // tiling memory savings would be < 50%.
reed85d91782015-09-10 14:33:38 -0700756 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(), srcRectPtr,
757 clippedSubset);
758 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
759 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
760 kBmpSmallTileSize * kBmpSmallTileSize;
761
762 return usedTileBytes < 2 * bmpSize;
763}
764
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000765bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800766 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000767 const GrTextureParams& params,
768 const SkRect* srcRectPtr,
769 int maxTileSize,
770 int* tileSize,
771 SkIRect* clippedSrcRect) const {
772 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700773 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000774 return false;
775 }
776
reed85d91782015-09-10 14:33:38 -0700777 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
778 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
779}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000780
reed85d91782015-09-10 14:33:38 -0700781bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
782 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
783 const SkMatrix& viewMatrix) const {
784 // if image is explictly texture backed then just use the texture
785 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786 return false;
787 }
788
reed85d91782015-09-10 14:33:38 -0700789 GrTextureParams params;
790 bool doBicubic;
791 GrTextureParams::FilterMode textureFilterMode =
792 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
793
794 int tileFilterPad;
795 if (doBicubic) {
796 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
797 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
798 tileFilterPad = 0;
799 } else {
800 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000801 }
reed85d91782015-09-10 14:33:38 -0700802 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000803
bsalomon8c07b7a2015-11-02 11:36:52 -0800804 int maxTileSize = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000805
reed85d91782015-09-10 14:33:38 -0700806 // these are output, which we safely ignore, as we just want to know the predicate
807 int outTileSize;
808 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000809
reed85d91782015-09-10 14:33:38 -0700810 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
811 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000812}
813
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000814void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000815 const SkBitmap& bitmap,
816 const SkMatrix& m,
817 const SkPaint& paint) {
bsalomonb1b01992015-11-18 10:56:08 -0800818 CHECK_SHOULD_DRAW(origDraw);
bsalomonb1b01992015-11-18 10:56:08 -0800819 SkMatrix viewMatrix;
820 viewMatrix.setConcat(*origDraw.fMatrix, m);
821 if (bitmap.getTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -0800822 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonb1b01992015-11-18 10:56:08 -0800823 // We can use kFast here because we know texture-backed bitmaps don't support extractSubset.
bsalomonf1ecd212015-12-09 17:06:02 -0800824 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
825 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -0800826 return;
827 }
bsalomonb1b01992015-11-18 10:56:08 -0800828 int maxTileSize = fContext->caps()->maxTileSize();
829
830 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
831 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
832 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
833 paint.isAntiAlias() &&
834 bitmap.width() <= maxTileSize &&
835 bitmap.height() <= maxTileSize;
836
837 bool skipTileCheck = drawAA || paint.getMaskFilter();
838
839 if (!skipTileCheck) {
840 SkRect srcRect = SkRect::MakeIWH(bitmap.width(), bitmap.height());
841 int tileSize;
842 SkIRect clippedSrcRect;
843
844 GrTextureParams params;
845 bool doBicubic;
846 GrTextureParams::FilterMode textureFilterMode =
847 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, SkMatrix::I(),
848 &doBicubic);
849
850 int tileFilterPad;
851
852 if (doBicubic) {
853 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
854 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
855 tileFilterPad = 0;
856 } else {
857 tileFilterPad = 1;
858 }
859 params.setFilterMode(textureFilterMode);
860
861 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
862 if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect,
863 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
864 this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint,
865 SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic);
866 return;
867 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000868 }
bsalomonb1b01992015-11-18 10:56:08 -0800869 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -0800870 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kStrict_SrcRectConstraint,
871 viewMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000872}
873
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000874// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000875// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
876// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000877static inline void clamped_outset_with_offset(SkIRect* iRect,
878 int outset,
879 SkPoint* offset,
880 const SkIRect& clamp) {
881 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000882
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000883 int leftClampDelta = clamp.fLeft - iRect->fLeft;
884 if (leftClampDelta > 0) {
885 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000886 iRect->fLeft = clamp.fLeft;
887 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000888 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000889 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000890
891 int topClampDelta = clamp.fTop - iRect->fTop;
892 if (topClampDelta > 0) {
893 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000894 iRect->fTop = clamp.fTop;
895 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000896 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000897 }
898
899 if (iRect->fRight > clamp.fRight) {
900 iRect->fRight = clamp.fRight;
901 }
902 if (iRect->fBottom > clamp.fBottom) {
903 iRect->fBottom = clamp.fBottom;
904 }
905}
906
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000907// Break 'bitmap' into several tiles to draw it since it has already
908// been determined to be too large to fit in VRAM
909void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800910 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000911 const SkRect& srcRect,
912 const SkIRect& clippedSrcIRect,
913 const GrTextureParams& params,
bsalomonc55271f2015-11-09 11:55:57 -0800914 const SkPaint& origPaint,
reeda5517e22015-07-14 10:54:12 -0700915 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000916 int tileSize,
917 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +0000918 // The following pixel lock is technically redundant, but it is desirable
919 // to lock outside of the tile loop to prevent redecoding the whole image
920 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
921 // is larger than the limit of the discardable memory pool.
922 SkAutoLockPixels alp(bitmap);
bsalomonc55271f2015-11-09 11:55:57 -0800923
924 const SkPaint* paint = &origPaint;
925 SkPaint tempPaint;
926 if (origPaint.isAntiAlias() && !fRenderTarget->isUnifiedMultisampled()) {
927 // Drop antialiasing to avoid seams at tile boundaries.
928 tempPaint = origPaint;
929 tempPaint.setAntiAlias(false);
930 paint = &tempPaint;
931 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000932 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
933
934 int nx = bitmap.width() / tileSize;
935 int ny = bitmap.height() / tileSize;
936 for (int x = 0; x <= nx; x++) {
937 for (int y = 0; y <= ny; y++) {
938 SkRect tileR;
939 tileR.set(SkIntToScalar(x * tileSize),
940 SkIntToScalar(y * tileSize),
941 SkIntToScalar((x + 1) * tileSize),
942 SkIntToScalar((y + 1) * tileSize));
943
944 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
945 continue;
946 }
947
948 if (!tileR.intersect(srcRect)) {
949 continue;
950 }
951
952 SkBitmap tmpB;
953 SkIRect iTileR;
954 tileR.roundOut(&iTileR);
955 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
956 SkIntToScalar(iTileR.fTop));
957
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000958 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -0800959 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000960 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000961 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -0800962 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000963
robertphillipsec8bb942014-11-21 10:16:25 -0800964 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000965 SkIRect iClampRect;
966
reeda5517e22015-07-14 10:54:12 -0700967 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000968 // In bleed mode we want to always expand the tile on all edges
969 // but stay within the bitmap bounds
970 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
971 } else {
972 // In texture-domain/clamp mode we only want to expand the
973 // tile on edges interior to "srcRect" (i.e., we want to
974 // not bleed across the original clamped edges)
975 srcRect.roundOut(&iClampRect);
976 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000977 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
978 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000979 }
980
981 if (bitmap.extractSubset(&tmpB, iTileR)) {
982 // now offset it to make it "local" to our tmp bitmap
983 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000984 GrTextureParams paramsTemp = params;
bsalomonb1b01992015-11-18 10:56:08 -0800985 // de-optimized this determination
986 bool needsTextureDomain = true;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000987 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -0800988 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000989 tileR,
990 paramsTemp,
bsalomonc55271f2015-11-09 11:55:57 -0800991 *paint,
reeda5517e22015-07-14 10:54:12 -0700992 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000993 bicubic,
994 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000995 }
996 }
997 }
998}
999
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001000/*
1001 * This is called by drawBitmap(), which has to handle images that may be too
1002 * large to be represented by a single texture.
1003 *
1004 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1005 * and that non-texture portion of the GrPaint has already been setup.
1006 */
1007void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001008 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001009 const SkRect& srcRect,
1010 const GrTextureParams& params,
1011 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001012 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001013 bool bicubic,
1014 bool needsTextureDomain) {
bsalomon9c586542015-11-02 12:33:21 -08001015 // We should have already handled bitmaps larger than the max texture size.
1016 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1017 bitmap.height() <= fContext->caps()->maxTextureSize());
1018 // Unless the bitmap is inherently texture-backed, we should be respecting the max tile size
1019 // by the time we get here.
1020 SkASSERT(bitmap.getTexture() ||
1021 (bitmap.width() <= fContext->caps()->maxTileSize() &&
1022 bitmap.height() <= fContext->caps()->maxTileSize()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001023
1024 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001025 AutoBitmapTexture abt(fContext, bitmap, params, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001026 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001027 return;
1028 }
1029
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001030 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001031 SkRect paintRect;
1032 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1033 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1034 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1035 SkScalarMul(srcRect.fTop, hInv),
1036 SkScalarMul(srcRect.fRight, wInv),
1037 SkScalarMul(srcRect.fBottom, hInv));
1038
egdaniel79da63f2015-10-09 10:55:16 -07001039 SkMatrix texMatrix;
1040 texMatrix.reset();
1041 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1042 // In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use
1043 // local coords with the bitmap draw since it may mess up texture look ups for the shader.
1044 // Thus we need to pass in the transform matrix directly to the texture processor used for
1045 // the bitmap draw.
1046 texMatrix.setScale(wInv, hInv);
1047 }
1048
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001049 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001050
1051 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1052 // the rest from the SkPaint.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001053 SkAutoTUnref<const GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001054
reeda5517e22015-07-14 10:54:12 -07001055 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001056 // Use a constrained texture domain to avoid color bleeding
1057 SkScalar left, top, right, bottom;
1058 if (srcRect.width() > SK_Scalar1) {
1059 SkScalar border = SK_ScalarHalf / texture->width();
1060 left = paintRect.left() + border;
1061 right = paintRect.right() - border;
1062 } else {
1063 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1064 }
1065 if (srcRect.height() > SK_Scalar1) {
1066 SkScalar border = SK_ScalarHalf / texture->height();
1067 top = paintRect.top() + border;
1068 bottom = paintRect.bottom() - border;
1069 } else {
1070 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1071 }
1072 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001073 if (bicubic) {
egdaniel79da63f2015-10-09 10:55:16 -07001074 fp.reset(GrBicubicEffect::Create(texture, texMatrix, textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001075 } else {
bsalomon4a339522015-10-06 08:40:50 -07001076 fp.reset(GrTextureDomainEffect::Create(texture,
egdaniel79da63f2015-10-09 10:55:16 -07001077 texMatrix,
joshualitt5531d512014-12-17 15:50:11 -08001078 textureDomain,
1079 GrTextureDomain::kClamp_Mode,
1080 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001081 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001082 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001083 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1084 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
egdaniel79da63f2015-10-09 10:55:16 -07001085 fp.reset(GrBicubicEffect::Create(texture, texMatrix, tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001086 } else {
egdaniel79da63f2015-10-09 10:55:16 -07001087 fp.reset(GrSimpleTextureEffect::Create(texture, texMatrix, params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001088 }
1089
joshualitt33a5fce2015-11-18 13:28:51 -08001090 GrPaint grPaint;
1091 if (!SkPaintToGrPaintWithTexture(this->context(), paint, viewMatrix, fp,
1092 kAlpha_8_SkColorType == bitmap.colorType(), &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001093 return;
1094 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001095
egdaniel79da63f2015-10-09 10:55:16 -07001096 if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
1097 // We don't have local coords in this case and have previously set the transform
1098 // matrices directly on the texture processor.
robertphillips2e1e51f2015-10-15 08:01:48 -07001099 fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
egdaniel79da63f2015-10-09 10:55:16 -07001100 } else {
bsalomona2e69fc2015-11-05 10:41:43 -08001101 fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
egdaniel79da63f2015-10-09 10:55:16 -07001102 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001103}
1104
fmalita2d97bc12014-11-20 10:44:58 -08001105bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001106 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001107 const SkImageFilter* filter,
1108 const SkImageFilter::Context& ctx,
1109 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001110 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001111
reed88d064d2015-10-12 11:30:02 -07001112 SkImageFilter::DeviceProxy proxy(this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001113
1114 if (filter->canFilterImageGPU()) {
senorblancod0d37ca2015-04-02 04:54:56 -07001115 return filter->filterImageGPU(&proxy, wrap_texture(texture, width, height),
1116 ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001117 } else {
1118 return false;
1119 }
1120}
1121
1122void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1123 int left, int top, const SkPaint& paint) {
1124 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001125 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001126
1127 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1128 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1129 return;
1130 }
1131
1132 int w = bitmap.width();
1133 int h = bitmap.height();
1134
1135 GrTexture* texture;
bsalomonafa95e22015-10-12 10:39:46 -07001136 // draw sprite neither filters nor tiles.
1137 AutoBitmapTexture abt(fContext, bitmap, GrTextureParams::ClampNoFilter(), &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001138 if (!texture) {
1139 return;
1140 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001141
bsalomonf1b7a1d2015-09-28 06:26:28 -07001142 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
1143
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001144 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001145 // This bitmap will own the filtered result as a texture.
1146 SkBitmap filteredBitmap;
1147
bsalomon49f085d2014-09-05 13:34:00 -07001148 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001149 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001150 SkMatrix matrix(*draw.fMatrix);
1151 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblancodb64af32015-12-09 10:11:43 -08001152#ifdef SK_SUPPORT_SRC_BOUNDS_BLOAT_FOR_IMAGEFILTERS
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001153 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancodb64af32015-12-09 10:11:43 -08001154#else
1155 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-left, -top);
1156#endif
senorblancobe129b22014-08-08 07:14:35 -07001157 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001158 // This cache is transient, and is freed (along with all its contained
1159 // textures) when it goes out of scope.
reedc9b5f8b2015-10-22 13:20:20 -07001160 SkImageFilter::Context ctx(matrix, clipBounds, cache, SkImageFilter::kApprox_SizeConstraint);
senorblancod0d37ca2015-04-02 04:54:56 -07001161 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001162 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001163 texture = (GrTexture*) filteredBitmap.getTexture();
1164 w = filteredBitmap.width();
1165 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001166 left += offset.x();
1167 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001168 } else {
1169 return;
1170 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001171 SkASSERT(!GrPixelConfigIsAlphaOnly(texture->config()));
1172 alphaOnly = false;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001173 }
1174
1175 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001176 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001177 GrSimpleTextureEffect::Create(texture, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001178 if (alphaOnly) {
1179 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1180 } else {
1181 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1182 }
1183 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001184 return;
1185 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001186
bsalomona2e69fc2015-11-05 10:41:43 -08001187 fDrawContext->fillRectToRect(fClip,
1188 grPaint,
1189 SkMatrix::I(),
1190 SkRect::MakeXYWH(SkIntToScalar(left),
1191 SkIntToScalar(top),
1192 SkIntToScalar(w),
1193 SkIntToScalar(h)),
1194 SkRect::MakeXYWH(0,
1195 0,
1196 SK_Scalar1 * w / texture->width(),
1197 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001198}
1199
bsalomonb1b01992015-11-18 10:56:08 -08001200void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1201 const SkRect* src, const SkRect& origDst,
reed562fe472015-07-28 07:35:14 -07001202 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
bsalomonb1b01992015-11-18 10:56:08 -08001203 if (bitmap.getTexture()) {
1204 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001205 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001206 this->drawTextureProducer(&adjuster, src, &origDst, constraint, *draw.fMatrix, fClip,
1207 paint);
bsalomonc55271f2015-11-09 11:55:57 -08001208 return;
1209 }
bsalomonb1b01992015-11-18 10:56:08 -08001210 // The src rect is inferred to be the bmp bounds if not provided. Otherwise, the src rect must
1211 // be clipped to the bmp bounds. To determine tiling parameters we need the filter mode which
1212 // in turn requires knowing the src-to-dst mapping. If the src was clipped to the bmp bounds
1213 // then we use the src-to-dst mapping to compute a new clipped dst rect.
1214 const SkRect* dst = &origDst;
1215 const SkRect bmpBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001216 // Compute matrix from the two rectangles
bsalomonb1b01992015-11-18 10:56:08 -08001217 if (!src) {
1218 src = &bmpBounds;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001219 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001220
bsalomonb1b01992015-11-18 10:56:08 -08001221 SkMatrix srcToDstMatrix;
1222 if (!srcToDstMatrix.setRectToRect(*src, *dst, SkMatrix::kFill_ScaleToFit)) {
1223 return;
1224 }
1225 SkRect tmpSrc, tmpDst;
1226 if (src != &bmpBounds) {
1227 if (!bmpBounds.contains(*src)) {
1228 tmpSrc = *src;
1229 if (!tmpSrc.intersect(bmpBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001230 return; // nothing to draw
1231 }
bsalomonb1b01992015-11-18 10:56:08 -08001232 src = &tmpSrc;
1233 srcToDstMatrix.mapRect(&tmpDst, *src);
1234 dst = &tmpDst;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001235 }
1236 }
1237
bsalomonb1b01992015-11-18 10:56:08 -08001238 int maxTileSize = fContext->caps()->maxTileSize();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001239
bsalomonb1b01992015-11-18 10:56:08 -08001240 // The tile code path doesn't currently support AA, so if the paint asked for aa and we could
1241 // draw untiled, then we bypass checking for tiling purely for optimization reasons.
1242 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
1243 paint.isAntiAlias() &&
1244 bitmap.width() <= maxTileSize &&
1245 bitmap.height() <= maxTileSize;
1246
1247 bool skipTileCheck = drawAA || paint.getMaskFilter();
1248
1249 if (!skipTileCheck) {
1250 int tileSize;
1251 SkIRect clippedSrcRect;
1252
1253 GrTextureParams params;
1254 bool doBicubic;
1255 GrTextureParams::FilterMode textureFilterMode =
1256 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, srcToDstMatrix,
1257 &doBicubic);
1258
1259 int tileFilterPad;
1260
1261 if (doBicubic) {
1262 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1263 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1264 tileFilterPad = 0;
1265 } else {
1266 tileFilterPad = 1;
1267 }
1268 params.setFilterMode(textureFilterMode);
1269
1270 int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
1271 // Fold the dst rect into the view matrix. This is only OK because we don't get here if
1272 // we have a mask filter.
1273 SkMatrix viewMatrix = *draw.fMatrix;
1274 viewMatrix.preTranslate(dst->fLeft, dst->fTop);
1275 viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height());
1276 if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
1277 maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
1278 this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
1279 constraint, tileSize, doBicubic);
1280 return;
1281 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001282 }
bsalomonb1b01992015-11-18 10:56:08 -08001283 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001284 this->drawTextureProducer(&maker, src, dst, constraint, *draw.fMatrix, fClip, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001285}
1286
1287void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1288 int x, int y, const SkPaint& paint) {
1289 // clear of the source device must occur before CHECK_SHOULD_DRAW
joshualitt5651ee62016-01-11 10:39:11 -08001290 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001291 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001292
1293 // TODO: If the source device covers the whole of this device, we could
1294 // omit fNeedsClear -related flushing.
1295 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001296
1297 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001298 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001299
1300 GrRenderTarget* devRT = dev->accessRenderTarget();
1301 GrTexture* devTex;
halcanary96fcdcc2015-08-27 07:41:13 -07001302 if (nullptr == (devTex = devRT->asTexture())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001303 return;
1304 }
1305
robertphillips7b9e8a42014-12-11 08:20:31 -08001306 const SkImageInfo ii = dev->imageInfo();
1307 int w = ii.width();
1308 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001309
1310 SkImageFilter* filter = paint.getImageFilter();
1311 // This bitmap will own the filtered result as a texture.
1312 SkBitmap filteredBitmap;
1313
bsalomon49f085d2014-09-05 13:34:00 -07001314 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001315 SkIPoint offset = SkIPoint::Make(0, 0);
1316 SkMatrix matrix(*draw.fMatrix);
1317 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblancodb64af32015-12-09 10:11:43 -08001318#ifdef SK_SUPPORT_SRC_BOUNDS_BLOAT_FOR_IMAGEFILTERS
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001319 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblancodb64af32015-12-09 10:11:43 -08001320#else
1321 SkIRect clipBounds = draw.fClip->getBounds().makeOffset(-x, -y);
1322#endif
senorblanco55b6d8b2014-07-30 11:26:46 -07001323 // This cache is transient, and is freed (along with all its contained
1324 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001325 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
reedc9b5f8b2015-10-22 13:20:20 -07001326 SkImageFilter::Context ctx(matrix, clipBounds, cache, SkImageFilter::kApprox_SizeConstraint);
senorblancod0d37ca2015-04-02 04:54:56 -07001327 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1328 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001329 devTex = filteredBitmap.getTexture();
1330 w = filteredBitmap.width();
1331 h = filteredBitmap.height();
1332 x += offset.fX;
1333 y += offset.fY;
1334 } else {
1335 return;
1336 }
1337 }
1338
1339 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001340 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -07001341 GrSimpleTextureEffect::Create(devTex, SkMatrix::I()));
bsalomonf1b7a1d2015-09-28 06:26:28 -07001342 if (GrPixelConfigIsAlphaOnly(devTex->config())) {
1343 // Can this happen?
1344 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1345 } else {
1346 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1347 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001348
bsalomonf1b7a1d2015-09-28 06:26:28 -07001349 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001350 return;
1351 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001352
1353 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1354 SkIntToScalar(y),
1355 SkIntToScalar(w),
1356 SkIntToScalar(h));
1357
1358 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1359 // scratch texture).
1360 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1361 SK_Scalar1 * h / devTex->height());
1362
bsalomona2e69fc2015-11-05 10:41:43 -08001363 fDrawContext->fillRectToRect(fClip, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001364}
1365
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001366bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001367 return filter->canFilterImageGPU();
1368}
1369
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001370bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001371 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001372 SkBitmap* result, SkIPoint* offset) {
1373 // want explicitly our impl, so guard against a subclass of us overriding it
1374 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1375 return false;
1376 }
1377
1378 SkAutoLockPixels alp(src, !src.getTexture());
1379 if (!src.getTexture() && !src.readyToDraw()) {
1380 return false;
1381 }
1382
1383 GrTexture* texture;
1384 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1385 // must be pushed upstack.
bsalomonafa95e22015-10-12 10:39:46 -07001386 AutoBitmapTexture abt(fContext, src, GrTextureParams::ClampNoFilter(), &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001387 if (!texture) {
1388 return false;
1389 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001390
senorblancod0d37ca2015-04-02 04:54:56 -07001391 return this->filterTexture(fContext, texture, src.width(), src.height(),
1392 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001393}
1394
reeda85d4d02015-05-06 12:56:48 -07001395void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1396 const SkPaint& paint) {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001397 SkMatrix viewMatrix = *draw.fMatrix;
1398 viewMatrix.preTranslate(x, y);
bsalomonf1ecd212015-12-09 17:06:02 -08001399 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001400 CHECK_SHOULD_DRAW(draw);
bsalomonc55271f2015-11-09 11:55:57 -08001401 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001402 this->drawTextureProducer(&adjuster, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1403 viewMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001404 return;
reed85d91782015-09-10 14:33:38 -07001405 } else {
bsalomon1cf6f9b2015-12-08 10:53:43 -08001406 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001407 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1408 paint.getFilterQuality(), *draw.fMatrix)) {
1409 // only support tiling as bitmap at the moment, so force raster-version
1410 if (!as_IB(image)->getROPixels(&bm)) {
1411 return;
1412 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001413 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1414 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1415 CHECK_SHOULD_DRAW(draw);
1416 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001417 this->drawTextureProducer(&maker, nullptr, nullptr, SkCanvas::kFast_SrcRectConstraint,
1418 viewMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001419 } else if (as_IB(image)->getROPixels(&bm)) {
1420 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reed85d91782015-09-10 14:33:38 -07001421 }
reeda85d4d02015-05-06 12:56:48 -07001422 }
1423}
1424
1425void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001426 const SkRect& dst, const SkPaint& paint,
1427 SkCanvas::SrcRectConstraint constraint) {
bsalomonf1ecd212015-12-09 17:06:02 -08001428 if (as_IB(image)->peekTexture()) {
bsalomonc55271f2015-11-09 11:55:57 -08001429 CHECK_SHOULD_DRAW(draw);
1430 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001431 this->drawTextureProducer(&adjuster, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomonc55271f2015-11-09 11:55:57 -08001432 return;
1433 }
1434 SkBitmap bm;
bsalomon1cf6f9b2015-12-08 10:53:43 -08001435 SkMatrix totalMatrix = *draw.fMatrix;
1436 totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1437 dst.height() / (src ? src->height() : image->height()));
1438 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) {
bsalomonc55271f2015-11-09 11:55:57 -08001439 // only support tiling as bitmap at the moment, so force raster-version
1440 if (!as_IB(image)->getROPixels(&bm)) {
1441 return;
1442 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001443 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
1444 } else if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1445 CHECK_SHOULD_DRAW(draw);
1446 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001447 this->drawTextureProducer(&maker, src, &dst, constraint, *draw.fMatrix, fClip, paint);
bsalomon1cf6f9b2015-12-08 10:53:43 -08001448 } else if (as_IB(image)->getROPixels(&bm)) {
1449 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001450 }
bsalomon1cf6f9b2015-12-08 10:53:43 -08001451}
1452
bsalomonf1ecd212015-12-09 17:06:02 -08001453void SkGpuDevice::drawProducerNine(const SkDraw& draw, GrTextureProducer* producer,
bsalomon2bbd0c62015-12-09 12:50:56 -08001454 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001455 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawProducerNine", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001456
1457 CHECK_FOR_ANNOTATION(paint);
1458 CHECK_SHOULD_DRAW(draw);
1459
joshualittedb36442015-11-19 14:29:30 -08001460 bool useFallback = paint.getMaskFilter() || paint.isAntiAlias() ||
1461 fRenderTarget->isUnifiedMultisampled();
joshualitt33a5fce2015-11-18 13:28:51 -08001462 bool doBicubic;
1463 GrTextureParams::FilterMode textureFilterMode =
bsalomon2bbd0c62015-12-09 12:50:56 -08001464 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I(),
1465 &doBicubic);
joshualitt33a5fce2015-11-18 13:28:51 -08001466 if (useFallback || doBicubic || GrTextureParams::kNone_FilterMode != textureFilterMode) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001467 SkNinePatchIter iter(producer->width(), producer->height(), center, dst);
joshualitt33a5fce2015-11-18 13:28:51 -08001468
1469 SkRect srcR, dstR;
1470 while (iter.next(&srcR, &dstR)) {
bsalomonf1ecd212015-12-09 17:06:02 -08001471 this->drawTextureProducer(producer, &srcR, &dstR, SkCanvas::kStrict_SrcRectConstraint,
1472 *draw.fMatrix, fClip, paint);
joshualitt33a5fce2015-11-18 13:28:51 -08001473 }
1474 return;
1475 }
1476
bsalomon2bbd0c62015-12-09 12:50:56 -08001477 static const GrTextureParams::FilterMode kMode = GrTextureParams::kNone_FilterMode;
1478 SkAutoTUnref<const GrFragmentProcessor> fp(
1479 producer->createFragmentProcessor(SkMatrix::I(),
1480 SkRect::MakeIWH(producer->width(), producer->height()),
1481 GrTextureProducer::kNo_FilterConstraint, true,
1482 &kMode));
joshualitt33a5fce2015-11-18 13:28:51 -08001483 GrPaint grPaint;
1484 if (!SkPaintToGrPaintWithTexture(this->context(), paint, *draw.fMatrix, fp,
bsalomonf1ecd212015-12-09 17:06:02 -08001485 producer->isAlphaOnly(), &grPaint)) {
joshualitt33a5fce2015-11-18 13:28:51 -08001486 return;
1487 }
1488
bsalomon2bbd0c62015-12-09 12:50:56 -08001489 fDrawContext->drawImageNine(fClip, grPaint, *draw.fMatrix, producer->width(),
1490 producer->height(), center, dst);
1491}
1492
1493void SkGpuDevice::drawImageNine(const SkDraw& draw, const SkImage* image,
1494 const SkIRect& center, const SkRect& dst, const SkPaint& paint) {
bsalomonf1ecd212015-12-09 17:06:02 -08001495 if (as_IB(image)->peekTexture()) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001496 GrImageTextureAdjuster adjuster(as_IB(image));
bsalomonf1ecd212015-12-09 17:06:02 -08001497 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001498 } else {
1499 SkBitmap bm;
1500 if (SkImageCacherator* cacher = as_IB(image)->peekCacherator()) {
1501 GrImageTextureMaker maker(fContext, cacher, image, SkImage::kAllow_CachingHint);
bsalomonf1ecd212015-12-09 17:06:02 -08001502 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001503 } else if (as_IB(image)->getROPixels(&bm)) {
1504 this->drawBitmapNine(draw, bm, center, dst, paint);
1505 }
1506 }
1507}
1508
1509void SkGpuDevice::drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
1510 const SkRect& dst, const SkPaint& paint) {
bsalomon2bbd0c62015-12-09 12:50:56 -08001511 if (bitmap.getTexture()) {
1512 GrBitmapTextureAdjuster adjuster(&bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001513 this->drawProducerNine(draw, &adjuster, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001514 } else {
1515 GrBitmapTextureMaker maker(fContext, bitmap);
bsalomonf1ecd212015-12-09 17:06:02 -08001516 this->drawProducerNine(draw, &maker, center, dst, paint);
bsalomon2bbd0c62015-12-09 12:50:56 -08001517 }
joshualitt33a5fce2015-11-18 13:28:51 -08001518}
1519
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001520///////////////////////////////////////////////////////////////////////////////
1521
1522// must be in SkCanvas::VertexMode order
1523static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1524 kTriangles_GrPrimitiveType,
1525 kTriangleStrip_GrPrimitiveType,
1526 kTriangleFan_GrPrimitiveType,
1527};
1528
1529void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1530 int vertexCount, const SkPoint vertices[],
1531 const SkPoint texs[], const SkColor colors[],
1532 SkXfermode* xmode,
1533 const uint16_t indices[], int indexCount,
1534 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001535 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001536 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001537
halcanary96fcdcc2015-08-27 07:41:13 -07001538 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1539 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001540
halcanary96fcdcc2015-08-27 07:41:13 -07001541 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001542
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001543 SkPaint copy(paint);
1544 copy.setStyle(SkPaint::kStroke_Style);
1545 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001546
bsalomonf1b7a1d2015-09-28 06:26:28 -07001547 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001548 // we ignore the shader if texs is null.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001549 if (!SkPaintToGrPaintNoShader(this->context(), copy, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001550 return;
1551 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001552
dandov32a311b2014-07-15 19:46:26 -07001553 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001554 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001555 switch (vmode) {
1556 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001557 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001558 break;
1559 case SkCanvas::kTriangleStrip_VertexMode:
1560 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001561 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001562 break;
1563 }
mtklein533eb782014-08-27 10:39:42 -07001564
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001565 VertState state(vertexCount, indices, indexCount);
1566 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001567
dandov32a311b2014-07-15 19:46:26 -07001568 //number of indices for lines per triangle with kLines
1569 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001570
bsalomonf1b7a1d2015-09-28 06:26:28 -07001571 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001572 int i = 0;
1573 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001574 lineIndices[i] = state.f0;
1575 lineIndices[i + 1] = state.f1;
1576 lineIndices[i + 2] = state.f1;
1577 lineIndices[i + 3] = state.f2;
1578 lineIndices[i + 4] = state.f2;
1579 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001580 i += 6;
1581 }
robertphillips2e1e51f2015-10-15 08:01:48 -07001582 fDrawContext->drawVertices(fClip,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001583 grPaint,
1584 *draw.fMatrix,
1585 kLines_GrPrimitiveType,
1586 vertexCount,
1587 vertices,
1588 texs,
1589 colors,
1590 lineIndices.get(),
1591 indexCount);
1592 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001593 }
1594
bsalomonf1b7a1d2015-09-28 06:26:28 -07001595 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001596
1597 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001598 if (colors) {
bsalomonaa48d362015-10-01 08:34:17 -07001599 // need to convert byte order and from non-PM to PM. TODO: Keep unpremul until after
1600 // interpolation.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001601 convertedColors.reset(vertexCount);
1602 for (int i = 0; i < vertexCount; ++i) {
bsalomonaa48d362015-10-01 08:34:17 -07001603 convertedColors[i] = SkColorToPremulGrColor(colors[i]);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001604 }
1605 colors = convertedColors.get();
1606 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001607 GrPaint grPaint;
bsalomonaa48d362015-10-01 08:34:17 -07001608 if (texs && paint.getShader()) {
1609 if (colors) {
1610 // When there are texs and colors the shader and colors are combined using xmode. A null
1611 // xmode is defined to mean modulate.
1612 SkXfermode::Mode colorMode;
1613 if (xmode) {
1614 if (!xmode->asMode(&colorMode)) {
1615 return;
1616 }
1617 } else {
1618 colorMode = SkXfermode::kModulate_Mode;
1619 }
1620 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode,
1621 false, &grPaint)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001622 return;
1623 }
1624 } else {
bsalomonaa48d362015-10-01 08:34:17 -07001625 // We have a shader, but no colors to blend it against.
1626 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
1627 return;
1628 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001629 }
bsalomonaa48d362015-10-01 08:34:17 -07001630 } else {
1631 if (colors) {
1632 // We have colors, but either have no shader or no texture coords (which implies that
1633 // we should ignore the shader).
1634 if (!SkPaintToGrPaintWithPrimitiveColor(this->context(), paint, &grPaint)) {
1635 return;
1636 }
1637 } else {
1638 // No colors and no shaders. Just draw with the paint color.
1639 if (!SkPaintToGrPaintNoShader(this->context(), paint, &grPaint)) {
1640 return;
1641 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001642 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001643 }
1644
robertphillips2e1e51f2015-10-15 08:01:48 -07001645 fDrawContext->drawVertices(fClip,
robertphillipsea461502015-05-26 11:38:03 -07001646 grPaint,
1647 *draw.fMatrix,
1648 primType,
1649 vertexCount,
1650 vertices,
1651 texs,
1652 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001653 indices,
robertphillipsea461502015-05-26 11:38:03 -07001654 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001655}
1656
1657///////////////////////////////////////////////////////////////////////////////
1658
jvanverth31ff7622015-08-07 10:09:28 -07001659void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001660 const SkRect texRect[], const SkColor colors[], int count,
1661 SkXfermode::Mode mode, const SkPaint& paint) {
1662 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001663 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001664 return;
1665 }
1666
jvanverth31ff7622015-08-07 10:09:28 -07001667 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001668 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
herb11a7f7f2015-11-24 12:41:00 -08001669
reedca109532015-06-25 16:25:25 -07001670 SkPaint p(paint);
1671 p.setShader(atlas->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
1672
jvanverth31ff7622015-08-07 10:09:28 -07001673 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001674 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001675 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
1676 &grPaint)) {
1677 return;
1678 }
1679 } else {
1680 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix, &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001681 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001682 }
1683 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001684
1685 SkDEBUGCODE(this->validate();)
robertphillips2e1e51f2015-10-15 08:01:48 -07001686 fDrawContext->drawAtlas(fClip, grPaint, *draw.fMatrix, count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001687}
1688
1689///////////////////////////////////////////////////////////////////////////////
1690
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001691void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001692 size_t byteLength, SkScalar x, SkScalar y,
1693 const SkPaint& paint) {
1694 CHECK_SHOULD_DRAW(draw);
joshualitt5651ee62016-01-11 10:39:11 -08001695 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001696
jvanverth8c27a182014-10-14 08:45:50 -07001697 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001698 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001699 return;
1700 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001701
jvanverth8c27a182014-10-14 08:45:50 -07001702 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001703
robertphillips2e1e51f2015-10-15 08:01:48 -07001704 fDrawContext->drawText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001705 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001706}
1707
fmalita05c4a432014-09-29 06:29:53 -07001708void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1709 const SkScalar pos[], int scalarsPerPos,
1710 const SkPoint& offset, const SkPaint& paint) {
joshualitt5651ee62016-01-11 10:39:11 -08001711 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001712 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001713
jvanverth8c27a182014-10-14 08:45:50 -07001714 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001715 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001716 return;
1717 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001718
jvanverth8c27a182014-10-14 08:45:50 -07001719 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001720
robertphillips2e1e51f2015-10-15 08:01:48 -07001721 fDrawContext->drawPosText(fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001722 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1723 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001724}
1725
joshualitt9c328182015-03-23 08:13:04 -07001726void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1727 const SkPaint& paint, SkDrawFilter* drawFilter) {
joshualitt5651ee62016-01-11 10:39:11 -08001728 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice", "drawTextBlob", fContext);
joshualitt9c328182015-03-23 08:13:04 -07001729 CHECK_SHOULD_DRAW(draw);
1730
1731 SkDEBUGCODE(this->validate();)
1732
robertphillips2e1e51f2015-10-15 08:01:48 -07001733 fDrawContext->drawTextBlob(fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001734 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001735}
1736
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001737///////////////////////////////////////////////////////////////////////////////
1738
reedb2db8982014-11-13 12:41:02 -08001739bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
robertphillips9c240a12015-05-28 07:45:59 -07001740 return GrTextContext::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001741}
1742
1743void SkGpuDevice::flush() {
1744 DO_DEFERRED_CLEAR();
bsalomonc49e8682015-06-30 11:37:35 -07001745 fRenderTarget->prepareForExternalIO();
joshualitt5651ee62016-01-11 10:39:11 -08001746
1747 // Clear batch debugging output
1748 if (GR_BATCH_DEBUGGING_OUTPUT) {
1749 SkDebugf("%s\n", fContext->getAuditTrail()->toJson().c_str());
1750 GR_AUDIT_TRAIL_RESET(fContext->getAuditTrail());
1751 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001752}
1753
1754///////////////////////////////////////////////////////////////////////////////
1755
reed76033be2015-03-14 10:54:31 -07001756SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
bsalomonf2703d82014-10-28 14:33:06 -07001757 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001758 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001759 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001760 desc.fWidth = cinfo.fInfo.width();
1761 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001762 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001763
1764 SkAutoTUnref<GrTexture> texture;
1765 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001766 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001767
hcm4396fa52014-10-27 21:43:30 -07001768 // layers are never draw in repeat modes, so we can request an approx
1769 // match and ignore any padding.
bsalomoneae62002015-07-31 13:59:30 -07001770 if (kNever_TileUsage == cinfo.fTileUsage) {
1771 texture.reset(fContext->textureProvider()->createApproxTexture(desc));
1772 } else {
1773 texture.reset(fContext->textureProvider()->createTexture(desc, true));
1774 }
bsalomonafe30052015-01-16 07:32:33 -08001775
1776 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001777 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001778 return SkGpuDevice::Create(
bsalomon74f681d2015-06-23 14:38:48 -07001779 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, init);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001780 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001781 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001782 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001783 cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary96fcdcc2015-08-27 07:41:13 -07001784 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001785 }
1786}
1787
reed4a8126e2014-09-22 07:29:03 -07001788SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001789 // TODO: Change the signature of newSurface to take a budgeted parameter.
1790 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
vbuzinovdded6962015-06-12 08:59:45 -07001791 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
bsalomonafe30052015-01-16 07:32:33 -08001792 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001793}
1794
robertphillips30d2cc62014-09-24 08:52:18 -07001795bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001796 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001797#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001798 // todo: should handle this natively
1799 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001800 return false;
1801 }
1802
halcanary96fcdcc2015-08-27 07:41:13 -07001803 const SkBigPicture::AccelData* data = nullptr;
mtklein9db912c2015-05-19 11:11:26 -07001804 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1805 data = bp->accelData();
1806 }
robertphillips81f71b62014-11-11 04:54:49 -08001807 if (!data) {
1808 return false;
1809 }
1810
robertphillipse5524cd2015-02-20 12:30:26 -08001811 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1812 if (0 == gpuData->numBlocks()) {
1813 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001814 }
1815
robertphillipsfd61ed02014-10-28 07:21:44 -07001816 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001817
robertphillipse5524cd2015-02-20 12:30:26 -08001818 SkIRect iBounds;
1819 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1820 return false;
1821 }
1822
1823 SkRect clipBounds = SkRect::Make(iBounds);
1824
1825 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1826
robertphillips60029a52015-11-09 13:51:06 -08001827 GrLayerHoister::Begin(fContext);
1828
robertphillipsfd61ed02014-10-28 07:21:44 -07001829 GrLayerHoister::FindLayersToAtlas(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 &atlasedNeedRendering, &atlasedRecycled,
vbuzinovdded6962015-06-12 08:59:45 -07001833 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001834
1835 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1836
1837 SkTDArray<GrHoistedLayer> needRendering, recycled;
1838
robertphillipse5524cd2015-02-20 12:30:26 -08001839 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1840
robertphillipsfd61ed02014-10-28 07:21:44 -07001841 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001842 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001843 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001844 &needRendering, &recycled,
vbuzinovdded6962015-06-12 08:59:45 -07001845 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001846
1847 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001848
robertphillips64bf7672014-08-21 13:07:35 -07001849 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001850 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
halcanary96fcdcc2015-08-27 07:41:13 -07001851 initialMatrix, nullptr);
robertphillips64bf7672014-08-21 13:07:35 -07001852
robertphillipsfd61ed02014-10-28 07:21:44 -07001853 GrLayerHoister::UnlockLayers(fContext, needRendering);
1854 GrLayerHoister::UnlockLayers(fContext, recycled);
1855 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1856 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips60029a52015-11-09 13:51:06 -08001857 GrLayerHoister::End(fContext);
robertphillips64bf7672014-08-21 13:07:35 -07001858
1859 return true;
robertphillips63242d72014-12-04 08:31:02 -08001860#else
1861 return false;
1862#endif
robertphillips64bf7672014-08-21 13:07:35 -07001863}
1864
reed13ccbf82015-10-20 09:56:52 -07001865SkImageFilter::Cache* SkGpuDevice::NewImageFilterCache() {
1866 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
1867}
1868
senorblancobe129b22014-08-08 07:14:35 -07001869SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001870 // We always return a transient cache, so it is freed after each
1871 // filter traversal.
reed13ccbf82015-10-20 09:56:52 -07001872 return SkGpuDevice::NewImageFilterCache();
senorblanco55b6d8b2014-07-30 11:26:46 -07001873}
reedf037e0b2014-10-30 11:34:15 -07001874
1875#endif