blob: 9518600e9948cd681e1bf9f4fb587bc046ccce1c [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
kkinnunenabcfab42015-02-22 22:53:44 -080010#include "GrContext.h"
kkinnunenabcfab42015-02-22 22:53:44 -080011#include "GrGpu.h"
12#include "GrGpuResourcePriv.h"
robertphillips98d709b2014-09-02 10:20:50 -070013#include "GrLayerHoister.h"
robertphillips274b4ba2014-09-04 07:24:18 -070014#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070015#include "GrStrokeInfo.h"
joshualitt8f94bb22015-04-28 07:04:11 -070016#include "GrTextContext.h"
egdanielbbcb38d2014-06-19 10:19:29 -070017#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080018#include "SkCanvasPriv.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000019#include "SkDeviceImageFilterProxy.h"
20#include "SkDrawProcs.h"
kkinnunenabcfab42015-02-22 22:53:44 -080021#include "SkErrorInternals.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000022#include "SkGlyphCache.h"
kkinnunenabcfab42015-02-22 22:53:44 -080023#include "SkGrTexturePixelRef.h"
reed620ba3a2015-05-06 07:54:38 -070024#include "SkImage_Base.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080026#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000027#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000029#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070030#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000031#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080032#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000034#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080035#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000036#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000038#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070039#include "SkXfermode.h"
kkinnunenabcfab42015-02-22 22:53:44 -080040#include "effects/GrBicubicEffect.h"
41#include "effects/GrDashingEffect.h"
42#include "effects/GrSimpleTextureEffect.h"
43#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000044
reedf037e0b2014-10-30 11:34:15 -070045#if SK_SUPPORT_GPU
46
senorblanco55b6d8b2014-07-30 11:26:46 -070047enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
48
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000049#if 0
50 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080051 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000052 do { \
53 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080054 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000055 } while (0)
56#else
joshualitt5531d512014-12-17 15:50:11 -080057 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058#endif
59
60// This constant represents the screen alignment criterion in texels for
61// requiring texture domain clamping to prevent color bleeding when drawing
62// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000063#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000064
65#define DO_DEFERRED_CLEAR() \
66 do { \
bsalomonafe30052015-01-16 07:32:33 -080067 if (fNeedClear) { \
68 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000069 } \
70 } while (false) \
71
72///////////////////////////////////////////////////////////////////////////////
73
74#define CHECK_FOR_ANNOTATION(paint) \
75 do { if (paint.getAnnotation()) { return; } } while (0)
76
77///////////////////////////////////////////////////////////////////////////////
78
bsalomonbcf0a522014-10-08 08:40:09 -070079// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
80// just accesses the backing GrTexture. Otherwise, it creates a cached texture
81// representation and releases it in the destructor.
82class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040083public:
bsalomonbcf0a522014-10-08 08:40:09 -070084 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070085
bsalomonbcf0a522014-10-08 08:40:09 -070086 AutoBitmapTexture(GrContext* context,
87 const SkBitmap& bitmap,
88 const GrTextureParams* params,
89 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040090 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070091 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040092 }
93
bsalomonbcf0a522014-10-08 08:40:09 -070094 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040095 const SkBitmap& bitmap,
96 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -070097 // Either get the texture directly from the bitmap, or else use the cache and
98 // remember to unref it.
99 if (GrTexture* bmpTexture = bitmap.getTexture()) {
100 fTexture.reset(NULL);
101 return bmpTexture;
102 } else {
103 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
104 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400105 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400106 }
107
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000108private:
bsalomonbcf0a522014-10-08 08:40:09 -0700109 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000110};
111
112///////////////////////////////////////////////////////////////////////////////
113
114struct GrSkDrawProcs : public SkDrawProcs {
115public:
116 GrContext* fContext;
117 GrTextContext* fTextContext;
118 GrFontScaler* fFontScaler; // cached in the skia glyphcache
119};
120
121///////////////////////////////////////////////////////////////////////////////
122
bsalomonafe30052015-01-16 07:32:33 -0800123SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags) {
senorblancod0d37ca2015-04-02 04:54:56 -0700124 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, flags);
125}
126
127SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
128 const SkSurfaceProps* props, unsigned flags) {
bsalomonafe30052015-01-16 07:32:33 -0800129 if (!rt || rt->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000130 return NULL;
131 }
senorblancod0d37ca2015-04-02 04:54:56 -0700132 return SkNEW_ARGS(SkGpuDevice, (rt, width, height, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000133}
134
bsalomonafe30052015-01-16 07:32:33 -0800135static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps* props) {
136 if (props) {
137 return SkDeviceProperties(props->pixelGeometry());
138 } else {
139 return SkDeviceProperties(SkDeviceProperties::kLegacyLCD_InitType);
140 }
reedb2db8982014-11-13 12:41:02 -0800141}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000142
bsalomonafe30052015-01-16 07:32:33 -0800143static SkSurfaceProps copy_or_default_props(const SkSurfaceProps* props) {
144 if (props) {
145 return SkSurfaceProps(*props);
146 } else {
147 return SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
148 }
149}
150
senorblancod0d37ca2015-04-02 04:54:56 -0700151SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
152 const SkSurfaceProps* props, unsigned flags)
reedb2db8982014-11-13 12:41:02 -0800153 : INHERITED(surfaceprops_to_deviceprops(props))
bsalomonafe30052015-01-16 07:32:33 -0800154 , fSurfaceProps(copy_or_default_props(props))
reedb2db8982014-11-13 12:41:02 -0800155{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000156 fDrawProcs = NULL;
157
bsalomonafe30052015-01-16 07:32:33 -0800158 fContext = SkRef(rt->getContext());
159 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000160
bsalomonafe30052015-01-16 07:32:33 -0800161 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000162
senorblancod0d37ca2015-04-02 04:54:56 -0700163 SkImageInfo info = rt->surfacePriv().info().makeWH(width, height);
bsalomonafe30052015-01-16 07:32:33 -0800164 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt));
bsalomonafbf2d62014-09-30 12:18:44 -0700165 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700166 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700167
bsalomonafe30052015-01-16 07:32:33 -0800168 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts();
joshualitt6e8cd962015-03-20 10:30:14 -0700169 fTextContext = fContext->createTextContext(fRenderTarget, this, this->getLeakyProperties(),
170 useDFT);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000171}
172
kkinnunenabcfab42015-02-22 22:53:44 -0800173GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
174 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000175 if (kUnknown_SkColorType == origInfo.colorType() ||
176 origInfo.width() < 0 || origInfo.height() < 0) {
177 return NULL;
178 }
179
bsalomonafe30052015-01-16 07:32:33 -0800180 if (!context) {
181 return NULL;
182 }
183
reede5ea5002014-09-03 11:54:58 -0700184 SkColorType ct = origInfo.colorType();
185 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700186 if (kRGB_565_SkColorType == ct) {
187 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800188 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
189 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700190 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800191 }
192 if (kOpaque_SkAlphaType != at) {
193 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000194 }
reede5ea5002014-09-03 11:54:58 -0700195 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000196
bsalomonf2703d82014-10-28 14:33:06 -0700197 GrSurfaceDesc desc;
198 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000199 desc.fWidth = info.width();
200 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000201 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000202 desc.fSampleCnt = sampleCount;
bsalomond309e7a2015-04-30 14:18:54 -0700203 GrTexture* texture = context->textureProvider()->createTexture(
204 desc, SkToBool(budgeted), NULL, 0);
kkinnunenabcfab42015-02-22 22:53:44 -0800205 if (NULL == texture) {
206 return NULL;
207 }
208 SkASSERT(NULL != texture->asRenderTarget());
209 return texture->asRenderTarget();
210}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000211
kkinnunenabcfab42015-02-22 22:53:44 -0800212SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
213 const SkImageInfo& info, int sampleCount,
214 const SkSurfaceProps* props, unsigned flags) {
215
216 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
217 if (NULL == rt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000218 return NULL;
219 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000220
senorblancod0d37ca2015-04-02 04:54:56 -0700221 return SkNEW_ARGS(SkGpuDevice, (rt, info.width(), info.height(), props, flags));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000222}
223
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000224SkGpuDevice::~SkGpuDevice() {
225 if (fDrawProcs) {
226 delete fDrawProcs;
227 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000228
jvanverth8c27a182014-10-14 08:45:50 -0700229 delete fTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000230
bsalomon32d0b3b2014-08-29 07:50:23 -0700231 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000232 fContext->unref();
233}
234
235///////////////////////////////////////////////////////////////////////////////
236
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000237bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
238 int x, int y) {
239 DO_DEFERRED_CLEAR();
240
241 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000242 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000243 if (kUnknown_GrPixelConfig == config) {
244 return false;
245 }
246
247 uint32_t flags = 0;
248 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
249 flags = GrContext::kUnpremul_PixelOpsFlag;
250 }
251 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
252 config, dstPixels, dstRowBytes, flags);
253}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000254
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000255bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
256 int x, int y) {
257 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000258 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000259 if (kUnknown_GrPixelConfig == config) {
260 return false;
261 }
262 uint32_t flags = 0;
263 if (kUnpremul_SkAlphaType == info.alphaType()) {
264 flags = GrContext::kUnpremul_PixelOpsFlag;
265 }
266 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
267
268 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700269 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000270
271 return true;
272}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000273
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000274const SkBitmap& SkGpuDevice::onAccessBitmap() {
275 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700276 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000277}
278
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000279void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
280 INHERITED::onAttachToCanvas(canvas);
281
282 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800283 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000284}
285
286void SkGpuDevice::onDetachFromCanvas() {
287 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800288 fClip.reset();
joshualitt44701df2015-02-23 14:44:57 -0800289 fClipStack.reset(NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000290}
291
292// call this every draw call, to ensure that the context reflects our state,
293// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800294void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800295 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000296
joshualitt44701df2015-02-23 14:44:57 -0800297 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000298
joshualitt570d2f82015-02-25 13:19:48 -0800299 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000300
301 DO_DEFERRED_CLEAR();
302}
303
304GrRenderTarget* SkGpuDevice::accessRenderTarget() {
305 DO_DEFERRED_CLEAR();
306 return fRenderTarget;
307}
308
reed8eddfb52014-12-04 07:50:14 -0800309void SkGpuDevice::clearAll() {
310 GrColor color = 0;
311 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
312 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
313 fContext->clear(&rect, color, true, fRenderTarget);
bsalomonafe30052015-01-16 07:32:33 -0800314 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800315}
316
kkinnunenabcfab42015-02-22 22:53:44 -0800317void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
318 // Caller must have accessed the render target, because it knows the rt must be replaced.
319 SkASSERT(!fNeedClear);
320
321 SkSurface::Budgeted budgeted =
322 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
323 : SkSurface::kNo_Budgeted;
324
325 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
326 fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->numSamples()));
327
328 if (NULL == newRT) {
329 return;
330 }
331
332 if (shouldRetainContent) {
333 if (fRenderTarget->wasDestroyed()) {
334 return;
335 }
336 this->context()->copySurface(newRT, fRenderTarget);
337 }
338
339 SkASSERT(fRenderTarget != newRT);
340
341 fRenderTarget->unref();
342 fRenderTarget = newRT.detach();
343
344 SkASSERT(fRenderTarget->surfacePriv().info() == fLegacyBitmap.info());
345 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (fRenderTarget->surfacePriv().info(), fRenderTarget));
346 fLegacyBitmap.setPixelRef(pr)->unref();
347}
348
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000349///////////////////////////////////////////////////////////////////////////////
350
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000351void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800352 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700353 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000354
355 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700356 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
357 return;
358 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000359
joshualitt570d2f82015-02-25 13:19:48 -0800360 fContext->drawPaint(fRenderTarget, fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000361}
362
363// must be in SkCanvas::PointMode order
364static const GrPrimitiveType gPointMode2PrimtiveType[] = {
365 kPoints_GrPrimitiveType,
366 kLines_GrPrimitiveType,
367 kLineStrip_GrPrimitiveType
368};
369
370void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
371 size_t count, const SkPoint pts[], const SkPaint& paint) {
372 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800373 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000374
375 SkScalar width = paint.getStrokeWidth();
376 if (width < 0) {
377 return;
378 }
379
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000380 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700381 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
382 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700383 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
384 &grPaint)) {
385 return;
386 }
egdaniele61c4112014-06-12 10:24:21 -0700387 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700388 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700389 path.moveTo(pts[0]);
390 path.lineTo(pts[1]);
joshualitt570d2f82015-02-25 13:19:48 -0800391 fContext->drawPath(fRenderTarget, fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700392 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000393 }
394
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000395 // we only handle hairlines and paints without path effects or mask filters,
396 // else we let the SkDraw call our drawPath()
397 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
398 draw.drawPoints(mode, count, pts, paint, true);
399 return;
400 }
401
402 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700403 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
404 return;
405 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000406
joshualitt25d9c152015-02-18 12:29:52 -0800407 fContext->drawVertices(fRenderTarget,
joshualitt570d2f82015-02-25 13:19:48 -0800408 fClip,
joshualitt25d9c152015-02-18 12:29:52 -0800409 grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800410 *draw.fMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000411 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000412 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000413 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000414 NULL,
415 NULL,
416 NULL,
417 0);
418}
419
420///////////////////////////////////////////////////////////////////////////////
421
422void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
423 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700424 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
425
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000426 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800427 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000428
429 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
430 SkScalar width = paint.getStrokeWidth();
431
432 /*
433 We have special code for hairline strokes, miter-strokes, bevel-stroke
434 and fills. Anything else we just call our path code.
435 */
436 bool usePath = doStroke && width > 0 &&
437 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
438 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
439 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700440
441 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000442 usePath = true;
443 }
egdanield58a0ba2014-06-11 10:30:05 -0700444
joshualitt5531d512014-12-17 15:50:11 -0800445 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000446#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
447 if (doStroke) {
448#endif
449 usePath = true;
450#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
451 } else {
joshualitt5531d512014-12-17 15:50:11 -0800452 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000453 }
454#endif
455 }
456 // until we can both stroke and fill rectangles
457 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
458 usePath = true;
459 }
460
egdanield58a0ba2014-06-11 10:30:05 -0700461 GrStrokeInfo strokeInfo(paint);
462
463 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700464 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700465 usePath = true;
466 }
467
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000468 if (usePath) {
469 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700470 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000471 path.addRect(rect);
472 this->drawPath(draw, path, paint, NULL, true);
473 return;
474 }
475
476 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700477 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
478 return;
479 }
Mike Klein744fb732014-06-23 15:13:26 -0400480
joshualitt570d2f82015-02-25 13:19:48 -0800481 fContext->drawRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000482}
483
484///////////////////////////////////////////////////////////////////////////////
485
486void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800487 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700488 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000489 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800490 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000491
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000492 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700493 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
494 return;
495 }
Mike Klein744fb732014-06-23 15:13:26 -0400496
egdanield58a0ba2014-06-11 10:30:05 -0700497 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000498 if (paint.getMaskFilter()) {
499 // try to hit the fast path for drawing filtered round rects
500
501 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800502 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000503 if (devRRect.allCornersCircular()) {
504 SkRect maskRect;
505 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800506 draw.fClip->getBounds(),
507 *draw.fMatrix,
508 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000509 SkIRect finalIRect;
510 maskRect.roundOut(&finalIRect);
511 if (draw.fClip->quickReject(finalIRect)) {
512 // clipped out
513 return;
514 }
joshualitt25d9c152015-02-18 12:29:52 -0800515 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
516 fRenderTarget,
517 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800518 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800519 *draw.fMatrix,
egdanield58a0ba2014-06-11 10:30:05 -0700520 strokeInfo.getStrokeRec(),
521 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000522 return;
523 }
524 }
525
526 }
527 }
528
529 }
530
egdanield58a0ba2014-06-11 10:30:05 -0700531 bool usePath = false;
532
533 if (paint.getMaskFilter()) {
534 usePath = true;
535 } else {
536 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700537 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700538 usePath = true;
539 }
540 }
541
542
543 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000544 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700545 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000546 path.addRRect(rect);
547 this->drawPath(draw, path, paint, NULL, true);
548 return;
549 }
Mike Klein744fb732014-06-23 15:13:26 -0400550
joshualitt570d2f82015-02-25 13:19:48 -0800551 fContext->drawRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000552}
553
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000554void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800555 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000556 SkStrokeRec stroke(paint);
557 if (stroke.isFillStyle()) {
558
559 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800560 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000561
562 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700563 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
564 &grPaint)) {
565 return;
566 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000567
568 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
joshualitt570d2f82015-02-25 13:19:48 -0800569 fContext->drawDRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000570 return;
571 }
572 }
573
574 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700575 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000576 path.addRRect(outer);
577 path.addRRect(inner);
578 path.setFillType(SkPath::kEvenOdd_FillType);
579
580 this->drawPath(draw, path, paint, NULL, true);
581}
582
583
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000584/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000585
586void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
587 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700588 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000589 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800590 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000591
egdanield58a0ba2014-06-11 10:30:05 -0700592 GrStrokeInfo strokeInfo(paint);
593
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000594 bool usePath = false;
595 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700596 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000597 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700598 } else {
599 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700600 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700601 usePath = true;
602 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000603 }
604
605 if (usePath) {
606 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700607 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000608 path.addOval(oval);
609 this->drawPath(draw, path, paint, NULL, true);
610 return;
611 }
612
613 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700614 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
615 return;
616 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000617
joshualitt570d2f82015-02-25 13:19:48 -0800618 fContext->drawOval(fRenderTarget, fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000619}
620
621#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000622
623///////////////////////////////////////////////////////////////////////////////
624
625// helpers for applying mask filters
626namespace {
627
628// Draw a mask using the supplied paint. Since the coverage/geometry
629// is already burnt into the mask this boils down to a rect draw.
630// Return true if the mask was successfully drawn.
joshualitt25d9c152015-02-18 12:29:52 -0800631bool draw_mask(GrContext* context,
632 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800633 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800634 const SkMatrix& viewMatrix,
635 const SkRect& maskRect,
636 GrPaint* grp,
637 GrTexture* mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000638 SkMatrix matrix;
639 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
640 matrix.postIDiv(mask->width(), mask->height());
641
joshualitt16b27892014-12-18 07:47:16 -0800642 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix,
643 kDevice_GrCoordSet))->unref();
644
645 SkMatrix inverse;
646 if (!viewMatrix.invert(&inverse)) {
647 return false;
648 }
joshualitt570d2f82015-02-25 13:19:48 -0800649 context->drawNonAARectWithLocalMatrix(rt, clip, *grp, SkMatrix::I(), maskRect, inverse);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000650 return true;
651}
652
joshualitt5efb8b82015-03-18 11:49:12 -0700653static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
654 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
655}
656
joshualitt25d9c152015-02-18 12:29:52 -0800657bool draw_with_mask_filter(GrContext* context,
658 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800659 const GrClip& clipData,
joshualitt25d9c152015-02-18 12:29:52 -0800660 const SkMatrix& viewMatrix,
661 const SkPath& devPath,
662 SkMaskFilter* filter,
joshualitt5efb8b82015-03-18 11:49:12 -0700663 const SkIRect& clipBounds,
joshualitt25d9c152015-02-18 12:29:52 -0800664 GrPaint* grp,
665 SkPaint::Style style) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000666 SkMask srcM, dstM;
667
joshualitt5efb8b82015-03-18 11:49:12 -0700668 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000669 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
670 return false;
671 }
672 SkAutoMaskFreeImage autoSrc(srcM.fImage);
673
joshualitt5531d512014-12-17 15:50:11 -0800674 if (!filter->filterMask(&dstM, srcM, viewMatrix, NULL)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000675 return false;
676 }
677 // this will free-up dstM when we're done (allocated in filterMask())
678 SkAutoMaskFreeImage autoDst(dstM.fImage);
679
joshualitt5efb8b82015-03-18 11:49:12 -0700680 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000681 return false;
682 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000683
684 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
685 // the current clip (and identity matrix) and GrPaint settings
bsalomonf2703d82014-10-28 14:33:06 -0700686 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000687 desc.fWidth = dstM.fBounds.width();
688 desc.fHeight = dstM.fBounds.height();
689 desc.fConfig = kAlpha_8_GrPixelConfig;
690
bsalomond309e7a2015-04-30 14:18:54 -0700691 SkAutoTUnref<GrTexture> texture(context->textureProvider()->refScratchTexture(
692 desc, GrTextureProvider::kApprox_ScratchTexMatch));
bsalomone3059732014-10-14 11:47:22 -0700693 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000694 return false;
695 }
696 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
697 dstM.fImage, dstM.fRowBytes);
698
699 SkRect maskRect = SkRect::Make(dstM.fBounds);
700
joshualitt570d2f82015-02-25 13:19:48 -0800701 return draw_mask(context, rt, clipData, viewMatrix, maskRect, grp, texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000702}
703
bsalomone3059732014-10-14 11:47:22 -0700704// Create a mask of 'devPath' and place the result in 'mask'.
705GrTexture* create_mask_GPU(GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800706 GrRenderTarget* rt,
bsalomone3059732014-10-14 11:47:22 -0700707 const SkRect& maskRect,
708 const SkPath& devPath,
709 const GrStrokeInfo& strokeInfo,
710 bool doAA,
711 int sampleCnt) {
bsalomonf2703d82014-10-28 14:33:06 -0700712 GrSurfaceDesc desc;
713 desc.fFlags = kRenderTarget_GrSurfaceFlag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000714 desc.fWidth = SkScalarCeilToInt(maskRect.width());
715 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700716 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000717 // We actually only need A8, but it often isn't supported as a
718 // render target so default to RGBA_8888
719 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700720
721 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
722 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000723 desc.fConfig = kAlpha_8_GrPixelConfig;
724 }
725
bsalomond309e7a2015-04-30 14:18:54 -0700726 GrTexture* mask = context->textureProvider()->refScratchTexture(
727 desc, GrTextureProvider::kApprox_ScratchTexMatch);
bsalomone3059732014-10-14 11:47:22 -0700728 if (NULL == mask) {
729 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000730 }
731
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000732 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
733
bsalomon89c62982014-11-03 12:08:42 -0800734 context->clear(NULL, 0x0, true, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000735
736 GrPaint tempPaint;
egdanielb197b8f2015-02-17 07:34:43 -0800737 tempPaint.setAntiAlias(doAA);
738 tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000739
joshualitt570d2f82015-02-25 13:19:48 -0800740 // setup new clip
741 GrClip clip(clipRect);
742
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000743 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
744 SkMatrix translate;
745 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
joshualitt570d2f82015-02-25 13:19:48 -0800746 context->drawPath(mask->asRenderTarget(), clip, tempPaint, translate, devPath, strokeInfo);
bsalomone3059732014-10-14 11:47:22 -0700747 return mask;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000748}
749
senorblancod0d37ca2015-04-02 04:54:56 -0700750SkBitmap wrap_texture(GrTexture* texture, int width, int height) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000751 SkBitmap result;
senorblancod0d37ca2015-04-02 04:54:56 -0700752 result.setInfo(SkImageInfo::MakeN32Premul(width, height));
reed6c225732014-06-09 19:52:07 -0700753 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000754 return result;
755}
756
757};
758
759void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
760 const SkPaint& paint, const SkMatrix* prePathMatrix,
761 bool pathIsMutable) {
762 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800763 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700764 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000765
joshualitt5efb8b82015-03-18 11:49:12 -0700766 return this->internalDrawPath(origSrcPath, paint, *draw.fMatrix, prePathMatrix,
767 draw.fClip->getBounds(), pathIsMutable);
768}
769
770void SkGpuDevice::internalDrawPath(const SkPath& origSrcPath, const SkPaint& paint,
771 const SkMatrix& origViewMatrix, const SkMatrix* prePathMatrix,
772 const SkIRect& clipBounds, bool pathIsMutable) {
jvanverthb3eb6872014-10-24 07:12:51 -0700773 SkASSERT(!pathIsMutable || origSrcPath.isVolatile());
joshualitt5531d512014-12-17 15:50:11 -0800774
bsalomon54443932015-01-29 09:34:18 -0800775 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000776
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000777 // If we have a prematrix, apply it to the path, optimizing for the case
778 // where the original path can in fact be modified in place (even though
779 // its parameter type is const).
780 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000781 SkTLazy<SkPath> tmpPath;
782 SkTLazy<SkPath> effectPath;
bsalomon54443932015-01-29 09:34:18 -0800783 SkPathEffect* pathEffect = paint.getPathEffect();
784
joshualitt5efb8b82015-03-18 11:49:12 -0700785 SkMatrix viewMatrix = origViewMatrix;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786
787 if (prePathMatrix) {
bsalomon54443932015-01-29 09:34:18 -0800788 // stroking and path effects are supposed to be applied *after* the prePathMatrix.
789 // The pre-path-matrix also should not affect shadeing.
790 if (NULL == pathEffect && NULL == paint.getShader() &&
791 (strokeInfo.getStrokeRec().isFillStyle() ||
792 strokeInfo.getStrokeRec().isHairlineStyle())) {
793 viewMatrix.preConcat(*prePathMatrix);
794 } else {
795 SkPath* result = pathPtr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000796
bsalomon54443932015-01-29 09:34:18 -0800797 if (!pathIsMutable) {
798 result = tmpPath.init();
799 result->setIsVolatile(true);
800 pathIsMutable = true;
801 }
802 // should I push prePathMatrix on our MV stack temporarily, instead
803 // of applying it here? See SkDraw.cpp
804 pathPtr->transform(*prePathMatrix, result);
805 pathPtr = result;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000806 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000807 }
808 // at this point we're done with prePathMatrix
809 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
810
bsalomon54443932015-01-29 09:34:18 -0800811 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700812 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, viewMatrix, true, &grPaint)) {
813 return;
814 }
bsalomon54443932015-01-29 09:34:18 -0800815
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000816 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700817 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
kkinnunen280a9c82015-04-28 22:19:25 -0700818 if (!strokeInfo.isDashed() && pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr,
819 strokePtr, cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000820 pathPtr = effectPath.get();
821 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000822 }
823
egdanield58a0ba2014-06-11 10:30:05 -0700824 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000825 if (paint.getMaskFilter()) {
826 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000827 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
kkinnunen280a9c82015-04-28 22:19:25 -0700828 if (strokeInfo.isDashed()) {
829 if (pathEffect->filterPath(strokedPath, *pathPtr, strokePtr, cullRect)) {
830 pathPtr = strokedPath;
831 pathIsMutable = true;
832 }
833 strokeInfo.removeDash();
834 }
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000835 if (stroke.applyToPath(strokedPath, *pathPtr)) {
836 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000837 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700838 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000839 }
840 }
841
842 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000843 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700844 if (!pathIsMutable) {
845 devPathPtr->setIsVolatile(true);
846 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000847
848 // transform the path into device space
bsalomon54443932015-01-29 09:34:18 -0800849 pathPtr->transform(viewMatrix, devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000850
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000851 SkRect maskRect;
852 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
joshualitt5efb8b82015-03-18 11:49:12 -0700853 clipBounds,
bsalomon54443932015-01-29 09:34:18 -0800854 viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000855 &maskRect)) {
856 SkIRect finalIRect;
857 maskRect.roundOut(&finalIRect);
joshualitt5efb8b82015-03-18 11:49:12 -0700858 if (clip_bounds_quick_reject(clipBounds, finalIRect)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000859 // clipped out
860 return;
861 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000862
joshualitt25d9c152015-02-18 12:29:52 -0800863 if (paint.getMaskFilter()->directFilterMaskGPU(fContext,
864 fRenderTarget,
865 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800866 fClip,
joshualitt25d9c152015-02-18 12:29:52 -0800867 viewMatrix,
868 stroke,
869 *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000870 // the mask filter was able to draw itself directly, so there's nothing
871 // left to do.
872 return;
873 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000874
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000875
joshualitt25d9c152015-02-18 12:29:52 -0800876 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext,
877 fRenderTarget,
878 maskRect,
879 *devPathPtr,
880 strokeInfo,
881 grPaint.isAntiAlias(),
bsalomone3059732014-10-14 11:47:22 -0700882 fRenderTarget->numSamples()));
883 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000884 GrTexture* filtered;
885
bsalomon54443932015-01-29 09:34:18 -0800886 if (paint.getMaskFilter()->filterMaskGPU(mask, viewMatrix, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000887 // filterMaskGPU gives us ownership of a ref to the result
888 SkAutoTUnref<GrTexture> atu(filtered);
joshualitt570d2f82015-02-25 13:19:48 -0800889 if (draw_mask(fContext,
890 fRenderTarget,
891 fClip,
892 viewMatrix,
893 maskRect,
894 &grPaint,
joshualitt25d9c152015-02-18 12:29:52 -0800895 filtered)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000896 // This path is completely drawn
897 return;
898 }
899 }
900 }
901 }
902
903 // draw the mask on the CPU - this is a fallthrough path in case the
904 // GPU path fails
905 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
906 SkPaint::kFill_Style;
joshualitt570d2f82015-02-25 13:19:48 -0800907 draw_with_mask_filter(fContext, fRenderTarget, fClip, viewMatrix, *devPathPtr,
joshualitt5efb8b82015-03-18 11:49:12 -0700908 paint.getMaskFilter(), clipBounds, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000909 return;
910 }
911
joshualitt570d2f82015-02-25 13:19:48 -0800912 fContext->drawPath(fRenderTarget, fClip, grPaint, viewMatrix, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000913}
914
915static const int kBmpSmallTileSize = 1 << 10;
916
917static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
918 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
919 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
920 return tilesX * tilesY;
921}
922
923static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
924 if (maxTileSize <= kBmpSmallTileSize) {
925 return maxTileSize;
926 }
927
928 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
929 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
930
931 maxTileTotalTileSize *= maxTileSize * maxTileSize;
932 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
933
934 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
935 return kBmpSmallTileSize;
936 } else {
937 return maxTileSize;
938 }
939}
940
941// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
942// pixels from the bitmap are necessary.
943static void determine_clipped_src_rect(const GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800944 const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800945 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800946 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000947 const SkBitmap& bitmap,
948 const SkRect* srcRectPtr,
949 SkIRect* clippedSrcIRect) {
joshualitt570d2f82015-02-25 13:19:48 -0800950 clip.getConservativeBounds(rt, clippedSrcIRect, NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000951 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800952 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000953 clippedSrcIRect->setEmpty();
954 return;
955 }
956 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
957 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700958 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000959 // we've setup src space 0,0 to map to the top left of the src rect.
960 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000961 if (!clippedSrcRect.intersect(*srcRectPtr)) {
962 clippedSrcIRect->setEmpty();
963 return;
964 }
965 }
966 clippedSrcRect.roundOut(clippedSrcIRect);
967 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
968 if (!clippedSrcIRect->intersect(bmpBounds)) {
969 clippedSrcIRect->setEmpty();
970 }
971}
972
973bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800974 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000975 const GrTextureParams& params,
976 const SkRect* srcRectPtr,
977 int maxTileSize,
978 int* tileSize,
979 SkIRect* clippedSrcRect) const {
980 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700981 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982 return false;
983 }
984
985 // if it's larger than the max tile size, then we have no choice but tiling.
986 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt570d2f82015-02-25 13:19:48 -0800987 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap,
988 srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000989 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
990 return true;
991 }
992
993 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
994 return false;
995 }
996
997 // if the entire texture is already in our cache then no reason to tile it
998 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
999 return false;
1000 }
1001
1002 // At this point we know we could do the draw by uploading the entire bitmap
1003 // as a texture. However, if the texture would be large compared to the
1004 // cache size and we don't require most of it for this draw then tile to
1005 // reduce the amount of upload and cache spill.
1006
1007 // assumption here is that sw bitmap size is a good proxy for its size as
1008 // a texture
1009 size_t bmpSize = bitmap.getSize();
1010 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001011 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001012 if (bmpSize < cacheSize / 2) {
1013 return false;
1014 }
1015
1016 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt570d2f82015-02-25 13:19:48 -08001017 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap, srcRectPtr,
joshualitt25d9c152015-02-18 12:29:52 -08001018 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001019 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1020 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
1021 kBmpSmallTileSize * kBmpSmallTileSize;
1022
1023 return usedTileBytes < 2 * bmpSize;
1024}
1025
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001026void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001027 const SkBitmap& bitmap,
1028 const SkMatrix& m,
1029 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001030 SkMatrix concat;
1031 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1032 if (!m.isIdentity()) {
1033 concat.setConcat(*draw->fMatrix, m);
1034 draw.writable()->fMatrix = &concat;
1035 }
1036 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001037}
1038
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001039// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001040// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1041// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001042static inline void clamped_outset_with_offset(SkIRect* iRect,
1043 int outset,
1044 SkPoint* offset,
1045 const SkIRect& clamp) {
1046 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001047
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001048 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1049 if (leftClampDelta > 0) {
1050 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001051 iRect->fLeft = clamp.fLeft;
1052 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001053 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001054 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001055
1056 int topClampDelta = clamp.fTop - iRect->fTop;
1057 if (topClampDelta > 0) {
1058 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001059 iRect->fTop = clamp.fTop;
1060 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001061 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001062 }
1063
1064 if (iRect->fRight > clamp.fRight) {
1065 iRect->fRight = clamp.fRight;
1066 }
1067 if (iRect->fBottom > clamp.fBottom) {
1068 iRect->fBottom = clamp.fBottom;
1069 }
1070}
1071
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001072static bool has_aligned_samples(const SkRect& srcRect,
1073 const SkRect& transformedRect) {
1074 // detect pixel disalignment
1075 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1076 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1077 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1078 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1079 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1080 COLOR_BLEED_TOLERANCE &&
1081 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1082 COLOR_BLEED_TOLERANCE) {
1083 return true;
1084 }
1085 return false;
1086}
1087
1088static bool may_color_bleed(const SkRect& srcRect,
1089 const SkRect& transformedRect,
1090 const SkMatrix& m) {
1091 // Only gets called if has_aligned_samples returned false.
1092 // So we can assume that sampling is axis aligned but not texel aligned.
1093 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1094 SkRect innerSrcRect(srcRect), innerTransformedRect,
1095 outerTransformedRect(transformedRect);
1096 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1097 m.mapRect(&innerTransformedRect, innerSrcRect);
1098
1099 // The gap between outerTransformedRect and innerTransformedRect
1100 // represents the projection of the source border area, which is
1101 // problematic for color bleeding. We must check whether any
1102 // destination pixels sample the border area.
1103 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1104 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1105 SkIRect outer, inner;
1106 outerTransformedRect.round(&outer);
1107 innerTransformedRect.round(&inner);
1108 // If the inner and outer rects round to the same result, it means the
1109 // border does not overlap any pixel centers. Yay!
1110 return inner != outer;
1111}
1112
1113static bool needs_texture_domain(const SkBitmap& bitmap,
1114 const SkRect& srcRect,
1115 GrTextureParams &params,
1116 const SkMatrix& contextMatrix,
1117 bool bicubic) {
1118 bool needsTextureDomain = false;
senorblancod0d37ca2015-04-02 04:54:56 -07001119 GrTexture* tex = bitmap.getTexture();
1120 int width = tex ? tex->width() : bitmap.width();
1121 int height = tex ? tex->height() : bitmap.height();
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001122
1123 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1124 // Need texture domain if drawing a sub rect
senorblancod0d37ca2015-04-02 04:54:56 -07001125 needsTextureDomain = srcRect.width() < width ||
1126 srcRect.height() < height;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001127 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1128 // sampling is axis-aligned
1129 SkRect transformedRect;
1130 contextMatrix.mapRect(&transformedRect, srcRect);
1131
1132 if (has_aligned_samples(srcRect, transformedRect)) {
1133 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1134 needsTextureDomain = false;
1135 } else {
1136 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1137 }
1138 }
1139 }
1140 return needsTextureDomain;
1141}
1142
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001143void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1144 const SkBitmap& bitmap,
1145 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001146 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001147 const SkPaint& paint,
1148 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -08001149 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001150
1151 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001152 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001153 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1154 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001155 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001156 SkScalar w = SkIntToScalar(bitmap.width());
1157 SkScalar h = SkIntToScalar(bitmap.height());
1158 dstSize.fWidth = w;
1159 dstSize.fHeight = h;
1160 srcRect.set(0, 0, w, h);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001161 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001162 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001163 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001164 dstSize = *dstSizePtr;
senorblancod0d37ca2015-04-02 04:54:56 -07001165 }
1166 GrTexture* tex = bitmap.getTexture();
1167 int width = tex ? tex->width() : bitmap.width();
1168 int height = tex ? tex->height() : bitmap.height();
1169 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1170 srcRect.fRight >= width && srcRect.fBottom >= height) {
1171 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001172 }
1173
derekf367e1862014-12-02 11:02:06 -08001174 // If the render target is not msaa and draw is antialiased, we call
1175 // drawRect instead of drawing on the render target directly.
1176 // FIXME: the tiled bitmap code path doesn't currently support
1177 // anti-aliased edges, we work around that for now by drawing directly
1178 // if the image size exceeds maximum texture size.
1179 int maxTextureSize = fContext->getMaxTextureSize();
1180 bool directDraw = fRenderTarget->isMultisampled() ||
1181 !paint.isAntiAlias() ||
1182 bitmap.width() > maxTextureSize ||
1183 bitmap.height() > maxTextureSize;
1184
1185 // we check whether dst rect are pixel aligned
1186 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -08001187 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -08001188
1189 if (staysRect) {
1190 SkRect rect;
1191 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -08001192 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -08001193 const SkScalar *scalars = rect.asScalars();
1194 bool isDstPixelAligned = true;
1195 for (int i = 0; i < 4; i++) {
1196 if (!SkScalarIsInt(scalars[i])) {
1197 isDstPixelAligned = false;
1198 break;
1199 }
1200 }
1201
1202 if (isDstPixelAligned)
1203 directDraw = true;
1204 }
1205 }
1206
1207 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001208 // Convert the bitmap to a shader so that the rect can be drawn
1209 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001210 SkBitmap tmp; // subset of bitmap, if necessary
1211 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001212 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001213 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001214 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1215 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1216 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001217 // In bleed mode we position and trim the bitmap based on the src rect which is
1218 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1219 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1220 // compensate.
1221 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1222 SkIRect iSrc;
1223 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001224
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001225 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1226 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001227
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001228 if (!bitmap.extractSubset(&tmp, iSrc)) {
1229 return; // extraction failed
1230 }
1231 bitmapPtr = &tmp;
1232 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001233
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001234 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001235 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001236 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001237 } else {
1238 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001239 }
1240
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001241 SkPaint paintWithShader(paint);
1242 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001243 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001244 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1245 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001246
1247 return;
1248 }
1249
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001250 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1251 // the view matrix rather than a local matrix.
1252 SkMatrix m;
1253 m.setScale(dstSize.fWidth / srcRect.width(),
1254 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -08001255 SkMatrix viewM = *draw.fMatrix;
1256 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001257
1258 GrTextureParams params;
reed93a12152015-03-16 10:08:34 -07001259 SkFilterQuality paintFilterQuality = paint.getFilterQuality();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001260 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001261
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001262 bool doBicubic = false;
1263
reed93a12152015-03-16 10:08:34 -07001264 switch(paintFilterQuality) {
1265 case kNone_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001266 textureFilterMode = GrTextureParams::kNone_FilterMode;
1267 break;
reed93a12152015-03-16 10:08:34 -07001268 case kLow_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001269 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1270 break;
reed93a12152015-03-16 10:08:34 -07001271 case kMedium_SkFilterQuality:
joshualitt5531d512014-12-17 15:50:11 -08001272 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001273 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1274 } else {
1275 // Don't trigger MIP level generation unnecessarily.
1276 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1277 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001278 break;
reed93a12152015-03-16 10:08:34 -07001279 case kHigh_SkFilterQuality:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001280 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001281 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001282 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001283 break;
1284 default:
1285 SkErrorInternals::SetError( kInvalidPaint_SkError,
1286 "Sorry, I don't understand the filtering "
1287 "mode you asked for. Falling back to "
1288 "MIPMaps.");
1289 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1290 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001291 }
1292
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001293 int tileFilterPad;
1294 if (doBicubic) {
1295 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1296 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1297 tileFilterPad = 0;
1298 } else {
1299 tileFilterPad = 1;
1300 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001301 params.setFilterMode(textureFilterMode);
1302
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001303 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001304 int tileSize;
1305
1306 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001307 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001308 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001309 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1310 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001311 } else {
1312 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001313 bool needsTextureDomain = needs_texture_domain(bitmap,
1314 srcRect,
1315 params,
joshualitt5531d512014-12-17 15:50:11 -08001316 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001317 doBicubic);
1318 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001319 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001320 srcRect,
1321 params,
1322 paint,
1323 flags,
1324 doBicubic,
1325 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001326 }
1327}
1328
1329// Break 'bitmap' into several tiles to draw it since it has already
1330// been determined to be too large to fit in VRAM
1331void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001332 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001333 const SkRect& srcRect,
1334 const SkIRect& clippedSrcIRect,
1335 const GrTextureParams& params,
1336 const SkPaint& paint,
1337 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001338 int tileSize,
1339 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001340 // The following pixel lock is technically redundant, but it is desirable
1341 // to lock outside of the tile loop to prevent redecoding the whole image
1342 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1343 // is larger than the limit of the discardable memory pool.
1344 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001345 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1346
1347 int nx = bitmap.width() / tileSize;
1348 int ny = bitmap.height() / tileSize;
1349 for (int x = 0; x <= nx; x++) {
1350 for (int y = 0; y <= ny; y++) {
1351 SkRect tileR;
1352 tileR.set(SkIntToScalar(x * tileSize),
1353 SkIntToScalar(y * tileSize),
1354 SkIntToScalar((x + 1) * tileSize),
1355 SkIntToScalar((y + 1) * tileSize));
1356
1357 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1358 continue;
1359 }
1360
1361 if (!tileR.intersect(srcRect)) {
1362 continue;
1363 }
1364
1365 SkBitmap tmpB;
1366 SkIRect iTileR;
1367 tileR.roundOut(&iTileR);
1368 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1369 SkIntToScalar(iTileR.fTop));
1370
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001371 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001372 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001373 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001374 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001375 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001376
robertphillipsec8bb942014-11-21 10:16:25 -08001377 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001378 SkIRect iClampRect;
1379
1380 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1381 // In bleed mode we want to always expand the tile on all edges
1382 // but stay within the bitmap bounds
1383 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1384 } else {
1385 // In texture-domain/clamp mode we only want to expand the
1386 // tile on edges interior to "srcRect" (i.e., we want to
1387 // not bleed across the original clamped edges)
1388 srcRect.roundOut(&iClampRect);
1389 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001390 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1391 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001392 }
1393
1394 if (bitmap.extractSubset(&tmpB, iTileR)) {
1395 // now offset it to make it "local" to our tmp bitmap
1396 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001397 GrTextureParams paramsTemp = params;
1398 bool needsTextureDomain = needs_texture_domain(bitmap,
1399 srcRect,
1400 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001401 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001402 bicubic);
1403 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001404 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001405 tileR,
1406 paramsTemp,
1407 paint,
1408 flags,
1409 bicubic,
1410 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001411 }
1412 }
1413 }
1414}
1415
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001416
1417/*
1418 * This is called by drawBitmap(), which has to handle images that may be too
1419 * large to be represented by a single texture.
1420 *
1421 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1422 * and that non-texture portion of the GrPaint has already been setup.
1423 */
1424void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001425 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001426 const SkRect& srcRect,
1427 const GrTextureParams& params,
1428 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001429 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001430 bool bicubic,
1431 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001432 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1433 bitmap.height() <= fContext->getMaxTextureSize());
1434
1435 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001436 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001437 if (NULL == texture) {
1438 return;
1439 }
1440
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001441 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001442 SkRect paintRect;
1443 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1444 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1445 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1446 SkScalarMul(srcRect.fTop, hInv),
1447 SkScalarMul(srcRect.fRight, wInv),
1448 SkScalarMul(srcRect.fBottom, hInv));
1449
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001450 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001451 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001452 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001453 // Use a constrained texture domain to avoid color bleeding
1454 SkScalar left, top, right, bottom;
1455 if (srcRect.width() > SK_Scalar1) {
1456 SkScalar border = SK_ScalarHalf / texture->width();
1457 left = paintRect.left() + border;
1458 right = paintRect.right() - border;
1459 } else {
1460 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1461 }
1462 if (srcRect.height() > SK_Scalar1) {
1463 SkScalar border = SK_ScalarHalf / texture->height();
1464 top = paintRect.top() + border;
1465 bottom = paintRect.bottom() - border;
1466 } else {
1467 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1468 }
1469 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001470 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001471 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001472 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001473 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001474 SkMatrix::I(),
1475 textureDomain,
1476 GrTextureDomain::kClamp_Mode,
1477 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001478 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001479 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001480 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1481 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001482 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001483 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001484 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001485 }
1486
1487 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1488 // the rest from the SkPaint.
1489 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001490 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001491 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001492 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1493 SkColor2GrColor(paint.getColor());
bsalomonbed83a62015-04-15 14:18:34 -07001494 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false,
1495 &grPaint)) {
1496 return;
1497 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001498
joshualitt570d2f82015-02-25 13:19:48 -08001499 fContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dstRect,
1500 paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001501}
1502
fmalita2d97bc12014-11-20 10:44:58 -08001503bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001504 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001505 const SkImageFilter* filter,
1506 const SkImageFilter::Context& ctx,
1507 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001508 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001509
1510 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1511 // (https://code.google.com/p/skia/issues/detail?id=3148).
1512 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001513
1514 if (filter->canFilterImageGPU()) {
senorblancod0d37ca2015-04-02 04:54:56 -07001515 return filter->filterImageGPU(&proxy, wrap_texture(texture, width, height),
1516 ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001517 } else {
1518 return false;
1519 }
1520}
1521
1522void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1523 int left, int top, const SkPaint& paint) {
1524 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001525 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001526
1527 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1528 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1529 return;
1530 }
1531
1532 int w = bitmap.width();
1533 int h = bitmap.height();
1534
1535 GrTexture* texture;
1536 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001537 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001538 if (!texture) {
1539 return;
1540 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001541
1542 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001543 // This bitmap will own the filtered result as a texture.
1544 SkBitmap filteredBitmap;
1545
bsalomon49f085d2014-09-05 13:34:00 -07001546 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001547 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001548 SkMatrix matrix(*draw.fMatrix);
1549 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001550 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001551 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001552 // This cache is transient, and is freed (along with all its contained
1553 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001554 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001555 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001556 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001557 texture = (GrTexture*) filteredBitmap.getTexture();
1558 w = filteredBitmap.width();
1559 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001560 left += offset.x();
1561 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001562 } else {
1563 return;
1564 }
1565 }
1566
1567 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001568 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001569
bsalomonbed83a62015-04-15 14:18:34 -07001570 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1571 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1572 return;
1573 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001574
joshualitt25d9c152015-02-18 12:29:52 -08001575 fContext->drawNonAARectToRect(fRenderTarget,
joshualitt570d2f82015-02-25 13:19:48 -08001576 fClip,
joshualitt25d9c152015-02-18 12:29:52 -08001577 grPaint,
joshualitt16b27892014-12-18 07:47:16 -08001578 SkMatrix::I(),
1579 SkRect::MakeXYWH(SkIntToScalar(left),
1580 SkIntToScalar(top),
1581 SkIntToScalar(w),
1582 SkIntToScalar(h)),
1583 SkRect::MakeXYWH(0,
1584 0,
1585 SK_Scalar1 * w / texture->width(),
1586 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001587}
1588
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001589void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001590 const SkRect* src, const SkRect& dst,
1591 const SkPaint& paint,
1592 SkCanvas::DrawBitmapRectFlags flags) {
1593 SkMatrix matrix;
1594 SkRect bitmapBounds, tmpSrc;
1595
1596 bitmapBounds.set(0, 0,
1597 SkIntToScalar(bitmap.width()),
1598 SkIntToScalar(bitmap.height()));
1599
1600 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001601 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001602 tmpSrc = *src;
1603 } else {
1604 tmpSrc = bitmapBounds;
1605 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001606
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001607 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1608
1609 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001610 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001611 if (!bitmapBounds.contains(tmpSrc)) {
1612 if (!tmpSrc.intersect(bitmapBounds)) {
1613 return; // nothing to draw
1614 }
1615 }
1616 }
1617
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001618 SkRect tmpDst;
1619 matrix.mapRect(&tmpDst, tmpSrc);
1620
1621 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1622 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1623 // Translate so that tempDst's top left is at the origin.
1624 matrix = *origDraw.fMatrix;
1625 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1626 draw.writable()->fMatrix = &matrix;
1627 }
1628 SkSize dstSize;
1629 dstSize.fWidth = tmpDst.width();
1630 dstSize.fHeight = tmpDst.height();
1631
1632 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001633}
1634
1635void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1636 int x, int y, const SkPaint& paint) {
1637 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001638 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001639 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001640
1641 // TODO: If the source device covers the whole of this device, we could
1642 // omit fNeedsClear -related flushing.
1643 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001644
1645 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001646 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001647
1648 GrRenderTarget* devRT = dev->accessRenderTarget();
1649 GrTexture* devTex;
1650 if (NULL == (devTex = devRT->asTexture())) {
1651 return;
1652 }
1653
robertphillips7b9e8a42014-12-11 08:20:31 -08001654 const SkImageInfo ii = dev->imageInfo();
1655 int w = ii.width();
1656 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001657
1658 SkImageFilter* filter = paint.getImageFilter();
1659 // This bitmap will own the filtered result as a texture.
1660 SkBitmap filteredBitmap;
1661
bsalomon49f085d2014-09-05 13:34:00 -07001662 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001663 SkIPoint offset = SkIPoint::Make(0, 0);
1664 SkMatrix matrix(*draw.fMatrix);
1665 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001666 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001667 // This cache is transient, and is freed (along with all its contained
1668 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001669 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001670 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001671 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1672 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001673 devTex = filteredBitmap.getTexture();
1674 w = filteredBitmap.width();
1675 h = filteredBitmap.height();
1676 x += offset.fX;
1677 y += offset.fY;
1678 } else {
1679 return;
1680 }
1681 }
1682
1683 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001684 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001685
bsalomonbed83a62015-04-15 14:18:34 -07001686 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1687 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1688 return;
1689 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001690
1691 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1692 SkIntToScalar(y),
1693 SkIntToScalar(w),
1694 SkIntToScalar(h));
1695
1696 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1697 // scratch texture).
1698 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1699 SK_Scalar1 * h / devTex->height());
1700
joshualitt570d2f82015-02-25 13:19:48 -08001701 fContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, SkMatrix::I(), dstRect,
1702 srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001703}
1704
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001705bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001706 return filter->canFilterImageGPU();
1707}
1708
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001709bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001710 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001711 SkBitmap* result, SkIPoint* offset) {
1712 // want explicitly our impl, so guard against a subclass of us overriding it
1713 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1714 return false;
1715 }
1716
1717 SkAutoLockPixels alp(src, !src.getTexture());
1718 if (!src.getTexture() && !src.readyToDraw()) {
1719 return false;
1720 }
1721
1722 GrTexture* texture;
1723 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1724 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001725 AutoBitmapTexture abt(fContext, src, NULL, &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001726 if (!texture) {
1727 return false;
1728 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001729
senorblancod0d37ca2015-04-02 04:54:56 -07001730 return this->filterTexture(fContext, texture, src.width(), src.height(),
1731 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001732}
1733
reed620ba3a2015-05-06 07:54:38 -07001734static SkImageInfo make_info(GrTexture* tex, int w, int h, bool isOpaque) {
1735 const GrPixelConfig config = tex->config();
1736 SkColorType ct;
1737 SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
1738 if (!GrPixelConfig2ColorAndProfileType(config, &ct, NULL)) {
1739 ct = kUnknown_SkColorType;
1740 }
1741 return SkImageInfo::Make(w, h, ct, at);
1742}
1743
1744static bool wrap_as_bm(const SkImage* image, SkBitmap* bm) {
1745 GrTexture* tex = image->getTexture();
1746 if (tex) {
1747 // TODO: handle the GrTexture directly, and skip GrPixelRef
1748 const SkImageInfo info = make_info(tex, image->width(), image->height(), image->isOpaque());
1749 bm->setInfo(info);
1750 bm->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, tex)))->unref();
1751 } else {
1752 if (!as_IB(image)->getROPixels(bm)) {
1753 return false;
1754 }
1755 }
1756 return true;
1757}
1758
1759void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1760 const SkPaint& paint) {
1761 SkBitmap bm;
1762 if (wrap_as_bm(image, &bm)) {
1763 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1764 }
1765}
1766
1767void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
1768 const SkRect& dst, const SkPaint& paint) {
1769 SkBitmap bm;
1770 if (wrap_as_bm(image, &bm)) {
1771 this->drawBitmapRect(draw, bm, src, dst, paint, SkCanvas::kNone_DrawBitmapRectFlag);
1772 }
1773}
1774
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001775///////////////////////////////////////////////////////////////////////////////
1776
1777// must be in SkCanvas::VertexMode order
1778static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1779 kTriangles_GrPrimitiveType,
1780 kTriangleStrip_GrPrimitiveType,
1781 kTriangleFan_GrPrimitiveType,
1782};
1783
1784void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1785 int vertexCount, const SkPoint vertices[],
1786 const SkPoint texs[], const SkColor colors[],
1787 SkXfermode* xmode,
1788 const uint16_t indices[], int indexCount,
1789 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001790 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001791 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001792
dandov32a311b2014-07-15 19:46:26 -07001793 const uint16_t* outIndices;
1794 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1795 GrPrimitiveType primType;
1796 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001797
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001798 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1799 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001800
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001801 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001802
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001803 SkPaint copy(paint);
1804 copy.setStyle(SkPaint::kStroke_Style);
1805 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001806
dandov32a311b2014-07-15 19:46:26 -07001807 // we ignore the shader if texs is null.
bsalomonbed83a62015-04-15 14:18:34 -07001808 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy,
1809 SkColor2GrColor(copy.getColor()), NULL == colors, &grPaint)) {
1810 return;
1811 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001812
dandov32a311b2014-07-15 19:46:26 -07001813 primType = kLines_GrPrimitiveType;
1814 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001815 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001816 switch (vmode) {
1817 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001818 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001819 break;
1820 case SkCanvas::kTriangleStrip_VertexMode:
1821 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001822 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001823 break;
1824 }
mtklein533eb782014-08-27 10:39:42 -07001825
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001826 VertState state(vertexCount, indices, indexCount);
1827 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001828
dandov32a311b2014-07-15 19:46:26 -07001829 //number of indices for lines per triangle with kLines
1830 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001831
dandov32a311b2014-07-15 19:46:26 -07001832 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1833 outIndices = outAlloc.get();
1834 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001835 int i = 0;
1836 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001837 auxIndices[i] = state.f0;
1838 auxIndices[i + 1] = state.f1;
1839 auxIndices[i + 2] = state.f1;
1840 auxIndices[i + 3] = state.f2;
1841 auxIndices[i + 4] = state.f2;
1842 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001843 i += 6;
1844 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001845 } else {
dandov32a311b2014-07-15 19:46:26 -07001846 outIndices = indices;
1847 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001848
dandov32a311b2014-07-15 19:46:26 -07001849 if (NULL == texs || NULL == paint.getShader()) {
bsalomonbed83a62015-04-15 14:18:34 -07001850 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1851 SkColor2GrColor(paint.getColor()),
1852 NULL == colors, &grPaint)) {
1853 return;
1854 }
dandov32a311b2014-07-15 19:46:26 -07001855 } else {
bsalomonbed83a62015-04-15 14:18:34 -07001856 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix,
1857 NULL == colors, &grPaint)) {
1858 return;
1859 }
dandov32a311b2014-07-15 19:46:26 -07001860 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001861 }
1862
mtklein2583b622014-06-04 08:20:41 -07001863#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001864 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001865 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1866 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001867 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001868 }
1869 }
mtklein2583b622014-06-04 08:20:41 -07001870#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001871
1872 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001873 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001874 // need to convert byte order and from non-PM to PM
1875 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001876 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001877 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001878 color = colors[i];
1879 if (paint.getAlpha() != 255) {
1880 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1881 }
1882 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001883 }
1884 colors = convertedColors.get();
1885 }
joshualitt25d9c152015-02-18 12:29:52 -08001886 fContext->drawVertices(fRenderTarget,
joshualitt570d2f82015-02-25 13:19:48 -08001887 fClip,
joshualitt25d9c152015-02-18 12:29:52 -08001888 grPaint,
joshualitt5531d512014-12-17 15:50:11 -08001889 *draw.fMatrix,
dandov32a311b2014-07-15 19:46:26 -07001890 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001891 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001892 vertices,
1893 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001894 colors,
dandov32a311b2014-07-15 19:46:26 -07001895 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001896 indexCount);
1897}
1898
1899///////////////////////////////////////////////////////////////////////////////
1900
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001901void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001902 size_t byteLength, SkScalar x, SkScalar y,
1903 const SkPaint& paint) {
1904 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001905 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001906
jvanverth8c27a182014-10-14 08:45:50 -07001907 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001908 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1909 return;
1910 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001911
jvanverth8c27a182014-10-14 08:45:50 -07001912 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001913
joshualitt6e8cd962015-03-20 10:30:14 -07001914 fTextContext->drawText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
1915 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001916}
1917
fmalita05c4a432014-09-29 06:29:53 -07001918void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1919 const SkScalar pos[], int scalarsPerPos,
1920 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001921 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001922 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001923
jvanverth8c27a182014-10-14 08:45:50 -07001924 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001925 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1926 return;
1927 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001928
jvanverth8c27a182014-10-14 08:45:50 -07001929 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001930
joshualitt6e8cd962015-03-20 10:30:14 -07001931 fTextContext->drawPosText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
1932 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1933 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001934}
1935
joshualitt9c328182015-03-23 08:13:04 -07001936void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1937 const SkPaint& paint, SkDrawFilter* drawFilter) {
1938 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawTextBlob", fContext);
1939 CHECK_SHOULD_DRAW(draw);
1940
1941 SkDEBUGCODE(this->validate();)
1942
1943 fTextContext->drawTextBlob(fRenderTarget, fClip, paint, *draw.fMatrix, blob, x, y, drawFilter,
1944 draw.fClip->getBounds());
1945}
1946
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001947///////////////////////////////////////////////////////////////////////////////
1948
reedb2db8982014-11-13 12:41:02 -08001949bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001950 if (paint.getShader() ||
reedb2db8982014-11-13 12:41:02 -08001951 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001952 paint.getMaskFilter() ||
1953 paint.getRasterizer() ||
1954 paint.getColorFilter() ||
1955 paint.getPathEffect() ||
1956 paint.isFakeBoldText() ||
reedb2db8982014-11-13 12:41:02 -08001957 paint.getStyle() != SkPaint::kFill_Style)
1958 {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001959 return true;
1960 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001961 return false;
1962}
1963
1964void SkGpuDevice::flush() {
1965 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001966 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001967}
1968
1969///////////////////////////////////////////////////////////////////////////////
1970
reed76033be2015-03-14 10:54:31 -07001971SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
bsalomonf2703d82014-10-28 14:33:06 -07001972 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001973 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001974 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001975 desc.fWidth = cinfo.fInfo.width();
1976 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001977 desc.fSampleCnt = fRenderTarget->numSamples();
1978
1979 SkAutoTUnref<GrTexture> texture;
1980 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001981 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001982
hcm4396fa52014-10-27 21:43:30 -07001983 // layers are never draw in repeat modes, so we can request an approx
1984 // match and ignore any padding.
bsalomond309e7a2015-04-30 14:18:54 -07001985 const GrTextureProvider::ScratchTexMatch match = (kNever_TileUsage == cinfo.fTileUsage) ?
1986 GrTextureProvider::kApprox_ScratchTexMatch :
1987 GrTextureProvider::kExact_ScratchTexMatch;
1988 texture.reset(fContext->textureProvider()->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001989
1990 if (texture) {
1991 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001992 return SkGpuDevice::Create(
1993 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001994 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001995 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001996 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001997 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001998 return NULL;
1999 }
2000}
2001
reed4a8126e2014-09-22 07:29:03 -07002002SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08002003 // TODO: Change the signature of newSurface to take a budgeted parameter.
2004 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
2005 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->numSamples(),
2006 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00002007}
2008
robertphillips30d2cc62014-09-24 08:52:18 -07002009bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07002010 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08002011#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08002012 // todo: should handle this natively
2013 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07002014 return false;
2015 }
2016
robertphillips82365912014-11-12 09:32:34 -08002017 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips81f71b62014-11-11 04:54:49 -08002018
2019 const SkPicture::AccelData* data = mainPicture->EXPERIMENTAL_getAccelData(key);
2020 if (!data) {
2021 return false;
2022 }
2023
robertphillipse5524cd2015-02-20 12:30:26 -08002024 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
2025 if (0 == gpuData->numBlocks()) {
2026 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00002027 }
2028
robertphillipsfd61ed02014-10-28 07:21:44 -07002029 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07002030
robertphillipse5524cd2015-02-20 12:30:26 -08002031 SkIRect iBounds;
2032 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
2033 return false;
2034 }
2035
2036 SkRect clipBounds = SkRect::Make(iBounds);
2037
2038 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
2039
robertphillipsfd61ed02014-10-28 07:21:44 -07002040 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08002041 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07002042 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08002043 &atlasedNeedRendering, &atlasedRecycled,
2044 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07002045
2046 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
2047
2048 SkTDArray<GrHoistedLayer> needRendering, recycled;
2049
robertphillipse5524cd2015-02-20 12:30:26 -08002050 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
2051
robertphillipsfd61ed02014-10-28 07:21:44 -07002052 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08002053 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07002054 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08002055 &needRendering, &recycled,
2056 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07002057
2058 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002059
robertphillips64bf7672014-08-21 13:07:35 -07002060 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08002061 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
2062 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07002063
robertphillipsfd61ed02014-10-28 07:21:44 -07002064 GrLayerHoister::UnlockLayers(fContext, needRendering);
2065 GrLayerHoister::UnlockLayers(fContext, recycled);
2066 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
2067 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07002068
2069 return true;
robertphillips63242d72014-12-04 08:31:02 -08002070#else
2071 return false;
2072#endif
robertphillips64bf7672014-08-21 13:07:35 -07002073}
2074
senorblancobe129b22014-08-08 07:14:35 -07002075SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07002076 // We always return a transient cache, so it is freed after each
2077 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07002078 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07002079}
reedf037e0b2014-10-30 11:34:15 -07002080
2081#endif