blob: 5781a58cf7b8320724909f39fe5b9a84e560ed65 [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"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000012#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000013#include "effects/GrSimpleTextureEffect.h"
14
15#include "GrContext.h"
16#include "GrBitmapTextContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000017#include "GrDistanceFieldTextContext.h"
robertphillips98d709b2014-09-02 10:20:50 -070018#include "GrLayerHoister.h"
robertphillips274b4ba2014-09-04 07:24:18 -070019#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070020#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070021#include "GrTracing.h"
derekff4555aa2014-10-06 12:19:12 -070022#include "GrGpu.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000023
24#include "SkGrTexturePixelRef.h"
25
robertphillips30d78412014-11-24 09:49:17 -080026#include "SkCanvasPriv.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000027#include "SkDeviceImageFilterProxy.h"
28#include "SkDrawProcs.h"
29#include "SkGlyphCache.h"
30#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080031#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000032#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000034#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070035#include "SkPictureData.h"
robertphillips274b4ba2014-09-04 07:24:18 -070036#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkRRect.h"
38#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000039#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000040#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000041#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000042#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070043#include "SkXfermode.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000044#include "SkErrorInternals.h"
45
reedf037e0b2014-10-30 11:34:15 -070046#if SK_SUPPORT_GPU
47
senorblanco55b6d8b2014-07-30 11:26:46 -070048enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
49
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000050#if 0
51 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080052 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000053 do { \
54 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080055 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000056 } while (0)
57#else
joshualitt5531d512014-12-17 15:50:11 -080058 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000059#endif
60
61// This constant represents the screen alignment criterion in texels for
62// requiring texture domain clamping to prevent color bleeding when drawing
63// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000064#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000065
66#define DO_DEFERRED_CLEAR() \
67 do { \
bsalomonafe30052015-01-16 07:32:33 -080068 if (fNeedClear) { \
69 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000070 } \
71 } while (false) \
72
73///////////////////////////////////////////////////////////////////////////////
74
75#define CHECK_FOR_ANNOTATION(paint) \
76 do { if (paint.getAnnotation()) { return; } } while (0)
77
78///////////////////////////////////////////////////////////////////////////////
79
bsalomonbcf0a522014-10-08 08:40:09 -070080// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
81// just accesses the backing GrTexture. Otherwise, it creates a cached texture
82// representation and releases it in the destructor.
83class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040084public:
bsalomonbcf0a522014-10-08 08:40:09 -070085 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070086
bsalomonbcf0a522014-10-08 08:40:09 -070087 AutoBitmapTexture(GrContext* context,
88 const SkBitmap& bitmap,
89 const GrTextureParams* params,
90 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040091 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070092 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040093 }
94
bsalomonbcf0a522014-10-08 08:40:09 -070095 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040096 const SkBitmap& bitmap,
97 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -070098 // Either get the texture directly from the bitmap, or else use the cache and
99 // remember to unref it.
100 if (GrTexture* bmpTexture = bitmap.getTexture()) {
101 fTexture.reset(NULL);
102 return bmpTexture;
103 } else {
104 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
105 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400106 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400107 }
108
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000109private:
bsalomonbcf0a522014-10-08 08:40:09 -0700110 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000111};
112
113///////////////////////////////////////////////////////////////////////////////
114
115struct GrSkDrawProcs : public SkDrawProcs {
116public:
117 GrContext* fContext;
118 GrTextContext* fTextContext;
119 GrFontScaler* fFontScaler; // cached in the skia glyphcache
120};
121
122///////////////////////////////////////////////////////////////////////////////
123
bsalomonafe30052015-01-16 07:32:33 -0800124SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags) {
125 if (!rt || rt->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000126 return NULL;
127 }
bsalomonafe30052015-01-16 07:32:33 -0800128 return SkNEW_ARGS(SkGpuDevice, (rt, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000129}
130
bsalomonafe30052015-01-16 07:32:33 -0800131static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps* props) {
132 if (props) {
133 return SkDeviceProperties(props->pixelGeometry());
134 } else {
135 return SkDeviceProperties(SkDeviceProperties::kLegacyLCD_InitType);
136 }
reedb2db8982014-11-13 12:41:02 -0800137}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000138
bsalomonafe30052015-01-16 07:32:33 -0800139static SkSurfaceProps copy_or_default_props(const SkSurfaceProps* props) {
140 if (props) {
141 return SkSurfaceProps(*props);
142 } else {
143 return SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
144 }
145}
146
147SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags)
reedb2db8982014-11-13 12:41:02 -0800148 : INHERITED(surfaceprops_to_deviceprops(props))
bsalomonafe30052015-01-16 07:32:33 -0800149 , fSurfaceProps(copy_or_default_props(props))
reedb2db8982014-11-13 12:41:02 -0800150{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000151 fDrawProcs = NULL;
152
bsalomonafe30052015-01-16 07:32:33 -0800153 fContext = SkRef(rt->getContext());
154 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000155
bsalomonafe30052015-01-16 07:32:33 -0800156 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000157
bsalomonafe30052015-01-16 07:32:33 -0800158 SkImageInfo info = rt->surfacePriv().info();
159 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt));
bsalomonafbf2d62014-09-30 12:18:44 -0700160 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700161 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700162
bsalomonafe30052015-01-16 07:32:33 -0800163 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts();
jvanverth4736e142014-11-07 07:12:46 -0800164 fTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFT);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000165}
166
bsalomonafe30052015-01-16 07:32:33 -0800167SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
168 const SkImageInfo& origInfo, int sampleCount,
169 const SkSurfaceProps* props, unsigned flags) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000170 if (kUnknown_SkColorType == origInfo.colorType() ||
171 origInfo.width() < 0 || origInfo.height() < 0) {
172 return NULL;
173 }
174
bsalomonafe30052015-01-16 07:32:33 -0800175 if (!context) {
176 return NULL;
177 }
178
reede5ea5002014-09-03 11:54:58 -0700179 SkColorType ct = origInfo.colorType();
180 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700181 if (kRGB_565_SkColorType == ct) {
182 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800183 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
184 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700185 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800186 }
187 if (kOpaque_SkAlphaType != at) {
188 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000189 }
reede5ea5002014-09-03 11:54:58 -0700190 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000191
bsalomonf2703d82014-10-28 14:33:06 -0700192 GrSurfaceDesc desc;
193 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000194 desc.fWidth = info.width();
195 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000196 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000197 desc.fSampleCnt = sampleCount;
198
bsalomond0423582015-02-06 08:49:24 -0800199 SkAutoTUnref<GrTexture> texture(context->createTexture(desc, SkToBool(budgeted), NULL, 0));
bsalomonafe30052015-01-16 07:32:33 -0800200 if (!texture) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000201 return NULL;
202 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000203
bsalomonafe30052015-01-16 07:32:33 -0800204 return SkNEW_ARGS(SkGpuDevice, (texture->asRenderTarget(), props, flags));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000205}
206
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000207SkGpuDevice::~SkGpuDevice() {
208 if (fDrawProcs) {
209 delete fDrawProcs;
210 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000211
jvanverth8c27a182014-10-14 08:45:50 -0700212 delete fTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000213
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000214 if (fContext->getClip() == &fClipData) {
215 fContext->setClip(NULL);
216 }
217
bsalomon32d0b3b2014-08-29 07:50:23 -0700218 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000219 fContext->unref();
220}
221
222///////////////////////////////////////////////////////////////////////////////
223
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000224bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
225 int x, int y) {
226 DO_DEFERRED_CLEAR();
227
228 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000229 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000230 if (kUnknown_GrPixelConfig == config) {
231 return false;
232 }
233
234 uint32_t flags = 0;
235 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
236 flags = GrContext::kUnpremul_PixelOpsFlag;
237 }
238 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
239 config, dstPixels, dstRowBytes, flags);
240}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000241
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000242bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
243 int x, int y) {
244 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000245 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000246 if (kUnknown_GrPixelConfig == config) {
247 return false;
248 }
249 uint32_t flags = 0;
250 if (kUnpremul_SkAlphaType == info.alphaType()) {
251 flags = GrContext::kUnpremul_PixelOpsFlag;
252 }
253 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
254
255 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700256 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000257
258 return true;
259}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000260
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000261const SkBitmap& SkGpuDevice::onAccessBitmap() {
262 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700263 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000264}
265
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000266void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
267 INHERITED::onAttachToCanvas(canvas);
268
269 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualittde358a92015-02-05 08:19:35 -0800270 fClipData.fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000271}
272
273void SkGpuDevice::onDetachFromCanvas() {
274 INHERITED::onDetachFromCanvas();
joshualittde358a92015-02-05 08:19:35 -0800275 fClipData.fClipStack.reset(NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000276}
277
278// call this every draw call, to ensure that the context reflects our state,
279// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800280void SkGpuDevice::prepareDraw(const SkDraw& draw) {
bsalomon49f085d2014-09-05 13:34:00 -0700281 SkASSERT(fClipData.fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000282
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000283 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
284
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000285 fClipData.fOrigin = this->getOrigin();
286
287 fContext->setClip(&fClipData);
288
289 DO_DEFERRED_CLEAR();
290}
291
292GrRenderTarget* SkGpuDevice::accessRenderTarget() {
293 DO_DEFERRED_CLEAR();
294 return fRenderTarget;
295}
296
reed8eddfb52014-12-04 07:50:14 -0800297void SkGpuDevice::clearAll() {
298 GrColor color = 0;
299 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
300 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
301 fContext->clear(&rect, color, true, fRenderTarget);
bsalomonafe30052015-01-16 07:32:33 -0800302 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800303}
304
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000305///////////////////////////////////////////////////////////////////////////////
306
307SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
308SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
309SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
310SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
311SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
312 shader_type_mismatch);
313SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
314 shader_type_mismatch);
315SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
316SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
317
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000318///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000319
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000320void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800321 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700322 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323
324 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800325 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000326
joshualitt25d9c152015-02-18 12:29:52 -0800327 fContext->drawPaint(fRenderTarget, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000328}
329
330// must be in SkCanvas::PointMode order
331static const GrPrimitiveType gPointMode2PrimtiveType[] = {
332 kPoints_GrPrimitiveType,
333 kLines_GrPrimitiveType,
334 kLineStrip_GrPrimitiveType
335};
336
337void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
338 size_t count, const SkPoint pts[], const SkPaint& paint) {
339 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800340 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000341
342 SkScalar width = paint.getStrokeWidth();
343 if (width < 0) {
344 return;
345 }
346
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000347 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700348 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
349 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800350 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
egdaniele61c4112014-06-12 10:24:21 -0700351 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700352 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700353 path.moveTo(pts[0]);
354 path.lineTo(pts[1]);
joshualitt25d9c152015-02-18 12:29:52 -0800355 fContext->drawPath(fRenderTarget, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700356 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000357 }
358
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000359 // we only handle hairlines and paints without path effects or mask filters,
360 // else we let the SkDraw call our drawPath()
361 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
362 draw.drawPoints(mode, count, pts, paint, true);
363 return;
364 }
365
366 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800367 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000368
joshualitt25d9c152015-02-18 12:29:52 -0800369 fContext->drawVertices(fRenderTarget,
370 grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800371 *draw.fMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000372 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000373 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000374 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000375 NULL,
376 NULL,
377 NULL,
378 0);
379}
380
381///////////////////////////////////////////////////////////////////////////////
382
383void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
384 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700385 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
386
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000387 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800388 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000389
390 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
391 SkScalar width = paint.getStrokeWidth();
392
393 /*
394 We have special code for hairline strokes, miter-strokes, bevel-stroke
395 and fills. Anything else we just call our path code.
396 */
397 bool usePath = doStroke && width > 0 &&
398 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
399 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
400 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700401
402 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000403 usePath = true;
404 }
egdanield58a0ba2014-06-11 10:30:05 -0700405
joshualitt5531d512014-12-17 15:50:11 -0800406 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000407#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
408 if (doStroke) {
409#endif
410 usePath = true;
411#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
412 } else {
joshualitt5531d512014-12-17 15:50:11 -0800413 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000414 }
415#endif
416 }
417 // until we can both stroke and fill rectangles
418 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
419 usePath = true;
420 }
421
egdanield58a0ba2014-06-11 10:30:05 -0700422 GrStrokeInfo strokeInfo(paint);
423
424 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700425 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700426 usePath = true;
427 }
428
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000429 if (usePath) {
430 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700431 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000432 path.addRect(rect);
433 this->drawPath(draw, path, paint, NULL, true);
434 return;
435 }
436
437 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800438 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400439
joshualitt25d9c152015-02-18 12:29:52 -0800440 fContext->drawRect(fRenderTarget, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000441}
442
443///////////////////////////////////////////////////////////////////////////////
444
445void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800446 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700447 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000448 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800449 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000450
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000451 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800452 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400453
egdanield58a0ba2014-06-11 10:30:05 -0700454 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000455 if (paint.getMaskFilter()) {
456 // try to hit the fast path for drawing filtered round rects
457
458 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800459 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000460 if (devRRect.allCornersCircular()) {
461 SkRect maskRect;
462 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800463 draw.fClip->getBounds(),
464 *draw.fMatrix,
465 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000466 SkIRect finalIRect;
467 maskRect.roundOut(&finalIRect);
468 if (draw.fClip->quickReject(finalIRect)) {
469 // clipped out
470 return;
471 }
joshualitt25d9c152015-02-18 12:29:52 -0800472 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
473 fRenderTarget,
474 &grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800475 *draw.fMatrix,
egdanield58a0ba2014-06-11 10:30:05 -0700476 strokeInfo.getStrokeRec(),
477 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000478 return;
479 }
480 }
481
482 }
483 }
484
485 }
486
egdanield58a0ba2014-06-11 10:30:05 -0700487 bool usePath = false;
488
489 if (paint.getMaskFilter()) {
490 usePath = true;
491 } else {
492 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700493 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700494 usePath = true;
495 }
496 }
497
498
499 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000500 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700501 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000502 path.addRRect(rect);
503 this->drawPath(draw, path, paint, NULL, true);
504 return;
505 }
Mike Klein744fb732014-06-23 15:13:26 -0400506
joshualitt25d9c152015-02-18 12:29:52 -0800507 fContext->drawRRect(fRenderTarget, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000508}
509
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000510void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800511 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000512 SkStrokeRec stroke(paint);
513 if (stroke.isFillStyle()) {
514
515 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800516 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000517
518 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800519 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000520
521 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
joshualitt25d9c152015-02-18 12:29:52 -0800522 fContext->drawDRRect(fRenderTarget, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000523 return;
524 }
525 }
526
527 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700528 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000529 path.addRRect(outer);
530 path.addRRect(inner);
531 path.setFillType(SkPath::kEvenOdd_FillType);
532
533 this->drawPath(draw, path, paint, NULL, true);
534}
535
536
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000537/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000538
539void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
540 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700541 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000542 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800543 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000544
egdanield58a0ba2014-06-11 10:30:05 -0700545 GrStrokeInfo strokeInfo(paint);
546
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000547 bool usePath = false;
548 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700549 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700551 } else {
552 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700553 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700554 usePath = true;
555 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000556 }
557
558 if (usePath) {
559 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700560 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000561 path.addOval(oval);
562 this->drawPath(draw, path, paint, NULL, true);
563 return;
564 }
565
566 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800567 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000568
joshualitt25d9c152015-02-18 12:29:52 -0800569 fContext->drawOval(fRenderTarget, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000570}
571
572#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000573
574///////////////////////////////////////////////////////////////////////////////
575
576// helpers for applying mask filters
577namespace {
578
579// Draw a mask using the supplied paint. Since the coverage/geometry
580// is already burnt into the mask this boils down to a rect draw.
581// Return true if the mask was successfully drawn.
joshualitt25d9c152015-02-18 12:29:52 -0800582bool draw_mask(GrContext* context,
583 GrRenderTarget* rt,
584 const SkMatrix& viewMatrix,
585 const SkRect& maskRect,
586 GrPaint* grp,
587 GrTexture* mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000588 SkMatrix matrix;
589 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
590 matrix.postIDiv(mask->width(), mask->height());
591
joshualitt16b27892014-12-18 07:47:16 -0800592 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix,
593 kDevice_GrCoordSet))->unref();
594
595 SkMatrix inverse;
596 if (!viewMatrix.invert(&inverse)) {
597 return false;
598 }
joshualitt25d9c152015-02-18 12:29:52 -0800599 context->drawNonAARectWithLocalMatrix(rt, *grp, SkMatrix::I(), maskRect, inverse);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000600 return true;
601}
602
joshualitt25d9c152015-02-18 12:29:52 -0800603bool draw_with_mask_filter(GrContext* context,
604 GrRenderTarget* rt,
605 const SkMatrix& viewMatrix,
606 const SkPath& devPath,
607 SkMaskFilter* filter,
608 const SkRegion& clip,
609 GrPaint* grp,
610 SkPaint::Style style) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000611 SkMask srcM, dstM;
612
joshualitt5531d512014-12-17 15:50:11 -0800613 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &viewMatrix, &srcM,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000614 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
615 return false;
616 }
617 SkAutoMaskFreeImage autoSrc(srcM.fImage);
618
joshualitt5531d512014-12-17 15:50:11 -0800619 if (!filter->filterMask(&dstM, srcM, viewMatrix, NULL)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000620 return false;
621 }
622 // this will free-up dstM when we're done (allocated in filterMask())
623 SkAutoMaskFreeImage autoDst(dstM.fImage);
624
625 if (clip.quickReject(dstM.fBounds)) {
626 return false;
627 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000628
629 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
630 // the current clip (and identity matrix) and GrPaint settings
bsalomonf2703d82014-10-28 14:33:06 -0700631 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000632 desc.fWidth = dstM.fBounds.width();
633 desc.fHeight = dstM.fBounds.height();
634 desc.fConfig = kAlpha_8_GrPixelConfig;
635
bsalomone3059732014-10-14 11:47:22 -0700636 SkAutoTUnref<GrTexture> texture(
637 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
638 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000639 return false;
640 }
641 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
642 dstM.fImage, dstM.fRowBytes);
643
644 SkRect maskRect = SkRect::Make(dstM.fBounds);
645
joshualitt25d9c152015-02-18 12:29:52 -0800646 return draw_mask(context, rt, viewMatrix, maskRect, grp, texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000647}
648
bsalomone3059732014-10-14 11:47:22 -0700649// Create a mask of 'devPath' and place the result in 'mask'.
650GrTexture* create_mask_GPU(GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800651 GrRenderTarget* rt,
bsalomone3059732014-10-14 11:47:22 -0700652 const SkRect& maskRect,
653 const SkPath& devPath,
654 const GrStrokeInfo& strokeInfo,
655 bool doAA,
656 int sampleCnt) {
bsalomonf2703d82014-10-28 14:33:06 -0700657 GrSurfaceDesc desc;
658 desc.fFlags = kRenderTarget_GrSurfaceFlag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000659 desc.fWidth = SkScalarCeilToInt(maskRect.width());
660 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700661 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000662 // We actually only need A8, but it often isn't supported as a
663 // render target so default to RGBA_8888
664 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700665
666 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
667 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000668 desc.fConfig = kAlpha_8_GrPixelConfig;
669 }
670
bsalomone3059732014-10-14 11:47:22 -0700671 GrTexture* mask = context->refScratchTexture(desc,GrContext::kApprox_ScratchTexMatch);
672 if (NULL == mask) {
673 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000674 }
675
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000676 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
677
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000678 GrContext::AutoClip ac(context, clipRect);
679
bsalomon89c62982014-11-03 12:08:42 -0800680 context->clear(NULL, 0x0, true, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000681
682 GrPaint tempPaint;
egdanielb197b8f2015-02-17 07:34:43 -0800683 tempPaint.setAntiAlias(doAA);
684 tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000685
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000686 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
687 SkMatrix translate;
688 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
joshualitt25d9c152015-02-18 12:29:52 -0800689 context->drawPath(mask->asRenderTarget(), tempPaint, translate, devPath, strokeInfo);
bsalomone3059732014-10-14 11:47:22 -0700690 return mask;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000691}
692
693SkBitmap wrap_texture(GrTexture* texture) {
694 SkBitmap result;
bsalomonafbf2d62014-09-30 12:18:44 -0700695 result.setInfo(texture->surfacePriv().info());
reed6c225732014-06-09 19:52:07 -0700696 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000697 return result;
698}
699
700};
701
702void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
703 const SkPaint& paint, const SkMatrix* prePathMatrix,
704 bool pathIsMutable) {
705 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800706 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700707 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000708
jvanverthb3eb6872014-10-24 07:12:51 -0700709 SkASSERT(!pathIsMutable || origSrcPath.isVolatile());
joshualitt5531d512014-12-17 15:50:11 -0800710
bsalomon54443932015-01-29 09:34:18 -0800711 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000712
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000713 // If we have a prematrix, apply it to the path, optimizing for the case
714 // where the original path can in fact be modified in place (even though
715 // its parameter type is const).
716 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000717 SkTLazy<SkPath> tmpPath;
718 SkTLazy<SkPath> effectPath;
bsalomon54443932015-01-29 09:34:18 -0800719 SkPathEffect* pathEffect = paint.getPathEffect();
720
721 SkMatrix viewMatrix = *draw.fMatrix;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000722
723 if (prePathMatrix) {
bsalomon54443932015-01-29 09:34:18 -0800724 // stroking and path effects are supposed to be applied *after* the prePathMatrix.
725 // The pre-path-matrix also should not affect shadeing.
726 if (NULL == pathEffect && NULL == paint.getShader() &&
727 (strokeInfo.getStrokeRec().isFillStyle() ||
728 strokeInfo.getStrokeRec().isHairlineStyle())) {
729 viewMatrix.preConcat(*prePathMatrix);
730 } else {
731 SkPath* result = pathPtr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000732
bsalomon54443932015-01-29 09:34:18 -0800733 if (!pathIsMutable) {
734 result = tmpPath.init();
735 result->setIsVolatile(true);
736 pathIsMutable = true;
737 }
738 // should I push prePathMatrix on our MV stack temporarily, instead
739 // of applying it here? See SkDraw.cpp
740 pathPtr->transform(*prePathMatrix, result);
741 pathPtr = result;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000743 }
744 // at this point we're done with prePathMatrix
745 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
746
bsalomon54443932015-01-29 09:34:18 -0800747 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800748 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, viewMatrix, true, &grPaint);
bsalomon54443932015-01-29 09:34:18 -0800749
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000750 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700751 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
752 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000753 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000754 pathPtr = effectPath.get();
755 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700756 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000757 }
758
egdanield58a0ba2014-06-11 10:30:05 -0700759 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000760 if (paint.getMaskFilter()) {
761 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000762 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
763 if (stroke.applyToPath(strokedPath, *pathPtr)) {
764 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000765 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700766 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000767 }
768 }
769
770 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000771 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700772 if (!pathIsMutable) {
773 devPathPtr->setIsVolatile(true);
774 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000775
776 // transform the path into device space
bsalomon54443932015-01-29 09:34:18 -0800777 pathPtr->transform(viewMatrix, devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000778
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000779 SkRect maskRect;
780 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
781 draw.fClip->getBounds(),
bsalomon54443932015-01-29 09:34:18 -0800782 viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000783 &maskRect)) {
784 SkIRect finalIRect;
785 maskRect.roundOut(&finalIRect);
786 if (draw.fClip->quickReject(finalIRect)) {
787 // clipped out
788 return;
789 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000790
joshualitt25d9c152015-02-18 12:29:52 -0800791 if (paint.getMaskFilter()->directFilterMaskGPU(fContext,
792 fRenderTarget,
793 &grPaint,
794 viewMatrix,
795 stroke,
796 *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000797 // the mask filter was able to draw itself directly, so there's nothing
798 // left to do.
799 return;
800 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000801
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000802
joshualitt25d9c152015-02-18 12:29:52 -0800803 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext,
804 fRenderTarget,
805 maskRect,
806 *devPathPtr,
807 strokeInfo,
808 grPaint.isAntiAlias(),
bsalomone3059732014-10-14 11:47:22 -0700809 fRenderTarget->numSamples()));
810 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000811 GrTexture* filtered;
812
bsalomon54443932015-01-29 09:34:18 -0800813 if (paint.getMaskFilter()->filterMaskGPU(mask, viewMatrix, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000814 // filterMaskGPU gives us ownership of a ref to the result
815 SkAutoTUnref<GrTexture> atu(filtered);
joshualitt25d9c152015-02-18 12:29:52 -0800816 if (draw_mask(fContext, fRenderTarget, viewMatrix, maskRect, &grPaint,
817 filtered)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000818 // This path is completely drawn
819 return;
820 }
821 }
822 }
823 }
824
825 // draw the mask on the CPU - this is a fallthrough path in case the
826 // GPU path fails
827 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
828 SkPaint::kFill_Style;
joshualitt25d9c152015-02-18 12:29:52 -0800829 draw_with_mask_filter(fContext, fRenderTarget, viewMatrix, *devPathPtr,
830 paint.getMaskFilter(), *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000831 return;
832 }
833
joshualitt25d9c152015-02-18 12:29:52 -0800834 fContext->drawPath(fRenderTarget, grPaint, viewMatrix, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000835}
836
837static const int kBmpSmallTileSize = 1 << 10;
838
839static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
840 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
841 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
842 return tilesX * tilesY;
843}
844
845static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
846 if (maxTileSize <= kBmpSmallTileSize) {
847 return maxTileSize;
848 }
849
850 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
851 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
852
853 maxTileTotalTileSize *= maxTileSize * maxTileSize;
854 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
855
856 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
857 return kBmpSmallTileSize;
858 } else {
859 return maxTileSize;
860 }
861}
862
863// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
864// pixels from the bitmap are necessary.
865static void determine_clipped_src_rect(const GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800866 const GrRenderTarget* rt,
joshualitt5531d512014-12-17 15:50:11 -0800867 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000868 const SkBitmap& bitmap,
869 const SkRect* srcRectPtr,
870 SkIRect* clippedSrcIRect) {
871 const GrClipData* clip = context->getClip();
joshualitt25d9c152015-02-18 12:29:52 -0800872 clip->getConservativeBounds(rt, clippedSrcIRect, NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000873 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800874 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000875 clippedSrcIRect->setEmpty();
876 return;
877 }
878 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
879 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700880 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000881 // we've setup src space 0,0 to map to the top left of the src rect.
882 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000883 if (!clippedSrcRect.intersect(*srcRectPtr)) {
884 clippedSrcIRect->setEmpty();
885 return;
886 }
887 }
888 clippedSrcRect.roundOut(clippedSrcIRect);
889 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
890 if (!clippedSrcIRect->intersect(bmpBounds)) {
891 clippedSrcIRect->setEmpty();
892 }
893}
894
895bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800896 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000897 const GrTextureParams& params,
898 const SkRect* srcRectPtr,
899 int maxTileSize,
900 int* tileSize,
901 SkIRect* clippedSrcRect) const {
902 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700903 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000904 return false;
905 }
906
907 // if it's larger than the max tile size, then we have no choice but tiling.
908 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt25d9c152015-02-18 12:29:52 -0800909 determine_clipped_src_rect(fContext, fRenderTarget, viewMatrix, bitmap, srcRectPtr,
910 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000911 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
912 return true;
913 }
914
915 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
916 return false;
917 }
918
919 // if the entire texture is already in our cache then no reason to tile it
920 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
921 return false;
922 }
923
924 // At this point we know we could do the draw by uploading the entire bitmap
925 // as a texture. However, if the texture would be large compared to the
926 // cache size and we don't require most of it for this draw then tile to
927 // reduce the amount of upload and cache spill.
928
929 // assumption here is that sw bitmap size is a good proxy for its size as
930 // a texture
931 size_t bmpSize = bitmap.getSize();
932 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000933 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000934 if (bmpSize < cacheSize / 2) {
935 return false;
936 }
937
938 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt25d9c152015-02-18 12:29:52 -0800939 determine_clipped_src_rect(fContext, fRenderTarget, viewMatrix, bitmap, srcRectPtr,
940 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000941 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
942 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
943 kBmpSmallTileSize * kBmpSmallTileSize;
944
945 return usedTileBytes < 2 * bmpSize;
946}
947
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000948void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000949 const SkBitmap& bitmap,
950 const SkMatrix& m,
951 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000952 SkMatrix concat;
953 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
954 if (!m.isIdentity()) {
955 concat.setConcat(*draw->fMatrix, m);
956 draw.writable()->fMatrix = &concat;
957 }
958 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000959}
960
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000961// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000962// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
963// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000964static inline void clamped_outset_with_offset(SkIRect* iRect,
965 int outset,
966 SkPoint* offset,
967 const SkIRect& clamp) {
968 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000969
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000970 int leftClampDelta = clamp.fLeft - iRect->fLeft;
971 if (leftClampDelta > 0) {
972 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000973 iRect->fLeft = clamp.fLeft;
974 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000975 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000976 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000977
978 int topClampDelta = clamp.fTop - iRect->fTop;
979 if (topClampDelta > 0) {
980 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000981 iRect->fTop = clamp.fTop;
982 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000983 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000984 }
985
986 if (iRect->fRight > clamp.fRight) {
987 iRect->fRight = clamp.fRight;
988 }
989 if (iRect->fBottom > clamp.fBottom) {
990 iRect->fBottom = clamp.fBottom;
991 }
992}
993
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000994static bool has_aligned_samples(const SkRect& srcRect,
995 const SkRect& transformedRect) {
996 // detect pixel disalignment
997 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
998 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
999 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1000 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1001 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1002 COLOR_BLEED_TOLERANCE &&
1003 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1004 COLOR_BLEED_TOLERANCE) {
1005 return true;
1006 }
1007 return false;
1008}
1009
1010static bool may_color_bleed(const SkRect& srcRect,
1011 const SkRect& transformedRect,
1012 const SkMatrix& m) {
1013 // Only gets called if has_aligned_samples returned false.
1014 // So we can assume that sampling is axis aligned but not texel aligned.
1015 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1016 SkRect innerSrcRect(srcRect), innerTransformedRect,
1017 outerTransformedRect(transformedRect);
1018 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1019 m.mapRect(&innerTransformedRect, innerSrcRect);
1020
1021 // The gap between outerTransformedRect and innerTransformedRect
1022 // represents the projection of the source border area, which is
1023 // problematic for color bleeding. We must check whether any
1024 // destination pixels sample the border area.
1025 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1026 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1027 SkIRect outer, inner;
1028 outerTransformedRect.round(&outer);
1029 innerTransformedRect.round(&inner);
1030 // If the inner and outer rects round to the same result, it means the
1031 // border does not overlap any pixel centers. Yay!
1032 return inner != outer;
1033}
1034
1035static bool needs_texture_domain(const SkBitmap& bitmap,
1036 const SkRect& srcRect,
1037 GrTextureParams &params,
1038 const SkMatrix& contextMatrix,
1039 bool bicubic) {
1040 bool needsTextureDomain = false;
1041
1042 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1043 // Need texture domain if drawing a sub rect
1044 needsTextureDomain = srcRect.width() < bitmap.width() ||
1045 srcRect.height() < bitmap.height();
1046 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1047 // sampling is axis-aligned
1048 SkRect transformedRect;
1049 contextMatrix.mapRect(&transformedRect, srcRect);
1050
1051 if (has_aligned_samples(srcRect, transformedRect)) {
1052 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1053 needsTextureDomain = false;
1054 } else {
1055 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1056 }
1057 }
1058 }
1059 return needsTextureDomain;
1060}
1061
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001062void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1063 const SkBitmap& bitmap,
1064 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001065 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001066 const SkPaint& paint,
1067 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -08001068 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001069
1070 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001071 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001072 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1073 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001074 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001075 SkScalar w = SkIntToScalar(bitmap.width());
1076 SkScalar h = SkIntToScalar(bitmap.height());
1077 dstSize.fWidth = w;
1078 dstSize.fHeight = h;
1079 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001080 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001081 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001082 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001083 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001084 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001085 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1086 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1087 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1088 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001089 }
1090
derekf367e1862014-12-02 11:02:06 -08001091 // If the render target is not msaa and draw is antialiased, we call
1092 // drawRect instead of drawing on the render target directly.
1093 // FIXME: the tiled bitmap code path doesn't currently support
1094 // anti-aliased edges, we work around that for now by drawing directly
1095 // if the image size exceeds maximum texture size.
1096 int maxTextureSize = fContext->getMaxTextureSize();
1097 bool directDraw = fRenderTarget->isMultisampled() ||
1098 !paint.isAntiAlias() ||
1099 bitmap.width() > maxTextureSize ||
1100 bitmap.height() > maxTextureSize;
1101
1102 // we check whether dst rect are pixel aligned
1103 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -08001104 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -08001105
1106 if (staysRect) {
1107 SkRect rect;
1108 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -08001109 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -08001110 const SkScalar *scalars = rect.asScalars();
1111 bool isDstPixelAligned = true;
1112 for (int i = 0; i < 4; i++) {
1113 if (!SkScalarIsInt(scalars[i])) {
1114 isDstPixelAligned = false;
1115 break;
1116 }
1117 }
1118
1119 if (isDstPixelAligned)
1120 directDraw = true;
1121 }
1122 }
1123
1124 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001125 // Convert the bitmap to a shader so that the rect can be drawn
1126 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001127 SkBitmap tmp; // subset of bitmap, if necessary
1128 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001129 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001130 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001131 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1132 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1133 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001134 // In bleed mode we position and trim the bitmap based on the src rect which is
1135 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1136 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1137 // compensate.
1138 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1139 SkIRect iSrc;
1140 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001141
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001142 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1143 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001144
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001145 if (!bitmap.extractSubset(&tmp, iSrc)) {
1146 return; // extraction failed
1147 }
1148 bitmapPtr = &tmp;
1149 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001150
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001151 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001152 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001153 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001154 } else {
1155 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001156 }
1157
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001158 SkPaint paintWithShader(paint);
1159 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001160 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001161 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1162 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001163
1164 return;
1165 }
1166
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001167 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1168 // the view matrix rather than a local matrix.
1169 SkMatrix m;
1170 m.setScale(dstSize.fWidth / srcRect.width(),
1171 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -08001172 SkMatrix viewM = *draw.fMatrix;
1173 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001174
1175 GrTextureParams params;
1176 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1177 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001178
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001179 bool doBicubic = false;
1180
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001181 switch(paintFilterLevel) {
1182 case SkPaint::kNone_FilterLevel:
1183 textureFilterMode = GrTextureParams::kNone_FilterMode;
1184 break;
1185 case SkPaint::kLow_FilterLevel:
1186 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1187 break;
1188 case SkPaint::kMedium_FilterLevel:
joshualitt5531d512014-12-17 15:50:11 -08001189 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001190 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1191 } else {
1192 // Don't trigger MIP level generation unnecessarily.
1193 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1194 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001196 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001197 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001198 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001199 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001200 break;
1201 default:
1202 SkErrorInternals::SetError( kInvalidPaint_SkError,
1203 "Sorry, I don't understand the filtering "
1204 "mode you asked for. Falling back to "
1205 "MIPMaps.");
1206 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1207 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001208 }
1209
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001210 int tileFilterPad;
1211 if (doBicubic) {
1212 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1213 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1214 tileFilterPad = 0;
1215 } else {
1216 tileFilterPad = 1;
1217 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001218 params.setFilterMode(textureFilterMode);
1219
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001220 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001221 int tileSize;
1222
1223 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001224 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001225 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001226 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1227 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001228 } else {
1229 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001230 bool needsTextureDomain = needs_texture_domain(bitmap,
1231 srcRect,
1232 params,
joshualitt5531d512014-12-17 15:50:11 -08001233 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001234 doBicubic);
1235 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001236 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001237 srcRect,
1238 params,
1239 paint,
1240 flags,
1241 doBicubic,
1242 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001243 }
1244}
1245
1246// Break 'bitmap' into several tiles to draw it since it has already
1247// been determined to be too large to fit in VRAM
1248void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001249 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001250 const SkRect& srcRect,
1251 const SkIRect& clippedSrcIRect,
1252 const GrTextureParams& params,
1253 const SkPaint& paint,
1254 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001255 int tileSize,
1256 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001257 // The following pixel lock is technically redundant, but it is desirable
1258 // to lock outside of the tile loop to prevent redecoding the whole image
1259 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1260 // is larger than the limit of the discardable memory pool.
1261 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001262 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1263
1264 int nx = bitmap.width() / tileSize;
1265 int ny = bitmap.height() / tileSize;
1266 for (int x = 0; x <= nx; x++) {
1267 for (int y = 0; y <= ny; y++) {
1268 SkRect tileR;
1269 tileR.set(SkIntToScalar(x * tileSize),
1270 SkIntToScalar(y * tileSize),
1271 SkIntToScalar((x + 1) * tileSize),
1272 SkIntToScalar((y + 1) * tileSize));
1273
1274 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1275 continue;
1276 }
1277
1278 if (!tileR.intersect(srcRect)) {
1279 continue;
1280 }
1281
1282 SkBitmap tmpB;
1283 SkIRect iTileR;
1284 tileR.roundOut(&iTileR);
1285 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1286 SkIntToScalar(iTileR.fTop));
1287
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001288 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001289 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001290 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001291 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001292 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001293
robertphillipsec8bb942014-11-21 10:16:25 -08001294 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001295 SkIRect iClampRect;
1296
1297 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1298 // In bleed mode we want to always expand the tile on all edges
1299 // but stay within the bitmap bounds
1300 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1301 } else {
1302 // In texture-domain/clamp mode we only want to expand the
1303 // tile on edges interior to "srcRect" (i.e., we want to
1304 // not bleed across the original clamped edges)
1305 srcRect.roundOut(&iClampRect);
1306 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001307 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1308 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001309 }
1310
1311 if (bitmap.extractSubset(&tmpB, iTileR)) {
1312 // now offset it to make it "local" to our tmp bitmap
1313 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001314 GrTextureParams paramsTemp = params;
1315 bool needsTextureDomain = needs_texture_domain(bitmap,
1316 srcRect,
1317 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001318 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001319 bicubic);
1320 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001321 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001322 tileR,
1323 paramsTemp,
1324 paint,
1325 flags,
1326 bicubic,
1327 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001328 }
1329 }
1330 }
1331}
1332
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001333
1334/*
1335 * This is called by drawBitmap(), which has to handle images that may be too
1336 * large to be represented by a single texture.
1337 *
1338 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1339 * and that non-texture portion of the GrPaint has already been setup.
1340 */
1341void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001342 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001343 const SkRect& srcRect,
1344 const GrTextureParams& params,
1345 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001346 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001347 bool bicubic,
1348 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001349 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1350 bitmap.height() <= fContext->getMaxTextureSize());
1351
1352 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001353 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001354 if (NULL == texture) {
1355 return;
1356 }
1357
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001358 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001359 SkRect paintRect;
1360 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1361 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1362 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1363 SkScalarMul(srcRect.fTop, hInv),
1364 SkScalarMul(srcRect.fRight, wInv),
1365 SkScalarMul(srcRect.fBottom, hInv));
1366
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001367 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001368 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001369 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001370 // Use a constrained texture domain to avoid color bleeding
1371 SkScalar left, top, right, bottom;
1372 if (srcRect.width() > SK_Scalar1) {
1373 SkScalar border = SK_ScalarHalf / texture->width();
1374 left = paintRect.left() + border;
1375 right = paintRect.right() - border;
1376 } else {
1377 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1378 }
1379 if (srcRect.height() > SK_Scalar1) {
1380 SkScalar border = SK_ScalarHalf / texture->height();
1381 top = paintRect.top() + border;
1382 bottom = paintRect.bottom() - border;
1383 } else {
1384 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1385 }
1386 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001387 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001388 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001389 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001390 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001391 SkMatrix::I(),
1392 textureDomain,
1393 GrTextureDomain::kClamp_Mode,
1394 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001395 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001396 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001397 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1398 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001399 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001400 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001401 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001402 }
1403
1404 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1405 // the rest from the SkPaint.
1406 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001407 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001408 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001409 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1410 SkColor2GrColor(paint.getColor());
joshualitt25d9c152015-02-18 12:29:52 -08001411 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001412
joshualitt25d9c152015-02-18 12:29:52 -08001413 fContext->drawNonAARectToRect(fRenderTarget, grPaint, viewMatrix, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001414}
1415
fmalita2d97bc12014-11-20 10:44:58 -08001416bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
1417 const SkImageFilter* filter,
1418 const SkImageFilter::Context& ctx,
1419 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001420 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001421
1422 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1423 // (https://code.google.com/p/skia/issues/detail?id=3148).
1424 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001425
1426 if (filter->canFilterImageGPU()) {
joshualitt25d9c152015-02-18 12:29:52 -08001427 // Set the clip wide open and the matrix to identity.
1428 GrContext::AutoWideOpenIdentityDraw awo(context);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001429 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001430 } else {
1431 return false;
1432 }
1433}
1434
1435void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1436 int left, int top, const SkPaint& paint) {
1437 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001438 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001439
1440 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1441 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1442 return;
1443 }
1444
1445 int w = bitmap.width();
1446 int h = bitmap.height();
1447
1448 GrTexture* texture;
1449 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001450 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001451
1452 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001453 // This bitmap will own the filtered result as a texture.
1454 SkBitmap filteredBitmap;
1455
bsalomon49f085d2014-09-05 13:34:00 -07001456 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001457 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001458 SkMatrix matrix(*draw.fMatrix);
1459 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001460 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001461 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001462 // This cache is transient, and is freed (along with all its contained
1463 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001464 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001465 if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap,
1466 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001467 texture = (GrTexture*) filteredBitmap.getTexture();
1468 w = filteredBitmap.width();
1469 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001470 left += offset.x();
1471 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001472 } else {
1473 return;
1474 }
1475 }
1476
1477 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001478 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001479
joshualitt25d9c152015-02-18 12:29:52 -08001480 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1481 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001482
joshualitt25d9c152015-02-18 12:29:52 -08001483 fContext->drawNonAARectToRect(fRenderTarget,
1484 grPaint,
joshualitt16b27892014-12-18 07:47:16 -08001485 SkMatrix::I(),
1486 SkRect::MakeXYWH(SkIntToScalar(left),
1487 SkIntToScalar(top),
1488 SkIntToScalar(w),
1489 SkIntToScalar(h)),
1490 SkRect::MakeXYWH(0,
1491 0,
1492 SK_Scalar1 * w / texture->width(),
1493 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001494}
1495
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001496void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001497 const SkRect* src, const SkRect& dst,
1498 const SkPaint& paint,
1499 SkCanvas::DrawBitmapRectFlags flags) {
1500 SkMatrix matrix;
1501 SkRect bitmapBounds, tmpSrc;
1502
1503 bitmapBounds.set(0, 0,
1504 SkIntToScalar(bitmap.width()),
1505 SkIntToScalar(bitmap.height()));
1506
1507 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001508 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001509 tmpSrc = *src;
1510 } else {
1511 tmpSrc = bitmapBounds;
1512 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001513
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001514 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1515
1516 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001517 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001518 if (!bitmapBounds.contains(tmpSrc)) {
1519 if (!tmpSrc.intersect(bitmapBounds)) {
1520 return; // nothing to draw
1521 }
1522 }
1523 }
1524
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001525 SkRect tmpDst;
1526 matrix.mapRect(&tmpDst, tmpSrc);
1527
1528 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1529 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1530 // Translate so that tempDst's top left is at the origin.
1531 matrix = *origDraw.fMatrix;
1532 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1533 draw.writable()->fMatrix = &matrix;
1534 }
1535 SkSize dstSize;
1536 dstSize.fWidth = tmpDst.width();
1537 dstSize.fHeight = tmpDst.height();
1538
1539 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001540}
1541
1542void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1543 int x, int y, const SkPaint& paint) {
1544 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001545 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001546 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001547
1548 // TODO: If the source device covers the whole of this device, we could
1549 // omit fNeedsClear -related flushing.
1550 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001551
1552 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001553 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001554
1555 GrRenderTarget* devRT = dev->accessRenderTarget();
1556 GrTexture* devTex;
1557 if (NULL == (devTex = devRT->asTexture())) {
1558 return;
1559 }
1560
robertphillips7b9e8a42014-12-11 08:20:31 -08001561 const SkImageInfo ii = dev->imageInfo();
1562 int w = ii.width();
1563 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001564
1565 SkImageFilter* filter = paint.getImageFilter();
1566 // This bitmap will own the filtered result as a texture.
1567 SkBitmap filteredBitmap;
1568
bsalomon49f085d2014-09-05 13:34:00 -07001569 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001570 SkIPoint offset = SkIPoint::Make(0, 0);
1571 SkMatrix matrix(*draw.fMatrix);
1572 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001573 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001574 // This cache is transient, and is freed (along with all its contained
1575 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001576 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001577 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001578 if (this->filterTexture(fContext, devTex, filter, ctx, &filteredBitmap,
1579 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001580 devTex = filteredBitmap.getTexture();
1581 w = filteredBitmap.width();
1582 h = filteredBitmap.height();
1583 x += offset.fX;
1584 y += offset.fY;
1585 } else {
1586 return;
1587 }
1588 }
1589
1590 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001591 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001592
joshualitt25d9c152015-02-18 12:29:52 -08001593 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1594 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001595
1596 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1597 SkIntToScalar(y),
1598 SkIntToScalar(w),
1599 SkIntToScalar(h));
1600
1601 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1602 // scratch texture).
1603 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1604 SK_Scalar1 * h / devTex->height());
1605
joshualitt25d9c152015-02-18 12:29:52 -08001606 fContext->drawNonAARectToRect(fRenderTarget, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001607}
1608
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001609bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001610 return filter->canFilterImageGPU();
1611}
1612
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001613bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001614 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001615 SkBitmap* result, SkIPoint* offset) {
1616 // want explicitly our impl, so guard against a subclass of us overriding it
1617 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1618 return false;
1619 }
1620
1621 SkAutoLockPixels alp(src, !src.getTexture());
1622 if (!src.getTexture() && !src.readyToDraw()) {
1623 return false;
1624 }
1625
1626 GrTexture* texture;
1627 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1628 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001629 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001630
fmalita2d97bc12014-11-20 10:44:58 -08001631 return this->filterTexture(fContext, texture, filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001632}
1633
1634///////////////////////////////////////////////////////////////////////////////
1635
1636// must be in SkCanvas::VertexMode order
1637static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1638 kTriangles_GrPrimitiveType,
1639 kTriangleStrip_GrPrimitiveType,
1640 kTriangleFan_GrPrimitiveType,
1641};
1642
1643void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1644 int vertexCount, const SkPoint vertices[],
1645 const SkPoint texs[], const SkColor colors[],
1646 SkXfermode* xmode,
1647 const uint16_t indices[], int indexCount,
1648 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001649 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001650 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001651
dandov32a311b2014-07-15 19:46:26 -07001652 const uint16_t* outIndices;
1653 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1654 GrPrimitiveType primType;
1655 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001656
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001657 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1658 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001659
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001660 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001661
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001662 SkPaint copy(paint);
1663 copy.setStyle(SkPaint::kStroke_Style);
1664 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001665
dandov32a311b2014-07-15 19:46:26 -07001666 // we ignore the shader if texs is null.
joshualitt25d9c152015-02-18 12:29:52 -08001667 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy,
1668 SkColor2GrColor(copy.getColor()), NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001669
dandov32a311b2014-07-15 19:46:26 -07001670 primType = kLines_GrPrimitiveType;
1671 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001672 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001673 switch (vmode) {
1674 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001675 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001676 break;
1677 case SkCanvas::kTriangleStrip_VertexMode:
1678 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001679 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001680 break;
1681 }
mtklein533eb782014-08-27 10:39:42 -07001682
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001683 VertState state(vertexCount, indices, indexCount);
1684 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001685
dandov32a311b2014-07-15 19:46:26 -07001686 //number of indices for lines per triangle with kLines
1687 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001688
dandov32a311b2014-07-15 19:46:26 -07001689 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1690 outIndices = outAlloc.get();
1691 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001692 int i = 0;
1693 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001694 auxIndices[i] = state.f0;
1695 auxIndices[i + 1] = state.f1;
1696 auxIndices[i + 2] = state.f1;
1697 auxIndices[i + 3] = state.f2;
1698 auxIndices[i + 4] = state.f2;
1699 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001700 i += 6;
1701 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001702 } else {
dandov32a311b2014-07-15 19:46:26 -07001703 outIndices = indices;
1704 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001705
dandov32a311b2014-07-15 19:46:26 -07001706 if (NULL == texs || NULL == paint.getShader()) {
joshualitt25d9c152015-02-18 12:29:52 -08001707 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1708 SkColor2GrColor(paint.getColor()),
dandov32a311b2014-07-15 19:46:26 -07001709 NULL == colors, &grPaint);
1710 } else {
joshualitt25d9c152015-02-18 12:29:52 -08001711 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix,
1712 NULL == colors, &grPaint);
dandov32a311b2014-07-15 19:46:26 -07001713 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001714 }
1715
mtklein2583b622014-06-04 08:20:41 -07001716#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001717 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001718 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1719 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001720 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001721 }
1722 }
mtklein2583b622014-06-04 08:20:41 -07001723#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001724
1725 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001726 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001727 // need to convert byte order and from non-PM to PM
1728 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001729 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001730 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001731 color = colors[i];
1732 if (paint.getAlpha() != 255) {
1733 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1734 }
1735 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001736 }
1737 colors = convertedColors.get();
1738 }
joshualitt25d9c152015-02-18 12:29:52 -08001739 fContext->drawVertices(fRenderTarget,
1740 grPaint,
joshualitt5531d512014-12-17 15:50:11 -08001741 *draw.fMatrix,
dandov32a311b2014-07-15 19:46:26 -07001742 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001743 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001744 vertices,
1745 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001746 colors,
dandov32a311b2014-07-15 19:46:26 -07001747 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001748 indexCount);
1749}
1750
1751///////////////////////////////////////////////////////////////////////////////
1752
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001753void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001754 size_t byteLength, SkScalar x, SkScalar y,
1755 const SkPaint& paint) {
1756 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001757 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001758
jvanverth8c27a182014-10-14 08:45:50 -07001759 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -08001760 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001761
jvanverth8c27a182014-10-14 08:45:50 -07001762 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001763
joshualitt25d9c152015-02-18 12:29:52 -08001764 if (!fTextContext->drawText(fRenderTarget, grPaint, paint, *draw.fMatrix, (const char *)text,
1765 byteLength, x, y)) {
jvanverth8c27a182014-10-14 08:45:50 -07001766 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001767 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001768 }
1769}
1770
fmalita05c4a432014-09-29 06:29:53 -07001771void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1772 const SkScalar pos[], int scalarsPerPos,
1773 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001774 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001775 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001776
jvanverth8c27a182014-10-14 08:45:50 -07001777 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -08001778 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001779
jvanverth8c27a182014-10-14 08:45:50 -07001780 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001781
joshualitt25d9c152015-02-18 12:29:52 -08001782 if (!fTextContext->drawPosText(fRenderTarget, grPaint, paint, *draw.fMatrix, (const char *)text,
1783 byteLength, pos, scalarsPerPos, offset)) {
jvanverth8c27a182014-10-14 08:45:50 -07001784 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001785 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001786 }
1787}
1788
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001789///////////////////////////////////////////////////////////////////////////////
1790
reedb2db8982014-11-13 12:41:02 -08001791bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001792 if (paint.getShader() ||
reedb2db8982014-11-13 12:41:02 -08001793 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001794 paint.getMaskFilter() ||
1795 paint.getRasterizer() ||
1796 paint.getColorFilter() ||
1797 paint.getPathEffect() ||
1798 paint.isFakeBoldText() ||
reedb2db8982014-11-13 12:41:02 -08001799 paint.getStyle() != SkPaint::kFill_Style)
1800 {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001801 return true;
1802 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001803 return false;
1804}
1805
1806void SkGpuDevice::flush() {
1807 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001808 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001809}
1810
1811///////////////////////////////////////////////////////////////////////////////
1812
fmalita6987dca2014-11-13 08:33:37 -08001813SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(const CreateInfo& cinfo) {
bsalomonf2703d82014-10-28 14:33:06 -07001814 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001815 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001816 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001817 desc.fWidth = cinfo.fInfo.width();
1818 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001819 desc.fSampleCnt = fRenderTarget->numSamples();
1820
1821 SkAutoTUnref<GrTexture> texture;
1822 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001823 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001824
hcm4396fa52014-10-27 21:43:30 -07001825 // layers are never draw in repeat modes, so we can request an approx
1826 // match and ignore any padding.
fmalita6987dca2014-11-13 08:33:37 -08001827 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == cinfo.fUsage) ?
hcm4396fa52014-10-27 21:43:30 -07001828 GrContext::kApprox_ScratchTexMatch :
1829 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001830 texture.reset(fContext->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001831
1832 if (texture) {
1833 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry);
1834 return SkGpuDevice::Create(texture->asRenderTarget(), &props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001835 } else {
tfarina38406c82014-10-31 07:11:12 -07001836 SkDebugf("---- failed to create compatible device texture [%d %d]\n",
fmalita6987dca2014-11-13 08:33:37 -08001837 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001838 return NULL;
1839 }
1840}
1841
reed4a8126e2014-09-22 07:29:03 -07001842SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001843 // TODO: Change the signature of newSurface to take a budgeted parameter.
1844 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
1845 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->numSamples(),
1846 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001847}
1848
robertphillips30d2cc62014-09-24 08:52:18 -07001849bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001850 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001851#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001852 // todo: should handle this natively
1853 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001854 return false;
1855 }
1856
robertphillips82365912014-11-12 09:32:34 -08001857 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips81f71b62014-11-11 04:54:49 -08001858
1859 const SkPicture::AccelData* data = mainPicture->EXPERIMENTAL_getAccelData(key);
1860 if (!data) {
1861 return false;
1862 }
1863
robertphillipse5524cd2015-02-20 12:30:26 -08001864 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1865 if (0 == gpuData->numBlocks()) {
1866 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001867 }
1868
robertphillipsfd61ed02014-10-28 07:21:44 -07001869 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001870
robertphillipse5524cd2015-02-20 12:30:26 -08001871 SkIRect iBounds;
1872 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1873 return false;
1874 }
1875
1876 SkRect clipBounds = SkRect::Make(iBounds);
1877
1878 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1879
robertphillipsfd61ed02014-10-28 07:21:44 -07001880 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001881 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001882 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001883 &atlasedNeedRendering, &atlasedRecycled,
1884 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001885
1886 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1887
1888 SkTDArray<GrHoistedLayer> needRendering, recycled;
1889
robertphillipse5524cd2015-02-20 12:30:26 -08001890 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1891
robertphillipsfd61ed02014-10-28 07:21:44 -07001892 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001893 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001894 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001895 &needRendering, &recycled,
1896 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001897
1898 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001899
robertphillips64bf7672014-08-21 13:07:35 -07001900 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001901 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1902 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001903
robertphillipsfd61ed02014-10-28 07:21:44 -07001904 GrLayerHoister::UnlockLayers(fContext, needRendering);
1905 GrLayerHoister::UnlockLayers(fContext, recycled);
1906 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1907 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001908
1909 return true;
robertphillips63242d72014-12-04 08:31:02 -08001910#else
1911 return false;
1912#endif
robertphillips64bf7672014-08-21 13:07:35 -07001913}
1914
senorblancobe129b22014-08-08 07:14:35 -07001915SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001916 // We always return a transient cache, so it is freed after each
1917 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001918 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001919}
reedf037e0b2014-10-30 11:34:15 -07001920
1921#endif