blob: 040cea83561e3abb2c5f277c3f55b34aa4367e74 [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#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
52
53#if 0
54 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080055 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000056 do { \
57 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080058 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000059 } while (0)
60#else
joshualitt5531d512014-12-17 15:50:11 -080061 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000062#endif
63
64// This constant represents the screen alignment criterion in texels for
65// requiring texture domain clamping to prevent color bleeding when drawing
66// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000067#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000068
69#define DO_DEFERRED_CLEAR() \
70 do { \
jvanverth4736e142014-11-07 07:12:46 -080071 if (fFlags & kNeedClear_Flag) { \
reed8eddfb52014-12-04 07:50:14 -080072 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000073 } \
74 } while (false) \
75
76///////////////////////////////////////////////////////////////////////////////
77
78#define CHECK_FOR_ANNOTATION(paint) \
79 do { if (paint.getAnnotation()) { return; } } while (0)
80
81///////////////////////////////////////////////////////////////////////////////
82
bsalomonbcf0a522014-10-08 08:40:09 -070083// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
84// just accesses the backing GrTexture. Otherwise, it creates a cached texture
85// representation and releases it in the destructor.
86class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040087public:
bsalomonbcf0a522014-10-08 08:40:09 -070088 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070089
bsalomonbcf0a522014-10-08 08:40:09 -070090 AutoBitmapTexture(GrContext* context,
91 const SkBitmap& bitmap,
92 const GrTextureParams* params,
93 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040094 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070095 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040096 }
97
bsalomonbcf0a522014-10-08 08:40:09 -070098 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040099 const SkBitmap& bitmap,
100 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -0700101 // Either get the texture directly from the bitmap, or else use the cache and
102 // remember to unref it.
103 if (GrTexture* bmpTexture = bitmap.getTexture()) {
104 fTexture.reset(NULL);
105 return bmpTexture;
106 } else {
107 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
108 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400109 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400110 }
111
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000112private:
bsalomonbcf0a522014-10-08 08:40:09 -0700113 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000114};
115
116///////////////////////////////////////////////////////////////////////////////
117
118struct GrSkDrawProcs : public SkDrawProcs {
119public:
120 GrContext* fContext;
121 GrTextContext* fTextContext;
122 GrFontScaler* fFontScaler; // cached in the skia glyphcache
123};
124
125///////////////////////////////////////////////////////////////////////////////
126
reed4a8126e2014-09-22 07:29:03 -0700127SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, const SkSurfaceProps& props, unsigned flags) {
bsalomon49f085d2014-09-05 13:34:00 -0700128 SkASSERT(surface);
bsalomon32d0b3b2014-08-29 07:50:23 -0700129 if (NULL == surface->asRenderTarget() || surface->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000130 return NULL;
131 }
reed4a8126e2014-09-22 07:29:03 -0700132 return SkNEW_ARGS(SkGpuDevice, (surface, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000133}
134
reedb2db8982014-11-13 12:41:02 -0800135static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps& props) {
136 return SkDeviceProperties(props.pixelGeometry());
137}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000138
reedb2db8982014-11-13 12:41:02 -0800139SkGpuDevice::SkGpuDevice(GrSurface* surface, const SkSurfaceProps& props, unsigned flags)
140 : INHERITED(surfaceprops_to_deviceprops(props))
141{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000142 fDrawProcs = NULL;
143
bsalomon32d0b3b2014-08-29 07:50:23 -0700144 fContext = SkRef(surface->getContext());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000145
jvanverth4736e142014-11-07 07:12:46 -0800146 fFlags = flags;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000147
bsalomon32d0b3b2014-08-29 07:50:23 -0700148 fRenderTarget = SkRef(surface->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000149
bsalomonafbf2d62014-09-30 12:18:44 -0700150 SkImageInfo info = surface->surfacePriv().info();
bsalomonbcf0a522014-10-08 08:40:09 -0700151 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface));
bsalomonafbf2d62014-09-30 12:18:44 -0700152 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700153 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700154
jvanverth4736e142014-11-07 07:12:46 -0800155 bool useDFT = SkToBool(flags & kDFText_Flag);
156 fTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFT);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000157}
158
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000159SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
reed4a8126e2014-09-22 07:29:03 -0700160 const SkSurfaceProps& props, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000161 if (kUnknown_SkColorType == origInfo.colorType() ||
162 origInfo.width() < 0 || origInfo.height() < 0) {
163 return NULL;
164 }
165
reede5ea5002014-09-03 11:54:58 -0700166 SkColorType ct = origInfo.colorType();
167 SkAlphaType at = origInfo.alphaType();
robertphillipsa0537de2014-09-18 08:01:23 -0700168 // TODO: perhaps we can loosen this check now that colortype is more detailed
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000169 // e.g. can we support both RGBA and BGRA here?
reede5ea5002014-09-03 11:54:58 -0700170 if (kRGB_565_SkColorType == ct) {
171 at = kOpaque_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000172 } else {
reede5ea5002014-09-03 11:54:58 -0700173 ct = kN32_SkColorType;
174 if (kOpaque_SkAlphaType != at) {
175 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000176 }
177 }
reede5ea5002014-09-03 11:54:58 -0700178 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000179
bsalomonf2703d82014-10-28 14:33:06 -0700180 GrSurfaceDesc desc;
181 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000182 desc.fWidth = info.width();
183 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000184 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000185 desc.fSampleCnt = sampleCount;
186
187 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
188 if (!texture.get()) {
189 return NULL;
190 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000191
reed4a8126e2014-09-22 07:29:03 -0700192 return SkNEW_ARGS(SkGpuDevice, (texture.get(), props));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000193}
194
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000195SkGpuDevice::~SkGpuDevice() {
196 if (fDrawProcs) {
197 delete fDrawProcs;
198 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000199
jvanverth8c27a182014-10-14 08:45:50 -0700200 delete fTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000201
202 // The GrContext takes a ref on the target. We don't want to cause the render
203 // target to be unnecessarily kept alive.
204 if (fContext->getRenderTarget() == fRenderTarget) {
205 fContext->setRenderTarget(NULL);
206 }
207
208 if (fContext->getClip() == &fClipData) {
209 fContext->setClip(NULL);
210 }
211
bsalomon32d0b3b2014-08-29 07:50:23 -0700212 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000213 fContext->unref();
214}
215
216///////////////////////////////////////////////////////////////////////////////
217
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000218bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
219 int x, int y) {
220 DO_DEFERRED_CLEAR();
221
222 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000223 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000224 if (kUnknown_GrPixelConfig == config) {
225 return false;
226 }
227
228 uint32_t flags = 0;
229 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
230 flags = GrContext::kUnpremul_PixelOpsFlag;
231 }
232 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
233 config, dstPixels, dstRowBytes, flags);
234}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000235
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000236bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
237 int x, int y) {
238 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000239 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000240 if (kUnknown_GrPixelConfig == config) {
241 return false;
242 }
243 uint32_t flags = 0;
244 if (kUnpremul_SkAlphaType == info.alphaType()) {
245 flags = GrContext::kUnpremul_PixelOpsFlag;
246 }
247 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
248
249 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700250 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000251
252 return true;
253}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000254
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000255const SkBitmap& SkGpuDevice::onAccessBitmap() {
256 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700257 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000258}
259
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000260void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
261 INHERITED::onAttachToCanvas(canvas);
262
263 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
264 fClipData.fClipStack = canvas->getClipStack();
265}
266
267void SkGpuDevice::onDetachFromCanvas() {
268 INHERITED::onDetachFromCanvas();
269 fClipData.fClipStack = NULL;
270}
271
272// call this every draw call, to ensure that the context reflects our state,
273// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800274void SkGpuDevice::prepareDraw(const SkDraw& draw) {
bsalomon49f085d2014-09-05 13:34:00 -0700275 SkASSERT(fClipData.fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000276
277 fContext->setRenderTarget(fRenderTarget);
278
279 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
280
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000281 fClipData.fOrigin = this->getOrigin();
282
283 fContext->setClip(&fClipData);
284
285 DO_DEFERRED_CLEAR();
286}
287
288GrRenderTarget* SkGpuDevice::accessRenderTarget() {
289 DO_DEFERRED_CLEAR();
290 return fRenderTarget;
291}
292
reed8eddfb52014-12-04 07:50:14 -0800293void SkGpuDevice::clearAll() {
294 GrColor color = 0;
295 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
296 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
297 fContext->clear(&rect, color, true, fRenderTarget);
298 fFlags &= ~kNeedClear_Flag;
299}
300
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000301///////////////////////////////////////////////////////////////////////////////
302
303SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
304SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
305SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
306SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
307SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
308 shader_type_mismatch);
309SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
310 shader_type_mismatch);
311SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
312SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
313
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000314///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000315
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000316void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800317 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700318 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000319
320 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800321 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000322
joshualitt5531d512014-12-17 15:50:11 -0800323 fContext->drawPaint(grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000324}
325
326// must be in SkCanvas::PointMode order
327static const GrPrimitiveType gPointMode2PrimtiveType[] = {
328 kPoints_GrPrimitiveType,
329 kLines_GrPrimitiveType,
330 kLineStrip_GrPrimitiveType
331};
332
333void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
334 size_t count, const SkPoint pts[], const SkPaint& paint) {
335 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800336 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000337
338 SkScalar width = paint.getStrokeWidth();
339 if (width < 0) {
340 return;
341 }
342
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000343 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700344 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
345 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800346 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
egdaniele61c4112014-06-12 10:24:21 -0700347 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700348 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700349 path.moveTo(pts[0]);
350 path.lineTo(pts[1]);
joshualitt5531d512014-12-17 15:50:11 -0800351 fContext->drawPath(grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700352 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000353 }
354
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000355 // we only handle hairlines and paints without path effects or mask filters,
356 // else we let the SkDraw call our drawPath()
357 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
358 draw.drawPoints(mode, count, pts, paint, true);
359 return;
360 }
361
362 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800363 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000364
365 fContext->drawVertices(grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800366 *draw.fMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000367 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000368 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000369 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000370 NULL,
371 NULL,
372 NULL,
373 0);
374}
375
376///////////////////////////////////////////////////////////////////////////////
377
378void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
379 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700380 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
381
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000382 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800383 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000384
385 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
386 SkScalar width = paint.getStrokeWidth();
387
388 /*
389 We have special code for hairline strokes, miter-strokes, bevel-stroke
390 and fills. Anything else we just call our path code.
391 */
392 bool usePath = doStroke && width > 0 &&
393 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
394 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
395 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700396
397 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000398 usePath = true;
399 }
egdanield58a0ba2014-06-11 10:30:05 -0700400
joshualitt5531d512014-12-17 15:50:11 -0800401 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000402#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
403 if (doStroke) {
404#endif
405 usePath = true;
406#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
407 } else {
joshualitt5531d512014-12-17 15:50:11 -0800408 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000409 }
410#endif
411 }
412 // until we can both stroke and fill rectangles
413 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
414 usePath = true;
415 }
416
egdanield58a0ba2014-06-11 10:30:05 -0700417 GrStrokeInfo strokeInfo(paint);
418
419 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700420 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700421 usePath = true;
422 }
423
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000424 if (usePath) {
425 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700426 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000427 path.addRect(rect);
428 this->drawPath(draw, path, paint, NULL, true);
429 return;
430 }
431
432 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800433 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400434
joshualitt5531d512014-12-17 15:50:11 -0800435 fContext->drawRect(grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000436}
437
438///////////////////////////////////////////////////////////////////////////////
439
440void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800441 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700442 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000443 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800444 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000445
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000446 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800447 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400448
egdanield58a0ba2014-06-11 10:30:05 -0700449 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000450 if (paint.getMaskFilter()) {
451 // try to hit the fast path for drawing filtered round rects
452
453 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800454 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000455 if (devRRect.allCornersCircular()) {
456 SkRect maskRect;
457 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800458 draw.fClip->getBounds(),
459 *draw.fMatrix,
460 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000461 SkIRect finalIRect;
462 maskRect.roundOut(&finalIRect);
463 if (draw.fClip->quickReject(finalIRect)) {
464 // clipped out
465 return;
466 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000467 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800468 *draw.fMatrix,
egdanield58a0ba2014-06-11 10:30:05 -0700469 strokeInfo.getStrokeRec(),
470 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000471 return;
472 }
473 }
474
475 }
476 }
477
478 }
479
egdanield58a0ba2014-06-11 10:30:05 -0700480 bool usePath = false;
481
482 if (paint.getMaskFilter()) {
483 usePath = true;
484 } else {
485 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700486 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700487 usePath = true;
488 }
489 }
490
491
492 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000493 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700494 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000495 path.addRRect(rect);
496 this->drawPath(draw, path, paint, NULL, true);
497 return;
498 }
Mike Klein744fb732014-06-23 15:13:26 -0400499
joshualitt5531d512014-12-17 15:50:11 -0800500 fContext->drawRRect(grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000501}
502
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000503void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800504 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000505 SkStrokeRec stroke(paint);
506 if (stroke.isFillStyle()) {
507
508 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800509 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000510
511 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800512 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000513
514 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
joshualitt5531d512014-12-17 15:50:11 -0800515 fContext->drawDRRect(grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000516 return;
517 }
518 }
519
520 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700521 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000522 path.addRRect(outer);
523 path.addRRect(inner);
524 path.setFillType(SkPath::kEvenOdd_FillType);
525
526 this->drawPath(draw, path, paint, NULL, true);
527}
528
529
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000530/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000531
532void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
533 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700534 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000535 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800536 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000537
egdanield58a0ba2014-06-11 10:30:05 -0700538 GrStrokeInfo strokeInfo(paint);
539
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000540 bool usePath = false;
541 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700542 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000543 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700544 } else {
545 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700546 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700547 usePath = true;
548 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000549 }
550
551 if (usePath) {
552 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700553 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000554 path.addOval(oval);
555 this->drawPath(draw, path, paint, NULL, true);
556 return;
557 }
558
559 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800560 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000561
joshualitt5531d512014-12-17 15:50:11 -0800562 fContext->drawOval(grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000563}
564
565#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000566
567///////////////////////////////////////////////////////////////////////////////
568
569// helpers for applying mask filters
570namespace {
571
572// Draw a mask using the supplied paint. Since the coverage/geometry
573// is already burnt into the mask this boils down to a rect draw.
574// Return true if the mask was successfully drawn.
joshualitt5531d512014-12-17 15:50:11 -0800575bool draw_mask(GrContext* context, const SkMatrix& viewMatrix, const SkRect& maskRect,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000576 GrPaint* grp, GrTexture* mask) {
joshualitt5531d512014-12-17 15:50:11 -0800577 if (!grp->localCoordChangeInverse(viewMatrix)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000578 return false;
579 }
580
581 SkMatrix matrix;
582 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
583 matrix.postIDiv(mask->width(), mask->height());
584
joshualittb0a8a372014-09-23 09:50:21 -0700585 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix))->unref();
joshualitt5531d512014-12-17 15:50:11 -0800586 context->drawRect(*grp, SkMatrix::I(), maskRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000587 return true;
588}
589
joshualitt5531d512014-12-17 15:50:11 -0800590bool draw_with_mask_filter(GrContext* context, const SkMatrix& viewMatrix, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700591 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000592 GrPaint* grp, SkPaint::Style style) {
593 SkMask srcM, dstM;
594
joshualitt5531d512014-12-17 15:50:11 -0800595 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &viewMatrix, &srcM,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000596 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
597 return false;
598 }
599 SkAutoMaskFreeImage autoSrc(srcM.fImage);
600
joshualitt5531d512014-12-17 15:50:11 -0800601 if (!filter->filterMask(&dstM, srcM, viewMatrix, NULL)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000602 return false;
603 }
604 // this will free-up dstM when we're done (allocated in filterMask())
605 SkAutoMaskFreeImage autoDst(dstM.fImage);
606
607 if (clip.quickReject(dstM.fBounds)) {
608 return false;
609 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000610
611 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
612 // the current clip (and identity matrix) and GrPaint settings
bsalomonf2703d82014-10-28 14:33:06 -0700613 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000614 desc.fWidth = dstM.fBounds.width();
615 desc.fHeight = dstM.fBounds.height();
616 desc.fConfig = kAlpha_8_GrPixelConfig;
617
bsalomone3059732014-10-14 11:47:22 -0700618 SkAutoTUnref<GrTexture> texture(
619 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
620 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000621 return false;
622 }
623 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
624 dstM.fImage, dstM.fRowBytes);
625
626 SkRect maskRect = SkRect::Make(dstM.fBounds);
627
joshualitt5531d512014-12-17 15:50:11 -0800628 return draw_mask(context, viewMatrix, maskRect, grp, texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000629}
630
bsalomone3059732014-10-14 11:47:22 -0700631// Create a mask of 'devPath' and place the result in 'mask'.
632GrTexture* create_mask_GPU(GrContext* context,
633 const SkRect& maskRect,
634 const SkPath& devPath,
635 const GrStrokeInfo& strokeInfo,
636 bool doAA,
637 int sampleCnt) {
bsalomonf2703d82014-10-28 14:33:06 -0700638 GrSurfaceDesc desc;
639 desc.fFlags = kRenderTarget_GrSurfaceFlag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000640 desc.fWidth = SkScalarCeilToInt(maskRect.width());
641 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700642 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000643 // We actually only need A8, but it often isn't supported as a
644 // render target so default to RGBA_8888
645 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700646
647 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
648 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000649 desc.fConfig = kAlpha_8_GrPixelConfig;
650 }
651
bsalomone3059732014-10-14 11:47:22 -0700652 GrTexture* mask = context->refScratchTexture(desc,GrContext::kApprox_ScratchTexMatch);
653 if (NULL == mask) {
654 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000655 }
656
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000657 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
658
bsalomone3059732014-10-14 11:47:22 -0700659 GrContext::AutoRenderTarget art(context, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660 GrContext::AutoClip ac(context, clipRect);
661
bsalomon89c62982014-11-03 12:08:42 -0800662 context->clear(NULL, 0x0, true, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000663
664 GrPaint tempPaint;
665 if (doAA) {
666 tempPaint.setAntiAlias(true);
667 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
668 // blend coeff of zero requires dual source blending support in order
669 // to properly blend partially covered pixels. This means the AA
670 // code path may not be taken. So we use a dst blend coeff of ISA. We
671 // could special case AA draws to a dst surface with known alpha=0 to
672 // use a zero dst coeff when dual source blending isn't available.
egdaniel95131432014-12-09 11:15:43 -0800673 tempPaint.setPorterDuffXPFactory(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000674 }
675
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000676 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
677 SkMatrix translate;
678 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -0800679 context->drawPath(tempPaint, translate, devPath, strokeInfo);
bsalomone3059732014-10-14 11:47:22 -0700680 return mask;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000681}
682
683SkBitmap wrap_texture(GrTexture* texture) {
684 SkBitmap result;
bsalomonafbf2d62014-09-30 12:18:44 -0700685 result.setInfo(texture->surfacePriv().info());
reed6c225732014-06-09 19:52:07 -0700686 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000687 return result;
688}
689
690};
691
692void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
693 const SkPaint& paint, const SkMatrix* prePathMatrix,
694 bool pathIsMutable) {
695 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800696 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700697 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000698
jvanverthb3eb6872014-10-24 07:12:51 -0700699 SkASSERT(!pathIsMutable || origSrcPath.isVolatile());
joshualitt5531d512014-12-17 15:50:11 -0800700
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000701 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800702 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000703
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000704 // If we have a prematrix, apply it to the path, optimizing for the case
705 // where the original path can in fact be modified in place (even though
706 // its parameter type is const).
707 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000708 SkTLazy<SkPath> tmpPath;
709 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000710
711 if (prePathMatrix) {
712 SkPath* result = pathPtr;
713
714 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000715 result = tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700716 result->setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000717 pathIsMutable = true;
718 }
719 // should I push prePathMatrix on our MV stack temporarily, instead
720 // of applying it here? See SkDraw.cpp
721 pathPtr->transform(*prePathMatrix, result);
722 pathPtr = result;
723 }
724 // at this point we're done with prePathMatrix
725 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
726
egdanield58a0ba2014-06-11 10:30:05 -0700727 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000728 SkPathEffect* pathEffect = paint.getPathEffect();
729 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700730 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
731 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000732 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000733 pathPtr = effectPath.get();
734 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700735 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000736 }
737
egdanield58a0ba2014-06-11 10:30:05 -0700738 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000739 if (paint.getMaskFilter()) {
740 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000741 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
742 if (stroke.applyToPath(strokedPath, *pathPtr)) {
743 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000744 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700745 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000746 }
747 }
748
749 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000750 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700751 if (!pathIsMutable) {
752 devPathPtr->setIsVolatile(true);
753 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000754
755 // transform the path into device space
joshualitt5531d512014-12-17 15:50:11 -0800756 pathPtr->transform(*draw.fMatrix, devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000757
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000758 SkRect maskRect;
759 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
760 draw.fClip->getBounds(),
joshualitt5531d512014-12-17 15:50:11 -0800761 *draw.fMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000762 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000763 // The context's matrix may change while creating the mask, so save the CTM here to
764 // pass to filterMaskGPU.
joshualitt5531d512014-12-17 15:50:11 -0800765 const SkMatrix ctm = *draw.fMatrix;
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000766
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000767 SkIRect finalIRect;
768 maskRect.roundOut(&finalIRect);
769 if (draw.fClip->quickReject(finalIRect)) {
770 // clipped out
771 return;
772 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000773
joshualitt5531d512014-12-17 15:50:11 -0800774 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint, *draw.fMatrix,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000775 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000776 // the mask filter was able to draw itself directly, so there's nothing
777 // left to do.
778 return;
779 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000780
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000781
bsalomone3059732014-10-14 11:47:22 -0700782 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext, maskRect, *devPathPtr,
783 strokeInfo, grPaint.isAntiAlias(),
784 fRenderTarget->numSamples()));
785 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786 GrTexture* filtered;
787
bsalomone3059732014-10-14 11:47:22 -0700788 if (paint.getMaskFilter()->filterMaskGPU(mask, ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000789 // filterMaskGPU gives us ownership of a ref to the result
790 SkAutoTUnref<GrTexture> atu(filtered);
joshualitt5531d512014-12-17 15:50:11 -0800791 if (draw_mask(fContext, *draw.fMatrix, maskRect, &grPaint, filtered)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000792 // This path is completely drawn
793 return;
794 }
795 }
796 }
797 }
798
799 // draw the mask on the CPU - this is a fallthrough path in case the
800 // GPU path fails
801 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
802 SkPaint::kFill_Style;
joshualitt5531d512014-12-17 15:50:11 -0800803 draw_with_mask_filter(fContext, *draw.fMatrix, *devPathPtr, paint.getMaskFilter(),
egdanield58a0ba2014-06-11 10:30:05 -0700804 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000805 return;
806 }
807
joshualitt5531d512014-12-17 15:50:11 -0800808 fContext->drawPath(grPaint, *draw.fMatrix, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000809}
810
811static const int kBmpSmallTileSize = 1 << 10;
812
813static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
814 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
815 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
816 return tilesX * tilesY;
817}
818
819static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
820 if (maxTileSize <= kBmpSmallTileSize) {
821 return maxTileSize;
822 }
823
824 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
825 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
826
827 maxTileTotalTileSize *= maxTileSize * maxTileSize;
828 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
829
830 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
831 return kBmpSmallTileSize;
832 } else {
833 return maxTileSize;
834 }
835}
836
837// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
838// pixels from the bitmap are necessary.
839static void determine_clipped_src_rect(const GrContext* context,
joshualitt5531d512014-12-17 15:50:11 -0800840 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000841 const SkBitmap& bitmap,
842 const SkRect* srcRectPtr,
843 SkIRect* clippedSrcIRect) {
844 const GrClipData* clip = context->getClip();
845 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
846 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800847 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000848 clippedSrcIRect->setEmpty();
849 return;
850 }
851 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
852 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700853 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000854 // we've setup src space 0,0 to map to the top left of the src rect.
855 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000856 if (!clippedSrcRect.intersect(*srcRectPtr)) {
857 clippedSrcIRect->setEmpty();
858 return;
859 }
860 }
861 clippedSrcRect.roundOut(clippedSrcIRect);
862 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
863 if (!clippedSrcIRect->intersect(bmpBounds)) {
864 clippedSrcIRect->setEmpty();
865 }
866}
867
868bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800869 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000870 const GrTextureParams& params,
871 const SkRect* srcRectPtr,
872 int maxTileSize,
873 int* tileSize,
874 SkIRect* clippedSrcRect) const {
875 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700876 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000877 return false;
878 }
879
880 // if it's larger than the max tile size, then we have no choice but tiling.
881 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt5531d512014-12-17 15:50:11 -0800882 determine_clipped_src_rect(fContext, viewMatrix, bitmap, srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000883 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
884 return true;
885 }
886
887 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
888 return false;
889 }
890
891 // if the entire texture is already in our cache then no reason to tile it
892 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
893 return false;
894 }
895
896 // At this point we know we could do the draw by uploading the entire bitmap
897 // as a texture. However, if the texture would be large compared to the
898 // cache size and we don't require most of it for this draw then tile to
899 // reduce the amount of upload and cache spill.
900
901 // assumption here is that sw bitmap size is a good proxy for its size as
902 // a texture
903 size_t bmpSize = bitmap.getSize();
904 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000905 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000906 if (bmpSize < cacheSize / 2) {
907 return false;
908 }
909
910 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt5531d512014-12-17 15:50:11 -0800911 determine_clipped_src_rect(fContext, viewMatrix, bitmap, srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000912 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
913 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
914 kBmpSmallTileSize * kBmpSmallTileSize;
915
916 return usedTileBytes < 2 * bmpSize;
917}
918
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000919void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000920 const SkBitmap& bitmap,
921 const SkMatrix& m,
922 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000923 SkMatrix concat;
924 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
925 if (!m.isIdentity()) {
926 concat.setConcat(*draw->fMatrix, m);
927 draw.writable()->fMatrix = &concat;
928 }
929 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000930}
931
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000932// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
934// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000935static inline void clamped_outset_with_offset(SkIRect* iRect,
936 int outset,
937 SkPoint* offset,
938 const SkIRect& clamp) {
939 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000940
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000941 int leftClampDelta = clamp.fLeft - iRect->fLeft;
942 if (leftClampDelta > 0) {
943 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000944 iRect->fLeft = clamp.fLeft;
945 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000946 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000947 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000948
949 int topClampDelta = clamp.fTop - iRect->fTop;
950 if (topClampDelta > 0) {
951 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000952 iRect->fTop = clamp.fTop;
953 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000954 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000955 }
956
957 if (iRect->fRight > clamp.fRight) {
958 iRect->fRight = clamp.fRight;
959 }
960 if (iRect->fBottom > clamp.fBottom) {
961 iRect->fBottom = clamp.fBottom;
962 }
963}
964
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000965static bool has_aligned_samples(const SkRect& srcRect,
966 const SkRect& transformedRect) {
967 // detect pixel disalignment
968 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
969 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
970 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
971 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
972 SkScalarAbs(transformedRect.width() - srcRect.width()) <
973 COLOR_BLEED_TOLERANCE &&
974 SkScalarAbs(transformedRect.height() - srcRect.height()) <
975 COLOR_BLEED_TOLERANCE) {
976 return true;
977 }
978 return false;
979}
980
981static bool may_color_bleed(const SkRect& srcRect,
982 const SkRect& transformedRect,
983 const SkMatrix& m) {
984 // Only gets called if has_aligned_samples returned false.
985 // So we can assume that sampling is axis aligned but not texel aligned.
986 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
987 SkRect innerSrcRect(srcRect), innerTransformedRect,
988 outerTransformedRect(transformedRect);
989 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
990 m.mapRect(&innerTransformedRect, innerSrcRect);
991
992 // The gap between outerTransformedRect and innerTransformedRect
993 // represents the projection of the source border area, which is
994 // problematic for color bleeding. We must check whether any
995 // destination pixels sample the border area.
996 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
997 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
998 SkIRect outer, inner;
999 outerTransformedRect.round(&outer);
1000 innerTransformedRect.round(&inner);
1001 // If the inner and outer rects round to the same result, it means the
1002 // border does not overlap any pixel centers. Yay!
1003 return inner != outer;
1004}
1005
1006static bool needs_texture_domain(const SkBitmap& bitmap,
1007 const SkRect& srcRect,
1008 GrTextureParams &params,
1009 const SkMatrix& contextMatrix,
1010 bool bicubic) {
1011 bool needsTextureDomain = false;
1012
1013 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1014 // Need texture domain if drawing a sub rect
1015 needsTextureDomain = srcRect.width() < bitmap.width() ||
1016 srcRect.height() < bitmap.height();
1017 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1018 // sampling is axis-aligned
1019 SkRect transformedRect;
1020 contextMatrix.mapRect(&transformedRect, srcRect);
1021
1022 if (has_aligned_samples(srcRect, transformedRect)) {
1023 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1024 needsTextureDomain = false;
1025 } else {
1026 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1027 }
1028 }
1029 }
1030 return needsTextureDomain;
1031}
1032
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001033void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1034 const SkBitmap& bitmap,
1035 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001036 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001037 const SkPaint& paint,
1038 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -08001039 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001040
1041 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001042 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001043 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1044 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001046 SkScalar w = SkIntToScalar(bitmap.width());
1047 SkScalar h = SkIntToScalar(bitmap.height());
1048 dstSize.fWidth = w;
1049 dstSize.fHeight = h;
1050 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001051 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001052 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001053 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001054 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001055 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001056 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1057 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1058 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1059 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001060 }
1061
derekf367e1862014-12-02 11:02:06 -08001062 // If the render target is not msaa and draw is antialiased, we call
1063 // drawRect instead of drawing on the render target directly.
1064 // FIXME: the tiled bitmap code path doesn't currently support
1065 // anti-aliased edges, we work around that for now by drawing directly
1066 // if the image size exceeds maximum texture size.
1067 int maxTextureSize = fContext->getMaxTextureSize();
1068 bool directDraw = fRenderTarget->isMultisampled() ||
1069 !paint.isAntiAlias() ||
1070 bitmap.width() > maxTextureSize ||
1071 bitmap.height() > maxTextureSize;
1072
1073 // we check whether dst rect are pixel aligned
1074 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -08001075 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -08001076
1077 if (staysRect) {
1078 SkRect rect;
1079 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -08001080 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -08001081 const SkScalar *scalars = rect.asScalars();
1082 bool isDstPixelAligned = true;
1083 for (int i = 0; i < 4; i++) {
1084 if (!SkScalarIsInt(scalars[i])) {
1085 isDstPixelAligned = false;
1086 break;
1087 }
1088 }
1089
1090 if (isDstPixelAligned)
1091 directDraw = true;
1092 }
1093 }
1094
1095 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001096 // Convert the bitmap to a shader so that the rect can be drawn
1097 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001098 SkBitmap tmp; // subset of bitmap, if necessary
1099 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001100 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001101 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001102 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1103 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1104 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001105 // In bleed mode we position and trim the bitmap based on the src rect which is
1106 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1107 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1108 // compensate.
1109 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1110 SkIRect iSrc;
1111 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001112
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001113 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1114 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001115
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001116 if (!bitmap.extractSubset(&tmp, iSrc)) {
1117 return; // extraction failed
1118 }
1119 bitmapPtr = &tmp;
1120 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001121
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001122 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001123 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001124 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001125 } else {
1126 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001127 }
1128
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001129 SkPaint paintWithShader(paint);
1130 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001131 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001132 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1133 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001134
1135 return;
1136 }
1137
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001138 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1139 // the view matrix rather than a local matrix.
1140 SkMatrix m;
1141 m.setScale(dstSize.fWidth / srcRect.width(),
1142 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -08001143 SkMatrix viewM = *draw.fMatrix;
1144 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001145
1146 GrTextureParams params;
1147 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1148 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001149
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001150 bool doBicubic = false;
1151
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001152 switch(paintFilterLevel) {
1153 case SkPaint::kNone_FilterLevel:
1154 textureFilterMode = GrTextureParams::kNone_FilterMode;
1155 break;
1156 case SkPaint::kLow_FilterLevel:
1157 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1158 break;
1159 case SkPaint::kMedium_FilterLevel:
joshualitt5531d512014-12-17 15:50:11 -08001160 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001161 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1162 } else {
1163 // Don't trigger MIP level generation unnecessarily.
1164 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1165 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001166 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001167 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001168 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001169 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001170 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001171 break;
1172 default:
1173 SkErrorInternals::SetError( kInvalidPaint_SkError,
1174 "Sorry, I don't understand the filtering "
1175 "mode you asked for. Falling back to "
1176 "MIPMaps.");
1177 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1178 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001179 }
1180
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001181 int tileFilterPad;
1182 if (doBicubic) {
1183 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1184 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1185 tileFilterPad = 0;
1186 } else {
1187 tileFilterPad = 1;
1188 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001189 params.setFilterMode(textureFilterMode);
1190
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001191 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001192 int tileSize;
1193
1194 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001195 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001196 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001197 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1198 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001199 } else {
1200 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001201 bool needsTextureDomain = needs_texture_domain(bitmap,
1202 srcRect,
1203 params,
joshualitt5531d512014-12-17 15:50:11 -08001204 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001205 doBicubic);
1206 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001207 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001208 srcRect,
1209 params,
1210 paint,
1211 flags,
1212 doBicubic,
1213 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001214 }
1215}
1216
1217// Break 'bitmap' into several tiles to draw it since it has already
1218// been determined to be too large to fit in VRAM
1219void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001220 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001221 const SkRect& srcRect,
1222 const SkIRect& clippedSrcIRect,
1223 const GrTextureParams& params,
1224 const SkPaint& paint,
1225 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001226 int tileSize,
1227 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001228 // The following pixel lock is technically redundant, but it is desirable
1229 // to lock outside of the tile loop to prevent redecoding the whole image
1230 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1231 // is larger than the limit of the discardable memory pool.
1232 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001233 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1234
1235 int nx = bitmap.width() / tileSize;
1236 int ny = bitmap.height() / tileSize;
1237 for (int x = 0; x <= nx; x++) {
1238 for (int y = 0; y <= ny; y++) {
1239 SkRect tileR;
1240 tileR.set(SkIntToScalar(x * tileSize),
1241 SkIntToScalar(y * tileSize),
1242 SkIntToScalar((x + 1) * tileSize),
1243 SkIntToScalar((y + 1) * tileSize));
1244
1245 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1246 continue;
1247 }
1248
1249 if (!tileR.intersect(srcRect)) {
1250 continue;
1251 }
1252
1253 SkBitmap tmpB;
1254 SkIRect iTileR;
1255 tileR.roundOut(&iTileR);
1256 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1257 SkIntToScalar(iTileR.fTop));
1258
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001259 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001260 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001261 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001262 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001263 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001264
robertphillipsec8bb942014-11-21 10:16:25 -08001265 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001266 SkIRect iClampRect;
1267
1268 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1269 // In bleed mode we want to always expand the tile on all edges
1270 // but stay within the bitmap bounds
1271 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1272 } else {
1273 // In texture-domain/clamp mode we only want to expand the
1274 // tile on edges interior to "srcRect" (i.e., we want to
1275 // not bleed across the original clamped edges)
1276 srcRect.roundOut(&iClampRect);
1277 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001278 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1279 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001280 }
1281
1282 if (bitmap.extractSubset(&tmpB, iTileR)) {
1283 // now offset it to make it "local" to our tmp bitmap
1284 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001285 GrTextureParams paramsTemp = params;
1286 bool needsTextureDomain = needs_texture_domain(bitmap,
1287 srcRect,
1288 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001289 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001290 bicubic);
1291 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001292 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001293 tileR,
1294 paramsTemp,
1295 paint,
1296 flags,
1297 bicubic,
1298 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001299 }
1300 }
1301 }
1302}
1303
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001304
1305/*
1306 * This is called by drawBitmap(), which has to handle images that may be too
1307 * large to be represented by a single texture.
1308 *
1309 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1310 * and that non-texture portion of the GrPaint has already been setup.
1311 */
1312void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001313 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001314 const SkRect& srcRect,
1315 const GrTextureParams& params,
1316 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001317 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001318 bool bicubic,
1319 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001320 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1321 bitmap.height() <= fContext->getMaxTextureSize());
1322
1323 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001324 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001325 if (NULL == texture) {
1326 return;
1327 }
1328
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001329 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001330 SkRect paintRect;
1331 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1332 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1333 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1334 SkScalarMul(srcRect.fTop, hInv),
1335 SkScalarMul(srcRect.fRight, wInv),
1336 SkScalarMul(srcRect.fBottom, hInv));
1337
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001338 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001339 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001340 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001341 // Use a constrained texture domain to avoid color bleeding
1342 SkScalar left, top, right, bottom;
1343 if (srcRect.width() > SK_Scalar1) {
1344 SkScalar border = SK_ScalarHalf / texture->width();
1345 left = paintRect.left() + border;
1346 right = paintRect.right() - border;
1347 } else {
1348 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1349 }
1350 if (srcRect.height() > SK_Scalar1) {
1351 SkScalar border = SK_ScalarHalf / texture->height();
1352 top = paintRect.top() + border;
1353 bottom = paintRect.bottom() - border;
1354 } else {
1355 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1356 }
1357 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001358 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001359 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001360 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001361 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001362 SkMatrix::I(),
1363 textureDomain,
1364 GrTextureDomain::kClamp_Mode,
1365 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001366 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001367 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001368 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1369 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001370 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001371 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001372 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001373 }
1374
1375 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1376 // the rest from the SkPaint.
1377 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001378 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001379 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001380 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1381 SkColor2GrColor(paint.getColor());
1382 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001383
joshualitt5531d512014-12-17 15:50:11 -08001384 fContext->drawRectToRect(grPaint, viewMatrix, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001385}
1386
fmalita2d97bc12014-11-20 10:44:58 -08001387bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
1388 const SkImageFilter* filter,
1389 const SkImageFilter::Context& ctx,
1390 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001391 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001392
1393 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1394 // (https://code.google.com/p/skia/issues/detail?id=3148).
1395 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001396
1397 if (filter->canFilterImageGPU()) {
1398 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1399 // filter. Also set the clip wide open and the matrix to identity.
1400 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001401 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001402 } else {
1403 return false;
1404 }
1405}
1406
1407void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1408 int left, int top, const SkPaint& paint) {
1409 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001410 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001411
1412 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1413 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1414 return;
1415 }
1416
1417 int w = bitmap.width();
1418 int h = bitmap.height();
1419
1420 GrTexture* texture;
1421 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001422 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001423
1424 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001425 // This bitmap will own the filtered result as a texture.
1426 SkBitmap filteredBitmap;
1427
bsalomon49f085d2014-09-05 13:34:00 -07001428 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001429 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001430 SkMatrix matrix(*draw.fMatrix);
1431 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001432 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001433 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001434 // This cache is transient, and is freed (along with all its contained
1435 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001436 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001437 if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap,
1438 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001439 texture = (GrTexture*) filteredBitmap.getTexture();
1440 w = filteredBitmap.width();
1441 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001442 left += offset.x();
1443 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001444 } else {
1445 return;
1446 }
1447 }
1448
1449 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001450 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001451
dandov9de5b512014-06-10 14:38:28 -07001452 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1453 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001454
1455 fContext->drawRectToRect(grPaint,
joshualitt5531d512014-12-17 15:50:11 -08001456 SkMatrix::I(),
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001457 SkRect::MakeXYWH(SkIntToScalar(left),
1458 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001459 SkIntToScalar(w),
1460 SkIntToScalar(h)),
1461 SkRect::MakeXYWH(0,
1462 0,
1463 SK_Scalar1 * w / texture->width(),
1464 SK_Scalar1 * h / texture->height()));
1465}
1466
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001467void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001468 const SkRect* src, const SkRect& dst,
1469 const SkPaint& paint,
1470 SkCanvas::DrawBitmapRectFlags flags) {
1471 SkMatrix matrix;
1472 SkRect bitmapBounds, tmpSrc;
1473
1474 bitmapBounds.set(0, 0,
1475 SkIntToScalar(bitmap.width()),
1476 SkIntToScalar(bitmap.height()));
1477
1478 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001479 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001480 tmpSrc = *src;
1481 } else {
1482 tmpSrc = bitmapBounds;
1483 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001484
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001485 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1486
1487 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001488 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001489 if (!bitmapBounds.contains(tmpSrc)) {
1490 if (!tmpSrc.intersect(bitmapBounds)) {
1491 return; // nothing to draw
1492 }
1493 }
1494 }
1495
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001496 SkRect tmpDst;
1497 matrix.mapRect(&tmpDst, tmpSrc);
1498
1499 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1500 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1501 // Translate so that tempDst's top left is at the origin.
1502 matrix = *origDraw.fMatrix;
1503 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1504 draw.writable()->fMatrix = &matrix;
1505 }
1506 SkSize dstSize;
1507 dstSize.fWidth = tmpDst.width();
1508 dstSize.fHeight = tmpDst.height();
1509
1510 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001511}
1512
1513void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1514 int x, int y, const SkPaint& paint) {
1515 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001516 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001517 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
jvanverth4736e142014-11-07 07:12:46 -08001518 if (dev->fFlags & kNeedClear_Flag) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001519 // TODO: could check here whether we really need to draw at all
reed8eddfb52014-12-04 07:50:14 -08001520 dev->clearAll();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001521 }
1522
1523 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001524 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001525
1526 GrRenderTarget* devRT = dev->accessRenderTarget();
1527 GrTexture* devTex;
1528 if (NULL == (devTex = devRT->asTexture())) {
1529 return;
1530 }
1531
robertphillips7b9e8a42014-12-11 08:20:31 -08001532 const SkImageInfo ii = dev->imageInfo();
1533 int w = ii.width();
1534 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001535
1536 SkImageFilter* filter = paint.getImageFilter();
1537 // This bitmap will own the filtered result as a texture.
1538 SkBitmap filteredBitmap;
1539
bsalomon49f085d2014-09-05 13:34:00 -07001540 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001541 SkIPoint offset = SkIPoint::Make(0, 0);
1542 SkMatrix matrix(*draw.fMatrix);
1543 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001544 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001545 // This cache is transient, and is freed (along with all its contained
1546 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001547 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001548 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001549 if (this->filterTexture(fContext, devTex, filter, ctx, &filteredBitmap,
1550 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001551 devTex = filteredBitmap.getTexture();
1552 w = filteredBitmap.width();
1553 h = filteredBitmap.height();
1554 x += offset.fX;
1555 y += offset.fY;
1556 } else {
1557 return;
1558 }
1559 }
1560
1561 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001562 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001563
dandov9de5b512014-06-10 14:38:28 -07001564 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1565 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001566
1567 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1568 SkIntToScalar(y),
1569 SkIntToScalar(w),
1570 SkIntToScalar(h));
1571
1572 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1573 // scratch texture).
1574 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1575 SK_Scalar1 * h / devTex->height());
1576
joshualitt5531d512014-12-17 15:50:11 -08001577 fContext->drawRectToRect(grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001578}
1579
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001580bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001581 return filter->canFilterImageGPU();
1582}
1583
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001584bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001585 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001586 SkBitmap* result, SkIPoint* offset) {
1587 // want explicitly our impl, so guard against a subclass of us overriding it
1588 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1589 return false;
1590 }
1591
1592 SkAutoLockPixels alp(src, !src.getTexture());
1593 if (!src.getTexture() && !src.readyToDraw()) {
1594 return false;
1595 }
1596
1597 GrTexture* texture;
1598 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1599 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001600 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001601
fmalita2d97bc12014-11-20 10:44:58 -08001602 return this->filterTexture(fContext, texture, filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001603}
1604
1605///////////////////////////////////////////////////////////////////////////////
1606
1607// must be in SkCanvas::VertexMode order
1608static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1609 kTriangles_GrPrimitiveType,
1610 kTriangleStrip_GrPrimitiveType,
1611 kTriangleFan_GrPrimitiveType,
1612};
1613
1614void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1615 int vertexCount, const SkPoint vertices[],
1616 const SkPoint texs[], const SkColor colors[],
1617 SkXfermode* xmode,
1618 const uint16_t indices[], int indexCount,
1619 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001620 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001621 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001622
dandov32a311b2014-07-15 19:46:26 -07001623 const uint16_t* outIndices;
1624 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1625 GrPrimitiveType primType;
1626 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001627
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001628 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1629 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001630
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001631 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001632
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001633 SkPaint copy(paint);
1634 copy.setStyle(SkPaint::kStroke_Style);
1635 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001636
dandov32a311b2014-07-15 19:46:26 -07001637 // we ignore the shader if texs is null.
1638 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1639 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001640
dandov32a311b2014-07-15 19:46:26 -07001641 primType = kLines_GrPrimitiveType;
1642 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001643 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001644 switch (vmode) {
1645 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001646 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001647 break;
1648 case SkCanvas::kTriangleStrip_VertexMode:
1649 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001650 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001651 break;
1652 }
mtklein533eb782014-08-27 10:39:42 -07001653
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001654 VertState state(vertexCount, indices, indexCount);
1655 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001656
dandov32a311b2014-07-15 19:46:26 -07001657 //number of indices for lines per triangle with kLines
1658 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001659
dandov32a311b2014-07-15 19:46:26 -07001660 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1661 outIndices = outAlloc.get();
1662 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001663 int i = 0;
1664 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001665 auxIndices[i] = state.f0;
1666 auxIndices[i + 1] = state.f1;
1667 auxIndices[i + 2] = state.f1;
1668 auxIndices[i + 3] = state.f2;
1669 auxIndices[i + 4] = state.f2;
1670 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001671 i += 6;
1672 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001673 } else {
dandov32a311b2014-07-15 19:46:26 -07001674 outIndices = indices;
1675 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001676
dandov32a311b2014-07-15 19:46:26 -07001677 if (NULL == texs || NULL == paint.getShader()) {
1678 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1679 NULL == colors, &grPaint);
1680 } else {
joshualitt5531d512014-12-17 15:50:11 -08001681 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, NULL == colors, &grPaint);
dandov32a311b2014-07-15 19:46:26 -07001682 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001683 }
1684
mtklein2583b622014-06-04 08:20:41 -07001685#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001686 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001687 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1688 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001689 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001690 }
1691 }
mtklein2583b622014-06-04 08:20:41 -07001692#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001693
1694 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001695 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001696 // need to convert byte order and from non-PM to PM
1697 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001698 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001699 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001700 color = colors[i];
1701 if (paint.getAlpha() != 255) {
1702 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1703 }
1704 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001705 }
1706 colors = convertedColors.get();
1707 }
1708 fContext->drawVertices(grPaint,
joshualitt5531d512014-12-17 15:50:11 -08001709 *draw.fMatrix,
dandov32a311b2014-07-15 19:46:26 -07001710 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001711 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001712 vertices,
1713 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001714 colors,
dandov32a311b2014-07-15 19:46:26 -07001715 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001716 indexCount);
1717}
1718
1719///////////////////////////////////////////////////////////////////////////////
1720
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001721void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001722 size_t byteLength, SkScalar x, SkScalar y,
1723 const SkPaint& paint) {
1724 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001725 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001726
jvanverth8c27a182014-10-14 08:45:50 -07001727 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -08001728 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001729
jvanverth8c27a182014-10-14 08:45:50 -07001730 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001731
joshualitt5531d512014-12-17 15:50:11 -08001732 if (!fTextContext->drawText(grPaint, paint, *draw.fMatrix, (const char *)text, byteLength, x,
1733 y)) {
jvanverth8c27a182014-10-14 08:45:50 -07001734 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001735 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001736 }
1737}
1738
fmalita05c4a432014-09-29 06:29:53 -07001739void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1740 const SkScalar pos[], int scalarsPerPos,
1741 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001742 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001743 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001744
jvanverth8c27a182014-10-14 08:45:50 -07001745 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -08001746 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001747
jvanverth8c27a182014-10-14 08:45:50 -07001748 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001749
joshualitt5531d512014-12-17 15:50:11 -08001750 if (!fTextContext->drawPosText(grPaint, paint, *draw.fMatrix, (const char *)text, byteLength,
1751 pos, scalarsPerPos, offset)) {
jvanverth8c27a182014-10-14 08:45:50 -07001752 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001753 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001754 }
1755}
1756
1757void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001758 size_t len, const SkPath& path,
1759 const SkMatrix* m, const SkPaint& paint) {
1760 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001761
1762 SkASSERT(draw.fDevice == this);
1763 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1764}
1765
1766///////////////////////////////////////////////////////////////////////////////
1767
reedb2db8982014-11-13 12:41:02 -08001768bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001769 if (paint.getShader() ||
reedb2db8982014-11-13 12:41:02 -08001770 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001771 paint.getMaskFilter() ||
1772 paint.getRasterizer() ||
1773 paint.getColorFilter() ||
1774 paint.getPathEffect() ||
1775 paint.isFakeBoldText() ||
reedb2db8982014-11-13 12:41:02 -08001776 paint.getStyle() != SkPaint::kFill_Style)
1777 {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001778 return true;
1779 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001780 return false;
1781}
1782
1783void SkGpuDevice::flush() {
1784 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001785 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001786}
1787
1788///////////////////////////////////////////////////////////////////////////////
1789
fmalita6987dca2014-11-13 08:33:37 -08001790SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(const CreateInfo& cinfo) {
bsalomonf2703d82014-10-28 14:33:06 -07001791 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001792 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001793 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001794 desc.fWidth = cinfo.fInfo.width();
1795 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001796 desc.fSampleCnt = fRenderTarget->numSamples();
1797
1798 SkAutoTUnref<GrTexture> texture;
1799 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001800 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
jvanverth4736e142014-11-07 07:12:46 -08001801 // If we're using distance field text, enable in the new device
1802 flags |= (fFlags & kDFText_Flag) ? kDFText_Flag : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001803
1804#if CACHE_COMPATIBLE_DEVICE_TEXTURES
hcm4396fa52014-10-27 21:43:30 -07001805 // layers are never draw in repeat modes, so we can request an approx
1806 // match and ignore any padding.
fmalita6987dca2014-11-13 08:33:37 -08001807 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == cinfo.fUsage) ?
hcm4396fa52014-10-27 21:43:30 -07001808 GrContext::kApprox_ScratchTexMatch :
1809 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001810 texture.reset(fContext->refScratchTexture(desc, match));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001811#else
1812 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1813#endif
bsalomon49f085d2014-09-05 13:34:00 -07001814 if (texture.get()) {
reedb2db8982014-11-13 12:41:02 -08001815 return SkGpuDevice::Create(texture, SkSurfaceProps(0, cinfo.fPixelGeometry), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001816 } else {
tfarina38406c82014-10-31 07:11:12 -07001817 SkDebugf("---- failed to create compatible device texture [%d %d]\n",
fmalita6987dca2014-11-13 08:33:37 -08001818 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001819 return NULL;
1820 }
1821}
1822
reed4a8126e2014-09-22 07:29:03 -07001823SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1824 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001825}
1826
robertphillips30d2cc62014-09-24 08:52:18 -07001827bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001828 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001829#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001830 // todo: should handle this natively
1831 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001832 return false;
1833 }
1834
robertphillips82365912014-11-12 09:32:34 -08001835 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips81f71b62014-11-11 04:54:49 -08001836
1837 const SkPicture::AccelData* data = mainPicture->EXPERIMENTAL_getAccelData(key);
1838 if (!data) {
1839 return false;
1840 }
1841
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001842 SkRect clipBounds;
robertphillips0c423322014-07-31 11:02:38 -07001843 if (!mainCanvas->getClipBounds(&clipBounds)) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001844 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001845 }
1846
robertphillips30d78412014-11-24 09:49:17 -08001847 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1848
1849 const SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1850
robertphillipsfd61ed02014-10-28 07:21:44 -07001851 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001852
robertphillipsfd61ed02014-10-28 07:21:44 -07001853 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001854 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001855 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001856 &atlasedNeedRendering, &atlasedRecycled,
1857 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001858
1859 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1860
1861 SkTDArray<GrHoistedLayer> needRendering, recycled;
1862
1863 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001864 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001865 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001866 &needRendering, &recycled,
1867 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001868
1869 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001870
robertphillips64bf7672014-08-21 13:07:35 -07001871 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001872 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1873 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001874
robertphillipsfd61ed02014-10-28 07:21:44 -07001875 GrLayerHoister::UnlockLayers(fContext, needRendering);
1876 GrLayerHoister::UnlockLayers(fContext, recycled);
1877 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1878 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001879
1880 return true;
robertphillips63242d72014-12-04 08:31:02 -08001881#else
1882 return false;
1883#endif
robertphillips64bf7672014-08-21 13:07:35 -07001884}
1885
senorblancobe129b22014-08-08 07:14:35 -07001886SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001887 // We always return a transient cache, so it is freed after each
1888 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001889 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001890}
reedf037e0b2014-10-30 11:34:15 -07001891
1892#endif