blob: 8912851d8f369c88a9b36aeb7d281c8b2490c690 [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
9
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000010#include "effects/GrBicubicEffect.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000011#include "effects/GrDashingEffect.h"
egdaniel95131432014-12-09 11:15:43 -080012#include "effects/GrPorterDuffXferProcessor.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000013#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000014#include "effects/GrSimpleTextureEffect.h"
15
16#include "GrContext.h"
17#include "GrBitmapTextContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000018#include "GrDistanceFieldTextContext.h"
robertphillips98d709b2014-09-02 10:20:50 -070019#include "GrLayerHoister.h"
robertphillips274b4ba2014-09-04 07:24:18 -070020#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070021#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070022#include "GrTracing.h"
derekff4555aa2014-10-06 12:19:12 -070023#include "GrGpu.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000024
25#include "SkGrTexturePixelRef.h"
26
robertphillips30d78412014-11-24 09:49:17 -080027#include "SkCanvasPriv.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkDeviceImageFilterProxy.h"
29#include "SkDrawProcs.h"
30#include "SkGlyphCache.h"
31#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080032#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000033#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000034#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000035#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070036#include "SkPictureData.h"
robertphillips274b4ba2014-09-04 07:24:18 -070037#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000038#include "SkRRect.h"
39#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000040#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000041#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000042#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000043#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070044#include "SkXfermode.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000045#include "SkErrorInternals.h"
46
reedf037e0b2014-10-30 11:34:15 -070047#if SK_SUPPORT_GPU
48
senorblanco55b6d8b2014-07-30 11:26:46 -070049enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
50
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000051#if 0
52 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080053 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000054 do { \
55 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080056 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000057 } while (0)
58#else
joshualitt5531d512014-12-17 15:50:11 -080059 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000060#endif
61
62// This constant represents the screen alignment criterion in texels for
63// requiring texture domain clamping to prevent color bleeding when drawing
64// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000065#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000066
67#define DO_DEFERRED_CLEAR() \
68 do { \
bsalomonafe30052015-01-16 07:32:33 -080069 if (fNeedClear) { \
70 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000071 } \
72 } while (false) \
73
74///////////////////////////////////////////////////////////////////////////////
75
76#define CHECK_FOR_ANNOTATION(paint) \
77 do { if (paint.getAnnotation()) { return; } } while (0)
78
79///////////////////////////////////////////////////////////////////////////////
80
bsalomonbcf0a522014-10-08 08:40:09 -070081// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
82// just accesses the backing GrTexture. Otherwise, it creates a cached texture
83// representation and releases it in the destructor.
84class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040085public:
bsalomonbcf0a522014-10-08 08:40:09 -070086 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070087
bsalomonbcf0a522014-10-08 08:40:09 -070088 AutoBitmapTexture(GrContext* context,
89 const SkBitmap& bitmap,
90 const GrTextureParams* params,
91 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040092 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070093 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040094 }
95
bsalomonbcf0a522014-10-08 08:40:09 -070096 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040097 const SkBitmap& bitmap,
98 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -070099 // Either get the texture directly from the bitmap, or else use the cache and
100 // remember to unref it.
101 if (GrTexture* bmpTexture = bitmap.getTexture()) {
102 fTexture.reset(NULL);
103 return bmpTexture;
104 } else {
105 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
106 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400107 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400108 }
109
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000110private:
bsalomonbcf0a522014-10-08 08:40:09 -0700111 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000112};
113
114///////////////////////////////////////////////////////////////////////////////
115
116struct GrSkDrawProcs : public SkDrawProcs {
117public:
118 GrContext* fContext;
119 GrTextContext* fTextContext;
120 GrFontScaler* fFontScaler; // cached in the skia glyphcache
121};
122
123///////////////////////////////////////////////////////////////////////////////
124
bsalomonafe30052015-01-16 07:32:33 -0800125SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags) {
126 if (!rt || rt->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000127 return NULL;
128 }
bsalomonafe30052015-01-16 07:32:33 -0800129 return SkNEW_ARGS(SkGpuDevice, (rt, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000130}
131
bsalomonafe30052015-01-16 07:32:33 -0800132static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps* props) {
133 if (props) {
134 return SkDeviceProperties(props->pixelGeometry());
135 } else {
136 return SkDeviceProperties(SkDeviceProperties::kLegacyLCD_InitType);
137 }
reedb2db8982014-11-13 12:41:02 -0800138}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000139
bsalomonafe30052015-01-16 07:32:33 -0800140static SkSurfaceProps copy_or_default_props(const SkSurfaceProps* props) {
141 if (props) {
142 return SkSurfaceProps(*props);
143 } else {
144 return SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
145 }
146}
147
148SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags)
reedb2db8982014-11-13 12:41:02 -0800149 : INHERITED(surfaceprops_to_deviceprops(props))
bsalomonafe30052015-01-16 07:32:33 -0800150 , fSurfaceProps(copy_or_default_props(props))
reedb2db8982014-11-13 12:41:02 -0800151{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000152 fDrawProcs = NULL;
153
bsalomonafe30052015-01-16 07:32:33 -0800154 fContext = SkRef(rt->getContext());
155 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000156
bsalomonafe30052015-01-16 07:32:33 -0800157 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000158
bsalomonafe30052015-01-16 07:32:33 -0800159 SkImageInfo info = rt->surfacePriv().info();
160 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt));
bsalomonafbf2d62014-09-30 12:18:44 -0700161 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700162 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700163
bsalomonafe30052015-01-16 07:32:33 -0800164 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts();
jvanverth4736e142014-11-07 07:12:46 -0800165 fTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFT);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000166}
167
bsalomonafe30052015-01-16 07:32:33 -0800168SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
169 const SkImageInfo& origInfo, int sampleCount,
170 const SkSurfaceProps* props, unsigned flags) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000171 if (kUnknown_SkColorType == origInfo.colorType() ||
172 origInfo.width() < 0 || origInfo.height() < 0) {
173 return NULL;
174 }
175
bsalomonafe30052015-01-16 07:32:33 -0800176 if (!context) {
177 return NULL;
178 }
179
reede5ea5002014-09-03 11:54:58 -0700180 SkColorType ct = origInfo.colorType();
181 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700182 if (kRGB_565_SkColorType == ct) {
183 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800184 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
185 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700186 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800187 }
188 if (kOpaque_SkAlphaType != at) {
189 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000190 }
reede5ea5002014-09-03 11:54:58 -0700191 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000192
bsalomonf2703d82014-10-28 14:33:06 -0700193 GrSurfaceDesc desc;
194 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000195 desc.fWidth = info.width();
196 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000197 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000198 desc.fSampleCnt = sampleCount;
199
bsalomond0423582015-02-06 08:49:24 -0800200 SkAutoTUnref<GrTexture> texture(context->createTexture(desc, SkToBool(budgeted), NULL, 0));
bsalomonafe30052015-01-16 07:32:33 -0800201 if (!texture) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000202 return NULL;
203 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000204
bsalomonafe30052015-01-16 07:32:33 -0800205 return SkNEW_ARGS(SkGpuDevice, (texture->asRenderTarget(), props, flags));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000206}
207
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000208SkGpuDevice::~SkGpuDevice() {
209 if (fDrawProcs) {
210 delete fDrawProcs;
211 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000212
jvanverth8c27a182014-10-14 08:45:50 -0700213 delete fTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000214
215 // The GrContext takes a ref on the target. We don't want to cause the render
216 // target to be unnecessarily kept alive.
217 if (fContext->getRenderTarget() == fRenderTarget) {
218 fContext->setRenderTarget(NULL);
219 }
220
221 if (fContext->getClip() == &fClipData) {
222 fContext->setClip(NULL);
223 }
224
bsalomon32d0b3b2014-08-29 07:50:23 -0700225 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000226 fContext->unref();
227}
228
229///////////////////////////////////////////////////////////////////////////////
230
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000231bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
232 int x, int y) {
233 DO_DEFERRED_CLEAR();
234
235 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000236 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000237 if (kUnknown_GrPixelConfig == config) {
238 return false;
239 }
240
241 uint32_t flags = 0;
242 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
243 flags = GrContext::kUnpremul_PixelOpsFlag;
244 }
245 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
246 config, dstPixels, dstRowBytes, flags);
247}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000248
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000249bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
250 int x, int y) {
251 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000252 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000253 if (kUnknown_GrPixelConfig == config) {
254 return false;
255 }
256 uint32_t flags = 0;
257 if (kUnpremul_SkAlphaType == info.alphaType()) {
258 flags = GrContext::kUnpremul_PixelOpsFlag;
259 }
260 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
261
262 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700263 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000264
265 return true;
266}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000267
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000268const SkBitmap& SkGpuDevice::onAccessBitmap() {
269 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700270 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000271}
272
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000273void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
274 INHERITED::onAttachToCanvas(canvas);
275
276 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualittde358a92015-02-05 08:19:35 -0800277 fClipData.fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000278}
279
280void SkGpuDevice::onDetachFromCanvas() {
281 INHERITED::onDetachFromCanvas();
joshualittde358a92015-02-05 08:19:35 -0800282 fClipData.fClipStack.reset(NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000283}
284
285// call this every draw call, to ensure that the context reflects our state,
286// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800287void SkGpuDevice::prepareDraw(const SkDraw& draw) {
bsalomon49f085d2014-09-05 13:34:00 -0700288 SkASSERT(fClipData.fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000289
290 fContext->setRenderTarget(fRenderTarget);
291
292 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
293
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000294 fClipData.fOrigin = this->getOrigin();
295
296 fContext->setClip(&fClipData);
297
298 DO_DEFERRED_CLEAR();
299}
300
301GrRenderTarget* SkGpuDevice::accessRenderTarget() {
302 DO_DEFERRED_CLEAR();
303 return fRenderTarget;
304}
305
reed8eddfb52014-12-04 07:50:14 -0800306void SkGpuDevice::clearAll() {
307 GrColor color = 0;
308 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
309 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
310 fContext->clear(&rect, color, true, fRenderTarget);
bsalomonafe30052015-01-16 07:32:33 -0800311 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800312}
313
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000314///////////////////////////////////////////////////////////////////////////////
315
316SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
317SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
318SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
319SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
320SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
321 shader_type_mismatch);
322SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
323 shader_type_mismatch);
324SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
325SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
326
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000327///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000328
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000329void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800330 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700331 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000332
333 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800334 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000335
joshualitt5531d512014-12-17 15:50:11 -0800336 fContext->drawPaint(grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000337}
338
339// must be in SkCanvas::PointMode order
340static const GrPrimitiveType gPointMode2PrimtiveType[] = {
341 kPoints_GrPrimitiveType,
342 kLines_GrPrimitiveType,
343 kLineStrip_GrPrimitiveType
344};
345
346void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
347 size_t count, const SkPoint pts[], const SkPaint& paint) {
348 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800349 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000350
351 SkScalar width = paint.getStrokeWidth();
352 if (width < 0) {
353 return;
354 }
355
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000356 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700357 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
358 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800359 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
egdaniele61c4112014-06-12 10:24:21 -0700360 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700361 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700362 path.moveTo(pts[0]);
363 path.lineTo(pts[1]);
joshualitt5531d512014-12-17 15:50:11 -0800364 fContext->drawPath(grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700365 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000366 }
367
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000368 // we only handle hairlines and paints without path effects or mask filters,
369 // else we let the SkDraw call our drawPath()
370 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
371 draw.drawPoints(mode, count, pts, paint, true);
372 return;
373 }
374
375 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800376 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000377
378 fContext->drawVertices(grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800379 *draw.fMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000380 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000381 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000382 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000383 NULL,
384 NULL,
385 NULL,
386 0);
387}
388
389///////////////////////////////////////////////////////////////////////////////
390
391void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
392 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700393 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
394
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000395 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800396 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000397
398 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
399 SkScalar width = paint.getStrokeWidth();
400
401 /*
402 We have special code for hairline strokes, miter-strokes, bevel-stroke
403 and fills. Anything else we just call our path code.
404 */
405 bool usePath = doStroke && width > 0 &&
406 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
407 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
408 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700409
410 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000411 usePath = true;
412 }
egdanield58a0ba2014-06-11 10:30:05 -0700413
joshualitt5531d512014-12-17 15:50:11 -0800414 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000415#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
416 if (doStroke) {
417#endif
418 usePath = true;
419#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
420 } else {
joshualitt5531d512014-12-17 15:50:11 -0800421 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000422 }
423#endif
424 }
425 // until we can both stroke and fill rectangles
426 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
427 usePath = true;
428 }
429
egdanield58a0ba2014-06-11 10:30:05 -0700430 GrStrokeInfo strokeInfo(paint);
431
432 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700433 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700434 usePath = true;
435 }
436
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000437 if (usePath) {
438 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700439 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000440 path.addRect(rect);
441 this->drawPath(draw, path, paint, NULL, true);
442 return;
443 }
444
445 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800446 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400447
joshualitt5531d512014-12-17 15:50:11 -0800448 fContext->drawRect(grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000449}
450
451///////////////////////////////////////////////////////////////////////////////
452
453void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800454 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700455 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000456 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800457 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000458
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000459 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800460 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400461
egdanield58a0ba2014-06-11 10:30:05 -0700462 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000463 if (paint.getMaskFilter()) {
464 // try to hit the fast path for drawing filtered round rects
465
466 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800467 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000468 if (devRRect.allCornersCircular()) {
469 SkRect maskRect;
470 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800471 draw.fClip->getBounds(),
472 *draw.fMatrix,
473 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000474 SkIRect finalIRect;
475 maskRect.roundOut(&finalIRect);
476 if (draw.fClip->quickReject(finalIRect)) {
477 // clipped out
478 return;
479 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000480 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800481 *draw.fMatrix,
egdanield58a0ba2014-06-11 10:30:05 -0700482 strokeInfo.getStrokeRec(),
483 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000484 return;
485 }
486 }
487
488 }
489 }
490
491 }
492
egdanield58a0ba2014-06-11 10:30:05 -0700493 bool usePath = false;
494
495 if (paint.getMaskFilter()) {
496 usePath = true;
497 } else {
498 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700499 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700500 usePath = true;
501 }
502 }
503
504
505 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000506 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700507 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000508 path.addRRect(rect);
509 this->drawPath(draw, path, paint, NULL, true);
510 return;
511 }
Mike Klein744fb732014-06-23 15:13:26 -0400512
joshualitt5531d512014-12-17 15:50:11 -0800513 fContext->drawRRect(grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000514}
515
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000516void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800517 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000518 SkStrokeRec stroke(paint);
519 if (stroke.isFillStyle()) {
520
521 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800522 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000523
524 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800525 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000526
527 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
joshualitt5531d512014-12-17 15:50:11 -0800528 fContext->drawDRRect(grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000529 return;
530 }
531 }
532
533 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700534 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000535 path.addRRect(outer);
536 path.addRRect(inner);
537 path.setFillType(SkPath::kEvenOdd_FillType);
538
539 this->drawPath(draw, path, paint, NULL, true);
540}
541
542
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000543/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000544
545void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
546 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700547 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000548 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800549 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550
egdanield58a0ba2014-06-11 10:30:05 -0700551 GrStrokeInfo strokeInfo(paint);
552
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000553 bool usePath = false;
554 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700555 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000556 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700557 } else {
558 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700559 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700560 usePath = true;
561 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000562 }
563
564 if (usePath) {
565 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700566 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000567 path.addOval(oval);
568 this->drawPath(draw, path, paint, NULL, true);
569 return;
570 }
571
572 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -0800573 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000574
joshualitt5531d512014-12-17 15:50:11 -0800575 fContext->drawOval(grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000576}
577
578#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000579
580///////////////////////////////////////////////////////////////////////////////
581
582// helpers for applying mask filters
583namespace {
584
585// Draw a mask using the supplied paint. Since the coverage/geometry
586// is already burnt into the mask this boils down to a rect draw.
587// Return true if the mask was successfully drawn.
joshualitt5531d512014-12-17 15:50:11 -0800588bool draw_mask(GrContext* context, const SkMatrix& viewMatrix, const SkRect& maskRect,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000589 GrPaint* grp, GrTexture* mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000590 SkMatrix matrix;
591 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
592 matrix.postIDiv(mask->width(), mask->height());
593
joshualitt16b27892014-12-18 07:47:16 -0800594 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix,
595 kDevice_GrCoordSet))->unref();
596
597 SkMatrix inverse;
598 if (!viewMatrix.invert(&inverse)) {
599 return false;
600 }
601 context->drawNonAARectWithLocalMatrix(*grp, SkMatrix::I(), maskRect, inverse);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000602 return true;
603}
604
joshualitt5531d512014-12-17 15:50:11 -0800605bool draw_with_mask_filter(GrContext* context, const SkMatrix& viewMatrix, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700606 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000607 GrPaint* grp, SkPaint::Style style) {
608 SkMask srcM, dstM;
609
joshualitt5531d512014-12-17 15:50:11 -0800610 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &viewMatrix, &srcM,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000611 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
612 return false;
613 }
614 SkAutoMaskFreeImage autoSrc(srcM.fImage);
615
joshualitt5531d512014-12-17 15:50:11 -0800616 if (!filter->filterMask(&dstM, srcM, viewMatrix, NULL)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000617 return false;
618 }
619 // this will free-up dstM when we're done (allocated in filterMask())
620 SkAutoMaskFreeImage autoDst(dstM.fImage);
621
622 if (clip.quickReject(dstM.fBounds)) {
623 return false;
624 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000625
626 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
627 // the current clip (and identity matrix) and GrPaint settings
bsalomonf2703d82014-10-28 14:33:06 -0700628 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000629 desc.fWidth = dstM.fBounds.width();
630 desc.fHeight = dstM.fBounds.height();
631 desc.fConfig = kAlpha_8_GrPixelConfig;
632
bsalomone3059732014-10-14 11:47:22 -0700633 SkAutoTUnref<GrTexture> texture(
634 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
635 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000636 return false;
637 }
638 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
639 dstM.fImage, dstM.fRowBytes);
640
641 SkRect maskRect = SkRect::Make(dstM.fBounds);
642
joshualitt5531d512014-12-17 15:50:11 -0800643 return draw_mask(context, viewMatrix, maskRect, grp, texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000644}
645
bsalomone3059732014-10-14 11:47:22 -0700646// Create a mask of 'devPath' and place the result in 'mask'.
647GrTexture* create_mask_GPU(GrContext* context,
648 const SkRect& maskRect,
649 const SkPath& devPath,
650 const GrStrokeInfo& strokeInfo,
651 bool doAA,
652 int sampleCnt) {
bsalomonf2703d82014-10-28 14:33:06 -0700653 GrSurfaceDesc desc;
654 desc.fFlags = kRenderTarget_GrSurfaceFlag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000655 desc.fWidth = SkScalarCeilToInt(maskRect.width());
656 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700657 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000658 // We actually only need A8, but it often isn't supported as a
659 // render target so default to RGBA_8888
660 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700661
662 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
663 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000664 desc.fConfig = kAlpha_8_GrPixelConfig;
665 }
666
bsalomone3059732014-10-14 11:47:22 -0700667 GrTexture* mask = context->refScratchTexture(desc,GrContext::kApprox_ScratchTexMatch);
668 if (NULL == mask) {
669 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000670 }
671
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000672 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
673
bsalomone3059732014-10-14 11:47:22 -0700674 GrContext::AutoRenderTarget art(context, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000675 GrContext::AutoClip ac(context, clipRect);
676
bsalomon89c62982014-11-03 12:08:42 -0800677 context->clear(NULL, 0x0, true, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000678
679 GrPaint tempPaint;
680 if (doAA) {
681 tempPaint.setAntiAlias(true);
682 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
683 // blend coeff of zero requires dual source blending support in order
684 // to properly blend partially covered pixels. This means the AA
685 // code path may not be taken. So we use a dst blend coeff of ISA. We
686 // could special case AA draws to a dst surface with known alpha=0 to
687 // use a zero dst coeff when dual source blending isn't available.
egdaniel95131432014-12-09 11:15:43 -0800688 tempPaint.setPorterDuffXPFactory(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000689 }
690
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000691 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
692 SkMatrix translate;
693 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -0800694 context->drawPath(tempPaint, translate, devPath, strokeInfo);
bsalomone3059732014-10-14 11:47:22 -0700695 return mask;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000696}
697
698SkBitmap wrap_texture(GrTexture* texture) {
699 SkBitmap result;
bsalomonafbf2d62014-09-30 12:18:44 -0700700 result.setInfo(texture->surfacePriv().info());
reed6c225732014-06-09 19:52:07 -0700701 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000702 return result;
703}
704
705};
706
707void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
708 const SkPaint& paint, const SkMatrix* prePathMatrix,
709 bool pathIsMutable) {
710 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800711 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700712 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000713
jvanverthb3eb6872014-10-24 07:12:51 -0700714 SkASSERT(!pathIsMutable || origSrcPath.isVolatile());
joshualitt5531d512014-12-17 15:50:11 -0800715
bsalomon54443932015-01-29 09:34:18 -0800716 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000717
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000718 // If we have a prematrix, apply it to the path, optimizing for the case
719 // where the original path can in fact be modified in place (even though
720 // its parameter type is const).
721 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000722 SkTLazy<SkPath> tmpPath;
723 SkTLazy<SkPath> effectPath;
bsalomon54443932015-01-29 09:34:18 -0800724 SkPathEffect* pathEffect = paint.getPathEffect();
725
726 SkMatrix viewMatrix = *draw.fMatrix;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000727
728 if (prePathMatrix) {
bsalomon54443932015-01-29 09:34:18 -0800729 // stroking and path effects are supposed to be applied *after* the prePathMatrix.
730 // The pre-path-matrix also should not affect shadeing.
731 if (NULL == pathEffect && NULL == paint.getShader() &&
732 (strokeInfo.getStrokeRec().isFillStyle() ||
733 strokeInfo.getStrokeRec().isHairlineStyle())) {
734 viewMatrix.preConcat(*prePathMatrix);
735 } else {
736 SkPath* result = pathPtr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000737
bsalomon54443932015-01-29 09:34:18 -0800738 if (!pathIsMutable) {
739 result = tmpPath.init();
740 result->setIsVolatile(true);
741 pathIsMutable = true;
742 }
743 // should I push prePathMatrix on our MV stack temporarily, instead
744 // of applying it here? See SkDraw.cpp
745 pathPtr->transform(*prePathMatrix, result);
746 pathPtr = result;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000747 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000748 }
749 // at this point we're done with prePathMatrix
750 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
751
bsalomon54443932015-01-29 09:34:18 -0800752 GrPaint grPaint;
753 SkPaint2GrPaintShader(this->context(), paint, viewMatrix, true, &grPaint);
754
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000755 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700756 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
757 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000758 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000759 pathPtr = effectPath.get();
760 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700761 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000762 }
763
egdanield58a0ba2014-06-11 10:30:05 -0700764 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000765 if (paint.getMaskFilter()) {
766 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000767 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
768 if (stroke.applyToPath(strokedPath, *pathPtr)) {
769 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000770 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700771 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000772 }
773 }
774
775 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000776 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700777 if (!pathIsMutable) {
778 devPathPtr->setIsVolatile(true);
779 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000780
781 // transform the path into device space
bsalomon54443932015-01-29 09:34:18 -0800782 pathPtr->transform(viewMatrix, devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000783
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000784 SkRect maskRect;
785 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
786 draw.fClip->getBounds(),
bsalomon54443932015-01-29 09:34:18 -0800787 viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000788 &maskRect)) {
789 SkIRect finalIRect;
790 maskRect.roundOut(&finalIRect);
791 if (draw.fClip->quickReject(finalIRect)) {
792 // clipped out
793 return;
794 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000795
bsalomon54443932015-01-29 09:34:18 -0800796 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint, viewMatrix,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000797 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000798 // the mask filter was able to draw itself directly, so there's nothing
799 // left to do.
800 return;
801 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000802
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000803
bsalomone3059732014-10-14 11:47:22 -0700804 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext, maskRect, *devPathPtr,
805 strokeInfo, grPaint.isAntiAlias(),
806 fRenderTarget->numSamples()));
807 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000808 GrTexture* filtered;
809
bsalomon54443932015-01-29 09:34:18 -0800810 if (paint.getMaskFilter()->filterMaskGPU(mask, viewMatrix, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000811 // filterMaskGPU gives us ownership of a ref to the result
812 SkAutoTUnref<GrTexture> atu(filtered);
bsalomon54443932015-01-29 09:34:18 -0800813 if (draw_mask(fContext, viewMatrix, maskRect, &grPaint, filtered)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000814 // This path is completely drawn
815 return;
816 }
817 }
818 }
819 }
820
821 // draw the mask on the CPU - this is a fallthrough path in case the
822 // GPU path fails
823 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
824 SkPaint::kFill_Style;
bsalomon54443932015-01-29 09:34:18 -0800825 draw_with_mask_filter(fContext, viewMatrix, *devPathPtr, paint.getMaskFilter(),
egdanield58a0ba2014-06-11 10:30:05 -0700826 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000827 return;
828 }
829
bsalomon54443932015-01-29 09:34:18 -0800830 fContext->drawPath(grPaint, viewMatrix, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000831}
832
833static const int kBmpSmallTileSize = 1 << 10;
834
835static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
836 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
837 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
838 return tilesX * tilesY;
839}
840
841static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
842 if (maxTileSize <= kBmpSmallTileSize) {
843 return maxTileSize;
844 }
845
846 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
847 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
848
849 maxTileTotalTileSize *= maxTileSize * maxTileSize;
850 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
851
852 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
853 return kBmpSmallTileSize;
854 } else {
855 return maxTileSize;
856 }
857}
858
859// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
860// pixels from the bitmap are necessary.
861static void determine_clipped_src_rect(const GrContext* context,
joshualitt5531d512014-12-17 15:50:11 -0800862 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000863 const SkBitmap& bitmap,
864 const SkRect* srcRectPtr,
865 SkIRect* clippedSrcIRect) {
866 const GrClipData* clip = context->getClip();
867 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
868 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800869 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000870 clippedSrcIRect->setEmpty();
871 return;
872 }
873 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
874 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700875 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000876 // we've setup src space 0,0 to map to the top left of the src rect.
877 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000878 if (!clippedSrcRect.intersect(*srcRectPtr)) {
879 clippedSrcIRect->setEmpty();
880 return;
881 }
882 }
883 clippedSrcRect.roundOut(clippedSrcIRect);
884 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
885 if (!clippedSrcIRect->intersect(bmpBounds)) {
886 clippedSrcIRect->setEmpty();
887 }
888}
889
890bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800891 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000892 const GrTextureParams& params,
893 const SkRect* srcRectPtr,
894 int maxTileSize,
895 int* tileSize,
896 SkIRect* clippedSrcRect) const {
897 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700898 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000899 return false;
900 }
901
902 // if it's larger than the max tile size, then we have no choice but tiling.
903 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt5531d512014-12-17 15:50:11 -0800904 determine_clipped_src_rect(fContext, viewMatrix, bitmap, srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000905 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
906 return true;
907 }
908
909 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
910 return false;
911 }
912
913 // if the entire texture is already in our cache then no reason to tile it
914 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
915 return false;
916 }
917
918 // At this point we know we could do the draw by uploading the entire bitmap
919 // as a texture. However, if the texture would be large compared to the
920 // cache size and we don't require most of it for this draw then tile to
921 // reduce the amount of upload and cache spill.
922
923 // assumption here is that sw bitmap size is a good proxy for its size as
924 // a texture
925 size_t bmpSize = bitmap.getSize();
926 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000927 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000928 if (bmpSize < cacheSize / 2) {
929 return false;
930 }
931
932 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt5531d512014-12-17 15:50:11 -0800933 determine_clipped_src_rect(fContext, viewMatrix, bitmap, srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000934 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
935 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
936 kBmpSmallTileSize * kBmpSmallTileSize;
937
938 return usedTileBytes < 2 * bmpSize;
939}
940
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000941void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000942 const SkBitmap& bitmap,
943 const SkMatrix& m,
944 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000945 SkMatrix concat;
946 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
947 if (!m.isIdentity()) {
948 concat.setConcat(*draw->fMatrix, m);
949 draw.writable()->fMatrix = &concat;
950 }
951 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000952}
953
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000954// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000955// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
956// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000957static inline void clamped_outset_with_offset(SkIRect* iRect,
958 int outset,
959 SkPoint* offset,
960 const SkIRect& clamp) {
961 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000962
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000963 int leftClampDelta = clamp.fLeft - iRect->fLeft;
964 if (leftClampDelta > 0) {
965 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000966 iRect->fLeft = clamp.fLeft;
967 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000968 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000969 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000970
971 int topClampDelta = clamp.fTop - iRect->fTop;
972 if (topClampDelta > 0) {
973 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000974 iRect->fTop = clamp.fTop;
975 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000976 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000977 }
978
979 if (iRect->fRight > clamp.fRight) {
980 iRect->fRight = clamp.fRight;
981 }
982 if (iRect->fBottom > clamp.fBottom) {
983 iRect->fBottom = clamp.fBottom;
984 }
985}
986
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000987static bool has_aligned_samples(const SkRect& srcRect,
988 const SkRect& transformedRect) {
989 // detect pixel disalignment
990 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
991 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
992 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
993 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
994 SkScalarAbs(transformedRect.width() - srcRect.width()) <
995 COLOR_BLEED_TOLERANCE &&
996 SkScalarAbs(transformedRect.height() - srcRect.height()) <
997 COLOR_BLEED_TOLERANCE) {
998 return true;
999 }
1000 return false;
1001}
1002
1003static bool may_color_bleed(const SkRect& srcRect,
1004 const SkRect& transformedRect,
1005 const SkMatrix& m) {
1006 // Only gets called if has_aligned_samples returned false.
1007 // So we can assume that sampling is axis aligned but not texel aligned.
1008 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1009 SkRect innerSrcRect(srcRect), innerTransformedRect,
1010 outerTransformedRect(transformedRect);
1011 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1012 m.mapRect(&innerTransformedRect, innerSrcRect);
1013
1014 // The gap between outerTransformedRect and innerTransformedRect
1015 // represents the projection of the source border area, which is
1016 // problematic for color bleeding. We must check whether any
1017 // destination pixels sample the border area.
1018 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1019 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1020 SkIRect outer, inner;
1021 outerTransformedRect.round(&outer);
1022 innerTransformedRect.round(&inner);
1023 // If the inner and outer rects round to the same result, it means the
1024 // border does not overlap any pixel centers. Yay!
1025 return inner != outer;
1026}
1027
1028static bool needs_texture_domain(const SkBitmap& bitmap,
1029 const SkRect& srcRect,
1030 GrTextureParams &params,
1031 const SkMatrix& contextMatrix,
1032 bool bicubic) {
1033 bool needsTextureDomain = false;
1034
1035 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1036 // Need texture domain if drawing a sub rect
1037 needsTextureDomain = srcRect.width() < bitmap.width() ||
1038 srcRect.height() < bitmap.height();
1039 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1040 // sampling is axis-aligned
1041 SkRect transformedRect;
1042 contextMatrix.mapRect(&transformedRect, srcRect);
1043
1044 if (has_aligned_samples(srcRect, transformedRect)) {
1045 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1046 needsTextureDomain = false;
1047 } else {
1048 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1049 }
1050 }
1051 }
1052 return needsTextureDomain;
1053}
1054
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001055void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1056 const SkBitmap& bitmap,
1057 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001058 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001059 const SkPaint& paint,
1060 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -08001061 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001062
1063 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001064 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001065 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1066 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001067 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001068 SkScalar w = SkIntToScalar(bitmap.width());
1069 SkScalar h = SkIntToScalar(bitmap.height());
1070 dstSize.fWidth = w;
1071 dstSize.fHeight = h;
1072 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001073 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001074 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001075 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001076 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001077 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001078 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1079 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1080 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1081 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001082 }
1083
derekf367e1862014-12-02 11:02:06 -08001084 // If the render target is not msaa and draw is antialiased, we call
1085 // drawRect instead of drawing on the render target directly.
1086 // FIXME: the tiled bitmap code path doesn't currently support
1087 // anti-aliased edges, we work around that for now by drawing directly
1088 // if the image size exceeds maximum texture size.
1089 int maxTextureSize = fContext->getMaxTextureSize();
1090 bool directDraw = fRenderTarget->isMultisampled() ||
1091 !paint.isAntiAlias() ||
1092 bitmap.width() > maxTextureSize ||
1093 bitmap.height() > maxTextureSize;
1094
1095 // we check whether dst rect are pixel aligned
1096 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -08001097 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -08001098
1099 if (staysRect) {
1100 SkRect rect;
1101 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -08001102 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -08001103 const SkScalar *scalars = rect.asScalars();
1104 bool isDstPixelAligned = true;
1105 for (int i = 0; i < 4; i++) {
1106 if (!SkScalarIsInt(scalars[i])) {
1107 isDstPixelAligned = false;
1108 break;
1109 }
1110 }
1111
1112 if (isDstPixelAligned)
1113 directDraw = true;
1114 }
1115 }
1116
1117 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001118 // Convert the bitmap to a shader so that the rect can be drawn
1119 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001120 SkBitmap tmp; // subset of bitmap, if necessary
1121 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001122 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001123 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001124 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1125 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1126 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001127 // In bleed mode we position and trim the bitmap based on the src rect which is
1128 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1129 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1130 // compensate.
1131 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1132 SkIRect iSrc;
1133 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001134
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001135 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1136 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001137
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001138 if (!bitmap.extractSubset(&tmp, iSrc)) {
1139 return; // extraction failed
1140 }
1141 bitmapPtr = &tmp;
1142 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001143
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001144 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001145 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001146 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001147 } else {
1148 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001149 }
1150
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001151 SkPaint paintWithShader(paint);
1152 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001153 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001154 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1155 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001156
1157 return;
1158 }
1159
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001160 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1161 // the view matrix rather than a local matrix.
1162 SkMatrix m;
1163 m.setScale(dstSize.fWidth / srcRect.width(),
1164 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -08001165 SkMatrix viewM = *draw.fMatrix;
1166 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167
1168 GrTextureParams params;
1169 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1170 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001171
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001172 bool doBicubic = false;
1173
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001174 switch(paintFilterLevel) {
1175 case SkPaint::kNone_FilterLevel:
1176 textureFilterMode = GrTextureParams::kNone_FilterMode;
1177 break;
1178 case SkPaint::kLow_FilterLevel:
1179 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1180 break;
1181 case SkPaint::kMedium_FilterLevel:
joshualitt5531d512014-12-17 15:50:11 -08001182 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001183 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1184 } else {
1185 // Don't trigger MIP level generation unnecessarily.
1186 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1187 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001188 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001189 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001190 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001191 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001192 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001193 break;
1194 default:
1195 SkErrorInternals::SetError( kInvalidPaint_SkError,
1196 "Sorry, I don't understand the filtering "
1197 "mode you asked for. Falling back to "
1198 "MIPMaps.");
1199 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1200 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001201 }
1202
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001203 int tileFilterPad;
1204 if (doBicubic) {
1205 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1206 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1207 tileFilterPad = 0;
1208 } else {
1209 tileFilterPad = 1;
1210 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001211 params.setFilterMode(textureFilterMode);
1212
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001213 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001214 int tileSize;
1215
1216 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001217 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001218 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001219 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1220 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001221 } else {
1222 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001223 bool needsTextureDomain = needs_texture_domain(bitmap,
1224 srcRect,
1225 params,
joshualitt5531d512014-12-17 15:50:11 -08001226 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001227 doBicubic);
1228 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001229 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001230 srcRect,
1231 params,
1232 paint,
1233 flags,
1234 doBicubic,
1235 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001236 }
1237}
1238
1239// Break 'bitmap' into several tiles to draw it since it has already
1240// been determined to be too large to fit in VRAM
1241void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001242 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001243 const SkRect& srcRect,
1244 const SkIRect& clippedSrcIRect,
1245 const GrTextureParams& params,
1246 const SkPaint& paint,
1247 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001248 int tileSize,
1249 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001250 // The following pixel lock is technically redundant, but it is desirable
1251 // to lock outside of the tile loop to prevent redecoding the whole image
1252 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1253 // is larger than the limit of the discardable memory pool.
1254 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001255 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1256
1257 int nx = bitmap.width() / tileSize;
1258 int ny = bitmap.height() / tileSize;
1259 for (int x = 0; x <= nx; x++) {
1260 for (int y = 0; y <= ny; y++) {
1261 SkRect tileR;
1262 tileR.set(SkIntToScalar(x * tileSize),
1263 SkIntToScalar(y * tileSize),
1264 SkIntToScalar((x + 1) * tileSize),
1265 SkIntToScalar((y + 1) * tileSize));
1266
1267 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1268 continue;
1269 }
1270
1271 if (!tileR.intersect(srcRect)) {
1272 continue;
1273 }
1274
1275 SkBitmap tmpB;
1276 SkIRect iTileR;
1277 tileR.roundOut(&iTileR);
1278 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1279 SkIntToScalar(iTileR.fTop));
1280
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001281 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001282 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001283 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001284 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001285 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001286
robertphillipsec8bb942014-11-21 10:16:25 -08001287 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001288 SkIRect iClampRect;
1289
1290 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1291 // In bleed mode we want to always expand the tile on all edges
1292 // but stay within the bitmap bounds
1293 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1294 } else {
1295 // In texture-domain/clamp mode we only want to expand the
1296 // tile on edges interior to "srcRect" (i.e., we want to
1297 // not bleed across the original clamped edges)
1298 srcRect.roundOut(&iClampRect);
1299 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001300 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1301 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001302 }
1303
1304 if (bitmap.extractSubset(&tmpB, iTileR)) {
1305 // now offset it to make it "local" to our tmp bitmap
1306 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001307 GrTextureParams paramsTemp = params;
1308 bool needsTextureDomain = needs_texture_domain(bitmap,
1309 srcRect,
1310 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001311 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001312 bicubic);
1313 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001314 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001315 tileR,
1316 paramsTemp,
1317 paint,
1318 flags,
1319 bicubic,
1320 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001321 }
1322 }
1323 }
1324}
1325
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001326
1327/*
1328 * This is called by drawBitmap(), which has to handle images that may be too
1329 * large to be represented by a single texture.
1330 *
1331 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1332 * and that non-texture portion of the GrPaint has already been setup.
1333 */
1334void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001335 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001336 const SkRect& srcRect,
1337 const GrTextureParams& params,
1338 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001339 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001340 bool bicubic,
1341 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001342 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1343 bitmap.height() <= fContext->getMaxTextureSize());
1344
1345 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001346 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001347 if (NULL == texture) {
1348 return;
1349 }
1350
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001351 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001352 SkRect paintRect;
1353 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1354 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1355 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1356 SkScalarMul(srcRect.fTop, hInv),
1357 SkScalarMul(srcRect.fRight, wInv),
1358 SkScalarMul(srcRect.fBottom, hInv));
1359
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001360 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001361 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001362 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001363 // Use a constrained texture domain to avoid color bleeding
1364 SkScalar left, top, right, bottom;
1365 if (srcRect.width() > SK_Scalar1) {
1366 SkScalar border = SK_ScalarHalf / texture->width();
1367 left = paintRect.left() + border;
1368 right = paintRect.right() - border;
1369 } else {
1370 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1371 }
1372 if (srcRect.height() > SK_Scalar1) {
1373 SkScalar border = SK_ScalarHalf / texture->height();
1374 top = paintRect.top() + border;
1375 bottom = paintRect.bottom() - border;
1376 } else {
1377 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1378 }
1379 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001380 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001381 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001382 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001383 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001384 SkMatrix::I(),
1385 textureDomain,
1386 GrTextureDomain::kClamp_Mode,
1387 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001388 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001389 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001390 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1391 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001392 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001393 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001394 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001395 }
1396
1397 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1398 // the rest from the SkPaint.
1399 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001400 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001401 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001402 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1403 SkColor2GrColor(paint.getColor());
1404 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001405
joshualitt16b27892014-12-18 07:47:16 -08001406 fContext->drawNonAARectToRect(grPaint, viewMatrix, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001407}
1408
fmalita2d97bc12014-11-20 10:44:58 -08001409bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
1410 const SkImageFilter* filter,
1411 const SkImageFilter::Context& ctx,
1412 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001413 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001414
1415 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1416 // (https://code.google.com/p/skia/issues/detail?id=3148).
1417 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001418
1419 if (filter->canFilterImageGPU()) {
1420 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1421 // filter. Also set the clip wide open and the matrix to identity.
1422 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001423 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001424 } else {
1425 return false;
1426 }
1427}
1428
1429void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1430 int left, int top, const SkPaint& paint) {
1431 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001432 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001433
1434 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1435 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1436 return;
1437 }
1438
1439 int w = bitmap.width();
1440 int h = bitmap.height();
1441
1442 GrTexture* texture;
1443 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001444 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001445
1446 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001447 // This bitmap will own the filtered result as a texture.
1448 SkBitmap filteredBitmap;
1449
bsalomon49f085d2014-09-05 13:34:00 -07001450 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001451 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001452 SkMatrix matrix(*draw.fMatrix);
1453 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001454 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001455 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001456 // This cache is transient, and is freed (along with all its contained
1457 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001458 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001459 if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap,
1460 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001461 texture = (GrTexture*) filteredBitmap.getTexture();
1462 w = filteredBitmap.width();
1463 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001464 left += offset.x();
1465 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001466 } else {
1467 return;
1468 }
1469 }
1470
1471 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001472 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001473
dandov9de5b512014-06-10 14:38:28 -07001474 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1475 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001476
joshualitt16b27892014-12-18 07:47:16 -08001477 fContext->drawNonAARectToRect(grPaint,
1478 SkMatrix::I(),
1479 SkRect::MakeXYWH(SkIntToScalar(left),
1480 SkIntToScalar(top),
1481 SkIntToScalar(w),
1482 SkIntToScalar(h)),
1483 SkRect::MakeXYWH(0,
1484 0,
1485 SK_Scalar1 * w / texture->width(),
1486 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001487}
1488
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001489void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001490 const SkRect* src, const SkRect& dst,
1491 const SkPaint& paint,
1492 SkCanvas::DrawBitmapRectFlags flags) {
1493 SkMatrix matrix;
1494 SkRect bitmapBounds, tmpSrc;
1495
1496 bitmapBounds.set(0, 0,
1497 SkIntToScalar(bitmap.width()),
1498 SkIntToScalar(bitmap.height()));
1499
1500 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001501 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001502 tmpSrc = *src;
1503 } else {
1504 tmpSrc = bitmapBounds;
1505 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001506
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001507 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1508
1509 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001510 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001511 if (!bitmapBounds.contains(tmpSrc)) {
1512 if (!tmpSrc.intersect(bitmapBounds)) {
1513 return; // nothing to draw
1514 }
1515 }
1516 }
1517
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001518 SkRect tmpDst;
1519 matrix.mapRect(&tmpDst, tmpSrc);
1520
1521 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1522 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1523 // Translate so that tempDst's top left is at the origin.
1524 matrix = *origDraw.fMatrix;
1525 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1526 draw.writable()->fMatrix = &matrix;
1527 }
1528 SkSize dstSize;
1529 dstSize.fWidth = tmpDst.width();
1530 dstSize.fHeight = tmpDst.height();
1531
1532 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001533}
1534
1535void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1536 int x, int y, const SkPaint& paint) {
1537 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001538 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001539 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
bsalomonafe30052015-01-16 07:32:33 -08001540 if (fNeedClear) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001541 // TODO: could check here whether we really need to draw at all
reed8eddfb52014-12-04 07:50:14 -08001542 dev->clearAll();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001543 }
1544
1545 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001546 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001547
1548 GrRenderTarget* devRT = dev->accessRenderTarget();
1549 GrTexture* devTex;
1550 if (NULL == (devTex = devRT->asTexture())) {
1551 return;
1552 }
1553
robertphillips7b9e8a42014-12-11 08:20:31 -08001554 const SkImageInfo ii = dev->imageInfo();
1555 int w = ii.width();
1556 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001557
1558 SkImageFilter* filter = paint.getImageFilter();
1559 // This bitmap will own the filtered result as a texture.
1560 SkBitmap filteredBitmap;
1561
bsalomon49f085d2014-09-05 13:34:00 -07001562 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001563 SkIPoint offset = SkIPoint::Make(0, 0);
1564 SkMatrix matrix(*draw.fMatrix);
1565 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001566 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001567 // This cache is transient, and is freed (along with all its contained
1568 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001569 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001570 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001571 if (this->filterTexture(fContext, devTex, filter, ctx, &filteredBitmap,
1572 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001573 devTex = filteredBitmap.getTexture();
1574 w = filteredBitmap.width();
1575 h = filteredBitmap.height();
1576 x += offset.fX;
1577 y += offset.fY;
1578 } else {
1579 return;
1580 }
1581 }
1582
1583 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001584 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001585
dandov9de5b512014-06-10 14:38:28 -07001586 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1587 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001588
1589 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1590 SkIntToScalar(y),
1591 SkIntToScalar(w),
1592 SkIntToScalar(h));
1593
1594 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1595 // scratch texture).
1596 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1597 SK_Scalar1 * h / devTex->height());
1598
joshualitt16b27892014-12-18 07:47:16 -08001599 fContext->drawNonAARectToRect(grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001600}
1601
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001602bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001603 return filter->canFilterImageGPU();
1604}
1605
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001606bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001607 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001608 SkBitmap* result, SkIPoint* offset) {
1609 // want explicitly our impl, so guard against a subclass of us overriding it
1610 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1611 return false;
1612 }
1613
1614 SkAutoLockPixels alp(src, !src.getTexture());
1615 if (!src.getTexture() && !src.readyToDraw()) {
1616 return false;
1617 }
1618
1619 GrTexture* texture;
1620 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1621 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001622 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001623
fmalita2d97bc12014-11-20 10:44:58 -08001624 return this->filterTexture(fContext, texture, filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001625}
1626
1627///////////////////////////////////////////////////////////////////////////////
1628
1629// must be in SkCanvas::VertexMode order
1630static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1631 kTriangles_GrPrimitiveType,
1632 kTriangleStrip_GrPrimitiveType,
1633 kTriangleFan_GrPrimitiveType,
1634};
1635
1636void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1637 int vertexCount, const SkPoint vertices[],
1638 const SkPoint texs[], const SkColor colors[],
1639 SkXfermode* xmode,
1640 const uint16_t indices[], int indexCount,
1641 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001642 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001643 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001644
dandov32a311b2014-07-15 19:46:26 -07001645 const uint16_t* outIndices;
1646 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1647 GrPrimitiveType primType;
1648 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001649
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001650 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1651 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001652
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001653 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001654
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001655 SkPaint copy(paint);
1656 copy.setStyle(SkPaint::kStroke_Style);
1657 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001658
dandov32a311b2014-07-15 19:46:26 -07001659 // we ignore the shader if texs is null.
1660 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1661 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001662
dandov32a311b2014-07-15 19:46:26 -07001663 primType = kLines_GrPrimitiveType;
1664 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001665 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001666 switch (vmode) {
1667 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001668 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001669 break;
1670 case SkCanvas::kTriangleStrip_VertexMode:
1671 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001672 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001673 break;
1674 }
mtklein533eb782014-08-27 10:39:42 -07001675
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001676 VertState state(vertexCount, indices, indexCount);
1677 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001678
dandov32a311b2014-07-15 19:46:26 -07001679 //number of indices for lines per triangle with kLines
1680 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001681
dandov32a311b2014-07-15 19:46:26 -07001682 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1683 outIndices = outAlloc.get();
1684 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001685 int i = 0;
1686 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001687 auxIndices[i] = state.f0;
1688 auxIndices[i + 1] = state.f1;
1689 auxIndices[i + 2] = state.f1;
1690 auxIndices[i + 3] = state.f2;
1691 auxIndices[i + 4] = state.f2;
1692 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001693 i += 6;
1694 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001695 } else {
dandov32a311b2014-07-15 19:46:26 -07001696 outIndices = indices;
1697 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001698
dandov32a311b2014-07-15 19:46:26 -07001699 if (NULL == texs || NULL == paint.getShader()) {
1700 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1701 NULL == colors, &grPaint);
1702 } else {
joshualitt5531d512014-12-17 15:50:11 -08001703 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, NULL == colors, &grPaint);
dandov32a311b2014-07-15 19:46:26 -07001704 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001705 }
1706
mtklein2583b622014-06-04 08:20:41 -07001707#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001708 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001709 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1710 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001711 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001712 }
1713 }
mtklein2583b622014-06-04 08:20:41 -07001714#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001715
1716 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001717 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001718 // need to convert byte order and from non-PM to PM
1719 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001720 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001721 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001722 color = colors[i];
1723 if (paint.getAlpha() != 255) {
1724 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1725 }
1726 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001727 }
1728 colors = convertedColors.get();
1729 }
1730 fContext->drawVertices(grPaint,
joshualitt5531d512014-12-17 15:50:11 -08001731 *draw.fMatrix,
dandov32a311b2014-07-15 19:46:26 -07001732 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001733 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001734 vertices,
1735 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001736 colors,
dandov32a311b2014-07-15 19:46:26 -07001737 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001738 indexCount);
1739}
1740
1741///////////////////////////////////////////////////////////////////////////////
1742
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001743void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001744 size_t byteLength, SkScalar x, SkScalar y,
1745 const SkPaint& paint) {
1746 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001747 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001748
jvanverth8c27a182014-10-14 08:45:50 -07001749 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -08001750 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001751
jvanverth8c27a182014-10-14 08:45:50 -07001752 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001753
joshualitt5531d512014-12-17 15:50:11 -08001754 if (!fTextContext->drawText(grPaint, paint, *draw.fMatrix, (const char *)text, byteLength, x,
1755 y)) {
jvanverth8c27a182014-10-14 08:45:50 -07001756 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001757 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001758 }
1759}
1760
fmalita05c4a432014-09-29 06:29:53 -07001761void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1762 const SkScalar pos[], int scalarsPerPos,
1763 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001764 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001765 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001766
jvanverth8c27a182014-10-14 08:45:50 -07001767 GrPaint grPaint;
joshualitt5531d512014-12-17 15:50:11 -08001768 SkPaint2GrPaintShader(this->context(), paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001769
jvanverth8c27a182014-10-14 08:45:50 -07001770 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001771
joshualitt5531d512014-12-17 15:50:11 -08001772 if (!fTextContext->drawPosText(grPaint, paint, *draw.fMatrix, (const char *)text, byteLength,
1773 pos, scalarsPerPos, offset)) {
jvanverth8c27a182014-10-14 08:45:50 -07001774 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001775 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001776 }
1777}
1778
1779void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001780 size_t len, const SkPath& path,
1781 const SkMatrix* m, const SkPaint& paint) {
1782 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001783
1784 SkASSERT(draw.fDevice == this);
1785 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1786}
1787
1788///////////////////////////////////////////////////////////////////////////////
1789
reedb2db8982014-11-13 12:41:02 -08001790bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001791 if (paint.getShader() ||
reedb2db8982014-11-13 12:41:02 -08001792 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001793 paint.getMaskFilter() ||
1794 paint.getRasterizer() ||
1795 paint.getColorFilter() ||
1796 paint.getPathEffect() ||
1797 paint.isFakeBoldText() ||
reedb2db8982014-11-13 12:41:02 -08001798 paint.getStyle() != SkPaint::kFill_Style)
1799 {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001800 return true;
1801 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001802 return false;
1803}
1804
1805void SkGpuDevice::flush() {
1806 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001807 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001808}
1809
1810///////////////////////////////////////////////////////////////////////////////
1811
fmalita6987dca2014-11-13 08:33:37 -08001812SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(const CreateInfo& cinfo) {
bsalomonf2703d82014-10-28 14:33:06 -07001813 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001814 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001815 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001816 desc.fWidth = cinfo.fInfo.width();
1817 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001818 desc.fSampleCnt = fRenderTarget->numSamples();
1819
1820 SkAutoTUnref<GrTexture> texture;
1821 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001822 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001823
hcm4396fa52014-10-27 21:43:30 -07001824 // layers are never draw in repeat modes, so we can request an approx
1825 // match and ignore any padding.
fmalita6987dca2014-11-13 08:33:37 -08001826 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == cinfo.fUsage) ?
hcm4396fa52014-10-27 21:43:30 -07001827 GrContext::kApprox_ScratchTexMatch :
1828 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001829 texture.reset(fContext->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001830
1831 if (texture) {
1832 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry);
1833 return SkGpuDevice::Create(texture->asRenderTarget(), &props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001834 } else {
tfarina38406c82014-10-31 07:11:12 -07001835 SkDebugf("---- failed to create compatible device texture [%d %d]\n",
fmalita6987dca2014-11-13 08:33:37 -08001836 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001837 return NULL;
1838 }
1839}
1840
reed4a8126e2014-09-22 07:29:03 -07001841SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001842 // TODO: Change the signature of newSurface to take a budgeted parameter.
1843 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
1844 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->numSamples(),
1845 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001846}
1847
robertphillips30d2cc62014-09-24 08:52:18 -07001848bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001849 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001850#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001851 // todo: should handle this natively
1852 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001853 return false;
1854 }
1855
robertphillips82365912014-11-12 09:32:34 -08001856 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips81f71b62014-11-11 04:54:49 -08001857
1858 const SkPicture::AccelData* data = mainPicture->EXPERIMENTAL_getAccelData(key);
1859 if (!data) {
1860 return false;
1861 }
1862
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001863 SkRect clipBounds;
robertphillips0c423322014-07-31 11:02:38 -07001864 if (!mainCanvas->getClipBounds(&clipBounds)) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001865 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001866 }
1867
robertphillips30d78412014-11-24 09:49:17 -08001868 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1869
1870 const SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1871
robertphillipsfd61ed02014-10-28 07:21:44 -07001872 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001873
robertphillipsfd61ed02014-10-28 07:21:44 -07001874 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001875 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001876 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001877 &atlasedNeedRendering, &atlasedRecycled,
1878 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001879
1880 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1881
1882 SkTDArray<GrHoistedLayer> needRendering, recycled;
1883
1884 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001885 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001886 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001887 &needRendering, &recycled,
1888 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001889
1890 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001891
robertphillips64bf7672014-08-21 13:07:35 -07001892 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001893 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1894 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001895
robertphillipsfd61ed02014-10-28 07:21:44 -07001896 GrLayerHoister::UnlockLayers(fContext, needRendering);
1897 GrLayerHoister::UnlockLayers(fContext, recycled);
1898 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1899 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001900
1901 return true;
robertphillips63242d72014-12-04 08:31:02 -08001902#else
1903 return false;
1904#endif
robertphillips64bf7672014-08-21 13:07:35 -07001905}
1906
senorblancobe129b22014-08-08 07:14:35 -07001907SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001908 // We always return a transient cache, so it is freed after each
1909 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001910 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001911}
reedf037e0b2014-10-30 11:34:15 -07001912
1913#endif