blob: 6db5c1db0d22bb64cedfabbb740dbb0a8f7b7276 [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
reed41e010c2015-06-09 12:16:53 -0700280bool SkGpuDevice::onAccessPixels(SkPixmap* pmap) {
281 DO_DEFERRED_CLEAR();
282 // For compatibility with clients the know we're backed w/ a bitmap, and want to inspect its
283 // genID. When we can hide/remove that fact, we can eliminate this call to notify.
284 // ... ugh.
285 fLegacyBitmap.notifyPixelsChanged();
286 return false;
287}
288
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000289void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
290 INHERITED::onAttachToCanvas(canvas);
291
292 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800293 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000294}
295
296void SkGpuDevice::onDetachFromCanvas() {
297 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800298 fClip.reset();
joshualitt44701df2015-02-23 14:44:57 -0800299 fClipStack.reset(NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000300}
301
302// call this every draw call, to ensure that the context reflects our state,
303// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800304void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800305 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000306
joshualitt44701df2015-02-23 14:44:57 -0800307 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000308
joshualitt570d2f82015-02-25 13:19:48 -0800309 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000310
311 DO_DEFERRED_CLEAR();
312}
313
314GrRenderTarget* SkGpuDevice::accessRenderTarget() {
robertphillips7156b092015-05-14 08:54:12 -0700315 DO_DEFERRED_CLEAR();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000316 return fRenderTarget;
317}
318
reed8eddfb52014-12-04 07:50:14 -0800319void SkGpuDevice::clearAll() {
320 GrColor color = 0;
321 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
322 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillipsea461502015-05-26 11:38:03 -0700323 fDrawContext->clear(fRenderTarget, &rect, color, true);
bsalomonafe30052015-01-16 07:32:33 -0800324 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800325}
326
kkinnunenabcfab42015-02-22 22:53:44 -0800327void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
328 // Caller must have accessed the render target, because it knows the rt must be replaced.
329 SkASSERT(!fNeedClear);
330
331 SkSurface::Budgeted budgeted =
332 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
333 : SkSurface::kNo_Budgeted;
334
335 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
336 fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->numSamples()));
337
338 if (NULL == newRT) {
339 return;
340 }
341
342 if (shouldRetainContent) {
343 if (fRenderTarget->wasDestroyed()) {
344 return;
345 }
346 this->context()->copySurface(newRT, fRenderTarget);
347 }
348
349 SkASSERT(fRenderTarget != newRT);
350
351 fRenderTarget->unref();
352 fRenderTarget = newRT.detach();
353
354 SkASSERT(fRenderTarget->surfacePriv().info() == fLegacyBitmap.info());
355 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (fRenderTarget->surfacePriv().info(), fRenderTarget));
356 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700357
358 fDrawContext.reset(SkRef(fRenderTarget->getContext()->drawContext()));
kkinnunenabcfab42015-02-22 22:53:44 -0800359}
360
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000361///////////////////////////////////////////////////////////////////////////////
362
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000363void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800364 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700365 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000366
367 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700368 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
369 return;
370 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000371
robertphillipsea461502015-05-26 11:38:03 -0700372 fDrawContext->drawPaint(fRenderTarget, fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000373}
374
375// must be in SkCanvas::PointMode order
376static const GrPrimitiveType gPointMode2PrimtiveType[] = {
377 kPoints_GrPrimitiveType,
378 kLines_GrPrimitiveType,
379 kLineStrip_GrPrimitiveType
380};
381
382void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
383 size_t count, const SkPoint pts[], const SkPaint& paint) {
384 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800385 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000386
387 SkScalar width = paint.getStrokeWidth();
388 if (width < 0) {
389 return;
390 }
391
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000392 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700393 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
394 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700395 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
396 &grPaint)) {
397 return;
398 }
egdaniele61c4112014-06-12 10:24:21 -0700399 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700400 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700401 path.moveTo(pts[0]);
402 path.lineTo(pts[1]);
robertphillipsea461502015-05-26 11:38:03 -0700403 fDrawContext->drawPath(fRenderTarget, fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700404 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000405 }
406
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000407 // we only handle hairlines and paints without path effects or mask filters,
408 // else we let the SkDraw call our drawPath()
409 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
410 draw.drawPoints(mode, count, pts, paint, true);
411 return;
412 }
413
414 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700415 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
416 return;
417 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000418
robertphillipsea461502015-05-26 11:38:03 -0700419 fDrawContext->drawVertices(fRenderTarget,
420 fClip,
421 grPaint,
422 *draw.fMatrix,
423 gPointMode2PrimtiveType[mode],
424 SkToS32(count),
425 (SkPoint*)pts,
426 NULL,
427 NULL,
428 NULL,
429 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000430}
431
432///////////////////////////////////////////////////////////////////////////////
433
434void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
435 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700436 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
437
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000438 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800439 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000440
441 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
442 SkScalar width = paint.getStrokeWidth();
443
444 /*
445 We have special code for hairline strokes, miter-strokes, bevel-stroke
446 and fills. Anything else we just call our path code.
447 */
448 bool usePath = doStroke && width > 0 &&
449 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
450 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
451 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700452
453 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000454 usePath = true;
455 }
egdanield58a0ba2014-06-11 10:30:05 -0700456
joshualitt5531d512014-12-17 15:50:11 -0800457 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000458#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
459 if (doStroke) {
460#endif
461 usePath = true;
462#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
463 } else {
joshualitt5531d512014-12-17 15:50:11 -0800464 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000465 }
466#endif
467 }
468 // until we can both stroke and fill rectangles
469 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
470 usePath = true;
471 }
472
egdanield58a0ba2014-06-11 10:30:05 -0700473 GrStrokeInfo strokeInfo(paint);
474
475 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700476 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700477 usePath = true;
478 }
479
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000480 if (usePath) {
481 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700482 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000483 path.addRect(rect);
484 this->drawPath(draw, path, paint, NULL, true);
485 return;
486 }
487
488 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700489 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
490 return;
491 }
Mike Klein744fb732014-06-23 15:13:26 -0400492
robertphillipsea461502015-05-26 11:38:03 -0700493 fDrawContext->drawRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000494}
495
496///////////////////////////////////////////////////////////////////////////////
497
498void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800499 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700500 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000501 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800502 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000503
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000504 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700505 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
506 return;
507 }
Mike Klein744fb732014-06-23 15:13:26 -0400508
egdanield58a0ba2014-06-11 10:30:05 -0700509 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000510 if (paint.getMaskFilter()) {
511 // try to hit the fast path for drawing filtered round rects
512
513 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800514 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000515 if (devRRect.allCornersCircular()) {
516 SkRect maskRect;
517 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800518 draw.fClip->getBounds(),
519 *draw.fMatrix,
520 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000521 SkIRect finalIRect;
522 maskRect.roundOut(&finalIRect);
523 if (draw.fClip->quickReject(finalIRect)) {
524 // clipped out
525 return;
526 }
joshualitt25d9c152015-02-18 12:29:52 -0800527 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
528 fRenderTarget,
529 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800530 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800531 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700532 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700533 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000534 return;
535 }
536 }
537
538 }
539 }
540
541 }
542
egdanield58a0ba2014-06-11 10:30:05 -0700543 bool usePath = false;
544
545 if (paint.getMaskFilter()) {
546 usePath = true;
547 } else {
548 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700549 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700550 usePath = true;
551 }
552 }
553
554
555 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000556 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700557 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000558 path.addRRect(rect);
559 this->drawPath(draw, path, paint, NULL, true);
560 return;
561 }
Mike Klein744fb732014-06-23 15:13:26 -0400562
robertphillipsea461502015-05-26 11:38:03 -0700563 fDrawContext->drawRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000564}
565
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000566void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800567 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000568 SkStrokeRec stroke(paint);
569 if (stroke.isFillStyle()) {
570
571 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800572 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000573
574 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700575 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
576 &grPaint)) {
577 return;
578 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000579
580 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
robertphillipsea461502015-05-26 11:38:03 -0700581 fDrawContext->drawDRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000582 return;
583 }
584 }
585
586 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700587 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000588 path.addRRect(outer);
589 path.addRRect(inner);
590 path.setFillType(SkPath::kEvenOdd_FillType);
591
592 this->drawPath(draw, path, paint, NULL, true);
593}
594
595
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000596/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000597
598void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
599 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700600 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000601 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800602 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000603
egdanield58a0ba2014-06-11 10:30:05 -0700604 GrStrokeInfo strokeInfo(paint);
605
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000606 bool usePath = false;
607 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700608 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000609 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700610 } else {
611 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700612 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700613 usePath = true;
614 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000615 }
616
617 if (usePath) {
618 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700619 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000620 path.addOval(oval);
621 this->drawPath(draw, path, paint, NULL, true);
622 return;
623 }
624
625 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700626 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
627 return;
628 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000629
robertphillipsea461502015-05-26 11:38:03 -0700630 fDrawContext->drawOval(fRenderTarget, fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000631}
632
633#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000634
635///////////////////////////////////////////////////////////////////////////////
636
robertphillipsccb1b572015-05-27 11:02:55 -0700637static SkBitmap wrap_texture(GrTexture* texture, int width, int height) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000638 SkBitmap result;
senorblancod0d37ca2015-04-02 04:54:56 -0700639 result.setInfo(SkImageInfo::MakeN32Premul(width, height));
reed6c225732014-06-09 19:52:07 -0700640 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000641 return result;
642}
643
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000644void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
645 const SkPaint& paint, const SkMatrix* prePathMatrix,
646 bool pathIsMutable) {
647 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800648 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700649 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000650
robertphillipsccb1b572015-05-27 11:02:55 -0700651 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, fRenderTarget,
652 fClip, origSrcPath, paint,
653 *draw.fMatrix, prePathMatrix,
654 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000655}
656
657static const int kBmpSmallTileSize = 1 << 10;
658
659static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
660 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
661 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
662 return tilesX * tilesY;
663}
664
665static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
666 if (maxTileSize <= kBmpSmallTileSize) {
667 return maxTileSize;
668 }
669
670 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
671 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
672
673 maxTileTotalTileSize *= maxTileSize * maxTileSize;
674 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
675
676 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
677 return kBmpSmallTileSize;
678 } else {
679 return maxTileSize;
680 }
681}
682
683// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
684// pixels from the bitmap are necessary.
685static void determine_clipped_src_rect(const GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800686 const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800687 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800688 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000689 const SkBitmap& bitmap,
690 const SkRect* srcRectPtr,
691 SkIRect* clippedSrcIRect) {
joshualitt570d2f82015-02-25 13:19:48 -0800692 clip.getConservativeBounds(rt, clippedSrcIRect, NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000693 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800694 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000695 clippedSrcIRect->setEmpty();
696 return;
697 }
698 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
699 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700700 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000701 // we've setup src space 0,0 to map to the top left of the src rect.
702 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000703 if (!clippedSrcRect.intersect(*srcRectPtr)) {
704 clippedSrcIRect->setEmpty();
705 return;
706 }
707 }
708 clippedSrcRect.roundOut(clippedSrcIRect);
709 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
710 if (!clippedSrcIRect->intersect(bmpBounds)) {
711 clippedSrcIRect->setEmpty();
712 }
713}
714
715bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800716 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000717 const GrTextureParams& params,
718 const SkRect* srcRectPtr,
719 int maxTileSize,
720 int* tileSize,
721 SkIRect* clippedSrcRect) const {
722 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700723 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000724 return false;
725 }
726
727 // if it's larger than the max tile size, then we have no choice but tiling.
728 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt570d2f82015-02-25 13:19:48 -0800729 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap,
730 srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000731 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
732 return true;
733 }
734
735 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
736 return false;
737 }
738
739 // if the entire texture is already in our cache then no reason to tile it
740 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
741 return false;
742 }
743
744 // At this point we know we could do the draw by uploading the entire bitmap
745 // as a texture. However, if the texture would be large compared to the
746 // cache size and we don't require most of it for this draw then tile to
747 // reduce the amount of upload and cache spill.
748
749 // assumption here is that sw bitmap size is a good proxy for its size as
750 // a texture
751 size_t bmpSize = bitmap.getSize();
752 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000753 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000754 if (bmpSize < cacheSize / 2) {
755 return false;
756 }
757
758 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt570d2f82015-02-25 13:19:48 -0800759 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap, srcRectPtr,
joshualitt25d9c152015-02-18 12:29:52 -0800760 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000761 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
762 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
763 kBmpSmallTileSize * kBmpSmallTileSize;
764
765 return usedTileBytes < 2 * bmpSize;
766}
767
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000768void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000769 const SkBitmap& bitmap,
770 const SkMatrix& m,
771 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000772 SkMatrix concat;
773 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
774 if (!m.isIdentity()) {
775 concat.setConcat(*draw->fMatrix, m);
776 draw.writable()->fMatrix = &concat;
777 }
778 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000779}
780
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000781// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000782// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
783// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000784static inline void clamped_outset_with_offset(SkIRect* iRect,
785 int outset,
786 SkPoint* offset,
787 const SkIRect& clamp) {
788 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000789
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000790 int leftClampDelta = clamp.fLeft - iRect->fLeft;
791 if (leftClampDelta > 0) {
792 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000793 iRect->fLeft = clamp.fLeft;
794 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000795 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000796 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000797
798 int topClampDelta = clamp.fTop - iRect->fTop;
799 if (topClampDelta > 0) {
800 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000801 iRect->fTop = clamp.fTop;
802 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000803 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000804 }
805
806 if (iRect->fRight > clamp.fRight) {
807 iRect->fRight = clamp.fRight;
808 }
809 if (iRect->fBottom > clamp.fBottom) {
810 iRect->fBottom = clamp.fBottom;
811 }
812}
813
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000814static bool has_aligned_samples(const SkRect& srcRect,
815 const SkRect& transformedRect) {
816 // detect pixel disalignment
817 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
818 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
819 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
820 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
821 SkScalarAbs(transformedRect.width() - srcRect.width()) <
822 COLOR_BLEED_TOLERANCE &&
823 SkScalarAbs(transformedRect.height() - srcRect.height()) <
824 COLOR_BLEED_TOLERANCE) {
825 return true;
826 }
827 return false;
828}
829
830static bool may_color_bleed(const SkRect& srcRect,
831 const SkRect& transformedRect,
832 const SkMatrix& m) {
833 // Only gets called if has_aligned_samples returned false.
834 // So we can assume that sampling is axis aligned but not texel aligned.
835 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
836 SkRect innerSrcRect(srcRect), innerTransformedRect,
837 outerTransformedRect(transformedRect);
838 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
839 m.mapRect(&innerTransformedRect, innerSrcRect);
840
841 // The gap between outerTransformedRect and innerTransformedRect
842 // represents the projection of the source border area, which is
843 // problematic for color bleeding. We must check whether any
844 // destination pixels sample the border area.
845 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
846 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
847 SkIRect outer, inner;
848 outerTransformedRect.round(&outer);
849 innerTransformedRect.round(&inner);
850 // If the inner and outer rects round to the same result, it means the
851 // border does not overlap any pixel centers. Yay!
852 return inner != outer;
853}
854
855static bool needs_texture_domain(const SkBitmap& bitmap,
856 const SkRect& srcRect,
857 GrTextureParams &params,
858 const SkMatrix& contextMatrix,
859 bool bicubic) {
860 bool needsTextureDomain = false;
senorblancod0d37ca2015-04-02 04:54:56 -0700861 GrTexture* tex = bitmap.getTexture();
862 int width = tex ? tex->width() : bitmap.width();
863 int height = tex ? tex->height() : bitmap.height();
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000864
865 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
866 // Need texture domain if drawing a sub rect
senorblancod0d37ca2015-04-02 04:54:56 -0700867 needsTextureDomain = srcRect.width() < width ||
868 srcRect.height() < height;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000869 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
870 // sampling is axis-aligned
871 SkRect transformedRect;
872 contextMatrix.mapRect(&transformedRect, srcRect);
873
874 if (has_aligned_samples(srcRect, transformedRect)) {
875 params.setFilterMode(GrTextureParams::kNone_FilterMode);
876 needsTextureDomain = false;
877 } else {
878 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
879 }
880 }
881 }
882 return needsTextureDomain;
883}
884
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000885void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
886 const SkBitmap& bitmap,
887 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000888 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000889 const SkPaint& paint,
890 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -0800891 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000892
893 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000894 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000895 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
896 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000897 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000898 SkScalar w = SkIntToScalar(bitmap.width());
899 SkScalar h = SkIntToScalar(bitmap.height());
900 dstSize.fWidth = w;
901 dstSize.fHeight = h;
902 srcRect.set(0, 0, w, h);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000903 } else {
bsalomon49f085d2014-09-05 13:34:00 -0700904 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000905 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000906 dstSize = *dstSizePtr;
senorblancod0d37ca2015-04-02 04:54:56 -0700907 }
908 GrTexture* tex = bitmap.getTexture();
909 int width = tex ? tex->width() : bitmap.width();
910 int height = tex ? tex->height() : bitmap.height();
911 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
912 srcRect.fRight >= width && srcRect.fBottom >= height) {
913 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000914 }
915
derekf367e1862014-12-02 11:02:06 -0800916 // If the render target is not msaa and draw is antialiased, we call
917 // drawRect instead of drawing on the render target directly.
918 // FIXME: the tiled bitmap code path doesn't currently support
919 // anti-aliased edges, we work around that for now by drawing directly
920 // if the image size exceeds maximum texture size.
bsalomon76228632015-05-29 08:02:10 -0700921 int maxTextureSize = fContext->caps()->maxTextureSize();
derekf367e1862014-12-02 11:02:06 -0800922 bool directDraw = fRenderTarget->isMultisampled() ||
923 !paint.isAntiAlias() ||
924 bitmap.width() > maxTextureSize ||
925 bitmap.height() > maxTextureSize;
926
927 // we check whether dst rect are pixel aligned
928 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -0800929 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -0800930
931 if (staysRect) {
932 SkRect rect;
933 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -0800934 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -0800935 const SkScalar *scalars = rect.asScalars();
936 bool isDstPixelAligned = true;
937 for (int i = 0; i < 4; i++) {
938 if (!SkScalarIsInt(scalars[i])) {
939 isDstPixelAligned = false;
940 break;
941 }
942 }
943
944 if (isDstPixelAligned)
945 directDraw = true;
946 }
947 }
948
949 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000950 // Convert the bitmap to a shader so that the rect can be drawn
951 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000952 SkBitmap tmp; // subset of bitmap, if necessary
953 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000954 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -0700955 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000956 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
957 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
958 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000959 // In bleed mode we position and trim the bitmap based on the src rect which is
960 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
961 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
962 // compensate.
963 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
964 SkIRect iSrc;
965 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000966
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000967 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
968 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000969
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000970 if (!bitmap.extractSubset(&tmp, iSrc)) {
971 return; // extraction failed
972 }
973 bitmapPtr = &tmp;
974 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000975
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000976 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000977 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000978 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000979 } else {
980 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000981 }
982
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000983 SkPaint paintWithShader(paint);
984 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000985 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000986 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
987 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000988
989 return;
990 }
991
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000992 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
993 // the view matrix rather than a local matrix.
994 SkMatrix m;
995 m.setScale(dstSize.fWidth / srcRect.width(),
996 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -0800997 SkMatrix viewM = *draw.fMatrix;
998 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000999
1000 GrTextureParams params;
reed93a12152015-03-16 10:08:34 -07001001 SkFilterQuality paintFilterQuality = paint.getFilterQuality();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001002 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001003
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001004 bool doBicubic = false;
1005
reed93a12152015-03-16 10:08:34 -07001006 switch(paintFilterQuality) {
1007 case kNone_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001008 textureFilterMode = GrTextureParams::kNone_FilterMode;
1009 break;
reed93a12152015-03-16 10:08:34 -07001010 case kLow_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001011 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1012 break;
reed93a12152015-03-16 10:08:34 -07001013 case kMedium_SkFilterQuality:
joshualitt5531d512014-12-17 15:50:11 -08001014 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001015 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1016 } else {
1017 // Don't trigger MIP level generation unnecessarily.
1018 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1019 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001020 break;
reed93a12152015-03-16 10:08:34 -07001021 case kHigh_SkFilterQuality:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001022 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001023 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001024 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001025 break;
1026 default:
1027 SkErrorInternals::SetError( kInvalidPaint_SkError,
1028 "Sorry, I don't understand the filtering "
1029 "mode you asked for. Falling back to "
1030 "MIPMaps.");
1031 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1032 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001033 }
1034
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001035 int tileFilterPad;
1036 if (doBicubic) {
1037 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1038 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1039 tileFilterPad = 0;
1040 } else {
1041 tileFilterPad = 1;
1042 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001043 params.setFilterMode(textureFilterMode);
1044
bsalomon76228632015-05-29 08:02:10 -07001045 int maxTileSize = fContext->caps()->maxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001046 int tileSize;
1047
1048 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001049 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001050 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001051 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1052 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001053 } else {
1054 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001055 bool needsTextureDomain = needs_texture_domain(bitmap,
1056 srcRect,
1057 params,
joshualitt5531d512014-12-17 15:50:11 -08001058 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001059 doBicubic);
1060 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001061 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001062 srcRect,
1063 params,
1064 paint,
1065 flags,
1066 doBicubic,
1067 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001068 }
1069}
1070
1071// Break 'bitmap' into several tiles to draw it since it has already
1072// been determined to be too large to fit in VRAM
1073void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001074 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001075 const SkRect& srcRect,
1076 const SkIRect& clippedSrcIRect,
1077 const GrTextureParams& params,
1078 const SkPaint& paint,
1079 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001080 int tileSize,
1081 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001082 // The following pixel lock is technically redundant, but it is desirable
1083 // to lock outside of the tile loop to prevent redecoding the whole image
1084 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1085 // is larger than the limit of the discardable memory pool.
1086 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001087 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1088
1089 int nx = bitmap.width() / tileSize;
1090 int ny = bitmap.height() / tileSize;
1091 for (int x = 0; x <= nx; x++) {
1092 for (int y = 0; y <= ny; y++) {
1093 SkRect tileR;
1094 tileR.set(SkIntToScalar(x * tileSize),
1095 SkIntToScalar(y * tileSize),
1096 SkIntToScalar((x + 1) * tileSize),
1097 SkIntToScalar((y + 1) * tileSize));
1098
1099 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1100 continue;
1101 }
1102
1103 if (!tileR.intersect(srcRect)) {
1104 continue;
1105 }
1106
1107 SkBitmap tmpB;
1108 SkIRect iTileR;
1109 tileR.roundOut(&iTileR);
1110 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1111 SkIntToScalar(iTileR.fTop));
1112
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001113 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001114 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001115 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001116 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001117 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001118
robertphillipsec8bb942014-11-21 10:16:25 -08001119 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001120 SkIRect iClampRect;
1121
1122 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1123 // In bleed mode we want to always expand the tile on all edges
1124 // but stay within the bitmap bounds
1125 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1126 } else {
1127 // In texture-domain/clamp mode we only want to expand the
1128 // tile on edges interior to "srcRect" (i.e., we want to
1129 // not bleed across the original clamped edges)
1130 srcRect.roundOut(&iClampRect);
1131 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001132 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1133 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001134 }
1135
1136 if (bitmap.extractSubset(&tmpB, iTileR)) {
1137 // now offset it to make it "local" to our tmp bitmap
1138 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001139 GrTextureParams paramsTemp = params;
1140 bool needsTextureDomain = needs_texture_domain(bitmap,
1141 srcRect,
1142 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001143 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001144 bicubic);
1145 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001146 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001147 tileR,
1148 paramsTemp,
1149 paint,
1150 flags,
1151 bicubic,
1152 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001153 }
1154 }
1155 }
1156}
1157
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001158
1159/*
1160 * This is called by drawBitmap(), which has to handle images that may be too
1161 * large to be represented by a single texture.
1162 *
1163 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1164 * and that non-texture portion of the GrPaint has already been setup.
1165 */
1166void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001167 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001168 const SkRect& srcRect,
1169 const GrTextureParams& params,
1170 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001171 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001172 bool bicubic,
1173 bool needsTextureDomain) {
bsalomon76228632015-05-29 08:02:10 -07001174 SkASSERT(bitmap.width() <= fContext->caps()->maxTextureSize() &&
1175 bitmap.height() <= fContext->caps()->maxTextureSize());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001176
1177 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001178 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001179 if (NULL == texture) {
1180 return;
1181 }
1182
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001183 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001184 SkRect paintRect;
1185 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1186 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1187 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1188 SkScalarMul(srcRect.fTop, hInv),
1189 SkScalarMul(srcRect.fRight, wInv),
1190 SkScalarMul(srcRect.fBottom, hInv));
1191
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001192 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001193 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001194 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195 // Use a constrained texture domain to avoid color bleeding
1196 SkScalar left, top, right, bottom;
1197 if (srcRect.width() > SK_Scalar1) {
1198 SkScalar border = SK_ScalarHalf / texture->width();
1199 left = paintRect.left() + border;
1200 right = paintRect.right() - border;
1201 } else {
1202 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1203 }
1204 if (srcRect.height() > SK_Scalar1) {
1205 SkScalar border = SK_ScalarHalf / texture->height();
1206 top = paintRect.top() + border;
1207 bottom = paintRect.bottom() - border;
1208 } else {
1209 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1210 }
1211 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001212 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001213 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001214 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001215 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001216 SkMatrix::I(),
1217 textureDomain,
1218 GrTextureDomain::kClamp_Mode,
1219 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001220 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001221 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001222 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1223 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001224 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001225 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001226 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001227 }
1228
1229 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1230 // the rest from the SkPaint.
1231 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001232 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001233 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001234 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1235 SkColor2GrColor(paint.getColor());
bsalomonbed83a62015-04-15 14:18:34 -07001236 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false,
1237 &grPaint)) {
1238 return;
1239 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001240
robertphillipsea461502015-05-26 11:38:03 -07001241 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dstRect,
1242 paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001243}
1244
fmalita2d97bc12014-11-20 10:44:58 -08001245bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001246 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001247 const SkImageFilter* filter,
1248 const SkImageFilter::Context& ctx,
1249 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001250 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001251
1252 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1253 // (https://code.google.com/p/skia/issues/detail?id=3148).
reed2c55d7b2015-06-09 08:18:39 -07001254 SkImageFilter::Proxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001255
1256 if (filter->canFilterImageGPU()) {
senorblancod0d37ca2015-04-02 04:54:56 -07001257 return filter->filterImageGPU(&proxy, wrap_texture(texture, width, height),
1258 ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001259 } else {
1260 return false;
1261 }
1262}
1263
1264void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1265 int left, int top, const SkPaint& paint) {
1266 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001267 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001268
1269 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1270 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1271 return;
1272 }
1273
1274 int w = bitmap.width();
1275 int h = bitmap.height();
1276
1277 GrTexture* texture;
1278 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001279 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001280 if (!texture) {
1281 return;
1282 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001283
1284 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001285 // This bitmap will own the filtered result as a texture.
1286 SkBitmap filteredBitmap;
1287
bsalomon49f085d2014-09-05 13:34:00 -07001288 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001289 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001290 SkMatrix matrix(*draw.fMatrix);
1291 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001292 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001293 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001294 // This cache is transient, and is freed (along with all its contained
1295 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001296 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001297 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001298 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001299 texture = (GrTexture*) filteredBitmap.getTexture();
1300 w = filteredBitmap.width();
1301 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001302 left += offset.x();
1303 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001304 } else {
1305 return;
1306 }
1307 }
1308
1309 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001310 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001311
bsalomonbed83a62015-04-15 14:18:34 -07001312 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1313 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1314 return;
1315 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001316
robertphillipsea461502015-05-26 11:38:03 -07001317 fDrawContext->drawNonAARectToRect(fRenderTarget,
1318 fClip,
1319 grPaint,
1320 SkMatrix::I(),
1321 SkRect::MakeXYWH(SkIntToScalar(left),
1322 SkIntToScalar(top),
1323 SkIntToScalar(w),
1324 SkIntToScalar(h)),
1325 SkRect::MakeXYWH(0,
1326 0,
1327 SK_Scalar1 * w / texture->width(),
1328 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001329}
1330
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001331void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001332 const SkRect* src, const SkRect& dst,
1333 const SkPaint& paint,
1334 SkCanvas::DrawBitmapRectFlags flags) {
1335 SkMatrix matrix;
1336 SkRect bitmapBounds, tmpSrc;
1337
1338 bitmapBounds.set(0, 0,
1339 SkIntToScalar(bitmap.width()),
1340 SkIntToScalar(bitmap.height()));
1341
1342 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001343 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001344 tmpSrc = *src;
1345 } else {
1346 tmpSrc = bitmapBounds;
1347 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001348
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001349 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1350
1351 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001352 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001353 if (!bitmapBounds.contains(tmpSrc)) {
1354 if (!tmpSrc.intersect(bitmapBounds)) {
1355 return; // nothing to draw
1356 }
1357 }
1358 }
1359
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001360 SkRect tmpDst;
1361 matrix.mapRect(&tmpDst, tmpSrc);
1362
1363 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1364 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1365 // Translate so that tempDst's top left is at the origin.
1366 matrix = *origDraw.fMatrix;
1367 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1368 draw.writable()->fMatrix = &matrix;
1369 }
1370 SkSize dstSize;
1371 dstSize.fWidth = tmpDst.width();
1372 dstSize.fHeight = tmpDst.height();
1373
1374 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001375}
1376
1377void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1378 int x, int y, const SkPaint& paint) {
1379 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001380 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001381 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001382
1383 // TODO: If the source device covers the whole of this device, we could
1384 // omit fNeedsClear -related flushing.
1385 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001386
1387 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001388 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001389
1390 GrRenderTarget* devRT = dev->accessRenderTarget();
1391 GrTexture* devTex;
1392 if (NULL == (devTex = devRT->asTexture())) {
1393 return;
1394 }
1395
robertphillips7b9e8a42014-12-11 08:20:31 -08001396 const SkImageInfo ii = dev->imageInfo();
1397 int w = ii.width();
1398 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001399
1400 SkImageFilter* filter = paint.getImageFilter();
1401 // This bitmap will own the filtered result as a texture.
1402 SkBitmap filteredBitmap;
1403
bsalomon49f085d2014-09-05 13:34:00 -07001404 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001405 SkIPoint offset = SkIPoint::Make(0, 0);
1406 SkMatrix matrix(*draw.fMatrix);
1407 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001408 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001409 // This cache is transient, and is freed (along with all its contained
1410 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001411 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001412 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001413 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1414 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001415 devTex = filteredBitmap.getTexture();
1416 w = filteredBitmap.width();
1417 h = filteredBitmap.height();
1418 x += offset.fX;
1419 y += offset.fY;
1420 } else {
1421 return;
1422 }
1423 }
1424
1425 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001426 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001427
bsalomonbed83a62015-04-15 14:18:34 -07001428 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1429 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1430 return;
1431 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001432
1433 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1434 SkIntToScalar(y),
1435 SkIntToScalar(w),
1436 SkIntToScalar(h));
1437
1438 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1439 // scratch texture).
1440 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1441 SK_Scalar1 * h / devTex->height());
1442
robertphillipsea461502015-05-26 11:38:03 -07001443 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, SkMatrix::I(), dstRect,
1444 srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001445}
1446
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001447bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001448 return filter->canFilterImageGPU();
1449}
1450
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001451bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001452 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001453 SkBitmap* result, SkIPoint* offset) {
1454 // want explicitly our impl, so guard against a subclass of us overriding it
1455 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1456 return false;
1457 }
1458
1459 SkAutoLockPixels alp(src, !src.getTexture());
1460 if (!src.getTexture() && !src.readyToDraw()) {
1461 return false;
1462 }
1463
1464 GrTexture* texture;
1465 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1466 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001467 AutoBitmapTexture abt(fContext, src, NULL, &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001468 if (!texture) {
1469 return false;
1470 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001471
senorblancod0d37ca2015-04-02 04:54:56 -07001472 return this->filterTexture(fContext, texture, src.width(), src.height(),
1473 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001474}
1475
reeda85d4d02015-05-06 12:56:48 -07001476static bool wrap_as_bm(const SkImage* image, SkBitmap* bm) {
bsalomon55812362015-06-10 08:49:28 -07001477 GrTexture* tex = as_IB(image)->getTexture();
reeda85d4d02015-05-06 12:56:48 -07001478 if (tex) {
reed8b26b992015-05-07 15:36:17 -07001479 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), bm);
1480 return true;
reeda85d4d02015-05-06 12:56:48 -07001481 } else {
reed8b26b992015-05-07 15:36:17 -07001482 return as_IB(image)->getROPixels(bm);
reeda85d4d02015-05-06 12:56:48 -07001483 }
reeda85d4d02015-05-06 12:56:48 -07001484}
1485
1486void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1487 const SkPaint& paint) {
1488 SkBitmap bm;
1489 if (wrap_as_bm(image, &bm)) {
1490 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1491 }
1492}
1493
1494void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
1495 const SkRect& dst, const SkPaint& paint) {
1496 SkBitmap bm;
1497 if (wrap_as_bm(image, &bm)) {
1498 this->drawBitmapRect(draw, bm, src, dst, paint, SkCanvas::kNone_DrawBitmapRectFlag);
1499 }
1500}
1501
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001502///////////////////////////////////////////////////////////////////////////////
1503
1504// must be in SkCanvas::VertexMode order
1505static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1506 kTriangles_GrPrimitiveType,
1507 kTriangleStrip_GrPrimitiveType,
1508 kTriangleFan_GrPrimitiveType,
1509};
1510
1511void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1512 int vertexCount, const SkPoint vertices[],
1513 const SkPoint texs[], const SkColor colors[],
1514 SkXfermode* xmode,
1515 const uint16_t indices[], int indexCount,
1516 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001517 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001518 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001519
dandov32a311b2014-07-15 19:46:26 -07001520 const uint16_t* outIndices;
1521 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1522 GrPrimitiveType primType;
1523 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001524
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001525 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1526 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001527
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001528 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001529
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001530 SkPaint copy(paint);
1531 copy.setStyle(SkPaint::kStroke_Style);
1532 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001533
dandov32a311b2014-07-15 19:46:26 -07001534 // we ignore the shader if texs is null.
bsalomonbed83a62015-04-15 14:18:34 -07001535 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy,
1536 SkColor2GrColor(copy.getColor()), NULL == colors, &grPaint)) {
1537 return;
1538 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001539
dandov32a311b2014-07-15 19:46:26 -07001540 primType = kLines_GrPrimitiveType;
1541 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001542 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001543 switch (vmode) {
1544 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001545 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001546 break;
1547 case SkCanvas::kTriangleStrip_VertexMode:
1548 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001549 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001550 break;
1551 }
mtklein533eb782014-08-27 10:39:42 -07001552
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001553 VertState state(vertexCount, indices, indexCount);
1554 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001555
dandov32a311b2014-07-15 19:46:26 -07001556 //number of indices for lines per triangle with kLines
1557 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001558
dandov32a311b2014-07-15 19:46:26 -07001559 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1560 outIndices = outAlloc.get();
1561 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001562 int i = 0;
1563 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001564 auxIndices[i] = state.f0;
1565 auxIndices[i + 1] = state.f1;
1566 auxIndices[i + 2] = state.f1;
1567 auxIndices[i + 3] = state.f2;
1568 auxIndices[i + 4] = state.f2;
1569 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001570 i += 6;
1571 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001572 } else {
dandov32a311b2014-07-15 19:46:26 -07001573 outIndices = indices;
1574 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001575
dandov32a311b2014-07-15 19:46:26 -07001576 if (NULL == texs || NULL == paint.getShader()) {
bsalomonbed83a62015-04-15 14:18:34 -07001577 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1578 SkColor2GrColor(paint.getColor()),
1579 NULL == colors, &grPaint)) {
1580 return;
1581 }
dandov32a311b2014-07-15 19:46:26 -07001582 } else {
bsalomonbed83a62015-04-15 14:18:34 -07001583 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix,
1584 NULL == colors, &grPaint)) {
1585 return;
1586 }
dandov32a311b2014-07-15 19:46:26 -07001587 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001588 }
1589
mtklein2583b622014-06-04 08:20:41 -07001590#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001591 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001592 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1593 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001594 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001595 }
1596 }
mtklein2583b622014-06-04 08:20:41 -07001597#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001598
1599 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001600 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001601 // need to convert byte order and from non-PM to PM
1602 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001603 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001604 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001605 color = colors[i];
1606 if (paint.getAlpha() != 255) {
1607 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1608 }
1609 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001610 }
1611 colors = convertedColors.get();
1612 }
robertphillipsea461502015-05-26 11:38:03 -07001613 fDrawContext->drawVertices(fRenderTarget,
1614 fClip,
1615 grPaint,
1616 *draw.fMatrix,
1617 primType,
1618 vertexCount,
1619 vertices,
1620 texs,
1621 colors,
1622 outIndices,
1623 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001624}
1625
1626///////////////////////////////////////////////////////////////////////////////
1627
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001628void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001629 size_t byteLength, SkScalar x, SkScalar y,
1630 const SkPaint& paint) {
1631 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001632 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001633
jvanverth8c27a182014-10-14 08:45:50 -07001634 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001635 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1636 return;
1637 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001638
jvanverth8c27a182014-10-14 08:45:50 -07001639 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001640
joshualitt6e8cd962015-03-20 10:30:14 -07001641 fTextContext->drawText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
1642 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001643}
1644
fmalita05c4a432014-09-29 06:29:53 -07001645void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1646 const SkScalar pos[], int scalarsPerPos,
1647 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001648 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001649 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001650
jvanverth8c27a182014-10-14 08:45:50 -07001651 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001652 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1653 return;
1654 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001655
jvanverth8c27a182014-10-14 08:45:50 -07001656 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001657
joshualitt6e8cd962015-03-20 10:30:14 -07001658 fTextContext->drawPosText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
1659 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1660 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001661}
1662
joshualitt9c328182015-03-23 08:13:04 -07001663void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1664 const SkPaint& paint, SkDrawFilter* drawFilter) {
1665 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawTextBlob", fContext);
1666 CHECK_SHOULD_DRAW(draw);
1667
1668 SkDEBUGCODE(this->validate();)
1669
robertphillips9c240a12015-05-28 07:45:59 -07001670 fTextContext->drawTextBlob(fRenderTarget, fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001671 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001672}
1673
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001674///////////////////////////////////////////////////////////////////////////////
1675
reedb2db8982014-11-13 12:41:02 -08001676bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
robertphillips9c240a12015-05-28 07:45:59 -07001677 return GrTextContext::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001678}
1679
1680void SkGpuDevice::flush() {
1681 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001682 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001683}
1684
1685///////////////////////////////////////////////////////////////////////////////
1686
reed76033be2015-03-14 10:54:31 -07001687SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
bsalomonf2703d82014-10-28 14:33:06 -07001688 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001689 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001690 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001691 desc.fWidth = cinfo.fInfo.width();
1692 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001693 desc.fSampleCnt = fRenderTarget->numSamples();
1694
1695 SkAutoTUnref<GrTexture> texture;
1696 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001697 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001698
hcm4396fa52014-10-27 21:43:30 -07001699 // layers are never draw in repeat modes, so we can request an approx
1700 // match and ignore any padding.
bsalomond309e7a2015-04-30 14:18:54 -07001701 const GrTextureProvider::ScratchTexMatch match = (kNever_TileUsage == cinfo.fTileUsage) ?
1702 GrTextureProvider::kApprox_ScratchTexMatch :
1703 GrTextureProvider::kExact_ScratchTexMatch;
1704 texture.reset(fContext->textureProvider()->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001705
1706 if (texture) {
1707 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001708 return SkGpuDevice::Create(
1709 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001710 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001711 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001712 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001713 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001714 return NULL;
1715 }
1716}
1717
reed4a8126e2014-09-22 07:29:03 -07001718SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001719 // TODO: Change the signature of newSurface to take a budgeted parameter.
1720 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
1721 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->numSamples(),
1722 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001723}
1724
robertphillips30d2cc62014-09-24 08:52:18 -07001725bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001726 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001727#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001728 // todo: should handle this natively
1729 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001730 return false;
1731 }
1732
mtklein9db912c2015-05-19 11:11:26 -07001733 const SkBigPicture::AccelData* data = NULL;
1734 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1735 data = bp->accelData();
1736 }
robertphillips81f71b62014-11-11 04:54:49 -08001737 if (!data) {
1738 return false;
1739 }
1740
robertphillipse5524cd2015-02-20 12:30:26 -08001741 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1742 if (0 == gpuData->numBlocks()) {
1743 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001744 }
1745
robertphillipsfd61ed02014-10-28 07:21:44 -07001746 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001747
robertphillipse5524cd2015-02-20 12:30:26 -08001748 SkIRect iBounds;
1749 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1750 return false;
1751 }
1752
1753 SkRect clipBounds = SkRect::Make(iBounds);
1754
1755 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1756
robertphillipsfd61ed02014-10-28 07:21:44 -07001757 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001758 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001759 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001760 &atlasedNeedRendering, &atlasedRecycled,
1761 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001762
1763 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1764
1765 SkTDArray<GrHoistedLayer> needRendering, recycled;
1766
robertphillipse5524cd2015-02-20 12:30:26 -08001767 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1768
robertphillipsfd61ed02014-10-28 07:21:44 -07001769 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001770 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001771 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001772 &needRendering, &recycled,
1773 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001774
1775 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001776
robertphillips64bf7672014-08-21 13:07:35 -07001777 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001778 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1779 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001780
robertphillipsfd61ed02014-10-28 07:21:44 -07001781 GrLayerHoister::UnlockLayers(fContext, needRendering);
1782 GrLayerHoister::UnlockLayers(fContext, recycled);
1783 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1784 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001785
1786 return true;
robertphillips63242d72014-12-04 08:31:02 -08001787#else
1788 return false;
1789#endif
robertphillips64bf7672014-08-21 13:07:35 -07001790}
1791
senorblancobe129b22014-08-08 07:14:35 -07001792SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001793 // We always return a transient cache, so it is freed after each
1794 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001795 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001796}
reedf037e0b2014-10-30 11:34:15 -07001797
1798#endif