blob: 1603cd64f1a9928e142d1ce5a2b8eb8480ab5b66 [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"
reeda85d4d02015-05-06 12:56:48 -070026#include "SkImage_Base.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000027#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080028#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000029#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000030#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000031#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070032#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080034#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000036#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080037#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000038#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000039#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000040#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070041#include "SkXfermode.h"
kkinnunenabcfab42015-02-22 22:53:44 -080042#include "effects/GrBicubicEffect.h"
43#include "effects/GrDashingEffect.h"
44#include "effects/GrSimpleTextureEffect.h"
45#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000046
reedf037e0b2014-10-30 11:34:15 -070047#if SK_SUPPORT_GPU
48
senorblanco55b6d8b2014-07-30 11:26:46 -070049enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
50
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000051#if 0
52 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080053 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000054 do { \
55 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080056 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000057 } while (0)
58#else
joshualitt5531d512014-12-17 15:50:11 -080059 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000060#endif
61
62// This constant represents the screen alignment criterion in texels for
63// requiring texture domain clamping to prevent color bleeding when drawing
64// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000065#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000066
67#define DO_DEFERRED_CLEAR() \
68 do { \
bsalomonafe30052015-01-16 07:32:33 -080069 if (fNeedClear) { \
70 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000071 } \
72 } while (false) \
73
74///////////////////////////////////////////////////////////////////////////////
75
76#define CHECK_FOR_ANNOTATION(paint) \
77 do { if (paint.getAnnotation()) { return; } } while (0)
78
79///////////////////////////////////////////////////////////////////////////////
80
bsalomonbcf0a522014-10-08 08:40:09 -070081// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
82// just accesses the backing GrTexture. Otherwise, it creates a cached texture
83// representation and releases it in the destructor.
84class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040085public:
bsalomonbcf0a522014-10-08 08:40:09 -070086 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070087
bsalomonbcf0a522014-10-08 08:40:09 -070088 AutoBitmapTexture(GrContext* context,
89 const SkBitmap& bitmap,
90 const GrTextureParams* params,
91 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040092 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070093 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040094 }
95
bsalomonbcf0a522014-10-08 08:40:09 -070096 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040097 const SkBitmap& bitmap,
98 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -070099 // Either get the texture directly from the bitmap, or else use the cache and
100 // remember to unref it.
101 if (GrTexture* bmpTexture = bitmap.getTexture()) {
102 fTexture.reset(NULL);
103 return bmpTexture;
104 } else {
105 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
106 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400107 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400108 }
109
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000110private:
bsalomonbcf0a522014-10-08 08:40:09 -0700111 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000112};
113
114///////////////////////////////////////////////////////////////////////////////
115
116struct GrSkDrawProcs : public SkDrawProcs {
117public:
118 GrContext* fContext;
119 GrTextContext* fTextContext;
120 GrFontScaler* fFontScaler; // cached in the skia glyphcache
121};
122
123///////////////////////////////////////////////////////////////////////////////
124
bsalomonafe30052015-01-16 07:32:33 -0800125SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags) {
senorblancod0d37ca2015-04-02 04:54:56 -0700126 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, flags);
127}
128
129SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
130 const SkSurfaceProps* props, unsigned flags) {
bsalomonafe30052015-01-16 07:32:33 -0800131 if (!rt || rt->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000132 return NULL;
133 }
senorblancod0d37ca2015-04-02 04:54:56 -0700134 return SkNEW_ARGS(SkGpuDevice, (rt, width, height, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000135}
136
senorblancod0d37ca2015-04-02 04:54:56 -0700137SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
138 const SkSurfaceProps* props, unsigned flags)
robertphillipsfcf78292015-06-19 11:49:52 -0700139 : INHERITED(SkSurfacePropsCopyOrDefault(props))
reedb2db8982014-11-13 12:41:02 -0800140{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000141 fDrawProcs = NULL;
142
bsalomonafe30052015-01-16 07:32:33 -0800143 fContext = SkRef(rt->getContext());
144 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000145
bsalomonafe30052015-01-16 07:32:33 -0800146 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000147
senorblancod0d37ca2015-04-02 04:54:56 -0700148 SkImageInfo info = rt->surfacePriv().info().makeWH(width, height);
bsalomonafe30052015-01-16 07:32:33 -0800149 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt));
bsalomonafbf2d62014-09-30 12:18:44 -0700150 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700151 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700152
robertphillips7b05ff12015-06-19 14:14:54 -0700153 fDrawContext.reset(SkRef(fContext->drawContext(&this->surfaceProps())));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000154}
155
kkinnunenabcfab42015-02-22 22:53:44 -0800156GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
157 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000158 if (kUnknown_SkColorType == origInfo.colorType() ||
159 origInfo.width() < 0 || origInfo.height() < 0) {
160 return NULL;
161 }
162
bsalomonafe30052015-01-16 07:32:33 -0800163 if (!context) {
164 return NULL;
165 }
166
reede5ea5002014-09-03 11:54:58 -0700167 SkColorType ct = origInfo.colorType();
168 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700169 if (kRGB_565_SkColorType == ct) {
170 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800171 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
172 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700173 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800174 }
175 if (kOpaque_SkAlphaType != at) {
176 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000177 }
reede5ea5002014-09-03 11:54:58 -0700178 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000179
bsalomonf2703d82014-10-28 14:33:06 -0700180 GrSurfaceDesc desc;
181 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000182 desc.fWidth = info.width();
183 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000184 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000185 desc.fSampleCnt = sampleCount;
bsalomond309e7a2015-04-30 14:18:54 -0700186 GrTexture* texture = context->textureProvider()->createTexture(
187 desc, SkToBool(budgeted), NULL, 0);
kkinnunenabcfab42015-02-22 22:53:44 -0800188 if (NULL == texture) {
189 return NULL;
190 }
191 SkASSERT(NULL != texture->asRenderTarget());
192 return texture->asRenderTarget();
193}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000194
kkinnunenabcfab42015-02-22 22:53:44 -0800195SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
196 const SkImageInfo& info, int sampleCount,
197 const SkSurfaceProps* props, unsigned flags) {
198
199 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
200 if (NULL == rt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000201 return NULL;
202 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000203
senorblancod0d37ca2015-04-02 04:54:56 -0700204 return SkNEW_ARGS(SkGpuDevice, (rt, info.width(), info.height(), props, flags));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000205}
206
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000207SkGpuDevice::~SkGpuDevice() {
208 if (fDrawProcs) {
209 delete fDrawProcs;
210 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000211
bsalomon32d0b3b2014-08-29 07:50:23 -0700212 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000213 fContext->unref();
214}
215
216///////////////////////////////////////////////////////////////////////////////
217
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000218bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
219 int x, int y) {
220 DO_DEFERRED_CLEAR();
221
222 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000223 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000224 if (kUnknown_GrPixelConfig == config) {
225 return false;
226 }
227
228 uint32_t flags = 0;
229 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
230 flags = GrContext::kUnpremul_PixelOpsFlag;
231 }
232 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
233 config, dstPixels, dstRowBytes, flags);
234}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000235
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000236bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
237 int x, int y) {
238 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000239 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000240 if (kUnknown_GrPixelConfig == config) {
241 return false;
242 }
243 uint32_t flags = 0;
244 if (kUnpremul_SkAlphaType == info.alphaType()) {
245 flags = GrContext::kUnpremul_PixelOpsFlag;
246 }
247 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
248
249 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700250 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000251
252 return true;
253}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000254
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000255const SkBitmap& SkGpuDevice::onAccessBitmap() {
256 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700257 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000258}
259
reed41e010c2015-06-09 12:16:53 -0700260bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
261 DO_DEFERRED_CLEAR();
262 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
263 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
264 // ... ugh.
265 fLegacyBitmap.notifyPixelsChanged();
266 return false;
267}
268
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000269void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
270 INHERITED::onAttachToCanvas(canvas);
271
272 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800273 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000274}
275
276void SkGpuDevice::onDetachFromCanvas() {
277 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800278 fClip.reset();
joshualitt44701df2015-02-23 14:44:57 -0800279 fClipStack.reset(NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000280}
281
282// call this every draw call, to ensure that the context reflects our state,
283// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800284void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800285 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000286
joshualitt44701df2015-02-23 14:44:57 -0800287 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000288
joshualitt570d2f82015-02-25 13:19:48 -0800289 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000290
291 DO_DEFERRED_CLEAR();
292}
293
294GrRenderTarget* SkGpuDevice::accessRenderTarget() {
robertphillips7156b092015-05-14 08:54:12 -0700295 DO_DEFERRED_CLEAR();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000296 return fRenderTarget;
297}
298
reed8eddfb52014-12-04 07:50:14 -0800299void SkGpuDevice::clearAll() {
300 GrColor color = 0;
301 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
302 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillipsea461502015-05-26 11:38:03 -0700303 fDrawContext->clear(fRenderTarget, &rect, color, true);
bsalomonafe30052015-01-16 07:32:33 -0800304 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800305}
306
kkinnunenabcfab42015-02-22 22:53:44 -0800307void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
308 // Caller must have accessed the render target, because it knows the rt must be replaced.
309 SkASSERT(!fNeedClear);
310
311 SkSurface::Budgeted budgeted =
312 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
313 : SkSurface::kNo_Budgeted;
314
315 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
vbuzinovdded6962015-06-12 08:59:45 -0700316 fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt));
kkinnunenabcfab42015-02-22 22:53:44 -0800317
318 if (NULL == newRT) {
319 return;
320 }
321
322 if (shouldRetainContent) {
323 if (fRenderTarget->wasDestroyed()) {
324 return;
325 }
326 this->context()->copySurface(newRT, fRenderTarget);
327 }
328
329 SkASSERT(fRenderTarget != newRT);
330
331 fRenderTarget->unref();
332 fRenderTarget = newRT.detach();
333
334 SkASSERT(fRenderTarget->surfacePriv().info() == fLegacyBitmap.info());
335 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (fRenderTarget->surfacePriv().info(), fRenderTarget));
336 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700337
robertphillips7b05ff12015-06-19 14:14:54 -0700338 fDrawContext.reset(SkRef(fRenderTarget->getContext()->drawContext(&this->surfaceProps())));
kkinnunenabcfab42015-02-22 22:53:44 -0800339}
340
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000341///////////////////////////////////////////////////////////////////////////////
342
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000343void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800344 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700345 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000346
347 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700348 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
349 return;
350 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000351
robertphillipsea461502015-05-26 11:38:03 -0700352 fDrawContext->drawPaint(fRenderTarget, fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000353}
354
355// must be in SkCanvas::PointMode order
356static const GrPrimitiveType gPointMode2PrimtiveType[] = {
357 kPoints_GrPrimitiveType,
358 kLines_GrPrimitiveType,
359 kLineStrip_GrPrimitiveType
360};
361
362void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
363 size_t count, const SkPoint pts[], const SkPaint& paint) {
364 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800365 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000366
367 SkScalar width = paint.getStrokeWidth();
368 if (width < 0) {
369 return;
370 }
371
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000372 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700373 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
374 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700375 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
376 &grPaint)) {
377 return;
378 }
egdaniele61c4112014-06-12 10:24:21 -0700379 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700380 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700381 path.moveTo(pts[0]);
382 path.lineTo(pts[1]);
robertphillipsea461502015-05-26 11:38:03 -0700383 fDrawContext->drawPath(fRenderTarget, fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700384 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000385 }
386
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000387 // we only handle hairlines and paints without path effects or mask filters,
388 // else we let the SkDraw call our drawPath()
389 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
390 draw.drawPoints(mode, count, pts, paint, true);
391 return;
392 }
393
394 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700395 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
396 return;
397 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000398
robertphillipsea461502015-05-26 11:38:03 -0700399 fDrawContext->drawVertices(fRenderTarget,
400 fClip,
401 grPaint,
402 *draw.fMatrix,
403 gPointMode2PrimtiveType[mode],
404 SkToS32(count),
405 (SkPoint*)pts,
406 NULL,
407 NULL,
408 NULL,
409 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000410}
411
412///////////////////////////////////////////////////////////////////////////////
413
414void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
415 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700416 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
417
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000418 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800419 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000420
421 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
422 SkScalar width = paint.getStrokeWidth();
423
424 /*
425 We have special code for hairline strokes, miter-strokes, bevel-stroke
426 and fills. Anything else we just call our path code.
427 */
428 bool usePath = doStroke && width > 0 &&
429 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
430 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
431 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700432
433 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000434 usePath = true;
435 }
egdanield58a0ba2014-06-11 10:30:05 -0700436
joshualitt5531d512014-12-17 15:50:11 -0800437 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000438#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
439 if (doStroke) {
440#endif
441 usePath = true;
442#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
443 } else {
joshualitt5531d512014-12-17 15:50:11 -0800444 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000445 }
446#endif
447 }
448 // until we can both stroke and fill rectangles
449 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
450 usePath = true;
451 }
452
egdanield58a0ba2014-06-11 10:30:05 -0700453 GrStrokeInfo strokeInfo(paint);
454
455 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700456 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700457 usePath = true;
458 }
459
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000460 if (usePath) {
461 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700462 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000463 path.addRect(rect);
464 this->drawPath(draw, path, paint, NULL, true);
465 return;
466 }
467
468 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700469 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
470 return;
471 }
Mike Klein744fb732014-06-23 15:13:26 -0400472
robertphillipsea461502015-05-26 11:38:03 -0700473 fDrawContext->drawRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000474}
475
476///////////////////////////////////////////////////////////////////////////////
477
478void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800479 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700480 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000481 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800482 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000483
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000484 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700485 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
486 return;
487 }
Mike Klein744fb732014-06-23 15:13:26 -0400488
egdanield58a0ba2014-06-11 10:30:05 -0700489 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000490 if (paint.getMaskFilter()) {
491 // try to hit the fast path for drawing filtered round rects
492
493 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800494 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000495 if (devRRect.allCornersCircular()) {
496 SkRect maskRect;
497 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800498 draw.fClip->getBounds(),
499 *draw.fMatrix,
500 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000501 SkIRect finalIRect;
502 maskRect.roundOut(&finalIRect);
503 if (draw.fClip->quickReject(finalIRect)) {
504 // clipped out
505 return;
506 }
joshualitt25d9c152015-02-18 12:29:52 -0800507 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
508 fRenderTarget,
509 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800510 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800511 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700512 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700513 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000514 return;
515 }
516 }
517
518 }
519 }
520
521 }
522
egdanield58a0ba2014-06-11 10:30:05 -0700523 bool usePath = false;
524
525 if (paint.getMaskFilter()) {
526 usePath = true;
527 } else {
528 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700529 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700530 usePath = true;
531 }
532 }
533
534
535 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000536 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700537 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000538 path.addRRect(rect);
539 this->drawPath(draw, path, paint, NULL, true);
540 return;
541 }
Mike Klein744fb732014-06-23 15:13:26 -0400542
robertphillipsea461502015-05-26 11:38:03 -0700543 fDrawContext->drawRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000544}
545
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000546void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800547 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000548 SkStrokeRec stroke(paint);
549 if (stroke.isFillStyle()) {
550
551 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800552 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000553
554 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700555 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
556 &grPaint)) {
557 return;
558 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000559
560 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
robertphillipsea461502015-05-26 11:38:03 -0700561 fDrawContext->drawDRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000562 return;
563 }
564 }
565
566 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700567 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000568 path.addRRect(outer);
569 path.addRRect(inner);
570 path.setFillType(SkPath::kEvenOdd_FillType);
571
572 this->drawPath(draw, path, paint, NULL, true);
573}
574
575
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000576/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000577
578void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
579 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700580 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000581 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800582 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000583
egdanield58a0ba2014-06-11 10:30:05 -0700584 GrStrokeInfo strokeInfo(paint);
585
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000586 bool usePath = false;
587 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700588 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000589 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700590 } else {
591 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700592 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700593 usePath = true;
594 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000595 }
596
597 if (usePath) {
598 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700599 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000600 path.addOval(oval);
601 this->drawPath(draw, path, paint, NULL, true);
602 return;
603 }
604
605 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700606 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
607 return;
608 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000609
robertphillipsea461502015-05-26 11:38:03 -0700610 fDrawContext->drawOval(fRenderTarget, fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000611}
612
613#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000614
615///////////////////////////////////////////////////////////////////////////////
616
robertphillipsccb1b572015-05-27 11:02:55 -0700617static SkBitmap wrap_texture(GrTexture* texture, int width, int height) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000618 SkBitmap result;
senorblancod0d37ca2015-04-02 04:54:56 -0700619 result.setInfo(SkImageInfo::MakeN32Premul(width, height));
reed6c225732014-06-09 19:52:07 -0700620 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000621 return result;
622}
623
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000624void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
625 const SkPaint& paint, const SkMatrix* prePathMatrix,
626 bool pathIsMutable) {
627 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800628 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700629 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000630
robertphillipsccb1b572015-05-27 11:02:55 -0700631 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, fRenderTarget,
632 fClip, origSrcPath, paint,
633 *draw.fMatrix, prePathMatrix,
634 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000635}
636
637static const int kBmpSmallTileSize = 1 << 10;
638
639static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
640 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
641 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
642 return tilesX * tilesY;
643}
644
645static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
646 if (maxTileSize <= kBmpSmallTileSize) {
647 return maxTileSize;
648 }
649
650 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
651 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
652
653 maxTileTotalTileSize *= maxTileSize * maxTileSize;
654 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
655
656 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
657 return kBmpSmallTileSize;
658 } else {
659 return maxTileSize;
660 }
661}
662
663// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
664// pixels from the bitmap are necessary.
665static void determine_clipped_src_rect(const GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800666 const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800667 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800668 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000669 const SkBitmap& bitmap,
670 const SkRect* srcRectPtr,
671 SkIRect* clippedSrcIRect) {
joshualitt570d2f82015-02-25 13:19:48 -0800672 clip.getConservativeBounds(rt, clippedSrcIRect, NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000673 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800674 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000675 clippedSrcIRect->setEmpty();
676 return;
677 }
678 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
679 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700680 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000681 // we've setup src space 0,0 to map to the top left of the src rect.
682 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000683 if (!clippedSrcRect.intersect(*srcRectPtr)) {
684 clippedSrcIRect->setEmpty();
685 return;
686 }
687 }
688 clippedSrcRect.roundOut(clippedSrcIRect);
689 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
690 if (!clippedSrcIRect->intersect(bmpBounds)) {
691 clippedSrcIRect->setEmpty();
692 }
693}
694
695bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800696 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000697 const GrTextureParams& params,
698 const SkRect* srcRectPtr,
699 int maxTileSize,
700 int* tileSize,
701 SkIRect* clippedSrcRect) const {
702 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700703 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000704 return false;
705 }
706
707 // if it's larger than the max tile size, then we have no choice but tiling.
708 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt570d2f82015-02-25 13:19:48 -0800709 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap,
710 srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000711 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
712 return true;
713 }
714
715 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
716 return false;
717 }
718
719 // if the entire texture is already in our cache then no reason to tile it
720 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
721 return false;
722 }
723
724 // At this point we know we could do the draw by uploading the entire bitmap
725 // as a texture. However, if the texture would be large compared to the
726 // cache size and we don't require most of it for this draw then tile to
727 // reduce the amount of upload and cache spill.
728
729 // assumption here is that sw bitmap size is a good proxy for its size as
730 // a texture
731 size_t bmpSize = bitmap.getSize();
732 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000733 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000734 if (bmpSize < cacheSize / 2) {
735 return false;
736 }
737
738 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt570d2f82015-02-25 13:19:48 -0800739 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap, srcRectPtr,
joshualitt25d9c152015-02-18 12:29:52 -0800740 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000741 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
742 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
743 kBmpSmallTileSize * kBmpSmallTileSize;
744
745 return usedTileBytes < 2 * bmpSize;
746}
747
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000748void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000749 const SkBitmap& bitmap,
750 const SkMatrix& m,
751 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000752 SkMatrix concat;
753 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
754 if (!m.isIdentity()) {
755 concat.setConcat(*draw->fMatrix, m);
756 draw.writable()->fMatrix = &concat;
757 }
758 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000759}
760
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000761// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000762// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
763// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000764static inline void clamped_outset_with_offset(SkIRect* iRect,
765 int outset,
766 SkPoint* offset,
767 const SkIRect& clamp) {
768 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000769
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000770 int leftClampDelta = clamp.fLeft - iRect->fLeft;
771 if (leftClampDelta > 0) {
772 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773 iRect->fLeft = clamp.fLeft;
774 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000775 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000776 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000777
778 int topClampDelta = clamp.fTop - iRect->fTop;
779 if (topClampDelta > 0) {
780 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000781 iRect->fTop = clamp.fTop;
782 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000783 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000784 }
785
786 if (iRect->fRight > clamp.fRight) {
787 iRect->fRight = clamp.fRight;
788 }
789 if (iRect->fBottom > clamp.fBottom) {
790 iRect->fBottom = clamp.fBottom;
791 }
792}
793
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000794static bool has_aligned_samples(const SkRect& srcRect,
795 const SkRect& transformedRect) {
796 // detect pixel disalignment
797 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
798 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
799 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
800 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
801 SkScalarAbs(transformedRect.width() - srcRect.width()) <
802 COLOR_BLEED_TOLERANCE &&
803 SkScalarAbs(transformedRect.height() - srcRect.height()) <
804 COLOR_BLEED_TOLERANCE) {
805 return true;
806 }
807 return false;
808}
809
810static bool may_color_bleed(const SkRect& srcRect,
811 const SkRect& transformedRect,
812 const SkMatrix& m) {
813 // Only gets called if has_aligned_samples returned false.
814 // So we can assume that sampling is axis aligned but not texel aligned.
815 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
816 SkRect innerSrcRect(srcRect), innerTransformedRect,
817 outerTransformedRect(transformedRect);
818 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
819 m.mapRect(&innerTransformedRect, innerSrcRect);
820
821 // The gap between outerTransformedRect and innerTransformedRect
822 // represents the projection of the source border area, which is
823 // problematic for color bleeding. We must check whether any
824 // destination pixels sample the border area.
825 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
826 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
827 SkIRect outer, inner;
828 outerTransformedRect.round(&outer);
829 innerTransformedRect.round(&inner);
830 // If the inner and outer rects round to the same result, it means the
831 // border does not overlap any pixel centers. Yay!
832 return inner != outer;
833}
834
835static bool needs_texture_domain(const SkBitmap& bitmap,
836 const SkRect& srcRect,
837 GrTextureParams &params,
838 const SkMatrix& contextMatrix,
839 bool bicubic) {
840 bool needsTextureDomain = false;
senorblancod0d37ca2015-04-02 04:54:56 -0700841 GrTexture* tex = bitmap.getTexture();
842 int width = tex ? tex->width() : bitmap.width();
843 int height = tex ? tex->height() : bitmap.height();
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000844
845 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
846 // Need texture domain if drawing a sub rect
senorblancod0d37ca2015-04-02 04:54:56 -0700847 needsTextureDomain = srcRect.width() < width ||
848 srcRect.height() < height;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000849 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
850 // sampling is axis-aligned
851 SkRect transformedRect;
852 contextMatrix.mapRect(&transformedRect, srcRect);
853
854 if (has_aligned_samples(srcRect, transformedRect)) {
855 params.setFilterMode(GrTextureParams::kNone_FilterMode);
856 needsTextureDomain = false;
857 } else {
858 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
859 }
860 }
861 }
862 return needsTextureDomain;
863}
864
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000865void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
866 const SkBitmap& bitmap,
867 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000868 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000869 const SkPaint& paint,
870 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -0800871 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000872
873 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000874 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000875 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
876 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000877 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000878 SkScalar w = SkIntToScalar(bitmap.width());
879 SkScalar h = SkIntToScalar(bitmap.height());
880 dstSize.fWidth = w;
881 dstSize.fHeight = h;
882 srcRect.set(0, 0, w, h);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000883 } else {
bsalomon49f085d2014-09-05 13:34:00 -0700884 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000885 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000886 dstSize = *dstSizePtr;
senorblancod0d37ca2015-04-02 04:54:56 -0700887 }
888 GrTexture* tex = bitmap.getTexture();
889 int width = tex ? tex->width() : bitmap.width();
890 int height = tex ? tex->height() : bitmap.height();
891 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
892 srcRect.fRight >= width && srcRect.fBottom >= height) {
893 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000894 }
895
derekf367e1862014-12-02 11:02:06 -0800896 // If the render target is not msaa and draw is antialiased, we call
897 // drawRect instead of drawing on the render target directly.
898 // FIXME: the tiled bitmap code path doesn't currently support
899 // anti-aliased edges, we work around that for now by drawing directly
900 // if the image size exceeds maximum texture size.
bsalomon76228632015-05-29 08:02:10 -0700901 int maxTextureSize = fContext->caps()->maxTextureSize();
vbuzinovdded6962015-06-12 08:59:45 -0700902 bool directDraw = fRenderTarget->isUnifiedMultisampled() ||
derekf367e1862014-12-02 11:02:06 -0800903 !paint.isAntiAlias() ||
904 bitmap.width() > maxTextureSize ||
905 bitmap.height() > maxTextureSize;
906
907 // we check whether dst rect are pixel aligned
908 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -0800909 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -0800910
911 if (staysRect) {
912 SkRect rect;
913 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -0800914 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -0800915 const SkScalar *scalars = rect.asScalars();
916 bool isDstPixelAligned = true;
917 for (int i = 0; i < 4; i++) {
918 if (!SkScalarIsInt(scalars[i])) {
919 isDstPixelAligned = false;
920 break;
921 }
922 }
923
924 if (isDstPixelAligned)
925 directDraw = true;
926 }
927 }
928
929 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000930 // Convert the bitmap to a shader so that the rect can be drawn
931 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000932 SkBitmap tmp; // subset of bitmap, if necessary
933 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000934 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -0700935 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000936 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
937 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
938 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000939 // In bleed mode we position and trim the bitmap based on the src rect which is
940 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
941 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
942 // compensate.
943 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
944 SkIRect iSrc;
945 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000946
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000947 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
948 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000949
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000950 if (!bitmap.extractSubset(&tmp, iSrc)) {
951 return; // extraction failed
952 }
953 bitmapPtr = &tmp;
954 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000955
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000956 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000957 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000959 } else {
960 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000961 }
962
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000963 SkPaint paintWithShader(paint);
964 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000965 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000966 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
967 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000968
969 return;
970 }
971
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000972 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
973 // the view matrix rather than a local matrix.
974 SkMatrix m;
975 m.setScale(dstSize.fWidth / srcRect.width(),
976 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -0800977 SkMatrix viewM = *draw.fMatrix;
978 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000979
980 GrTextureParams params;
reed93a12152015-03-16 10:08:34 -0700981 SkFilterQuality paintFilterQuality = paint.getFilterQuality();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000983
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000984 bool doBicubic = false;
985
reed93a12152015-03-16 10:08:34 -0700986 switch(paintFilterQuality) {
987 case kNone_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000988 textureFilterMode = GrTextureParams::kNone_FilterMode;
989 break;
reed93a12152015-03-16 10:08:34 -0700990 case kLow_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000991 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
992 break;
reed93a12152015-03-16 10:08:34 -0700993 case kMedium_SkFilterQuality:
joshualitt5531d512014-12-17 15:50:11 -0800994 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +0000995 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
996 } else {
997 // Don't trigger MIP level generation unnecessarily.
998 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
999 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001000 break;
reed93a12152015-03-16 10:08:34 -07001001 case kHigh_SkFilterQuality:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001002 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001003 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001004 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001005 break;
1006 default:
1007 SkErrorInternals::SetError( kInvalidPaint_SkError,
1008 "Sorry, I don't understand the filtering "
1009 "mode you asked for. Falling back to "
1010 "MIPMaps.");
1011 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1012 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001013 }
1014
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001015 int tileFilterPad;
1016 if (doBicubic) {
1017 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1018 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1019 tileFilterPad = 0;
1020 } else {
1021 tileFilterPad = 1;
1022 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001023 params.setFilterMode(textureFilterMode);
1024
bsalomon76228632015-05-29 08:02:10 -07001025 int maxTileSize = fContext->caps()->maxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001026 int tileSize;
1027
1028 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001029 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001030 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001031 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1032 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001033 } else {
1034 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001035 bool needsTextureDomain = needs_texture_domain(bitmap,
1036 srcRect,
1037 params,
joshualitt5531d512014-12-17 15:50:11 -08001038 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001039 doBicubic);
1040 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001041 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001042 srcRect,
1043 params,
1044 paint,
1045 flags,
1046 doBicubic,
1047 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001048 }
1049}
1050
1051// Break 'bitmap' into several tiles to draw it since it has already
1052// been determined to be too large to fit in VRAM
1053void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001054 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001055 const SkRect& srcRect,
1056 const SkIRect& clippedSrcIRect,
1057 const GrTextureParams& params,
1058 const SkPaint& paint,
1059 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001060 int tileSize,
1061 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001062 // The following pixel lock is technically redundant, but it is desirable
1063 // to lock outside of the tile loop to prevent redecoding the whole image
1064 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1065 // is larger than the limit of the discardable memory pool.
1066 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001067 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1068
1069 int nx = bitmap.width() / tileSize;
1070 int ny = bitmap.height() / tileSize;
1071 for (int x = 0; x <= nx; x++) {
1072 for (int y = 0; y <= ny; y++) {
1073 SkRect tileR;
1074 tileR.set(SkIntToScalar(x * tileSize),
1075 SkIntToScalar(y * tileSize),
1076 SkIntToScalar((x + 1) * tileSize),
1077 SkIntToScalar((y + 1) * tileSize));
1078
1079 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1080 continue;
1081 }
1082
1083 if (!tileR.intersect(srcRect)) {
1084 continue;
1085 }
1086
1087 SkBitmap tmpB;
1088 SkIRect iTileR;
1089 tileR.roundOut(&iTileR);
1090 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1091 SkIntToScalar(iTileR.fTop));
1092
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001093 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001094 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001095 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001096 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001097 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001098
robertphillipsec8bb942014-11-21 10:16:25 -08001099 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001100 SkIRect iClampRect;
1101
1102 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1103 // In bleed mode we want to always expand the tile on all edges
1104 // but stay within the bitmap bounds
1105 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1106 } else {
1107 // In texture-domain/clamp mode we only want to expand the
1108 // tile on edges interior to "srcRect" (i.e., we want to
1109 // not bleed across the original clamped edges)
1110 srcRect.roundOut(&iClampRect);
1111 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001112 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1113 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001114 }
1115
1116 if (bitmap.extractSubset(&tmpB, iTileR)) {
1117 // now offset it to make it "local" to our tmp bitmap
1118 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001119 GrTextureParams paramsTemp = params;
1120 bool needsTextureDomain = needs_texture_domain(bitmap,
1121 srcRect,
1122 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001123 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001124 bicubic);
1125 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001126 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001127 tileR,
1128 paramsTemp,
1129 paint,
1130 flags,
1131 bicubic,
1132 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001133 }
1134 }
1135 }
1136}
1137
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001138
1139/*
1140 * This is called by drawBitmap(), which has to handle images that may be too
1141 * large to be represented by a single texture.
1142 *
1143 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1144 * and that non-texture portion of the GrPaint has already been setup.
1145 */
1146void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001147 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001148 const SkRect& srcRect,
1149 const GrTextureParams& params,
1150 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001151 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001152 bool bicubic,
1153 bool needsTextureDomain) {
bsalomon76228632015-05-29 08:02:10 -07001154 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1155 bitmap.height() <= fContext->caps()->maxTextureSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001156
1157 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001158 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001159 if (NULL == texture) {
1160 return;
1161 }
1162
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001163 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001164 SkRect paintRect;
1165 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1166 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1167 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1168 SkScalarMul(srcRect.fTop, hInv),
1169 SkScalarMul(srcRect.fRight, wInv),
1170 SkScalarMul(srcRect.fBottom, hInv));
1171
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001172 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001173 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001174 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001175 // Use a constrained texture domain to avoid color bleeding
1176 SkScalar left, top, right, bottom;
1177 if (srcRect.width() > SK_Scalar1) {
1178 SkScalar border = SK_ScalarHalf / texture->width();
1179 left = paintRect.left() + border;
1180 right = paintRect.right() - border;
1181 } else {
1182 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1183 }
1184 if (srcRect.height() > SK_Scalar1) {
1185 SkScalar border = SK_ScalarHalf / texture->height();
1186 top = paintRect.top() + border;
1187 bottom = paintRect.bottom() - border;
1188 } else {
1189 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1190 }
1191 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001192 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001193 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001194 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001195 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001196 SkMatrix::I(),
1197 textureDomain,
1198 GrTextureDomain::kClamp_Mode,
1199 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001200 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001201 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001202 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1203 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001204 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001205 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001206 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001207 }
1208
1209 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1210 // the rest from the SkPaint.
1211 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001212 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001213 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001214 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1215 SkColor2GrColor(paint.getColor());
bsalomonbed83a62015-04-15 14:18:34 -07001216 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false,
1217 &grPaint)) {
1218 return;
1219 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001220
robertphillipsea461502015-05-26 11:38:03 -07001221 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dstRect,
1222 paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001223}
1224
fmalita2d97bc12014-11-20 10:44:58 -08001225bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001226 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001227 const SkImageFilter* filter,
1228 const SkImageFilter::Context& ctx,
1229 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001230 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001231
robertphillipsefbffed2015-06-22 12:06:08 -07001232 SkImageFilter::Proxy proxy(this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001233
1234 if (filter->canFilterImageGPU()) {
senorblancod0d37ca2015-04-02 04:54:56 -07001235 return filter->filterImageGPU(&proxy, wrap_texture(texture, width, height),
1236 ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001237 } else {
1238 return false;
1239 }
1240}
1241
1242void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1243 int left, int top, const SkPaint& paint) {
1244 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001245 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001246
1247 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1248 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1249 return;
1250 }
1251
1252 int w = bitmap.width();
1253 int h = bitmap.height();
1254
1255 GrTexture* texture;
1256 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001257 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001258 if (!texture) {
1259 return;
1260 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001261
1262 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001263 // This bitmap will own the filtered result as a texture.
1264 SkBitmap filteredBitmap;
1265
bsalomon49f085d2014-09-05 13:34:00 -07001266 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001267 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001268 SkMatrix matrix(*draw.fMatrix);
1269 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001270 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001271 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001272 // This cache is transient, and is freed (along with all its contained
1273 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001274 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001275 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001276 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001277 texture = (GrTexture*) filteredBitmap.getTexture();
1278 w = filteredBitmap.width();
1279 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001280 left += offset.x();
1281 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001282 } else {
1283 return;
1284 }
1285 }
1286
1287 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001288 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001289
bsalomonbed83a62015-04-15 14:18:34 -07001290 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1291 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1292 return;
1293 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001294
robertphillipsea461502015-05-26 11:38:03 -07001295 fDrawContext->drawNonAARectToRect(fRenderTarget,
1296 fClip,
1297 grPaint,
1298 SkMatrix::I(),
1299 SkRect::MakeXYWH(SkIntToScalar(left),
1300 SkIntToScalar(top),
1301 SkIntToScalar(w),
1302 SkIntToScalar(h)),
1303 SkRect::MakeXYWH(0,
1304 0,
1305 SK_Scalar1 * w / texture->width(),
1306 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001307}
1308
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001309void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001310 const SkRect* src, const SkRect& dst,
1311 const SkPaint& paint,
1312 SkCanvas::DrawBitmapRectFlags flags) {
1313 SkMatrix matrix;
1314 SkRect bitmapBounds, tmpSrc;
1315
1316 bitmapBounds.set(0, 0,
1317 SkIntToScalar(bitmap.width()),
1318 SkIntToScalar(bitmap.height()));
1319
1320 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001321 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001322 tmpSrc = *src;
1323 } else {
1324 tmpSrc = bitmapBounds;
1325 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001326
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001327 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1328
1329 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001330 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001331 if (!bitmapBounds.contains(tmpSrc)) {
1332 if (!tmpSrc.intersect(bitmapBounds)) {
1333 return; // nothing to draw
1334 }
1335 }
1336 }
1337
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001338 SkRect tmpDst;
1339 matrix.mapRect(&tmpDst, tmpSrc);
1340
1341 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1342 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1343 // Translate so that tempDst's top left is at the origin.
1344 matrix = *origDraw.fMatrix;
1345 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1346 draw.writable()->fMatrix = &matrix;
1347 }
1348 SkSize dstSize;
1349 dstSize.fWidth = tmpDst.width();
1350 dstSize.fHeight = tmpDst.height();
1351
1352 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001353}
1354
1355void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1356 int x, int y, const SkPaint& paint) {
1357 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001358 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001359 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001360
1361 // TODO: If the source device covers the whole of this device, we could
1362 // omit fNeedsClear -related flushing.
1363 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001364
1365 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001366 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001367
1368 GrRenderTarget* devRT = dev->accessRenderTarget();
1369 GrTexture* devTex;
1370 if (NULL == (devTex = devRT->asTexture())) {
1371 return;
1372 }
1373
robertphillips7b9e8a42014-12-11 08:20:31 -08001374 const SkImageInfo ii = dev->imageInfo();
1375 int w = ii.width();
1376 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001377
1378 SkImageFilter* filter = paint.getImageFilter();
1379 // This bitmap will own the filtered result as a texture.
1380 SkBitmap filteredBitmap;
1381
bsalomon49f085d2014-09-05 13:34:00 -07001382 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001383 SkIPoint offset = SkIPoint::Make(0, 0);
1384 SkMatrix matrix(*draw.fMatrix);
1385 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001386 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001387 // This cache is transient, and is freed (along with all its contained
1388 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001389 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001390 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001391 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1392 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001393 devTex = filteredBitmap.getTexture();
1394 w = filteredBitmap.width();
1395 h = filteredBitmap.height();
1396 x += offset.fX;
1397 y += offset.fY;
1398 } else {
1399 return;
1400 }
1401 }
1402
1403 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001404 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001405
bsalomonbed83a62015-04-15 14:18:34 -07001406 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1407 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1408 return;
1409 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001410
1411 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1412 SkIntToScalar(y),
1413 SkIntToScalar(w),
1414 SkIntToScalar(h));
1415
1416 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1417 // scratch texture).
1418 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1419 SK_Scalar1 * h / devTex->height());
1420
robertphillipsea461502015-05-26 11:38:03 -07001421 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, SkMatrix::I(), dstRect,
1422 srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001423}
1424
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001425bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001426 return filter->canFilterImageGPU();
1427}
1428
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001429bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001430 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001431 SkBitmap* result, SkIPoint* offset) {
1432 // want explicitly our impl, so guard against a subclass of us overriding it
1433 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1434 return false;
1435 }
1436
1437 SkAutoLockPixels alp(src, !src.getTexture());
1438 if (!src.getTexture() && !src.readyToDraw()) {
1439 return false;
1440 }
1441
1442 GrTexture* texture;
1443 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1444 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001445 AutoBitmapTexture abt(fContext, src, NULL, &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001446 if (!texture) {
1447 return false;
1448 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001449
senorblancod0d37ca2015-04-02 04:54:56 -07001450 return this->filterTexture(fContext, texture, src.width(), src.height(),
1451 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001452}
1453
reeda85d4d02015-05-06 12:56:48 -07001454static bool wrap_as_bm(const SkImage* image, SkBitmap* bm) {
bsalomon55812362015-06-10 08:49:28 -07001455 GrTexture* tex = as_IB(image)->getTexture();
reeda85d4d02015-05-06 12:56:48 -07001456 if (tex) {
reed8b26b992015-05-07 15:36:17 -07001457 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), bm);
1458 return true;
reeda85d4d02015-05-06 12:56:48 -07001459 } else {
reed8b26b992015-05-07 15:36:17 -07001460 return as_IB(image)->getROPixels(bm);
reeda85d4d02015-05-06 12:56:48 -07001461 }
reeda85d4d02015-05-06 12:56:48 -07001462}
1463
1464void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1465 const SkPaint& paint) {
1466 SkBitmap bm;
1467 if (wrap_as_bm(image, &bm)) {
1468 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1469 }
1470}
1471
1472void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
1473 const SkRect& dst, const SkPaint& paint) {
1474 SkBitmap bm;
1475 if (wrap_as_bm(image, &bm)) {
1476 this->drawBitmapRect(draw, bm, src, dst, paint, SkCanvas::kNone_DrawBitmapRectFlag);
1477 }
1478}
1479
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001480///////////////////////////////////////////////////////////////////////////////
1481
1482// must be in SkCanvas::VertexMode order
1483static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1484 kTriangles_GrPrimitiveType,
1485 kTriangleStrip_GrPrimitiveType,
1486 kTriangleFan_GrPrimitiveType,
1487};
1488
1489void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1490 int vertexCount, const SkPoint vertices[],
1491 const SkPoint texs[], const SkColor colors[],
1492 SkXfermode* xmode,
1493 const uint16_t indices[], int indexCount,
1494 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001495 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001496 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001497
dandov32a311b2014-07-15 19:46:26 -07001498 const uint16_t* outIndices;
1499 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1500 GrPrimitiveType primType;
1501 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001502
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001503 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1504 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001505
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001506 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001507
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001508 SkPaint copy(paint);
1509 copy.setStyle(SkPaint::kStroke_Style);
1510 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001511
dandov32a311b2014-07-15 19:46:26 -07001512 // we ignore the shader if texs is null.
bsalomonbed83a62015-04-15 14:18:34 -07001513 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy,
1514 SkColor2GrColor(copy.getColor()), NULL == colors, &grPaint)) {
1515 return;
1516 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001517
dandov32a311b2014-07-15 19:46:26 -07001518 primType = kLines_GrPrimitiveType;
1519 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001520 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001521 switch (vmode) {
1522 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001523 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001524 break;
1525 case SkCanvas::kTriangleStrip_VertexMode:
1526 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001527 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001528 break;
1529 }
mtklein533eb782014-08-27 10:39:42 -07001530
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001531 VertState state(vertexCount, indices, indexCount);
1532 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001533
dandov32a311b2014-07-15 19:46:26 -07001534 //number of indices for lines per triangle with kLines
1535 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001536
dandov32a311b2014-07-15 19:46:26 -07001537 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1538 outIndices = outAlloc.get();
1539 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001540 int i = 0;
1541 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001542 auxIndices[i] = state.f0;
1543 auxIndices[i + 1] = state.f1;
1544 auxIndices[i + 2] = state.f1;
1545 auxIndices[i + 3] = state.f2;
1546 auxIndices[i + 4] = state.f2;
1547 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001548 i += 6;
1549 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001550 } else {
dandov32a311b2014-07-15 19:46:26 -07001551 outIndices = indices;
1552 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001553
dandov32a311b2014-07-15 19:46:26 -07001554 if (NULL == texs || NULL == paint.getShader()) {
bsalomonbed83a62015-04-15 14:18:34 -07001555 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1556 SkColor2GrColor(paint.getColor()),
1557 NULL == colors, &grPaint)) {
1558 return;
1559 }
dandov32a311b2014-07-15 19:46:26 -07001560 } else {
bsalomonbed83a62015-04-15 14:18:34 -07001561 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix,
1562 NULL == colors, &grPaint)) {
1563 return;
1564 }
dandov32a311b2014-07-15 19:46:26 -07001565 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001566 }
1567
mtklein2583b622014-06-04 08:20:41 -07001568#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001569 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001570 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1571 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001572 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001573 }
1574 }
mtklein2583b622014-06-04 08:20:41 -07001575#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001576
1577 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001578 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001579 // need to convert byte order and from non-PM to PM
1580 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001581 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001582 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001583 color = colors[i];
1584 if (paint.getAlpha() != 255) {
1585 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1586 }
1587 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001588 }
1589 colors = convertedColors.get();
1590 }
robertphillipsea461502015-05-26 11:38:03 -07001591 fDrawContext->drawVertices(fRenderTarget,
1592 fClip,
1593 grPaint,
1594 *draw.fMatrix,
1595 primType,
1596 vertexCount,
1597 vertices,
1598 texs,
1599 colors,
1600 outIndices,
1601 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001602}
1603
1604///////////////////////////////////////////////////////////////////////////////
1605
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001606void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001607 size_t byteLength, SkScalar x, SkScalar y,
1608 const SkPaint& paint) {
1609 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001610 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001611
jvanverth8c27a182014-10-14 08:45:50 -07001612 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001613 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1614 return;
1615 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001616
jvanverth8c27a182014-10-14 08:45:50 -07001617 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001618
robertphillips2334fb62015-06-17 05:43:33 -07001619 fDrawContext->drawText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001620 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001621}
1622
fmalita05c4a432014-09-29 06:29:53 -07001623void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1624 const SkScalar pos[], int scalarsPerPos,
1625 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001626 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001627 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001628
jvanverth8c27a182014-10-14 08:45:50 -07001629 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001630 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1631 return;
1632 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001633
jvanverth8c27a182014-10-14 08:45:50 -07001634 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001635
robertphillips2334fb62015-06-17 05:43:33 -07001636 fDrawContext->drawPosText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001637 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1638 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001639}
1640
joshualitt9c328182015-03-23 08:13:04 -07001641void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1642 const SkPaint& paint, SkDrawFilter* drawFilter) {
1643 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawTextBlob", fContext);
1644 CHECK_SHOULD_DRAW(draw);
1645
1646 SkDEBUGCODE(this->validate();)
1647
robertphillips2334fb62015-06-17 05:43:33 -07001648 fDrawContext->drawTextBlob(fRenderTarget, fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001649 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001650}
1651
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001652///////////////////////////////////////////////////////////////////////////////
1653
reedb2db8982014-11-13 12:41:02 -08001654bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
robertphillips9c240a12015-05-28 07:45:59 -07001655 return GrTextContext::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001656}
1657
1658void SkGpuDevice::flush() {
1659 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001660 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001661}
1662
1663///////////////////////////////////////////////////////////////////////////////
1664
reed76033be2015-03-14 10:54:31 -07001665SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
bsalomonf2703d82014-10-28 14:33:06 -07001666 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001667 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001668 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001669 desc.fWidth = cinfo.fInfo.width();
1670 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001671 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001672
1673 SkAutoTUnref<GrTexture> texture;
1674 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001675 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001676
hcm4396fa52014-10-27 21:43:30 -07001677 // layers are never draw in repeat modes, so we can request an approx
1678 // match and ignore any padding.
bsalomond309e7a2015-04-30 14:18:54 -07001679 const GrTextureProvider::ScratchTexMatch match = (kNever_TileUsage == cinfo.fTileUsage) ?
1680 GrTextureProvider::kApprox_ScratchTexMatch :
1681 GrTextureProvider::kExact_ScratchTexMatch;
1682 texture.reset(fContext->textureProvider()->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001683
1684 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001685 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001686 return SkGpuDevice::Create(
1687 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001688 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001689 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001690 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001691 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001692 return NULL;
1693 }
1694}
1695
reed4a8126e2014-09-22 07:29:03 -07001696SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001697 // TODO: Change the signature of newSurface to take a budgeted parameter.
1698 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
vbuzinovdded6962015-06-12 08:59:45 -07001699 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
bsalomonafe30052015-01-16 07:32:33 -08001700 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001701}
1702
robertphillips30d2cc62014-09-24 08:52:18 -07001703bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001704 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001705#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001706 // todo: should handle this natively
1707 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001708 return false;
1709 }
1710
mtklein9db912c2015-05-19 11:11:26 -07001711 const SkBigPicture::AccelData* data = NULL;
1712 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1713 data = bp->accelData();
1714 }
robertphillips81f71b62014-11-11 04:54:49 -08001715 if (!data) {
1716 return false;
1717 }
1718
robertphillipse5524cd2015-02-20 12:30:26 -08001719 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1720 if (0 == gpuData->numBlocks()) {
1721 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001722 }
1723
robertphillipsfd61ed02014-10-28 07:21:44 -07001724 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001725
robertphillipse5524cd2015-02-20 12:30:26 -08001726 SkIRect iBounds;
1727 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1728 return false;
1729 }
1730
1731 SkRect clipBounds = SkRect::Make(iBounds);
1732
1733 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1734
robertphillipsfd61ed02014-10-28 07:21:44 -07001735 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001736 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001737 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001738 &atlasedNeedRendering, &atlasedRecycled,
vbuzinovdded6962015-06-12 08:59:45 -07001739 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001740
1741 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1742
1743 SkTDArray<GrHoistedLayer> needRendering, recycled;
1744
robertphillipse5524cd2015-02-20 12:30:26 -08001745 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1746
robertphillipsfd61ed02014-10-28 07:21:44 -07001747 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001748 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001749 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001750 &needRendering, &recycled,
vbuzinovdded6962015-06-12 08:59:45 -07001751 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001752
1753 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001754
robertphillips64bf7672014-08-21 13:07:35 -07001755 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001756 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1757 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001758
robertphillipsfd61ed02014-10-28 07:21:44 -07001759 GrLayerHoister::UnlockLayers(fContext, needRendering);
1760 GrLayerHoister::UnlockLayers(fContext, recycled);
1761 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1762 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001763
1764 return true;
robertphillips63242d72014-12-04 08:31:02 -08001765#else
1766 return false;
1767#endif
robertphillips64bf7672014-08-21 13:07:35 -07001768}
1769
senorblancobe129b22014-08-08 07:14:35 -07001770SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001771 // We always return a transient cache, so it is freed after each
1772 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001773 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001774}
reedf037e0b2014-10-30 11:34:15 -07001775
1776#endif