blob: 5ed068cc18364ce7bb2b65718c99017f4c9ce9d7 [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
bsalomon54443932015-01-29 09:34:18 -0800722 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000723
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000724 // If we have a prematrix, apply it to the path, optimizing for the case
725 // where the original path can in fact be modified in place (even though
726 // its parameter type is const).
727 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000728 SkTLazy<SkPath> tmpPath;
729 SkTLazy<SkPath> effectPath;
bsalomon54443932015-01-29 09:34:18 -0800730 SkPathEffect* pathEffect = paint.getPathEffect();
731
732 SkMatrix viewMatrix = *draw.fMatrix;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000733
734 if (prePathMatrix) {
bsalomon54443932015-01-29 09:34:18 -0800735 // stroking and path effects are supposed to be applied *after* the prePathMatrix.
736 // The pre-path-matrix also should not affect shadeing.
737 if (NULL == pathEffect && NULL == paint.getShader() &&
738 (strokeInfo.getStrokeRec().isFillStyle() ||
739 strokeInfo.getStrokeRec().isHairlineStyle())) {
740 viewMatrix.preConcat(*prePathMatrix);
741 } else {
742 SkPath* result = pathPtr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000743
bsalomon54443932015-01-29 09:34:18 -0800744 if (!pathIsMutable) {
745 result = tmpPath.init();
746 result->setIsVolatile(true);
747 pathIsMutable = true;
748 }
749 // should I push prePathMatrix on our MV stack temporarily, instead
750 // of applying it here? See SkDraw.cpp
751 pathPtr->transform(*prePathMatrix, result);
752 pathPtr = result;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000753 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000754 }
755 // at this point we're done with prePathMatrix
756 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
757
bsalomon54443932015-01-29 09:34:18 -0800758 GrPaint grPaint;
759 SkPaint2GrPaintShader(this->context(), paint, viewMatrix, true, &grPaint);
760
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000761 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700762 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
763 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000765 pathPtr = effectPath.get();
766 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700767 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000768 }
769
egdanield58a0ba2014-06-11 10:30:05 -0700770 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000771 if (paint.getMaskFilter()) {
772 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000773 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
774 if (stroke.applyToPath(strokedPath, *pathPtr)) {
775 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000776 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700777 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000778 }
779 }
780
781 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000782 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700783 if (!pathIsMutable) {
784 devPathPtr->setIsVolatile(true);
785 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786
787 // transform the path into device space
bsalomon54443932015-01-29 09:34:18 -0800788 pathPtr->transform(viewMatrix, devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000789
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000790 SkRect maskRect;
791 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
792 draw.fClip->getBounds(),
bsalomon54443932015-01-29 09:34:18 -0800793 viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000794 &maskRect)) {
795 SkIRect finalIRect;
796 maskRect.roundOut(&finalIRect);
797 if (draw.fClip->quickReject(finalIRect)) {
798 // clipped out
799 return;
800 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000801
bsalomon54443932015-01-29 09:34:18 -0800802 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint, viewMatrix,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000803 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000804 // the mask filter was able to draw itself directly, so there's nothing
805 // left to do.
806 return;
807 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000808
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000809
bsalomone3059732014-10-14 11:47:22 -0700810 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext, maskRect, *devPathPtr,
811 strokeInfo, grPaint.isAntiAlias(),
812 fRenderTarget->numSamples()));
813 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000814 GrTexture* filtered;
815
bsalomon54443932015-01-29 09:34:18 -0800816 if (paint.getMaskFilter()->filterMaskGPU(mask, viewMatrix, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000817 // filterMaskGPU gives us ownership of a ref to the result
818 SkAutoTUnref<GrTexture> atu(filtered);
bsalomon54443932015-01-29 09:34:18 -0800819 if (draw_mask(fContext, viewMatrix, maskRect, &grPaint, filtered)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000820 // This path is completely drawn
821 return;
822 }
823 }
824 }
825 }
826
827 // draw the mask on the CPU - this is a fallthrough path in case the
828 // GPU path fails
829 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
830 SkPaint::kFill_Style;
bsalomon54443932015-01-29 09:34:18 -0800831 draw_with_mask_filter(fContext, viewMatrix, *devPathPtr, paint.getMaskFilter(),
egdanield58a0ba2014-06-11 10:30:05 -0700832 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000833 return;
834 }
835
bsalomon54443932015-01-29 09:34:18 -0800836 fContext->drawPath(grPaint, viewMatrix, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000837}
838
839static const int kBmpSmallTileSize = 1 << 10;
840
841static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
842 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
843 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
844 return tilesX * tilesY;
845}
846
847static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
848 if (maxTileSize <= kBmpSmallTileSize) {
849 return maxTileSize;
850 }
851
852 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
853 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
854
855 maxTileTotalTileSize *= maxTileSize * maxTileSize;
856 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
857
858 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
859 return kBmpSmallTileSize;
860 } else {
861 return maxTileSize;
862 }
863}
864
865// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
866// pixels from the bitmap are necessary.
867static void determine_clipped_src_rect(const GrContext* context,
joshualitt5531d512014-12-17 15:50:11 -0800868 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000869 const SkBitmap& bitmap,
870 const SkRect* srcRectPtr,
871 SkIRect* clippedSrcIRect) {
872 const GrClipData* clip = context->getClip();
873 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
874 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800875 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000876 clippedSrcIRect->setEmpty();
877 return;
878 }
879 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
880 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700881 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000882 // we've setup src space 0,0 to map to the top left of the src rect.
883 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000884 if (!clippedSrcRect.intersect(*srcRectPtr)) {
885 clippedSrcIRect->setEmpty();
886 return;
887 }
888 }
889 clippedSrcRect.roundOut(clippedSrcIRect);
890 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
891 if (!clippedSrcIRect->intersect(bmpBounds)) {
892 clippedSrcIRect->setEmpty();
893 }
894}
895
896bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800897 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000898 const GrTextureParams& params,
899 const SkRect* srcRectPtr,
900 int maxTileSize,
901 int* tileSize,
902 SkIRect* clippedSrcRect) const {
903 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700904 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000905 return false;
906 }
907
908 // if it's larger than the max tile size, then we have no choice but tiling.
909 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt5531d512014-12-17 15:50:11 -0800910 determine_clipped_src_rect(fContext, viewMatrix, bitmap, srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000911 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
912 return true;
913 }
914
915 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
916 return false;
917 }
918
919 // if the entire texture is already in our cache then no reason to tile it
920 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
921 return false;
922 }
923
924 // At this point we know we could do the draw by uploading the entire bitmap
925 // as a texture. However, if the texture would be large compared to the
926 // cache size and we don't require most of it for this draw then tile to
927 // reduce the amount of upload and cache spill.
928
929 // assumption here is that sw bitmap size is a good proxy for its size as
930 // a texture
931 size_t bmpSize = bitmap.getSize();
932 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000933 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000934 if (bmpSize < cacheSize / 2) {
935 return false;
936 }
937
938 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt5531d512014-12-17 15:50:11 -0800939 determine_clipped_src_rect(fContext, viewMatrix, bitmap, srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000940 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
941 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
942 kBmpSmallTileSize * kBmpSmallTileSize;
943
944 return usedTileBytes < 2 * bmpSize;
945}
946
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000947void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000948 const SkBitmap& bitmap,
949 const SkMatrix& m,
950 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000951 SkMatrix concat;
952 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
953 if (!m.isIdentity()) {
954 concat.setConcat(*draw->fMatrix, m);
955 draw.writable()->fMatrix = &concat;
956 }
957 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958}
959
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000960// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000961// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
962// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000963static inline void clamped_outset_with_offset(SkIRect* iRect,
964 int outset,
965 SkPoint* offset,
966 const SkIRect& clamp) {
967 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000968
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000969 int leftClampDelta = clamp.fLeft - iRect->fLeft;
970 if (leftClampDelta > 0) {
971 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000972 iRect->fLeft = clamp.fLeft;
973 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000974 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000975 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000976
977 int topClampDelta = clamp.fTop - iRect->fTop;
978 if (topClampDelta > 0) {
979 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000980 iRect->fTop = clamp.fTop;
981 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000982 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000983 }
984
985 if (iRect->fRight > clamp.fRight) {
986 iRect->fRight = clamp.fRight;
987 }
988 if (iRect->fBottom > clamp.fBottom) {
989 iRect->fBottom = clamp.fBottom;
990 }
991}
992
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000993static bool has_aligned_samples(const SkRect& srcRect,
994 const SkRect& transformedRect) {
995 // detect pixel disalignment
996 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
997 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
998 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
999 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1000 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1001 COLOR_BLEED_TOLERANCE &&
1002 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1003 COLOR_BLEED_TOLERANCE) {
1004 return true;
1005 }
1006 return false;
1007}
1008
1009static bool may_color_bleed(const SkRect& srcRect,
1010 const SkRect& transformedRect,
1011 const SkMatrix& m) {
1012 // Only gets called if has_aligned_samples returned false.
1013 // So we can assume that sampling is axis aligned but not texel aligned.
1014 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1015 SkRect innerSrcRect(srcRect), innerTransformedRect,
1016 outerTransformedRect(transformedRect);
1017 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1018 m.mapRect(&innerTransformedRect, innerSrcRect);
1019
1020 // The gap between outerTransformedRect and innerTransformedRect
1021 // represents the projection of the source border area, which is
1022 // problematic for color bleeding. We must check whether any
1023 // destination pixels sample the border area.
1024 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1025 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1026 SkIRect outer, inner;
1027 outerTransformedRect.round(&outer);
1028 innerTransformedRect.round(&inner);
1029 // If the inner and outer rects round to the same result, it means the
1030 // border does not overlap any pixel centers. Yay!
1031 return inner != outer;
1032}
1033
1034static bool needs_texture_domain(const SkBitmap& bitmap,
1035 const SkRect& srcRect,
1036 GrTextureParams &params,
1037 const SkMatrix& contextMatrix,
1038 bool bicubic) {
1039 bool needsTextureDomain = false;
1040
1041 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1042 // Need texture domain if drawing a sub rect
1043 needsTextureDomain = srcRect.width() < bitmap.width() ||
1044 srcRect.height() < bitmap.height();
1045 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1046 // sampling is axis-aligned
1047 SkRect transformedRect;
1048 contextMatrix.mapRect(&transformedRect, srcRect);
1049
1050 if (has_aligned_samples(srcRect, transformedRect)) {
1051 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1052 needsTextureDomain = false;
1053 } else {
1054 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1055 }
1056 }
1057 }
1058 return needsTextureDomain;
1059}
1060
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001061void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1062 const SkBitmap& bitmap,
1063 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001064 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001065 const SkPaint& paint,
1066 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -08001067 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001068
1069 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001070 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001071 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1072 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001073 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001074 SkScalar w = SkIntToScalar(bitmap.width());
1075 SkScalar h = SkIntToScalar(bitmap.height());
1076 dstSize.fWidth = w;
1077 dstSize.fHeight = h;
1078 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001079 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001080 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001081 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001082 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001083 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001084 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1085 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1086 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1087 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001088 }
1089
derekf367e1862014-12-02 11:02:06 -08001090 // If the render target is not msaa and draw is antialiased, we call
1091 // drawRect instead of drawing on the render target directly.
1092 // FIXME: the tiled bitmap code path doesn't currently support
1093 // anti-aliased edges, we work around that for now by drawing directly
1094 // if the image size exceeds maximum texture size.
1095 int maxTextureSize = fContext->getMaxTextureSize();
1096 bool directDraw = fRenderTarget->isMultisampled() ||
1097 !paint.isAntiAlias() ||
1098 bitmap.width() > maxTextureSize ||
1099 bitmap.height() > maxTextureSize;
1100
1101 // we check whether dst rect are pixel aligned
1102 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -08001103 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -08001104
1105 if (staysRect) {
1106 SkRect rect;
1107 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -08001108 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -08001109 const SkScalar *scalars = rect.asScalars();
1110 bool isDstPixelAligned = true;
1111 for (int i = 0; i < 4; i++) {
1112 if (!SkScalarIsInt(scalars[i])) {
1113 isDstPixelAligned = false;
1114 break;
1115 }
1116 }
1117
1118 if (isDstPixelAligned)
1119 directDraw = true;
1120 }
1121 }
1122
1123 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001124 // Convert the bitmap to a shader so that the rect can be drawn
1125 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001126 SkBitmap tmp; // subset of bitmap, if necessary
1127 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001128 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001129 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001130 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1131 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1132 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001133 // In bleed mode we position and trim the bitmap based on the src rect which is
1134 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1135 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1136 // compensate.
1137 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1138 SkIRect iSrc;
1139 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001140
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001141 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1142 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001143
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001144 if (!bitmap.extractSubset(&tmp, iSrc)) {
1145 return; // extraction failed
1146 }
1147 bitmapPtr = &tmp;
1148 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001149
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001150 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001151 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001152 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001153 } else {
1154 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001155 }
1156
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001157 SkPaint paintWithShader(paint);
1158 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001159 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001160 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1161 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001162
1163 return;
1164 }
1165
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001166 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1167 // the view matrix rather than a local matrix.
1168 SkMatrix m;
1169 m.setScale(dstSize.fWidth / srcRect.width(),
1170 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -08001171 SkMatrix viewM = *draw.fMatrix;
1172 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001173
1174 GrTextureParams params;
1175 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1176 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001177
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001178 bool doBicubic = false;
1179
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001180 switch(paintFilterLevel) {
1181 case SkPaint::kNone_FilterLevel:
1182 textureFilterMode = GrTextureParams::kNone_FilterMode;
1183 break;
1184 case SkPaint::kLow_FilterLevel:
1185 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1186 break;
1187 case SkPaint::kMedium_FilterLevel:
joshualitt5531d512014-12-17 15:50:11 -08001188 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001189 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1190 } else {
1191 // Don't trigger MIP level generation unnecessarily.
1192 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1193 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001194 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001195 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001196 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001197 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001198 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001199 break;
1200 default:
1201 SkErrorInternals::SetError( kInvalidPaint_SkError,
1202 "Sorry, I don't understand the filtering "
1203 "mode you asked for. Falling back to "
1204 "MIPMaps.");
1205 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1206 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001207 }
1208
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001209 int tileFilterPad;
1210 if (doBicubic) {
1211 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1212 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1213 tileFilterPad = 0;
1214 } else {
1215 tileFilterPad = 1;
1216 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001217 params.setFilterMode(textureFilterMode);
1218
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001219 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001220 int tileSize;
1221
1222 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001223 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001224 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001225 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1226 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001227 } else {
1228 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001229 bool needsTextureDomain = needs_texture_domain(bitmap,
1230 srcRect,
1231 params,
joshualitt5531d512014-12-17 15:50:11 -08001232 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001233 doBicubic);
1234 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001235 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001236 srcRect,
1237 params,
1238 paint,
1239 flags,
1240 doBicubic,
1241 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001242 }
1243}
1244
1245// Break 'bitmap' into several tiles to draw it since it has already
1246// been determined to be too large to fit in VRAM
1247void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001248 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001249 const SkRect& srcRect,
1250 const SkIRect& clippedSrcIRect,
1251 const GrTextureParams& params,
1252 const SkPaint& paint,
1253 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001254 int tileSize,
1255 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001256 // The following pixel lock is technically redundant, but it is desirable
1257 // to lock outside of the tile loop to prevent redecoding the whole image
1258 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1259 // is larger than the limit of the discardable memory pool.
1260 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001261 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1262
1263 int nx = bitmap.width() / tileSize;
1264 int ny = bitmap.height() / tileSize;
1265 for (int x = 0; x <= nx; x++) {
1266 for (int y = 0; y <= ny; y++) {
1267 SkRect tileR;
1268 tileR.set(SkIntToScalar(x * tileSize),
1269 SkIntToScalar(y * tileSize),
1270 SkIntToScalar((x + 1) * tileSize),
1271 SkIntToScalar((y + 1) * tileSize));
1272
1273 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1274 continue;
1275 }
1276
1277 if (!tileR.intersect(srcRect)) {
1278 continue;
1279 }
1280
1281 SkBitmap tmpB;
1282 SkIRect iTileR;
1283 tileR.roundOut(&iTileR);
1284 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1285 SkIntToScalar(iTileR.fTop));
1286
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001287 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001288 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001289 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001290 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001291 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001292
robertphillipsec8bb942014-11-21 10:16:25 -08001293 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001294 SkIRect iClampRect;
1295
1296 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1297 // In bleed mode we want to always expand the tile on all edges
1298 // but stay within the bitmap bounds
1299 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1300 } else {
1301 // In texture-domain/clamp mode we only want to expand the
1302 // tile on edges interior to "srcRect" (i.e., we want to
1303 // not bleed across the original clamped edges)
1304 srcRect.roundOut(&iClampRect);
1305 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001306 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1307 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001308 }
1309
1310 if (bitmap.extractSubset(&tmpB, iTileR)) {
1311 // now offset it to make it "local" to our tmp bitmap
1312 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001313 GrTextureParams paramsTemp = params;
1314 bool needsTextureDomain = needs_texture_domain(bitmap,
1315 srcRect,
1316 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001317 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001318 bicubic);
1319 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001320 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001321 tileR,
1322 paramsTemp,
1323 paint,
1324 flags,
1325 bicubic,
1326 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001327 }
1328 }
1329 }
1330}
1331
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001332
1333/*
1334 * This is called by drawBitmap(), which has to handle images that may be too
1335 * large to be represented by a single texture.
1336 *
1337 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1338 * and that non-texture portion of the GrPaint has already been setup.
1339 */
1340void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001341 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001342 const SkRect& srcRect,
1343 const GrTextureParams& params,
1344 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001345 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001346 bool bicubic,
1347 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001348 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1349 bitmap.height() <= fContext->getMaxTextureSize());
1350
1351 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001352 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001353 if (NULL == texture) {
1354 return;
1355 }
1356
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001357 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001358 SkRect paintRect;
1359 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1360 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1361 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1362 SkScalarMul(srcRect.fTop, hInv),
1363 SkScalarMul(srcRect.fRight, wInv),
1364 SkScalarMul(srcRect.fBottom, hInv));
1365
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001366 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001367 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001368 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001369 // Use a constrained texture domain to avoid color bleeding
1370 SkScalar left, top, right, bottom;
1371 if (srcRect.width() > SK_Scalar1) {
1372 SkScalar border = SK_ScalarHalf / texture->width();
1373 left = paintRect.left() + border;
1374 right = paintRect.right() - border;
1375 } else {
1376 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1377 }
1378 if (srcRect.height() > SK_Scalar1) {
1379 SkScalar border = SK_ScalarHalf / texture->height();
1380 top = paintRect.top() + border;
1381 bottom = paintRect.bottom() - border;
1382 } else {
1383 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1384 }
1385 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001386 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001387 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001388 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001389 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001390 SkMatrix::I(),
1391 textureDomain,
1392 GrTextureDomain::kClamp_Mode,
1393 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001394 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001395 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001396 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1397 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001398 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001399 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001400 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001401 }
1402
1403 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1404 // the rest from the SkPaint.
1405 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001406 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001407 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001408 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1409 SkColor2GrColor(paint.getColor());
1410 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001411
joshualitt16b27892014-12-18 07:47:16 -08001412 fContext->drawNonAARectToRect(grPaint, viewMatrix, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001413}
1414
fmalita2d97bc12014-11-20 10:44:58 -08001415bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
1416 const SkImageFilter* filter,
1417 const SkImageFilter::Context& ctx,
1418 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001419 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001420
1421 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1422 // (https://code.google.com/p/skia/issues/detail?id=3148).
1423 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001424
1425 if (filter->canFilterImageGPU()) {
1426 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1427 // filter. Also set the clip wide open and the matrix to identity.
1428 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001429 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001430 } else {
1431 return false;
1432 }
1433}
1434
1435void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1436 int left, int top, const SkPaint& paint) {
1437 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001438 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001439
1440 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1441 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1442 return;
1443 }
1444
1445 int w = bitmap.width();
1446 int h = bitmap.height();
1447
1448 GrTexture* texture;
1449 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001450 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001451
1452 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001453 // This bitmap will own the filtered result as a texture.
1454 SkBitmap filteredBitmap;
1455
bsalomon49f085d2014-09-05 13:34:00 -07001456 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001457 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001458 SkMatrix matrix(*draw.fMatrix);
1459 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001460 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001461 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001462 // This cache is transient, and is freed (along with all its contained
1463 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001464 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001465 if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap,
1466 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001467 texture = (GrTexture*) filteredBitmap.getTexture();
1468 w = filteredBitmap.width();
1469 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001470 left += offset.x();
1471 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001472 } else {
1473 return;
1474 }
1475 }
1476
1477 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001478 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001479
dandov9de5b512014-06-10 14:38:28 -07001480 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1481 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001482
joshualitt16b27892014-12-18 07:47:16 -08001483 fContext->drawNonAARectToRect(grPaint,
1484 SkMatrix::I(),
1485 SkRect::MakeXYWH(SkIntToScalar(left),
1486 SkIntToScalar(top),
1487 SkIntToScalar(w),
1488 SkIntToScalar(h)),
1489 SkRect::MakeXYWH(0,
1490 0,
1491 SK_Scalar1 * w / texture->width(),
1492 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001493}
1494
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001495void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001496 const SkRect* src, const SkRect& dst,
1497 const SkPaint& paint,
1498 SkCanvas::DrawBitmapRectFlags flags) {
1499 SkMatrix matrix;
1500 SkRect bitmapBounds, tmpSrc;
1501
1502 bitmapBounds.set(0, 0,
1503 SkIntToScalar(bitmap.width()),
1504 SkIntToScalar(bitmap.height()));
1505
1506 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001507 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001508 tmpSrc = *src;
1509 } else {
1510 tmpSrc = bitmapBounds;
1511 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001512
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001513 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1514
1515 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001516 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001517 if (!bitmapBounds.contains(tmpSrc)) {
1518 if (!tmpSrc.intersect(bitmapBounds)) {
1519 return; // nothing to draw
1520 }
1521 }
1522 }
1523
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001524 SkRect tmpDst;
1525 matrix.mapRect(&tmpDst, tmpSrc);
1526
1527 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1528 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1529 // Translate so that tempDst's top left is at the origin.
1530 matrix = *origDraw.fMatrix;
1531 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1532 draw.writable()->fMatrix = &matrix;
1533 }
1534 SkSize dstSize;
1535 dstSize.fWidth = tmpDst.width();
1536 dstSize.fHeight = tmpDst.height();
1537
1538 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001539}
1540
1541void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1542 int x, int y, const SkPaint& paint) {
1543 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001544 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001545 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
bsalomonafe30052015-01-16 07:32:33 -08001546 if (fNeedClear) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001547 // TODO: could check here whether we really need to draw at all
reed8eddfb52014-12-04 07:50:14 -08001548 dev->clearAll();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001549 }
1550
1551 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001552 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001553
1554 GrRenderTarget* devRT = dev->accessRenderTarget();
1555 GrTexture* devTex;
1556 if (NULL == (devTex = devRT->asTexture())) {
1557 return;
1558 }
1559
robertphillips7b9e8a42014-12-11 08:20:31 -08001560 const SkImageInfo ii = dev->imageInfo();
1561 int w = ii.width();
1562 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001563
1564 SkImageFilter* filter = paint.getImageFilter();
1565 // This bitmap will own the filtered result as a texture.
1566 SkBitmap filteredBitmap;
1567
bsalomon49f085d2014-09-05 13:34:00 -07001568 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001569 SkIPoint offset = SkIPoint::Make(0, 0);
1570 SkMatrix matrix(*draw.fMatrix);
1571 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001572 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001573 // This cache is transient, and is freed (along with all its contained
1574 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001575 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001576 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001577 if (this->filterTexture(fContext, devTex, filter, ctx, &filteredBitmap,
1578 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001579 devTex = filteredBitmap.getTexture();
1580 w = filteredBitmap.width();
1581 h = filteredBitmap.height();
1582 x += offset.fX;
1583 y += offset.fY;
1584 } else {
1585 return;
1586 }
1587 }
1588
1589 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001590 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001591
dandov9de5b512014-06-10 14:38:28 -07001592 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1593 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001594
1595 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1596 SkIntToScalar(y),
1597 SkIntToScalar(w),
1598 SkIntToScalar(h));
1599
1600 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1601 // scratch texture).
1602 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1603 SK_Scalar1 * h / devTex->height());
1604
joshualitt16b27892014-12-18 07:47:16 -08001605 fContext->drawNonAARectToRect(grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001606}
1607
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001608bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001609 return filter->canFilterImageGPU();
1610}
1611
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001612bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001613 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001614 SkBitmap* result, SkIPoint* offset) {
1615 // want explicitly our impl, so guard against a subclass of us overriding it
1616 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1617 return false;
1618 }
1619
1620 SkAutoLockPixels alp(src, !src.getTexture());
1621 if (!src.getTexture() && !src.readyToDraw()) {
1622 return false;
1623 }
1624
1625 GrTexture* texture;
1626 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1627 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001628 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001629
fmalita2d97bc12014-11-20 10:44:58 -08001630 return this->filterTexture(fContext, texture, filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001631}
1632
1633///////////////////////////////////////////////////////////////////////////////
1634
1635// must be in SkCanvas::VertexMode order
1636static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1637 kTriangles_GrPrimitiveType,
1638 kTriangleStrip_GrPrimitiveType,
1639 kTriangleFan_GrPrimitiveType,
1640};
1641
1642void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1643 int vertexCount, const SkPoint vertices[],
1644 const SkPoint texs[], const SkColor colors[],
1645 SkXfermode* xmode,
1646 const uint16_t indices[], int indexCount,
1647 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001648 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001649 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001650
dandov32a311b2014-07-15 19:46:26 -07001651 const uint16_t* outIndices;
1652 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1653 GrPrimitiveType primType;
1654 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001655
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001656 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1657 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001658
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001659 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001660
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001661 SkPaint copy(paint);
1662 copy.setStyle(SkPaint::kStroke_Style);
1663 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001664
dandov32a311b2014-07-15 19:46:26 -07001665 // we ignore the shader if texs is null.
1666 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1667 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001668
dandov32a311b2014-07-15 19:46:26 -07001669 primType = kLines_GrPrimitiveType;
1670 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001671 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001672 switch (vmode) {
1673 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001674 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001675 break;
1676 case SkCanvas::kTriangleStrip_VertexMode:
1677 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001678 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001679 break;
1680 }
mtklein533eb782014-08-27 10:39:42 -07001681
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001682 VertState state(vertexCount, indices, indexCount);
1683 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001684
dandov32a311b2014-07-15 19:46:26 -07001685 //number of indices for lines per triangle with kLines
1686 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001687
dandov32a311b2014-07-15 19:46:26 -07001688 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1689 outIndices = outAlloc.get();
1690 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001691 int i = 0;
1692 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001693 auxIndices[i] = state.f0;
1694 auxIndices[i + 1] = state.f1;
1695 auxIndices[i + 2] = state.f1;
1696 auxIndices[i + 3] = state.f2;
1697 auxIndices[i + 4] = state.f2;
1698 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001699 i += 6;
1700 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001701 } else {
dandov32a311b2014-07-15 19:46:26 -07001702 outIndices = indices;
1703 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001704
dandov32a311b2014-07-15 19:46:26 -07001705 if (NULL == texs || NULL == paint.getShader()) {
1706 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1707 NULL == colors, &grPaint);
1708 } else {
joshualitt5531d512014-12-17 15:50:11 -08001709 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, NULL == colors, &grPaint);
dandov32a311b2014-07-15 19:46:26 -07001710 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001711 }
1712
mtklein2583b622014-06-04 08:20:41 -07001713#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001714 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001715 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1716 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001717 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001718 }
1719 }
mtklein2583b622014-06-04 08:20:41 -07001720#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001721
1722 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001723 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001724 // need to convert byte order and from non-PM to PM
1725 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001726 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001727 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001728 color = colors[i];
1729 if (paint.getAlpha() != 255) {
1730 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1731 }
1732 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001733 }
1734 colors = convertedColors.get();
1735 }
1736 fContext->drawVertices(grPaint,
joshualitt5531d512014-12-17 15:50:11 -08001737 *draw.fMatrix,
dandov32a311b2014-07-15 19:46:26 -07001738 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001739 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001740 vertices,
1741 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001742 colors,
dandov32a311b2014-07-15 19:46:26 -07001743 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001744 indexCount);
1745}
1746
1747///////////////////////////////////////////////////////////////////////////////
1748
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001749void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001750 size_t byteLength, SkScalar x, SkScalar y,
1751 const SkPaint& paint) {
1752 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001753 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001754
jvanverth8c27a182014-10-14 08:45:50 -07001755 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -08001756 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001757
jvanverth8c27a182014-10-14 08:45:50 -07001758 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001759
joshualitt5531d512014-12-17 15:50:11 -08001760 if (!fTextContext->drawText(grPaint, paint, *draw.fMatrix, (const char *)text, byteLength, x,
1761 y)) {
jvanverth8c27a182014-10-14 08:45:50 -07001762 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001763 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001764 }
1765}
1766
fmalita05c4a432014-09-29 06:29:53 -07001767void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1768 const SkScalar pos[], int scalarsPerPos,
1769 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001770 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001771 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001772
jvanverth8c27a182014-10-14 08:45:50 -07001773 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -08001774 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001775
jvanverth8c27a182014-10-14 08:45:50 -07001776 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001777
joshualitt5531d512014-12-17 15:50:11 -08001778 if (!fTextContext->drawPosText(grPaint, paint, *draw.fMatrix, (const char *)text, byteLength,
1779 pos, scalarsPerPos, offset)) {
jvanverth8c27a182014-10-14 08:45:50 -07001780 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001781 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001782 }
1783}
1784
1785void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001786 size_t len, const SkPath& path,
1787 const SkMatrix* m, const SkPaint& paint) {
1788 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001789
1790 SkASSERT(draw.fDevice == this);
1791 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1792}
1793
1794///////////////////////////////////////////////////////////////////////////////
1795
reedb2db8982014-11-13 12:41:02 -08001796bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001797 if (paint.getShader() ||
reedb2db8982014-11-13 12:41:02 -08001798 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001799 paint.getMaskFilter() ||
1800 paint.getRasterizer() ||
1801 paint.getColorFilter() ||
1802 paint.getPathEffect() ||
1803 paint.isFakeBoldText() ||
reedb2db8982014-11-13 12:41:02 -08001804 paint.getStyle() != SkPaint::kFill_Style)
1805 {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001806 return true;
1807 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001808 return false;
1809}
1810
1811void SkGpuDevice::flush() {
1812 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001813 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001814}
1815
1816///////////////////////////////////////////////////////////////////////////////
1817
fmalita6987dca2014-11-13 08:33:37 -08001818SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(const CreateInfo& cinfo) {
bsalomonf2703d82014-10-28 14:33:06 -07001819 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001820 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001821 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001822 desc.fWidth = cinfo.fInfo.width();
1823 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001824 desc.fSampleCnt = fRenderTarget->numSamples();
1825
1826 SkAutoTUnref<GrTexture> texture;
1827 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001828 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001829
hcm4396fa52014-10-27 21:43:30 -07001830 // layers are never draw in repeat modes, so we can request an approx
1831 // match and ignore any padding.
fmalita6987dca2014-11-13 08:33:37 -08001832 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == cinfo.fUsage) ?
hcm4396fa52014-10-27 21:43:30 -07001833 GrContext::kApprox_ScratchTexMatch :
1834 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001835 texture.reset(fContext->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001836
1837 if (texture) {
1838 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry);
1839 return SkGpuDevice::Create(texture->asRenderTarget(), &props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001840 } else {
tfarina38406c82014-10-31 07:11:12 -07001841 SkDebugf("---- failed to create compatible device texture [%d %d]\n",
fmalita6987dca2014-11-13 08:33:37 -08001842 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001843 return NULL;
1844 }
1845}
1846
reed4a8126e2014-09-22 07:29:03 -07001847SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001848 // TODO: Change the signature of newSurface to take a budgeted parameter.
1849 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
1850 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->numSamples(),
1851 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001852}
1853
robertphillips30d2cc62014-09-24 08:52:18 -07001854bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001855 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001856#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001857 // todo: should handle this natively
1858 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001859 return false;
1860 }
1861
robertphillips82365912014-11-12 09:32:34 -08001862 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips81f71b62014-11-11 04:54:49 -08001863
1864 const SkPicture::AccelData* data = mainPicture->EXPERIMENTAL_getAccelData(key);
1865 if (!data) {
1866 return false;
1867 }
1868
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001869 SkRect clipBounds;
robertphillips0c423322014-07-31 11:02:38 -07001870 if (!mainCanvas->getClipBounds(&clipBounds)) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001871 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001872 }
1873
robertphillips30d78412014-11-24 09:49:17 -08001874 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1875
1876 const SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1877
robertphillipsfd61ed02014-10-28 07:21:44 -07001878 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001879
robertphillipsfd61ed02014-10-28 07:21:44 -07001880 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001881 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001882 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001883 &atlasedNeedRendering, &atlasedRecycled,
1884 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001885
1886 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1887
1888 SkTDArray<GrHoistedLayer> needRendering, recycled;
1889
1890 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001891 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001892 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001893 &needRendering, &recycled,
1894 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001895
1896 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001897
robertphillips64bf7672014-08-21 13:07:35 -07001898 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001899 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1900 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001901
robertphillipsfd61ed02014-10-28 07:21:44 -07001902 GrLayerHoister::UnlockLayers(fContext, needRendering);
1903 GrLayerHoister::UnlockLayers(fContext, recycled);
1904 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1905 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001906
1907 return true;
robertphillips63242d72014-12-04 08:31:02 -08001908#else
1909 return false;
1910#endif
robertphillips64bf7672014-08-21 13:07:35 -07001911}
1912
senorblancobe129b22014-08-08 07:14:35 -07001913SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001914 // We always return a transient cache, so it is freed after each
1915 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001916 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001917}
reedf037e0b2014-10-30 11:34:15 -07001918
1919#endif