blob: 5289019cc9703aab9aeb5a0794cdd880250fcaef [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"
robertphillipsea461502015-05-26 11:38:03 -070012#include "GrDrawContext.h"
robertphillips2334fb62015-06-17 05:43:33 -070013#include "GrFontScaler.h"
kkinnunenabcfab42015-02-22 22:53:44 -080014#include "GrGpu.h"
15#include "GrGpuResourcePriv.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"
joshualitt8f94bb22015-04-28 07:04:11 -070019#include "GrTextContext.h"
egdanielbbcb38d2014-06-19 10:19:29 -070020#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080021#include "SkCanvasPriv.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000022#include "SkDrawProcs.h"
kkinnunenabcfab42015-02-22 22:53:44 -080023#include "SkErrorInternals.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000024#include "SkGlyphCache.h"
kkinnunenabcfab42015-02-22 22:53:44 -080025#include "SkGrTexturePixelRef.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070026#include "SkGrPriv.h"
reeda85d4d02015-05-06 12:56:48 -070027#include "SkImage_Base.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"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000031#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000032#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070033#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000034#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080035#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000036#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000037#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080038#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000039#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000040#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000041#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070042#include "SkXfermode.h"
joshualitta61c8172015-08-17 10:51:22 -070043#include "batches/GrRectBatchFactory.h"
kkinnunenabcfab42015-02-22 22:53:44 -080044#include "effects/GrBicubicEffect.h"
45#include "effects/GrDashingEffect.h"
46#include "effects/GrSimpleTextureEffect.h"
47#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000048
reedf037e0b2014-10-30 11:34:15 -070049#if SK_SUPPORT_GPU
50
senorblanco55b6d8b2014-07-30 11:26:46 -070051enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
52
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000053#if 0
54 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080055 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000056 do { \
57 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080058 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000059 } while (0)
60#else
joshualitt5531d512014-12-17 15:50:11 -080061 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000062#endif
63
64// This constant represents the screen alignment criterion in texels for
65// requiring texture domain clamping to prevent color bleeding when drawing
66// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000067#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000068
69#define DO_DEFERRED_CLEAR() \
70 do { \
bsalomonafe30052015-01-16 07:32:33 -080071 if (fNeedClear) { \
72 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000073 } \
74 } while (false) \
75
76///////////////////////////////////////////////////////////////////////////////
77
78#define CHECK_FOR_ANNOTATION(paint) \
79 do { if (paint.getAnnotation()) { return; } } while (0)
80
81///////////////////////////////////////////////////////////////////////////////
82
bsalomonbcf0a522014-10-08 08:40:09 -070083// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
84// just accesses the backing GrTexture. Otherwise, it creates a cached texture
85// representation and releases it in the destructor.
86class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040087public:
bsalomonbcf0a522014-10-08 08:40:09 -070088 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070089
bsalomonbcf0a522014-10-08 08:40:09 -070090 AutoBitmapTexture(GrContext* context,
91 const SkBitmap& bitmap,
92 const GrTextureParams* params,
93 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040094 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070095 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040096 }
97
bsalomonbcf0a522014-10-08 08:40:09 -070098 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040099 const SkBitmap& bitmap,
100 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -0700101 // Either get the texture directly from the bitmap, or else use the cache and
102 // remember to unref it.
103 if (GrTexture* bmpTexture = bitmap.getTexture()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700104 fTexture.reset(nullptr);
bsalomonbcf0a522014-10-08 08:40:09 -0700105 return bmpTexture;
106 } else {
107 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
108 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400109 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400110 }
111
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000112private:
bsalomonbcf0a522014-10-08 08:40:09 -0700113 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000114};
115
116///////////////////////////////////////////////////////////////////////////////
117
118struct GrSkDrawProcs : public SkDrawProcs {
119public:
120 GrContext* fContext;
121 GrTextContext* fTextContext;
122 GrFontScaler* fFontScaler; // cached in the skia glyphcache
123};
124
125///////////////////////////////////////////////////////////////////////////////
126
bsalomon74f681d2015-06-23 14:38:48 -0700127/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
128 should fail. */
129bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
130 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
131 *flags = 0;
132 if (info) {
133 switch (info->alphaType()) {
134 case kPremul_SkAlphaType:
135 break;
136 case kOpaque_SkAlphaType:
137 *flags |= SkGpuDevice::kIsOpaque_Flag;
138 break;
139 default: // If it is unpremul or unknown don't try to render
140 return false;
141 }
142 }
143 if (kClear_InitContents == init) {
144 *flags |= kNeedClear_Flag;
145 }
146 return true;
147}
148
149SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props,
150 InitContents init) {
151 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, init);
senorblancod0d37ca2015-04-02 04:54:56 -0700152}
153
154SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
bsalomon74f681d2015-06-23 14:38:48 -0700155 const SkSurfaceProps* props, InitContents init) {
bsalomonafe30052015-01-16 07:32:33 -0800156 if (!rt || rt->wasDestroyed()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700157 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000158 }
bsalomon74f681d2015-06-23 14:38:48 -0700159 unsigned flags;
halcanary96fcdcc2015-08-27 07:41:13 -0700160 if (!CheckAlphaTypeAndGetFlags(nullptr, init, &flags)) {
161 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700162 }
halcanary385fe4d2015-08-26 13:07:48 -0700163 return new SkGpuDevice(rt, width, height, props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000164}
165
bsalomon74f681d2015-06-23 14:38:48 -0700166SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
167 const SkImageInfo& info, int sampleCount,
168 const SkSurfaceProps* props, InitContents init) {
169 unsigned flags;
170 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700171 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700172 }
173
174 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
halcanary96fcdcc2015-08-27 07:41:13 -0700175 if (nullptr == rt) {
176 return nullptr;
bsalomon74f681d2015-06-23 14:38:48 -0700177 }
178
halcanary385fe4d2015-08-26 13:07:48 -0700179 return new SkGpuDevice(rt, info.width(), info.height(), props, flags);
bsalomon74f681d2015-06-23 14:38:48 -0700180}
181
senorblancod0d37ca2015-04-02 04:54:56 -0700182SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
183 const SkSurfaceProps* props, unsigned flags)
robertphillipsfcf78292015-06-19 11:49:52 -0700184 : INHERITED(SkSurfacePropsCopyOrDefault(props))
reedb2db8982014-11-13 12:41:02 -0800185{
halcanary96fcdcc2015-08-27 07:41:13 -0700186 fDrawProcs = nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000187
bsalomonafe30052015-01-16 07:32:33 -0800188 fContext = SkRef(rt->getContext());
bsalomon74f681d2015-06-23 14:38:48 -0700189 fNeedClear = SkToBool(flags & kNeedClear_Flag);
190 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000191
bsalomonafe30052015-01-16 07:32:33 -0800192 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000193
bsalomon74f681d2015-06-23 14:38:48 -0700194 SkAlphaType at = fOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
195 SkImageInfo info = rt->surfacePriv().info(at).makeWH(width, height);
halcanary385fe4d2015-08-26 13:07:48 -0700196 SkPixelRef* pr = new SkGrPixelRef(info, rt);
bsalomonafbf2d62014-09-30 12:18:44 -0700197 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700198 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700199
robertphillipsc9a37062015-09-01 08:34:28 -0700200 fDrawContext.reset(fContext->drawContext(&this->surfaceProps()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000201}
202
kkinnunenabcfab42015-02-22 22:53:44 -0800203GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
204 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000205 if (kUnknown_SkColorType == origInfo.colorType() ||
206 origInfo.width() < 0 || origInfo.height() < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700207 return nullptr;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000208 }
209
bsalomonafe30052015-01-16 07:32:33 -0800210 if (!context) {
halcanary96fcdcc2015-08-27 07:41:13 -0700211 return nullptr;
bsalomonafe30052015-01-16 07:32:33 -0800212 }
213
reede5ea5002014-09-03 11:54:58 -0700214 SkColorType ct = origInfo.colorType();
215 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700216 if (kRGB_565_SkColorType == ct) {
217 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800218 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
219 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700220 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800221 }
222 if (kOpaque_SkAlphaType != at) {
223 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000224 }
reede5ea5002014-09-03 11:54:58 -0700225 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000226
bsalomonf2703d82014-10-28 14:33:06 -0700227 GrSurfaceDesc desc;
228 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000229 desc.fWidth = info.width();
230 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000231 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000232 desc.fSampleCnt = sampleCount;
bsalomond309e7a2015-04-30 14:18:54 -0700233 GrTexture* texture = context->textureProvider()->createTexture(
halcanary96fcdcc2015-08-27 07:41:13 -0700234 desc, SkToBool(budgeted), nullptr, 0);
235 if (nullptr == texture) {
236 return nullptr;
kkinnunenabcfab42015-02-22 22:53:44 -0800237 }
halcanary96fcdcc2015-08-27 07:41:13 -0700238 SkASSERT(nullptr != texture->asRenderTarget());
kkinnunenabcfab42015-02-22 22:53:44 -0800239 return texture->asRenderTarget();
240}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000241
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000242SkGpuDevice::~SkGpuDevice() {
243 if (fDrawProcs) {
244 delete fDrawProcs;
245 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000246
bsalomon32d0b3b2014-08-29 07:50:23 -0700247 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000248 fContext->unref();
249}
250
251///////////////////////////////////////////////////////////////////////////////
252
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000253bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
254 int x, int y) {
255 DO_DEFERRED_CLEAR();
256
257 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000258 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000259 if (kUnknown_GrPixelConfig == config) {
260 return false;
261 }
262
263 uint32_t flags = 0;
264 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
265 flags = GrContext::kUnpremul_PixelOpsFlag;
266 }
bsalomon74f681d2015-06-23 14:38:48 -0700267 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
268 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000269}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000270
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000271bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
272 int x, int y) {
273 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000274 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000275 if (kUnknown_GrPixelConfig == config) {
276 return false;
277 }
278 uint32_t flags = 0;
279 if (kUnpremul_SkAlphaType == info.alphaType()) {
280 flags = GrContext::kUnpremul_PixelOpsFlag;
281 }
282 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
283
284 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700285 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000286
287 return true;
288}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000289
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000290const SkBitmap& SkGpuDevice::onAccessBitmap() {
291 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700292 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000293}
294
reed41e010c2015-06-09 12:16:53 -0700295bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
296 DO_DEFERRED_CLEAR();
297 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
298 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
299 // ... ugh.
300 fLegacyBitmap.notifyPixelsChanged();
301 return false;
302}
303
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000304void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
305 INHERITED::onAttachToCanvas(canvas);
306
307 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800308 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000309}
310
311void SkGpuDevice::onDetachFromCanvas() {
312 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800313 fClip.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700314 fClipStack.reset(nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000315}
316
317// call this every draw call, to ensure that the context reflects our state,
318// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800319void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800320 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000321
joshualitt44701df2015-02-23 14:44:57 -0800322 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323
joshualitt570d2f82015-02-25 13:19:48 -0800324 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000325
326 DO_DEFERRED_CLEAR();
327}
328
329GrRenderTarget* SkGpuDevice::accessRenderTarget() {
robertphillips7156b092015-05-14 08:54:12 -0700330 DO_DEFERRED_CLEAR();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000331 return fRenderTarget;
332}
333
reed8eddfb52014-12-04 07:50:14 -0800334void SkGpuDevice::clearAll() {
335 GrColor color = 0;
336 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
337 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillipsea461502015-05-26 11:38:03 -0700338 fDrawContext->clear(fRenderTarget, &rect, color, true);
bsalomonafe30052015-01-16 07:32:33 -0800339 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800340}
341
kkinnunenabcfab42015-02-22 22:53:44 -0800342void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
343 // Caller must have accessed the render target, because it knows the rt must be replaced.
344 SkASSERT(!fNeedClear);
345
346 SkSurface::Budgeted budgeted =
347 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
348 : SkSurface::kNo_Budgeted;
349
350 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
vbuzinovdded6962015-06-12 08:59:45 -0700351 fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt));
kkinnunenabcfab42015-02-22 22:53:44 -0800352
halcanary96fcdcc2015-08-27 07:41:13 -0700353 if (nullptr == newRT) {
kkinnunenabcfab42015-02-22 22:53:44 -0800354 return;
355 }
356
357 if (shouldRetainContent) {
358 if (fRenderTarget->wasDestroyed()) {
359 return;
360 }
361 this->context()->copySurface(newRT, fRenderTarget);
362 }
363
364 SkASSERT(fRenderTarget != newRT);
365
366 fRenderTarget->unref();
367 fRenderTarget = newRT.detach();
368
bsalomon74f681d2015-06-23 14:38:48 -0700369#ifdef SK_DEBUG
370 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
371 kPremul_SkAlphaType);
372 SkASSERT(info == fLegacyBitmap.info());
373#endif
halcanary385fe4d2015-08-26 13:07:48 -0700374 SkPixelRef* pr = new SkGrPixelRef(fLegacyBitmap.info(), fRenderTarget);
kkinnunenabcfab42015-02-22 22:53:44 -0800375 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700376
robertphillipsc9a37062015-09-01 08:34:28 -0700377 fDrawContext.reset(fRenderTarget->getContext()->drawContext(&this->surfaceProps()));
kkinnunenabcfab42015-02-22 22:53:44 -0800378}
379
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000380///////////////////////////////////////////////////////////////////////////////
381
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000382void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800383 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700384 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000385
386 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700387 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700388 return;
389 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000390
robertphillipsea461502015-05-26 11:38:03 -0700391 fDrawContext->drawPaint(fRenderTarget, fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000392}
393
394// must be in SkCanvas::PointMode order
395static const GrPrimitiveType gPointMode2PrimtiveType[] = {
396 kPoints_GrPrimitiveType,
397 kLines_GrPrimitiveType,
398 kLineStrip_GrPrimitiveType
399};
400
ethannicholas330bb952015-07-17 06:44:02 -0700401// suppress antialiasing on axis-aligned integer-coordinate lines
402static bool needs_antialiasing(SkCanvas::PointMode mode, size_t count, const SkPoint pts[]) {
403 if (mode == SkCanvas::PointMode::kPoints_PointMode) {
404 return false;
405 }
406 if (count == 2) {
407 // We do not antialias as long as the primary axis of the line is integer-aligned, even if
408 // the other coordinates are not. This does mean the two end pixels of the line will be
409 // sharp even when they shouldn't be, but turning antialiasing on (as things stand
410 // currently) means that the line will turn into a two-pixel-wide blur. While obviously a
411 // more complete fix is possible down the road, for the time being we accept the error on
412 // the two end pixels as being the lesser of two evils.
413 if (pts[0].fX == pts[1].fX) {
414 return ((int) pts[0].fX) != pts[0].fX;
415 }
416 if (pts[0].fY == pts[1].fY) {
417 return ((int) pts[0].fY) != pts[0].fY;
418 }
419 }
420 return true;
421}
422
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000423void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
424 size_t count, const SkPoint pts[], const SkPaint& paint) {
425 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800426 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000427
428 SkScalar width = paint.getStrokeWidth();
429 if (width < 0) {
430 return;
431 }
432
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000433 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700434 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
435 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700436 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700437 return;
438 }
egdaniele61c4112014-06-12 10:24:21 -0700439 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700440 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700441 path.moveTo(pts[0]);
442 path.lineTo(pts[1]);
robertphillipsea461502015-05-26 11:38:03 -0700443 fDrawContext->drawPath(fRenderTarget, fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700444 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000445 }
446
ethannicholas330bb952015-07-17 06:44:02 -0700447 // we only handle non-antialiased hairlines and paints without path effects or mask filters,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000448 // else we let the SkDraw call our drawPath()
ethannicholas330bb952015-07-17 06:44:02 -0700449 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter() ||
450 (paint.isAntiAlias() && needs_antialiasing(mode, count, pts))) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000451 draw.drawPoints(mode, count, pts, paint, true);
452 return;
453 }
454
455 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700456 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700457 return;
458 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000459
robertphillipsea461502015-05-26 11:38:03 -0700460 fDrawContext->drawVertices(fRenderTarget,
461 fClip,
462 grPaint,
463 *draw.fMatrix,
464 gPointMode2PrimtiveType[mode],
465 SkToS32(count),
466 (SkPoint*)pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700467 nullptr,
468 nullptr,
469 nullptr,
robertphillipsea461502015-05-26 11:38:03 -0700470 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000471}
472
473///////////////////////////////////////////////////////////////////////////////
474
475void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
476 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700477 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
478
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000479 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800480 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000481
482 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
483 SkScalar width = paint.getStrokeWidth();
484
485 /*
486 We have special code for hairline strokes, miter-strokes, bevel-stroke
487 and fills. Anything else we just call our path code.
488 */
489 bool usePath = doStroke && width > 0 &&
490 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
491 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
egdanield58a0ba2014-06-11 10:30:05 -0700492
robertphillipsd8aa59d2015-08-05 09:07:12 -0700493 // a few other reasons we might need to call drawPath...
494 if (paint.getMaskFilter() ||
495 paint.getStyle() == SkPaint::kStrokeAndFill_Style) { // we can't both stroke and fill rects
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000496 usePath = true;
497 }
egdanield58a0ba2014-06-11 10:30:05 -0700498
joshualitt5531d512014-12-17 15:50:11 -0800499 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000500 usePath = true;
501 }
502
egdanield58a0ba2014-06-11 10:30:05 -0700503 GrStrokeInfo strokeInfo(paint);
504
505 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700506 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700507 usePath = true;
508 }
509
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000510 if (usePath) {
511 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700512 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000513 path.addRect(rect);
halcanary96fcdcc2015-08-27 07:41:13 -0700514 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000515 return;
516 }
517
518 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700519 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700520 return;
521 }
Mike Klein744fb732014-06-23 15:13:26 -0400522
robertphillipsea461502015-05-26 11:38:03 -0700523 fDrawContext->drawRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000524}
525
526///////////////////////////////////////////////////////////////////////////////
527
528void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800529 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700530 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000531 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800532 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000533
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000534 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700535 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700536 return;
537 }
Mike Klein744fb732014-06-23 15:13:26 -0400538
egdanield58a0ba2014-06-11 10:30:05 -0700539 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000540 if (paint.getMaskFilter()) {
541 // try to hit the fast path for drawing filtered round rects
542
543 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800544 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000545 if (devRRect.allCornersCircular()) {
546 SkRect maskRect;
robertphillips30c4cae2015-09-15 10:20:55 -0700547 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect,
joshualitt5531d512014-12-17 15:50:11 -0800548 draw.fClip->getBounds(),
549 *draw.fMatrix,
550 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000551 SkIRect finalIRect;
552 maskRect.roundOut(&finalIRect);
553 if (draw.fClip->quickReject(finalIRect)) {
554 // clipped out
555 return;
556 }
robertphillipsff0ca5e2015-07-22 11:54:44 -0700557 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext->textureProvider(),
558 fDrawContext,
joshualitt25d9c152015-02-18 12:29:52 -0800559 fRenderTarget,
560 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800561 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800562 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700563 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700564 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000565 return;
566 }
567 }
568
569 }
570 }
571
572 }
573
egdanield58a0ba2014-06-11 10:30:05 -0700574 bool usePath = false;
575
576 if (paint.getMaskFilter()) {
577 usePath = true;
578 } else {
579 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700580 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700581 usePath = true;
582 }
583 }
584
585
586 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000587 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700588 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000589 path.addRRect(rect);
halcanary96fcdcc2015-08-27 07:41:13 -0700590 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000591 return;
592 }
Mike Klein744fb732014-06-23 15:13:26 -0400593
robertphillipsea461502015-05-26 11:38:03 -0700594 fDrawContext->drawRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000595}
596
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000597void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800598 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000599 SkStrokeRec stroke(paint);
600 if (stroke.isFillStyle()) {
601
602 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800603 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000604
605 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700606 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700607 return;
608 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000609
halcanary96fcdcc2015-08-27 07:41:13 -0700610 if (nullptr == paint.getMaskFilter() && nullptr == paint.getPathEffect()) {
robertphillipsea461502015-05-26 11:38:03 -0700611 fDrawContext->drawDRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000612 return;
613 }
614 }
615
616 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700617 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000618 path.addRRect(outer);
619 path.addRRect(inner);
620 path.setFillType(SkPath::kEvenOdd_FillType);
621
halcanary96fcdcc2015-08-27 07:41:13 -0700622 this->drawPath(draw, path, paint, nullptr, true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000623}
624
625
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000626/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000627
628void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
629 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700630 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000631 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800632 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000633
egdanield58a0ba2014-06-11 10:30:05 -0700634 GrStrokeInfo strokeInfo(paint);
635
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000636 bool usePath = false;
637 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700638 if (paint.getMaskFilter()) {
robertphillips30c4cae2015-09-15 10:20:55 -0700639 // The RRect path can handle special case blurring
640 SkRRect rr = SkRRect::MakeOval(oval);
641 return this->drawRRect(draw, rr, paint);
egdanield58a0ba2014-06-11 10:30:05 -0700642 } else {
643 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700644 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700645 usePath = true;
646 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000647 }
648
649 if (usePath) {
650 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700651 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000652 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700653 this->drawPath(draw, path, paint, nullptr, true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000654 return;
655 }
656
657 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -0700658 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -0700659 return;
660 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000661
robertphillipsea461502015-05-26 11:38:03 -0700662 fDrawContext->drawOval(fRenderTarget, fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000663}
664
665#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000666
667///////////////////////////////////////////////////////////////////////////////
668
robertphillipsccb1b572015-05-27 11:02:55 -0700669static SkBitmap wrap_texture(GrTexture* texture, int width, int height) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000670 SkBitmap result;
senorblancod0d37ca2015-04-02 04:54:56 -0700671 result.setInfo(SkImageInfo::MakeN32Premul(width, height));
halcanary385fe4d2015-08-26 13:07:48 -0700672 result.setPixelRef(new SkGrPixelRef(result.info(), texture))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000673 return result;
674}
675
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000676void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
677 const SkPaint& paint, const SkMatrix* prePathMatrix,
678 bool pathIsMutable) {
679 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800680 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700681 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000682
robertphillipsccb1b572015-05-27 11:02:55 -0700683 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, fRenderTarget,
684 fClip, origSrcPath, paint,
685 *draw.fMatrix, prePathMatrix,
686 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000687}
688
689static const int kBmpSmallTileSize = 1 << 10;
690
691static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
692 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
693 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
694 return tilesX * tilesY;
695}
696
reed85d91782015-09-10 14:33:38 -0700697static int determine_tile_size(const SkIRect& src, int maxTileSize) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000698 if (maxTileSize <= kBmpSmallTileSize) {
699 return maxTileSize;
700 }
701
702 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
703 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
704
705 maxTileTotalTileSize *= maxTileSize * maxTileSize;
706 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
707
708 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
709 return kBmpSmallTileSize;
710 } else {
711 return maxTileSize;
712 }
713}
714
715// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
716// pixels from the bitmap are necessary.
bsalomon74f681d2015-06-23 14:38:48 -0700717static void determine_clipped_src_rect(const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800718 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800719 const SkMatrix& viewMatrix,
reed85d91782015-09-10 14:33:38 -0700720 const SkISize& imageSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000721 const SkRect* srcRectPtr,
722 SkIRect* clippedSrcIRect) {
halcanary96fcdcc2015-08-27 07:41:13 -0700723 clip.getConservativeBounds(rt, clippedSrcIRect, nullptr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000724 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800725 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000726 clippedSrcIRect->setEmpty();
727 return;
728 }
729 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
730 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700731 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000732 // we've setup src space 0,0 to map to the top left of the src rect.
733 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000734 if (!clippedSrcRect.intersect(*srcRectPtr)) {
735 clippedSrcIRect->setEmpty();
736 return;
737 }
738 }
739 clippedSrcRect.roundOut(clippedSrcIRect);
reed85d91782015-09-10 14:33:38 -0700740 SkIRect bmpBounds = SkIRect::MakeSize(imageSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000741 if (!clippedSrcIRect->intersect(bmpBounds)) {
742 clippedSrcIRect->setEmpty();
743 }
744}
745
reed85d91782015-09-10 14:33:38 -0700746bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
747 const SkMatrix& viewMatrix,
748 const GrTextureParams& params,
749 const SkRect* srcRectPtr,
750 int maxTileSize,
751 int* tileSize,
752 SkIRect* clippedSubset) const {
753 // if it's larger than the max tile size, then we have no choice but tiling.
754 if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
755 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(),
756 srcRectPtr, clippedSubset);
757 *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
758 return true;
759 }
760
761 const size_t area = imageRect.width() * imageRect.height();
762 if (area < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
763 return false;
764 }
765
766 // if the entire image/bitmap is already in our cache then no reason to tile it
767 if (GrIsImageInCache(fContext, imageID, imageRect, nullptr, &params)) {
768 return false;
769 }
770
771 // At this point we know we could do the draw by uploading the entire bitmap
772 // as a texture. However, if the texture would be large compared to the
773 // cache size and we don't require most of it for this draw then tile to
774 // reduce the amount of upload and cache spill.
775
776 // assumption here is that sw bitmap size is a good proxy for its size as
777 // a texture
778 size_t bmpSize = area * sizeof(SkPMColor); // assume 32bit pixels
779 size_t cacheSize;
780 fContext->getResourceCacheLimits(nullptr, &cacheSize);
781 if (bmpSize < cacheSize / 2) {
782 return false;
783 }
784
785 // Figure out how much of the src we will need based on the src rect and clipping.
786 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, imageRect.size(), srcRectPtr,
787 clippedSubset);
788 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
789 size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
790 kBmpSmallTileSize * kBmpSmallTileSize;
791
792 return usedTileBytes < 2 * bmpSize;
793}
794
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000795bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800796 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000797 const GrTextureParams& params,
798 const SkRect* srcRectPtr,
799 int maxTileSize,
800 int* tileSize,
801 SkIRect* clippedSrcRect) const {
802 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700803 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000804 return false;
805 }
806
reed85d91782015-09-10 14:33:38 -0700807 return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
808 srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
809}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000810
reed85d91782015-09-10 14:33:38 -0700811bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
812 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
813 const SkMatrix& viewMatrix) const {
814 // if image is explictly texture backed then just use the texture
815 if (as_IB(image)->peekTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000816 return false;
817 }
818
reed85d91782015-09-10 14:33:38 -0700819 GrTextureParams params;
820 bool doBicubic;
821 GrTextureParams::FilterMode textureFilterMode =
822 GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic);
823
824 int tileFilterPad;
825 if (doBicubic) {
826 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
827 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
828 tileFilterPad = 0;
829 } else {
830 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000831 }
reed85d91782015-09-10 14:33:38 -0700832 params.setFilterMode(textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000833
reed85d91782015-09-10 14:33:38 -0700834 int maxTileSize = fContext->caps()->maxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000835
reed85d91782015-09-10 14:33:38 -0700836 // these are output, which we safely ignore, as we just want to know the predicate
837 int outTileSize;
838 SkIRect outClippedSrcRect;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000839
reed85d91782015-09-10 14:33:38 -0700840 return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr,
841 maxTileSize, &outTileSize, &outClippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000842}
843
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000844void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000845 const SkBitmap& bitmap,
846 const SkMatrix& m,
847 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000848 SkMatrix concat;
849 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
850 if (!m.isIdentity()) {
851 concat.setConcat(*draw->fMatrix, m);
852 draw.writable()->fMatrix = &concat;
853 }
halcanary96fcdcc2015-08-27 07:41:13 -0700854 this->drawBitmapCommon(*draw, bitmap, nullptr, nullptr, paint, SkCanvas::kStrict_SrcRectConstraint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000855}
856
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000857// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000858// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
859// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000860static inline void clamped_outset_with_offset(SkIRect* iRect,
861 int outset,
862 SkPoint* offset,
863 const SkIRect& clamp) {
864 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000865
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000866 int leftClampDelta = clamp.fLeft - iRect->fLeft;
867 if (leftClampDelta > 0) {
868 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000869 iRect->fLeft = clamp.fLeft;
870 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000871 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000872 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000873
874 int topClampDelta = clamp.fTop - iRect->fTop;
875 if (topClampDelta > 0) {
876 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000877 iRect->fTop = clamp.fTop;
878 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000879 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000880 }
881
882 if (iRect->fRight > clamp.fRight) {
883 iRect->fRight = clamp.fRight;
884 }
885 if (iRect->fBottom > clamp.fBottom) {
886 iRect->fBottom = clamp.fBottom;
887 }
888}
889
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000890static bool has_aligned_samples(const SkRect& srcRect,
891 const SkRect& transformedRect) {
892 // detect pixel disalignment
893 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
894 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
895 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
896 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
897 SkScalarAbs(transformedRect.width() - srcRect.width()) <
898 COLOR_BLEED_TOLERANCE &&
899 SkScalarAbs(transformedRect.height() - srcRect.height()) <
900 COLOR_BLEED_TOLERANCE) {
901 return true;
902 }
903 return false;
904}
905
906static bool may_color_bleed(const SkRect& srcRect,
907 const SkRect& transformedRect,
robertphillips1accc4c2015-07-20 10:22:29 -0700908 const SkMatrix& m,
909 bool isMSAA) {
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000910 // Only gets called if has_aligned_samples returned false.
911 // So we can assume that sampling is axis aligned but not texel aligned.
912 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
913 SkRect innerSrcRect(srcRect), innerTransformedRect,
914 outerTransformedRect(transformedRect);
robertphillips1accc4c2015-07-20 10:22:29 -0700915 if (isMSAA) {
916 innerSrcRect.inset(SK_Scalar1, SK_Scalar1);
917 } else {
918 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
919 }
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000920 m.mapRect(&innerTransformedRect, innerSrcRect);
921
922 // The gap between outerTransformedRect and innerTransformedRect
923 // represents the projection of the source border area, which is
924 // problematic for color bleeding. We must check whether any
925 // destination pixels sample the border area.
926 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
927 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
928 SkIRect outer, inner;
929 outerTransformedRect.round(&outer);
930 innerTransformedRect.round(&inner);
931 // If the inner and outer rects round to the same result, it means the
932 // border does not overlap any pixel centers. Yay!
933 return inner != outer;
934}
935
936static bool needs_texture_domain(const SkBitmap& bitmap,
937 const SkRect& srcRect,
938 GrTextureParams &params,
939 const SkMatrix& contextMatrix,
robertphillips1accc4c2015-07-20 10:22:29 -0700940 bool bicubic,
941 bool isMSAA) {
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000942 bool needsTextureDomain = false;
senorblancod0d37ca2015-04-02 04:54:56 -0700943 GrTexture* tex = bitmap.getTexture();
944 int width = tex ? tex->width() : bitmap.width();
945 int height = tex ? tex->height() : bitmap.height();
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000946
947 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
948 // Need texture domain if drawing a sub rect
senorblancod0d37ca2015-04-02 04:54:56 -0700949 needsTextureDomain = srcRect.width() < width ||
950 srcRect.height() < height;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000951 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
952 // sampling is axis-aligned
953 SkRect transformedRect;
954 contextMatrix.mapRect(&transformedRect, srcRect);
955
956 if (has_aligned_samples(srcRect, transformedRect)) {
957 params.setFilterMode(GrTextureParams::kNone_FilterMode);
958 needsTextureDomain = false;
959 } else {
robertphillips1accc4c2015-07-20 10:22:29 -0700960 needsTextureDomain = may_color_bleed(srcRect, transformedRect,
961 contextMatrix, isMSAA);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000962 }
963 }
964 }
965 return needsTextureDomain;
966}
967
joshualitta61c8172015-08-17 10:51:22 -0700968static void draw_aa_bitmap(GrDrawContext* drawContext, GrContext* context,
969 GrRenderTarget* renderTarget, const GrClip& clip,
970 const SkMatrix& viewMatrix, const SkMatrix& srcRectToDstRect,
971 const SkPaint& paint, const SkBitmap* bitmapPtr, const SkSize& dstSize) {
972 SkShader::TileMode tm[] = {
973 SkShader::kClamp_TileMode,
974 SkShader::kClamp_TileMode,
975 };
976
977 bool doBicubic;
978 GrTextureParams::FilterMode textureFilterMode =
979 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix,
980 srcRectToDstRect,
981 &doBicubic);
982
983 // Setup texture to wrap bitmap
984 GrTextureParams params(tm, textureFilterMode);
985 SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, *bitmapPtr, &params));
986
987 if (!texture) {
988 SkErrorInternals::SetError(kInternalError_SkError,
989 "Couldn't convert bitmap to texture.");
990 return;
991 }
992
joshualitta61c8172015-08-17 10:51:22 -0700993
994 GrPaint grPaint;
995
996 // Create and insert texture effect
997 SkAutoTUnref<const GrFragmentProcessor> fp;
998 if (doBicubic) {
999 fp.reset(GrBicubicEffect::Create(grPaint.getProcessorDataManager(), texture,
1000 SkMatrix::I(),
1001 tm));
1002 } else {
1003 fp.reset(GrSimpleTextureEffect::Create(grPaint.getProcessorDataManager(), texture,
1004 SkMatrix::I(), params));
1005 }
1006
bsalomonf1b7a1d2015-09-28 06:26:28 -07001007 if (kAlpha_8_SkColorType == bitmapPtr->colorType()) {
1008 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1009 } else {
1010 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
joshualitta61c8172015-08-17 10:51:22 -07001011 }
1012
bsalomonf1b7a1d2015-09-28 06:26:28 -07001013 if (!SkPaintToGrPaintReplaceShader(context, paint, fp, &grPaint)) {
1014 return;
1015 }
joshualitta61c8172015-08-17 10:51:22 -07001016
1017 // Setup dst rect and final matrix
1018 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1019
1020 SkRect devRect;
1021 viewMatrix.mapRect(&devRect, dstRect);
1022
1023 SkMatrix matrix;
1024 matrix.setIDiv(bitmapPtr->width(), bitmapPtr->height());
1025
1026 SkMatrix dstRectToSrcRect;
1027 if (!srcRectToDstRect.invert(&dstRectToSrcRect)) {
1028 return;
1029 }
1030 matrix.preConcat(dstRectToSrcRect);
1031
joshualittd2b23e02015-08-21 10:53:34 -07001032 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFill(grPaint.getColor(),
joshualitta61c8172015-08-17 10:51:22 -07001033 viewMatrix,
1034 matrix,
1035 dstRect,
1036 devRect));
1037
1038 drawContext->drawBatch(renderTarget, clip, grPaint, batch);
1039}
1040
reed3322a812015-09-16 10:09:24 -07001041static bool can_ignore_strict_subset_constraint(const SkBitmap& bitmap, const SkRect& subset) {
1042 GrTexture* tex = bitmap.getTexture();
1043 int width = tex ? tex->width() : bitmap.width();
1044 int height = tex ? tex->height() : bitmap.height();
1045 return subset.contains(SkRect::MakeIWH(width, height));
1046}
1047
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001048void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1049 const SkBitmap& bitmap,
1050 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001051 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001052 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001053 SkCanvas::SrcRectConstraint constraint) {
joshualitt5531d512014-12-17 15:50:11 -08001054 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001055
1056 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001057 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001058 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1059 // in the (easier) bleed case, so update flags.
halcanary96fcdcc2015-08-27 07:41:13 -07001060 if (nullptr == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001061 SkScalar w = SkIntToScalar(bitmap.width());
1062 SkScalar h = SkIntToScalar(bitmap.height());
1063 dstSize.fWidth = w;
1064 dstSize.fHeight = h;
1065 srcRect.set(0, 0, w, h);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001066 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001067 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001068 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001069 dstSize = *dstSizePtr;
senorblancod0d37ca2015-04-02 04:54:56 -07001070 }
reed3322a812015-09-16 10:09:24 -07001071
1072 if (can_ignore_strict_subset_constraint(bitmap, srcRect)) {
reeda5517e22015-07-14 10:54:12 -07001073 constraint = SkCanvas::kFast_SrcRectConstraint;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001074 }
1075
derekf367e1862014-12-02 11:02:06 -08001076 // If the render target is not msaa and draw is antialiased, we call
1077 // drawRect instead of drawing on the render target directly.
1078 // FIXME: the tiled bitmap code path doesn't currently support
1079 // anti-aliased edges, we work around that for now by drawing directly
1080 // if the image size exceeds maximum texture size.
bsalomon76228632015-05-29 08:02:10 -07001081 int maxTextureSize = fContext->caps()->maxTextureSize();
joshualitt1a899c92015-08-17 11:53:44 -07001082 bool drawAA = !fRenderTarget->isUnifiedMultisampled() &&
1083 paint.isAntiAlias() &&
1084 bitmap.width() <= maxTextureSize &&
1085 bitmap.height() <= maxTextureSize;
derekf367e1862014-12-02 11:02:06 -08001086
joshualitt1a899c92015-08-17 11:53:44 -07001087 if (paint.getMaskFilter() || drawAA) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001088 // Convert the bitmap to a shader so that the rect can be drawn
1089 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001090 SkBitmap tmp; // subset of bitmap, if necessary
1091 const SkBitmap* bitmapPtr = &bitmap;
joshualitta61c8172015-08-17 10:51:22 -07001092 SkMatrix srcRectToDstRect;
bsalomon49f085d2014-09-05 13:34:00 -07001093 if (srcRectPtr) {
joshualitta61c8172015-08-17 10:51:22 -07001094 srcRectToDstRect.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1095 srcRectToDstRect.postScale(dstSize.fWidth / srcRectPtr->width(),
1096 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001097 // In bleed mode we position and trim the bitmap based on the src rect which is
1098 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1099 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1100 // compensate.
reeda5517e22015-07-14 10:54:12 -07001101 if (SkCanvas::kStrict_SrcRectConstraint == constraint) {
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001102 SkIRect iSrc;
1103 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001104
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001105 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1106 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001107
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001108 if (!bitmap.extractSubset(&tmp, iSrc)) {
1109 return; // extraction failed
1110 }
1111 bitmapPtr = &tmp;
1112 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001113
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001114 // The source rect has changed so update the matrix
joshualitta61c8172015-08-17 10:51:22 -07001115 srcRectToDstRect.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001116 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001117 } else {
joshualitta61c8172015-08-17 10:51:22 -07001118 srcRectToDstRect.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001119 }
1120
joshualitta61c8172015-08-17 10:51:22 -07001121 // If we have a maskfilter then we can't batch, so we take a slow path. However, we fast
1122 // path the case where we are drawing an AA rect so we can batch many drawImageRect calls
1123 if (paint.getMaskFilter()) {
1124 SkPaint paintWithShader(paint);
1125 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
1126 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
1127 &srcRectToDstRect))->unref();
1128 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1129 this->drawRect(draw, dstRect, paintWithShader);
1130 } else {
1131 draw_aa_bitmap(fDrawContext, fContext, fRenderTarget, fClip, *draw.fMatrix,
1132 srcRectToDstRect, paint, bitmapPtr, dstSize);
1133 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001134
1135 return;
1136 }
1137
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001138 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1139 // the view matrix rather than a local matrix.
joshualitt5531d512014-12-17 15:50:11 -08001140 SkMatrix viewM = *draw.fMatrix;
reed85d91782015-09-10 14:33:38 -07001141 viewM.preScale(dstSize.fWidth / srcRect.width(),
1142 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001143
1144 GrTextureParams params;
joshualitt9bc39542015-08-12 12:57:54 -07001145 bool doBicubic;
1146 GrTextureParams::FilterMode textureFilterMode =
1147 GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewM, SkMatrix::I(),
1148 &doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001149
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001150 int tileFilterPad;
1151 if (doBicubic) {
1152 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1153 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1154 tileFilterPad = 0;
1155 } else {
1156 tileFilterPad = 1;
1157 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001158 params.setFilterMode(textureFilterMode);
1159
bsalomon76228632015-05-29 08:02:10 -07001160 int maxTileSize = fContext->caps()->maxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001161 int tileSize;
1162
1163 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001164 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001165 &clippedSrcRect)) {
reeda5517e22015-07-14 10:54:12 -07001166 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, constraint,
joshualitt5531d512014-12-17 15:50:11 -08001167 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001168 } else {
1169 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001170 bool needsTextureDomain = needs_texture_domain(bitmap,
1171 srcRect,
1172 params,
joshualitt5531d512014-12-17 15:50:11 -08001173 viewM,
robertphillips1accc4c2015-07-20 10:22:29 -07001174 doBicubic,
1175 fRenderTarget->isUnifiedMultisampled());
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001176 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001177 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001178 srcRect,
1179 params,
1180 paint,
reeda5517e22015-07-14 10:54:12 -07001181 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001182 doBicubic,
1183 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001184 }
1185}
1186
1187// Break 'bitmap' into several tiles to draw it since it has already
1188// been determined to be too large to fit in VRAM
1189void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001190 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001191 const SkRect& srcRect,
1192 const SkIRect& clippedSrcIRect,
1193 const GrTextureParams& params,
1194 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001195 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001196 int tileSize,
1197 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001198 // The following pixel lock is technically redundant, but it is desirable
1199 // to lock outside of the tile loop to prevent redecoding the whole image
1200 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1201 // is larger than the limit of the discardable memory pool.
1202 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001203 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1204
1205 int nx = bitmap.width() / tileSize;
1206 int ny = bitmap.height() / tileSize;
1207 for (int x = 0; x <= nx; x++) {
1208 for (int y = 0; y <= ny; y++) {
1209 SkRect tileR;
1210 tileR.set(SkIntToScalar(x * tileSize),
1211 SkIntToScalar(y * tileSize),
1212 SkIntToScalar((x + 1) * tileSize),
1213 SkIntToScalar((y + 1) * tileSize));
1214
1215 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1216 continue;
1217 }
1218
1219 if (!tileR.intersect(srcRect)) {
1220 continue;
1221 }
1222
1223 SkBitmap tmpB;
1224 SkIRect iTileR;
1225 tileR.roundOut(&iTileR);
1226 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1227 SkIntToScalar(iTileR.fTop));
1228
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001229 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001230 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001231 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001232 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001233 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001234
robertphillipsec8bb942014-11-21 10:16:25 -08001235 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001236 SkIRect iClampRect;
1237
reeda5517e22015-07-14 10:54:12 -07001238 if (SkCanvas::kFast_SrcRectConstraint == constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001239 // In bleed mode we want to always expand the tile on all edges
1240 // but stay within the bitmap bounds
1241 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1242 } else {
1243 // In texture-domain/clamp mode we only want to expand the
1244 // tile on edges interior to "srcRect" (i.e., we want to
1245 // not bleed across the original clamped edges)
1246 srcRect.roundOut(&iClampRect);
1247 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001248 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1249 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001250 }
1251
1252 if (bitmap.extractSubset(&tmpB, iTileR)) {
1253 // now offset it to make it "local" to our tmp bitmap
1254 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001255 GrTextureParams paramsTemp = params;
robertphillips1accc4c2015-07-20 10:22:29 -07001256 bool needsTextureDomain = needs_texture_domain(
1257 bitmap, srcRect, paramsTemp,
1258 viewM, bicubic,
1259 fRenderTarget->isUnifiedMultisampled());
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001260 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001261 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001262 tileR,
1263 paramsTemp,
1264 paint,
reeda5517e22015-07-14 10:54:12 -07001265 constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001266 bicubic,
1267 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001268 }
1269 }
1270 }
1271}
1272
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001273
1274/*
1275 * This is called by drawBitmap(), which has to handle images that may be too
1276 * large to be represented by a single texture.
1277 *
1278 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1279 * and that non-texture portion of the GrPaint has already been setup.
1280 */
1281void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001282 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001283 const SkRect& srcRect,
1284 const GrTextureParams& params,
1285 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -07001286 SkCanvas::SrcRectConstraint constraint,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001287 bool bicubic,
1288 bool needsTextureDomain) {
bsalomon76228632015-05-29 08:02:10 -07001289 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1290 bitmap.height() <= fContext->caps()->maxTextureSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001291
1292 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001293 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
halcanary96fcdcc2015-08-27 07:41:13 -07001294 if (nullptr == texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001295 return;
1296 }
1297
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001298 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001299 SkRect paintRect;
1300 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1301 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1302 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1303 SkScalarMul(srcRect.fTop, hInv),
1304 SkScalarMul(srcRect.fRight, wInv),
1305 SkScalarMul(srcRect.fBottom, hInv));
1306
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001307 SkRect textureDomain = SkRect::MakeEmpty();
joshualitt5f10b5c2015-07-09 10:24:35 -07001308
1309 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1310 // the rest from the SkPaint.
1311 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001312 SkAutoTUnref<const GrFragmentProcessor> fp;
joshualitt5f10b5c2015-07-09 10:24:35 -07001313
reeda5517e22015-07-14 10:54:12 -07001314 if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001315 // Use a constrained texture domain to avoid color bleeding
1316 SkScalar left, top, right, bottom;
1317 if (srcRect.width() > SK_Scalar1) {
1318 SkScalar border = SK_ScalarHalf / texture->width();
1319 left = paintRect.left() + border;
1320 right = paintRect.right() - border;
1321 } else {
1322 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1323 }
1324 if (srcRect.height() > SK_Scalar1) {
1325 SkScalar border = SK_ScalarHalf / texture->height();
1326 top = paintRect.top() + border;
1327 bottom = paintRect.bottom() - border;
1328 } else {
1329 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1330 }
1331 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001332 if (bicubic) {
joshualitt5f10b5c2015-07-09 10:24:35 -07001333 fp.reset(GrBicubicEffect::Create(grPaint.getProcessorDataManager(), texture,
1334 SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001335 } else {
joshualitt5f10b5c2015-07-09 10:24:35 -07001336 fp.reset(GrTextureDomainEffect::Create(grPaint.getProcessorDataManager(),
1337 texture,
joshualitt5531d512014-12-17 15:50:11 -08001338 SkMatrix::I(),
1339 textureDomain,
1340 GrTextureDomain::kClamp_Mode,
1341 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001342 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001343 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001344 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1345 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualitt5f10b5c2015-07-09 10:24:35 -07001346 fp.reset(GrBicubicEffect::Create(grPaint.getProcessorDataManager(), texture, SkMatrix::I(),
1347 tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001348 } else {
joshualitt5f10b5c2015-07-09 10:24:35 -07001349 fp.reset(GrSimpleTextureEffect::Create(grPaint.getProcessorDataManager(), texture,
1350 SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001351 }
1352
bsalomonf1b7a1d2015-09-28 06:26:28 -07001353 if (kAlpha_8_SkColorType == bitmap.colorType()) {
1354 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1355 } else {
1356 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1357 }
1358
1359 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001360 return;
1361 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001362
robertphillipsea461502015-05-26 11:38:03 -07001363 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dstRect,
1364 paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001365}
1366
fmalita2d97bc12014-11-20 10:44:58 -08001367bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001368 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001369 const SkImageFilter* filter,
1370 const SkImageFilter::Context& ctx,
1371 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001372 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001373
robertphillipsefbffed2015-06-22 12:06:08 -07001374 SkImageFilter::Proxy proxy(this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001375
1376 if (filter->canFilterImageGPU()) {
senorblancod0d37ca2015-04-02 04:54:56 -07001377 return filter->filterImageGPU(&proxy, wrap_texture(texture, width, height),
1378 ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001379 } else {
1380 return false;
1381 }
1382}
1383
1384void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1385 int left, int top, const SkPaint& paint) {
1386 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001387 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001388
1389 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1390 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1391 return;
1392 }
1393
1394 int w = bitmap.width();
1395 int h = bitmap.height();
1396
1397 GrTexture* texture;
1398 // draw sprite uses the default texture params
halcanary96fcdcc2015-08-27 07:41:13 -07001399 AutoBitmapTexture abt(fContext, bitmap, nullptr, &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001400 if (!texture) {
1401 return;
1402 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001403
bsalomonf1b7a1d2015-09-28 06:26:28 -07001404 bool alphaOnly = kAlpha_8_SkColorType == bitmap.colorType();
1405
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001406 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001407 // This bitmap will own the filtered result as a texture.
1408 SkBitmap filteredBitmap;
1409
bsalomon49f085d2014-09-05 13:34:00 -07001410 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001411 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001412 SkMatrix matrix(*draw.fMatrix);
1413 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001414 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001415 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001416 // This cache is transient, and is freed (along with all its contained
1417 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001418 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001419 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001420 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001421 texture = (GrTexture*) filteredBitmap.getTexture();
1422 w = filteredBitmap.width();
1423 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001424 left += offset.x();
1425 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001426 } else {
1427 return;
1428 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001429 SkASSERT(!GrPixelConfigIsAlphaOnly(texture->config()));
1430 alphaOnly = false;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001431 }
1432
1433 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001434 SkAutoTUnref<const GrFragmentProcessor> fp(
1435 GrSimpleTextureEffect::Create(grPaint.getProcessorDataManager(), texture, SkMatrix::I()));
1436 if (alphaOnly) {
1437 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1438 } else {
1439 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1440 }
1441 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001442 return;
1443 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001444
robertphillipsea461502015-05-26 11:38:03 -07001445 fDrawContext->drawNonAARectToRect(fRenderTarget,
1446 fClip,
1447 grPaint,
1448 SkMatrix::I(),
1449 SkRect::MakeXYWH(SkIntToScalar(left),
1450 SkIntToScalar(top),
1451 SkIntToScalar(w),
1452 SkIntToScalar(h)),
1453 SkRect::MakeXYWH(0,
1454 0,
1455 SK_Scalar1 * w / texture->width(),
1456 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001457}
1458
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001459void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001460 const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -07001461 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001462 SkMatrix matrix;
1463 SkRect bitmapBounds, tmpSrc;
1464
1465 bitmapBounds.set(0, 0,
1466 SkIntToScalar(bitmap.width()),
1467 SkIntToScalar(bitmap.height()));
1468
1469 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001470 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001471 tmpSrc = *src;
1472 } else {
1473 tmpSrc = bitmapBounds;
1474 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001475
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001476 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1477
1478 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001479 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001480 if (!bitmapBounds.contains(tmpSrc)) {
1481 if (!tmpSrc.intersect(bitmapBounds)) {
1482 return; // nothing to draw
1483 }
1484 }
1485 }
1486
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001487 SkRect tmpDst;
1488 matrix.mapRect(&tmpDst, tmpSrc);
1489
1490 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1491 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1492 // Translate so that tempDst's top left is at the origin.
1493 matrix = *origDraw.fMatrix;
1494 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1495 draw.writable()->fMatrix = &matrix;
1496 }
1497 SkSize dstSize;
1498 dstSize.fWidth = tmpDst.width();
1499 dstSize.fHeight = tmpDst.height();
1500
reeda5517e22015-07-14 10:54:12 -07001501 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, constraint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001502}
1503
1504void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1505 int x, int y, const SkPaint& paint) {
1506 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001507 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001508 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001509
1510 // TODO: If the source device covers the whole of this device, we could
1511 // omit fNeedsClear -related flushing.
1512 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001513
1514 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001515 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001516
1517 GrRenderTarget* devRT = dev->accessRenderTarget();
1518 GrTexture* devTex;
halcanary96fcdcc2015-08-27 07:41:13 -07001519 if (nullptr == (devTex = devRT->asTexture())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001520 return;
1521 }
1522
robertphillips7b9e8a42014-12-11 08:20:31 -08001523 const SkImageInfo ii = dev->imageInfo();
1524 int w = ii.width();
1525 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001526
1527 SkImageFilter* filter = paint.getImageFilter();
1528 // This bitmap will own the filtered result as a texture.
1529 SkBitmap filteredBitmap;
1530
bsalomon49f085d2014-09-05 13:34:00 -07001531 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001532 SkIPoint offset = SkIPoint::Make(0, 0);
1533 SkMatrix matrix(*draw.fMatrix);
1534 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001535 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001536 // This cache is transient, and is freed (along with all its contained
1537 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001538 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001539 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001540 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1541 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001542 devTex = filteredBitmap.getTexture();
1543 w = filteredBitmap.width();
1544 h = filteredBitmap.height();
1545 x += offset.fX;
1546 y += offset.fY;
1547 } else {
1548 return;
1549 }
1550 }
1551
1552 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001553 SkAutoTUnref<const GrFragmentProcessor> fp(
1554 GrSimpleTextureEffect::Create(grPaint.getProcessorDataManager(), devTex, SkMatrix::I()));
1555 if (GrPixelConfigIsAlphaOnly(devTex->config())) {
1556 // Can this happen?
1557 fp.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
1558 } else {
1559 fp.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
1560 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001561
bsalomonf1b7a1d2015-09-28 06:26:28 -07001562 if (!SkPaintToGrPaintReplaceShader(this->context(), paint, fp, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001563 return;
1564 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001565
1566 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1567 SkIntToScalar(y),
1568 SkIntToScalar(w),
1569 SkIntToScalar(h));
1570
1571 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1572 // scratch texture).
1573 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1574 SK_Scalar1 * h / devTex->height());
1575
robertphillipsea461502015-05-26 11:38:03 -07001576 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, SkMatrix::I(), dstRect,
1577 srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001578}
1579
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001580bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001581 return filter->canFilterImageGPU();
1582}
1583
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001584bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001585 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001586 SkBitmap* result, SkIPoint* offset) {
1587 // want explicitly our impl, so guard against a subclass of us overriding it
1588 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1589 return false;
1590 }
1591
1592 SkAutoLockPixels alp(src, !src.getTexture());
1593 if (!src.getTexture() && !src.readyToDraw()) {
1594 return false;
1595 }
1596
1597 GrTexture* texture;
1598 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1599 // must be pushed upstack.
halcanary96fcdcc2015-08-27 07:41:13 -07001600 AutoBitmapTexture abt(fContext, src, nullptr, &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001601 if (!texture) {
1602 return false;
1603 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001604
senorblancod0d37ca2015-04-02 04:54:56 -07001605 return this->filterTexture(fContext, texture, src.width(), src.height(),
1606 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001607}
1608
reed85d91782015-09-10 14:33:38 -07001609static bool wrap_as_bm(GrContext* ctx, const SkImage* image, SkBitmap* bm) {
1610 SkAutoTUnref<GrTexture> tex(as_IB(image)->asTextureRef(ctx, kUntiled_SkImageUsageType));
reeda85d4d02015-05-06 12:56:48 -07001611 if (tex) {
reed8b26b992015-05-07 15:36:17 -07001612 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), bm);
1613 return true;
reeda85d4d02015-05-06 12:56:48 -07001614 } else {
reed8b26b992015-05-07 15:36:17 -07001615 return as_IB(image)->getROPixels(bm);
reeda85d4d02015-05-06 12:56:48 -07001616 }
reeda85d4d02015-05-06 12:56:48 -07001617}
1618
1619void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1620 const SkPaint& paint) {
1621 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001622 if (GrTexture* tex = as_IB(image)->peekTexture()) {
1623 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), &bm);
1624 } else {
1625 if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
1626 paint.getFilterQuality(), *draw.fMatrix)) {
1627 // only support tiling as bitmap at the moment, so force raster-version
1628 if (!as_IB(image)->getROPixels(&bm)) {
1629 return;
1630 }
1631 } else {
1632 if (!wrap_as_bm(this->context(), image, &bm)) {
1633 return;
1634 }
1635 }
reeda85d4d02015-05-06 12:56:48 -07001636 }
reed85d91782015-09-10 14:33:38 -07001637 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
reeda85d4d02015-05-06 12:56:48 -07001638}
1639
1640void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
reeda5517e22015-07-14 10:54:12 -07001641 const SkRect& dst, const SkPaint& paint,
1642 SkCanvas::SrcRectConstraint constraint) {
reeda85d4d02015-05-06 12:56:48 -07001643 SkBitmap bm;
reed85d91782015-09-10 14:33:38 -07001644 if (GrTexture* tex = as_IB(image)->peekTexture()) {
1645 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), &bm);
1646 } else {
1647 SkMatrix viewMatrix = *draw.fMatrix;
1648 viewMatrix.preScale(dst.width() / (src ? src->width() : image->width()),
1649 dst.height() / (src ? src->height() : image->height()));
1650
1651 if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), viewMatrix)) {
1652 // only support tiling as bitmap at the moment, so force raster-version
1653 if (!as_IB(image)->getROPixels(&bm)) {
1654 return;
1655 }
1656 } else {
1657 if (!wrap_as_bm(this->context(), image, &bm)) {
1658 return;
1659 }
1660 }
reeda85d4d02015-05-06 12:56:48 -07001661 }
reed85d91782015-09-10 14:33:38 -07001662 this->drawBitmapRect(draw, bm, src, dst, paint, constraint);
reeda85d4d02015-05-06 12:56:48 -07001663}
1664
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001665///////////////////////////////////////////////////////////////////////////////
1666
1667// must be in SkCanvas::VertexMode order
1668static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1669 kTriangles_GrPrimitiveType,
1670 kTriangleStrip_GrPrimitiveType,
1671 kTriangleFan_GrPrimitiveType,
1672};
1673
1674void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1675 int vertexCount, const SkPoint vertices[],
1676 const SkPoint texs[], const SkColor colors[],
1677 SkXfermode* xmode,
1678 const uint16_t indices[], int indexCount,
1679 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001680 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001681 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001682
halcanary96fcdcc2015-08-27 07:41:13 -07001683 // If both textures and vertex-colors are nullptr, strokes hairlines with the paint's color.
1684 if ((nullptr == texs || nullptr == paint.getShader()) && nullptr == colors) {
mtklein533eb782014-08-27 10:39:42 -07001685
halcanary96fcdcc2015-08-27 07:41:13 -07001686 texs = nullptr;
mtklein533eb782014-08-27 10:39:42 -07001687
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001688 SkPaint copy(paint);
1689 copy.setStyle(SkPaint::kStroke_Style);
1690 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001691
bsalomonf1b7a1d2015-09-28 06:26:28 -07001692 GrPaint grPaint;
dandov32a311b2014-07-15 19:46:26 -07001693 // we ignore the shader if texs is null.
bsalomonf1b7a1d2015-09-28 06:26:28 -07001694 if (!SkPaintToGrPaintNoShader(this->context(), copy, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001695 return;
1696 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001697
dandov32a311b2014-07-15 19:46:26 -07001698 int triangleCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001699 int n = (nullptr == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001700 switch (vmode) {
1701 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001702 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001703 break;
1704 case SkCanvas::kTriangleStrip_VertexMode:
1705 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001706 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001707 break;
1708 }
mtklein533eb782014-08-27 10:39:42 -07001709
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001710 VertState state(vertexCount, indices, indexCount);
1711 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001712
dandov32a311b2014-07-15 19:46:26 -07001713 //number of indices for lines per triangle with kLines
1714 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001715
bsalomonf1b7a1d2015-09-28 06:26:28 -07001716 SkAutoTDeleteArray<uint16_t> lineIndices(new uint16_t[indexCount]);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001717 int i = 0;
1718 while (vertProc(&state)) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001719 lineIndices[i] = state.f0;
1720 lineIndices[i + 1] = state.f1;
1721 lineIndices[i + 2] = state.f1;
1722 lineIndices[i + 3] = state.f2;
1723 lineIndices[i + 4] = state.f2;
1724 lineIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001725 i += 6;
1726 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001727 fDrawContext->drawVertices(fRenderTarget,
1728 fClip,
1729 grPaint,
1730 *draw.fMatrix,
1731 kLines_GrPrimitiveType,
1732 vertexCount,
1733 vertices,
1734 texs,
1735 colors,
1736 lineIndices.get(),
1737 indexCount);
1738 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001739 }
1740
bsalomonf1b7a1d2015-09-28 06:26:28 -07001741 GrPrimitiveType primType = gVertexMode2PrimitiveType[vmode];
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001742
1743 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001744 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001745 // need to convert byte order and from non-PM to PM
1746 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001747 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001748 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001749 color = colors[i];
1750 if (paint.getAlpha() != 255) {
1751 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1752 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001753 /// TODO: Perform the premul after interpolating
1754 convertedColors[i] = SkColorToPremulGrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001755 }
1756 colors = convertedColors.get();
1757 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001758 GrPaint grPaint;
1759 if (texs && colors && paint.getShader()) {
1760 // When there are texs and colors the shader and colors are combined using xmode. A null
1761 // xmode is defined to mean modulate.
1762 SkXfermode::Mode colorMode;
1763 if (xmode) {
1764 if (!xmode->asMode(&colorMode)) {
1765 return;
1766 }
1767 } else {
1768 colorMode = SkXfermode::kModulate_Mode;
1769 }
1770 if (!SkPaintToGrPaintWithXfermode(this->context(), paint, *draw.fMatrix, colorMode, false,
1771 &grPaint)) {
1772 return;
1773 }
1774 } else if (!texs) {
1775 // Defined to ignore the shader unless texs is provided.
1776 if (!SkPaintToGrPaintNoShader(this->context(), paint, &grPaint)) {
1777 return;
1778 }
1779 } else if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
1780 return;
1781 }
1782
robertphillipsea461502015-05-26 11:38:03 -07001783 fDrawContext->drawVertices(fRenderTarget,
1784 fClip,
1785 grPaint,
1786 *draw.fMatrix,
1787 primType,
1788 vertexCount,
1789 vertices,
1790 texs,
1791 colors,
bsalomonf1b7a1d2015-09-28 06:26:28 -07001792 indices,
robertphillipsea461502015-05-26 11:38:03 -07001793 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001794}
1795
1796///////////////////////////////////////////////////////////////////////////////
1797
jvanverth31ff7622015-08-07 10:09:28 -07001798void SkGpuDevice::drawAtlas(const SkDraw& draw, const SkImage* atlas, const SkRSXform xform[],
reedca109532015-06-25 16:25:25 -07001799 const SkRect texRect[], const SkColor colors[], int count,
1800 SkXfermode::Mode mode, const SkPaint& paint) {
1801 if (paint.isAntiAlias()) {
jvanverth31ff7622015-08-07 10:09:28 -07001802 this->INHERITED::drawAtlas(draw, atlas, xform, texRect, colors, count, mode, paint);
reedca109532015-06-25 16:25:25 -07001803 return;
1804 }
1805
jvanverth31ff7622015-08-07 10:09:28 -07001806 CHECK_SHOULD_DRAW(draw);
1807 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
1808
reedca109532015-06-25 16:25:25 -07001809 SkPaint p(paint);
1810 p.setShader(atlas->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
1811
jvanverth31ff7622015-08-07 10:09:28 -07001812 GrPaint grPaint;
robertphillips29ccdf82015-07-24 10:20:45 -07001813 if (colors) {
bsalomonf1b7a1d2015-09-28 06:26:28 -07001814 if (!SkPaintToGrPaintWithXfermode(this->context(), p, *draw.fMatrix, mode, true,
1815 &grPaint)) {
1816 return;
1817 }
1818 } else {
1819 if (!SkPaintToGrPaint(this->context(), p, *draw.fMatrix, &grPaint)) {
jvanverth31ff7622015-08-07 10:09:28 -07001820 return;
robertphillips29ccdf82015-07-24 10:20:45 -07001821 }
1822 }
bsalomonf1b7a1d2015-09-28 06:26:28 -07001823
1824 SkDEBUGCODE(this->validate();)
jvanverth31ff7622015-08-07 10:09:28 -07001825 fDrawContext->drawAtlas(fRenderTarget, fClip, grPaint, *draw.fMatrix,
1826 count, xform, texRect, colors);
reedca109532015-06-25 16:25:25 -07001827}
1828
1829///////////////////////////////////////////////////////////////////////////////
1830
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001831void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001832 size_t byteLength, SkScalar x, SkScalar y,
1833 const SkPaint& paint) {
1834 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001835 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001836
jvanverth8c27a182014-10-14 08:45:50 -07001837 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001838 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001839 return;
1840 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001841
jvanverth8c27a182014-10-14 08:45:50 -07001842 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001843
robertphillips2334fb62015-06-17 05:43:33 -07001844 fDrawContext->drawText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001845 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001846}
1847
fmalita05c4a432014-09-29 06:29:53 -07001848void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1849 const SkScalar pos[], int scalarsPerPos,
1850 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001851 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001852 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001853
jvanverth8c27a182014-10-14 08:45:50 -07001854 GrPaint grPaint;
bsalomonf1b7a1d2015-09-28 06:26:28 -07001855 if (!SkPaintToGrPaint(this->context(), paint, *draw.fMatrix, &grPaint)) {
bsalomonbed83a62015-04-15 14:18:34 -07001856 return;
1857 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001858
jvanverth8c27a182014-10-14 08:45:50 -07001859 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001860
robertphillips2334fb62015-06-17 05:43:33 -07001861 fDrawContext->drawPosText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001862 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1863 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001864}
1865
joshualitt9c328182015-03-23 08:13:04 -07001866void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1867 const SkPaint& paint, SkDrawFilter* drawFilter) {
1868 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawTextBlob", fContext);
1869 CHECK_SHOULD_DRAW(draw);
1870
1871 SkDEBUGCODE(this->validate();)
1872
robertphillips2334fb62015-06-17 05:43:33 -07001873 fDrawContext->drawTextBlob(fRenderTarget, fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001874 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001875}
1876
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001877///////////////////////////////////////////////////////////////////////////////
1878
reedb2db8982014-11-13 12:41:02 -08001879bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
robertphillips9c240a12015-05-28 07:45:59 -07001880 return GrTextContext::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001881}
1882
1883void SkGpuDevice::flush() {
1884 DO_DEFERRED_CLEAR();
bsalomonc49e8682015-06-30 11:37:35 -07001885 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001886}
1887
1888///////////////////////////////////////////////////////////////////////////////
1889
reed76033be2015-03-14 10:54:31 -07001890SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
bsalomonf2703d82014-10-28 14:33:06 -07001891 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001892 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001893 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001894 desc.fWidth = cinfo.fInfo.width();
1895 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001896 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001897
1898 SkAutoTUnref<GrTexture> texture;
1899 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001900 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001901
hcm4396fa52014-10-27 21:43:30 -07001902 // layers are never draw in repeat modes, so we can request an approx
1903 // match and ignore any padding.
bsalomoneae62002015-07-31 13:59:30 -07001904 if (kNever_TileUsage == cinfo.fTileUsage) {
1905 texture.reset(fContext->textureProvider()->createApproxTexture(desc));
1906 } else {
1907 texture.reset(fContext->textureProvider()->createTexture(desc, true));
1908 }
bsalomonafe30052015-01-16 07:32:33 -08001909
1910 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001911 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001912 return SkGpuDevice::Create(
bsalomon74f681d2015-06-23 14:38:48 -07001913 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, init);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001914 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001915 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001916 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001917 cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary96fcdcc2015-08-27 07:41:13 -07001918 return nullptr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001919 }
1920}
1921
reed4a8126e2014-09-22 07:29:03 -07001922SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001923 // TODO: Change the signature of newSurface to take a budgeted parameter.
1924 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
vbuzinovdded6962015-06-12 08:59:45 -07001925 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
bsalomonafe30052015-01-16 07:32:33 -08001926 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001927}
1928
robertphillips30d2cc62014-09-24 08:52:18 -07001929bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001930 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001931#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001932 // todo: should handle this natively
1933 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001934 return false;
1935 }
1936
halcanary96fcdcc2015-08-27 07:41:13 -07001937 const SkBigPicture::AccelData* data = nullptr;
mtklein9db912c2015-05-19 11:11:26 -07001938 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1939 data = bp->accelData();
1940 }
robertphillips81f71b62014-11-11 04:54:49 -08001941 if (!data) {
1942 return false;
1943 }
1944
robertphillipse5524cd2015-02-20 12:30:26 -08001945 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1946 if (0 == gpuData->numBlocks()) {
1947 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001948 }
1949
robertphillipsfd61ed02014-10-28 07:21:44 -07001950 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001951
robertphillipse5524cd2015-02-20 12:30:26 -08001952 SkIRect iBounds;
1953 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1954 return false;
1955 }
1956
1957 SkRect clipBounds = SkRect::Make(iBounds);
1958
1959 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1960
robertphillipsfd61ed02014-10-28 07:21:44 -07001961 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001962 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001963 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001964 &atlasedNeedRendering, &atlasedRecycled,
vbuzinovdded6962015-06-12 08:59:45 -07001965 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001966
1967 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1968
1969 SkTDArray<GrHoistedLayer> needRendering, recycled;
1970
robertphillipse5524cd2015-02-20 12:30:26 -08001971 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1972
robertphillipsfd61ed02014-10-28 07:21:44 -07001973 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001974 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001975 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001976 &needRendering, &recycled,
vbuzinovdded6962015-06-12 08:59:45 -07001977 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001978
1979 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001980
robertphillips64bf7672014-08-21 13:07:35 -07001981 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001982 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
halcanary96fcdcc2015-08-27 07:41:13 -07001983 initialMatrix, nullptr);
robertphillips64bf7672014-08-21 13:07:35 -07001984
robertphillipsfd61ed02014-10-28 07:21:44 -07001985 GrLayerHoister::UnlockLayers(fContext, needRendering);
1986 GrLayerHoister::UnlockLayers(fContext, recycled);
1987 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1988 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001989
1990 return true;
robertphillips63242d72014-12-04 08:31:02 -08001991#else
1992 return false;
1993#endif
robertphillips64bf7672014-08-21 13:07:35 -07001994}
1995
senorblancobe129b22014-08-08 07:14:35 -07001996SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001997 // We always return a transient cache, so it is freed after each
1998 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001999 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07002000}
reedf037e0b2014-10-30 11:34:15 -07002001
2002#endif