blob: a97e42ece0d12072ff7056e7f8fd2bd3120ed62f [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
9
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000010#include "GrBitmapTextContext.h"
kkinnunenabcfab42015-02-22 22:53:44 -080011#include "GrContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000012#include "GrDistanceFieldTextContext.h"
kkinnunenabcfab42015-02-22 22:53:44 -080013#include "GrGpu.h"
14#include "GrGpuResourcePriv.h"
robertphillips98d709b2014-09-02 10:20:50 -070015#include "GrLayerHoister.h"
robertphillips274b4ba2014-09-04 07:24:18 -070016#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070017#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070018#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080019#include "SkCanvasPriv.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000020#include "SkDeviceImageFilterProxy.h"
21#include "SkDrawProcs.h"
kkinnunenabcfab42015-02-22 22:53:44 -080022#include "SkErrorInternals.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000023#include "SkGlyphCache.h"
kkinnunenabcfab42015-02-22 22:53:44 -080024#include "SkGrTexturePixelRef.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080026#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000027#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000029#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070030#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000031#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080032#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000034#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080035#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000036#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000038#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070039#include "SkXfermode.h"
kkinnunenabcfab42015-02-22 22:53:44 -080040#include "effects/GrBicubicEffect.h"
41#include "effects/GrDashingEffect.h"
42#include "effects/GrSimpleTextureEffect.h"
43#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000044
reedf037e0b2014-10-30 11:34:15 -070045#if SK_SUPPORT_GPU
46
senorblanco55b6d8b2014-07-30 11:26:46 -070047enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
48
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000049#if 0
50 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080051 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000052 do { \
53 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080054 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000055 } while (0)
56#else
joshualitt5531d512014-12-17 15:50:11 -080057 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058#endif
59
60// This constant represents the screen alignment criterion in texels for
61// requiring texture domain clamping to prevent color bleeding when drawing
62// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000063#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000064
65#define DO_DEFERRED_CLEAR() \
66 do { \
bsalomonafe30052015-01-16 07:32:33 -080067 if (fNeedClear) { \
68 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000069 } \
70 } while (false) \
71
72///////////////////////////////////////////////////////////////////////////////
73
74#define CHECK_FOR_ANNOTATION(paint) \
75 do { if (paint.getAnnotation()) { return; } } while (0)
76
77///////////////////////////////////////////////////////////////////////////////
78
bsalomonbcf0a522014-10-08 08:40:09 -070079// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
80// just accesses the backing GrTexture. Otherwise, it creates a cached texture
81// representation and releases it in the destructor.
82class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040083public:
bsalomonbcf0a522014-10-08 08:40:09 -070084 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070085
bsalomonbcf0a522014-10-08 08:40:09 -070086 AutoBitmapTexture(GrContext* context,
87 const SkBitmap& bitmap,
88 const GrTextureParams* params,
89 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040090 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070091 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040092 }
93
bsalomonbcf0a522014-10-08 08:40:09 -070094 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040095 const SkBitmap& bitmap,
96 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -070097 // Either get the texture directly from the bitmap, or else use the cache and
98 // remember to unref it.
99 if (GrTexture* bmpTexture = bitmap.getTexture()) {
100 fTexture.reset(NULL);
101 return bmpTexture;
102 } else {
103 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
104 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400105 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400106 }
107
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000108private:
bsalomonbcf0a522014-10-08 08:40:09 -0700109 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000110};
111
112///////////////////////////////////////////////////////////////////////////////
113
114struct GrSkDrawProcs : public SkDrawProcs {
115public:
116 GrContext* fContext;
117 GrTextContext* fTextContext;
118 GrFontScaler* fFontScaler; // cached in the skia glyphcache
119};
120
121///////////////////////////////////////////////////////////////////////////////
122
bsalomonafe30052015-01-16 07:32:33 -0800123SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags) {
124 if (!rt || rt->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000125 return NULL;
126 }
bsalomonafe30052015-01-16 07:32:33 -0800127 return SkNEW_ARGS(SkGpuDevice, (rt, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000128}
129
bsalomonafe30052015-01-16 07:32:33 -0800130static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps* props) {
131 if (props) {
132 return SkDeviceProperties(props->pixelGeometry());
133 } else {
134 return SkDeviceProperties(SkDeviceProperties::kLegacyLCD_InitType);
135 }
reedb2db8982014-11-13 12:41:02 -0800136}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000137
bsalomonafe30052015-01-16 07:32:33 -0800138static SkSurfaceProps copy_or_default_props(const SkSurfaceProps* props) {
139 if (props) {
140 return SkSurfaceProps(*props);
141 } else {
142 return SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
143 }
144}
145
146SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags)
reedb2db8982014-11-13 12:41:02 -0800147 : INHERITED(surfaceprops_to_deviceprops(props))
bsalomonafe30052015-01-16 07:32:33 -0800148 , fSurfaceProps(copy_or_default_props(props))
reedb2db8982014-11-13 12:41:02 -0800149{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000150 fDrawProcs = NULL;
151
bsalomonafe30052015-01-16 07:32:33 -0800152 fContext = SkRef(rt->getContext());
153 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000154
bsalomonafe30052015-01-16 07:32:33 -0800155 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000156
bsalomonafe30052015-01-16 07:32:33 -0800157 SkImageInfo info = rt->surfacePriv().info();
158 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt));
bsalomonafbf2d62014-09-30 12:18:44 -0700159 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700160 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700161
bsalomonafe30052015-01-16 07:32:33 -0800162 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts();
jvanverth4736e142014-11-07 07:12:46 -0800163 fTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFT);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000164}
165
kkinnunenabcfab42015-02-22 22:53:44 -0800166GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
167 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000168 if (kUnknown_SkColorType == origInfo.colorType() ||
169 origInfo.width() < 0 || origInfo.height() < 0) {
170 return NULL;
171 }
172
bsalomonafe30052015-01-16 07:32:33 -0800173 if (!context) {
174 return NULL;
175 }
176
reede5ea5002014-09-03 11:54:58 -0700177 SkColorType ct = origInfo.colorType();
178 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700179 if (kRGB_565_SkColorType == ct) {
180 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800181 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
182 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700183 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800184 }
185 if (kOpaque_SkAlphaType != at) {
186 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000187 }
reede5ea5002014-09-03 11:54:58 -0700188 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000189
bsalomonf2703d82014-10-28 14:33:06 -0700190 GrSurfaceDesc desc;
191 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000192 desc.fWidth = info.width();
193 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000194 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000195 desc.fSampleCnt = sampleCount;
kkinnunenabcfab42015-02-22 22:53:44 -0800196 GrTexture* texture = context->createTexture(desc, SkToBool(budgeted), NULL, 0);
197 if (NULL == texture) {
198 return NULL;
199 }
200 SkASSERT(NULL != texture->asRenderTarget());
201 return texture->asRenderTarget();
202}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000203
kkinnunenabcfab42015-02-22 22:53:44 -0800204SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
205 const SkImageInfo& info, int sampleCount,
206 const SkSurfaceProps* props, unsigned flags) {
207
208 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
209 if (NULL == rt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000210 return NULL;
211 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000212
kkinnunenabcfab42015-02-22 22:53:44 -0800213 return SkNEW_ARGS(SkGpuDevice, (rt, props, flags));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000214}
215
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000216SkGpuDevice::~SkGpuDevice() {
217 if (fDrawProcs) {
218 delete fDrawProcs;
219 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000220
jvanverth8c27a182014-10-14 08:45:50 -0700221 delete fTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000222
bsalomon32d0b3b2014-08-29 07:50:23 -0700223 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000224 fContext->unref();
225}
226
227///////////////////////////////////////////////////////////////////////////////
228
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000229bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
230 int x, int y) {
231 DO_DEFERRED_CLEAR();
232
233 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000234 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000235 if (kUnknown_GrPixelConfig == config) {
236 return false;
237 }
238
239 uint32_t flags = 0;
240 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
241 flags = GrContext::kUnpremul_PixelOpsFlag;
242 }
243 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
244 config, dstPixels, dstRowBytes, flags);
245}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000246
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000247bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
248 int x, int y) {
249 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000250 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000251 if (kUnknown_GrPixelConfig == config) {
252 return false;
253 }
254 uint32_t flags = 0;
255 if (kUnpremul_SkAlphaType == info.alphaType()) {
256 flags = GrContext::kUnpremul_PixelOpsFlag;
257 }
258 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
259
260 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700261 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000262
263 return true;
264}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000265
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000266const SkBitmap& SkGpuDevice::onAccessBitmap() {
267 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700268 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000269}
270
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000271void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
272 INHERITED::onAttachToCanvas(canvas);
273
274 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800275 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000276}
277
278void SkGpuDevice::onDetachFromCanvas() {
279 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800280 fClip.reset();
joshualitt44701df2015-02-23 14:44:57 -0800281 fClipStack.reset(NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000282}
283
284// call this every draw call, to ensure that the context reflects our state,
285// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800286void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800287 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000288
joshualitt44701df2015-02-23 14:44:57 -0800289 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000290
joshualitt570d2f82015-02-25 13:19:48 -0800291 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000292
293 DO_DEFERRED_CLEAR();
294}
295
296GrRenderTarget* SkGpuDevice::accessRenderTarget() {
297 DO_DEFERRED_CLEAR();
298 return fRenderTarget;
299}
300
reed8eddfb52014-12-04 07:50:14 -0800301void SkGpuDevice::clearAll() {
302 GrColor color = 0;
303 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
304 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
305 fContext->clear(&rect, color, true, fRenderTarget);
bsalomonafe30052015-01-16 07:32:33 -0800306 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800307}
308
kkinnunenabcfab42015-02-22 22:53:44 -0800309void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
310 // Caller must have accessed the render target, because it knows the rt must be replaced.
311 SkASSERT(!fNeedClear);
312
313 SkSurface::Budgeted budgeted =
314 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
315 : SkSurface::kNo_Budgeted;
316
317 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
318 fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->numSamples()));
319
320 if (NULL == newRT) {
321 return;
322 }
323
324 if (shouldRetainContent) {
325 if (fRenderTarget->wasDestroyed()) {
326 return;
327 }
328 this->context()->copySurface(newRT, fRenderTarget);
329 }
330
331 SkASSERT(fRenderTarget != newRT);
332
333 fRenderTarget->unref();
334 fRenderTarget = newRT.detach();
335
336 SkASSERT(fRenderTarget->surfacePriv().info() == fLegacyBitmap.info());
337 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (fRenderTarget->surfacePriv().info(), fRenderTarget));
338 fLegacyBitmap.setPixelRef(pr)->unref();
339}
340
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000341///////////////////////////////////////////////////////////////////////////////
342
343SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
344SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
345SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
346SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
347SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
348 shader_type_mismatch);
349SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
350 shader_type_mismatch);
351SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
352SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
353
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000354///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000355
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800357 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700358 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000359
360 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800361 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000362
joshualitt570d2f82015-02-25 13:19:48 -0800363 fContext->drawPaint(fRenderTarget, fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000364}
365
366// must be in SkCanvas::PointMode order
367static const GrPrimitiveType gPointMode2PrimtiveType[] = {
368 kPoints_GrPrimitiveType,
369 kLines_GrPrimitiveType,
370 kLineStrip_GrPrimitiveType
371};
372
373void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
374 size_t count, const SkPoint pts[], const SkPaint& paint) {
375 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800376 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000377
378 SkScalar width = paint.getStrokeWidth();
379 if (width < 0) {
380 return;
381 }
382
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000383 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700384 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
385 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800386 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
egdaniele61c4112014-06-12 10:24:21 -0700387 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700388 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700389 path.moveTo(pts[0]);
390 path.lineTo(pts[1]);
joshualitt570d2f82015-02-25 13:19:48 -0800391 fContext->drawPath(fRenderTarget, fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700392 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000393 }
394
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000395 // we only handle hairlines and paints without path effects or mask filters,
396 // else we let the SkDraw call our drawPath()
397 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
398 draw.drawPoints(mode, count, pts, paint, true);
399 return;
400 }
401
402 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800403 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000404
joshualitt25d9c152015-02-18 12:29:52 -0800405 fContext->drawVertices(fRenderTarget,
joshualitt570d2f82015-02-25 13:19:48 -0800406 fClip,
joshualitt25d9c152015-02-18 12:29:52 -0800407 grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800408 *draw.fMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000409 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000410 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000411 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000412 NULL,
413 NULL,
414 NULL,
415 0);
416}
417
418///////////////////////////////////////////////////////////////////////////////
419
420void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
421 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700422 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
423
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000424 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800425 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000426
427 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
428 SkScalar width = paint.getStrokeWidth();
429
430 /*
431 We have special code for hairline strokes, miter-strokes, bevel-stroke
432 and fills. Anything else we just call our path code.
433 */
434 bool usePath = doStroke && width > 0 &&
435 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
436 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
437 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700438
439 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000440 usePath = true;
441 }
egdanield58a0ba2014-06-11 10:30:05 -0700442
joshualitt5531d512014-12-17 15:50:11 -0800443 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000444#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
445 if (doStroke) {
446#endif
447 usePath = true;
448#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
449 } else {
joshualitt5531d512014-12-17 15:50:11 -0800450 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000451 }
452#endif
453 }
454 // until we can both stroke and fill rectangles
455 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
456 usePath = true;
457 }
458
egdanield58a0ba2014-06-11 10:30:05 -0700459 GrStrokeInfo strokeInfo(paint);
460
461 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700462 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700463 usePath = true;
464 }
465
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000466 if (usePath) {
467 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700468 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000469 path.addRect(rect);
470 this->drawPath(draw, path, paint, NULL, true);
471 return;
472 }
473
474 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800475 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400476
joshualitt570d2f82015-02-25 13:19:48 -0800477 fContext->drawRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000478}
479
480///////////////////////////////////////////////////////////////////////////////
481
482void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800483 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700484 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000485 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800486 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000487
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000488 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800489 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400490
egdanield58a0ba2014-06-11 10:30:05 -0700491 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000492 if (paint.getMaskFilter()) {
493 // try to hit the fast path for drawing filtered round rects
494
495 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800496 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000497 if (devRRect.allCornersCircular()) {
498 SkRect maskRect;
499 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800500 draw.fClip->getBounds(),
501 *draw.fMatrix,
502 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000503 SkIRect finalIRect;
504 maskRect.roundOut(&finalIRect);
505 if (draw.fClip->quickReject(finalIRect)) {
506 // clipped out
507 return;
508 }
joshualitt25d9c152015-02-18 12:29:52 -0800509 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
510 fRenderTarget,
511 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800512 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800513 *draw.fMatrix,
egdanield58a0ba2014-06-11 10:30:05 -0700514 strokeInfo.getStrokeRec(),
515 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000516 return;
517 }
518 }
519
520 }
521 }
522
523 }
524
egdanield58a0ba2014-06-11 10:30:05 -0700525 bool usePath = false;
526
527 if (paint.getMaskFilter()) {
528 usePath = true;
529 } else {
530 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700531 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700532 usePath = true;
533 }
534 }
535
536
537 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000538 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700539 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000540 path.addRRect(rect);
541 this->drawPath(draw, path, paint, NULL, true);
542 return;
543 }
Mike Klein744fb732014-06-23 15:13:26 -0400544
joshualitt570d2f82015-02-25 13:19:48 -0800545 fContext->drawRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000546}
547
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000548void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800549 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000550 SkStrokeRec stroke(paint);
551 if (stroke.isFillStyle()) {
552
553 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800554 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000555
556 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800557 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000558
559 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
joshualitt570d2f82015-02-25 13:19:48 -0800560 fContext->drawDRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000561 return;
562 }
563 }
564
565 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700566 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000567 path.addRRect(outer);
568 path.addRRect(inner);
569 path.setFillType(SkPath::kEvenOdd_FillType);
570
571 this->drawPath(draw, path, paint, NULL, true);
572}
573
574
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000575/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000576
577void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
578 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700579 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000580 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800581 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000582
egdanield58a0ba2014-06-11 10:30:05 -0700583 GrStrokeInfo strokeInfo(paint);
584
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000585 bool usePath = false;
586 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700587 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000588 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700589 } else {
590 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700591 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700592 usePath = true;
593 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000594 }
595
596 if (usePath) {
597 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700598 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000599 path.addOval(oval);
600 this->drawPath(draw, path, paint, NULL, true);
601 return;
602 }
603
604 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800605 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000606
joshualitt570d2f82015-02-25 13:19:48 -0800607 fContext->drawOval(fRenderTarget, fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000608}
609
610#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000611
612///////////////////////////////////////////////////////////////////////////////
613
614// helpers for applying mask filters
615namespace {
616
617// Draw a mask using the supplied paint. Since the coverage/geometry
618// is already burnt into the mask this boils down to a rect draw.
619// Return true if the mask was successfully drawn.
joshualitt25d9c152015-02-18 12:29:52 -0800620bool draw_mask(GrContext* context,
621 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800622 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800623 const SkMatrix& viewMatrix,
624 const SkRect& maskRect,
625 GrPaint* grp,
626 GrTexture* mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000627 SkMatrix matrix;
628 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
629 matrix.postIDiv(mask->width(), mask->height());
630
joshualitt16b27892014-12-18 07:47:16 -0800631 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix,
632 kDevice_GrCoordSet))->unref();
633
634 SkMatrix inverse;
635 if (!viewMatrix.invert(&inverse)) {
636 return false;
637 }
joshualitt570d2f82015-02-25 13:19:48 -0800638 context->drawNonAARectWithLocalMatrix(rt, clip, *grp, SkMatrix::I(), maskRect, inverse);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000639 return true;
640}
641
joshualitt5efb8b82015-03-18 11:49:12 -0700642static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
643 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
644}
645
joshualitt25d9c152015-02-18 12:29:52 -0800646bool draw_with_mask_filter(GrContext* context,
647 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800648 const GrClip& clipData,
joshualitt25d9c152015-02-18 12:29:52 -0800649 const SkMatrix& viewMatrix,
650 const SkPath& devPath,
651 SkMaskFilter* filter,
joshualitt5efb8b82015-03-18 11:49:12 -0700652 const SkIRect& clipBounds,
joshualitt25d9c152015-02-18 12:29:52 -0800653 GrPaint* grp,
654 SkPaint::Style style) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000655 SkMask srcM, dstM;
656
joshualitt5efb8b82015-03-18 11:49:12 -0700657 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000658 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
659 return false;
660 }
661 SkAutoMaskFreeImage autoSrc(srcM.fImage);
662
joshualitt5531d512014-12-17 15:50:11 -0800663 if (!filter->filterMask(&dstM, srcM, viewMatrix, NULL)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000664 return false;
665 }
666 // this will free-up dstM when we're done (allocated in filterMask())
667 SkAutoMaskFreeImage autoDst(dstM.fImage);
668
joshualitt5efb8b82015-03-18 11:49:12 -0700669 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000670 return false;
671 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000672
673 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
674 // the current clip (and identity matrix) and GrPaint settings
bsalomonf2703d82014-10-28 14:33:06 -0700675 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000676 desc.fWidth = dstM.fBounds.width();
677 desc.fHeight = dstM.fBounds.height();
678 desc.fConfig = kAlpha_8_GrPixelConfig;
679
bsalomone3059732014-10-14 11:47:22 -0700680 SkAutoTUnref<GrTexture> texture(
681 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
682 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000683 return false;
684 }
685 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
686 dstM.fImage, dstM.fRowBytes);
687
688 SkRect maskRect = SkRect::Make(dstM.fBounds);
689
joshualitt570d2f82015-02-25 13:19:48 -0800690 return draw_mask(context, rt, clipData, viewMatrix, maskRect, grp, texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000691}
692
bsalomone3059732014-10-14 11:47:22 -0700693// Create a mask of 'devPath' and place the result in 'mask'.
694GrTexture* create_mask_GPU(GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800695 GrRenderTarget* rt,
bsalomone3059732014-10-14 11:47:22 -0700696 const SkRect& maskRect,
697 const SkPath& devPath,
698 const GrStrokeInfo& strokeInfo,
699 bool doAA,
700 int sampleCnt) {
bsalomonf2703d82014-10-28 14:33:06 -0700701 GrSurfaceDesc desc;
702 desc.fFlags = kRenderTarget_GrSurfaceFlag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000703 desc.fWidth = SkScalarCeilToInt(maskRect.width());
704 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700705 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000706 // We actually only need A8, but it often isn't supported as a
707 // render target so default to RGBA_8888
708 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700709
710 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
711 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000712 desc.fConfig = kAlpha_8_GrPixelConfig;
713 }
714
bsalomone3059732014-10-14 11:47:22 -0700715 GrTexture* mask = context->refScratchTexture(desc,GrContext::kApprox_ScratchTexMatch);
716 if (NULL == mask) {
717 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000718 }
719
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000720 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
721
bsalomon89c62982014-11-03 12:08:42 -0800722 context->clear(NULL, 0x0, true, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000723
724 GrPaint tempPaint;
egdanielb197b8f2015-02-17 07:34:43 -0800725 tempPaint.setAntiAlias(doAA);
726 tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000727
joshualitt570d2f82015-02-25 13:19:48 -0800728 // setup new clip
729 GrClip clip(clipRect);
730
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000731 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
732 SkMatrix translate;
733 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
joshualitt570d2f82015-02-25 13:19:48 -0800734 context->drawPath(mask->asRenderTarget(), clip, tempPaint, translate, devPath, strokeInfo);
bsalomone3059732014-10-14 11:47:22 -0700735 return mask;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000736}
737
738SkBitmap wrap_texture(GrTexture* texture) {
739 SkBitmap result;
bsalomonafbf2d62014-09-30 12:18:44 -0700740 result.setInfo(texture->surfacePriv().info());
reed6c225732014-06-09 19:52:07 -0700741 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742 return result;
743}
744
745};
746
747void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
748 const SkPaint& paint, const SkMatrix* prePathMatrix,
749 bool pathIsMutable) {
750 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800751 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700752 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000753
joshualitt5efb8b82015-03-18 11:49:12 -0700754 return this->internalDrawPath(origSrcPath, paint, *draw.fMatrix, prePathMatrix,
755 draw.fClip->getBounds(), pathIsMutable);
756}
757
758void SkGpuDevice::internalDrawPath(const SkPath& origSrcPath, const SkPaint& paint,
759 const SkMatrix& origViewMatrix, const SkMatrix* prePathMatrix,
760 const SkIRect& clipBounds, bool pathIsMutable) {
jvanverthb3eb6872014-10-24 07:12:51 -0700761 SkASSERT(!pathIsMutable || origSrcPath.isVolatile());
joshualitt5531d512014-12-17 15:50:11 -0800762
bsalomon54443932015-01-29 09:34:18 -0800763 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000765 // If we have a prematrix, apply it to the path, optimizing for the case
766 // where the original path can in fact be modified in place (even though
767 // its parameter type is const).
768 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000769 SkTLazy<SkPath> tmpPath;
770 SkTLazy<SkPath> effectPath;
bsalomon54443932015-01-29 09:34:18 -0800771 SkPathEffect* pathEffect = paint.getPathEffect();
772
joshualitt5efb8b82015-03-18 11:49:12 -0700773 SkMatrix viewMatrix = origViewMatrix;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000774
775 if (prePathMatrix) {
bsalomon54443932015-01-29 09:34:18 -0800776 // stroking and path effects are supposed to be applied *after* the prePathMatrix.
777 // The pre-path-matrix also should not affect shadeing.
778 if (NULL == pathEffect && NULL == paint.getShader() &&
779 (strokeInfo.getStrokeRec().isFillStyle() ||
780 strokeInfo.getStrokeRec().isHairlineStyle())) {
781 viewMatrix.preConcat(*prePathMatrix);
782 } else {
783 SkPath* result = pathPtr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000784
bsalomon54443932015-01-29 09:34:18 -0800785 if (!pathIsMutable) {
786 result = tmpPath.init();
787 result->setIsVolatile(true);
788 pathIsMutable = true;
789 }
790 // should I push prePathMatrix on our MV stack temporarily, instead
791 // of applying it here? See SkDraw.cpp
792 pathPtr->transform(*prePathMatrix, result);
793 pathPtr = result;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000794 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000795 }
796 // at this point we're done with prePathMatrix
797 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
798
bsalomon54443932015-01-29 09:34:18 -0800799 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800800 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, viewMatrix, true, &grPaint);
bsalomon54443932015-01-29 09:34:18 -0800801
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000802 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700803 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
804 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000805 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000806 pathPtr = effectPath.get();
807 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700808 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000809 }
810
egdanield58a0ba2014-06-11 10:30:05 -0700811 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000812 if (paint.getMaskFilter()) {
813 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000814 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
815 if (stroke.applyToPath(strokedPath, *pathPtr)) {
816 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000817 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700818 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000819 }
820 }
821
822 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000823 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700824 if (!pathIsMutable) {
825 devPathPtr->setIsVolatile(true);
826 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000827
828 // transform the path into device space
bsalomon54443932015-01-29 09:34:18 -0800829 pathPtr->transform(viewMatrix, devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000830
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000831 SkRect maskRect;
832 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
joshualitt5efb8b82015-03-18 11:49:12 -0700833 clipBounds,
bsalomon54443932015-01-29 09:34:18 -0800834 viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000835 &maskRect)) {
836 SkIRect finalIRect;
837 maskRect.roundOut(&finalIRect);
joshualitt5efb8b82015-03-18 11:49:12 -0700838 if (clip_bounds_quick_reject(clipBounds, finalIRect)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000839 // clipped out
840 return;
841 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000842
joshualitt25d9c152015-02-18 12:29:52 -0800843 if (paint.getMaskFilter()->directFilterMaskGPU(fContext,
844 fRenderTarget,
845 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800846 fClip,
joshualitt25d9c152015-02-18 12:29:52 -0800847 viewMatrix,
848 stroke,
849 *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000850 // the mask filter was able to draw itself directly, so there's nothing
851 // left to do.
852 return;
853 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000854
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000855
joshualitt25d9c152015-02-18 12:29:52 -0800856 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext,
857 fRenderTarget,
858 maskRect,
859 *devPathPtr,
860 strokeInfo,
861 grPaint.isAntiAlias(),
bsalomone3059732014-10-14 11:47:22 -0700862 fRenderTarget->numSamples()));
863 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000864 GrTexture* filtered;
865
bsalomon54443932015-01-29 09:34:18 -0800866 if (paint.getMaskFilter()->filterMaskGPU(mask, viewMatrix, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000867 // filterMaskGPU gives us ownership of a ref to the result
868 SkAutoTUnref<GrTexture> atu(filtered);
joshualitt570d2f82015-02-25 13:19:48 -0800869 if (draw_mask(fContext,
870 fRenderTarget,
871 fClip,
872 viewMatrix,
873 maskRect,
874 &grPaint,
joshualitt25d9c152015-02-18 12:29:52 -0800875 filtered)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000876 // This path is completely drawn
877 return;
878 }
879 }
880 }
881 }
882
883 // draw the mask on the CPU - this is a fallthrough path in case the
884 // GPU path fails
885 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
886 SkPaint::kFill_Style;
joshualitt570d2f82015-02-25 13:19:48 -0800887 draw_with_mask_filter(fContext, fRenderTarget, fClip, viewMatrix, *devPathPtr,
joshualitt5efb8b82015-03-18 11:49:12 -0700888 paint.getMaskFilter(), clipBounds, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000889 return;
890 }
891
joshualitt570d2f82015-02-25 13:19:48 -0800892 fContext->drawPath(fRenderTarget, fClip, grPaint, viewMatrix, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000893}
894
895static const int kBmpSmallTileSize = 1 << 10;
896
897static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
898 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
899 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
900 return tilesX * tilesY;
901}
902
903static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
904 if (maxTileSize <= kBmpSmallTileSize) {
905 return maxTileSize;
906 }
907
908 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
909 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
910
911 maxTileTotalTileSize *= maxTileSize * maxTileSize;
912 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
913
914 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
915 return kBmpSmallTileSize;
916 } else {
917 return maxTileSize;
918 }
919}
920
921// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
922// pixels from the bitmap are necessary.
923static void determine_clipped_src_rect(const GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800924 const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800925 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800926 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000927 const SkBitmap& bitmap,
928 const SkRect* srcRectPtr,
929 SkIRect* clippedSrcIRect) {
joshualitt570d2f82015-02-25 13:19:48 -0800930 clip.getConservativeBounds(rt, clippedSrcIRect, NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000931 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800932 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933 clippedSrcIRect->setEmpty();
934 return;
935 }
936 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
937 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700938 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000939 // we've setup src space 0,0 to map to the top left of the src rect.
940 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000941 if (!clippedSrcRect.intersect(*srcRectPtr)) {
942 clippedSrcIRect->setEmpty();
943 return;
944 }
945 }
946 clippedSrcRect.roundOut(clippedSrcIRect);
947 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
948 if (!clippedSrcIRect->intersect(bmpBounds)) {
949 clippedSrcIRect->setEmpty();
950 }
951}
952
953bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800954 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000955 const GrTextureParams& params,
956 const SkRect* srcRectPtr,
957 int maxTileSize,
958 int* tileSize,
959 SkIRect* clippedSrcRect) const {
960 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700961 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000962 return false;
963 }
964
965 // if it's larger than the max tile size, then we have no choice but tiling.
966 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt570d2f82015-02-25 13:19:48 -0800967 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap,
968 srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000969 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
970 return true;
971 }
972
973 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
974 return false;
975 }
976
977 // if the entire texture is already in our cache then no reason to tile it
978 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
979 return false;
980 }
981
982 // At this point we know we could do the draw by uploading the entire bitmap
983 // as a texture. However, if the texture would be large compared to the
984 // cache size and we don't require most of it for this draw then tile to
985 // reduce the amount of upload and cache spill.
986
987 // assumption here is that sw bitmap size is a good proxy for its size as
988 // a texture
989 size_t bmpSize = bitmap.getSize();
990 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000991 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000992 if (bmpSize < cacheSize / 2) {
993 return false;
994 }
995
996 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt570d2f82015-02-25 13:19:48 -0800997 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap, srcRectPtr,
joshualitt25d9c152015-02-18 12:29:52 -0800998 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000999 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
1000 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
1001 kBmpSmallTileSize * kBmpSmallTileSize;
1002
1003 return usedTileBytes < 2 * bmpSize;
1004}
1005
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001006void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001007 const SkBitmap& bitmap,
1008 const SkMatrix& m,
1009 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001010 SkMatrix concat;
1011 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1012 if (!m.isIdentity()) {
1013 concat.setConcat(*draw->fMatrix, m);
1014 draw.writable()->fMatrix = &concat;
1015 }
1016 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001017}
1018
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001019// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001020// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1021// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001022static inline void clamped_outset_with_offset(SkIRect* iRect,
1023 int outset,
1024 SkPoint* offset,
1025 const SkIRect& clamp) {
1026 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001027
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001028 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1029 if (leftClampDelta > 0) {
1030 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001031 iRect->fLeft = clamp.fLeft;
1032 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001033 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001034 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001035
1036 int topClampDelta = clamp.fTop - iRect->fTop;
1037 if (topClampDelta > 0) {
1038 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001039 iRect->fTop = clamp.fTop;
1040 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001041 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001042 }
1043
1044 if (iRect->fRight > clamp.fRight) {
1045 iRect->fRight = clamp.fRight;
1046 }
1047 if (iRect->fBottom > clamp.fBottom) {
1048 iRect->fBottom = clamp.fBottom;
1049 }
1050}
1051
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001052static bool has_aligned_samples(const SkRect& srcRect,
1053 const SkRect& transformedRect) {
1054 // detect pixel disalignment
1055 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1056 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1057 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1058 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1059 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1060 COLOR_BLEED_TOLERANCE &&
1061 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1062 COLOR_BLEED_TOLERANCE) {
1063 return true;
1064 }
1065 return false;
1066}
1067
1068static bool may_color_bleed(const SkRect& srcRect,
1069 const SkRect& transformedRect,
1070 const SkMatrix& m) {
1071 // Only gets called if has_aligned_samples returned false.
1072 // So we can assume that sampling is axis aligned but not texel aligned.
1073 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1074 SkRect innerSrcRect(srcRect), innerTransformedRect,
1075 outerTransformedRect(transformedRect);
1076 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1077 m.mapRect(&innerTransformedRect, innerSrcRect);
1078
1079 // The gap between outerTransformedRect and innerTransformedRect
1080 // represents the projection of the source border area, which is
1081 // problematic for color bleeding. We must check whether any
1082 // destination pixels sample the border area.
1083 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1084 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1085 SkIRect outer, inner;
1086 outerTransformedRect.round(&outer);
1087 innerTransformedRect.round(&inner);
1088 // If the inner and outer rects round to the same result, it means the
1089 // border does not overlap any pixel centers. Yay!
1090 return inner != outer;
1091}
1092
1093static bool needs_texture_domain(const SkBitmap& bitmap,
1094 const SkRect& srcRect,
1095 GrTextureParams &params,
1096 const SkMatrix& contextMatrix,
1097 bool bicubic) {
1098 bool needsTextureDomain = false;
1099
1100 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1101 // Need texture domain if drawing a sub rect
1102 needsTextureDomain = srcRect.width() < bitmap.width() ||
1103 srcRect.height() < bitmap.height();
1104 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1105 // sampling is axis-aligned
1106 SkRect transformedRect;
1107 contextMatrix.mapRect(&transformedRect, srcRect);
1108
1109 if (has_aligned_samples(srcRect, transformedRect)) {
1110 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1111 needsTextureDomain = false;
1112 } else {
1113 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1114 }
1115 }
1116 }
1117 return needsTextureDomain;
1118}
1119
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001120void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1121 const SkBitmap& bitmap,
1122 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001123 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001124 const SkPaint& paint,
1125 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -08001126 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001127
1128 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001129 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001130 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1131 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001132 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001133 SkScalar w = SkIntToScalar(bitmap.width());
1134 SkScalar h = SkIntToScalar(bitmap.height());
1135 dstSize.fWidth = w;
1136 dstSize.fHeight = h;
1137 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001138 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001139 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001140 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001141 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001142 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001143 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1144 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1145 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1146 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001147 }
1148
derekf367e1862014-12-02 11:02:06 -08001149 // If the render target is not msaa and draw is antialiased, we call
1150 // drawRect instead of drawing on the render target directly.
1151 // FIXME: the tiled bitmap code path doesn't currently support
1152 // anti-aliased edges, we work around that for now by drawing directly
1153 // if the image size exceeds maximum texture size.
1154 int maxTextureSize = fContext->getMaxTextureSize();
1155 bool directDraw = fRenderTarget->isMultisampled() ||
1156 !paint.isAntiAlias() ||
1157 bitmap.width() > maxTextureSize ||
1158 bitmap.height() > maxTextureSize;
1159
1160 // we check whether dst rect are pixel aligned
1161 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -08001162 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -08001163
1164 if (staysRect) {
1165 SkRect rect;
1166 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -08001167 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -08001168 const SkScalar *scalars = rect.asScalars();
1169 bool isDstPixelAligned = true;
1170 for (int i = 0; i < 4; i++) {
1171 if (!SkScalarIsInt(scalars[i])) {
1172 isDstPixelAligned = false;
1173 break;
1174 }
1175 }
1176
1177 if (isDstPixelAligned)
1178 directDraw = true;
1179 }
1180 }
1181
1182 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001183 // Convert the bitmap to a shader so that the rect can be drawn
1184 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001185 SkBitmap tmp; // subset of bitmap, if necessary
1186 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001187 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001188 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001189 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1190 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1191 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001192 // In bleed mode we position and trim the bitmap based on the src rect which is
1193 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1194 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1195 // compensate.
1196 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1197 SkIRect iSrc;
1198 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001199
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001200 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1201 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001202
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001203 if (!bitmap.extractSubset(&tmp, iSrc)) {
1204 return; // extraction failed
1205 }
1206 bitmapPtr = &tmp;
1207 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001208
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001209 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001210 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001211 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001212 } else {
1213 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001214 }
1215
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001216 SkPaint paintWithShader(paint);
1217 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001218 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001219 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1220 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001221
1222 return;
1223 }
1224
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001225 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1226 // the view matrix rather than a local matrix.
1227 SkMatrix m;
1228 m.setScale(dstSize.fWidth / srcRect.width(),
1229 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -08001230 SkMatrix viewM = *draw.fMatrix;
1231 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001232
1233 GrTextureParams params;
reed93a12152015-03-16 10:08:34 -07001234 SkFilterQuality paintFilterQuality = paint.getFilterQuality();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001235 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001236
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001237 bool doBicubic = false;
1238
reed93a12152015-03-16 10:08:34 -07001239 switch(paintFilterQuality) {
1240 case kNone_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001241 textureFilterMode = GrTextureParams::kNone_FilterMode;
1242 break;
reed93a12152015-03-16 10:08:34 -07001243 case kLow_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001244 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1245 break;
reed93a12152015-03-16 10:08:34 -07001246 case kMedium_SkFilterQuality:
joshualitt5531d512014-12-17 15:50:11 -08001247 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001248 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1249 } else {
1250 // Don't trigger MIP level generation unnecessarily.
1251 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1252 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001253 break;
reed93a12152015-03-16 10:08:34 -07001254 case kHigh_SkFilterQuality:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001255 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001256 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001257 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001258 break;
1259 default:
1260 SkErrorInternals::SetError( kInvalidPaint_SkError,
1261 "Sorry, I don't understand the filtering "
1262 "mode you asked for. Falling back to "
1263 "MIPMaps.");
1264 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1265 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001266 }
1267
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001268 int tileFilterPad;
1269 if (doBicubic) {
1270 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1271 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1272 tileFilterPad = 0;
1273 } else {
1274 tileFilterPad = 1;
1275 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001276 params.setFilterMode(textureFilterMode);
1277
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001278 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001279 int tileSize;
1280
1281 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001282 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001283 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001284 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1285 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001286 } else {
1287 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001288 bool needsTextureDomain = needs_texture_domain(bitmap,
1289 srcRect,
1290 params,
joshualitt5531d512014-12-17 15:50:11 -08001291 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001292 doBicubic);
1293 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001294 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001295 srcRect,
1296 params,
1297 paint,
1298 flags,
1299 doBicubic,
1300 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001301 }
1302}
1303
1304// Break 'bitmap' into several tiles to draw it since it has already
1305// been determined to be too large to fit in VRAM
1306void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001307 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001308 const SkRect& srcRect,
1309 const SkIRect& clippedSrcIRect,
1310 const GrTextureParams& params,
1311 const SkPaint& paint,
1312 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001313 int tileSize,
1314 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001315 // The following pixel lock is technically redundant, but it is desirable
1316 // to lock outside of the tile loop to prevent redecoding the whole image
1317 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1318 // is larger than the limit of the discardable memory pool.
1319 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001320 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1321
1322 int nx = bitmap.width() / tileSize;
1323 int ny = bitmap.height() / tileSize;
1324 for (int x = 0; x <= nx; x++) {
1325 for (int y = 0; y <= ny; y++) {
1326 SkRect tileR;
1327 tileR.set(SkIntToScalar(x * tileSize),
1328 SkIntToScalar(y * tileSize),
1329 SkIntToScalar((x + 1) * tileSize),
1330 SkIntToScalar((y + 1) * tileSize));
1331
1332 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1333 continue;
1334 }
1335
1336 if (!tileR.intersect(srcRect)) {
1337 continue;
1338 }
1339
1340 SkBitmap tmpB;
1341 SkIRect iTileR;
1342 tileR.roundOut(&iTileR);
1343 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1344 SkIntToScalar(iTileR.fTop));
1345
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001346 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001347 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001348 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001349 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001350 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001351
robertphillipsec8bb942014-11-21 10:16:25 -08001352 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001353 SkIRect iClampRect;
1354
1355 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1356 // In bleed mode we want to always expand the tile on all edges
1357 // but stay within the bitmap bounds
1358 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1359 } else {
1360 // In texture-domain/clamp mode we only want to expand the
1361 // tile on edges interior to "srcRect" (i.e., we want to
1362 // not bleed across the original clamped edges)
1363 srcRect.roundOut(&iClampRect);
1364 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001365 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1366 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001367 }
1368
1369 if (bitmap.extractSubset(&tmpB, iTileR)) {
1370 // now offset it to make it "local" to our tmp bitmap
1371 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001372 GrTextureParams paramsTemp = params;
1373 bool needsTextureDomain = needs_texture_domain(bitmap,
1374 srcRect,
1375 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001376 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001377 bicubic);
1378 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001379 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001380 tileR,
1381 paramsTemp,
1382 paint,
1383 flags,
1384 bicubic,
1385 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001386 }
1387 }
1388 }
1389}
1390
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001391
1392/*
1393 * This is called by drawBitmap(), which has to handle images that may be too
1394 * large to be represented by a single texture.
1395 *
1396 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1397 * and that non-texture portion of the GrPaint has already been setup.
1398 */
1399void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001400 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001401 const SkRect& srcRect,
1402 const GrTextureParams& params,
1403 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001404 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001405 bool bicubic,
1406 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001407 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1408 bitmap.height() <= fContext->getMaxTextureSize());
1409
1410 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001411 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001412 if (NULL == texture) {
1413 return;
1414 }
1415
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001416 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001417 SkRect paintRect;
1418 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1419 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1420 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1421 SkScalarMul(srcRect.fTop, hInv),
1422 SkScalarMul(srcRect.fRight, wInv),
1423 SkScalarMul(srcRect.fBottom, hInv));
1424
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001425 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001426 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001427 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001428 // Use a constrained texture domain to avoid color bleeding
1429 SkScalar left, top, right, bottom;
1430 if (srcRect.width() > SK_Scalar1) {
1431 SkScalar border = SK_ScalarHalf / texture->width();
1432 left = paintRect.left() + border;
1433 right = paintRect.right() - border;
1434 } else {
1435 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1436 }
1437 if (srcRect.height() > SK_Scalar1) {
1438 SkScalar border = SK_ScalarHalf / texture->height();
1439 top = paintRect.top() + border;
1440 bottom = paintRect.bottom() - border;
1441 } else {
1442 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1443 }
1444 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001445 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001446 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001447 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001448 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001449 SkMatrix::I(),
1450 textureDomain,
1451 GrTextureDomain::kClamp_Mode,
1452 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001453 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001454 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001455 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1456 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001457 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001458 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001459 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001460 }
1461
1462 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1463 // the rest from the SkPaint.
1464 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001465 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001466 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001467 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1468 SkColor2GrColor(paint.getColor());
joshualitt25d9c152015-02-18 12:29:52 -08001469 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001470
joshualitt570d2f82015-02-25 13:19:48 -08001471 fContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dstRect,
1472 paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001473}
1474
fmalita2d97bc12014-11-20 10:44:58 -08001475bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
1476 const SkImageFilter* filter,
1477 const SkImageFilter::Context& ctx,
1478 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001479 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001480
1481 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1482 // (https://code.google.com/p/skia/issues/detail?id=3148).
1483 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001484
1485 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001486 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001487 } else {
1488 return false;
1489 }
1490}
1491
1492void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1493 int left, int top, const SkPaint& paint) {
1494 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001495 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001496
1497 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1498 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1499 return;
1500 }
1501
1502 int w = bitmap.width();
1503 int h = bitmap.height();
1504
1505 GrTexture* texture;
1506 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001507 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001508 if (!texture) {
1509 return;
1510 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001511
1512 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001513 // This bitmap will own the filtered result as a texture.
1514 SkBitmap filteredBitmap;
1515
bsalomon49f085d2014-09-05 13:34:00 -07001516 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001517 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001518 SkMatrix matrix(*draw.fMatrix);
1519 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001520 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001521 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001522 // This cache is transient, and is freed (along with all its contained
1523 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001524 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001525 if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap,
1526 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001527 texture = (GrTexture*) filteredBitmap.getTexture();
1528 w = filteredBitmap.width();
1529 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001530 left += offset.x();
1531 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001532 } else {
1533 return;
1534 }
1535 }
1536
1537 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001538 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001539
joshualitt25d9c152015-02-18 12:29:52 -08001540 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1541 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001542
joshualitt25d9c152015-02-18 12:29:52 -08001543 fContext->drawNonAARectToRect(fRenderTarget,
joshualitt570d2f82015-02-25 13:19:48 -08001544 fClip,
joshualitt25d9c152015-02-18 12:29:52 -08001545 grPaint,
joshualitt16b27892014-12-18 07:47:16 -08001546 SkMatrix::I(),
1547 SkRect::MakeXYWH(SkIntToScalar(left),
1548 SkIntToScalar(top),
1549 SkIntToScalar(w),
1550 SkIntToScalar(h)),
1551 SkRect::MakeXYWH(0,
1552 0,
1553 SK_Scalar1 * w / texture->width(),
1554 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001555}
1556
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001557void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001558 const SkRect* src, const SkRect& dst,
1559 const SkPaint& paint,
1560 SkCanvas::DrawBitmapRectFlags flags) {
1561 SkMatrix matrix;
1562 SkRect bitmapBounds, tmpSrc;
1563
1564 bitmapBounds.set(0, 0,
1565 SkIntToScalar(bitmap.width()),
1566 SkIntToScalar(bitmap.height()));
1567
1568 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001569 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001570 tmpSrc = *src;
1571 } else {
1572 tmpSrc = bitmapBounds;
1573 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001574
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001575 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1576
1577 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001578 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001579 if (!bitmapBounds.contains(tmpSrc)) {
1580 if (!tmpSrc.intersect(bitmapBounds)) {
1581 return; // nothing to draw
1582 }
1583 }
1584 }
1585
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001586 SkRect tmpDst;
1587 matrix.mapRect(&tmpDst, tmpSrc);
1588
1589 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1590 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1591 // Translate so that tempDst's top left is at the origin.
1592 matrix = *origDraw.fMatrix;
1593 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1594 draw.writable()->fMatrix = &matrix;
1595 }
1596 SkSize dstSize;
1597 dstSize.fWidth = tmpDst.width();
1598 dstSize.fHeight = tmpDst.height();
1599
1600 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001601}
1602
1603void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1604 int x, int y, const SkPaint& paint) {
1605 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001606 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001607 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001608
1609 // TODO: If the source device covers the whole of this device, we could
1610 // omit fNeedsClear -related flushing.
1611 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001612
1613 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001614 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001615
1616 GrRenderTarget* devRT = dev->accessRenderTarget();
1617 GrTexture* devTex;
1618 if (NULL == (devTex = devRT->asTexture())) {
1619 return;
1620 }
1621
robertphillips7b9e8a42014-12-11 08:20:31 -08001622 const SkImageInfo ii = dev->imageInfo();
1623 int w = ii.width();
1624 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001625
1626 SkImageFilter* filter = paint.getImageFilter();
1627 // This bitmap will own the filtered result as a texture.
1628 SkBitmap filteredBitmap;
1629
bsalomon49f085d2014-09-05 13:34:00 -07001630 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001631 SkIPoint offset = SkIPoint::Make(0, 0);
1632 SkMatrix matrix(*draw.fMatrix);
1633 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001634 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001635 // This cache is transient, and is freed (along with all its contained
1636 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001637 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001638 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001639 if (this->filterTexture(fContext, devTex, filter, ctx, &filteredBitmap,
1640 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001641 devTex = filteredBitmap.getTexture();
1642 w = filteredBitmap.width();
1643 h = filteredBitmap.height();
1644 x += offset.fX;
1645 y += offset.fY;
1646 } else {
1647 return;
1648 }
1649 }
1650
1651 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001652 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001653
joshualitt25d9c152015-02-18 12:29:52 -08001654 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1655 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001656
1657 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1658 SkIntToScalar(y),
1659 SkIntToScalar(w),
1660 SkIntToScalar(h));
1661
1662 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1663 // scratch texture).
1664 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1665 SK_Scalar1 * h / devTex->height());
1666
joshualitt570d2f82015-02-25 13:19:48 -08001667 fContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, SkMatrix::I(), dstRect,
1668 srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001669}
1670
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001671bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001672 return filter->canFilterImageGPU();
1673}
1674
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001675bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001676 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001677 SkBitmap* result, SkIPoint* offset) {
1678 // want explicitly our impl, so guard against a subclass of us overriding it
1679 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1680 return false;
1681 }
1682
1683 SkAutoLockPixels alp(src, !src.getTexture());
1684 if (!src.getTexture() && !src.readyToDraw()) {
1685 return false;
1686 }
1687
1688 GrTexture* texture;
1689 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1690 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001691 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001692
fmalita2d97bc12014-11-20 10:44:58 -08001693 return this->filterTexture(fContext, texture, filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001694}
1695
1696///////////////////////////////////////////////////////////////////////////////
1697
1698// must be in SkCanvas::VertexMode order
1699static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1700 kTriangles_GrPrimitiveType,
1701 kTriangleStrip_GrPrimitiveType,
1702 kTriangleFan_GrPrimitiveType,
1703};
1704
1705void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1706 int vertexCount, const SkPoint vertices[],
1707 const SkPoint texs[], const SkColor colors[],
1708 SkXfermode* xmode,
1709 const uint16_t indices[], int indexCount,
1710 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001711 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001712 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001713
dandov32a311b2014-07-15 19:46:26 -07001714 const uint16_t* outIndices;
1715 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1716 GrPrimitiveType primType;
1717 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001718
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001719 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1720 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001721
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001722 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001723
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001724 SkPaint copy(paint);
1725 copy.setStyle(SkPaint::kStroke_Style);
1726 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001727
dandov32a311b2014-07-15 19:46:26 -07001728 // we ignore the shader if texs is null.
joshualitt25d9c152015-02-18 12:29:52 -08001729 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy,
1730 SkColor2GrColor(copy.getColor()), NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001731
dandov32a311b2014-07-15 19:46:26 -07001732 primType = kLines_GrPrimitiveType;
1733 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001734 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001735 switch (vmode) {
1736 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001737 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001738 break;
1739 case SkCanvas::kTriangleStrip_VertexMode:
1740 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001741 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001742 break;
1743 }
mtklein533eb782014-08-27 10:39:42 -07001744
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001745 VertState state(vertexCount, indices, indexCount);
1746 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001747
dandov32a311b2014-07-15 19:46:26 -07001748 //number of indices for lines per triangle with kLines
1749 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001750
dandov32a311b2014-07-15 19:46:26 -07001751 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1752 outIndices = outAlloc.get();
1753 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001754 int i = 0;
1755 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001756 auxIndices[i] = state.f0;
1757 auxIndices[i + 1] = state.f1;
1758 auxIndices[i + 2] = state.f1;
1759 auxIndices[i + 3] = state.f2;
1760 auxIndices[i + 4] = state.f2;
1761 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001762 i += 6;
1763 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001764 } else {
dandov32a311b2014-07-15 19:46:26 -07001765 outIndices = indices;
1766 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001767
dandov32a311b2014-07-15 19:46:26 -07001768 if (NULL == texs || NULL == paint.getShader()) {
joshualitt25d9c152015-02-18 12:29:52 -08001769 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1770 SkColor2GrColor(paint.getColor()),
dandov32a311b2014-07-15 19:46:26 -07001771 NULL == colors, &grPaint);
1772 } else {
joshualitt25d9c152015-02-18 12:29:52 -08001773 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix,
1774 NULL == colors, &grPaint);
dandov32a311b2014-07-15 19:46:26 -07001775 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001776 }
1777
mtklein2583b622014-06-04 08:20:41 -07001778#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001779 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001780 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1781 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001782 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001783 }
1784 }
mtklein2583b622014-06-04 08:20:41 -07001785#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001786
1787 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001788 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001789 // need to convert byte order and from non-PM to PM
1790 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001791 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001792 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001793 color = colors[i];
1794 if (paint.getAlpha() != 255) {
1795 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1796 }
1797 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001798 }
1799 colors = convertedColors.get();
1800 }
joshualitt25d9c152015-02-18 12:29:52 -08001801 fContext->drawVertices(fRenderTarget,
joshualitt570d2f82015-02-25 13:19:48 -08001802 fClip,
joshualitt25d9c152015-02-18 12:29:52 -08001803 grPaint,
joshualitt5531d512014-12-17 15:50:11 -08001804 *draw.fMatrix,
dandov32a311b2014-07-15 19:46:26 -07001805 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001806 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001807 vertices,
1808 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001809 colors,
dandov32a311b2014-07-15 19:46:26 -07001810 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001811 indexCount);
1812}
1813
1814///////////////////////////////////////////////////////////////////////////////
1815
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001816void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001817 size_t byteLength, SkScalar x, SkScalar y,
1818 const SkPaint& paint) {
1819 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001820 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001821
jvanverth8c27a182014-10-14 08:45:50 -07001822 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -08001823 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001824
jvanverth8c27a182014-10-14 08:45:50 -07001825 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001826
joshualitt570d2f82015-02-25 13:19:48 -08001827 if (!fTextContext->drawText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
1828 (const char *)text, byteLength, x, y)) {
jvanverth8c27a182014-10-14 08:45:50 -07001829 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001830 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001831 }
1832}
1833
fmalita05c4a432014-09-29 06:29:53 -07001834void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1835 const SkScalar pos[], int scalarsPerPos,
1836 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001837 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001838 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001839
jvanverth8c27a182014-10-14 08:45:50 -07001840 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -08001841 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001842
jvanverth8c27a182014-10-14 08:45:50 -07001843 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001844
joshualitt570d2f82015-02-25 13:19:48 -08001845 if (!fTextContext->drawPosText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
1846 (const char *)text, byteLength, pos, scalarsPerPos, offset)) {
jvanverth8c27a182014-10-14 08:45:50 -07001847 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001848 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001849 }
1850}
1851
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001852///////////////////////////////////////////////////////////////////////////////
1853
reedb2db8982014-11-13 12:41:02 -08001854bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001855 if (paint.getShader() ||
reedb2db8982014-11-13 12:41:02 -08001856 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001857 paint.getMaskFilter() ||
1858 paint.getRasterizer() ||
1859 paint.getColorFilter() ||
1860 paint.getPathEffect() ||
1861 paint.isFakeBoldText() ||
reedb2db8982014-11-13 12:41:02 -08001862 paint.getStyle() != SkPaint::kFill_Style)
1863 {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001864 return true;
1865 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001866 return false;
1867}
1868
1869void SkGpuDevice::flush() {
1870 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001871 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001872}
1873
1874///////////////////////////////////////////////////////////////////////////////
1875
reed76033be2015-03-14 10:54:31 -07001876SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
bsalomonf2703d82014-10-28 14:33:06 -07001877 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001878 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001879 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001880 desc.fWidth = cinfo.fInfo.width();
1881 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001882 desc.fSampleCnt = fRenderTarget->numSamples();
1883
1884 SkAutoTUnref<GrTexture> texture;
1885 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001886 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001887
hcm4396fa52014-10-27 21:43:30 -07001888 // layers are never draw in repeat modes, so we can request an approx
1889 // match and ignore any padding.
reed76033be2015-03-14 10:54:31 -07001890 const GrContext::ScratchTexMatch match = (kNever_TileUsage == cinfo.fTileUsage) ?
hcm4396fa52014-10-27 21:43:30 -07001891 GrContext::kApprox_ScratchTexMatch :
1892 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001893 texture.reset(fContext->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001894
1895 if (texture) {
1896 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry);
1897 return SkGpuDevice::Create(texture->asRenderTarget(), &props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001898 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001899 SkErrorInternals::SetError( kInternalError_SkError,
1900 "---- failed to create compatible device texture [%d %d]\n",
1901 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001902 return NULL;
1903 }
1904}
1905
reed4a8126e2014-09-22 07:29:03 -07001906SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001907 // TODO: Change the signature of newSurface to take a budgeted parameter.
1908 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
1909 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->numSamples(),
1910 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001911}
1912
robertphillips30d2cc62014-09-24 08:52:18 -07001913bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001914 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001915#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001916 // todo: should handle this natively
1917 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001918 return false;
1919 }
1920
robertphillips82365912014-11-12 09:32:34 -08001921 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips81f71b62014-11-11 04:54:49 -08001922
1923 const SkPicture::AccelData* data = mainPicture->EXPERIMENTAL_getAccelData(key);
1924 if (!data) {
1925 return false;
1926 }
1927
robertphillipse5524cd2015-02-20 12:30:26 -08001928 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1929 if (0 == gpuData->numBlocks()) {
1930 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001931 }
1932
robertphillipsfd61ed02014-10-28 07:21:44 -07001933 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001934
robertphillipse5524cd2015-02-20 12:30:26 -08001935 SkIRect iBounds;
1936 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1937 return false;
1938 }
1939
1940 SkRect clipBounds = SkRect::Make(iBounds);
1941
1942 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1943
robertphillipsfd61ed02014-10-28 07:21:44 -07001944 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001945 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001946 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001947 &atlasedNeedRendering, &atlasedRecycled,
1948 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001949
1950 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1951
1952 SkTDArray<GrHoistedLayer> needRendering, recycled;
1953
robertphillipse5524cd2015-02-20 12:30:26 -08001954 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1955
robertphillipsfd61ed02014-10-28 07:21:44 -07001956 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001957 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001958 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001959 &needRendering, &recycled,
1960 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001961
1962 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001963
robertphillips64bf7672014-08-21 13:07:35 -07001964 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001965 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1966 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001967
robertphillipsfd61ed02014-10-28 07:21:44 -07001968 GrLayerHoister::UnlockLayers(fContext, needRendering);
1969 GrLayerHoister::UnlockLayers(fContext, recycled);
1970 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1971 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001972
1973 return true;
robertphillips63242d72014-12-04 08:31:02 -08001974#else
1975 return false;
1976#endif
robertphillips64bf7672014-08-21 13:07:35 -07001977}
1978
senorblancobe129b22014-08-08 07:14:35 -07001979SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001980 // We always return a transient cache, so it is freed after each
1981 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001982 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001983}
reedf037e0b2014-10-30 11:34:15 -07001984
1985#endif