blob: 028ab561f4f57f1d9b36fb19bae0bb767d328cf1 [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
bsalomon74f681d2015-06-23 14:38:48 -0700125/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
126 should fail. */
127bool SkGpuDevice::CheckAlphaTypeAndGetFlags(
128 const SkImageInfo* info, SkGpuDevice::InitContents init, unsigned* flags) {
129 *flags = 0;
130 if (info) {
131 switch (info->alphaType()) {
132 case kPremul_SkAlphaType:
133 break;
134 case kOpaque_SkAlphaType:
135 *flags |= SkGpuDevice::kIsOpaque_Flag;
136 break;
137 default: // If it is unpremul or unknown don't try to render
138 return false;
139 }
140 }
141 if (kClear_InitContents == init) {
142 *flags |= kNeedClear_Flag;
143 }
144 return true;
145}
146
147SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props,
148 InitContents init) {
149 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, init);
senorblancod0d37ca2015-04-02 04:54:56 -0700150}
151
152SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
bsalomon74f681d2015-06-23 14:38:48 -0700153 const SkSurfaceProps* props, InitContents init) {
bsalomonafe30052015-01-16 07:32:33 -0800154 if (!rt || rt->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000155 return NULL;
156 }
bsalomon74f681d2015-06-23 14:38:48 -0700157 unsigned flags;
158 if (!CheckAlphaTypeAndGetFlags(NULL, init, &flags)) {
159 return NULL;
160 }
senorblancod0d37ca2015-04-02 04:54:56 -0700161 return SkNEW_ARGS(SkGpuDevice, (rt, width, height, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000162}
163
bsalomon74f681d2015-06-23 14:38:48 -0700164SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
165 const SkImageInfo& info, int sampleCount,
166 const SkSurfaceProps* props, InitContents init) {
167 unsigned flags;
168 if (!CheckAlphaTypeAndGetFlags(&info, init, &flags)) {
169 return NULL;
170 }
171
172 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
173 if (NULL == rt) {
174 return NULL;
175 }
176
177 return SkNEW_ARGS(SkGpuDevice, (rt, info.width(), info.height(), props, flags));
178}
179
senorblancod0d37ca2015-04-02 04:54:56 -0700180SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
181 const SkSurfaceProps* props, unsigned flags)
robertphillipsfcf78292015-06-19 11:49:52 -0700182 : INHERITED(SkSurfacePropsCopyOrDefault(props))
reedb2db8982014-11-13 12:41:02 -0800183{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000184 fDrawProcs = NULL;
185
bsalomonafe30052015-01-16 07:32:33 -0800186 fContext = SkRef(rt->getContext());
bsalomon74f681d2015-06-23 14:38:48 -0700187 fNeedClear = SkToBool(flags & kNeedClear_Flag);
188 fOpaque = SkToBool(flags & kIsOpaque_Flag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000189
bsalomonafe30052015-01-16 07:32:33 -0800190 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000191
bsalomon74f681d2015-06-23 14:38:48 -0700192 SkAlphaType at = fOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
193 SkImageInfo info = rt->surfacePriv().info(at).makeWH(width, height);
bsalomonafe30052015-01-16 07:32:33 -0800194 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt));
bsalomonafbf2d62014-09-30 12:18:44 -0700195 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700196 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700197
robertphillips7b05ff12015-06-19 14:14:54 -0700198 fDrawContext.reset(SkRef(fContext->drawContext(&this->surfaceProps())));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000199}
200
kkinnunenabcfab42015-02-22 22:53:44 -0800201GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
202 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000203 if (kUnknown_SkColorType == origInfo.colorType() ||
204 origInfo.width() < 0 || origInfo.height() < 0) {
205 return NULL;
206 }
207
bsalomonafe30052015-01-16 07:32:33 -0800208 if (!context) {
209 return NULL;
210 }
211
reede5ea5002014-09-03 11:54:58 -0700212 SkColorType ct = origInfo.colorType();
213 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700214 if (kRGB_565_SkColorType == ct) {
215 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800216 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
217 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700218 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800219 }
220 if (kOpaque_SkAlphaType != at) {
221 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000222 }
reede5ea5002014-09-03 11:54:58 -0700223 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000224
bsalomonf2703d82014-10-28 14:33:06 -0700225 GrSurfaceDesc desc;
226 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000227 desc.fWidth = info.width();
228 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000229 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000230 desc.fSampleCnt = sampleCount;
bsalomond309e7a2015-04-30 14:18:54 -0700231 GrTexture* texture = context->textureProvider()->createTexture(
232 desc, SkToBool(budgeted), NULL, 0);
kkinnunenabcfab42015-02-22 22:53:44 -0800233 if (NULL == texture) {
234 return NULL;
235 }
236 SkASSERT(NULL != texture->asRenderTarget());
237 return texture->asRenderTarget();
238}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000239
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000240SkGpuDevice::~SkGpuDevice() {
241 if (fDrawProcs) {
242 delete fDrawProcs;
243 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000244
bsalomon32d0b3b2014-08-29 07:50:23 -0700245 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000246 fContext->unref();
247}
248
249///////////////////////////////////////////////////////////////////////////////
250
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000251bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
252 int x, int y) {
253 DO_DEFERRED_CLEAR();
254
255 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000256 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000257 if (kUnknown_GrPixelConfig == config) {
258 return false;
259 }
260
261 uint32_t flags = 0;
262 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
263 flags = GrContext::kUnpremul_PixelOpsFlag;
264 }
bsalomon74f681d2015-06-23 14:38:48 -0700265 return fRenderTarget->readPixels(x, y, dstInfo.width(), dstInfo.height(), config, dstPixels,
266 dstRowBytes, flags);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000267}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000268
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000269bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
270 int x, int y) {
271 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000272 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000273 if (kUnknown_GrPixelConfig == config) {
274 return false;
275 }
276 uint32_t flags = 0;
277 if (kUnpremul_SkAlphaType == info.alphaType()) {
278 flags = GrContext::kUnpremul_PixelOpsFlag;
279 }
280 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
281
282 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700283 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000284
285 return true;
286}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000287
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000288const SkBitmap& SkGpuDevice::onAccessBitmap() {
289 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700290 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000291}
292
reed41e010c2015-06-09 12:16:53 -0700293bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
294 DO_DEFERRED_CLEAR();
295 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
296 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
297 // ... ugh.
298 fLegacyBitmap.notifyPixelsChanged();
299 return false;
300}
301
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000302void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
303 INHERITED::onAttachToCanvas(canvas);
304
305 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800306 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000307}
308
309void SkGpuDevice::onDetachFromCanvas() {
310 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800311 fClip.reset();
joshualitt44701df2015-02-23 14:44:57 -0800312 fClipStack.reset(NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000313}
314
315// call this every draw call, to ensure that the context reflects our state,
316// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800317void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800318 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000319
joshualitt44701df2015-02-23 14:44:57 -0800320 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000321
joshualitt570d2f82015-02-25 13:19:48 -0800322 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323
324 DO_DEFERRED_CLEAR();
325}
326
327GrRenderTarget* SkGpuDevice::accessRenderTarget() {
robertphillips7156b092015-05-14 08:54:12 -0700328 DO_DEFERRED_CLEAR();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000329 return fRenderTarget;
330}
331
reed8eddfb52014-12-04 07:50:14 -0800332void SkGpuDevice::clearAll() {
333 GrColor color = 0;
334 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
335 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillipsea461502015-05-26 11:38:03 -0700336 fDrawContext->clear(fRenderTarget, &rect, color, true);
bsalomonafe30052015-01-16 07:32:33 -0800337 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800338}
339
kkinnunenabcfab42015-02-22 22:53:44 -0800340void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
341 // Caller must have accessed the render target, because it knows the rt must be replaced.
342 SkASSERT(!fNeedClear);
343
344 SkSurface::Budgeted budgeted =
345 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
346 : SkSurface::kNo_Budgeted;
347
348 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
vbuzinovdded6962015-06-12 08:59:45 -0700349 fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->desc().fSampleCnt));
kkinnunenabcfab42015-02-22 22:53:44 -0800350
351 if (NULL == newRT) {
352 return;
353 }
354
355 if (shouldRetainContent) {
356 if (fRenderTarget->wasDestroyed()) {
357 return;
358 }
359 this->context()->copySurface(newRT, fRenderTarget);
360 }
361
362 SkASSERT(fRenderTarget != newRT);
363
364 fRenderTarget->unref();
365 fRenderTarget = newRT.detach();
366
bsalomon74f681d2015-06-23 14:38:48 -0700367#ifdef SK_DEBUG
368 SkImageInfo info = fRenderTarget->surfacePriv().info(fOpaque ? kOpaque_SkAlphaType :
369 kPremul_SkAlphaType);
370 SkASSERT(info == fLegacyBitmap.info());
371#endif
372 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (fLegacyBitmap.info(), fRenderTarget));
kkinnunenabcfab42015-02-22 22:53:44 -0800373 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700374
robertphillips7b05ff12015-06-19 14:14:54 -0700375 fDrawContext.reset(SkRef(fRenderTarget->getContext()->drawContext(&this->surfaceProps())));
kkinnunenabcfab42015-02-22 22:53:44 -0800376}
377
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000378///////////////////////////////////////////////////////////////////////////////
379
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000380void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800381 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700382 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000383
384 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700385 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
386 return;
387 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000388
robertphillipsea461502015-05-26 11:38:03 -0700389 fDrawContext->drawPaint(fRenderTarget, fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000390}
391
392// must be in SkCanvas::PointMode order
393static const GrPrimitiveType gPointMode2PrimtiveType[] = {
394 kPoints_GrPrimitiveType,
395 kLines_GrPrimitiveType,
396 kLineStrip_GrPrimitiveType
397};
398
399void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
400 size_t count, const SkPoint pts[], const SkPaint& paint) {
401 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800402 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000403
404 SkScalar width = paint.getStrokeWidth();
405 if (width < 0) {
406 return;
407 }
408
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000409 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700410 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
411 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700412 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
413 &grPaint)) {
414 return;
415 }
egdaniele61c4112014-06-12 10:24:21 -0700416 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700417 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700418 path.moveTo(pts[0]);
419 path.lineTo(pts[1]);
robertphillipsea461502015-05-26 11:38:03 -0700420 fDrawContext->drawPath(fRenderTarget, fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700421 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000422 }
423
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000424 // we only handle hairlines and paints without path effects or mask filters,
425 // else we let the SkDraw call our drawPath()
426 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
427 draw.drawPoints(mode, count, pts, paint, true);
428 return;
429 }
430
431 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700432 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
433 return;
434 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000435
robertphillipsea461502015-05-26 11:38:03 -0700436 fDrawContext->drawVertices(fRenderTarget,
437 fClip,
438 grPaint,
439 *draw.fMatrix,
440 gPointMode2PrimtiveType[mode],
441 SkToS32(count),
442 (SkPoint*)pts,
443 NULL,
444 NULL,
445 NULL,
446 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000447}
448
449///////////////////////////////////////////////////////////////////////////////
450
451void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
452 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700453 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
454
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000455 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800456 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000457
458 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
459 SkScalar width = paint.getStrokeWidth();
460
461 /*
462 We have special code for hairline strokes, miter-strokes, bevel-stroke
463 and fills. Anything else we just call our path code.
464 */
465 bool usePath = doStroke && width > 0 &&
466 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
467 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
468 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700469
470 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000471 usePath = true;
472 }
egdanield58a0ba2014-06-11 10:30:05 -0700473
joshualitt5531d512014-12-17 15:50:11 -0800474 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000475#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
476 if (doStroke) {
477#endif
478 usePath = true;
479#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
480 } else {
joshualitt5531d512014-12-17 15:50:11 -0800481 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000482 }
483#endif
484 }
485 // until we can both stroke and fill rectangles
486 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
487 usePath = true;
488 }
489
egdanield58a0ba2014-06-11 10:30:05 -0700490 GrStrokeInfo strokeInfo(paint);
491
492 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700493 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700494 usePath = true;
495 }
496
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000497 if (usePath) {
498 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700499 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000500 path.addRect(rect);
501 this->drawPath(draw, path, paint, NULL, true);
502 return;
503 }
504
505 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700506 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
507 return;
508 }
Mike Klein744fb732014-06-23 15:13:26 -0400509
robertphillipsea461502015-05-26 11:38:03 -0700510 fDrawContext->drawRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000511}
512
513///////////////////////////////////////////////////////////////////////////////
514
515void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800516 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700517 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000518 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800519 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000520
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000521 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700522 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
523 return;
524 }
Mike Klein744fb732014-06-23 15:13:26 -0400525
egdanield58a0ba2014-06-11 10:30:05 -0700526 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000527 if (paint.getMaskFilter()) {
528 // try to hit the fast path for drawing filtered round rects
529
530 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800531 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000532 if (devRRect.allCornersCircular()) {
533 SkRect maskRect;
534 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800535 draw.fClip->getBounds(),
536 *draw.fMatrix,
537 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000538 SkIRect finalIRect;
539 maskRect.roundOut(&finalIRect);
540 if (draw.fClip->quickReject(finalIRect)) {
541 // clipped out
542 return;
543 }
joshualitt25d9c152015-02-18 12:29:52 -0800544 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
545 fRenderTarget,
546 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800547 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800548 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700549 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700550 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000551 return;
552 }
553 }
554
555 }
556 }
557
558 }
559
egdanield58a0ba2014-06-11 10:30:05 -0700560 bool usePath = false;
561
562 if (paint.getMaskFilter()) {
563 usePath = true;
564 } else {
565 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700566 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700567 usePath = true;
568 }
569 }
570
571
572 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000573 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700574 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000575 path.addRRect(rect);
576 this->drawPath(draw, path, paint, NULL, true);
577 return;
578 }
Mike Klein744fb732014-06-23 15:13:26 -0400579
robertphillipsea461502015-05-26 11:38:03 -0700580 fDrawContext->drawRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000581}
582
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000583void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800584 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000585 SkStrokeRec stroke(paint);
586 if (stroke.isFillStyle()) {
587
588 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800589 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000590
591 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700592 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
593 &grPaint)) {
594 return;
595 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000596
597 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
robertphillipsea461502015-05-26 11:38:03 -0700598 fDrawContext->drawDRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000599 return;
600 }
601 }
602
603 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700604 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000605 path.addRRect(outer);
606 path.addRRect(inner);
607 path.setFillType(SkPath::kEvenOdd_FillType);
608
609 this->drawPath(draw, path, paint, NULL, true);
610}
611
612
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000613/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000614
615void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
616 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700617 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000618 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800619 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000620
egdanield58a0ba2014-06-11 10:30:05 -0700621 GrStrokeInfo strokeInfo(paint);
622
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000623 bool usePath = false;
624 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700625 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000626 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700627 } else {
628 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700629 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700630 usePath = true;
631 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000632 }
633
634 if (usePath) {
635 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700636 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000637 path.addOval(oval);
638 this->drawPath(draw, path, paint, NULL, true);
639 return;
640 }
641
642 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700643 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
644 return;
645 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000646
robertphillipsea461502015-05-26 11:38:03 -0700647 fDrawContext->drawOval(fRenderTarget, fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000648}
649
650#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000651
652///////////////////////////////////////////////////////////////////////////////
653
robertphillipsccb1b572015-05-27 11:02:55 -0700654static SkBitmap wrap_texture(GrTexture* texture, int width, int height) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000655 SkBitmap result;
senorblancod0d37ca2015-04-02 04:54:56 -0700656 result.setInfo(SkImageInfo::MakeN32Premul(width, height));
reed6c225732014-06-09 19:52:07 -0700657 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000658 return result;
659}
660
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000661void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
662 const SkPaint& paint, const SkMatrix* prePathMatrix,
663 bool pathIsMutable) {
664 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800665 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700666 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000667
robertphillipsccb1b572015-05-27 11:02:55 -0700668 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, fRenderTarget,
669 fClip, origSrcPath, paint,
670 *draw.fMatrix, prePathMatrix,
671 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000672}
673
674static const int kBmpSmallTileSize = 1 << 10;
675
676static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
677 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
678 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
679 return tilesX * tilesY;
680}
681
682static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
683 if (maxTileSize <= kBmpSmallTileSize) {
684 return maxTileSize;
685 }
686
687 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
688 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
689
690 maxTileTotalTileSize *= maxTileSize * maxTileSize;
691 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
692
693 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
694 return kBmpSmallTileSize;
695 } else {
696 return maxTileSize;
697 }
698}
699
700// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
701// pixels from the bitmap are necessary.
bsalomon74f681d2015-06-23 14:38:48 -0700702static void determine_clipped_src_rect(const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800703 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800704 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000705 const SkBitmap& bitmap,
706 const SkRect* srcRectPtr,
707 SkIRect* clippedSrcIRect) {
joshualitt570d2f82015-02-25 13:19:48 -0800708 clip.getConservativeBounds(rt, clippedSrcIRect, NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000709 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800710 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000711 clippedSrcIRect->setEmpty();
712 return;
713 }
714 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
715 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700716 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000717 // we've setup src space 0,0 to map to the top left of the src rect.
718 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000719 if (!clippedSrcRect.intersect(*srcRectPtr)) {
720 clippedSrcIRect->setEmpty();
721 return;
722 }
723 }
724 clippedSrcRect.roundOut(clippedSrcIRect);
725 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
726 if (!clippedSrcIRect->intersect(bmpBounds)) {
727 clippedSrcIRect->setEmpty();
728 }
729}
730
731bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800732 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000733 const GrTextureParams& params,
734 const SkRect* srcRectPtr,
735 int maxTileSize,
736 int* tileSize,
737 SkIRect* clippedSrcRect) const {
738 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700739 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000740 return false;
741 }
742
743 // if it's larger than the max tile size, then we have no choice but tiling.
744 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
bsalomon74f681d2015-06-23 14:38:48 -0700745 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, bitmap,
joshualitt570d2f82015-02-25 13:19:48 -0800746 srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000747 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
748 return true;
749 }
750
751 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
752 return false;
753 }
754
755 // if the entire texture is already in our cache then no reason to tile it
756 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
757 return false;
758 }
759
760 // At this point we know we could do the draw by uploading the entire bitmap
761 // as a texture. However, if the texture would be large compared to the
762 // cache size and we don't require most of it for this draw then tile to
763 // reduce the amount of upload and cache spill.
764
765 // assumption here is that sw bitmap size is a good proxy for its size as
766 // a texture
767 size_t bmpSize = bitmap.getSize();
768 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000769 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000770 if (bmpSize < cacheSize / 2) {
771 return false;
772 }
773
774 // Figure out how much of the src we will need based on the src rect and clipping.
bsalomon74f681d2015-06-23 14:38:48 -0700775 determine_clipped_src_rect(fRenderTarget, fClip, viewMatrix, bitmap, srcRectPtr,
joshualitt25d9c152015-02-18 12:29:52 -0800776 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000777 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
778 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
779 kBmpSmallTileSize * kBmpSmallTileSize;
780
781 return usedTileBytes < 2 * bmpSize;
782}
783
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000784void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000785 const SkBitmap& bitmap,
786 const SkMatrix& m,
787 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000788 SkMatrix concat;
789 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
790 if (!m.isIdentity()) {
791 concat.setConcat(*draw->fMatrix, m);
792 draw.writable()->fMatrix = &concat;
793 }
794 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000795}
796
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000797// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000798// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
799// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000800static inline void clamped_outset_with_offset(SkIRect* iRect,
801 int outset,
802 SkPoint* offset,
803 const SkIRect& clamp) {
804 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000805
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000806 int leftClampDelta = clamp.fLeft - iRect->fLeft;
807 if (leftClampDelta > 0) {
808 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000809 iRect->fLeft = clamp.fLeft;
810 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000811 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000812 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000813
814 int topClampDelta = clamp.fTop - iRect->fTop;
815 if (topClampDelta > 0) {
816 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000817 iRect->fTop = clamp.fTop;
818 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000819 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000820 }
821
822 if (iRect->fRight > clamp.fRight) {
823 iRect->fRight = clamp.fRight;
824 }
825 if (iRect->fBottom > clamp.fBottom) {
826 iRect->fBottom = clamp.fBottom;
827 }
828}
829
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000830static bool has_aligned_samples(const SkRect& srcRect,
831 const SkRect& transformedRect) {
832 // detect pixel disalignment
833 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
834 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
835 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
836 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
837 SkScalarAbs(transformedRect.width() - srcRect.width()) <
838 COLOR_BLEED_TOLERANCE &&
839 SkScalarAbs(transformedRect.height() - srcRect.height()) <
840 COLOR_BLEED_TOLERANCE) {
841 return true;
842 }
843 return false;
844}
845
846static bool may_color_bleed(const SkRect& srcRect,
847 const SkRect& transformedRect,
848 const SkMatrix& m) {
849 // Only gets called if has_aligned_samples returned false.
850 // So we can assume that sampling is axis aligned but not texel aligned.
851 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
852 SkRect innerSrcRect(srcRect), innerTransformedRect,
853 outerTransformedRect(transformedRect);
854 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
855 m.mapRect(&innerTransformedRect, innerSrcRect);
856
857 // The gap between outerTransformedRect and innerTransformedRect
858 // represents the projection of the source border area, which is
859 // problematic for color bleeding. We must check whether any
860 // destination pixels sample the border area.
861 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
862 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
863 SkIRect outer, inner;
864 outerTransformedRect.round(&outer);
865 innerTransformedRect.round(&inner);
866 // If the inner and outer rects round to the same result, it means the
867 // border does not overlap any pixel centers. Yay!
868 return inner != outer;
869}
870
871static bool needs_texture_domain(const SkBitmap& bitmap,
872 const SkRect& srcRect,
873 GrTextureParams &params,
874 const SkMatrix& contextMatrix,
875 bool bicubic) {
876 bool needsTextureDomain = false;
senorblancod0d37ca2015-04-02 04:54:56 -0700877 GrTexture* tex = bitmap.getTexture();
878 int width = tex ? tex->width() : bitmap.width();
879 int height = tex ? tex->height() : bitmap.height();
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000880
881 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
882 // Need texture domain if drawing a sub rect
senorblancod0d37ca2015-04-02 04:54:56 -0700883 needsTextureDomain = srcRect.width() < width ||
884 srcRect.height() < height;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000885 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
886 // sampling is axis-aligned
887 SkRect transformedRect;
888 contextMatrix.mapRect(&transformedRect, srcRect);
889
890 if (has_aligned_samples(srcRect, transformedRect)) {
891 params.setFilterMode(GrTextureParams::kNone_FilterMode);
892 needsTextureDomain = false;
893 } else {
894 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
895 }
896 }
897 }
898 return needsTextureDomain;
899}
900
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000901void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
902 const SkBitmap& bitmap,
903 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000904 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000905 const SkPaint& paint,
906 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -0800907 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000908
909 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000910 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000911 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
912 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000913 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000914 SkScalar w = SkIntToScalar(bitmap.width());
915 SkScalar h = SkIntToScalar(bitmap.height());
916 dstSize.fWidth = w;
917 dstSize.fHeight = h;
918 srcRect.set(0, 0, w, h);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000919 } else {
bsalomon49f085d2014-09-05 13:34:00 -0700920 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000921 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000922 dstSize = *dstSizePtr;
senorblancod0d37ca2015-04-02 04:54:56 -0700923 }
924 GrTexture* tex = bitmap.getTexture();
925 int width = tex ? tex->width() : bitmap.width();
926 int height = tex ? tex->height() : bitmap.height();
927 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
928 srcRect.fRight >= width && srcRect.fBottom >= height) {
929 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000930 }
931
derekf367e1862014-12-02 11:02:06 -0800932 // If the render target is not msaa and draw is antialiased, we call
933 // drawRect instead of drawing on the render target directly.
934 // FIXME: the tiled bitmap code path doesn't currently support
935 // anti-aliased edges, we work around that for now by drawing directly
936 // if the image size exceeds maximum texture size.
bsalomon76228632015-05-29 08:02:10 -0700937 int maxTextureSize = fContext->caps()->maxTextureSize();
vbuzinovdded6962015-06-12 08:59:45 -0700938 bool directDraw = fRenderTarget->isUnifiedMultisampled() ||
derekf367e1862014-12-02 11:02:06 -0800939 !paint.isAntiAlias() ||
940 bitmap.width() > maxTextureSize ||
941 bitmap.height() > maxTextureSize;
942
943 // we check whether dst rect are pixel aligned
944 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -0800945 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -0800946
947 if (staysRect) {
948 SkRect rect;
949 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -0800950 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -0800951 const SkScalar *scalars = rect.asScalars();
952 bool isDstPixelAligned = true;
953 for (int i = 0; i < 4; i++) {
954 if (!SkScalarIsInt(scalars[i])) {
955 isDstPixelAligned = false;
956 break;
957 }
958 }
959
960 if (isDstPixelAligned)
961 directDraw = true;
962 }
963 }
964
965 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000966 // Convert the bitmap to a shader so that the rect can be drawn
967 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000968 SkBitmap tmp; // subset of bitmap, if necessary
969 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000970 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -0700971 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000972 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
973 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
974 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000975 // In bleed mode we position and trim the bitmap based on the src rect which is
976 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
977 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
978 // compensate.
979 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
980 SkIRect iSrc;
981 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000983 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
984 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000985
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000986 if (!bitmap.extractSubset(&tmp, iSrc)) {
987 return; // extraction failed
988 }
989 bitmapPtr = &tmp;
990 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000991
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000992 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000993 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000994 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000995 } else {
996 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000997 }
998
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000999 SkPaint paintWithShader(paint);
1000 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001001 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001002 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1003 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001004
1005 return;
1006 }
1007
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001008 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1009 // the view matrix rather than a local matrix.
1010 SkMatrix m;
1011 m.setScale(dstSize.fWidth / srcRect.width(),
1012 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -08001013 SkMatrix viewM = *draw.fMatrix;
1014 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001015
1016 GrTextureParams params;
reed93a12152015-03-16 10:08:34 -07001017 SkFilterQuality paintFilterQuality = paint.getFilterQuality();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001018 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001019
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001020 bool doBicubic = false;
1021
reed93a12152015-03-16 10:08:34 -07001022 switch(paintFilterQuality) {
1023 case kNone_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001024 textureFilterMode = GrTextureParams::kNone_FilterMode;
1025 break;
reed93a12152015-03-16 10:08:34 -07001026 case kLow_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001027 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1028 break;
reed93a12152015-03-16 10:08:34 -07001029 case kMedium_SkFilterQuality:
joshualitt5531d512014-12-17 15:50:11 -08001030 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001031 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1032 } else {
1033 // Don't trigger MIP level generation unnecessarily.
1034 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1035 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001036 break;
reed93a12152015-03-16 10:08:34 -07001037 case kHigh_SkFilterQuality:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001038 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001039 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001040 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001041 break;
1042 default:
1043 SkErrorInternals::SetError( kInvalidPaint_SkError,
1044 "Sorry, I don't understand the filtering "
1045 "mode you asked for. Falling back to "
1046 "MIPMaps.");
1047 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1048 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001049 }
1050
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001051 int tileFilterPad;
1052 if (doBicubic) {
1053 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1054 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1055 tileFilterPad = 0;
1056 } else {
1057 tileFilterPad = 1;
1058 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001059 params.setFilterMode(textureFilterMode);
1060
bsalomon76228632015-05-29 08:02:10 -07001061 int maxTileSize = fContext->caps()->maxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001062 int tileSize;
1063
1064 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001065 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001066 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001067 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1068 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001069 } else {
1070 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001071 bool needsTextureDomain = needs_texture_domain(bitmap,
1072 srcRect,
1073 params,
joshualitt5531d512014-12-17 15:50:11 -08001074 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001075 doBicubic);
1076 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001077 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001078 srcRect,
1079 params,
1080 paint,
1081 flags,
1082 doBicubic,
1083 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001084 }
1085}
1086
1087// Break 'bitmap' into several tiles to draw it since it has already
1088// been determined to be too large to fit in VRAM
1089void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001090 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001091 const SkRect& srcRect,
1092 const SkIRect& clippedSrcIRect,
1093 const GrTextureParams& params,
1094 const SkPaint& paint,
1095 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001096 int tileSize,
1097 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001098 // The following pixel lock is technically redundant, but it is desirable
1099 // to lock outside of the tile loop to prevent redecoding the whole image
1100 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1101 // is larger than the limit of the discardable memory pool.
1102 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001103 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1104
1105 int nx = bitmap.width() / tileSize;
1106 int ny = bitmap.height() / tileSize;
1107 for (int x = 0; x <= nx; x++) {
1108 for (int y = 0; y <= ny; y++) {
1109 SkRect tileR;
1110 tileR.set(SkIntToScalar(x * tileSize),
1111 SkIntToScalar(y * tileSize),
1112 SkIntToScalar((x + 1) * tileSize),
1113 SkIntToScalar((y + 1) * tileSize));
1114
1115 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1116 continue;
1117 }
1118
1119 if (!tileR.intersect(srcRect)) {
1120 continue;
1121 }
1122
1123 SkBitmap tmpB;
1124 SkIRect iTileR;
1125 tileR.roundOut(&iTileR);
1126 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1127 SkIntToScalar(iTileR.fTop));
1128
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001129 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001130 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001131 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001132 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001133 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001134
robertphillipsec8bb942014-11-21 10:16:25 -08001135 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001136 SkIRect iClampRect;
1137
1138 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1139 // In bleed mode we want to always expand the tile on all edges
1140 // but stay within the bitmap bounds
1141 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1142 } else {
1143 // In texture-domain/clamp mode we only want to expand the
1144 // tile on edges interior to "srcRect" (i.e., we want to
1145 // not bleed across the original clamped edges)
1146 srcRect.roundOut(&iClampRect);
1147 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001148 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1149 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001150 }
1151
1152 if (bitmap.extractSubset(&tmpB, iTileR)) {
1153 // now offset it to make it "local" to our tmp bitmap
1154 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001155 GrTextureParams paramsTemp = params;
1156 bool needsTextureDomain = needs_texture_domain(bitmap,
1157 srcRect,
1158 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001159 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001160 bicubic);
1161 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001162 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001163 tileR,
1164 paramsTemp,
1165 paint,
1166 flags,
1167 bicubic,
1168 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001169 }
1170 }
1171 }
1172}
1173
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001174
1175/*
1176 * This is called by drawBitmap(), which has to handle images that may be too
1177 * large to be represented by a single texture.
1178 *
1179 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1180 * and that non-texture portion of the GrPaint has already been setup.
1181 */
1182void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001183 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001184 const SkRect& srcRect,
1185 const GrTextureParams& params,
1186 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001187 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001188 bool bicubic,
1189 bool needsTextureDomain) {
bsalomon76228632015-05-29 08:02:10 -07001190 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1191 bitmap.height() <= fContext->caps()->maxTextureSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001192
1193 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001194 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195 if (NULL == texture) {
1196 return;
1197 }
1198
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001199 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001200 SkRect paintRect;
1201 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1202 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1203 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1204 SkScalarMul(srcRect.fTop, hInv),
1205 SkScalarMul(srcRect.fRight, wInv),
1206 SkScalarMul(srcRect.fBottom, hInv));
1207
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001208 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001209 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001210 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001211 // Use a constrained texture domain to avoid color bleeding
1212 SkScalar left, top, right, bottom;
1213 if (srcRect.width() > SK_Scalar1) {
1214 SkScalar border = SK_ScalarHalf / texture->width();
1215 left = paintRect.left() + border;
1216 right = paintRect.right() - border;
1217 } else {
1218 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1219 }
1220 if (srcRect.height() > SK_Scalar1) {
1221 SkScalar border = SK_ScalarHalf / texture->height();
1222 top = paintRect.top() + border;
1223 bottom = paintRect.bottom() - border;
1224 } else {
1225 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1226 }
1227 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001228 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001229 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001230 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001231 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001232 SkMatrix::I(),
1233 textureDomain,
1234 GrTextureDomain::kClamp_Mode,
1235 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001236 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001237 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001238 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1239 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001240 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001241 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001242 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001243 }
1244
1245 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1246 // the rest from the SkPaint.
1247 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001248 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001249 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001250 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1251 SkColor2GrColor(paint.getColor());
bsalomonbed83a62015-04-15 14:18:34 -07001252 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false,
1253 &grPaint)) {
1254 return;
1255 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001256
robertphillipsea461502015-05-26 11:38:03 -07001257 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dstRect,
1258 paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001259}
1260
fmalita2d97bc12014-11-20 10:44:58 -08001261bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001262 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001263 const SkImageFilter* filter,
1264 const SkImageFilter::Context& ctx,
1265 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001266 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001267
robertphillipsefbffed2015-06-22 12:06:08 -07001268 SkImageFilter::Proxy proxy(this);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001269
1270 if (filter->canFilterImageGPU()) {
senorblancod0d37ca2015-04-02 04:54:56 -07001271 return filter->filterImageGPU(&proxy, wrap_texture(texture, width, height),
1272 ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001273 } else {
1274 return false;
1275 }
1276}
1277
1278void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1279 int left, int top, const SkPaint& paint) {
1280 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001281 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001282
1283 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1284 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1285 return;
1286 }
1287
1288 int w = bitmap.width();
1289 int h = bitmap.height();
1290
1291 GrTexture* texture;
1292 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001293 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001294 if (!texture) {
1295 return;
1296 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001297
1298 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001299 // This bitmap will own the filtered result as a texture.
1300 SkBitmap filteredBitmap;
1301
bsalomon49f085d2014-09-05 13:34:00 -07001302 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001303 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001304 SkMatrix matrix(*draw.fMatrix);
1305 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001306 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001307 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001308 // This cache is transient, and is freed (along with all its contained
1309 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001310 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001311 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001312 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001313 texture = (GrTexture*) filteredBitmap.getTexture();
1314 w = filteredBitmap.width();
1315 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001316 left += offset.x();
1317 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001318 } else {
1319 return;
1320 }
1321 }
1322
1323 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001324 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001325
bsalomonbed83a62015-04-15 14:18:34 -07001326 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1327 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1328 return;
1329 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001330
robertphillipsea461502015-05-26 11:38:03 -07001331 fDrawContext->drawNonAARectToRect(fRenderTarget,
1332 fClip,
1333 grPaint,
1334 SkMatrix::I(),
1335 SkRect::MakeXYWH(SkIntToScalar(left),
1336 SkIntToScalar(top),
1337 SkIntToScalar(w),
1338 SkIntToScalar(h)),
1339 SkRect::MakeXYWH(0,
1340 0,
1341 SK_Scalar1 * w / texture->width(),
1342 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001343}
1344
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001345void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001346 const SkRect* src, const SkRect& dst,
1347 const SkPaint& paint,
1348 SkCanvas::DrawBitmapRectFlags flags) {
1349 SkMatrix matrix;
1350 SkRect bitmapBounds, tmpSrc;
1351
1352 bitmapBounds.set(0, 0,
1353 SkIntToScalar(bitmap.width()),
1354 SkIntToScalar(bitmap.height()));
1355
1356 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001357 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001358 tmpSrc = *src;
1359 } else {
1360 tmpSrc = bitmapBounds;
1361 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001362
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001363 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1364
1365 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001366 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001367 if (!bitmapBounds.contains(tmpSrc)) {
1368 if (!tmpSrc.intersect(bitmapBounds)) {
1369 return; // nothing to draw
1370 }
1371 }
1372 }
1373
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001374 SkRect tmpDst;
1375 matrix.mapRect(&tmpDst, tmpSrc);
1376
1377 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1378 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1379 // Translate so that tempDst's top left is at the origin.
1380 matrix = *origDraw.fMatrix;
1381 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1382 draw.writable()->fMatrix = &matrix;
1383 }
1384 SkSize dstSize;
1385 dstSize.fWidth = tmpDst.width();
1386 dstSize.fHeight = tmpDst.height();
1387
1388 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001389}
1390
1391void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1392 int x, int y, const SkPaint& paint) {
1393 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001394 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001395 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001396
1397 // TODO: If the source device covers the whole of this device, we could
1398 // omit fNeedsClear -related flushing.
1399 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001400
1401 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001402 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001403
1404 GrRenderTarget* devRT = dev->accessRenderTarget();
1405 GrTexture* devTex;
1406 if (NULL == (devTex = devRT->asTexture())) {
1407 return;
1408 }
1409
robertphillips7b9e8a42014-12-11 08:20:31 -08001410 const SkImageInfo ii = dev->imageInfo();
1411 int w = ii.width();
1412 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001413
1414 SkImageFilter* filter = paint.getImageFilter();
1415 // This bitmap will own the filtered result as a texture.
1416 SkBitmap filteredBitmap;
1417
bsalomon49f085d2014-09-05 13:34:00 -07001418 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001419 SkIPoint offset = SkIPoint::Make(0, 0);
1420 SkMatrix matrix(*draw.fMatrix);
1421 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001422 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001423 // This cache is transient, and is freed (along with all its contained
1424 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001425 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001426 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001427 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1428 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001429 devTex = filteredBitmap.getTexture();
1430 w = filteredBitmap.width();
1431 h = filteredBitmap.height();
1432 x += offset.fX;
1433 y += offset.fY;
1434 } else {
1435 return;
1436 }
1437 }
1438
1439 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001440 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001441
bsalomonbed83a62015-04-15 14:18:34 -07001442 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1443 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1444 return;
1445 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001446
1447 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1448 SkIntToScalar(y),
1449 SkIntToScalar(w),
1450 SkIntToScalar(h));
1451
1452 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1453 // scratch texture).
1454 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1455 SK_Scalar1 * h / devTex->height());
1456
robertphillipsea461502015-05-26 11:38:03 -07001457 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, SkMatrix::I(), dstRect,
1458 srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001459}
1460
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001461bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001462 return filter->canFilterImageGPU();
1463}
1464
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001465bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001466 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001467 SkBitmap* result, SkIPoint* offset) {
1468 // want explicitly our impl, so guard against a subclass of us overriding it
1469 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1470 return false;
1471 }
1472
1473 SkAutoLockPixels alp(src, !src.getTexture());
1474 if (!src.getTexture() && !src.readyToDraw()) {
1475 return false;
1476 }
1477
1478 GrTexture* texture;
1479 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1480 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001481 AutoBitmapTexture abt(fContext, src, NULL, &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001482 if (!texture) {
1483 return false;
1484 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001485
senorblancod0d37ca2015-04-02 04:54:56 -07001486 return this->filterTexture(fContext, texture, src.width(), src.height(),
1487 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001488}
1489
reeda85d4d02015-05-06 12:56:48 -07001490static bool wrap_as_bm(const SkImage* image, SkBitmap* bm) {
bsalomon55812362015-06-10 08:49:28 -07001491 GrTexture* tex = as_IB(image)->getTexture();
reeda85d4d02015-05-06 12:56:48 -07001492 if (tex) {
reed8b26b992015-05-07 15:36:17 -07001493 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), bm);
1494 return true;
reeda85d4d02015-05-06 12:56:48 -07001495 } else {
reed8b26b992015-05-07 15:36:17 -07001496 return as_IB(image)->getROPixels(bm);
reeda85d4d02015-05-06 12:56:48 -07001497 }
reeda85d4d02015-05-06 12:56:48 -07001498}
1499
1500void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1501 const SkPaint& paint) {
1502 SkBitmap bm;
1503 if (wrap_as_bm(image, &bm)) {
1504 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1505 }
1506}
1507
1508void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
1509 const SkRect& dst, const SkPaint& paint) {
1510 SkBitmap bm;
1511 if (wrap_as_bm(image, &bm)) {
1512 this->drawBitmapRect(draw, bm, src, dst, paint, SkCanvas::kNone_DrawBitmapRectFlag);
1513 }
1514}
1515
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001516///////////////////////////////////////////////////////////////////////////////
1517
1518// must be in SkCanvas::VertexMode order
1519static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1520 kTriangles_GrPrimitiveType,
1521 kTriangleStrip_GrPrimitiveType,
1522 kTriangleFan_GrPrimitiveType,
1523};
1524
1525void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1526 int vertexCount, const SkPoint vertices[],
1527 const SkPoint texs[], const SkColor colors[],
1528 SkXfermode* xmode,
1529 const uint16_t indices[], int indexCount,
1530 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001531 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001532 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001533
dandov32a311b2014-07-15 19:46:26 -07001534 const uint16_t* outIndices;
1535 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1536 GrPrimitiveType primType;
1537 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001538
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001539 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1540 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001541
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001542 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001543
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001544 SkPaint copy(paint);
1545 copy.setStyle(SkPaint::kStroke_Style);
1546 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001547
dandov32a311b2014-07-15 19:46:26 -07001548 // we ignore the shader if texs is null.
bsalomonbed83a62015-04-15 14:18:34 -07001549 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy,
1550 SkColor2GrColor(copy.getColor()), NULL == colors, &grPaint)) {
1551 return;
1552 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001553
dandov32a311b2014-07-15 19:46:26 -07001554 primType = kLines_GrPrimitiveType;
1555 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001556 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001557 switch (vmode) {
1558 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001559 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001560 break;
1561 case SkCanvas::kTriangleStrip_VertexMode:
1562 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001563 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001564 break;
1565 }
mtklein533eb782014-08-27 10:39:42 -07001566
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001567 VertState state(vertexCount, indices, indexCount);
1568 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001569
dandov32a311b2014-07-15 19:46:26 -07001570 //number of indices for lines per triangle with kLines
1571 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001572
dandov32a311b2014-07-15 19:46:26 -07001573 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1574 outIndices = outAlloc.get();
1575 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001576 int i = 0;
1577 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001578 auxIndices[i] = state.f0;
1579 auxIndices[i + 1] = state.f1;
1580 auxIndices[i + 2] = state.f1;
1581 auxIndices[i + 3] = state.f2;
1582 auxIndices[i + 4] = state.f2;
1583 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001584 i += 6;
1585 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001586 } else {
dandov32a311b2014-07-15 19:46:26 -07001587 outIndices = indices;
1588 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001589
dandov32a311b2014-07-15 19:46:26 -07001590 if (NULL == texs || NULL == paint.getShader()) {
bsalomonbed83a62015-04-15 14:18:34 -07001591 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1592 SkColor2GrColor(paint.getColor()),
1593 NULL == colors, &grPaint)) {
1594 return;
1595 }
dandov32a311b2014-07-15 19:46:26 -07001596 } else {
bsalomonbed83a62015-04-15 14:18:34 -07001597 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix,
1598 NULL == colors, &grPaint)) {
1599 return;
1600 }
dandov32a311b2014-07-15 19:46:26 -07001601 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001602 }
1603
mtklein2583b622014-06-04 08:20:41 -07001604#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001605 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001606 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1607 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001608 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001609 }
1610 }
mtklein2583b622014-06-04 08:20:41 -07001611#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001612
1613 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001614 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001615 // need to convert byte order and from non-PM to PM
1616 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001617 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001618 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001619 color = colors[i];
1620 if (paint.getAlpha() != 255) {
1621 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1622 }
1623 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001624 }
1625 colors = convertedColors.get();
1626 }
robertphillipsea461502015-05-26 11:38:03 -07001627 fDrawContext->drawVertices(fRenderTarget,
1628 fClip,
1629 grPaint,
1630 *draw.fMatrix,
1631 primType,
1632 vertexCount,
1633 vertices,
1634 texs,
1635 colors,
1636 outIndices,
1637 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001638}
1639
1640///////////////////////////////////////////////////////////////////////////////
1641
reedca109532015-06-25 16:25:25 -07001642static void append_quad_indices(uint16_t indices[], int quadIndex) {
1643 int i = quadIndex * 4;
1644 indices[0] = i + 0; indices[1] = i + 1; indices[2] = i + 2;
1645 indices[3] = i + 2; indices[4] = i + 3; indices[5] = i + 0;
1646}
1647
1648void SkGpuDevice::drawAtlas(const SkDraw& d, const SkImage* atlas, const SkRSXform xform[],
1649 const SkRect texRect[], const SkColor colors[], int count,
1650 SkXfermode::Mode mode, const SkPaint& paint) {
1651 if (paint.isAntiAlias()) {
1652 this->INHERITED::drawAtlas(d, atlas, xform, texRect, colors, count, mode, paint);
1653 return;
1654 }
1655
1656 SkPaint p(paint);
1657 p.setShader(atlas->newShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
1658
1659 const int vertCount = count * 4;
1660 const int indexCount = count * 6;
1661 SkAutoTMalloc<SkPoint> vertStorage(vertCount * 2);
1662 SkPoint* verts = vertStorage.get();
1663 SkPoint* texs = verts + vertCount;
1664 SkAutoTMalloc<uint16_t> indexStorage(indexCount);
1665 uint16_t* indices = indexStorage.get();
1666 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(mode));
1667
1668 for (int i = 0; i < count; ++i) {
1669 xform[i].toQuad(texRect[i].width(), texRect[i].height(), verts);
1670 texRect[i].toQuad(texs);
1671 append_quad_indices(indices, i);
1672 verts += 4;
1673 texs += 4;
1674 indices += 6;
1675 }
1676
1677 verts = vertStorage.get();
1678 texs = verts + vertCount;
1679 indices = indexStorage.get();
1680 this->drawVertices(d, SkCanvas::kTriangles_VertexMode, vertCount, verts, texs, colors, xfer,
1681 indices, indexCount, p);
1682}
1683
1684///////////////////////////////////////////////////////////////////////////////
1685
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001686void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001687 size_t byteLength, SkScalar x, SkScalar y,
1688 const SkPaint& paint) {
1689 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001690 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001691
jvanverth8c27a182014-10-14 08:45:50 -07001692 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001693 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1694 return;
1695 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001696
jvanverth8c27a182014-10-14 08:45:50 -07001697 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001698
robertphillips2334fb62015-06-17 05:43:33 -07001699 fDrawContext->drawText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001700 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001701}
1702
fmalita05c4a432014-09-29 06:29:53 -07001703void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1704 const SkScalar pos[], int scalarsPerPos,
1705 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001706 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001707 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001708
jvanverth8c27a182014-10-14 08:45:50 -07001709 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001710 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1711 return;
1712 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001713
jvanverth8c27a182014-10-14 08:45:50 -07001714 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001715
robertphillips2334fb62015-06-17 05:43:33 -07001716 fDrawContext->drawPosText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
joshualitt6e8cd962015-03-20 10:30:14 -07001717 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1718 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001719}
1720
joshualitt9c328182015-03-23 08:13:04 -07001721void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1722 const SkPaint& paint, SkDrawFilter* drawFilter) {
1723 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawTextBlob", fContext);
1724 CHECK_SHOULD_DRAW(draw);
1725
1726 SkDEBUGCODE(this->validate();)
1727
robertphillips2334fb62015-06-17 05:43:33 -07001728 fDrawContext->drawTextBlob(fRenderTarget, fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001729 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001730}
1731
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001732///////////////////////////////////////////////////////////////////////////////
1733
reedb2db8982014-11-13 12:41:02 -08001734bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
robertphillips9c240a12015-05-28 07:45:59 -07001735 return GrTextContext::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001736}
1737
1738void SkGpuDevice::flush() {
1739 DO_DEFERRED_CLEAR();
bsalomonc49e8682015-06-30 11:37:35 -07001740 fRenderTarget->prepareForExternalIO();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001741}
1742
1743///////////////////////////////////////////////////////////////////////////////
1744
reed76033be2015-03-14 10:54:31 -07001745SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
bsalomonf2703d82014-10-28 14:33:06 -07001746 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001747 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001748 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001749 desc.fWidth = cinfo.fInfo.width();
1750 desc.fHeight = cinfo.fInfo.height();
vbuzinovdded6962015-06-12 08:59:45 -07001751 desc.fSampleCnt = fRenderTarget->desc().fSampleCnt;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001752
1753 SkAutoTUnref<GrTexture> texture;
1754 // Skia's convention is to only clear a device if it is non-opaque.
bsalomon74f681d2015-06-23 14:38:48 -07001755 InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001756
hcm4396fa52014-10-27 21:43:30 -07001757 // layers are never draw in repeat modes, so we can request an approx
1758 // match and ignore any padding.
bsalomond309e7a2015-04-30 14:18:54 -07001759 const GrTextureProvider::ScratchTexMatch match = (kNever_TileUsage == cinfo.fTileUsage) ?
1760 GrTextureProvider::kApprox_ScratchTexMatch :
1761 GrTextureProvider::kExact_ScratchTexMatch;
1762 texture.reset(fContext->textureProvider()->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001763
1764 if (texture) {
robertphillips7b05ff12015-06-19 14:14:54 -07001765 SkSurfaceProps props(this->surfaceProps().flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001766 return SkGpuDevice::Create(
bsalomon74f681d2015-06-23 14:38:48 -07001767 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, init);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001768 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001769 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001770 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001771 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001772 return NULL;
1773 }
1774}
1775
reed4a8126e2014-09-22 07:29:03 -07001776SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001777 // TODO: Change the signature of newSurface to take a budgeted parameter.
1778 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
vbuzinovdded6962015-06-12 08:59:45 -07001779 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->desc().fSampleCnt,
bsalomonafe30052015-01-16 07:32:33 -08001780 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001781}
1782
robertphillips30d2cc62014-09-24 08:52:18 -07001783bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001784 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001785#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001786 // todo: should handle this natively
1787 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001788 return false;
1789 }
1790
mtklein9db912c2015-05-19 11:11:26 -07001791 const SkBigPicture::AccelData* data = NULL;
1792 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1793 data = bp->accelData();
1794 }
robertphillips81f71b62014-11-11 04:54:49 -08001795 if (!data) {
1796 return false;
1797 }
1798
robertphillipse5524cd2015-02-20 12:30:26 -08001799 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1800 if (0 == gpuData->numBlocks()) {
1801 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001802 }
1803
robertphillipsfd61ed02014-10-28 07:21:44 -07001804 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001805
robertphillipse5524cd2015-02-20 12:30:26 -08001806 SkIRect iBounds;
1807 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1808 return false;
1809 }
1810
1811 SkRect clipBounds = SkRect::Make(iBounds);
1812
1813 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1814
robertphillipsfd61ed02014-10-28 07:21:44 -07001815 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001816 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001817 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001818 &atlasedNeedRendering, &atlasedRecycled,
vbuzinovdded6962015-06-12 08:59:45 -07001819 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001820
1821 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1822
1823 SkTDArray<GrHoistedLayer> needRendering, recycled;
1824
robertphillipse5524cd2015-02-20 12:30:26 -08001825 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1826
robertphillipsfd61ed02014-10-28 07:21:44 -07001827 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001828 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001829 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001830 &needRendering, &recycled,
vbuzinovdded6962015-06-12 08:59:45 -07001831 fRenderTarget->numColorSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001832
1833 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001834
robertphillips64bf7672014-08-21 13:07:35 -07001835 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001836 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1837 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001838
robertphillipsfd61ed02014-10-28 07:21:44 -07001839 GrLayerHoister::UnlockLayers(fContext, needRendering);
1840 GrLayerHoister::UnlockLayers(fContext, recycled);
1841 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1842 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001843
1844 return true;
robertphillips63242d72014-12-04 08:31:02 -08001845#else
1846 return false;
1847#endif
robertphillips64bf7672014-08-21 13:07:35 -07001848}
1849
senorblancobe129b22014-08-08 07:14:35 -07001850SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001851 // We always return a transient cache, so it is freed after each
1852 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001853 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001854}
reedf037e0b2014-10-30 11:34:15 -07001855
1856#endif