blob: 8bd5b5ebd543d0452453db8c2781903e27a5dc6a [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"
kkinnunenabcfab42015-02-22 22:53:44 -080013#include "GrGpu.h"
14#include "GrGpuResourcePriv.h"
robertphillips98d709b2014-09-02 10:20:50 -070015#include "GrLayerHoister.h"
robertphillips274b4ba2014-09-04 07:24:18 -070016#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070017#include "GrStrokeInfo.h"
joshualitt8f94bb22015-04-28 07:04:11 -070018#include "GrTextContext.h"
egdanielbbcb38d2014-06-19 10:19:29 -070019#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080020#include "SkCanvasPriv.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000021#include "SkDrawProcs.h"
kkinnunenabcfab42015-02-22 22:53:44 -080022#include "SkErrorInternals.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000023#include "SkGlyphCache.h"
kkinnunenabcfab42015-02-22 22:53:44 -080024#include "SkGrTexturePixelRef.h"
reeda85d4d02015-05-06 12:56:48 -070025#include "SkImage_Base.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000026#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080027#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000028#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000029#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000030#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070031#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000032#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080033#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000034#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000035#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080036#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000037#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000038#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000039#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070040#include "SkXfermode.h"
kkinnunenabcfab42015-02-22 22:53:44 -080041#include "effects/GrBicubicEffect.h"
42#include "effects/GrDashingEffect.h"
43#include "effects/GrSimpleTextureEffect.h"
44#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000045
reedf037e0b2014-10-30 11:34:15 -070046#if SK_SUPPORT_GPU
47
senorblanco55b6d8b2014-07-30 11:26:46 -070048enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
49
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000050#if 0
51 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080052 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000053 do { \
54 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080055 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000056 } while (0)
57#else
joshualitt5531d512014-12-17 15:50:11 -080058 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000059#endif
60
61// This constant represents the screen alignment criterion in texels for
62// requiring texture domain clamping to prevent color bleeding when drawing
63// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000064#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000065
66#define DO_DEFERRED_CLEAR() \
67 do { \
bsalomonafe30052015-01-16 07:32:33 -080068 if (fNeedClear) { \
69 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000070 } \
71 } while (false) \
72
73///////////////////////////////////////////////////////////////////////////////
74
75#define CHECK_FOR_ANNOTATION(paint) \
76 do { if (paint.getAnnotation()) { return; } } while (0)
77
78///////////////////////////////////////////////////////////////////////////////
79
bsalomonbcf0a522014-10-08 08:40:09 -070080// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
81// just accesses the backing GrTexture. Otherwise, it creates a cached texture
82// representation and releases it in the destructor.
83class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040084public:
bsalomonbcf0a522014-10-08 08:40:09 -070085 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070086
bsalomonbcf0a522014-10-08 08:40:09 -070087 AutoBitmapTexture(GrContext* context,
88 const SkBitmap& bitmap,
89 const GrTextureParams* params,
90 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040091 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070092 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040093 }
94
bsalomonbcf0a522014-10-08 08:40:09 -070095 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040096 const SkBitmap& bitmap,
97 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -070098 // Either get the texture directly from the bitmap, or else use the cache and
99 // remember to unref it.
100 if (GrTexture* bmpTexture = bitmap.getTexture()) {
101 fTexture.reset(NULL);
102 return bmpTexture;
103 } else {
104 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
105 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400106 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400107 }
108
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000109private:
bsalomonbcf0a522014-10-08 08:40:09 -0700110 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000111};
112
113///////////////////////////////////////////////////////////////////////////////
114
115struct GrSkDrawProcs : public SkDrawProcs {
116public:
117 GrContext* fContext;
118 GrTextContext* fTextContext;
119 GrFontScaler* fFontScaler; // cached in the skia glyphcache
120};
121
122///////////////////////////////////////////////////////////////////////////////
123
bsalomonafe30052015-01-16 07:32:33 -0800124SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags) {
senorblancod0d37ca2015-04-02 04:54:56 -0700125 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, flags);
126}
127
128SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
129 const SkSurfaceProps* props, unsigned flags) {
bsalomonafe30052015-01-16 07:32:33 -0800130 if (!rt || rt->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000131 return NULL;
132 }
senorblancod0d37ca2015-04-02 04:54:56 -0700133 return SkNEW_ARGS(SkGpuDevice, (rt, width, height, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000134}
135
bsalomonafe30052015-01-16 07:32:33 -0800136static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps* props) {
137 if (props) {
138 return SkDeviceProperties(props->pixelGeometry());
139 } else {
140 return SkDeviceProperties(SkDeviceProperties::kLegacyLCD_InitType);
141 }
reedb2db8982014-11-13 12:41:02 -0800142}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000143
bsalomonafe30052015-01-16 07:32:33 -0800144static SkSurfaceProps copy_or_default_props(const SkSurfaceProps* props) {
145 if (props) {
146 return SkSurfaceProps(*props);
147 } else {
148 return SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
149 }
150}
151
senorblancod0d37ca2015-04-02 04:54:56 -0700152SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
153 const SkSurfaceProps* props, unsigned flags)
reedb2db8982014-11-13 12:41:02 -0800154 : INHERITED(surfaceprops_to_deviceprops(props))
bsalomonafe30052015-01-16 07:32:33 -0800155 , fSurfaceProps(copy_or_default_props(props))
reedb2db8982014-11-13 12:41:02 -0800156{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000157 fDrawProcs = NULL;
158
bsalomonafe30052015-01-16 07:32:33 -0800159 fContext = SkRef(rt->getContext());
160 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000161
bsalomonafe30052015-01-16 07:32:33 -0800162 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000163
senorblancod0d37ca2015-04-02 04:54:56 -0700164 SkImageInfo info = rt->surfacePriv().info().makeWH(width, height);
bsalomonafe30052015-01-16 07:32:33 -0800165 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt));
bsalomonafbf2d62014-09-30 12:18:44 -0700166 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700167 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700168
bsalomonafe30052015-01-16 07:32:33 -0800169 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts();
robertphillipsccb1b572015-05-27 11:02:55 -0700170 fTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFT);
robertphillipsea461502015-05-26 11:38:03 -0700171 fDrawContext.reset(SkRef(fContext->drawContext()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000172}
173
kkinnunenabcfab42015-02-22 22:53:44 -0800174GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
175 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000176 if (kUnknown_SkColorType == origInfo.colorType() ||
177 origInfo.width() < 0 || origInfo.height() < 0) {
178 return NULL;
179 }
180
bsalomonafe30052015-01-16 07:32:33 -0800181 if (!context) {
182 return NULL;
183 }
184
reede5ea5002014-09-03 11:54:58 -0700185 SkColorType ct = origInfo.colorType();
186 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700187 if (kRGB_565_SkColorType == ct) {
188 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800189 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
190 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700191 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800192 }
193 if (kOpaque_SkAlphaType != at) {
194 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000195 }
reede5ea5002014-09-03 11:54:58 -0700196 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000197
bsalomonf2703d82014-10-28 14:33:06 -0700198 GrSurfaceDesc desc;
199 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000200 desc.fWidth = info.width();
201 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000202 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000203 desc.fSampleCnt = sampleCount;
bsalomond309e7a2015-04-30 14:18:54 -0700204 GrTexture* texture = context->textureProvider()->createTexture(
205 desc, SkToBool(budgeted), NULL, 0);
kkinnunenabcfab42015-02-22 22:53:44 -0800206 if (NULL == texture) {
207 return NULL;
208 }
209 SkASSERT(NULL != texture->asRenderTarget());
210 return texture->asRenderTarget();
211}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000212
kkinnunenabcfab42015-02-22 22:53:44 -0800213SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
214 const SkImageInfo& info, int sampleCount,
215 const SkSurfaceProps* props, unsigned flags) {
216
217 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
218 if (NULL == rt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000219 return NULL;
220 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000221
senorblancod0d37ca2015-04-02 04:54:56 -0700222 return SkNEW_ARGS(SkGpuDevice, (rt, info.width(), info.height(), props, flags));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000223}
224
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000225SkGpuDevice::~SkGpuDevice() {
226 if (fDrawProcs) {
227 delete fDrawProcs;
228 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000229
robertphillipsea461502015-05-26 11:38:03 -0700230 SkDELETE(fTextContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000231
bsalomon32d0b3b2014-08-29 07:50:23 -0700232 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000233 fContext->unref();
234}
235
236///////////////////////////////////////////////////////////////////////////////
237
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000238bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
239 int x, int y) {
240 DO_DEFERRED_CLEAR();
241
242 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000243 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000244 if (kUnknown_GrPixelConfig == config) {
245 return false;
246 }
247
248 uint32_t flags = 0;
249 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
250 flags = GrContext::kUnpremul_PixelOpsFlag;
251 }
252 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
253 config, dstPixels, dstRowBytes, flags);
254}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000255
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000256bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
257 int x, int y) {
258 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000259 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000260 if (kUnknown_GrPixelConfig == config) {
261 return false;
262 }
263 uint32_t flags = 0;
264 if (kUnpremul_SkAlphaType == info.alphaType()) {
265 flags = GrContext::kUnpremul_PixelOpsFlag;
266 }
267 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
268
269 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700270 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000271
272 return true;
273}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000274
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000275const SkBitmap& SkGpuDevice::onAccessBitmap() {
276 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700277 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000278}
279
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000280void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
281 INHERITED::onAttachToCanvas(canvas);
282
283 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800284 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000285}
286
287void SkGpuDevice::onDetachFromCanvas() {
288 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800289 fClip.reset();
joshualitt44701df2015-02-23 14:44:57 -0800290 fClipStack.reset(NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000291}
292
293// call this every draw call, to ensure that the context reflects our state,
294// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800295void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800296 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000297
joshualitt44701df2015-02-23 14:44:57 -0800298 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000299
joshualitt570d2f82015-02-25 13:19:48 -0800300 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000301
302 DO_DEFERRED_CLEAR();
303}
304
305GrRenderTarget* SkGpuDevice::accessRenderTarget() {
robertphillips7156b092015-05-14 08:54:12 -0700306 DO_DEFERRED_CLEAR();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000307 return fRenderTarget;
308}
309
reed8eddfb52014-12-04 07:50:14 -0800310void SkGpuDevice::clearAll() {
311 GrColor color = 0;
312 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
313 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillipsea461502015-05-26 11:38:03 -0700314 fDrawContext->clear(fRenderTarget, &rect, color, true);
bsalomonafe30052015-01-16 07:32:33 -0800315 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800316}
317
kkinnunenabcfab42015-02-22 22:53:44 -0800318void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
319 // Caller must have accessed the render target, because it knows the rt must be replaced.
320 SkASSERT(!fNeedClear);
321
322 SkSurface::Budgeted budgeted =
323 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
324 : SkSurface::kNo_Budgeted;
325
326 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
327 fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->numSamples()));
328
329 if (NULL == newRT) {
330 return;
331 }
332
333 if (shouldRetainContent) {
334 if (fRenderTarget->wasDestroyed()) {
335 return;
336 }
337 this->context()->copySurface(newRT, fRenderTarget);
338 }
339
340 SkASSERT(fRenderTarget != newRT);
341
342 fRenderTarget->unref();
343 fRenderTarget = newRT.detach();
344
345 SkASSERT(fRenderTarget->surfacePriv().info() == fLegacyBitmap.info());
346 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (fRenderTarget->surfacePriv().info(), fRenderTarget));
347 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700348
349 fDrawContext.reset(SkRef(fRenderTarget->getContext()->drawContext()));
kkinnunenabcfab42015-02-22 22:53:44 -0800350}
351
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000352///////////////////////////////////////////////////////////////////////////////
353
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000354void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800355 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700356 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000357
358 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700359 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
360 return;
361 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000362
robertphillipsea461502015-05-26 11:38:03 -0700363 fDrawContext->drawPaint(fRenderTarget, fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000364}
365
366// must be in SkCanvas::PointMode order
367static const GrPrimitiveType gPointMode2PrimtiveType[] = {
368 kPoints_GrPrimitiveType,
369 kLines_GrPrimitiveType,
370 kLineStrip_GrPrimitiveType
371};
372
373void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
374 size_t count, const SkPoint pts[], const SkPaint& paint) {
375 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800376 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000377
378 SkScalar width = paint.getStrokeWidth();
379 if (width < 0) {
380 return;
381 }
382
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000383 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700384 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
385 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700386 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
387 &grPaint)) {
388 return;
389 }
egdaniele61c4112014-06-12 10:24:21 -0700390 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700391 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700392 path.moveTo(pts[0]);
393 path.lineTo(pts[1]);
robertphillipsea461502015-05-26 11:38:03 -0700394 fDrawContext->drawPath(fRenderTarget, fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700395 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000396 }
397
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000398 // we only handle hairlines and paints without path effects or mask filters,
399 // else we let the SkDraw call our drawPath()
400 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
401 draw.drawPoints(mode, count, pts, paint, true);
402 return;
403 }
404
405 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700406 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
407 return;
408 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000409
robertphillipsea461502015-05-26 11:38:03 -0700410 fDrawContext->drawVertices(fRenderTarget,
411 fClip,
412 grPaint,
413 *draw.fMatrix,
414 gPointMode2PrimtiveType[mode],
415 SkToS32(count),
416 (SkPoint*)pts,
417 NULL,
418 NULL,
419 NULL,
420 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000421}
422
423///////////////////////////////////////////////////////////////////////////////
424
425void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
426 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700427 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
428
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000429 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800430 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000431
432 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
433 SkScalar width = paint.getStrokeWidth();
434
435 /*
436 We have special code for hairline strokes, miter-strokes, bevel-stroke
437 and fills. Anything else we just call our path code.
438 */
439 bool usePath = doStroke && width > 0 &&
440 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
441 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
442 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700443
444 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000445 usePath = true;
446 }
egdanield58a0ba2014-06-11 10:30:05 -0700447
joshualitt5531d512014-12-17 15:50:11 -0800448 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000449#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
450 if (doStroke) {
451#endif
452 usePath = true;
453#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
454 } else {
joshualitt5531d512014-12-17 15:50:11 -0800455 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000456 }
457#endif
458 }
459 // until we can both stroke and fill rectangles
460 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
461 usePath = true;
462 }
463
egdanield58a0ba2014-06-11 10:30:05 -0700464 GrStrokeInfo strokeInfo(paint);
465
466 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700467 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700468 usePath = true;
469 }
470
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000471 if (usePath) {
472 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700473 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000474 path.addRect(rect);
475 this->drawPath(draw, path, paint, NULL, true);
476 return;
477 }
478
479 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700480 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
481 return;
482 }
Mike Klein744fb732014-06-23 15:13:26 -0400483
robertphillipsea461502015-05-26 11:38:03 -0700484 fDrawContext->drawRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000485}
486
487///////////////////////////////////////////////////////////////////////////////
488
489void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800490 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700491 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000492 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800493 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000494
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000495 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700496 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
497 return;
498 }
Mike Klein744fb732014-06-23 15:13:26 -0400499
egdanield58a0ba2014-06-11 10:30:05 -0700500 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000501 if (paint.getMaskFilter()) {
502 // try to hit the fast path for drawing filtered round rects
503
504 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800505 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000506 if (devRRect.allCornersCircular()) {
507 SkRect maskRect;
508 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800509 draw.fClip->getBounds(),
510 *draw.fMatrix,
511 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000512 SkIRect finalIRect;
513 maskRect.roundOut(&finalIRect);
514 if (draw.fClip->quickReject(finalIRect)) {
515 // clipped out
516 return;
517 }
joshualitt25d9c152015-02-18 12:29:52 -0800518 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
519 fRenderTarget,
520 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800521 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800522 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700523 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700524 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000525 return;
526 }
527 }
528
529 }
530 }
531
532 }
533
egdanield58a0ba2014-06-11 10:30:05 -0700534 bool usePath = false;
535
536 if (paint.getMaskFilter()) {
537 usePath = true;
538 } else {
539 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700540 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700541 usePath = true;
542 }
543 }
544
545
546 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000547 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700548 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000549 path.addRRect(rect);
550 this->drawPath(draw, path, paint, NULL, true);
551 return;
552 }
Mike Klein744fb732014-06-23 15:13:26 -0400553
robertphillipsea461502015-05-26 11:38:03 -0700554 fDrawContext->drawRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000555}
556
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000557void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800558 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000559 SkStrokeRec stroke(paint);
560 if (stroke.isFillStyle()) {
561
562 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800563 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000564
565 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700566 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
567 &grPaint)) {
568 return;
569 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000570
571 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
robertphillipsea461502015-05-26 11:38:03 -0700572 fDrawContext->drawDRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000573 return;
574 }
575 }
576
577 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700578 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000579 path.addRRect(outer);
580 path.addRRect(inner);
581 path.setFillType(SkPath::kEvenOdd_FillType);
582
583 this->drawPath(draw, path, paint, NULL, true);
584}
585
586
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000587/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000588
589void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
590 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700591 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000592 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800593 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000594
egdanield58a0ba2014-06-11 10:30:05 -0700595 GrStrokeInfo strokeInfo(paint);
596
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000597 bool usePath = false;
598 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700599 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000600 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700601 } else {
602 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700603 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700604 usePath = true;
605 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000606 }
607
608 if (usePath) {
609 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700610 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000611 path.addOval(oval);
612 this->drawPath(draw, path, paint, NULL, true);
613 return;
614 }
615
616 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700617 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
618 return;
619 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000620
robertphillipsea461502015-05-26 11:38:03 -0700621 fDrawContext->drawOval(fRenderTarget, fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000622}
623
624#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000625
626///////////////////////////////////////////////////////////////////////////////
627
robertphillipsccb1b572015-05-27 11:02:55 -0700628static SkBitmap wrap_texture(GrTexture* texture, int width, int height) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000629 SkBitmap result;
senorblancod0d37ca2015-04-02 04:54:56 -0700630 result.setInfo(SkImageInfo::MakeN32Premul(width, height));
reed6c225732014-06-09 19:52:07 -0700631 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000632 return result;
633}
634
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000635void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
636 const SkPaint& paint, const SkMatrix* prePathMatrix,
637 bool pathIsMutable) {
638 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800639 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700640 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000641
robertphillipsccb1b572015-05-27 11:02:55 -0700642 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, fRenderTarget,
643 fClip, origSrcPath, paint,
644 *draw.fMatrix, prePathMatrix,
645 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000646}
647
648static const int kBmpSmallTileSize = 1 << 10;
649
650static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
651 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
652 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
653 return tilesX * tilesY;
654}
655
656static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
657 if (maxTileSize <= kBmpSmallTileSize) {
658 return maxTileSize;
659 }
660
661 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
662 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
663
664 maxTileTotalTileSize *= maxTileSize * maxTileSize;
665 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
666
667 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
668 return kBmpSmallTileSize;
669 } else {
670 return maxTileSize;
671 }
672}
673
674// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
675// pixels from the bitmap are necessary.
676static void determine_clipped_src_rect(const GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800677 const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800678 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800679 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000680 const SkBitmap& bitmap,
681 const SkRect* srcRectPtr,
682 SkIRect* clippedSrcIRect) {
joshualitt570d2f82015-02-25 13:19:48 -0800683 clip.getConservativeBounds(rt, clippedSrcIRect, NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000684 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800685 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000686 clippedSrcIRect->setEmpty();
687 return;
688 }
689 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
690 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700691 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000692 // we've setup src space 0,0 to map to the top left of the src rect.
693 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000694 if (!clippedSrcRect.intersect(*srcRectPtr)) {
695 clippedSrcIRect->setEmpty();
696 return;
697 }
698 }
699 clippedSrcRect.roundOut(clippedSrcIRect);
700 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
701 if (!clippedSrcIRect->intersect(bmpBounds)) {
702 clippedSrcIRect->setEmpty();
703 }
704}
705
706bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800707 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000708 const GrTextureParams& params,
709 const SkRect* srcRectPtr,
710 int maxTileSize,
711 int* tileSize,
712 SkIRect* clippedSrcRect) const {
713 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700714 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000715 return false;
716 }
717
718 // if it's larger than the max tile size, then we have no choice but tiling.
719 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt570d2f82015-02-25 13:19:48 -0800720 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap,
721 srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000722 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
723 return true;
724 }
725
726 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
727 return false;
728 }
729
730 // if the entire texture is already in our cache then no reason to tile it
731 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
732 return false;
733 }
734
735 // At this point we know we could do the draw by uploading the entire bitmap
736 // as a texture. However, if the texture would be large compared to the
737 // cache size and we don't require most of it for this draw then tile to
738 // reduce the amount of upload and cache spill.
739
740 // assumption here is that sw bitmap size is a good proxy for its size as
741 // a texture
742 size_t bmpSize = bitmap.getSize();
743 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000744 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000745 if (bmpSize < cacheSize / 2) {
746 return false;
747 }
748
749 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt570d2f82015-02-25 13:19:48 -0800750 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap, srcRectPtr,
joshualitt25d9c152015-02-18 12:29:52 -0800751 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000752 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
753 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
754 kBmpSmallTileSize * kBmpSmallTileSize;
755
756 return usedTileBytes < 2 * bmpSize;
757}
758
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000759void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000760 const SkBitmap& bitmap,
761 const SkMatrix& m,
762 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000763 SkMatrix concat;
764 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
765 if (!m.isIdentity()) {
766 concat.setConcat(*draw->fMatrix, m);
767 draw.writable()->fMatrix = &concat;
768 }
769 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000770}
771
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000772// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
774// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000775static inline void clamped_outset_with_offset(SkIRect* iRect,
776 int outset,
777 SkPoint* offset,
778 const SkIRect& clamp) {
779 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000780
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000781 int leftClampDelta = clamp.fLeft - iRect->fLeft;
782 if (leftClampDelta > 0) {
783 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000784 iRect->fLeft = clamp.fLeft;
785 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000786 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000787 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000788
789 int topClampDelta = clamp.fTop - iRect->fTop;
790 if (topClampDelta > 0) {
791 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000792 iRect->fTop = clamp.fTop;
793 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000794 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000795 }
796
797 if (iRect->fRight > clamp.fRight) {
798 iRect->fRight = clamp.fRight;
799 }
800 if (iRect->fBottom > clamp.fBottom) {
801 iRect->fBottom = clamp.fBottom;
802 }
803}
804
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000805static bool has_aligned_samples(const SkRect& srcRect,
806 const SkRect& transformedRect) {
807 // detect pixel disalignment
808 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
809 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
810 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
811 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
812 SkScalarAbs(transformedRect.width() - srcRect.width()) <
813 COLOR_BLEED_TOLERANCE &&
814 SkScalarAbs(transformedRect.height() - srcRect.height()) <
815 COLOR_BLEED_TOLERANCE) {
816 return true;
817 }
818 return false;
819}
820
821static bool may_color_bleed(const SkRect& srcRect,
822 const SkRect& transformedRect,
823 const SkMatrix& m) {
824 // Only gets called if has_aligned_samples returned false.
825 // So we can assume that sampling is axis aligned but not texel aligned.
826 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
827 SkRect innerSrcRect(srcRect), innerTransformedRect,
828 outerTransformedRect(transformedRect);
829 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
830 m.mapRect(&innerTransformedRect, innerSrcRect);
831
832 // The gap between outerTransformedRect and innerTransformedRect
833 // represents the projection of the source border area, which is
834 // problematic for color bleeding. We must check whether any
835 // destination pixels sample the border area.
836 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
837 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
838 SkIRect outer, inner;
839 outerTransformedRect.round(&outer);
840 innerTransformedRect.round(&inner);
841 // If the inner and outer rects round to the same result, it means the
842 // border does not overlap any pixel centers. Yay!
843 return inner != outer;
844}
845
846static bool needs_texture_domain(const SkBitmap& bitmap,
847 const SkRect& srcRect,
848 GrTextureParams &params,
849 const SkMatrix& contextMatrix,
850 bool bicubic) {
851 bool needsTextureDomain = false;
senorblancod0d37ca2015-04-02 04:54:56 -0700852 GrTexture* tex = bitmap.getTexture();
853 int width = tex ? tex->width() : bitmap.width();
854 int height = tex ? tex->height() : bitmap.height();
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000855
856 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
857 // Need texture domain if drawing a sub rect
senorblancod0d37ca2015-04-02 04:54:56 -0700858 needsTextureDomain = srcRect.width() < width ||
859 srcRect.height() < height;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000860 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
861 // sampling is axis-aligned
862 SkRect transformedRect;
863 contextMatrix.mapRect(&transformedRect, srcRect);
864
865 if (has_aligned_samples(srcRect, transformedRect)) {
866 params.setFilterMode(GrTextureParams::kNone_FilterMode);
867 needsTextureDomain = false;
868 } else {
869 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
870 }
871 }
872 }
873 return needsTextureDomain;
874}
875
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000876void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
877 const SkBitmap& bitmap,
878 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000879 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000880 const SkPaint& paint,
881 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -0800882 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000883
884 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000885 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000886 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
887 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000888 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000889 SkScalar w = SkIntToScalar(bitmap.width());
890 SkScalar h = SkIntToScalar(bitmap.height());
891 dstSize.fWidth = w;
892 dstSize.fHeight = h;
893 srcRect.set(0, 0, w, h);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000894 } else {
bsalomon49f085d2014-09-05 13:34:00 -0700895 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000896 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000897 dstSize = *dstSizePtr;
senorblancod0d37ca2015-04-02 04:54:56 -0700898 }
899 GrTexture* tex = bitmap.getTexture();
900 int width = tex ? tex->width() : bitmap.width();
901 int height = tex ? tex->height() : bitmap.height();
902 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
903 srcRect.fRight >= width && srcRect.fBottom >= height) {
904 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000905 }
906
derekf367e1862014-12-02 11:02:06 -0800907 // If the render target is not msaa and draw is antialiased, we call
908 // drawRect instead of drawing on the render target directly.
909 // FIXME: the tiled bitmap code path doesn't currently support
910 // anti-aliased edges, we work around that for now by drawing directly
911 // if the image size exceeds maximum texture size.
bsalomon76228632015-05-29 08:02:10 -0700912 int maxTextureSize = fContext->caps()->maxTextureSize();
derekf367e1862014-12-02 11:02:06 -0800913 bool directDraw = fRenderTarget->isMultisampled() ||
914 !paint.isAntiAlias() ||
915 bitmap.width() > maxTextureSize ||
916 bitmap.height() > maxTextureSize;
917
918 // we check whether dst rect are pixel aligned
919 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -0800920 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -0800921
922 if (staysRect) {
923 SkRect rect;
924 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -0800925 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -0800926 const SkScalar *scalars = rect.asScalars();
927 bool isDstPixelAligned = true;
928 for (int i = 0; i < 4; i++) {
929 if (!SkScalarIsInt(scalars[i])) {
930 isDstPixelAligned = false;
931 break;
932 }
933 }
934
935 if (isDstPixelAligned)
936 directDraw = true;
937 }
938 }
939
940 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000941 // Convert the bitmap to a shader so that the rect can be drawn
942 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000943 SkBitmap tmp; // subset of bitmap, if necessary
944 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000945 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -0700946 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000947 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
948 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
949 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000950 // In bleed mode we position and trim the bitmap based on the src rect which is
951 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
952 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
953 // compensate.
954 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
955 SkIRect iSrc;
956 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000957
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000958 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
959 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000960
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000961 if (!bitmap.extractSubset(&tmp, iSrc)) {
962 return; // extraction failed
963 }
964 bitmapPtr = &tmp;
965 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000966
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000967 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000968 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000969 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000970 } else {
971 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000972 }
973
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000974 SkPaint paintWithShader(paint);
975 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000976 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000977 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
978 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000979
980 return;
981 }
982
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000983 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
984 // the view matrix rather than a local matrix.
985 SkMatrix m;
986 m.setScale(dstSize.fWidth / srcRect.width(),
987 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -0800988 SkMatrix viewM = *draw.fMatrix;
989 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000990
991 GrTextureParams params;
reed93a12152015-03-16 10:08:34 -0700992 SkFilterQuality paintFilterQuality = paint.getFilterQuality();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000993 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000994
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000995 bool doBicubic = false;
996
reed93a12152015-03-16 10:08:34 -0700997 switch(paintFilterQuality) {
998 case kNone_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000999 textureFilterMode = GrTextureParams::kNone_FilterMode;
1000 break;
reed93a12152015-03-16 10:08:34 -07001001 case kLow_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001002 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1003 break;
reed93a12152015-03-16 10:08:34 -07001004 case kMedium_SkFilterQuality:
joshualitt5531d512014-12-17 15:50:11 -08001005 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001006 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1007 } else {
1008 // Don't trigger MIP level generation unnecessarily.
1009 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1010 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001011 break;
reed93a12152015-03-16 10:08:34 -07001012 case kHigh_SkFilterQuality:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001013 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001014 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001015 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001016 break;
1017 default:
1018 SkErrorInternals::SetError( kInvalidPaint_SkError,
1019 "Sorry, I don't understand the filtering "
1020 "mode you asked for. Falling back to "
1021 "MIPMaps.");
1022 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1023 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001024 }
1025
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001026 int tileFilterPad;
1027 if (doBicubic) {
1028 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1029 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1030 tileFilterPad = 0;
1031 } else {
1032 tileFilterPad = 1;
1033 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001034 params.setFilterMode(textureFilterMode);
1035
bsalomon76228632015-05-29 08:02:10 -07001036 int maxTileSize = fContext->caps()->maxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001037 int tileSize;
1038
1039 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001040 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001041 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001042 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1043 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001044 } else {
1045 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001046 bool needsTextureDomain = needs_texture_domain(bitmap,
1047 srcRect,
1048 params,
joshualitt5531d512014-12-17 15:50:11 -08001049 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001050 doBicubic);
1051 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001052 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001053 srcRect,
1054 params,
1055 paint,
1056 flags,
1057 doBicubic,
1058 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001059 }
1060}
1061
1062// Break 'bitmap' into several tiles to draw it since it has already
1063// been determined to be too large to fit in VRAM
1064void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001065 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001066 const SkRect& srcRect,
1067 const SkIRect& clippedSrcIRect,
1068 const GrTextureParams& params,
1069 const SkPaint& paint,
1070 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001071 int tileSize,
1072 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001073 // The following pixel lock is technically redundant, but it is desirable
1074 // to lock outside of the tile loop to prevent redecoding the whole image
1075 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1076 // is larger than the limit of the discardable memory pool.
1077 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001078 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1079
1080 int nx = bitmap.width() / tileSize;
1081 int ny = bitmap.height() / tileSize;
1082 for (int x = 0; x <= nx; x++) {
1083 for (int y = 0; y <= ny; y++) {
1084 SkRect tileR;
1085 tileR.set(SkIntToScalar(x * tileSize),
1086 SkIntToScalar(y * tileSize),
1087 SkIntToScalar((x + 1) * tileSize),
1088 SkIntToScalar((y + 1) * tileSize));
1089
1090 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1091 continue;
1092 }
1093
1094 if (!tileR.intersect(srcRect)) {
1095 continue;
1096 }
1097
1098 SkBitmap tmpB;
1099 SkIRect iTileR;
1100 tileR.roundOut(&iTileR);
1101 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1102 SkIntToScalar(iTileR.fTop));
1103
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001104 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001105 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001106 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001107 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001108 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001109
robertphillipsec8bb942014-11-21 10:16:25 -08001110 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001111 SkIRect iClampRect;
1112
1113 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1114 // In bleed mode we want to always expand the tile on all edges
1115 // but stay within the bitmap bounds
1116 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1117 } else {
1118 // In texture-domain/clamp mode we only want to expand the
1119 // tile on edges interior to "srcRect" (i.e., we want to
1120 // not bleed across the original clamped edges)
1121 srcRect.roundOut(&iClampRect);
1122 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001123 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1124 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001125 }
1126
1127 if (bitmap.extractSubset(&tmpB, iTileR)) {
1128 // now offset it to make it "local" to our tmp bitmap
1129 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001130 GrTextureParams paramsTemp = params;
1131 bool needsTextureDomain = needs_texture_domain(bitmap,
1132 srcRect,
1133 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001134 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001135 bicubic);
1136 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001137 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001138 tileR,
1139 paramsTemp,
1140 paint,
1141 flags,
1142 bicubic,
1143 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001144 }
1145 }
1146 }
1147}
1148
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001149
1150/*
1151 * This is called by drawBitmap(), which has to handle images that may be too
1152 * large to be represented by a single texture.
1153 *
1154 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1155 * and that non-texture portion of the GrPaint has already been setup.
1156 */
1157void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001158 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001159 const SkRect& srcRect,
1160 const GrTextureParams& params,
1161 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001162 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001163 bool bicubic,
1164 bool needsTextureDomain) {
bsalomon76228632015-05-29 08:02:10 -07001165 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1166 bitmap.height() <= fContext->caps()->maxTextureSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167
1168 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001169 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001170 if (NULL == texture) {
1171 return;
1172 }
1173
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001174 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001175 SkRect paintRect;
1176 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1177 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1178 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1179 SkScalarMul(srcRect.fTop, hInv),
1180 SkScalarMul(srcRect.fRight, wInv),
1181 SkScalarMul(srcRect.fBottom, hInv));
1182
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001183 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001184 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001185 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001186 // Use a constrained texture domain to avoid color bleeding
1187 SkScalar left, top, right, bottom;
1188 if (srcRect.width() > SK_Scalar1) {
1189 SkScalar border = SK_ScalarHalf / texture->width();
1190 left = paintRect.left() + border;
1191 right = paintRect.right() - border;
1192 } else {
1193 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1194 }
1195 if (srcRect.height() > SK_Scalar1) {
1196 SkScalar border = SK_ScalarHalf / texture->height();
1197 top = paintRect.top() + border;
1198 bottom = paintRect.bottom() - border;
1199 } else {
1200 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1201 }
1202 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001203 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001204 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001205 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001206 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001207 SkMatrix::I(),
1208 textureDomain,
1209 GrTextureDomain::kClamp_Mode,
1210 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001211 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001212 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001213 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1214 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001215 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001216 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001217 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001218 }
1219
1220 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1221 // the rest from the SkPaint.
1222 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001223 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001224 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001225 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1226 SkColor2GrColor(paint.getColor());
bsalomonbed83a62015-04-15 14:18:34 -07001227 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false,
1228 &grPaint)) {
1229 return;
1230 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001231
robertphillipsea461502015-05-26 11:38:03 -07001232 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dstRect,
1233 paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001234}
1235
fmalita2d97bc12014-11-20 10:44:58 -08001236bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001237 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001238 const SkImageFilter* filter,
1239 const SkImageFilter::Context& ctx,
1240 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001241 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001242
1243 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1244 // (https://code.google.com/p/skia/issues/detail?id=3148).
reed2c55d7b2015-06-09 08:18:39 -07001245 SkImageFilter::Proxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001246
1247 if (filter->canFilterImageGPU()) {
senorblancod0d37ca2015-04-02 04:54:56 -07001248 return filter->filterImageGPU(&proxy, wrap_texture(texture, width, height),
1249 ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001250 } else {
1251 return false;
1252 }
1253}
1254
1255void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1256 int left, int top, const SkPaint& paint) {
1257 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001258 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001259
1260 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1261 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1262 return;
1263 }
1264
1265 int w = bitmap.width();
1266 int h = bitmap.height();
1267
1268 GrTexture* texture;
1269 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001270 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001271 if (!texture) {
1272 return;
1273 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001274
1275 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001276 // This bitmap will own the filtered result as a texture.
1277 SkBitmap filteredBitmap;
1278
bsalomon49f085d2014-09-05 13:34:00 -07001279 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001280 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001281 SkMatrix matrix(*draw.fMatrix);
1282 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001283 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001284 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001285 // This cache is transient, and is freed (along with all its contained
1286 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001287 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001288 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001289 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001290 texture = (GrTexture*) filteredBitmap.getTexture();
1291 w = filteredBitmap.width();
1292 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001293 left += offset.x();
1294 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001295 } else {
1296 return;
1297 }
1298 }
1299
1300 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001301 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001302
bsalomonbed83a62015-04-15 14:18:34 -07001303 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1304 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1305 return;
1306 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001307
robertphillipsea461502015-05-26 11:38:03 -07001308 fDrawContext->drawNonAARectToRect(fRenderTarget,
1309 fClip,
1310 grPaint,
1311 SkMatrix::I(),
1312 SkRect::MakeXYWH(SkIntToScalar(left),
1313 SkIntToScalar(top),
1314 SkIntToScalar(w),
1315 SkIntToScalar(h)),
1316 SkRect::MakeXYWH(0,
1317 0,
1318 SK_Scalar1 * w / texture->width(),
1319 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001320}
1321
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001322void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001323 const SkRect* src, const SkRect& dst,
1324 const SkPaint& paint,
1325 SkCanvas::DrawBitmapRectFlags flags) {
1326 SkMatrix matrix;
1327 SkRect bitmapBounds, tmpSrc;
1328
1329 bitmapBounds.set(0, 0,
1330 SkIntToScalar(bitmap.width()),
1331 SkIntToScalar(bitmap.height()));
1332
1333 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001334 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001335 tmpSrc = *src;
1336 } else {
1337 tmpSrc = bitmapBounds;
1338 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001339
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001340 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1341
1342 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001343 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001344 if (!bitmapBounds.contains(tmpSrc)) {
1345 if (!tmpSrc.intersect(bitmapBounds)) {
1346 return; // nothing to draw
1347 }
1348 }
1349 }
1350
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001351 SkRect tmpDst;
1352 matrix.mapRect(&tmpDst, tmpSrc);
1353
1354 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1355 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1356 // Translate so that tempDst's top left is at the origin.
1357 matrix = *origDraw.fMatrix;
1358 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1359 draw.writable()->fMatrix = &matrix;
1360 }
1361 SkSize dstSize;
1362 dstSize.fWidth = tmpDst.width();
1363 dstSize.fHeight = tmpDst.height();
1364
1365 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001366}
1367
1368void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1369 int x, int y, const SkPaint& paint) {
1370 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001371 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001372 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001373
1374 // TODO: If the source device covers the whole of this device, we could
1375 // omit fNeedsClear -related flushing.
1376 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001377
1378 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001379 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001380
1381 GrRenderTarget* devRT = dev->accessRenderTarget();
1382 GrTexture* devTex;
1383 if (NULL == (devTex = devRT->asTexture())) {
1384 return;
1385 }
1386
robertphillips7b9e8a42014-12-11 08:20:31 -08001387 const SkImageInfo ii = dev->imageInfo();
1388 int w = ii.width();
1389 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001390
1391 SkImageFilter* filter = paint.getImageFilter();
1392 // This bitmap will own the filtered result as a texture.
1393 SkBitmap filteredBitmap;
1394
bsalomon49f085d2014-09-05 13:34:00 -07001395 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001396 SkIPoint offset = SkIPoint::Make(0, 0);
1397 SkMatrix matrix(*draw.fMatrix);
1398 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001399 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001400 // This cache is transient, and is freed (along with all its contained
1401 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001402 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001403 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001404 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1405 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001406 devTex = filteredBitmap.getTexture();
1407 w = filteredBitmap.width();
1408 h = filteredBitmap.height();
1409 x += offset.fX;
1410 y += offset.fY;
1411 } else {
1412 return;
1413 }
1414 }
1415
1416 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001417 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001418
bsalomonbed83a62015-04-15 14:18:34 -07001419 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1420 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1421 return;
1422 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001423
1424 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1425 SkIntToScalar(y),
1426 SkIntToScalar(w),
1427 SkIntToScalar(h));
1428
1429 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1430 // scratch texture).
1431 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1432 SK_Scalar1 * h / devTex->height());
1433
robertphillipsea461502015-05-26 11:38:03 -07001434 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, SkMatrix::I(), dstRect,
1435 srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001436}
1437
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001438bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001439 return filter->canFilterImageGPU();
1440}
1441
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001442bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001443 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001444 SkBitmap* result, SkIPoint* offset) {
1445 // want explicitly our impl, so guard against a subclass of us overriding it
1446 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1447 return false;
1448 }
1449
1450 SkAutoLockPixels alp(src, !src.getTexture());
1451 if (!src.getTexture() && !src.readyToDraw()) {
1452 return false;
1453 }
1454
1455 GrTexture* texture;
1456 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1457 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001458 AutoBitmapTexture abt(fContext, src, NULL, &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001459 if (!texture) {
1460 return false;
1461 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001462
senorblancod0d37ca2015-04-02 04:54:56 -07001463 return this->filterTexture(fContext, texture, src.width(), src.height(),
1464 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001465}
1466
reeda85d4d02015-05-06 12:56:48 -07001467static bool wrap_as_bm(const SkImage* image, SkBitmap* bm) {
1468 GrTexture* tex = image->getTexture();
1469 if (tex) {
reed8b26b992015-05-07 15:36:17 -07001470 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), bm);
1471 return true;
reeda85d4d02015-05-06 12:56:48 -07001472 } else {
reed8b26b992015-05-07 15:36:17 -07001473 return as_IB(image)->getROPixels(bm);
reeda85d4d02015-05-06 12:56:48 -07001474 }
reeda85d4d02015-05-06 12:56:48 -07001475}
1476
1477void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1478 const SkPaint& paint) {
1479 SkBitmap bm;
1480 if (wrap_as_bm(image, &bm)) {
1481 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1482 }
1483}
1484
1485void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
1486 const SkRect& dst, const SkPaint& paint) {
1487 SkBitmap bm;
1488 if (wrap_as_bm(image, &bm)) {
1489 this->drawBitmapRect(draw, bm, src, dst, paint, SkCanvas::kNone_DrawBitmapRectFlag);
1490 }
1491}
1492
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001493///////////////////////////////////////////////////////////////////////////////
1494
1495// must be in SkCanvas::VertexMode order
1496static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1497 kTriangles_GrPrimitiveType,
1498 kTriangleStrip_GrPrimitiveType,
1499 kTriangleFan_GrPrimitiveType,
1500};
1501
1502void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1503 int vertexCount, const SkPoint vertices[],
1504 const SkPoint texs[], const SkColor colors[],
1505 SkXfermode* xmode,
1506 const uint16_t indices[], int indexCount,
1507 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001508 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001509 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001510
dandov32a311b2014-07-15 19:46:26 -07001511 const uint16_t* outIndices;
1512 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1513 GrPrimitiveType primType;
1514 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001515
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001516 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1517 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001518
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001519 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001520
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001521 SkPaint copy(paint);
1522 copy.setStyle(SkPaint::kStroke_Style);
1523 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001524
dandov32a311b2014-07-15 19:46:26 -07001525 // we ignore the shader if texs is null.
bsalomonbed83a62015-04-15 14:18:34 -07001526 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy,
1527 SkColor2GrColor(copy.getColor()), NULL == colors, &grPaint)) {
1528 return;
1529 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001530
dandov32a311b2014-07-15 19:46:26 -07001531 primType = kLines_GrPrimitiveType;
1532 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001533 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001534 switch (vmode) {
1535 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001536 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001537 break;
1538 case SkCanvas::kTriangleStrip_VertexMode:
1539 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001540 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001541 break;
1542 }
mtklein533eb782014-08-27 10:39:42 -07001543
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001544 VertState state(vertexCount, indices, indexCount);
1545 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001546
dandov32a311b2014-07-15 19:46:26 -07001547 //number of indices for lines per triangle with kLines
1548 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001549
dandov32a311b2014-07-15 19:46:26 -07001550 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1551 outIndices = outAlloc.get();
1552 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001553 int i = 0;
1554 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001555 auxIndices[i] = state.f0;
1556 auxIndices[i + 1] = state.f1;
1557 auxIndices[i + 2] = state.f1;
1558 auxIndices[i + 3] = state.f2;
1559 auxIndices[i + 4] = state.f2;
1560 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001561 i += 6;
1562 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001563 } else {
dandov32a311b2014-07-15 19:46:26 -07001564 outIndices = indices;
1565 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001566
dandov32a311b2014-07-15 19:46:26 -07001567 if (NULL == texs || NULL == paint.getShader()) {
bsalomonbed83a62015-04-15 14:18:34 -07001568 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1569 SkColor2GrColor(paint.getColor()),
1570 NULL == colors, &grPaint)) {
1571 return;
1572 }
dandov32a311b2014-07-15 19:46:26 -07001573 } else {
bsalomonbed83a62015-04-15 14:18:34 -07001574 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix,
1575 NULL == colors, &grPaint)) {
1576 return;
1577 }
dandov32a311b2014-07-15 19:46:26 -07001578 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001579 }
1580
mtklein2583b622014-06-04 08:20:41 -07001581#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001582 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001583 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1584 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001585 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001586 }
1587 }
mtklein2583b622014-06-04 08:20:41 -07001588#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001589
1590 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001591 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001592 // need to convert byte order and from non-PM to PM
1593 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001594 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001595 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001596 color = colors[i];
1597 if (paint.getAlpha() != 255) {
1598 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1599 }
1600 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001601 }
1602 colors = convertedColors.get();
1603 }
robertphillipsea461502015-05-26 11:38:03 -07001604 fDrawContext->drawVertices(fRenderTarget,
1605 fClip,
1606 grPaint,
1607 *draw.fMatrix,
1608 primType,
1609 vertexCount,
1610 vertices,
1611 texs,
1612 colors,
1613 outIndices,
1614 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001615}
1616
1617///////////////////////////////////////////////////////////////////////////////
1618
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001619void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001620 size_t byteLength, SkScalar x, SkScalar y,
1621 const SkPaint& paint) {
1622 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001623 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001624
jvanverth8c27a182014-10-14 08:45:50 -07001625 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001626 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1627 return;
1628 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001629
jvanverth8c27a182014-10-14 08:45:50 -07001630 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001631
joshualitt6e8cd962015-03-20 10:30:14 -07001632 fTextContext->drawText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
1633 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001634}
1635
fmalita05c4a432014-09-29 06:29:53 -07001636void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1637 const SkScalar pos[], int scalarsPerPos,
1638 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001639 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001640 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001641
jvanverth8c27a182014-10-14 08:45:50 -07001642 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001643 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1644 return;
1645 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001646
jvanverth8c27a182014-10-14 08:45:50 -07001647 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001648
joshualitt6e8cd962015-03-20 10:30:14 -07001649 fTextContext->drawPosText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
1650 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1651 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001652}
1653
joshualitt9c328182015-03-23 08:13:04 -07001654void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1655 const SkPaint& paint, SkDrawFilter* drawFilter) {
1656 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawTextBlob", fContext);
1657 CHECK_SHOULD_DRAW(draw);
1658
1659 SkDEBUGCODE(this->validate();)
1660
robertphillips9c240a12015-05-28 07:45:59 -07001661 fTextContext->drawTextBlob(fRenderTarget, fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001662 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001663}
1664
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001665///////////////////////////////////////////////////////////////////////////////
1666
reedb2db8982014-11-13 12:41:02 -08001667bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
robertphillips9c240a12015-05-28 07:45:59 -07001668 return GrTextContext::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001669}
1670
1671void SkGpuDevice::flush() {
1672 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001673 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001674}
1675
1676///////////////////////////////////////////////////////////////////////////////
1677
reed76033be2015-03-14 10:54:31 -07001678SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
bsalomonf2703d82014-10-28 14:33:06 -07001679 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001680 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001681 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001682 desc.fWidth = cinfo.fInfo.width();
1683 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001684 desc.fSampleCnt = fRenderTarget->numSamples();
1685
1686 SkAutoTUnref<GrTexture> texture;
1687 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001688 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001689
hcm4396fa52014-10-27 21:43:30 -07001690 // layers are never draw in repeat modes, so we can request an approx
1691 // match and ignore any padding.
bsalomond309e7a2015-04-30 14:18:54 -07001692 const GrTextureProvider::ScratchTexMatch match = (kNever_TileUsage == cinfo.fTileUsage) ?
1693 GrTextureProvider::kApprox_ScratchTexMatch :
1694 GrTextureProvider::kExact_ScratchTexMatch;
1695 texture.reset(fContext->textureProvider()->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001696
1697 if (texture) {
1698 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001699 return SkGpuDevice::Create(
1700 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001701 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001702 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001703 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001704 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001705 return NULL;
1706 }
1707}
1708
reed4a8126e2014-09-22 07:29:03 -07001709SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001710 // TODO: Change the signature of newSurface to take a budgeted parameter.
1711 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
1712 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->numSamples(),
1713 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001714}
1715
robertphillips30d2cc62014-09-24 08:52:18 -07001716bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001717 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001718#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001719 // todo: should handle this natively
1720 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001721 return false;
1722 }
1723
mtklein9db912c2015-05-19 11:11:26 -07001724 const SkBigPicture::AccelData* data = NULL;
1725 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1726 data = bp->accelData();
1727 }
robertphillips81f71b62014-11-11 04:54:49 -08001728 if (!data) {
1729 return false;
1730 }
1731
robertphillipse5524cd2015-02-20 12:30:26 -08001732 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1733 if (0 == gpuData->numBlocks()) {
1734 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001735 }
1736
robertphillipsfd61ed02014-10-28 07:21:44 -07001737 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001738
robertphillipse5524cd2015-02-20 12:30:26 -08001739 SkIRect iBounds;
1740 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1741 return false;
1742 }
1743
1744 SkRect clipBounds = SkRect::Make(iBounds);
1745
1746 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1747
robertphillipsfd61ed02014-10-28 07:21:44 -07001748 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001749 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001750 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001751 &atlasedNeedRendering, &atlasedRecycled,
1752 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001753
1754 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1755
1756 SkTDArray<GrHoistedLayer> needRendering, recycled;
1757
robertphillipse5524cd2015-02-20 12:30:26 -08001758 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1759
robertphillipsfd61ed02014-10-28 07:21:44 -07001760 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001761 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001762 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001763 &needRendering, &recycled,
1764 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001765
1766 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001767
robertphillips64bf7672014-08-21 13:07:35 -07001768 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001769 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1770 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001771
robertphillipsfd61ed02014-10-28 07:21:44 -07001772 GrLayerHoister::UnlockLayers(fContext, needRendering);
1773 GrLayerHoister::UnlockLayers(fContext, recycled);
1774 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1775 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001776
1777 return true;
robertphillips63242d72014-12-04 08:31:02 -08001778#else
1779 return false;
1780#endif
robertphillips64bf7672014-08-21 13:07:35 -07001781}
1782
senorblancobe129b22014-08-08 07:14:35 -07001783SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001784 // We always return a transient cache, so it is freed after each
1785 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001786 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001787}
reedf037e0b2014-10-30 11:34:15 -07001788
1789#endif