blob: d384cd17ca5b9e20605e31c050eee33d62945a56 [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
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000010#include "GrBitmapTextContext.h"
kkinnunenabcfab42015-02-22 22:53:44 -080011#include "GrContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000012#include "GrDistanceFieldTextContext.h"
kkinnunenabcfab42015-02-22 22:53:44 -080013#include "GrGpu.h"
14#include "GrGpuResourcePriv.h"
robertphillips98d709b2014-09-02 10:20:50 -070015#include "GrLayerHoister.h"
robertphillips274b4ba2014-09-04 07:24:18 -070016#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070017#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070018#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080019#include "SkCanvasPriv.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000020#include "SkDeviceImageFilterProxy.h"
21#include "SkDrawProcs.h"
kkinnunenabcfab42015-02-22 22:53:44 -080022#include "SkErrorInternals.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000023#include "SkGlyphCache.h"
kkinnunenabcfab42015-02-22 22:53:44 -080024#include "SkGrTexturePixelRef.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080026#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000027#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000029#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070030#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000031#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080032#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000034#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080035#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000036#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000038#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070039#include "SkXfermode.h"
kkinnunenabcfab42015-02-22 22:53:44 -080040#include "effects/GrBicubicEffect.h"
41#include "effects/GrDashingEffect.h"
42#include "effects/GrSimpleTextureEffect.h"
43#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000044
reedf037e0b2014-10-30 11:34:15 -070045#if SK_SUPPORT_GPU
46
senorblanco55b6d8b2014-07-30 11:26:46 -070047enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
48
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000049#if 0
50 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080051 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000052 do { \
53 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080054 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000055 } while (0)
56#else
joshualitt5531d512014-12-17 15:50:11 -080057 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058#endif
59
60// This constant represents the screen alignment criterion in texels for
61// requiring texture domain clamping to prevent color bleeding when drawing
62// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000063#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000064
65#define DO_DEFERRED_CLEAR() \
66 do { \
bsalomonafe30052015-01-16 07:32:33 -080067 if (fNeedClear) { \
68 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000069 } \
70 } while (false) \
71
72///////////////////////////////////////////////////////////////////////////////
73
74#define CHECK_FOR_ANNOTATION(paint) \
75 do { if (paint.getAnnotation()) { return; } } while (0)
76
77///////////////////////////////////////////////////////////////////////////////
78
bsalomonbcf0a522014-10-08 08:40:09 -070079// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
80// just accesses the backing GrTexture. Otherwise, it creates a cached texture
81// representation and releases it in the destructor.
82class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040083public:
bsalomonbcf0a522014-10-08 08:40:09 -070084 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070085
bsalomonbcf0a522014-10-08 08:40:09 -070086 AutoBitmapTexture(GrContext* context,
87 const SkBitmap& bitmap,
88 const GrTextureParams* params,
89 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040090 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070091 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040092 }
93
bsalomonbcf0a522014-10-08 08:40:09 -070094 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040095 const SkBitmap& bitmap,
96 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -070097 // Either get the texture directly from the bitmap, or else use the cache and
98 // remember to unref it.
99 if (GrTexture* bmpTexture = bitmap.getTexture()) {
100 fTexture.reset(NULL);
101 return bmpTexture;
102 } else {
103 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
104 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400105 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400106 }
107
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000108private:
bsalomonbcf0a522014-10-08 08:40:09 -0700109 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000110};
111
112///////////////////////////////////////////////////////////////////////////////
113
114struct GrSkDrawProcs : public SkDrawProcs {
115public:
116 GrContext* fContext;
117 GrTextContext* fTextContext;
118 GrFontScaler* fFontScaler; // cached in the skia glyphcache
119};
120
121///////////////////////////////////////////////////////////////////////////////
122
bsalomonafe30052015-01-16 07:32:33 -0800123SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags) {
124 if (!rt || rt->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000125 return NULL;
126 }
bsalomonafe30052015-01-16 07:32:33 -0800127 return SkNEW_ARGS(SkGpuDevice, (rt, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000128}
129
bsalomonafe30052015-01-16 07:32:33 -0800130static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps* props) {
131 if (props) {
132 return SkDeviceProperties(props->pixelGeometry());
133 } else {
134 return SkDeviceProperties(SkDeviceProperties::kLegacyLCD_InitType);
135 }
reedb2db8982014-11-13 12:41:02 -0800136}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000137
bsalomonafe30052015-01-16 07:32:33 -0800138static SkSurfaceProps copy_or_default_props(const SkSurfaceProps* props) {
139 if (props) {
140 return SkSurfaceProps(*props);
141 } else {
142 return SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
143 }
144}
145
146SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags)
reedb2db8982014-11-13 12:41:02 -0800147 : INHERITED(surfaceprops_to_deviceprops(props))
bsalomonafe30052015-01-16 07:32:33 -0800148 , fSurfaceProps(copy_or_default_props(props))
reedb2db8982014-11-13 12:41:02 -0800149{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000150 fDrawProcs = NULL;
151
bsalomonafe30052015-01-16 07:32:33 -0800152 fContext = SkRef(rt->getContext());
153 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000154
bsalomonafe30052015-01-16 07:32:33 -0800155 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000156
bsalomonafe30052015-01-16 07:32:33 -0800157 SkImageInfo info = rt->surfacePriv().info();
158 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt));
bsalomonafbf2d62014-09-30 12:18:44 -0700159 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700160 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700161
bsalomonafe30052015-01-16 07:32:33 -0800162 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts();
jvanverth4736e142014-11-07 07:12:46 -0800163 fTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFT);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000164}
165
kkinnunenabcfab42015-02-22 22:53:44 -0800166GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
167 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000168 if (kUnknown_SkColorType == origInfo.colorType() ||
169 origInfo.width() < 0 || origInfo.height() < 0) {
170 return NULL;
171 }
172
bsalomonafe30052015-01-16 07:32:33 -0800173 if (!context) {
174 return NULL;
175 }
176
reede5ea5002014-09-03 11:54:58 -0700177 SkColorType ct = origInfo.colorType();
178 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700179 if (kRGB_565_SkColorType == ct) {
180 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800181 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
182 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700183 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800184 }
185 if (kOpaque_SkAlphaType != at) {
186 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000187 }
reede5ea5002014-09-03 11:54:58 -0700188 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000189
bsalomonf2703d82014-10-28 14:33:06 -0700190 GrSurfaceDesc desc;
191 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000192 desc.fWidth = info.width();
193 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000194 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000195 desc.fSampleCnt = sampleCount;
kkinnunenabcfab42015-02-22 22:53:44 -0800196 GrTexture* texture = context->createTexture(desc, SkToBool(budgeted), NULL, 0);
197 if (NULL == texture) {
198 return NULL;
199 }
200 SkASSERT(NULL != texture->asRenderTarget());
201 return texture->asRenderTarget();
202}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000203
kkinnunenabcfab42015-02-22 22:53:44 -0800204SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
205 const SkImageInfo& info, int sampleCount,
206 const SkSurfaceProps* props, unsigned flags) {
207
208 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
209 if (NULL == rt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000210 return NULL;
211 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000212
kkinnunenabcfab42015-02-22 22:53:44 -0800213 return SkNEW_ARGS(SkGpuDevice, (rt, props, flags));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000214}
215
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000216SkGpuDevice::~SkGpuDevice() {
217 if (fDrawProcs) {
218 delete fDrawProcs;
219 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000220
jvanverth8c27a182014-10-14 08:45:50 -0700221 delete fTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000222
joshualitt29070592015-02-25 13:04:43 -0800223 if (fContext->getClip() == &fClipData) {
224 fContext->setClip(NULL);
225 }
226
bsalomon32d0b3b2014-08-29 07:50:23 -0700227 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000228 fContext->unref();
229}
230
231///////////////////////////////////////////////////////////////////////////////
232
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000233bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
234 int x, int y) {
235 DO_DEFERRED_CLEAR();
236
237 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000238 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000239 if (kUnknown_GrPixelConfig == config) {
240 return false;
241 }
242
243 uint32_t flags = 0;
244 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
245 flags = GrContext::kUnpremul_PixelOpsFlag;
246 }
247 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
248 config, dstPixels, dstRowBytes, flags);
249}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000250
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000251bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
252 int x, int y) {
253 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000254 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000255 if (kUnknown_GrPixelConfig == config) {
256 return false;
257 }
258 uint32_t flags = 0;
259 if (kUnpremul_SkAlphaType == info.alphaType()) {
260 flags = GrContext::kUnpremul_PixelOpsFlag;
261 }
262 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
263
264 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700265 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000266
267 return true;
268}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000269
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000270const SkBitmap& SkGpuDevice::onAccessBitmap() {
271 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700272 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000273}
274
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000275void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
276 INHERITED::onAttachToCanvas(canvas);
277
278 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800279 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000280}
281
282void SkGpuDevice::onDetachFromCanvas() {
283 INHERITED::onDetachFromCanvas();
joshualitt29070592015-02-25 13:04:43 -0800284 fClipData.reset();
joshualitt44701df2015-02-23 14:44:57 -0800285 fClipStack.reset(NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000286}
287
288// call this every draw call, to ensure that the context reflects our state,
289// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800290void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800291 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000292
joshualitt44701df2015-02-23 14:44:57 -0800293 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000294
joshualitt29070592015-02-25 13:04:43 -0800295 fClipData.setClipStack(fClipStack, &this->getOrigin());
296
297 fContext->setClip(&fClipData);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000298
299 DO_DEFERRED_CLEAR();
300}
301
302GrRenderTarget* SkGpuDevice::accessRenderTarget() {
303 DO_DEFERRED_CLEAR();
304 return fRenderTarget;
305}
306
reed8eddfb52014-12-04 07:50:14 -0800307void SkGpuDevice::clearAll() {
308 GrColor color = 0;
309 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
310 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
311 fContext->clear(&rect, color, true, fRenderTarget);
bsalomonafe30052015-01-16 07:32:33 -0800312 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800313}
314
kkinnunenabcfab42015-02-22 22:53:44 -0800315void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
316 // Caller must have accessed the render target, because it knows the rt must be replaced.
317 SkASSERT(!fNeedClear);
318
319 SkSurface::Budgeted budgeted =
320 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
321 : SkSurface::kNo_Budgeted;
322
323 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
324 fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->numSamples()));
325
326 if (NULL == newRT) {
327 return;
328 }
329
330 if (shouldRetainContent) {
331 if (fRenderTarget->wasDestroyed()) {
332 return;
333 }
334 this->context()->copySurface(newRT, fRenderTarget);
335 }
336
337 SkASSERT(fRenderTarget != newRT);
338
339 fRenderTarget->unref();
340 fRenderTarget = newRT.detach();
341
342 SkASSERT(fRenderTarget->surfacePriv().info() == fLegacyBitmap.info());
343 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (fRenderTarget->surfacePriv().info(), fRenderTarget));
344 fLegacyBitmap.setPixelRef(pr)->unref();
345}
346
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000347///////////////////////////////////////////////////////////////////////////////
348
349SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
350SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
351SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
352SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
353SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
354 shader_type_mismatch);
355SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
356 shader_type_mismatch);
357SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
358SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
359
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000360///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000361
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000362void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800363 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700364 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000365
366 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800367 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000368
joshualitt29070592015-02-25 13:04:43 -0800369 fContext->drawPaint(fRenderTarget, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000370}
371
372// must be in SkCanvas::PointMode order
373static const GrPrimitiveType gPointMode2PrimtiveType[] = {
374 kPoints_GrPrimitiveType,
375 kLines_GrPrimitiveType,
376 kLineStrip_GrPrimitiveType
377};
378
379void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
380 size_t count, const SkPoint pts[], const SkPaint& paint) {
381 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800382 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000383
384 SkScalar width = paint.getStrokeWidth();
385 if (width < 0) {
386 return;
387 }
388
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000389 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700390 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
391 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800392 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
egdaniele61c4112014-06-12 10:24:21 -0700393 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700394 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700395 path.moveTo(pts[0]);
396 path.lineTo(pts[1]);
joshualitt29070592015-02-25 13:04:43 -0800397 fContext->drawPath(fRenderTarget, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700398 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000399 }
400
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000401 // we only handle hairlines and paints without path effects or mask filters,
402 // else we let the SkDraw call our drawPath()
403 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
404 draw.drawPoints(mode, count, pts, paint, true);
405 return;
406 }
407
408 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800409 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000410
joshualitt25d9c152015-02-18 12:29:52 -0800411 fContext->drawVertices(fRenderTarget,
412 grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800413 *draw.fMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000414 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000415 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000416 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000417 NULL,
418 NULL,
419 NULL,
420 0);
421}
422
423///////////////////////////////////////////////////////////////////////////////
424
425void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
426 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700427 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
428
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000429 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800430 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000431
432 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
433 SkScalar width = paint.getStrokeWidth();
434
435 /*
436 We have special code for hairline strokes, miter-strokes, bevel-stroke
437 and fills. Anything else we just call our path code.
438 */
439 bool usePath = doStroke && width > 0 &&
440 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
441 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
442 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700443
444 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000445 usePath = true;
446 }
egdanield58a0ba2014-06-11 10:30:05 -0700447
joshualitt5531d512014-12-17 15:50:11 -0800448 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000449#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
450 if (doStroke) {
451#endif
452 usePath = true;
453#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
454 } else {
joshualitt5531d512014-12-17 15:50:11 -0800455 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000456 }
457#endif
458 }
459 // until we can both stroke and fill rectangles
460 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
461 usePath = true;
462 }
463
egdanield58a0ba2014-06-11 10:30:05 -0700464 GrStrokeInfo strokeInfo(paint);
465
466 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700467 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700468 usePath = true;
469 }
470
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000471 if (usePath) {
472 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700473 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000474 path.addRect(rect);
475 this->drawPath(draw, path, paint, NULL, true);
476 return;
477 }
478
479 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800480 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400481
joshualitt29070592015-02-25 13:04:43 -0800482 fContext->drawRect(fRenderTarget, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000483}
484
485///////////////////////////////////////////////////////////////////////////////
486
487void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800488 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700489 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000490 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800491 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000492
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000493 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800494 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400495
egdanield58a0ba2014-06-11 10:30:05 -0700496 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000497 if (paint.getMaskFilter()) {
498 // try to hit the fast path for drawing filtered round rects
499
500 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800501 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000502 if (devRRect.allCornersCircular()) {
503 SkRect maskRect;
504 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800505 draw.fClip->getBounds(),
506 *draw.fMatrix,
507 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000508 SkIRect finalIRect;
509 maskRect.roundOut(&finalIRect);
510 if (draw.fClip->quickReject(finalIRect)) {
511 // clipped out
512 return;
513 }
joshualitt25d9c152015-02-18 12:29:52 -0800514 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
515 fRenderTarget,
516 &grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800517 *draw.fMatrix,
egdanield58a0ba2014-06-11 10:30:05 -0700518 strokeInfo.getStrokeRec(),
519 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000520 return;
521 }
522 }
523
524 }
525 }
526
527 }
528
egdanield58a0ba2014-06-11 10:30:05 -0700529 bool usePath = false;
530
531 if (paint.getMaskFilter()) {
532 usePath = true;
533 } else {
534 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700535 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700536 usePath = true;
537 }
538 }
539
540
541 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000542 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700543 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000544 path.addRRect(rect);
545 this->drawPath(draw, path, paint, NULL, true);
546 return;
547 }
Mike Klein744fb732014-06-23 15:13:26 -0400548
joshualitt29070592015-02-25 13:04:43 -0800549 fContext->drawRRect(fRenderTarget, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550}
551
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000552void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800553 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000554 SkStrokeRec stroke(paint);
555 if (stroke.isFillStyle()) {
556
557 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800558 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000559
560 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800561 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000562
563 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
joshualitt29070592015-02-25 13:04:43 -0800564 fContext->drawDRRect(fRenderTarget, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000565 return;
566 }
567 }
568
569 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700570 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000571 path.addRRect(outer);
572 path.addRRect(inner);
573 path.setFillType(SkPath::kEvenOdd_FillType);
574
575 this->drawPath(draw, path, paint, NULL, true);
576}
577
578
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000579/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000580
581void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
582 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700583 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000584 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800585 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000586
egdanield58a0ba2014-06-11 10:30:05 -0700587 GrStrokeInfo strokeInfo(paint);
588
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000589 bool usePath = false;
590 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700591 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000592 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700593 } else {
594 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700595 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700596 usePath = true;
597 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000598 }
599
600 if (usePath) {
601 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700602 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000603 path.addOval(oval);
604 this->drawPath(draw, path, paint, NULL, true);
605 return;
606 }
607
608 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800609 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000610
joshualitt29070592015-02-25 13:04:43 -0800611 fContext->drawOval(fRenderTarget, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000612}
613
614#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000615
616///////////////////////////////////////////////////////////////////////////////
617
618// helpers for applying mask filters
619namespace {
620
621// Draw a mask using the supplied paint. Since the coverage/geometry
622// is already burnt into the mask this boils down to a rect draw.
623// Return true if the mask was successfully drawn.
joshualitt25d9c152015-02-18 12:29:52 -0800624bool draw_mask(GrContext* context,
625 GrRenderTarget* rt,
626 const SkMatrix& viewMatrix,
627 const SkRect& maskRect,
628 GrPaint* grp,
629 GrTexture* mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000630 SkMatrix matrix;
631 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
632 matrix.postIDiv(mask->width(), mask->height());
633
joshualitt16b27892014-12-18 07:47:16 -0800634 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix,
635 kDevice_GrCoordSet))->unref();
636
637 SkMatrix inverse;
638 if (!viewMatrix.invert(&inverse)) {
639 return false;
640 }
joshualitt29070592015-02-25 13:04:43 -0800641 context->drawNonAARectWithLocalMatrix(rt, *grp, SkMatrix::I(), maskRect, inverse);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000642 return true;
643}
644
joshualitt25d9c152015-02-18 12:29:52 -0800645bool draw_with_mask_filter(GrContext* context,
646 GrRenderTarget* rt,
647 const SkMatrix& viewMatrix,
648 const SkPath& devPath,
649 SkMaskFilter* filter,
650 const SkRegion& clip,
651 GrPaint* grp,
652 SkPaint::Style style) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000653 SkMask srcM, dstM;
654
joshualitt5531d512014-12-17 15:50:11 -0800655 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &viewMatrix, &srcM,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000656 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
657 return false;
658 }
659 SkAutoMaskFreeImage autoSrc(srcM.fImage);
660
joshualitt5531d512014-12-17 15:50:11 -0800661 if (!filter->filterMask(&dstM, srcM, viewMatrix, NULL)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000662 return false;
663 }
664 // this will free-up dstM when we're done (allocated in filterMask())
665 SkAutoMaskFreeImage autoDst(dstM.fImage);
666
667 if (clip.quickReject(dstM.fBounds)) {
668 return false;
669 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000670
671 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
672 // the current clip (and identity matrix) and GrPaint settings
bsalomonf2703d82014-10-28 14:33:06 -0700673 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000674 desc.fWidth = dstM.fBounds.width();
675 desc.fHeight = dstM.fBounds.height();
676 desc.fConfig = kAlpha_8_GrPixelConfig;
677
bsalomone3059732014-10-14 11:47:22 -0700678 SkAutoTUnref<GrTexture> texture(
679 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
680 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000681 return false;
682 }
683 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
684 dstM.fImage, dstM.fRowBytes);
685
686 SkRect maskRect = SkRect::Make(dstM.fBounds);
687
joshualitt29070592015-02-25 13:04:43 -0800688 return draw_mask(context, rt, viewMatrix, maskRect, grp, texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000689}
690
bsalomone3059732014-10-14 11:47:22 -0700691// Create a mask of 'devPath' and place the result in 'mask'.
692GrTexture* create_mask_GPU(GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800693 GrRenderTarget* rt,
bsalomone3059732014-10-14 11:47:22 -0700694 const SkRect& maskRect,
695 const SkPath& devPath,
696 const GrStrokeInfo& strokeInfo,
697 bool doAA,
698 int sampleCnt) {
bsalomonf2703d82014-10-28 14:33:06 -0700699 GrSurfaceDesc desc;
700 desc.fFlags = kRenderTarget_GrSurfaceFlag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000701 desc.fWidth = SkScalarCeilToInt(maskRect.width());
702 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700703 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000704 // We actually only need A8, but it often isn't supported as a
705 // render target so default to RGBA_8888
706 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700707
708 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
709 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000710 desc.fConfig = kAlpha_8_GrPixelConfig;
711 }
712
bsalomone3059732014-10-14 11:47:22 -0700713 GrTexture* mask = context->refScratchTexture(desc,GrContext::kApprox_ScratchTexMatch);
714 if (NULL == mask) {
715 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000716 }
717
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000718 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
719
joshualitt29070592015-02-25 13:04:43 -0800720 GrContext::AutoClip ac(context, clipRect);
721
bsalomon89c62982014-11-03 12:08:42 -0800722 context->clear(NULL, 0x0, true, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000723
724 GrPaint tempPaint;
egdanielb197b8f2015-02-17 07:34:43 -0800725 tempPaint.setAntiAlias(doAA);
726 tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000727
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000728 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
729 SkMatrix translate;
730 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
joshualitt29070592015-02-25 13:04:43 -0800731 context->drawPath(mask->asRenderTarget(), tempPaint, translate, devPath, strokeInfo);
bsalomone3059732014-10-14 11:47:22 -0700732 return mask;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000733}
734
735SkBitmap wrap_texture(GrTexture* texture) {
736 SkBitmap result;
bsalomonafbf2d62014-09-30 12:18:44 -0700737 result.setInfo(texture->surfacePriv().info());
reed6c225732014-06-09 19:52:07 -0700738 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000739 return result;
740}
741
742};
743
744void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
745 const SkPaint& paint, const SkMatrix* prePathMatrix,
746 bool pathIsMutable) {
747 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800748 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700749 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000750
jvanverthb3eb6872014-10-24 07:12:51 -0700751 SkASSERT(!pathIsMutable || origSrcPath.isVolatile());
joshualitt5531d512014-12-17 15:50:11 -0800752
bsalomon54443932015-01-29 09:34:18 -0800753 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000754
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000755 // If we have a prematrix, apply it to the path, optimizing for the case
756 // where the original path can in fact be modified in place (even though
757 // its parameter type is const).
758 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000759 SkTLazy<SkPath> tmpPath;
760 SkTLazy<SkPath> effectPath;
bsalomon54443932015-01-29 09:34:18 -0800761 SkPathEffect* pathEffect = paint.getPathEffect();
762
763 SkMatrix viewMatrix = *draw.fMatrix;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764
765 if (prePathMatrix) {
bsalomon54443932015-01-29 09:34:18 -0800766 // stroking and path effects are supposed to be applied *after* the prePathMatrix.
767 // The pre-path-matrix also should not affect shadeing.
768 if (NULL == pathEffect && NULL == paint.getShader() &&
769 (strokeInfo.getStrokeRec().isFillStyle() ||
770 strokeInfo.getStrokeRec().isHairlineStyle())) {
771 viewMatrix.preConcat(*prePathMatrix);
772 } else {
773 SkPath* result = pathPtr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000774
bsalomon54443932015-01-29 09:34:18 -0800775 if (!pathIsMutable) {
776 result = tmpPath.init();
777 result->setIsVolatile(true);
778 pathIsMutable = true;
779 }
780 // should I push prePathMatrix on our MV stack temporarily, instead
781 // of applying it here? See SkDraw.cpp
782 pathPtr->transform(*prePathMatrix, result);
783 pathPtr = result;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000784 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000785 }
786 // at this point we're done with prePathMatrix
787 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
788
bsalomon54443932015-01-29 09:34:18 -0800789 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800790 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, viewMatrix, true, &grPaint);
bsalomon54443932015-01-29 09:34:18 -0800791
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000792 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700793 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
794 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000795 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000796 pathPtr = effectPath.get();
797 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700798 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000799 }
800
egdanield58a0ba2014-06-11 10:30:05 -0700801 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000802 if (paint.getMaskFilter()) {
803 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000804 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
805 if (stroke.applyToPath(strokedPath, *pathPtr)) {
806 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000807 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700808 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000809 }
810 }
811
812 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000813 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700814 if (!pathIsMutable) {
815 devPathPtr->setIsVolatile(true);
816 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000817
818 // transform the path into device space
bsalomon54443932015-01-29 09:34:18 -0800819 pathPtr->transform(viewMatrix, devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000820
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000821 SkRect maskRect;
822 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
823 draw.fClip->getBounds(),
bsalomon54443932015-01-29 09:34:18 -0800824 viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000825 &maskRect)) {
826 SkIRect finalIRect;
827 maskRect.roundOut(&finalIRect);
828 if (draw.fClip->quickReject(finalIRect)) {
829 // clipped out
830 return;
831 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000832
joshualitt25d9c152015-02-18 12:29:52 -0800833 if (paint.getMaskFilter()->directFilterMaskGPU(fContext,
834 fRenderTarget,
835 &grPaint,
836 viewMatrix,
837 stroke,
838 *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000839 // the mask filter was able to draw itself directly, so there's nothing
840 // left to do.
841 return;
842 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000843
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000844
joshualitt25d9c152015-02-18 12:29:52 -0800845 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext,
846 fRenderTarget,
847 maskRect,
848 *devPathPtr,
849 strokeInfo,
850 grPaint.isAntiAlias(),
bsalomone3059732014-10-14 11:47:22 -0700851 fRenderTarget->numSamples()));
852 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000853 GrTexture* filtered;
854
bsalomon54443932015-01-29 09:34:18 -0800855 if (paint.getMaskFilter()->filterMaskGPU(mask, viewMatrix, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000856 // filterMaskGPU gives us ownership of a ref to the result
857 SkAutoTUnref<GrTexture> atu(filtered);
joshualitt29070592015-02-25 13:04:43 -0800858 if (draw_mask(fContext, fRenderTarget, viewMatrix, maskRect, &grPaint,
joshualitt25d9c152015-02-18 12:29:52 -0800859 filtered)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000860 // This path is completely drawn
861 return;
862 }
863 }
864 }
865 }
866
867 // draw the mask on the CPU - this is a fallthrough path in case the
868 // GPU path fails
869 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
870 SkPaint::kFill_Style;
joshualitt29070592015-02-25 13:04:43 -0800871 draw_with_mask_filter(fContext, fRenderTarget, viewMatrix, *devPathPtr,
joshualitt25d9c152015-02-18 12:29:52 -0800872 paint.getMaskFilter(), *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000873 return;
874 }
875
joshualitt29070592015-02-25 13:04:43 -0800876 fContext->drawPath(fRenderTarget, grPaint, viewMatrix, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000877}
878
879static const int kBmpSmallTileSize = 1 << 10;
880
881static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
882 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
883 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
884 return tilesX * tilesY;
885}
886
887static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
888 if (maxTileSize <= kBmpSmallTileSize) {
889 return maxTileSize;
890 }
891
892 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
893 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
894
895 maxTileTotalTileSize *= maxTileSize * maxTileSize;
896 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
897
898 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
899 return kBmpSmallTileSize;
900 } else {
901 return maxTileSize;
902 }
903}
904
905// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
906// pixels from the bitmap are necessary.
907static void determine_clipped_src_rect(const GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800908 const GrRenderTarget* rt,
joshualitt5531d512014-12-17 15:50:11 -0800909 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000910 const SkBitmap& bitmap,
911 const SkRect* srcRectPtr,
912 SkIRect* clippedSrcIRect) {
joshualitt29070592015-02-25 13:04:43 -0800913 const GrClip* clip = context->getClip();
914 clip->getConservativeBounds(rt, clippedSrcIRect, NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000915 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800916 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000917 clippedSrcIRect->setEmpty();
918 return;
919 }
920 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
921 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700922 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000923 // we've setup src space 0,0 to map to the top left of the src rect.
924 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000925 if (!clippedSrcRect.intersect(*srcRectPtr)) {
926 clippedSrcIRect->setEmpty();
927 return;
928 }
929 }
930 clippedSrcRect.roundOut(clippedSrcIRect);
931 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
932 if (!clippedSrcIRect->intersect(bmpBounds)) {
933 clippedSrcIRect->setEmpty();
934 }
935}
936
937bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800938 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000939 const GrTextureParams& params,
940 const SkRect* srcRectPtr,
941 int maxTileSize,
942 int* tileSize,
943 SkIRect* clippedSrcRect) const {
944 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700945 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000946 return false;
947 }
948
949 // if it's larger than the max tile size, then we have no choice but tiling.
950 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt29070592015-02-25 13:04:43 -0800951 determine_clipped_src_rect(fContext, fRenderTarget, viewMatrix, bitmap, srcRectPtr,
952 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000953 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
954 return true;
955 }
956
957 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
958 return false;
959 }
960
961 // if the entire texture is already in our cache then no reason to tile it
962 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
963 return false;
964 }
965
966 // At this point we know we could do the draw by uploading the entire bitmap
967 // as a texture. However, if the texture would be large compared to the
968 // cache size and we don't require most of it for this draw then tile to
969 // reduce the amount of upload and cache spill.
970
971 // assumption here is that sw bitmap size is a good proxy for its size as
972 // a texture
973 size_t bmpSize = bitmap.getSize();
974 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000975 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000976 if (bmpSize < cacheSize / 2) {
977 return false;
978 }
979
980 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt29070592015-02-25 13:04:43 -0800981 determine_clipped_src_rect(fContext, fRenderTarget, viewMatrix, bitmap, srcRectPtr,
joshualitt25d9c152015-02-18 12:29:52 -0800982 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000983 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
984 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
985 kBmpSmallTileSize * kBmpSmallTileSize;
986
987 return usedTileBytes < 2 * bmpSize;
988}
989
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000990void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000991 const SkBitmap& bitmap,
992 const SkMatrix& m,
993 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000994 SkMatrix concat;
995 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
996 if (!m.isIdentity()) {
997 concat.setConcat(*draw->fMatrix, m);
998 draw.writable()->fMatrix = &concat;
999 }
1000 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001001}
1002
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001003// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001004// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1005// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001006static inline void clamped_outset_with_offset(SkIRect* iRect,
1007 int outset,
1008 SkPoint* offset,
1009 const SkIRect& clamp) {
1010 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001011
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001012 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1013 if (leftClampDelta > 0) {
1014 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001015 iRect->fLeft = clamp.fLeft;
1016 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001017 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001018 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001019
1020 int topClampDelta = clamp.fTop - iRect->fTop;
1021 if (topClampDelta > 0) {
1022 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001023 iRect->fTop = clamp.fTop;
1024 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001025 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001026 }
1027
1028 if (iRect->fRight > clamp.fRight) {
1029 iRect->fRight = clamp.fRight;
1030 }
1031 if (iRect->fBottom > clamp.fBottom) {
1032 iRect->fBottom = clamp.fBottom;
1033 }
1034}
1035
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001036static bool has_aligned_samples(const SkRect& srcRect,
1037 const SkRect& transformedRect) {
1038 // detect pixel disalignment
1039 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1040 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1041 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1042 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1043 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1044 COLOR_BLEED_TOLERANCE &&
1045 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1046 COLOR_BLEED_TOLERANCE) {
1047 return true;
1048 }
1049 return false;
1050}
1051
1052static bool may_color_bleed(const SkRect& srcRect,
1053 const SkRect& transformedRect,
1054 const SkMatrix& m) {
1055 // Only gets called if has_aligned_samples returned false.
1056 // So we can assume that sampling is axis aligned but not texel aligned.
1057 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1058 SkRect innerSrcRect(srcRect), innerTransformedRect,
1059 outerTransformedRect(transformedRect);
1060 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1061 m.mapRect(&innerTransformedRect, innerSrcRect);
1062
1063 // The gap between outerTransformedRect and innerTransformedRect
1064 // represents the projection of the source border area, which is
1065 // problematic for color bleeding. We must check whether any
1066 // destination pixels sample the border area.
1067 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1068 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1069 SkIRect outer, inner;
1070 outerTransformedRect.round(&outer);
1071 innerTransformedRect.round(&inner);
1072 // If the inner and outer rects round to the same result, it means the
1073 // border does not overlap any pixel centers. Yay!
1074 return inner != outer;
1075}
1076
1077static bool needs_texture_domain(const SkBitmap& bitmap,
1078 const SkRect& srcRect,
1079 GrTextureParams &params,
1080 const SkMatrix& contextMatrix,
1081 bool bicubic) {
1082 bool needsTextureDomain = false;
1083
1084 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1085 // Need texture domain if drawing a sub rect
1086 needsTextureDomain = srcRect.width() < bitmap.width() ||
1087 srcRect.height() < bitmap.height();
1088 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1089 // sampling is axis-aligned
1090 SkRect transformedRect;
1091 contextMatrix.mapRect(&transformedRect, srcRect);
1092
1093 if (has_aligned_samples(srcRect, transformedRect)) {
1094 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1095 needsTextureDomain = false;
1096 } else {
1097 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1098 }
1099 }
1100 }
1101 return needsTextureDomain;
1102}
1103
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001104void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1105 const SkBitmap& bitmap,
1106 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001107 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001108 const SkPaint& paint,
1109 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -08001110 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001111
1112 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001113 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001114 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1115 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001116 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001117 SkScalar w = SkIntToScalar(bitmap.width());
1118 SkScalar h = SkIntToScalar(bitmap.height());
1119 dstSize.fWidth = w;
1120 dstSize.fHeight = h;
1121 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001122 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001123 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001124 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001125 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001126 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001127 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1128 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1129 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1130 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001131 }
1132
derekf367e1862014-12-02 11:02:06 -08001133 // If the render target is not msaa and draw is antialiased, we call
1134 // drawRect instead of drawing on the render target directly.
1135 // FIXME: the tiled bitmap code path doesn't currently support
1136 // anti-aliased edges, we work around that for now by drawing directly
1137 // if the image size exceeds maximum texture size.
1138 int maxTextureSize = fContext->getMaxTextureSize();
1139 bool directDraw = fRenderTarget->isMultisampled() ||
1140 !paint.isAntiAlias() ||
1141 bitmap.width() > maxTextureSize ||
1142 bitmap.height() > maxTextureSize;
1143
1144 // we check whether dst rect are pixel aligned
1145 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -08001146 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -08001147
1148 if (staysRect) {
1149 SkRect rect;
1150 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -08001151 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -08001152 const SkScalar *scalars = rect.asScalars();
1153 bool isDstPixelAligned = true;
1154 for (int i = 0; i < 4; i++) {
1155 if (!SkScalarIsInt(scalars[i])) {
1156 isDstPixelAligned = false;
1157 break;
1158 }
1159 }
1160
1161 if (isDstPixelAligned)
1162 directDraw = true;
1163 }
1164 }
1165
1166 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167 // Convert the bitmap to a shader so that the rect can be drawn
1168 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001169 SkBitmap tmp; // subset of bitmap, if necessary
1170 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001171 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001172 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001173 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1174 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1175 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001176 // In bleed mode we position and trim the bitmap based on the src rect which is
1177 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1178 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1179 // compensate.
1180 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1181 SkIRect iSrc;
1182 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001183
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001184 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1185 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001186
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001187 if (!bitmap.extractSubset(&tmp, iSrc)) {
1188 return; // extraction failed
1189 }
1190 bitmapPtr = &tmp;
1191 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001192
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001193 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001194 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001196 } else {
1197 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001198 }
1199
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001200 SkPaint paintWithShader(paint);
1201 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001202 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001203 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1204 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001205
1206 return;
1207 }
1208
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001209 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1210 // the view matrix rather than a local matrix.
1211 SkMatrix m;
1212 m.setScale(dstSize.fWidth / srcRect.width(),
1213 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -08001214 SkMatrix viewM = *draw.fMatrix;
1215 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001216
1217 GrTextureParams params;
1218 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1219 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001220
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001221 bool doBicubic = false;
1222
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001223 switch(paintFilterLevel) {
1224 case SkPaint::kNone_FilterLevel:
1225 textureFilterMode = GrTextureParams::kNone_FilterMode;
1226 break;
1227 case SkPaint::kLow_FilterLevel:
1228 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1229 break;
1230 case SkPaint::kMedium_FilterLevel:
joshualitt5531d512014-12-17 15:50:11 -08001231 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001232 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1233 } else {
1234 // Don't trigger MIP level generation unnecessarily.
1235 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1236 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001237 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001238 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001239 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001240 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001241 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001242 break;
1243 default:
1244 SkErrorInternals::SetError( kInvalidPaint_SkError,
1245 "Sorry, I don't understand the filtering "
1246 "mode you asked for. Falling back to "
1247 "MIPMaps.");
1248 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1249 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001250 }
1251
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001252 int tileFilterPad;
1253 if (doBicubic) {
1254 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1255 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1256 tileFilterPad = 0;
1257 } else {
1258 tileFilterPad = 1;
1259 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001260 params.setFilterMode(textureFilterMode);
1261
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001262 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001263 int tileSize;
1264
1265 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001266 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001267 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001268 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1269 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001270 } else {
1271 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001272 bool needsTextureDomain = needs_texture_domain(bitmap,
1273 srcRect,
1274 params,
joshualitt5531d512014-12-17 15:50:11 -08001275 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001276 doBicubic);
1277 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001278 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001279 srcRect,
1280 params,
1281 paint,
1282 flags,
1283 doBicubic,
1284 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001285 }
1286}
1287
1288// Break 'bitmap' into several tiles to draw it since it has already
1289// been determined to be too large to fit in VRAM
1290void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001291 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001292 const SkRect& srcRect,
1293 const SkIRect& clippedSrcIRect,
1294 const GrTextureParams& params,
1295 const SkPaint& paint,
1296 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001297 int tileSize,
1298 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001299 // The following pixel lock is technically redundant, but it is desirable
1300 // to lock outside of the tile loop to prevent redecoding the whole image
1301 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1302 // is larger than the limit of the discardable memory pool.
1303 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001304 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1305
1306 int nx = bitmap.width() / tileSize;
1307 int ny = bitmap.height() / tileSize;
1308 for (int x = 0; x <= nx; x++) {
1309 for (int y = 0; y <= ny; y++) {
1310 SkRect tileR;
1311 tileR.set(SkIntToScalar(x * tileSize),
1312 SkIntToScalar(y * tileSize),
1313 SkIntToScalar((x + 1) * tileSize),
1314 SkIntToScalar((y + 1) * tileSize));
1315
1316 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1317 continue;
1318 }
1319
1320 if (!tileR.intersect(srcRect)) {
1321 continue;
1322 }
1323
1324 SkBitmap tmpB;
1325 SkIRect iTileR;
1326 tileR.roundOut(&iTileR);
1327 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1328 SkIntToScalar(iTileR.fTop));
1329
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001330 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001331 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001332 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001333 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001334 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001335
robertphillipsec8bb942014-11-21 10:16:25 -08001336 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001337 SkIRect iClampRect;
1338
1339 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1340 // In bleed mode we want to always expand the tile on all edges
1341 // but stay within the bitmap bounds
1342 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1343 } else {
1344 // In texture-domain/clamp mode we only want to expand the
1345 // tile on edges interior to "srcRect" (i.e., we want to
1346 // not bleed across the original clamped edges)
1347 srcRect.roundOut(&iClampRect);
1348 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001349 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1350 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001351 }
1352
1353 if (bitmap.extractSubset(&tmpB, iTileR)) {
1354 // now offset it to make it "local" to our tmp bitmap
1355 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001356 GrTextureParams paramsTemp = params;
1357 bool needsTextureDomain = needs_texture_domain(bitmap,
1358 srcRect,
1359 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001360 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001361 bicubic);
1362 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001363 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001364 tileR,
1365 paramsTemp,
1366 paint,
1367 flags,
1368 bicubic,
1369 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001370 }
1371 }
1372 }
1373}
1374
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001375
1376/*
1377 * This is called by drawBitmap(), which has to handle images that may be too
1378 * large to be represented by a single texture.
1379 *
1380 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1381 * and that non-texture portion of the GrPaint has already been setup.
1382 */
1383void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001384 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001385 const SkRect& srcRect,
1386 const GrTextureParams& params,
1387 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001388 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001389 bool bicubic,
1390 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001391 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1392 bitmap.height() <= fContext->getMaxTextureSize());
1393
1394 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001395 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001396 if (NULL == texture) {
1397 return;
1398 }
1399
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001400 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001401 SkRect paintRect;
1402 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1403 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1404 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1405 SkScalarMul(srcRect.fTop, hInv),
1406 SkScalarMul(srcRect.fRight, wInv),
1407 SkScalarMul(srcRect.fBottom, hInv));
1408
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001409 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001410 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001411 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001412 // Use a constrained texture domain to avoid color bleeding
1413 SkScalar left, top, right, bottom;
1414 if (srcRect.width() > SK_Scalar1) {
1415 SkScalar border = SK_ScalarHalf / texture->width();
1416 left = paintRect.left() + border;
1417 right = paintRect.right() - border;
1418 } else {
1419 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1420 }
1421 if (srcRect.height() > SK_Scalar1) {
1422 SkScalar border = SK_ScalarHalf / texture->height();
1423 top = paintRect.top() + border;
1424 bottom = paintRect.bottom() - border;
1425 } else {
1426 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1427 }
1428 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001429 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001430 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001431 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001432 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001433 SkMatrix::I(),
1434 textureDomain,
1435 GrTextureDomain::kClamp_Mode,
1436 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001437 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001438 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001439 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1440 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001441 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001442 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001443 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001444 }
1445
1446 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1447 // the rest from the SkPaint.
1448 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001449 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001450 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001451 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1452 SkColor2GrColor(paint.getColor());
joshualitt25d9c152015-02-18 12:29:52 -08001453 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001454
joshualitt29070592015-02-25 13:04:43 -08001455 fContext->drawNonAARectToRect(fRenderTarget, grPaint, viewMatrix, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001456}
1457
fmalita2d97bc12014-11-20 10:44:58 -08001458bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
1459 const SkImageFilter* filter,
1460 const SkImageFilter::Context& ctx,
1461 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001462 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001463
1464 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1465 // (https://code.google.com/p/skia/issues/detail?id=3148).
1466 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001467
1468 if (filter->canFilterImageGPU()) {
joshualitt29070592015-02-25 13:04:43 -08001469 // Set the clip wide open and the matrix to identity.
1470 GrContext::AutoWideOpenIdentityDraw awo(context);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001471 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001472 } else {
1473 return false;
1474 }
1475}
1476
1477void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1478 int left, int top, const SkPaint& paint) {
1479 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001480 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001481
1482 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1483 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1484 return;
1485 }
1486
1487 int w = bitmap.width();
1488 int h = bitmap.height();
1489
1490 GrTexture* texture;
1491 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001492 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001493
1494 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001495 // This bitmap will own the filtered result as a texture.
1496 SkBitmap filteredBitmap;
1497
bsalomon49f085d2014-09-05 13:34:00 -07001498 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001499 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001500 SkMatrix matrix(*draw.fMatrix);
1501 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001502 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001503 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001504 // This cache is transient, and is freed (along with all its contained
1505 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001506 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001507 if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap,
1508 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001509 texture = (GrTexture*) filteredBitmap.getTexture();
1510 w = filteredBitmap.width();
1511 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001512 left += offset.x();
1513 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001514 } else {
1515 return;
1516 }
1517 }
1518
1519 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001520 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001521
joshualitt25d9c152015-02-18 12:29:52 -08001522 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1523 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001524
joshualitt25d9c152015-02-18 12:29:52 -08001525 fContext->drawNonAARectToRect(fRenderTarget,
1526 grPaint,
joshualitt16b27892014-12-18 07:47:16 -08001527 SkMatrix::I(),
1528 SkRect::MakeXYWH(SkIntToScalar(left),
1529 SkIntToScalar(top),
1530 SkIntToScalar(w),
1531 SkIntToScalar(h)),
1532 SkRect::MakeXYWH(0,
1533 0,
1534 SK_Scalar1 * w / texture->width(),
1535 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001536}
1537
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001538void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001539 const SkRect* src, const SkRect& dst,
1540 const SkPaint& paint,
1541 SkCanvas::DrawBitmapRectFlags flags) {
1542 SkMatrix matrix;
1543 SkRect bitmapBounds, tmpSrc;
1544
1545 bitmapBounds.set(0, 0,
1546 SkIntToScalar(bitmap.width()),
1547 SkIntToScalar(bitmap.height()));
1548
1549 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001550 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001551 tmpSrc = *src;
1552 } else {
1553 tmpSrc = bitmapBounds;
1554 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001555
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001556 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1557
1558 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001559 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001560 if (!bitmapBounds.contains(tmpSrc)) {
1561 if (!tmpSrc.intersect(bitmapBounds)) {
1562 return; // nothing to draw
1563 }
1564 }
1565 }
1566
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001567 SkRect tmpDst;
1568 matrix.mapRect(&tmpDst, tmpSrc);
1569
1570 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1571 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1572 // Translate so that tempDst's top left is at the origin.
1573 matrix = *origDraw.fMatrix;
1574 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1575 draw.writable()->fMatrix = &matrix;
1576 }
1577 SkSize dstSize;
1578 dstSize.fWidth = tmpDst.width();
1579 dstSize.fHeight = tmpDst.height();
1580
1581 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001582}
1583
1584void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1585 int x, int y, const SkPaint& paint) {
1586 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001587 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001588 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001589
1590 // TODO: If the source device covers the whole of this device, we could
1591 // omit fNeedsClear -related flushing.
1592 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001593
1594 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001595 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001596
1597 GrRenderTarget* devRT = dev->accessRenderTarget();
1598 GrTexture* devTex;
1599 if (NULL == (devTex = devRT->asTexture())) {
1600 return;
1601 }
1602
robertphillips7b9e8a42014-12-11 08:20:31 -08001603 const SkImageInfo ii = dev->imageInfo();
1604 int w = ii.width();
1605 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001606
1607 SkImageFilter* filter = paint.getImageFilter();
1608 // This bitmap will own the filtered result as a texture.
1609 SkBitmap filteredBitmap;
1610
bsalomon49f085d2014-09-05 13:34:00 -07001611 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001612 SkIPoint offset = SkIPoint::Make(0, 0);
1613 SkMatrix matrix(*draw.fMatrix);
1614 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001615 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001616 // This cache is transient, and is freed (along with all its contained
1617 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001618 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001619 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001620 if (this->filterTexture(fContext, devTex, filter, ctx, &filteredBitmap,
1621 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001622 devTex = filteredBitmap.getTexture();
1623 w = filteredBitmap.width();
1624 h = filteredBitmap.height();
1625 x += offset.fX;
1626 y += offset.fY;
1627 } else {
1628 return;
1629 }
1630 }
1631
1632 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001633 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001634
joshualitt25d9c152015-02-18 12:29:52 -08001635 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1636 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001637
1638 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1639 SkIntToScalar(y),
1640 SkIntToScalar(w),
1641 SkIntToScalar(h));
1642
1643 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1644 // scratch texture).
1645 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1646 SK_Scalar1 * h / devTex->height());
1647
joshualitt29070592015-02-25 13:04:43 -08001648 fContext->drawNonAARectToRect(fRenderTarget, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001649}
1650
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001651bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001652 return filter->canFilterImageGPU();
1653}
1654
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001655bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001656 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001657 SkBitmap* result, SkIPoint* offset) {
1658 // want explicitly our impl, so guard against a subclass of us overriding it
1659 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1660 return false;
1661 }
1662
1663 SkAutoLockPixels alp(src, !src.getTexture());
1664 if (!src.getTexture() && !src.readyToDraw()) {
1665 return false;
1666 }
1667
1668 GrTexture* texture;
1669 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1670 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001671 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001672
fmalita2d97bc12014-11-20 10:44:58 -08001673 return this->filterTexture(fContext, texture, filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001674}
1675
1676///////////////////////////////////////////////////////////////////////////////
1677
1678// must be in SkCanvas::VertexMode order
1679static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1680 kTriangles_GrPrimitiveType,
1681 kTriangleStrip_GrPrimitiveType,
1682 kTriangleFan_GrPrimitiveType,
1683};
1684
1685void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1686 int vertexCount, const SkPoint vertices[],
1687 const SkPoint texs[], const SkColor colors[],
1688 SkXfermode* xmode,
1689 const uint16_t indices[], int indexCount,
1690 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001691 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001692 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001693
dandov32a311b2014-07-15 19:46:26 -07001694 const uint16_t* outIndices;
1695 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1696 GrPrimitiveType primType;
1697 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001698
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001699 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1700 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001701
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001702 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001703
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001704 SkPaint copy(paint);
1705 copy.setStyle(SkPaint::kStroke_Style);
1706 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001707
dandov32a311b2014-07-15 19:46:26 -07001708 // we ignore the shader if texs is null.
joshualitt25d9c152015-02-18 12:29:52 -08001709 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy,
1710 SkColor2GrColor(copy.getColor()), NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001711
dandov32a311b2014-07-15 19:46:26 -07001712 primType = kLines_GrPrimitiveType;
1713 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001714 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001715 switch (vmode) {
1716 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001717 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001718 break;
1719 case SkCanvas::kTriangleStrip_VertexMode:
1720 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001721 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001722 break;
1723 }
mtklein533eb782014-08-27 10:39:42 -07001724
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001725 VertState state(vertexCount, indices, indexCount);
1726 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001727
dandov32a311b2014-07-15 19:46:26 -07001728 //number of indices for lines per triangle with kLines
1729 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001730
dandov32a311b2014-07-15 19:46:26 -07001731 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1732 outIndices = outAlloc.get();
1733 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001734 int i = 0;
1735 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001736 auxIndices[i] = state.f0;
1737 auxIndices[i + 1] = state.f1;
1738 auxIndices[i + 2] = state.f1;
1739 auxIndices[i + 3] = state.f2;
1740 auxIndices[i + 4] = state.f2;
1741 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001742 i += 6;
1743 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001744 } else {
dandov32a311b2014-07-15 19:46:26 -07001745 outIndices = indices;
1746 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001747
dandov32a311b2014-07-15 19:46:26 -07001748 if (NULL == texs || NULL == paint.getShader()) {
joshualitt25d9c152015-02-18 12:29:52 -08001749 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1750 SkColor2GrColor(paint.getColor()),
dandov32a311b2014-07-15 19:46:26 -07001751 NULL == colors, &grPaint);
1752 } else {
joshualitt25d9c152015-02-18 12:29:52 -08001753 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix,
1754 NULL == colors, &grPaint);
dandov32a311b2014-07-15 19:46:26 -07001755 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001756 }
1757
mtklein2583b622014-06-04 08:20:41 -07001758#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001759 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001760 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1761 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001762 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001763 }
1764 }
mtklein2583b622014-06-04 08:20:41 -07001765#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001766
1767 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001768 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001769 // need to convert byte order and from non-PM to PM
1770 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001771 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001772 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001773 color = colors[i];
1774 if (paint.getAlpha() != 255) {
1775 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1776 }
1777 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001778 }
1779 colors = convertedColors.get();
1780 }
joshualitt25d9c152015-02-18 12:29:52 -08001781 fContext->drawVertices(fRenderTarget,
1782 grPaint,
joshualitt5531d512014-12-17 15:50:11 -08001783 *draw.fMatrix,
dandov32a311b2014-07-15 19:46:26 -07001784 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001785 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001786 vertices,
1787 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001788 colors,
dandov32a311b2014-07-15 19:46:26 -07001789 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001790 indexCount);
1791}
1792
1793///////////////////////////////////////////////////////////////////////////////
1794
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001795void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001796 size_t byteLength, SkScalar x, SkScalar y,
1797 const SkPaint& paint) {
1798 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001799 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001800
jvanverth8c27a182014-10-14 08:45:50 -07001801 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -08001802 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001803
jvanverth8c27a182014-10-14 08:45:50 -07001804 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001805
joshualitt29070592015-02-25 13:04:43 -08001806 if (!fTextContext->drawText(fRenderTarget, grPaint, paint, *draw.fMatrix, (const char *)text,
1807 byteLength, x, y)) {
jvanverth8c27a182014-10-14 08:45:50 -07001808 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001809 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001810 }
1811}
1812
fmalita05c4a432014-09-29 06:29:53 -07001813void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1814 const SkScalar pos[], int scalarsPerPos,
1815 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001816 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001817 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001818
jvanverth8c27a182014-10-14 08:45:50 -07001819 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -08001820 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001821
jvanverth8c27a182014-10-14 08:45:50 -07001822 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001823
joshualitt29070592015-02-25 13:04:43 -08001824 if (!fTextContext->drawPosText(fRenderTarget, grPaint, paint, *draw.fMatrix, (const char *)text,
1825 byteLength, pos, scalarsPerPos, offset)) {
jvanverth8c27a182014-10-14 08:45:50 -07001826 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001827 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001828 }
1829}
1830
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001831///////////////////////////////////////////////////////////////////////////////
1832
reedb2db8982014-11-13 12:41:02 -08001833bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001834 if (paint.getShader() ||
reedb2db8982014-11-13 12:41:02 -08001835 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001836 paint.getMaskFilter() ||
1837 paint.getRasterizer() ||
1838 paint.getColorFilter() ||
1839 paint.getPathEffect() ||
1840 paint.isFakeBoldText() ||
reedb2db8982014-11-13 12:41:02 -08001841 paint.getStyle() != SkPaint::kFill_Style)
1842 {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001843 return true;
1844 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001845 return false;
1846}
1847
1848void SkGpuDevice::flush() {
1849 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001850 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001851}
1852
1853///////////////////////////////////////////////////////////////////////////////
1854
fmalita6987dca2014-11-13 08:33:37 -08001855SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(const CreateInfo& cinfo) {
bsalomonf2703d82014-10-28 14:33:06 -07001856 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001857 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001858 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001859 desc.fWidth = cinfo.fInfo.width();
1860 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001861 desc.fSampleCnt = fRenderTarget->numSamples();
1862
1863 SkAutoTUnref<GrTexture> texture;
1864 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001865 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001866
hcm4396fa52014-10-27 21:43:30 -07001867 // layers are never draw in repeat modes, so we can request an approx
1868 // match and ignore any padding.
fmalita6987dca2014-11-13 08:33:37 -08001869 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == cinfo.fUsage) ?
hcm4396fa52014-10-27 21:43:30 -07001870 GrContext::kApprox_ScratchTexMatch :
1871 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001872 texture.reset(fContext->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001873
1874 if (texture) {
1875 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry);
1876 return SkGpuDevice::Create(texture->asRenderTarget(), &props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001877 } else {
tfarina38406c82014-10-31 07:11:12 -07001878 SkDebugf("---- failed to create compatible device texture [%d %d]\n",
fmalita6987dca2014-11-13 08:33:37 -08001879 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001880 return NULL;
1881 }
1882}
1883
reed4a8126e2014-09-22 07:29:03 -07001884SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001885 // TODO: Change the signature of newSurface to take a budgeted parameter.
1886 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
1887 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->numSamples(),
1888 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001889}
1890
robertphillips30d2cc62014-09-24 08:52:18 -07001891bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001892 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001893#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001894 // todo: should handle this natively
1895 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001896 return false;
1897 }
1898
robertphillips82365912014-11-12 09:32:34 -08001899 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips81f71b62014-11-11 04:54:49 -08001900
1901 const SkPicture::AccelData* data = mainPicture->EXPERIMENTAL_getAccelData(key);
1902 if (!data) {
1903 return false;
1904 }
1905
robertphillipse5524cd2015-02-20 12:30:26 -08001906 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1907 if (0 == gpuData->numBlocks()) {
1908 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001909 }
1910
robertphillipsfd61ed02014-10-28 07:21:44 -07001911 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001912
robertphillipse5524cd2015-02-20 12:30:26 -08001913 SkIRect iBounds;
1914 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1915 return false;
1916 }
1917
1918 SkRect clipBounds = SkRect::Make(iBounds);
1919
1920 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1921
robertphillipsfd61ed02014-10-28 07:21:44 -07001922 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001923 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001924 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001925 &atlasedNeedRendering, &atlasedRecycled,
1926 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001927
1928 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1929
1930 SkTDArray<GrHoistedLayer> needRendering, recycled;
1931
robertphillipse5524cd2015-02-20 12:30:26 -08001932 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1933
robertphillipsfd61ed02014-10-28 07:21:44 -07001934 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001935 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001936 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001937 &needRendering, &recycled,
1938 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001939
1940 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001941
robertphillips64bf7672014-08-21 13:07:35 -07001942 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001943 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1944 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001945
robertphillipsfd61ed02014-10-28 07:21:44 -07001946 GrLayerHoister::UnlockLayers(fContext, needRendering);
1947 GrLayerHoister::UnlockLayers(fContext, recycled);
1948 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1949 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001950
1951 return true;
robertphillips63242d72014-12-04 08:31:02 -08001952#else
1953 return false;
1954#endif
robertphillips64bf7672014-08-21 13:07:35 -07001955}
1956
senorblancobe129b22014-08-08 07:14:35 -07001957SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001958 // We always return a transient cache, so it is freed after each
1959 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001960 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001961}
reedf037e0b2014-10-30 11:34:15 -07001962
1963#endif