blob: 337f400056f87c9d48d7d536c6ae3d30568a4c55 [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) {
senorblancod0d37ca2015-04-02 04:54:56 -0700124 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, flags);
125}
126
127SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
128 const SkSurfaceProps* props, unsigned flags) {
bsalomonafe30052015-01-16 07:32:33 -0800129 if (!rt || rt->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000130 return NULL;
131 }
senorblancod0d37ca2015-04-02 04:54:56 -0700132 return SkNEW_ARGS(SkGpuDevice, (rt, width, height, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000133}
134
bsalomonafe30052015-01-16 07:32:33 -0800135static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps* props) {
136 if (props) {
137 return SkDeviceProperties(props->pixelGeometry());
138 } else {
139 return SkDeviceProperties(SkDeviceProperties::kLegacyLCD_InitType);
140 }
reedb2db8982014-11-13 12:41:02 -0800141}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000142
bsalomonafe30052015-01-16 07:32:33 -0800143static SkSurfaceProps copy_or_default_props(const SkSurfaceProps* props) {
144 if (props) {
145 return SkSurfaceProps(*props);
146 } else {
147 return SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
148 }
149}
150
senorblancod0d37ca2015-04-02 04:54:56 -0700151SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
152 const SkSurfaceProps* props, unsigned flags)
reedb2db8982014-11-13 12:41:02 -0800153 : INHERITED(surfaceprops_to_deviceprops(props))
bsalomonafe30052015-01-16 07:32:33 -0800154 , fSurfaceProps(copy_or_default_props(props))
reedb2db8982014-11-13 12:41:02 -0800155{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000156 fDrawProcs = NULL;
157
bsalomonafe30052015-01-16 07:32:33 -0800158 fContext = SkRef(rt->getContext());
159 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000160
bsalomonafe30052015-01-16 07:32:33 -0800161 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000162
senorblancod0d37ca2015-04-02 04:54:56 -0700163 SkImageInfo info = rt->surfacePriv().info().makeWH(width, height);
bsalomonafe30052015-01-16 07:32:33 -0800164 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt));
bsalomonafbf2d62014-09-30 12:18:44 -0700165 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700166 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700167
bsalomonafe30052015-01-16 07:32:33 -0800168 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts();
joshualitt6e8cd962015-03-20 10:30:14 -0700169 fTextContext = fContext->createTextContext(fRenderTarget, this, this->getLeakyProperties(),
170 useDFT);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000171}
172
kkinnunenabcfab42015-02-22 22:53:44 -0800173GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
174 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000175 if (kUnknown_SkColorType == origInfo.colorType() ||
176 origInfo.width() < 0 || origInfo.height() < 0) {
177 return NULL;
178 }
179
bsalomonafe30052015-01-16 07:32:33 -0800180 if (!context) {
181 return NULL;
182 }
183
reede5ea5002014-09-03 11:54:58 -0700184 SkColorType ct = origInfo.colorType();
185 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700186 if (kRGB_565_SkColorType == ct) {
187 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800188 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
189 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700190 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800191 }
192 if (kOpaque_SkAlphaType != at) {
193 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000194 }
reede5ea5002014-09-03 11:54:58 -0700195 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000196
bsalomonf2703d82014-10-28 14:33:06 -0700197 GrSurfaceDesc desc;
198 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000199 desc.fWidth = info.width();
200 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000201 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000202 desc.fSampleCnt = sampleCount;
kkinnunenabcfab42015-02-22 22:53:44 -0800203 GrTexture* texture = context->createTexture(desc, SkToBool(budgeted), NULL, 0);
204 if (NULL == texture) {
205 return NULL;
206 }
207 SkASSERT(NULL != texture->asRenderTarget());
208 return texture->asRenderTarget();
209}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000210
kkinnunenabcfab42015-02-22 22:53:44 -0800211SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
212 const SkImageInfo& info, int sampleCount,
213 const SkSurfaceProps* props, unsigned flags) {
214
215 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
216 if (NULL == rt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000217 return NULL;
218 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000219
senorblancod0d37ca2015-04-02 04:54:56 -0700220 return SkNEW_ARGS(SkGpuDevice, (rt, info.width(), info.height(), props, flags));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000221}
222
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000223SkGpuDevice::~SkGpuDevice() {
224 if (fDrawProcs) {
225 delete fDrawProcs;
226 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000227
jvanverth8c27a182014-10-14 08:45:50 -0700228 delete fTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000229
bsalomon32d0b3b2014-08-29 07:50:23 -0700230 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000231 fContext->unref();
232}
233
234///////////////////////////////////////////////////////////////////////////////
235
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000236bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
237 int x, int y) {
238 DO_DEFERRED_CLEAR();
239
240 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000241 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000242 if (kUnknown_GrPixelConfig == config) {
243 return false;
244 }
245
246 uint32_t flags = 0;
247 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
248 flags = GrContext::kUnpremul_PixelOpsFlag;
249 }
250 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
251 config, dstPixels, dstRowBytes, flags);
252}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000253
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000254bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
255 int x, int y) {
256 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000257 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000258 if (kUnknown_GrPixelConfig == config) {
259 return false;
260 }
261 uint32_t flags = 0;
262 if (kUnpremul_SkAlphaType == info.alphaType()) {
263 flags = GrContext::kUnpremul_PixelOpsFlag;
264 }
265 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
266
267 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700268 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000269
270 return true;
271}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000272
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000273const SkBitmap& SkGpuDevice::onAccessBitmap() {
274 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700275 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000276}
277
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000278void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
279 INHERITED::onAttachToCanvas(canvas);
280
281 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800282 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000283}
284
285void SkGpuDevice::onDetachFromCanvas() {
286 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800287 fClip.reset();
joshualitt44701df2015-02-23 14:44:57 -0800288 fClipStack.reset(NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000289}
290
291// call this every draw call, to ensure that the context reflects our state,
292// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800293void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800294 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000295
joshualitt44701df2015-02-23 14:44:57 -0800296 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000297
joshualitt570d2f82015-02-25 13:19:48 -0800298 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000299
300 DO_DEFERRED_CLEAR();
301}
302
303GrRenderTarget* SkGpuDevice::accessRenderTarget() {
304 DO_DEFERRED_CLEAR();
305 return fRenderTarget;
306}
307
reed8eddfb52014-12-04 07:50:14 -0800308void SkGpuDevice::clearAll() {
309 GrColor color = 0;
310 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
311 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
312 fContext->clear(&rect, color, true, fRenderTarget);
bsalomonafe30052015-01-16 07:32:33 -0800313 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800314}
315
kkinnunenabcfab42015-02-22 22:53:44 -0800316void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
317 // Caller must have accessed the render target, because it knows the rt must be replaced.
318 SkASSERT(!fNeedClear);
319
320 SkSurface::Budgeted budgeted =
321 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
322 : SkSurface::kNo_Budgeted;
323
324 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
325 fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->numSamples()));
326
327 if (NULL == newRT) {
328 return;
329 }
330
331 if (shouldRetainContent) {
332 if (fRenderTarget->wasDestroyed()) {
333 return;
334 }
335 this->context()->copySurface(newRT, fRenderTarget);
336 }
337
338 SkASSERT(fRenderTarget != newRT);
339
340 fRenderTarget->unref();
341 fRenderTarget = newRT.detach();
342
343 SkASSERT(fRenderTarget->surfacePriv().info() == fLegacyBitmap.info());
344 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (fRenderTarget->surfacePriv().info(), fRenderTarget));
345 fLegacyBitmap.setPixelRef(pr)->unref();
346}
347
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000348///////////////////////////////////////////////////////////////////////////////
349
350SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
351SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
352SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
353SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
354SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
355 shader_type_mismatch);
356SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
357 shader_type_mismatch);
358SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
359SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
360
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000361///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000362
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000363void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800364 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700365 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000366
367 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700368 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
369 return;
370 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000371
joshualitt570d2f82015-02-25 13:19:48 -0800372 fContext->drawPaint(fRenderTarget, fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000373}
374
375// must be in SkCanvas::PointMode order
376static const GrPrimitiveType gPointMode2PrimtiveType[] = {
377 kPoints_GrPrimitiveType,
378 kLines_GrPrimitiveType,
379 kLineStrip_GrPrimitiveType
380};
381
382void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
383 size_t count, const SkPoint pts[], const SkPaint& paint) {
384 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800385 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000386
387 SkScalar width = paint.getStrokeWidth();
388 if (width < 0) {
389 return;
390 }
391
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000392 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700393 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
394 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700395 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
396 &grPaint)) {
397 return;
398 }
egdaniele61c4112014-06-12 10:24:21 -0700399 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700400 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700401 path.moveTo(pts[0]);
402 path.lineTo(pts[1]);
joshualitt570d2f82015-02-25 13:19:48 -0800403 fContext->drawPath(fRenderTarget, fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700404 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000405 }
406
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000407 // we only handle hairlines and paints without path effects or mask filters,
408 // else we let the SkDraw call our drawPath()
409 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
410 draw.drawPoints(mode, count, pts, paint, true);
411 return;
412 }
413
414 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700415 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
416 return;
417 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000418
joshualitt25d9c152015-02-18 12:29:52 -0800419 fContext->drawVertices(fRenderTarget,
joshualitt570d2f82015-02-25 13:19:48 -0800420 fClip,
joshualitt25d9c152015-02-18 12:29:52 -0800421 grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800422 *draw.fMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000423 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000424 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000425 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000426 NULL,
427 NULL,
428 NULL,
429 0);
430}
431
432///////////////////////////////////////////////////////////////////////////////
433
434void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
435 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700436 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
437
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000438 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800439 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000440
441 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
442 SkScalar width = paint.getStrokeWidth();
443
444 /*
445 We have special code for hairline strokes, miter-strokes, bevel-stroke
446 and fills. Anything else we just call our path code.
447 */
448 bool usePath = doStroke && width > 0 &&
449 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
450 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
451 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700452
453 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000454 usePath = true;
455 }
egdanield58a0ba2014-06-11 10:30:05 -0700456
joshualitt5531d512014-12-17 15:50:11 -0800457 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000458#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
459 if (doStroke) {
460#endif
461 usePath = true;
462#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
463 } else {
joshualitt5531d512014-12-17 15:50:11 -0800464 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000465 }
466#endif
467 }
468 // until we can both stroke and fill rectangles
469 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
470 usePath = true;
471 }
472
egdanield58a0ba2014-06-11 10:30:05 -0700473 GrStrokeInfo strokeInfo(paint);
474
475 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700476 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700477 usePath = true;
478 }
479
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000480 if (usePath) {
481 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700482 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000483 path.addRect(rect);
484 this->drawPath(draw, path, paint, NULL, true);
485 return;
486 }
487
488 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700489 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
490 return;
491 }
Mike Klein744fb732014-06-23 15:13:26 -0400492
joshualitt570d2f82015-02-25 13:19:48 -0800493 fContext->drawRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000494}
495
496///////////////////////////////////////////////////////////////////////////////
497
498void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800499 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700500 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000501 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800502 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000503
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000504 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700505 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
506 return;
507 }
Mike Klein744fb732014-06-23 15:13:26 -0400508
egdanield58a0ba2014-06-11 10:30:05 -0700509 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000510 if (paint.getMaskFilter()) {
511 // try to hit the fast path for drawing filtered round rects
512
513 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800514 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000515 if (devRRect.allCornersCircular()) {
516 SkRect maskRect;
517 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800518 draw.fClip->getBounds(),
519 *draw.fMatrix,
520 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000521 SkIRect finalIRect;
522 maskRect.roundOut(&finalIRect);
523 if (draw.fClip->quickReject(finalIRect)) {
524 // clipped out
525 return;
526 }
joshualitt25d9c152015-02-18 12:29:52 -0800527 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
528 fRenderTarget,
529 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800530 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800531 *draw.fMatrix,
egdanield58a0ba2014-06-11 10:30:05 -0700532 strokeInfo.getStrokeRec(),
533 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000534 return;
535 }
536 }
537
538 }
539 }
540
541 }
542
egdanield58a0ba2014-06-11 10:30:05 -0700543 bool usePath = false;
544
545 if (paint.getMaskFilter()) {
546 usePath = true;
547 } else {
548 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700549 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700550 usePath = true;
551 }
552 }
553
554
555 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000556 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700557 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000558 path.addRRect(rect);
559 this->drawPath(draw, path, paint, NULL, true);
560 return;
561 }
Mike Klein744fb732014-06-23 15:13:26 -0400562
joshualitt570d2f82015-02-25 13:19:48 -0800563 fContext->drawRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000564}
565
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000566void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800567 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000568 SkStrokeRec stroke(paint);
569 if (stroke.isFillStyle()) {
570
571 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800572 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000573
574 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700575 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
576 &grPaint)) {
577 return;
578 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000579
580 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
joshualitt570d2f82015-02-25 13:19:48 -0800581 fContext->drawDRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000582 return;
583 }
584 }
585
586 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700587 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000588 path.addRRect(outer);
589 path.addRRect(inner);
590 path.setFillType(SkPath::kEvenOdd_FillType);
591
592 this->drawPath(draw, path, paint, NULL, true);
593}
594
595
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000596/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000597
598void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
599 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700600 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000601 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800602 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000603
egdanield58a0ba2014-06-11 10:30:05 -0700604 GrStrokeInfo strokeInfo(paint);
605
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000606 bool usePath = false;
607 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700608 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000609 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700610 } else {
611 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700612 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700613 usePath = true;
614 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000615 }
616
617 if (usePath) {
618 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700619 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000620 path.addOval(oval);
621 this->drawPath(draw, path, paint, NULL, true);
622 return;
623 }
624
625 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700626 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
627 return;
628 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000629
joshualitt570d2f82015-02-25 13:19:48 -0800630 fContext->drawOval(fRenderTarget, fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000631}
632
633#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000634
635///////////////////////////////////////////////////////////////////////////////
636
637// helpers for applying mask filters
638namespace {
639
640// Draw a mask using the supplied paint. Since the coverage/geometry
641// is already burnt into the mask this boils down to a rect draw.
642// Return true if the mask was successfully drawn.
joshualitt25d9c152015-02-18 12:29:52 -0800643bool draw_mask(GrContext* context,
644 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800645 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800646 const SkMatrix& viewMatrix,
647 const SkRect& maskRect,
648 GrPaint* grp,
649 GrTexture* mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000650 SkMatrix matrix;
651 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
652 matrix.postIDiv(mask->width(), mask->height());
653
joshualitt16b27892014-12-18 07:47:16 -0800654 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix,
655 kDevice_GrCoordSet))->unref();
656
657 SkMatrix inverse;
658 if (!viewMatrix.invert(&inverse)) {
659 return false;
660 }
joshualitt570d2f82015-02-25 13:19:48 -0800661 context->drawNonAARectWithLocalMatrix(rt, clip, *grp, SkMatrix::I(), maskRect, inverse);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000662 return true;
663}
664
joshualitt5efb8b82015-03-18 11:49:12 -0700665static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
666 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
667}
668
joshualitt25d9c152015-02-18 12:29:52 -0800669bool draw_with_mask_filter(GrContext* context,
670 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800671 const GrClip& clipData,
joshualitt25d9c152015-02-18 12:29:52 -0800672 const SkMatrix& viewMatrix,
673 const SkPath& devPath,
674 SkMaskFilter* filter,
joshualitt5efb8b82015-03-18 11:49:12 -0700675 const SkIRect& clipBounds,
joshualitt25d9c152015-02-18 12:29:52 -0800676 GrPaint* grp,
677 SkPaint::Style style) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000678 SkMask srcM, dstM;
679
joshualitt5efb8b82015-03-18 11:49:12 -0700680 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000681 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
682 return false;
683 }
684 SkAutoMaskFreeImage autoSrc(srcM.fImage);
685
joshualitt5531d512014-12-17 15:50:11 -0800686 if (!filter->filterMask(&dstM, srcM, viewMatrix, NULL)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000687 return false;
688 }
689 // this will free-up dstM when we're done (allocated in filterMask())
690 SkAutoMaskFreeImage autoDst(dstM.fImage);
691
joshualitt5efb8b82015-03-18 11:49:12 -0700692 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000693 return false;
694 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000695
696 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
697 // the current clip (and identity matrix) and GrPaint settings
bsalomonf2703d82014-10-28 14:33:06 -0700698 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000699 desc.fWidth = dstM.fBounds.width();
700 desc.fHeight = dstM.fBounds.height();
701 desc.fConfig = kAlpha_8_GrPixelConfig;
702
bsalomone3059732014-10-14 11:47:22 -0700703 SkAutoTUnref<GrTexture> texture(
704 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
705 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000706 return false;
707 }
708 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
709 dstM.fImage, dstM.fRowBytes);
710
711 SkRect maskRect = SkRect::Make(dstM.fBounds);
712
joshualitt570d2f82015-02-25 13:19:48 -0800713 return draw_mask(context, rt, clipData, viewMatrix, maskRect, grp, texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000714}
715
bsalomone3059732014-10-14 11:47:22 -0700716// Create a mask of 'devPath' and place the result in 'mask'.
717GrTexture* create_mask_GPU(GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800718 GrRenderTarget* rt,
bsalomone3059732014-10-14 11:47:22 -0700719 const SkRect& maskRect,
720 const SkPath& devPath,
721 const GrStrokeInfo& strokeInfo,
722 bool doAA,
723 int sampleCnt) {
bsalomonf2703d82014-10-28 14:33:06 -0700724 GrSurfaceDesc desc;
725 desc.fFlags = kRenderTarget_GrSurfaceFlag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000726 desc.fWidth = SkScalarCeilToInt(maskRect.width());
727 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700728 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000729 // We actually only need A8, but it often isn't supported as a
730 // render target so default to RGBA_8888
731 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700732
733 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
734 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000735 desc.fConfig = kAlpha_8_GrPixelConfig;
736 }
737
bsalomone3059732014-10-14 11:47:22 -0700738 GrTexture* mask = context->refScratchTexture(desc,GrContext::kApprox_ScratchTexMatch);
739 if (NULL == mask) {
740 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000741 }
742
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000743 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
744
bsalomon89c62982014-11-03 12:08:42 -0800745 context->clear(NULL, 0x0, true, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000746
747 GrPaint tempPaint;
egdanielb197b8f2015-02-17 07:34:43 -0800748 tempPaint.setAntiAlias(doAA);
749 tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000750
joshualitt570d2f82015-02-25 13:19:48 -0800751 // setup new clip
752 GrClip clip(clipRect);
753
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000754 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
755 SkMatrix translate;
756 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
joshualitt570d2f82015-02-25 13:19:48 -0800757 context->drawPath(mask->asRenderTarget(), clip, tempPaint, translate, devPath, strokeInfo);
bsalomone3059732014-10-14 11:47:22 -0700758 return mask;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000759}
760
senorblancod0d37ca2015-04-02 04:54:56 -0700761SkBitmap wrap_texture(GrTexture* texture, int width, int height) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000762 SkBitmap result;
senorblancod0d37ca2015-04-02 04:54:56 -0700763 result.setInfo(SkImageInfo::MakeN32Premul(width, height));
reed6c225732014-06-09 19:52:07 -0700764 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000765 return result;
766}
767
768};
769
770void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
771 const SkPaint& paint, const SkMatrix* prePathMatrix,
772 bool pathIsMutable) {
773 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800774 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700775 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000776
joshualitt5efb8b82015-03-18 11:49:12 -0700777 return this->internalDrawPath(origSrcPath, paint, *draw.fMatrix, prePathMatrix,
778 draw.fClip->getBounds(), pathIsMutable);
779}
780
781void SkGpuDevice::internalDrawPath(const SkPath& origSrcPath, const SkPaint& paint,
782 const SkMatrix& origViewMatrix, const SkMatrix* prePathMatrix,
783 const SkIRect& clipBounds, bool pathIsMutable) {
jvanverthb3eb6872014-10-24 07:12:51 -0700784 SkASSERT(!pathIsMutable || origSrcPath.isVolatile());
joshualitt5531d512014-12-17 15:50:11 -0800785
bsalomon54443932015-01-29 09:34:18 -0800786 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000787
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000788 // If we have a prematrix, apply it to the path, optimizing for the case
789 // where the original path can in fact be modified in place (even though
790 // its parameter type is const).
791 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000792 SkTLazy<SkPath> tmpPath;
793 SkTLazy<SkPath> effectPath;
bsalomon54443932015-01-29 09:34:18 -0800794 SkPathEffect* pathEffect = paint.getPathEffect();
795
joshualitt5efb8b82015-03-18 11:49:12 -0700796 SkMatrix viewMatrix = origViewMatrix;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000797
798 if (prePathMatrix) {
bsalomon54443932015-01-29 09:34:18 -0800799 // stroking and path effects are supposed to be applied *after* the prePathMatrix.
800 // The pre-path-matrix also should not affect shadeing.
801 if (NULL == pathEffect && NULL == paint.getShader() &&
802 (strokeInfo.getStrokeRec().isFillStyle() ||
803 strokeInfo.getStrokeRec().isHairlineStyle())) {
804 viewMatrix.preConcat(*prePathMatrix);
805 } else {
806 SkPath* result = pathPtr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000807
bsalomon54443932015-01-29 09:34:18 -0800808 if (!pathIsMutable) {
809 result = tmpPath.init();
810 result->setIsVolatile(true);
811 pathIsMutable = true;
812 }
813 // should I push prePathMatrix on our MV stack temporarily, instead
814 // of applying it here? See SkDraw.cpp
815 pathPtr->transform(*prePathMatrix, result);
816 pathPtr = result;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000817 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000818 }
819 // at this point we're done with prePathMatrix
820 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
821
bsalomon54443932015-01-29 09:34:18 -0800822 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700823 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, viewMatrix, true, &grPaint)) {
824 return;
825 }
bsalomon54443932015-01-29 09:34:18 -0800826
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000827 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700828 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
829 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000830 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000831 pathPtr = effectPath.get();
832 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700833 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000834 }
835
egdanield58a0ba2014-06-11 10:30:05 -0700836 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000837 if (paint.getMaskFilter()) {
838 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000839 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
840 if (stroke.applyToPath(strokedPath, *pathPtr)) {
841 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000842 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700843 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000844 }
845 }
846
847 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000848 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700849 if (!pathIsMutable) {
850 devPathPtr->setIsVolatile(true);
851 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000852
853 // transform the path into device space
bsalomon54443932015-01-29 09:34:18 -0800854 pathPtr->transform(viewMatrix, devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000855
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000856 SkRect maskRect;
857 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
joshualitt5efb8b82015-03-18 11:49:12 -0700858 clipBounds,
bsalomon54443932015-01-29 09:34:18 -0800859 viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000860 &maskRect)) {
861 SkIRect finalIRect;
862 maskRect.roundOut(&finalIRect);
joshualitt5efb8b82015-03-18 11:49:12 -0700863 if (clip_bounds_quick_reject(clipBounds, finalIRect)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000864 // clipped out
865 return;
866 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000867
joshualitt25d9c152015-02-18 12:29:52 -0800868 if (paint.getMaskFilter()->directFilterMaskGPU(fContext,
869 fRenderTarget,
870 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800871 fClip,
joshualitt25d9c152015-02-18 12:29:52 -0800872 viewMatrix,
873 stroke,
874 *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000875 // the mask filter was able to draw itself directly, so there's nothing
876 // left to do.
877 return;
878 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000879
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000880
joshualitt25d9c152015-02-18 12:29:52 -0800881 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext,
882 fRenderTarget,
883 maskRect,
884 *devPathPtr,
885 strokeInfo,
886 grPaint.isAntiAlias(),
bsalomone3059732014-10-14 11:47:22 -0700887 fRenderTarget->numSamples()));
888 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000889 GrTexture* filtered;
890
bsalomon54443932015-01-29 09:34:18 -0800891 if (paint.getMaskFilter()->filterMaskGPU(mask, viewMatrix, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000892 // filterMaskGPU gives us ownership of a ref to the result
893 SkAutoTUnref<GrTexture> atu(filtered);
joshualitt570d2f82015-02-25 13:19:48 -0800894 if (draw_mask(fContext,
895 fRenderTarget,
896 fClip,
897 viewMatrix,
898 maskRect,
899 &grPaint,
joshualitt25d9c152015-02-18 12:29:52 -0800900 filtered)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000901 // This path is completely drawn
902 return;
903 }
904 }
905 }
906 }
907
908 // draw the mask on the CPU - this is a fallthrough path in case the
909 // GPU path fails
910 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
911 SkPaint::kFill_Style;
joshualitt570d2f82015-02-25 13:19:48 -0800912 draw_with_mask_filter(fContext, fRenderTarget, fClip, viewMatrix, *devPathPtr,
joshualitt5efb8b82015-03-18 11:49:12 -0700913 paint.getMaskFilter(), clipBounds, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000914 return;
915 }
916
joshualitt570d2f82015-02-25 13:19:48 -0800917 fContext->drawPath(fRenderTarget, fClip, grPaint, viewMatrix, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000918}
919
920static const int kBmpSmallTileSize = 1 << 10;
921
922static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
923 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
924 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
925 return tilesX * tilesY;
926}
927
928static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
929 if (maxTileSize <= kBmpSmallTileSize) {
930 return maxTileSize;
931 }
932
933 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
934 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
935
936 maxTileTotalTileSize *= maxTileSize * maxTileSize;
937 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
938
939 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
940 return kBmpSmallTileSize;
941 } else {
942 return maxTileSize;
943 }
944}
945
946// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
947// pixels from the bitmap are necessary.
948static void determine_clipped_src_rect(const GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800949 const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800950 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800951 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000952 const SkBitmap& bitmap,
953 const SkRect* srcRectPtr,
954 SkIRect* clippedSrcIRect) {
joshualitt570d2f82015-02-25 13:19:48 -0800955 clip.getConservativeBounds(rt, clippedSrcIRect, NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000956 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800957 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958 clippedSrcIRect->setEmpty();
959 return;
960 }
961 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
962 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700963 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000964 // we've setup src space 0,0 to map to the top left of the src rect.
965 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000966 if (!clippedSrcRect.intersect(*srcRectPtr)) {
967 clippedSrcIRect->setEmpty();
968 return;
969 }
970 }
971 clippedSrcRect.roundOut(clippedSrcIRect);
972 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
973 if (!clippedSrcIRect->intersect(bmpBounds)) {
974 clippedSrcIRect->setEmpty();
975 }
976}
977
978bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800979 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000980 const GrTextureParams& params,
981 const SkRect* srcRectPtr,
982 int maxTileSize,
983 int* tileSize,
984 SkIRect* clippedSrcRect) const {
985 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700986 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000987 return false;
988 }
989
990 // if it's larger than the max tile size, then we have no choice but tiling.
991 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt570d2f82015-02-25 13:19:48 -0800992 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap,
993 srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000994 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
995 return true;
996 }
997
998 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
999 return false;
1000 }
1001
1002 // if the entire texture is already in our cache then no reason to tile it
1003 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
1004 return false;
1005 }
1006
1007 // At this point we know we could do the draw by uploading the entire bitmap
1008 // as a texture. However, if the texture would be large compared to the
1009 // cache size and we don't require most of it for this draw then tile to
1010 // reduce the amount of upload and cache spill.
1011
1012 // assumption here is that sw bitmap size is a good proxy for its size as
1013 // a texture
1014 size_t bmpSize = bitmap.getSize();
1015 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001016 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001017 if (bmpSize < cacheSize / 2) {
1018 return false;
1019 }
1020
1021 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt570d2f82015-02-25 13:19:48 -08001022 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap, srcRectPtr,
joshualitt25d9c152015-02-18 12:29:52 -08001023 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001024 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1025 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
1026 kBmpSmallTileSize * kBmpSmallTileSize;
1027
1028 return usedTileBytes < 2 * bmpSize;
1029}
1030
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001031void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001032 const SkBitmap& bitmap,
1033 const SkMatrix& m,
1034 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001035 SkMatrix concat;
1036 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1037 if (!m.isIdentity()) {
1038 concat.setConcat(*draw->fMatrix, m);
1039 draw.writable()->fMatrix = &concat;
1040 }
1041 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001042}
1043
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001044// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1046// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001047static inline void clamped_outset_with_offset(SkIRect* iRect,
1048 int outset,
1049 SkPoint* offset,
1050 const SkIRect& clamp) {
1051 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001052
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001053 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1054 if (leftClampDelta > 0) {
1055 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001056 iRect->fLeft = clamp.fLeft;
1057 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001058 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001059 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001060
1061 int topClampDelta = clamp.fTop - iRect->fTop;
1062 if (topClampDelta > 0) {
1063 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001064 iRect->fTop = clamp.fTop;
1065 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001066 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001067 }
1068
1069 if (iRect->fRight > clamp.fRight) {
1070 iRect->fRight = clamp.fRight;
1071 }
1072 if (iRect->fBottom > clamp.fBottom) {
1073 iRect->fBottom = clamp.fBottom;
1074 }
1075}
1076
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001077static bool has_aligned_samples(const SkRect& srcRect,
1078 const SkRect& transformedRect) {
1079 // detect pixel disalignment
1080 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1081 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1082 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1083 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1084 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1085 COLOR_BLEED_TOLERANCE &&
1086 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1087 COLOR_BLEED_TOLERANCE) {
1088 return true;
1089 }
1090 return false;
1091}
1092
1093static bool may_color_bleed(const SkRect& srcRect,
1094 const SkRect& transformedRect,
1095 const SkMatrix& m) {
1096 // Only gets called if has_aligned_samples returned false.
1097 // So we can assume that sampling is axis aligned but not texel aligned.
1098 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1099 SkRect innerSrcRect(srcRect), innerTransformedRect,
1100 outerTransformedRect(transformedRect);
1101 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1102 m.mapRect(&innerTransformedRect, innerSrcRect);
1103
1104 // The gap between outerTransformedRect and innerTransformedRect
1105 // represents the projection of the source border area, which is
1106 // problematic for color bleeding. We must check whether any
1107 // destination pixels sample the border area.
1108 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1109 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1110 SkIRect outer, inner;
1111 outerTransformedRect.round(&outer);
1112 innerTransformedRect.round(&inner);
1113 // If the inner and outer rects round to the same result, it means the
1114 // border does not overlap any pixel centers. Yay!
1115 return inner != outer;
1116}
1117
1118static bool needs_texture_domain(const SkBitmap& bitmap,
1119 const SkRect& srcRect,
1120 GrTextureParams &params,
1121 const SkMatrix& contextMatrix,
1122 bool bicubic) {
1123 bool needsTextureDomain = false;
senorblancod0d37ca2015-04-02 04:54:56 -07001124 GrTexture* tex = bitmap.getTexture();
1125 int width = tex ? tex->width() : bitmap.width();
1126 int height = tex ? tex->height() : bitmap.height();
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001127
1128 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1129 // Need texture domain if drawing a sub rect
senorblancod0d37ca2015-04-02 04:54:56 -07001130 needsTextureDomain = srcRect.width() < width ||
1131 srcRect.height() < height;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001132 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1133 // sampling is axis-aligned
1134 SkRect transformedRect;
1135 contextMatrix.mapRect(&transformedRect, srcRect);
1136
1137 if (has_aligned_samples(srcRect, transformedRect)) {
1138 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1139 needsTextureDomain = false;
1140 } else {
1141 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1142 }
1143 }
1144 }
1145 return needsTextureDomain;
1146}
1147
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001148void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1149 const SkBitmap& bitmap,
1150 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001151 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001152 const SkPaint& paint,
1153 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -08001154 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001155
1156 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001157 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001158 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1159 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001160 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001161 SkScalar w = SkIntToScalar(bitmap.width());
1162 SkScalar h = SkIntToScalar(bitmap.height());
1163 dstSize.fWidth = w;
1164 dstSize.fHeight = h;
1165 srcRect.set(0, 0, w, h);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001166 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001167 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001168 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001169 dstSize = *dstSizePtr;
senorblancod0d37ca2015-04-02 04:54:56 -07001170 }
1171 GrTexture* tex = bitmap.getTexture();
1172 int width = tex ? tex->width() : bitmap.width();
1173 int height = tex ? tex->height() : bitmap.height();
1174 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1175 srcRect.fRight >= width && srcRect.fBottom >= height) {
1176 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001177 }
1178
derekf367e1862014-12-02 11:02:06 -08001179 // If the render target is not msaa and draw is antialiased, we call
1180 // drawRect instead of drawing on the render target directly.
1181 // FIXME: the tiled bitmap code path doesn't currently support
1182 // anti-aliased edges, we work around that for now by drawing directly
1183 // if the image size exceeds maximum texture size.
1184 int maxTextureSize = fContext->getMaxTextureSize();
1185 bool directDraw = fRenderTarget->isMultisampled() ||
1186 !paint.isAntiAlias() ||
1187 bitmap.width() > maxTextureSize ||
1188 bitmap.height() > maxTextureSize;
1189
1190 // we check whether dst rect are pixel aligned
1191 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -08001192 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -08001193
1194 if (staysRect) {
1195 SkRect rect;
1196 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -08001197 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -08001198 const SkScalar *scalars = rect.asScalars();
1199 bool isDstPixelAligned = true;
1200 for (int i = 0; i < 4; i++) {
1201 if (!SkScalarIsInt(scalars[i])) {
1202 isDstPixelAligned = false;
1203 break;
1204 }
1205 }
1206
1207 if (isDstPixelAligned)
1208 directDraw = true;
1209 }
1210 }
1211
1212 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001213 // Convert the bitmap to a shader so that the rect can be drawn
1214 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001215 SkBitmap tmp; // subset of bitmap, if necessary
1216 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001217 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001218 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001219 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1220 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1221 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001222 // In bleed mode we position and trim the bitmap based on the src rect which is
1223 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1224 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1225 // compensate.
1226 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1227 SkIRect iSrc;
1228 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001229
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001230 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1231 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001232
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001233 if (!bitmap.extractSubset(&tmp, iSrc)) {
1234 return; // extraction failed
1235 }
1236 bitmapPtr = &tmp;
1237 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001238
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001239 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001240 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001241 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001242 } else {
1243 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001244 }
1245
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001246 SkPaint paintWithShader(paint);
1247 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001248 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001249 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1250 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001251
1252 return;
1253 }
1254
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001255 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1256 // the view matrix rather than a local matrix.
1257 SkMatrix m;
1258 m.setScale(dstSize.fWidth / srcRect.width(),
1259 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -08001260 SkMatrix viewM = *draw.fMatrix;
1261 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001262
1263 GrTextureParams params;
reed93a12152015-03-16 10:08:34 -07001264 SkFilterQuality paintFilterQuality = paint.getFilterQuality();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001265 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001266
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001267 bool doBicubic = false;
1268
reed93a12152015-03-16 10:08:34 -07001269 switch(paintFilterQuality) {
1270 case kNone_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001271 textureFilterMode = GrTextureParams::kNone_FilterMode;
1272 break;
reed93a12152015-03-16 10:08:34 -07001273 case kLow_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001274 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1275 break;
reed93a12152015-03-16 10:08:34 -07001276 case kMedium_SkFilterQuality:
joshualitt5531d512014-12-17 15:50:11 -08001277 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001278 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1279 } else {
1280 // Don't trigger MIP level generation unnecessarily.
1281 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1282 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001283 break;
reed93a12152015-03-16 10:08:34 -07001284 case kHigh_SkFilterQuality:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001285 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001286 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001287 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001288 break;
1289 default:
1290 SkErrorInternals::SetError( kInvalidPaint_SkError,
1291 "Sorry, I don't understand the filtering "
1292 "mode you asked for. Falling back to "
1293 "MIPMaps.");
1294 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1295 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001296 }
1297
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001298 int tileFilterPad;
1299 if (doBicubic) {
1300 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1301 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1302 tileFilterPad = 0;
1303 } else {
1304 tileFilterPad = 1;
1305 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001306 params.setFilterMode(textureFilterMode);
1307
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001308 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001309 int tileSize;
1310
1311 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001312 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001313 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001314 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1315 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001316 } else {
1317 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001318 bool needsTextureDomain = needs_texture_domain(bitmap,
1319 srcRect,
1320 params,
joshualitt5531d512014-12-17 15:50:11 -08001321 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001322 doBicubic);
1323 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001324 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001325 srcRect,
1326 params,
1327 paint,
1328 flags,
1329 doBicubic,
1330 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001331 }
1332}
1333
1334// Break 'bitmap' into several tiles to draw it since it has already
1335// been determined to be too large to fit in VRAM
1336void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001337 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001338 const SkRect& srcRect,
1339 const SkIRect& clippedSrcIRect,
1340 const GrTextureParams& params,
1341 const SkPaint& paint,
1342 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001343 int tileSize,
1344 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001345 // The following pixel lock is technically redundant, but it is desirable
1346 // to lock outside of the tile loop to prevent redecoding the whole image
1347 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1348 // is larger than the limit of the discardable memory pool.
1349 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001350 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1351
1352 int nx = bitmap.width() / tileSize;
1353 int ny = bitmap.height() / tileSize;
1354 for (int x = 0; x <= nx; x++) {
1355 for (int y = 0; y <= ny; y++) {
1356 SkRect tileR;
1357 tileR.set(SkIntToScalar(x * tileSize),
1358 SkIntToScalar(y * tileSize),
1359 SkIntToScalar((x + 1) * tileSize),
1360 SkIntToScalar((y + 1) * tileSize));
1361
1362 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1363 continue;
1364 }
1365
1366 if (!tileR.intersect(srcRect)) {
1367 continue;
1368 }
1369
1370 SkBitmap tmpB;
1371 SkIRect iTileR;
1372 tileR.roundOut(&iTileR);
1373 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1374 SkIntToScalar(iTileR.fTop));
1375
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001376 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001377 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001378 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001379 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001380 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001381
robertphillipsec8bb942014-11-21 10:16:25 -08001382 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001383 SkIRect iClampRect;
1384
1385 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1386 // In bleed mode we want to always expand the tile on all edges
1387 // but stay within the bitmap bounds
1388 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1389 } else {
1390 // In texture-domain/clamp mode we only want to expand the
1391 // tile on edges interior to "srcRect" (i.e., we want to
1392 // not bleed across the original clamped edges)
1393 srcRect.roundOut(&iClampRect);
1394 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001395 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1396 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001397 }
1398
1399 if (bitmap.extractSubset(&tmpB, iTileR)) {
1400 // now offset it to make it "local" to our tmp bitmap
1401 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001402 GrTextureParams paramsTemp = params;
1403 bool needsTextureDomain = needs_texture_domain(bitmap,
1404 srcRect,
1405 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001406 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001407 bicubic);
1408 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001409 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001410 tileR,
1411 paramsTemp,
1412 paint,
1413 flags,
1414 bicubic,
1415 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001416 }
1417 }
1418 }
1419}
1420
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001421
1422/*
1423 * This is called by drawBitmap(), which has to handle images that may be too
1424 * large to be represented by a single texture.
1425 *
1426 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1427 * and that non-texture portion of the GrPaint has already been setup.
1428 */
1429void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001430 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001431 const SkRect& srcRect,
1432 const GrTextureParams& params,
1433 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001434 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001435 bool bicubic,
1436 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001437 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1438 bitmap.height() <= fContext->getMaxTextureSize());
1439
1440 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001441 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001442 if (NULL == texture) {
1443 return;
1444 }
1445
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001446 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001447 SkRect paintRect;
1448 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1449 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1450 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1451 SkScalarMul(srcRect.fTop, hInv),
1452 SkScalarMul(srcRect.fRight, wInv),
1453 SkScalarMul(srcRect.fBottom, hInv));
1454
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001455 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001456 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001457 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001458 // Use a constrained texture domain to avoid color bleeding
1459 SkScalar left, top, right, bottom;
1460 if (srcRect.width() > SK_Scalar1) {
1461 SkScalar border = SK_ScalarHalf / texture->width();
1462 left = paintRect.left() + border;
1463 right = paintRect.right() - border;
1464 } else {
1465 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1466 }
1467 if (srcRect.height() > SK_Scalar1) {
1468 SkScalar border = SK_ScalarHalf / texture->height();
1469 top = paintRect.top() + border;
1470 bottom = paintRect.bottom() - border;
1471 } else {
1472 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1473 }
1474 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001475 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001476 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001477 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001478 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001479 SkMatrix::I(),
1480 textureDomain,
1481 GrTextureDomain::kClamp_Mode,
1482 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001483 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001484 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001485 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1486 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001487 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001488 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001489 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001490 }
1491
1492 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1493 // the rest from the SkPaint.
1494 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001495 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001496 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001497 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1498 SkColor2GrColor(paint.getColor());
bsalomonbed83a62015-04-15 14:18:34 -07001499 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false,
1500 &grPaint)) {
1501 return;
1502 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001503
joshualitt570d2f82015-02-25 13:19:48 -08001504 fContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dstRect,
1505 paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001506}
1507
fmalita2d97bc12014-11-20 10:44:58 -08001508bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001509 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001510 const SkImageFilter* filter,
1511 const SkImageFilter::Context& ctx,
1512 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001513 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001514
1515 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1516 // (https://code.google.com/p/skia/issues/detail?id=3148).
1517 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001518
1519 if (filter->canFilterImageGPU()) {
senorblancod0d37ca2015-04-02 04:54:56 -07001520 return filter->filterImageGPU(&proxy, wrap_texture(texture, width, height),
1521 ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001522 } else {
1523 return false;
1524 }
1525}
1526
1527void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1528 int left, int top, const SkPaint& paint) {
1529 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001530 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001531
1532 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1533 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1534 return;
1535 }
1536
1537 int w = bitmap.width();
1538 int h = bitmap.height();
1539
1540 GrTexture* texture;
1541 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001542 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001543 if (!texture) {
1544 return;
1545 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001546
1547 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001548 // This bitmap will own the filtered result as a texture.
1549 SkBitmap filteredBitmap;
1550
bsalomon49f085d2014-09-05 13:34:00 -07001551 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001552 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001553 SkMatrix matrix(*draw.fMatrix);
1554 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001555 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001556 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001557 // This cache is transient, and is freed (along with all its contained
1558 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001559 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001560 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001561 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001562 texture = (GrTexture*) filteredBitmap.getTexture();
1563 w = filteredBitmap.width();
1564 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001565 left += offset.x();
1566 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001567 } else {
1568 return;
1569 }
1570 }
1571
1572 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001573 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001574
bsalomonbed83a62015-04-15 14:18:34 -07001575 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1576 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1577 return;
1578 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001579
joshualitt25d9c152015-02-18 12:29:52 -08001580 fContext->drawNonAARectToRect(fRenderTarget,
joshualitt570d2f82015-02-25 13:19:48 -08001581 fClip,
joshualitt25d9c152015-02-18 12:29:52 -08001582 grPaint,
joshualitt16b27892014-12-18 07:47:16 -08001583 SkMatrix::I(),
1584 SkRect::MakeXYWH(SkIntToScalar(left),
1585 SkIntToScalar(top),
1586 SkIntToScalar(w),
1587 SkIntToScalar(h)),
1588 SkRect::MakeXYWH(0,
1589 0,
1590 SK_Scalar1 * w / texture->width(),
1591 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001592}
1593
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001594void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001595 const SkRect* src, const SkRect& dst,
1596 const SkPaint& paint,
1597 SkCanvas::DrawBitmapRectFlags flags) {
1598 SkMatrix matrix;
1599 SkRect bitmapBounds, tmpSrc;
1600
1601 bitmapBounds.set(0, 0,
1602 SkIntToScalar(bitmap.width()),
1603 SkIntToScalar(bitmap.height()));
1604
1605 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001606 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001607 tmpSrc = *src;
1608 } else {
1609 tmpSrc = bitmapBounds;
1610 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001611
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001612 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1613
1614 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001615 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001616 if (!bitmapBounds.contains(tmpSrc)) {
1617 if (!tmpSrc.intersect(bitmapBounds)) {
1618 return; // nothing to draw
1619 }
1620 }
1621 }
1622
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001623 SkRect tmpDst;
1624 matrix.mapRect(&tmpDst, tmpSrc);
1625
1626 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1627 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1628 // Translate so that tempDst's top left is at the origin.
1629 matrix = *origDraw.fMatrix;
1630 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1631 draw.writable()->fMatrix = &matrix;
1632 }
1633 SkSize dstSize;
1634 dstSize.fWidth = tmpDst.width();
1635 dstSize.fHeight = tmpDst.height();
1636
1637 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001638}
1639
1640void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1641 int x, int y, const SkPaint& paint) {
1642 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001643 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001644 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001645
1646 // TODO: If the source device covers the whole of this device, we could
1647 // omit fNeedsClear -related flushing.
1648 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001649
1650 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001651 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001652
1653 GrRenderTarget* devRT = dev->accessRenderTarget();
1654 GrTexture* devTex;
1655 if (NULL == (devTex = devRT->asTexture())) {
1656 return;
1657 }
1658
robertphillips7b9e8a42014-12-11 08:20:31 -08001659 const SkImageInfo ii = dev->imageInfo();
1660 int w = ii.width();
1661 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001662
1663 SkImageFilter* filter = paint.getImageFilter();
1664 // This bitmap will own the filtered result as a texture.
1665 SkBitmap filteredBitmap;
1666
bsalomon49f085d2014-09-05 13:34:00 -07001667 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001668 SkIPoint offset = SkIPoint::Make(0, 0);
1669 SkMatrix matrix(*draw.fMatrix);
1670 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001671 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001672 // This cache is transient, and is freed (along with all its contained
1673 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001674 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001675 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001676 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1677 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001678 devTex = filteredBitmap.getTexture();
1679 w = filteredBitmap.width();
1680 h = filteredBitmap.height();
1681 x += offset.fX;
1682 y += offset.fY;
1683 } else {
1684 return;
1685 }
1686 }
1687
1688 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001689 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001690
bsalomonbed83a62015-04-15 14:18:34 -07001691 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1692 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1693 return;
1694 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001695
1696 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1697 SkIntToScalar(y),
1698 SkIntToScalar(w),
1699 SkIntToScalar(h));
1700
1701 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1702 // scratch texture).
1703 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1704 SK_Scalar1 * h / devTex->height());
1705
joshualitt570d2f82015-02-25 13:19:48 -08001706 fContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, SkMatrix::I(), dstRect,
1707 srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001708}
1709
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001710bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001711 return filter->canFilterImageGPU();
1712}
1713
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001714bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001715 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001716 SkBitmap* result, SkIPoint* offset) {
1717 // want explicitly our impl, so guard against a subclass of us overriding it
1718 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1719 return false;
1720 }
1721
1722 SkAutoLockPixels alp(src, !src.getTexture());
1723 if (!src.getTexture() && !src.readyToDraw()) {
1724 return false;
1725 }
1726
1727 GrTexture* texture;
1728 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1729 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001730 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001731
senorblancod0d37ca2015-04-02 04:54:56 -07001732 return this->filterTexture(fContext, texture, src.width(), src.height(),
1733 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001734}
1735
1736///////////////////////////////////////////////////////////////////////////////
1737
1738// must be in SkCanvas::VertexMode order
1739static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1740 kTriangles_GrPrimitiveType,
1741 kTriangleStrip_GrPrimitiveType,
1742 kTriangleFan_GrPrimitiveType,
1743};
1744
1745void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1746 int vertexCount, const SkPoint vertices[],
1747 const SkPoint texs[], const SkColor colors[],
1748 SkXfermode* xmode,
1749 const uint16_t indices[], int indexCount,
1750 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001751 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001752 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001753
dandov32a311b2014-07-15 19:46:26 -07001754 const uint16_t* outIndices;
1755 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1756 GrPrimitiveType primType;
1757 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001758
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001759 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1760 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001761
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001762 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001763
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001764 SkPaint copy(paint);
1765 copy.setStyle(SkPaint::kStroke_Style);
1766 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001767
dandov32a311b2014-07-15 19:46:26 -07001768 // we ignore the shader if texs is null.
bsalomonbed83a62015-04-15 14:18:34 -07001769 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy,
1770 SkColor2GrColor(copy.getColor()), NULL == colors, &grPaint)) {
1771 return;
1772 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001773
dandov32a311b2014-07-15 19:46:26 -07001774 primType = kLines_GrPrimitiveType;
1775 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001776 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001777 switch (vmode) {
1778 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001779 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001780 break;
1781 case SkCanvas::kTriangleStrip_VertexMode:
1782 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001783 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001784 break;
1785 }
mtklein533eb782014-08-27 10:39:42 -07001786
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001787 VertState state(vertexCount, indices, indexCount);
1788 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001789
dandov32a311b2014-07-15 19:46:26 -07001790 //number of indices for lines per triangle with kLines
1791 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001792
dandov32a311b2014-07-15 19:46:26 -07001793 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1794 outIndices = outAlloc.get();
1795 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001796 int i = 0;
1797 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001798 auxIndices[i] = state.f0;
1799 auxIndices[i + 1] = state.f1;
1800 auxIndices[i + 2] = state.f1;
1801 auxIndices[i + 3] = state.f2;
1802 auxIndices[i + 4] = state.f2;
1803 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001804 i += 6;
1805 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001806 } else {
dandov32a311b2014-07-15 19:46:26 -07001807 outIndices = indices;
1808 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001809
dandov32a311b2014-07-15 19:46:26 -07001810 if (NULL == texs || NULL == paint.getShader()) {
bsalomonbed83a62015-04-15 14:18:34 -07001811 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1812 SkColor2GrColor(paint.getColor()),
1813 NULL == colors, &grPaint)) {
1814 return;
1815 }
dandov32a311b2014-07-15 19:46:26 -07001816 } else {
bsalomonbed83a62015-04-15 14:18:34 -07001817 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix,
1818 NULL == colors, &grPaint)) {
1819 return;
1820 }
dandov32a311b2014-07-15 19:46:26 -07001821 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001822 }
1823
mtklein2583b622014-06-04 08:20:41 -07001824#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001825 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001826 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1827 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001828 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001829 }
1830 }
mtklein2583b622014-06-04 08:20:41 -07001831#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001832
1833 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001834 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001835 // need to convert byte order and from non-PM to PM
1836 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001837 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001838 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001839 color = colors[i];
1840 if (paint.getAlpha() != 255) {
1841 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1842 }
1843 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001844 }
1845 colors = convertedColors.get();
1846 }
joshualitt25d9c152015-02-18 12:29:52 -08001847 fContext->drawVertices(fRenderTarget,
joshualitt570d2f82015-02-25 13:19:48 -08001848 fClip,
joshualitt25d9c152015-02-18 12:29:52 -08001849 grPaint,
joshualitt5531d512014-12-17 15:50:11 -08001850 *draw.fMatrix,
dandov32a311b2014-07-15 19:46:26 -07001851 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001852 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001853 vertices,
1854 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001855 colors,
dandov32a311b2014-07-15 19:46:26 -07001856 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001857 indexCount);
1858}
1859
1860///////////////////////////////////////////////////////////////////////////////
1861
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001862void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001863 size_t byteLength, SkScalar x, SkScalar y,
1864 const SkPaint& paint) {
1865 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001866 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001867
jvanverth8c27a182014-10-14 08:45:50 -07001868 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001869 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1870 return;
1871 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001872
jvanverth8c27a182014-10-14 08:45:50 -07001873 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001874
joshualitt6e8cd962015-03-20 10:30:14 -07001875 fTextContext->drawText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
1876 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001877}
1878
fmalita05c4a432014-09-29 06:29:53 -07001879void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1880 const SkScalar pos[], int scalarsPerPos,
1881 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001882 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001883 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001884
jvanverth8c27a182014-10-14 08:45:50 -07001885 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001886 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1887 return;
1888 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001889
jvanverth8c27a182014-10-14 08:45:50 -07001890 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001891
joshualitt6e8cd962015-03-20 10:30:14 -07001892 fTextContext->drawPosText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
1893 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1894 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001895}
1896
joshualitt9c328182015-03-23 08:13:04 -07001897void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1898 const SkPaint& paint, SkDrawFilter* drawFilter) {
1899 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawTextBlob", fContext);
1900 CHECK_SHOULD_DRAW(draw);
1901
1902 SkDEBUGCODE(this->validate();)
1903
1904 fTextContext->drawTextBlob(fRenderTarget, fClip, paint, *draw.fMatrix, blob, x, y, drawFilter,
1905 draw.fClip->getBounds());
1906}
1907
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001908///////////////////////////////////////////////////////////////////////////////
1909
reedb2db8982014-11-13 12:41:02 -08001910bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001911 if (paint.getShader() ||
reedb2db8982014-11-13 12:41:02 -08001912 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001913 paint.getMaskFilter() ||
1914 paint.getRasterizer() ||
1915 paint.getColorFilter() ||
1916 paint.getPathEffect() ||
1917 paint.isFakeBoldText() ||
reedb2db8982014-11-13 12:41:02 -08001918 paint.getStyle() != SkPaint::kFill_Style)
1919 {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001920 return true;
1921 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001922 return false;
1923}
1924
1925void SkGpuDevice::flush() {
1926 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001927 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001928}
1929
1930///////////////////////////////////////////////////////////////////////////////
1931
reed76033be2015-03-14 10:54:31 -07001932SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
bsalomonf2703d82014-10-28 14:33:06 -07001933 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001934 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001935 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001936 desc.fWidth = cinfo.fInfo.width();
1937 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001938 desc.fSampleCnt = fRenderTarget->numSamples();
1939
1940 SkAutoTUnref<GrTexture> texture;
1941 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001942 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001943
hcm4396fa52014-10-27 21:43:30 -07001944 // layers are never draw in repeat modes, so we can request an approx
1945 // match and ignore any padding.
reed76033be2015-03-14 10:54:31 -07001946 const GrContext::ScratchTexMatch match = (kNever_TileUsage == cinfo.fTileUsage) ?
hcm4396fa52014-10-27 21:43:30 -07001947 GrContext::kApprox_ScratchTexMatch :
1948 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001949 texture.reset(fContext->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001950
1951 if (texture) {
1952 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001953 return SkGpuDevice::Create(
1954 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001955 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001956 SkErrorInternals::SetError( kInternalError_SkError,
1957 "---- failed to create compatible device texture [%d %d]\n",
1958 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001959 return NULL;
1960 }
1961}
1962
reed4a8126e2014-09-22 07:29:03 -07001963SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001964 // TODO: Change the signature of newSurface to take a budgeted parameter.
1965 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
1966 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->numSamples(),
1967 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001968}
1969
robertphillips30d2cc62014-09-24 08:52:18 -07001970bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001971 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001972#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001973 // todo: should handle this natively
1974 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001975 return false;
1976 }
1977
robertphillips82365912014-11-12 09:32:34 -08001978 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips81f71b62014-11-11 04:54:49 -08001979
1980 const SkPicture::AccelData* data = mainPicture->EXPERIMENTAL_getAccelData(key);
1981 if (!data) {
1982 return false;
1983 }
1984
robertphillipse5524cd2015-02-20 12:30:26 -08001985 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1986 if (0 == gpuData->numBlocks()) {
1987 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001988 }
1989
robertphillipsfd61ed02014-10-28 07:21:44 -07001990 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001991
robertphillipse5524cd2015-02-20 12:30:26 -08001992 SkIRect iBounds;
1993 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1994 return false;
1995 }
1996
1997 SkRect clipBounds = SkRect::Make(iBounds);
1998
1999 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
2000
robertphillipsfd61ed02014-10-28 07:21:44 -07002001 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08002002 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07002003 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08002004 &atlasedNeedRendering, &atlasedRecycled,
2005 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07002006
2007 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
2008
2009 SkTDArray<GrHoistedLayer> needRendering, recycled;
2010
robertphillipse5524cd2015-02-20 12:30:26 -08002011 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
2012
robertphillipsfd61ed02014-10-28 07:21:44 -07002013 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08002014 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07002015 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08002016 &needRendering, &recycled,
2017 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07002018
2019 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002020
robertphillips64bf7672014-08-21 13:07:35 -07002021 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08002022 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
2023 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07002024
robertphillipsfd61ed02014-10-28 07:21:44 -07002025 GrLayerHoister::UnlockLayers(fContext, needRendering);
2026 GrLayerHoister::UnlockLayers(fContext, recycled);
2027 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
2028 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07002029
2030 return true;
robertphillips63242d72014-12-04 08:31:02 -08002031#else
2032 return false;
2033#endif
robertphillips64bf7672014-08-21 13:07:35 -07002034}
2035
senorblancobe129b22014-08-08 07:14:35 -07002036SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07002037 // We always return a transient cache, so it is freed after each
2038 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07002039 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07002040}
reedf037e0b2014-10-30 11:34:15 -07002041
2042#endif