blob: e25cba52020d935364ed30de6d3f23d3b3fef446 [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
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000010#include "effects/GrBicubicEffect.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000011#include "effects/GrDashingEffect.h"
egdaniel95131432014-12-09 11:15:43 -080012#include "effects/GrPorterDuffXferProcessor.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000013#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000014#include "effects/GrSimpleTextureEffect.h"
15
16#include "GrContext.h"
17#include "GrBitmapTextContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000018#include "GrDistanceFieldTextContext.h"
robertphillips98d709b2014-09-02 10:20:50 -070019#include "GrLayerHoister.h"
robertphillips274b4ba2014-09-04 07:24:18 -070020#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070021#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070022#include "GrTracing.h"
derekff4555aa2014-10-06 12:19:12 -070023#include "GrGpu.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000024
25#include "SkGrTexturePixelRef.h"
26
robertphillips30d78412014-11-24 09:49:17 -080027#include "SkCanvasPriv.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkDeviceImageFilterProxy.h"
29#include "SkDrawProcs.h"
30#include "SkGlyphCache.h"
31#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080032#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000033#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000034#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000035#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070036#include "SkPictureData.h"
robertphillips274b4ba2014-09-04 07:24:18 -070037#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000038#include "SkRRect.h"
39#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000040#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000041#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000042#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000043#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070044#include "SkXfermode.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000045#include "SkErrorInternals.h"
46
reedf037e0b2014-10-30 11:34:15 -070047#if SK_SUPPORT_GPU
48
senorblanco55b6d8b2014-07-30 11:26:46 -070049enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
50
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000051#if 0
52 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080053 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000054 do { \
55 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080056 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000057 } while (0)
58#else
joshualitt5531d512014-12-17 15:50:11 -080059 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000060#endif
61
62// This constant represents the screen alignment criterion in texels for
63// requiring texture domain clamping to prevent color bleeding when drawing
64// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000065#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000066
67#define DO_DEFERRED_CLEAR() \
68 do { \
bsalomonafe30052015-01-16 07:32:33 -080069 if (fNeedClear) { \
70 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000071 } \
72 } while (false) \
73
74///////////////////////////////////////////////////////////////////////////////
75
76#define CHECK_FOR_ANNOTATION(paint) \
77 do { if (paint.getAnnotation()) { return; } } while (0)
78
79///////////////////////////////////////////////////////////////////////////////
80
bsalomonbcf0a522014-10-08 08:40:09 -070081// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
82// just accesses the backing GrTexture. Otherwise, it creates a cached texture
83// representation and releases it in the destructor.
84class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040085public:
bsalomonbcf0a522014-10-08 08:40:09 -070086 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070087
bsalomonbcf0a522014-10-08 08:40:09 -070088 AutoBitmapTexture(GrContext* context,
89 const SkBitmap& bitmap,
90 const GrTextureParams* params,
91 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040092 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070093 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040094 }
95
bsalomonbcf0a522014-10-08 08:40:09 -070096 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040097 const SkBitmap& bitmap,
98 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -070099 // Either get the texture directly from the bitmap, or else use the cache and
100 // remember to unref it.
101 if (GrTexture* bmpTexture = bitmap.getTexture()) {
102 fTexture.reset(NULL);
103 return bmpTexture;
104 } else {
105 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
106 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400107 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400108 }
109
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000110private:
bsalomonbcf0a522014-10-08 08:40:09 -0700111 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000112};
113
114///////////////////////////////////////////////////////////////////////////////
115
116struct GrSkDrawProcs : public SkDrawProcs {
117public:
118 GrContext* fContext;
119 GrTextContext* fTextContext;
120 GrFontScaler* fFontScaler; // cached in the skia glyphcache
121};
122
123///////////////////////////////////////////////////////////////////////////////
124
bsalomonafe30052015-01-16 07:32:33 -0800125SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags) {
126 if (!rt || rt->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000127 return NULL;
128 }
bsalomonafe30052015-01-16 07:32:33 -0800129 return SkNEW_ARGS(SkGpuDevice, (rt, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000130}
131
bsalomonafe30052015-01-16 07:32:33 -0800132static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps* props) {
133 if (props) {
134 return SkDeviceProperties(props->pixelGeometry());
135 } else {
136 return SkDeviceProperties(SkDeviceProperties::kLegacyLCD_InitType);
137 }
reedb2db8982014-11-13 12:41:02 -0800138}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000139
bsalomonafe30052015-01-16 07:32:33 -0800140static SkSurfaceProps copy_or_default_props(const SkSurfaceProps* props) {
141 if (props) {
142 return SkSurfaceProps(*props);
143 } else {
144 return SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
145 }
146}
147
148SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags)
reedb2db8982014-11-13 12:41:02 -0800149 : INHERITED(surfaceprops_to_deviceprops(props))
bsalomonafe30052015-01-16 07:32:33 -0800150 , fSurfaceProps(copy_or_default_props(props))
reedb2db8982014-11-13 12:41:02 -0800151{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000152 fDrawProcs = NULL;
153
bsalomonafe30052015-01-16 07:32:33 -0800154 fContext = SkRef(rt->getContext());
155 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000156
bsalomonafe30052015-01-16 07:32:33 -0800157 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000158
bsalomonafe30052015-01-16 07:32:33 -0800159 SkImageInfo info = rt->surfacePriv().info();
160 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt));
bsalomonafbf2d62014-09-30 12:18:44 -0700161 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700162 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700163
bsalomonafe30052015-01-16 07:32:33 -0800164 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts();
jvanverth4736e142014-11-07 07:12:46 -0800165 fTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFT);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000166}
167
bsalomonafe30052015-01-16 07:32:33 -0800168SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
169 const SkImageInfo& origInfo, int sampleCount,
170 const SkSurfaceProps* props, unsigned flags) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000171 if (kUnknown_SkColorType == origInfo.colorType() ||
172 origInfo.width() < 0 || origInfo.height() < 0) {
173 return NULL;
174 }
175
bsalomonafe30052015-01-16 07:32:33 -0800176 if (!context) {
177 return NULL;
178 }
179
reede5ea5002014-09-03 11:54:58 -0700180 SkColorType ct = origInfo.colorType();
181 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700182 if (kRGB_565_SkColorType == ct) {
183 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800184 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
185 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700186 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800187 }
188 if (kOpaque_SkAlphaType != at) {
189 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000190 }
reede5ea5002014-09-03 11:54:58 -0700191 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000192
bsalomonf2703d82014-10-28 14:33:06 -0700193 GrSurfaceDesc desc;
194 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000195 desc.fWidth = info.width();
196 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000197 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000198 desc.fSampleCnt = sampleCount;
199
bsalomonafe30052015-01-16 07:32:33 -0800200 SkAutoTUnref<GrTexture> texture;
201 if (SkSurface::kYes_Budgeted == budgeted) {
202 texture.reset(context->refScratchTexture(desc, GrContext::kExact_ScratchTexMatch));
203 } else {
204 texture.reset(context->createUncachedTexture(desc, NULL, 0));
205 }
206
207 if (!texture) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000208 return NULL;
209 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000210
bsalomonafe30052015-01-16 07:32:33 -0800211 return SkNEW_ARGS(SkGpuDevice, (texture->asRenderTarget(), props, flags));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000212}
213
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000214SkGpuDevice::~SkGpuDevice() {
215 if (fDrawProcs) {
216 delete fDrawProcs;
217 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000218
jvanverth8c27a182014-10-14 08:45:50 -0700219 delete fTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000220
221 // The GrContext takes a ref on the target. We don't want to cause the render
222 // target to be unnecessarily kept alive.
223 if (fContext->getRenderTarget() == fRenderTarget) {
224 fContext->setRenderTarget(NULL);
225 }
226
227 if (fContext->getClip() == &fClipData) {
228 fContext->setClip(NULL);
229 }
230
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
283 fClipData.fClipStack = canvas->getClipStack();
284}
285
286void SkGpuDevice::onDetachFromCanvas() {
287 INHERITED::onDetachFromCanvas();
288 fClipData.fClipStack = NULL;
289}
290
291// call this every draw call, to ensure that the context reflects our state,
292// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800293void SkGpuDevice::prepareDraw(const SkDraw& draw) {
bsalomon49f085d2014-09-05 13:34:00 -0700294 SkASSERT(fClipData.fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000295
296 fContext->setRenderTarget(fRenderTarget);
297
298 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
299
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000300 fClipData.fOrigin = this->getOrigin();
301
302 fContext->setClip(&fClipData);
303
304 DO_DEFERRED_CLEAR();
305}
306
307GrRenderTarget* SkGpuDevice::accessRenderTarget() {
308 DO_DEFERRED_CLEAR();
309 return fRenderTarget;
310}
311
reed8eddfb52014-12-04 07:50:14 -0800312void SkGpuDevice::clearAll() {
313 GrColor color = 0;
314 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
315 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
316 fContext->clear(&rect, color, true, fRenderTarget);
bsalomonafe30052015-01-16 07:32:33 -0800317 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800318}
319
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000320///////////////////////////////////////////////////////////////////////////////
321
322SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
323SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
324SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
325SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
326SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
327 shader_type_mismatch);
328SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
329 shader_type_mismatch);
330SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
331SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
332
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000333///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000334
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000335void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800336 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700337 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000338
339 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800340 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000341
joshualitt5531d512014-12-17 15:50:11 -0800342 fContext->drawPaint(grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000343}
344
345// must be in SkCanvas::PointMode order
346static const GrPrimitiveType gPointMode2PrimtiveType[] = {
347 kPoints_GrPrimitiveType,
348 kLines_GrPrimitiveType,
349 kLineStrip_GrPrimitiveType
350};
351
352void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
353 size_t count, const SkPoint pts[], const SkPaint& paint) {
354 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800355 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356
357 SkScalar width = paint.getStrokeWidth();
358 if (width < 0) {
359 return;
360 }
361
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000362 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700363 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
364 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800365 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
egdaniele61c4112014-06-12 10:24:21 -0700366 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700367 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700368 path.moveTo(pts[0]);
369 path.lineTo(pts[1]);
joshualitt5531d512014-12-17 15:50:11 -0800370 fContext->drawPath(grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700371 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000372 }
373
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000374 // we only handle hairlines and paints without path effects or mask filters,
375 // else we let the SkDraw call our drawPath()
376 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
377 draw.drawPoints(mode, count, pts, paint, true);
378 return;
379 }
380
381 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800382 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000383
384 fContext->drawVertices(grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800385 *draw.fMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000386 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000387 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000388 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000389 NULL,
390 NULL,
391 NULL,
392 0);
393}
394
395///////////////////////////////////////////////////////////////////////////////
396
397void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
398 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700399 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
400
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000401 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800402 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000403
404 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
405 SkScalar width = paint.getStrokeWidth();
406
407 /*
408 We have special code for hairline strokes, miter-strokes, bevel-stroke
409 and fills. Anything else we just call our path code.
410 */
411 bool usePath = doStroke && width > 0 &&
412 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
413 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
414 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700415
416 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000417 usePath = true;
418 }
egdanield58a0ba2014-06-11 10:30:05 -0700419
joshualitt5531d512014-12-17 15:50:11 -0800420 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000421#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
422 if (doStroke) {
423#endif
424 usePath = true;
425#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
426 } else {
joshualitt5531d512014-12-17 15:50:11 -0800427 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000428 }
429#endif
430 }
431 // until we can both stroke and fill rectangles
432 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
433 usePath = true;
434 }
435
egdanield58a0ba2014-06-11 10:30:05 -0700436 GrStrokeInfo strokeInfo(paint);
437
438 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700439 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700440 usePath = true;
441 }
442
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000443 if (usePath) {
444 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700445 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000446 path.addRect(rect);
447 this->drawPath(draw, path, paint, NULL, true);
448 return;
449 }
450
451 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800452 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400453
joshualitt5531d512014-12-17 15:50:11 -0800454 fContext->drawRect(grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000455}
456
457///////////////////////////////////////////////////////////////////////////////
458
459void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800460 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700461 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000462 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800463 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000464
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000465 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800466 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400467
egdanield58a0ba2014-06-11 10:30:05 -0700468 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000469 if (paint.getMaskFilter()) {
470 // try to hit the fast path for drawing filtered round rects
471
472 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800473 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000474 if (devRRect.allCornersCircular()) {
475 SkRect maskRect;
476 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800477 draw.fClip->getBounds(),
478 *draw.fMatrix,
479 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000480 SkIRect finalIRect;
481 maskRect.roundOut(&finalIRect);
482 if (draw.fClip->quickReject(finalIRect)) {
483 // clipped out
484 return;
485 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000486 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800487 *draw.fMatrix,
egdanield58a0ba2014-06-11 10:30:05 -0700488 strokeInfo.getStrokeRec(),
489 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000490 return;
491 }
492 }
493
494 }
495 }
496
497 }
498
egdanield58a0ba2014-06-11 10:30:05 -0700499 bool usePath = false;
500
501 if (paint.getMaskFilter()) {
502 usePath = true;
503 } else {
504 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700505 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700506 usePath = true;
507 }
508 }
509
510
511 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000512 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700513 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000514 path.addRRect(rect);
515 this->drawPath(draw, path, paint, NULL, true);
516 return;
517 }
Mike Klein744fb732014-06-23 15:13:26 -0400518
joshualitt5531d512014-12-17 15:50:11 -0800519 fContext->drawRRect(grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000520}
521
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000522void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800523 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000524 SkStrokeRec stroke(paint);
525 if (stroke.isFillStyle()) {
526
527 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800528 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000529
530 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800531 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000532
533 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
joshualitt5531d512014-12-17 15:50:11 -0800534 fContext->drawDRRect(grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000535 return;
536 }
537 }
538
539 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700540 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000541 path.addRRect(outer);
542 path.addRRect(inner);
543 path.setFillType(SkPath::kEvenOdd_FillType);
544
545 this->drawPath(draw, path, paint, NULL, true);
546}
547
548
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000549/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550
551void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
552 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700553 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000554 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800555 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000556
egdanield58a0ba2014-06-11 10:30:05 -0700557 GrStrokeInfo strokeInfo(paint);
558
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000559 bool usePath = false;
560 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700561 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000562 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700563 } else {
564 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700565 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700566 usePath = true;
567 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000568 }
569
570 if (usePath) {
571 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700572 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000573 path.addOval(oval);
574 this->drawPath(draw, path, paint, NULL, true);
575 return;
576 }
577
578 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800579 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000580
joshualitt5531d512014-12-17 15:50:11 -0800581 fContext->drawOval(grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000582}
583
584#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000585
586///////////////////////////////////////////////////////////////////////////////
587
588// helpers for applying mask filters
589namespace {
590
591// Draw a mask using the supplied paint. Since the coverage/geometry
592// is already burnt into the mask this boils down to a rect draw.
593// Return true if the mask was successfully drawn.
joshualitt5531d512014-12-17 15:50:11 -0800594bool draw_mask(GrContext* context, const SkMatrix& viewMatrix, const SkRect& maskRect,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000595 GrPaint* grp, GrTexture* mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000596 SkMatrix matrix;
597 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
598 matrix.postIDiv(mask->width(), mask->height());
599
joshualitt16b27892014-12-18 07:47:16 -0800600 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix,
601 kDevice_GrCoordSet))->unref();
602
603 SkMatrix inverse;
604 if (!viewMatrix.invert(&inverse)) {
605 return false;
606 }
607 context->drawNonAARectWithLocalMatrix(*grp, SkMatrix::I(), maskRect, inverse);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000608 return true;
609}
610
joshualitt5531d512014-12-17 15:50:11 -0800611bool draw_with_mask_filter(GrContext* context, const SkMatrix& viewMatrix, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700612 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000613 GrPaint* grp, SkPaint::Style style) {
614 SkMask srcM, dstM;
615
joshualitt5531d512014-12-17 15:50:11 -0800616 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &viewMatrix, &srcM,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000617 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
618 return false;
619 }
620 SkAutoMaskFreeImage autoSrc(srcM.fImage);
621
joshualitt5531d512014-12-17 15:50:11 -0800622 if (!filter->filterMask(&dstM, srcM, viewMatrix, NULL)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000623 return false;
624 }
625 // this will free-up dstM when we're done (allocated in filterMask())
626 SkAutoMaskFreeImage autoDst(dstM.fImage);
627
628 if (clip.quickReject(dstM.fBounds)) {
629 return false;
630 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000631
632 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
633 // the current clip (and identity matrix) and GrPaint settings
bsalomonf2703d82014-10-28 14:33:06 -0700634 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000635 desc.fWidth = dstM.fBounds.width();
636 desc.fHeight = dstM.fBounds.height();
637 desc.fConfig = kAlpha_8_GrPixelConfig;
638
bsalomone3059732014-10-14 11:47:22 -0700639 SkAutoTUnref<GrTexture> texture(
640 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
641 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000642 return false;
643 }
644 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
645 dstM.fImage, dstM.fRowBytes);
646
647 SkRect maskRect = SkRect::Make(dstM.fBounds);
648
joshualitt5531d512014-12-17 15:50:11 -0800649 return draw_mask(context, viewMatrix, maskRect, grp, texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000650}
651
bsalomone3059732014-10-14 11:47:22 -0700652// Create a mask of 'devPath' and place the result in 'mask'.
653GrTexture* create_mask_GPU(GrContext* context,
654 const SkRect& maskRect,
655 const SkPath& devPath,
656 const GrStrokeInfo& strokeInfo,
657 bool doAA,
658 int sampleCnt) {
bsalomonf2703d82014-10-28 14:33:06 -0700659 GrSurfaceDesc desc;
660 desc.fFlags = kRenderTarget_GrSurfaceFlag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000661 desc.fWidth = SkScalarCeilToInt(maskRect.width());
662 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700663 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000664 // We actually only need A8, but it often isn't supported as a
665 // render target so default to RGBA_8888
666 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700667
668 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
669 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000670 desc.fConfig = kAlpha_8_GrPixelConfig;
671 }
672
bsalomone3059732014-10-14 11:47:22 -0700673 GrTexture* mask = context->refScratchTexture(desc,GrContext::kApprox_ScratchTexMatch);
674 if (NULL == mask) {
675 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000676 }
677
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000678 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
679
bsalomone3059732014-10-14 11:47:22 -0700680 GrContext::AutoRenderTarget art(context, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000681 GrContext::AutoClip ac(context, clipRect);
682
bsalomon89c62982014-11-03 12:08:42 -0800683 context->clear(NULL, 0x0, true, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000684
685 GrPaint tempPaint;
686 if (doAA) {
687 tempPaint.setAntiAlias(true);
688 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
689 // blend coeff of zero requires dual source blending support in order
690 // to properly blend partially covered pixels. This means the AA
691 // code path may not be taken. So we use a dst blend coeff of ISA. We
692 // could special case AA draws to a dst surface with known alpha=0 to
693 // use a zero dst coeff when dual source blending isn't available.
egdaniel95131432014-12-09 11:15:43 -0800694 tempPaint.setPorterDuffXPFactory(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000695 }
696
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000697 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
698 SkMatrix translate;
699 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -0800700 context->drawPath(tempPaint, translate, devPath, strokeInfo);
bsalomone3059732014-10-14 11:47:22 -0700701 return mask;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000702}
703
704SkBitmap wrap_texture(GrTexture* texture) {
705 SkBitmap result;
bsalomonafbf2d62014-09-30 12:18:44 -0700706 result.setInfo(texture->surfacePriv().info());
reed6c225732014-06-09 19:52:07 -0700707 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000708 return result;
709}
710
711};
712
713void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
714 const SkPaint& paint, const SkMatrix* prePathMatrix,
715 bool pathIsMutable) {
716 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800717 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700718 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000719
jvanverthb3eb6872014-10-24 07:12:51 -0700720 SkASSERT(!pathIsMutable || origSrcPath.isVolatile());
joshualitt5531d512014-12-17 15:50:11 -0800721
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000722 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800723 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000724
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000725 // If we have a prematrix, apply it to the path, optimizing for the case
726 // where the original path can in fact be modified in place (even though
727 // its parameter type is const).
728 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000729 SkTLazy<SkPath> tmpPath;
730 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000731
732 if (prePathMatrix) {
733 SkPath* result = pathPtr;
734
735 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000736 result = tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700737 result->setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000738 pathIsMutable = true;
739 }
740 // should I push prePathMatrix on our MV stack temporarily, instead
741 // of applying it here? See SkDraw.cpp
742 pathPtr->transform(*prePathMatrix, result);
743 pathPtr = result;
744 }
745 // at this point we're done with prePathMatrix
746 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
747
egdanield58a0ba2014-06-11 10:30:05 -0700748 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000749 SkPathEffect* pathEffect = paint.getPathEffect();
750 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700751 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
752 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000753 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000754 pathPtr = effectPath.get();
755 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700756 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000757 }
758
egdanield58a0ba2014-06-11 10:30:05 -0700759 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000760 if (paint.getMaskFilter()) {
761 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000762 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
763 if (stroke.applyToPath(strokedPath, *pathPtr)) {
764 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000765 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700766 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000767 }
768 }
769
770 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000771 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700772 if (!pathIsMutable) {
773 devPathPtr->setIsVolatile(true);
774 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000775
776 // transform the path into device space
joshualitt5531d512014-12-17 15:50:11 -0800777 pathPtr->transform(*draw.fMatrix, devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000778
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000779 SkRect maskRect;
780 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
781 draw.fClip->getBounds(),
joshualitt5531d512014-12-17 15:50:11 -0800782 *draw.fMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000783 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000784 // The context's matrix may change while creating the mask, so save the CTM here to
785 // pass to filterMaskGPU.
joshualitt5531d512014-12-17 15:50:11 -0800786 const SkMatrix ctm = *draw.fMatrix;
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000787
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000788 SkIRect finalIRect;
789 maskRect.roundOut(&finalIRect);
790 if (draw.fClip->quickReject(finalIRect)) {
791 // clipped out
792 return;
793 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000794
joshualitt5531d512014-12-17 15:50:11 -0800795 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint, *draw.fMatrix,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000796 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000797 // the mask filter was able to draw itself directly, so there's nothing
798 // left to do.
799 return;
800 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000801
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000802
bsalomone3059732014-10-14 11:47:22 -0700803 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext, maskRect, *devPathPtr,
804 strokeInfo, grPaint.isAntiAlias(),
805 fRenderTarget->numSamples()));
806 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000807 GrTexture* filtered;
808
bsalomone3059732014-10-14 11:47:22 -0700809 if (paint.getMaskFilter()->filterMaskGPU(mask, ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000810 // filterMaskGPU gives us ownership of a ref to the result
811 SkAutoTUnref<GrTexture> atu(filtered);
joshualitt5531d512014-12-17 15:50:11 -0800812 if (draw_mask(fContext, *draw.fMatrix, maskRect, &grPaint, filtered)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000813 // This path is completely drawn
814 return;
815 }
816 }
817 }
818 }
819
820 // draw the mask on the CPU - this is a fallthrough path in case the
821 // GPU path fails
822 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
823 SkPaint::kFill_Style;
joshualitt5531d512014-12-17 15:50:11 -0800824 draw_with_mask_filter(fContext, *draw.fMatrix, *devPathPtr, paint.getMaskFilter(),
egdanield58a0ba2014-06-11 10:30:05 -0700825 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000826 return;
827 }
828
joshualitt5531d512014-12-17 15:50:11 -0800829 fContext->drawPath(grPaint, *draw.fMatrix, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000830}
831
832static const int kBmpSmallTileSize = 1 << 10;
833
834static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
835 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
836 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
837 return tilesX * tilesY;
838}
839
840static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
841 if (maxTileSize <= kBmpSmallTileSize) {
842 return maxTileSize;
843 }
844
845 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
846 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
847
848 maxTileTotalTileSize *= maxTileSize * maxTileSize;
849 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
850
851 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
852 return kBmpSmallTileSize;
853 } else {
854 return maxTileSize;
855 }
856}
857
858// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
859// pixels from the bitmap are necessary.
860static void determine_clipped_src_rect(const GrContext* context,
joshualitt5531d512014-12-17 15:50:11 -0800861 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000862 const SkBitmap& bitmap,
863 const SkRect* srcRectPtr,
864 SkIRect* clippedSrcIRect) {
865 const GrClipData* clip = context->getClip();
866 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
867 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800868 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000869 clippedSrcIRect->setEmpty();
870 return;
871 }
872 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
873 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700874 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000875 // we've setup src space 0,0 to map to the top left of the src rect.
876 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000877 if (!clippedSrcRect.intersect(*srcRectPtr)) {
878 clippedSrcIRect->setEmpty();
879 return;
880 }
881 }
882 clippedSrcRect.roundOut(clippedSrcIRect);
883 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
884 if (!clippedSrcIRect->intersect(bmpBounds)) {
885 clippedSrcIRect->setEmpty();
886 }
887}
888
889bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800890 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000891 const GrTextureParams& params,
892 const SkRect* srcRectPtr,
893 int maxTileSize,
894 int* tileSize,
895 SkIRect* clippedSrcRect) const {
896 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700897 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000898 return false;
899 }
900
901 // if it's larger than the max tile size, then we have no choice but tiling.
902 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt5531d512014-12-17 15:50:11 -0800903 determine_clipped_src_rect(fContext, viewMatrix, bitmap, srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000904 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
905 return true;
906 }
907
908 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
909 return false;
910 }
911
912 // if the entire texture is already in our cache then no reason to tile it
913 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
914 return false;
915 }
916
917 // At this point we know we could do the draw by uploading the entire bitmap
918 // as a texture. However, if the texture would be large compared to the
919 // cache size and we don't require most of it for this draw then tile to
920 // reduce the amount of upload and cache spill.
921
922 // assumption here is that sw bitmap size is a good proxy for its size as
923 // a texture
924 size_t bmpSize = bitmap.getSize();
925 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000926 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000927 if (bmpSize < cacheSize / 2) {
928 return false;
929 }
930
931 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt5531d512014-12-17 15:50:11 -0800932 determine_clipped_src_rect(fContext, viewMatrix, bitmap, srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
934 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
935 kBmpSmallTileSize * kBmpSmallTileSize;
936
937 return usedTileBytes < 2 * bmpSize;
938}
939
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000940void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000941 const SkBitmap& bitmap,
942 const SkMatrix& m,
943 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000944 SkMatrix concat;
945 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
946 if (!m.isIdentity()) {
947 concat.setConcat(*draw->fMatrix, m);
948 draw.writable()->fMatrix = &concat;
949 }
950 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000951}
952
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000953// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000954// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
955// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000956static inline void clamped_outset_with_offset(SkIRect* iRect,
957 int outset,
958 SkPoint* offset,
959 const SkIRect& clamp) {
960 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000961
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000962 int leftClampDelta = clamp.fLeft - iRect->fLeft;
963 if (leftClampDelta > 0) {
964 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000965 iRect->fLeft = clamp.fLeft;
966 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000967 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000968 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000969
970 int topClampDelta = clamp.fTop - iRect->fTop;
971 if (topClampDelta > 0) {
972 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000973 iRect->fTop = clamp.fTop;
974 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000975 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000976 }
977
978 if (iRect->fRight > clamp.fRight) {
979 iRect->fRight = clamp.fRight;
980 }
981 if (iRect->fBottom > clamp.fBottom) {
982 iRect->fBottom = clamp.fBottom;
983 }
984}
985
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000986static bool has_aligned_samples(const SkRect& srcRect,
987 const SkRect& transformedRect) {
988 // detect pixel disalignment
989 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
990 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
991 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
992 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
993 SkScalarAbs(transformedRect.width() - srcRect.width()) <
994 COLOR_BLEED_TOLERANCE &&
995 SkScalarAbs(transformedRect.height() - srcRect.height()) <
996 COLOR_BLEED_TOLERANCE) {
997 return true;
998 }
999 return false;
1000}
1001
1002static bool may_color_bleed(const SkRect& srcRect,
1003 const SkRect& transformedRect,
1004 const SkMatrix& m) {
1005 // Only gets called if has_aligned_samples returned false.
1006 // So we can assume that sampling is axis aligned but not texel aligned.
1007 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1008 SkRect innerSrcRect(srcRect), innerTransformedRect,
1009 outerTransformedRect(transformedRect);
1010 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1011 m.mapRect(&innerTransformedRect, innerSrcRect);
1012
1013 // The gap between outerTransformedRect and innerTransformedRect
1014 // represents the projection of the source border area, which is
1015 // problematic for color bleeding. We must check whether any
1016 // destination pixels sample the border area.
1017 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1018 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1019 SkIRect outer, inner;
1020 outerTransformedRect.round(&outer);
1021 innerTransformedRect.round(&inner);
1022 // If the inner and outer rects round to the same result, it means the
1023 // border does not overlap any pixel centers. Yay!
1024 return inner != outer;
1025}
1026
1027static bool needs_texture_domain(const SkBitmap& bitmap,
1028 const SkRect& srcRect,
1029 GrTextureParams &params,
1030 const SkMatrix& contextMatrix,
1031 bool bicubic) {
1032 bool needsTextureDomain = false;
1033
1034 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1035 // Need texture domain if drawing a sub rect
1036 needsTextureDomain = srcRect.width() < bitmap.width() ||
1037 srcRect.height() < bitmap.height();
1038 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1039 // sampling is axis-aligned
1040 SkRect transformedRect;
1041 contextMatrix.mapRect(&transformedRect, srcRect);
1042
1043 if (has_aligned_samples(srcRect, transformedRect)) {
1044 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1045 needsTextureDomain = false;
1046 } else {
1047 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1048 }
1049 }
1050 }
1051 return needsTextureDomain;
1052}
1053
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001054void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1055 const SkBitmap& bitmap,
1056 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001057 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001058 const SkPaint& paint,
1059 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -08001060 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001061
1062 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001063 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001064 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1065 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001066 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001067 SkScalar w = SkIntToScalar(bitmap.width());
1068 SkScalar h = SkIntToScalar(bitmap.height());
1069 dstSize.fWidth = w;
1070 dstSize.fHeight = h;
1071 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001072 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001073 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001074 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001075 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001076 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001077 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1078 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1079 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1080 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001081 }
1082
derekf367e1862014-12-02 11:02:06 -08001083 // If the render target is not msaa and draw is antialiased, we call
1084 // drawRect instead of drawing on the render target directly.
1085 // FIXME: the tiled bitmap code path doesn't currently support
1086 // anti-aliased edges, we work around that for now by drawing directly
1087 // if the image size exceeds maximum texture size.
1088 int maxTextureSize = fContext->getMaxTextureSize();
1089 bool directDraw = fRenderTarget->isMultisampled() ||
1090 !paint.isAntiAlias() ||
1091 bitmap.width() > maxTextureSize ||
1092 bitmap.height() > maxTextureSize;
1093
1094 // we check whether dst rect are pixel aligned
1095 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -08001096 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -08001097
1098 if (staysRect) {
1099 SkRect rect;
1100 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -08001101 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -08001102 const SkScalar *scalars = rect.asScalars();
1103 bool isDstPixelAligned = true;
1104 for (int i = 0; i < 4; i++) {
1105 if (!SkScalarIsInt(scalars[i])) {
1106 isDstPixelAligned = false;
1107 break;
1108 }
1109 }
1110
1111 if (isDstPixelAligned)
1112 directDraw = true;
1113 }
1114 }
1115
1116 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001117 // Convert the bitmap to a shader so that the rect can be drawn
1118 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001119 SkBitmap tmp; // subset of bitmap, if necessary
1120 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001121 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001122 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001123 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1124 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1125 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001126 // In bleed mode we position and trim the bitmap based on the src rect which is
1127 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1128 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1129 // compensate.
1130 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1131 SkIRect iSrc;
1132 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001133
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001134 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1135 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001136
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001137 if (!bitmap.extractSubset(&tmp, iSrc)) {
1138 return; // extraction failed
1139 }
1140 bitmapPtr = &tmp;
1141 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001142
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001143 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001144 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001145 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001146 } else {
1147 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001148 }
1149
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001150 SkPaint paintWithShader(paint);
1151 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001152 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001153 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1154 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001155
1156 return;
1157 }
1158
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001159 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1160 // the view matrix rather than a local matrix.
1161 SkMatrix m;
1162 m.setScale(dstSize.fWidth / srcRect.width(),
1163 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -08001164 SkMatrix viewM = *draw.fMatrix;
1165 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001166
1167 GrTextureParams params;
1168 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1169 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001170
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001171 bool doBicubic = false;
1172
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001173 switch(paintFilterLevel) {
1174 case SkPaint::kNone_FilterLevel:
1175 textureFilterMode = GrTextureParams::kNone_FilterMode;
1176 break;
1177 case SkPaint::kLow_FilterLevel:
1178 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1179 break;
1180 case SkPaint::kMedium_FilterLevel:
joshualitt5531d512014-12-17 15:50:11 -08001181 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001182 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1183 } else {
1184 // Don't trigger MIP level generation unnecessarily.
1185 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1186 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001187 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001188 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001189 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001190 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001191 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001192 break;
1193 default:
1194 SkErrorInternals::SetError( kInvalidPaint_SkError,
1195 "Sorry, I don't understand the filtering "
1196 "mode you asked for. Falling back to "
1197 "MIPMaps.");
1198 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1199 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001200 }
1201
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001202 int tileFilterPad;
1203 if (doBicubic) {
1204 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1205 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1206 tileFilterPad = 0;
1207 } else {
1208 tileFilterPad = 1;
1209 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001210 params.setFilterMode(textureFilterMode);
1211
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001212 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001213 int tileSize;
1214
1215 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001216 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001217 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001218 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1219 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001220 } else {
1221 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001222 bool needsTextureDomain = needs_texture_domain(bitmap,
1223 srcRect,
1224 params,
joshualitt5531d512014-12-17 15:50:11 -08001225 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001226 doBicubic);
1227 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001228 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001229 srcRect,
1230 params,
1231 paint,
1232 flags,
1233 doBicubic,
1234 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001235 }
1236}
1237
1238// Break 'bitmap' into several tiles to draw it since it has already
1239// been determined to be too large to fit in VRAM
1240void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001241 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001242 const SkRect& srcRect,
1243 const SkIRect& clippedSrcIRect,
1244 const GrTextureParams& params,
1245 const SkPaint& paint,
1246 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001247 int tileSize,
1248 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001249 // The following pixel lock is technically redundant, but it is desirable
1250 // to lock outside of the tile loop to prevent redecoding the whole image
1251 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1252 // is larger than the limit of the discardable memory pool.
1253 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001254 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1255
1256 int nx = bitmap.width() / tileSize;
1257 int ny = bitmap.height() / tileSize;
1258 for (int x = 0; x <= nx; x++) {
1259 for (int y = 0; y <= ny; y++) {
1260 SkRect tileR;
1261 tileR.set(SkIntToScalar(x * tileSize),
1262 SkIntToScalar(y * tileSize),
1263 SkIntToScalar((x + 1) * tileSize),
1264 SkIntToScalar((y + 1) * tileSize));
1265
1266 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1267 continue;
1268 }
1269
1270 if (!tileR.intersect(srcRect)) {
1271 continue;
1272 }
1273
1274 SkBitmap tmpB;
1275 SkIRect iTileR;
1276 tileR.roundOut(&iTileR);
1277 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1278 SkIntToScalar(iTileR.fTop));
1279
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001280 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001281 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001282 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001283 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001284 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001285
robertphillipsec8bb942014-11-21 10:16:25 -08001286 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001287 SkIRect iClampRect;
1288
1289 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1290 // In bleed mode we want to always expand the tile on all edges
1291 // but stay within the bitmap bounds
1292 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1293 } else {
1294 // In texture-domain/clamp mode we only want to expand the
1295 // tile on edges interior to "srcRect" (i.e., we want to
1296 // not bleed across the original clamped edges)
1297 srcRect.roundOut(&iClampRect);
1298 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001299 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1300 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001301 }
1302
1303 if (bitmap.extractSubset(&tmpB, iTileR)) {
1304 // now offset it to make it "local" to our tmp bitmap
1305 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001306 GrTextureParams paramsTemp = params;
1307 bool needsTextureDomain = needs_texture_domain(bitmap,
1308 srcRect,
1309 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001310 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001311 bicubic);
1312 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001313 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001314 tileR,
1315 paramsTemp,
1316 paint,
1317 flags,
1318 bicubic,
1319 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001320 }
1321 }
1322 }
1323}
1324
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001325
1326/*
1327 * This is called by drawBitmap(), which has to handle images that may be too
1328 * large to be represented by a single texture.
1329 *
1330 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1331 * and that non-texture portion of the GrPaint has already been setup.
1332 */
1333void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001334 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001335 const SkRect& srcRect,
1336 const GrTextureParams& params,
1337 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001338 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001339 bool bicubic,
1340 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001341 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1342 bitmap.height() <= fContext->getMaxTextureSize());
1343
1344 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001345 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001346 if (NULL == texture) {
1347 return;
1348 }
1349
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001350 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001351 SkRect paintRect;
1352 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1353 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1354 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1355 SkScalarMul(srcRect.fTop, hInv),
1356 SkScalarMul(srcRect.fRight, wInv),
1357 SkScalarMul(srcRect.fBottom, hInv));
1358
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001359 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001360 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001361 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001362 // Use a constrained texture domain to avoid color bleeding
1363 SkScalar left, top, right, bottom;
1364 if (srcRect.width() > SK_Scalar1) {
1365 SkScalar border = SK_ScalarHalf / texture->width();
1366 left = paintRect.left() + border;
1367 right = paintRect.right() - border;
1368 } else {
1369 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1370 }
1371 if (srcRect.height() > SK_Scalar1) {
1372 SkScalar border = SK_ScalarHalf / texture->height();
1373 top = paintRect.top() + border;
1374 bottom = paintRect.bottom() - border;
1375 } else {
1376 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1377 }
1378 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001379 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001380 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001381 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001382 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001383 SkMatrix::I(),
1384 textureDomain,
1385 GrTextureDomain::kClamp_Mode,
1386 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001387 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001388 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001389 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1390 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001391 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001392 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001393 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001394 }
1395
1396 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1397 // the rest from the SkPaint.
1398 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001399 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001400 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001401 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1402 SkColor2GrColor(paint.getColor());
1403 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001404
joshualitt16b27892014-12-18 07:47:16 -08001405 fContext->drawNonAARectToRect(grPaint, viewMatrix, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001406}
1407
fmalita2d97bc12014-11-20 10:44:58 -08001408bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
1409 const SkImageFilter* filter,
1410 const SkImageFilter::Context& ctx,
1411 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001412 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001413
1414 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1415 // (https://code.google.com/p/skia/issues/detail?id=3148).
1416 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001417
1418 if (filter->canFilterImageGPU()) {
1419 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1420 // filter. Also set the clip wide open and the matrix to identity.
1421 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001422 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001423 } else {
1424 return false;
1425 }
1426}
1427
1428void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1429 int left, int top, const SkPaint& paint) {
1430 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001431 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001432
1433 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1434 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1435 return;
1436 }
1437
1438 int w = bitmap.width();
1439 int h = bitmap.height();
1440
1441 GrTexture* texture;
1442 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001443 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001444
1445 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001446 // This bitmap will own the filtered result as a texture.
1447 SkBitmap filteredBitmap;
1448
bsalomon49f085d2014-09-05 13:34:00 -07001449 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001450 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001451 SkMatrix matrix(*draw.fMatrix);
1452 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001453 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001454 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001455 // This cache is transient, and is freed (along with all its contained
1456 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001457 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001458 if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap,
1459 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001460 texture = (GrTexture*) filteredBitmap.getTexture();
1461 w = filteredBitmap.width();
1462 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001463 left += offset.x();
1464 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001465 } else {
1466 return;
1467 }
1468 }
1469
1470 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001471 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001472
dandov9de5b512014-06-10 14:38:28 -07001473 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1474 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001475
joshualitt16b27892014-12-18 07:47:16 -08001476 fContext->drawNonAARectToRect(grPaint,
1477 SkMatrix::I(),
1478 SkRect::MakeXYWH(SkIntToScalar(left),
1479 SkIntToScalar(top),
1480 SkIntToScalar(w),
1481 SkIntToScalar(h)),
1482 SkRect::MakeXYWH(0,
1483 0,
1484 SK_Scalar1 * w / texture->width(),
1485 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001486}
1487
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001488void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001489 const SkRect* src, const SkRect& dst,
1490 const SkPaint& paint,
1491 SkCanvas::DrawBitmapRectFlags flags) {
1492 SkMatrix matrix;
1493 SkRect bitmapBounds, tmpSrc;
1494
1495 bitmapBounds.set(0, 0,
1496 SkIntToScalar(bitmap.width()),
1497 SkIntToScalar(bitmap.height()));
1498
1499 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001500 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001501 tmpSrc = *src;
1502 } else {
1503 tmpSrc = bitmapBounds;
1504 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001505
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001506 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1507
1508 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001509 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001510 if (!bitmapBounds.contains(tmpSrc)) {
1511 if (!tmpSrc.intersect(bitmapBounds)) {
1512 return; // nothing to draw
1513 }
1514 }
1515 }
1516
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001517 SkRect tmpDst;
1518 matrix.mapRect(&tmpDst, tmpSrc);
1519
1520 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1521 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1522 // Translate so that tempDst's top left is at the origin.
1523 matrix = *origDraw.fMatrix;
1524 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1525 draw.writable()->fMatrix = &matrix;
1526 }
1527 SkSize dstSize;
1528 dstSize.fWidth = tmpDst.width();
1529 dstSize.fHeight = tmpDst.height();
1530
1531 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001532}
1533
1534void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1535 int x, int y, const SkPaint& paint) {
1536 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001537 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001538 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
bsalomonafe30052015-01-16 07:32:33 -08001539 if (fNeedClear) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001540 // TODO: could check here whether we really need to draw at all
reed8eddfb52014-12-04 07:50:14 -08001541 dev->clearAll();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001542 }
1543
1544 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001545 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001546
1547 GrRenderTarget* devRT = dev->accessRenderTarget();
1548 GrTexture* devTex;
1549 if (NULL == (devTex = devRT->asTexture())) {
1550 return;
1551 }
1552
robertphillips7b9e8a42014-12-11 08:20:31 -08001553 const SkImageInfo ii = dev->imageInfo();
1554 int w = ii.width();
1555 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001556
1557 SkImageFilter* filter = paint.getImageFilter();
1558 // This bitmap will own the filtered result as a texture.
1559 SkBitmap filteredBitmap;
1560
bsalomon49f085d2014-09-05 13:34:00 -07001561 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001562 SkIPoint offset = SkIPoint::Make(0, 0);
1563 SkMatrix matrix(*draw.fMatrix);
1564 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001565 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001566 // This cache is transient, and is freed (along with all its contained
1567 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001568 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001569 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001570 if (this->filterTexture(fContext, devTex, filter, ctx, &filteredBitmap,
1571 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001572 devTex = filteredBitmap.getTexture();
1573 w = filteredBitmap.width();
1574 h = filteredBitmap.height();
1575 x += offset.fX;
1576 y += offset.fY;
1577 } else {
1578 return;
1579 }
1580 }
1581
1582 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001583 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001584
dandov9de5b512014-06-10 14:38:28 -07001585 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1586 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001587
1588 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1589 SkIntToScalar(y),
1590 SkIntToScalar(w),
1591 SkIntToScalar(h));
1592
1593 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1594 // scratch texture).
1595 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1596 SK_Scalar1 * h / devTex->height());
1597
joshualitt16b27892014-12-18 07:47:16 -08001598 fContext->drawNonAARectToRect(grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001599}
1600
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001601bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001602 return filter->canFilterImageGPU();
1603}
1604
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001605bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001606 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001607 SkBitmap* result, SkIPoint* offset) {
1608 // want explicitly our impl, so guard against a subclass of us overriding it
1609 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1610 return false;
1611 }
1612
1613 SkAutoLockPixels alp(src, !src.getTexture());
1614 if (!src.getTexture() && !src.readyToDraw()) {
1615 return false;
1616 }
1617
1618 GrTexture* texture;
1619 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1620 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001621 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001622
fmalita2d97bc12014-11-20 10:44:58 -08001623 return this->filterTexture(fContext, texture, filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001624}
1625
1626///////////////////////////////////////////////////////////////////////////////
1627
1628// must be in SkCanvas::VertexMode order
1629static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1630 kTriangles_GrPrimitiveType,
1631 kTriangleStrip_GrPrimitiveType,
1632 kTriangleFan_GrPrimitiveType,
1633};
1634
1635void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1636 int vertexCount, const SkPoint vertices[],
1637 const SkPoint texs[], const SkColor colors[],
1638 SkXfermode* xmode,
1639 const uint16_t indices[], int indexCount,
1640 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001641 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001642 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001643
dandov32a311b2014-07-15 19:46:26 -07001644 const uint16_t* outIndices;
1645 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1646 GrPrimitiveType primType;
1647 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001648
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001649 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1650 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001651
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001652 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001653
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001654 SkPaint copy(paint);
1655 copy.setStyle(SkPaint::kStroke_Style);
1656 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001657
dandov32a311b2014-07-15 19:46:26 -07001658 // we ignore the shader if texs is null.
1659 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1660 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001661
dandov32a311b2014-07-15 19:46:26 -07001662 primType = kLines_GrPrimitiveType;
1663 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001664 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001665 switch (vmode) {
1666 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001667 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001668 break;
1669 case SkCanvas::kTriangleStrip_VertexMode:
1670 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001671 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001672 break;
1673 }
mtklein533eb782014-08-27 10:39:42 -07001674
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001675 VertState state(vertexCount, indices, indexCount);
1676 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001677
dandov32a311b2014-07-15 19:46:26 -07001678 //number of indices for lines per triangle with kLines
1679 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001680
dandov32a311b2014-07-15 19:46:26 -07001681 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1682 outIndices = outAlloc.get();
1683 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001684 int i = 0;
1685 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001686 auxIndices[i] = state.f0;
1687 auxIndices[i + 1] = state.f1;
1688 auxIndices[i + 2] = state.f1;
1689 auxIndices[i + 3] = state.f2;
1690 auxIndices[i + 4] = state.f2;
1691 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001692 i += 6;
1693 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001694 } else {
dandov32a311b2014-07-15 19:46:26 -07001695 outIndices = indices;
1696 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001697
dandov32a311b2014-07-15 19:46:26 -07001698 if (NULL == texs || NULL == paint.getShader()) {
1699 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1700 NULL == colors, &grPaint);
1701 } else {
joshualitt5531d512014-12-17 15:50:11 -08001702 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, NULL == colors, &grPaint);
dandov32a311b2014-07-15 19:46:26 -07001703 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001704 }
1705
mtklein2583b622014-06-04 08:20:41 -07001706#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001707 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001708 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1709 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001710 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001711 }
1712 }
mtklein2583b622014-06-04 08:20:41 -07001713#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001714
1715 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001716 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001717 // need to convert byte order and from non-PM to PM
1718 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001719 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001720 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001721 color = colors[i];
1722 if (paint.getAlpha() != 255) {
1723 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1724 }
1725 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001726 }
1727 colors = convertedColors.get();
1728 }
1729 fContext->drawVertices(grPaint,
joshualitt5531d512014-12-17 15:50:11 -08001730 *draw.fMatrix,
dandov32a311b2014-07-15 19:46:26 -07001731 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001732 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001733 vertices,
1734 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001735 colors,
dandov32a311b2014-07-15 19:46:26 -07001736 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001737 indexCount);
1738}
1739
1740///////////////////////////////////////////////////////////////////////////////
1741
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001742void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001743 size_t byteLength, SkScalar x, SkScalar y,
1744 const SkPaint& paint) {
1745 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001746 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001747
jvanverth8c27a182014-10-14 08:45:50 -07001748 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -08001749 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001750
jvanverth8c27a182014-10-14 08:45:50 -07001751 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001752
joshualitt5531d512014-12-17 15:50:11 -08001753 if (!fTextContext->drawText(grPaint, paint, *draw.fMatrix, (const char *)text, byteLength, x,
1754 y)) {
jvanverth8c27a182014-10-14 08:45:50 -07001755 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001756 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001757 }
1758}
1759
fmalita05c4a432014-09-29 06:29:53 -07001760void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1761 const SkScalar pos[], int scalarsPerPos,
1762 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001763 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001764 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001765
jvanverth8c27a182014-10-14 08:45:50 -07001766 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -08001767 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001768
jvanverth8c27a182014-10-14 08:45:50 -07001769 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001770
joshualitt5531d512014-12-17 15:50:11 -08001771 if (!fTextContext->drawPosText(grPaint, paint, *draw.fMatrix, (const char *)text, byteLength,
1772 pos, scalarsPerPos, offset)) {
jvanverth8c27a182014-10-14 08:45:50 -07001773 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001774 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001775 }
1776}
1777
1778void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001779 size_t len, const SkPath& path,
1780 const SkMatrix* m, const SkPaint& paint) {
1781 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001782
1783 SkASSERT(draw.fDevice == this);
1784 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1785}
1786
1787///////////////////////////////////////////////////////////////////////////////
1788
reedb2db8982014-11-13 12:41:02 -08001789bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001790 if (paint.getShader() ||
reedb2db8982014-11-13 12:41:02 -08001791 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001792 paint.getMaskFilter() ||
1793 paint.getRasterizer() ||
1794 paint.getColorFilter() ||
1795 paint.getPathEffect() ||
1796 paint.isFakeBoldText() ||
reedb2db8982014-11-13 12:41:02 -08001797 paint.getStyle() != SkPaint::kFill_Style)
1798 {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001799 return true;
1800 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001801 return false;
1802}
1803
1804void SkGpuDevice::flush() {
1805 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001806 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001807}
1808
1809///////////////////////////////////////////////////////////////////////////////
1810
fmalita6987dca2014-11-13 08:33:37 -08001811SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(const CreateInfo& cinfo) {
bsalomonf2703d82014-10-28 14:33:06 -07001812 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001813 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001814 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001815 desc.fWidth = cinfo.fInfo.width();
1816 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001817 desc.fSampleCnt = fRenderTarget->numSamples();
1818
1819 SkAutoTUnref<GrTexture> texture;
1820 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001821 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001822
hcm4396fa52014-10-27 21:43:30 -07001823 // layers are never draw in repeat modes, so we can request an approx
1824 // match and ignore any padding.
fmalita6987dca2014-11-13 08:33:37 -08001825 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == cinfo.fUsage) ?
hcm4396fa52014-10-27 21:43:30 -07001826 GrContext::kApprox_ScratchTexMatch :
1827 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001828 texture.reset(fContext->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001829
1830 if (texture) {
1831 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry);
1832 return SkGpuDevice::Create(texture->asRenderTarget(), &props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001833 } else {
tfarina38406c82014-10-31 07:11:12 -07001834 SkDebugf("---- failed to create compatible device texture [%d %d]\n",
fmalita6987dca2014-11-13 08:33:37 -08001835 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001836 return NULL;
1837 }
1838}
1839
reed4a8126e2014-09-22 07:29:03 -07001840SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001841 // TODO: Change the signature of newSurface to take a budgeted parameter.
1842 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
1843 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->numSamples(),
1844 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001845}
1846
robertphillips30d2cc62014-09-24 08:52:18 -07001847bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001848 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001849#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001850 // todo: should handle this natively
1851 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001852 return false;
1853 }
1854
robertphillips82365912014-11-12 09:32:34 -08001855 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips81f71b62014-11-11 04:54:49 -08001856
1857 const SkPicture::AccelData* data = mainPicture->EXPERIMENTAL_getAccelData(key);
1858 if (!data) {
1859 return false;
1860 }
1861
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001862 SkRect clipBounds;
robertphillips0c423322014-07-31 11:02:38 -07001863 if (!mainCanvas->getClipBounds(&clipBounds)) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001864 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001865 }
1866
robertphillips30d78412014-11-24 09:49:17 -08001867 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1868
1869 const SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1870
robertphillipsfd61ed02014-10-28 07:21:44 -07001871 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001872
robertphillipsfd61ed02014-10-28 07:21:44 -07001873 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001874 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001875 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001876 &atlasedNeedRendering, &atlasedRecycled,
1877 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001878
1879 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1880
1881 SkTDArray<GrHoistedLayer> needRendering, recycled;
1882
1883 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001884 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001885 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001886 &needRendering, &recycled,
1887 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001888
1889 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001890
robertphillips64bf7672014-08-21 13:07:35 -07001891 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001892 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1893 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001894
robertphillipsfd61ed02014-10-28 07:21:44 -07001895 GrLayerHoister::UnlockLayers(fContext, needRendering);
1896 GrLayerHoister::UnlockLayers(fContext, recycled);
1897 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1898 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001899
1900 return true;
robertphillips63242d72014-12-04 08:31:02 -08001901#else
1902 return false;
1903#endif
robertphillips64bf7672014-08-21 13:07:35 -07001904}
1905
senorblancobe129b22014-08-08 07:14:35 -07001906SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001907 // We always return a transient cache, so it is freed after each
1908 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001909 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001910}
reedf037e0b2014-10-30 11:34:15 -07001911
1912#endif