blob: ad633931eac18ce4895bf8efcf1880902937541a [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
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000223 if (fContext->getClip() == &fClipData) {
224 fContext->setClip(NULL);
225 }
226
bsalomon32d0b3b2014-08-29 07:50:23 -0700227 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000228 fContext->unref();
229}
230
231///////////////////////////////////////////////////////////////////////////////
232
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000233bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
234 int x, int y) {
235 DO_DEFERRED_CLEAR();
236
237 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000238 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000239 if (kUnknown_GrPixelConfig == config) {
240 return false;
241 }
242
243 uint32_t flags = 0;
244 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
245 flags = GrContext::kUnpremul_PixelOpsFlag;
246 }
247 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
248 config, dstPixels, dstRowBytes, flags);
249}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000250
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000251bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
252 int x, int y) {
253 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000254 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000255 if (kUnknown_GrPixelConfig == config) {
256 return false;
257 }
258 uint32_t flags = 0;
259 if (kUnpremul_SkAlphaType == info.alphaType()) {
260 flags = GrContext::kUnpremul_PixelOpsFlag;
261 }
262 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
263
264 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700265 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000266
267 return true;
268}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000269
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000270const SkBitmap& SkGpuDevice::onAccessBitmap() {
271 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700272 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000273}
274
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000275void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
276 INHERITED::onAttachToCanvas(canvas);
277
278 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualittde358a92015-02-05 08:19:35 -0800279 fClipData.fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000280}
281
282void SkGpuDevice::onDetachFromCanvas() {
283 INHERITED::onDetachFromCanvas();
joshualittde358a92015-02-05 08:19:35 -0800284 fClipData.fClipStack.reset(NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000285}
286
287// call this every draw call, to ensure that the context reflects our state,
288// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800289void SkGpuDevice::prepareDraw(const SkDraw& draw) {
bsalomon49f085d2014-09-05 13:34:00 -0700290 SkASSERT(fClipData.fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000291
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000292 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
293
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000294 fClipData.fOrigin = this->getOrigin();
295
296 fContext->setClip(&fClipData);
297
298 DO_DEFERRED_CLEAR();
299}
300
301GrRenderTarget* SkGpuDevice::accessRenderTarget() {
302 DO_DEFERRED_CLEAR();
303 return fRenderTarget;
304}
305
reed8eddfb52014-12-04 07:50:14 -0800306void SkGpuDevice::clearAll() {
307 GrColor color = 0;
308 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
309 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
310 fContext->clear(&rect, color, true, fRenderTarget);
bsalomonafe30052015-01-16 07:32:33 -0800311 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800312}
313
kkinnunenabcfab42015-02-22 22:53:44 -0800314void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
315 // Caller must have accessed the render target, because it knows the rt must be replaced.
316 SkASSERT(!fNeedClear);
317
318 SkSurface::Budgeted budgeted =
319 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
320 : SkSurface::kNo_Budgeted;
321
322 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
323 fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->numSamples()));
324
325 if (NULL == newRT) {
326 return;
327 }
328
329 if (shouldRetainContent) {
330 if (fRenderTarget->wasDestroyed()) {
331 return;
332 }
333 this->context()->copySurface(newRT, fRenderTarget);
334 }
335
336 SkASSERT(fRenderTarget != newRT);
337
338 fRenderTarget->unref();
339 fRenderTarget = newRT.detach();
340
341 SkASSERT(fRenderTarget->surfacePriv().info() == fLegacyBitmap.info());
342 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (fRenderTarget->surfacePriv().info(), fRenderTarget));
343 fLegacyBitmap.setPixelRef(pr)->unref();
344}
345
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000346///////////////////////////////////////////////////////////////////////////////
347
348SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
349SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
350SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
351SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
352SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
353 shader_type_mismatch);
354SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
355 shader_type_mismatch);
356SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
357SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
358
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000359///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000360
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000361void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800362 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700363 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000364
365 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800366 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000367
joshualitt25d9c152015-02-18 12:29:52 -0800368 fContext->drawPaint(fRenderTarget, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000369}
370
371// must be in SkCanvas::PointMode order
372static const GrPrimitiveType gPointMode2PrimtiveType[] = {
373 kPoints_GrPrimitiveType,
374 kLines_GrPrimitiveType,
375 kLineStrip_GrPrimitiveType
376};
377
378void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
379 size_t count, const SkPoint pts[], const SkPaint& paint) {
380 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800381 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000382
383 SkScalar width = paint.getStrokeWidth();
384 if (width < 0) {
385 return;
386 }
387
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000388 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700389 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
390 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800391 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
egdaniele61c4112014-06-12 10:24:21 -0700392 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700393 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700394 path.moveTo(pts[0]);
395 path.lineTo(pts[1]);
joshualitt25d9c152015-02-18 12:29:52 -0800396 fContext->drawPath(fRenderTarget, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700397 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000398 }
399
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000400 // we only handle hairlines and paints without path effects or mask filters,
401 // else we let the SkDraw call our drawPath()
402 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
403 draw.drawPoints(mode, count, pts, paint, true);
404 return;
405 }
406
407 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800408 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000409
joshualitt25d9c152015-02-18 12:29:52 -0800410 fContext->drawVertices(fRenderTarget,
411 grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800412 *draw.fMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000413 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000414 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000415 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000416 NULL,
417 NULL,
418 NULL,
419 0);
420}
421
422///////////////////////////////////////////////////////////////////////////////
423
424void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
425 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700426 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
427
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000428 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800429 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000430
431 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
432 SkScalar width = paint.getStrokeWidth();
433
434 /*
435 We have special code for hairline strokes, miter-strokes, bevel-stroke
436 and fills. Anything else we just call our path code.
437 */
438 bool usePath = doStroke && width > 0 &&
439 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
440 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
441 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700442
443 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000444 usePath = true;
445 }
egdanield58a0ba2014-06-11 10:30:05 -0700446
joshualitt5531d512014-12-17 15:50:11 -0800447 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000448#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
449 if (doStroke) {
450#endif
451 usePath = true;
452#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
453 } else {
joshualitt5531d512014-12-17 15:50:11 -0800454 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000455 }
456#endif
457 }
458 // until we can both stroke and fill rectangles
459 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
460 usePath = true;
461 }
462
egdanield58a0ba2014-06-11 10:30:05 -0700463 GrStrokeInfo strokeInfo(paint);
464
465 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700466 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700467 usePath = true;
468 }
469
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000470 if (usePath) {
471 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700472 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000473 path.addRect(rect);
474 this->drawPath(draw, path, paint, NULL, true);
475 return;
476 }
477
478 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800479 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400480
joshualitt25d9c152015-02-18 12:29:52 -0800481 fContext->drawRect(fRenderTarget, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000482}
483
484///////////////////////////////////////////////////////////////////////////////
485
486void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800487 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700488 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000489 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800490 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000491
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000492 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800493 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400494
egdanield58a0ba2014-06-11 10:30:05 -0700495 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000496 if (paint.getMaskFilter()) {
497 // try to hit the fast path for drawing filtered round rects
498
499 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800500 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000501 if (devRRect.allCornersCircular()) {
502 SkRect maskRect;
503 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800504 draw.fClip->getBounds(),
505 *draw.fMatrix,
506 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000507 SkIRect finalIRect;
508 maskRect.roundOut(&finalIRect);
509 if (draw.fClip->quickReject(finalIRect)) {
510 // clipped out
511 return;
512 }
joshualitt25d9c152015-02-18 12:29:52 -0800513 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
514 fRenderTarget,
515 &grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800516 *draw.fMatrix,
egdanield58a0ba2014-06-11 10:30:05 -0700517 strokeInfo.getStrokeRec(),
518 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000519 return;
520 }
521 }
522
523 }
524 }
525
526 }
527
egdanield58a0ba2014-06-11 10:30:05 -0700528 bool usePath = false;
529
530 if (paint.getMaskFilter()) {
531 usePath = true;
532 } else {
533 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700534 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700535 usePath = true;
536 }
537 }
538
539
540 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700542 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000543 path.addRRect(rect);
544 this->drawPath(draw, path, paint, NULL, true);
545 return;
546 }
Mike Klein744fb732014-06-23 15:13:26 -0400547
joshualitt25d9c152015-02-18 12:29:52 -0800548 fContext->drawRRect(fRenderTarget, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000549}
550
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000551void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800552 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000553 SkStrokeRec stroke(paint);
554 if (stroke.isFillStyle()) {
555
556 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800557 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000558
559 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800560 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000561
562 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
joshualitt25d9c152015-02-18 12:29:52 -0800563 fContext->drawDRRect(fRenderTarget, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000564 return;
565 }
566 }
567
568 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700569 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000570 path.addRRect(outer);
571 path.addRRect(inner);
572 path.setFillType(SkPath::kEvenOdd_FillType);
573
574 this->drawPath(draw, path, paint, NULL, true);
575}
576
577
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000578/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000579
580void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
581 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700582 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000583 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800584 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000585
egdanield58a0ba2014-06-11 10:30:05 -0700586 GrStrokeInfo strokeInfo(paint);
587
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000588 bool usePath = false;
589 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700590 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000591 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700592 } else {
593 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700594 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700595 usePath = true;
596 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000597 }
598
599 if (usePath) {
600 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700601 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000602 path.addOval(oval);
603 this->drawPath(draw, path, paint, NULL, true);
604 return;
605 }
606
607 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800608 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000609
joshualitt25d9c152015-02-18 12:29:52 -0800610 fContext->drawOval(fRenderTarget, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000611}
612
613#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000614
615///////////////////////////////////////////////////////////////////////////////
616
617// helpers for applying mask filters
618namespace {
619
620// Draw a mask using the supplied paint. Since the coverage/geometry
621// is already burnt into the mask this boils down to a rect draw.
622// Return true if the mask was successfully drawn.
joshualitt25d9c152015-02-18 12:29:52 -0800623bool draw_mask(GrContext* context,
624 GrRenderTarget* rt,
625 const SkMatrix& viewMatrix,
626 const SkRect& maskRect,
627 GrPaint* grp,
628 GrTexture* mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000629 SkMatrix matrix;
630 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
631 matrix.postIDiv(mask->width(), mask->height());
632
joshualitt16b27892014-12-18 07:47:16 -0800633 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix,
634 kDevice_GrCoordSet))->unref();
635
636 SkMatrix inverse;
637 if (!viewMatrix.invert(&inverse)) {
638 return false;
639 }
joshualitt25d9c152015-02-18 12:29:52 -0800640 context->drawNonAARectWithLocalMatrix(rt, *grp, SkMatrix::I(), maskRect, inverse);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000641 return true;
642}
643
joshualitt25d9c152015-02-18 12:29:52 -0800644bool draw_with_mask_filter(GrContext* context,
645 GrRenderTarget* rt,
646 const SkMatrix& viewMatrix,
647 const SkPath& devPath,
648 SkMaskFilter* filter,
649 const SkRegion& clip,
650 GrPaint* grp,
651 SkPaint::Style style) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000652 SkMask srcM, dstM;
653
joshualitt5531d512014-12-17 15:50:11 -0800654 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &viewMatrix, &srcM,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000655 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
656 return false;
657 }
658 SkAutoMaskFreeImage autoSrc(srcM.fImage);
659
joshualitt5531d512014-12-17 15:50:11 -0800660 if (!filter->filterMask(&dstM, srcM, viewMatrix, NULL)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000661 return false;
662 }
663 // this will free-up dstM when we're done (allocated in filterMask())
664 SkAutoMaskFreeImage autoDst(dstM.fImage);
665
666 if (clip.quickReject(dstM.fBounds)) {
667 return false;
668 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000669
670 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
671 // the current clip (and identity matrix) and GrPaint settings
bsalomonf2703d82014-10-28 14:33:06 -0700672 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000673 desc.fWidth = dstM.fBounds.width();
674 desc.fHeight = dstM.fBounds.height();
675 desc.fConfig = kAlpha_8_GrPixelConfig;
676
bsalomone3059732014-10-14 11:47:22 -0700677 SkAutoTUnref<GrTexture> texture(
678 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
679 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000680 return false;
681 }
682 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
683 dstM.fImage, dstM.fRowBytes);
684
685 SkRect maskRect = SkRect::Make(dstM.fBounds);
686
joshualitt25d9c152015-02-18 12:29:52 -0800687 return draw_mask(context, rt, viewMatrix, maskRect, grp, texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000688}
689
bsalomone3059732014-10-14 11:47:22 -0700690// Create a mask of 'devPath' and place the result in 'mask'.
691GrTexture* create_mask_GPU(GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800692 GrRenderTarget* rt,
bsalomone3059732014-10-14 11:47:22 -0700693 const SkRect& maskRect,
694 const SkPath& devPath,
695 const GrStrokeInfo& strokeInfo,
696 bool doAA,
697 int sampleCnt) {
bsalomonf2703d82014-10-28 14:33:06 -0700698 GrSurfaceDesc desc;
699 desc.fFlags = kRenderTarget_GrSurfaceFlag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000700 desc.fWidth = SkScalarCeilToInt(maskRect.width());
701 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700702 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000703 // We actually only need A8, but it often isn't supported as a
704 // render target so default to RGBA_8888
705 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700706
707 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
708 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000709 desc.fConfig = kAlpha_8_GrPixelConfig;
710 }
711
bsalomone3059732014-10-14 11:47:22 -0700712 GrTexture* mask = context->refScratchTexture(desc,GrContext::kApprox_ScratchTexMatch);
713 if (NULL == mask) {
714 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000715 }
716
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000717 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
718
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000719 GrContext::AutoClip ac(context, clipRect);
720
bsalomon89c62982014-11-03 12:08:42 -0800721 context->clear(NULL, 0x0, true, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000722
723 GrPaint tempPaint;
egdanielb197b8f2015-02-17 07:34:43 -0800724 tempPaint.setAntiAlias(doAA);
725 tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000726
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000727 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
728 SkMatrix translate;
729 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
joshualitt25d9c152015-02-18 12:29:52 -0800730 context->drawPath(mask->asRenderTarget(), tempPaint, translate, devPath, strokeInfo);
bsalomone3059732014-10-14 11:47:22 -0700731 return mask;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000732}
733
734SkBitmap wrap_texture(GrTexture* texture) {
735 SkBitmap result;
bsalomonafbf2d62014-09-30 12:18:44 -0700736 result.setInfo(texture->surfacePriv().info());
reed6c225732014-06-09 19:52:07 -0700737 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000738 return result;
739}
740
741};
742
743void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
744 const SkPaint& paint, const SkMatrix* prePathMatrix,
745 bool pathIsMutable) {
746 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800747 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700748 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000749
jvanverthb3eb6872014-10-24 07:12:51 -0700750 SkASSERT(!pathIsMutable || origSrcPath.isVolatile());
joshualitt5531d512014-12-17 15:50:11 -0800751
bsalomon54443932015-01-29 09:34:18 -0800752 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000753
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000754 // If we have a prematrix, apply it to the path, optimizing for the case
755 // where the original path can in fact be modified in place (even though
756 // its parameter type is const).
757 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000758 SkTLazy<SkPath> tmpPath;
759 SkTLazy<SkPath> effectPath;
bsalomon54443932015-01-29 09:34:18 -0800760 SkPathEffect* pathEffect = paint.getPathEffect();
761
762 SkMatrix viewMatrix = *draw.fMatrix;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000763
764 if (prePathMatrix) {
bsalomon54443932015-01-29 09:34:18 -0800765 // stroking and path effects are supposed to be applied *after* the prePathMatrix.
766 // The pre-path-matrix also should not affect shadeing.
767 if (NULL == pathEffect && NULL == paint.getShader() &&
768 (strokeInfo.getStrokeRec().isFillStyle() ||
769 strokeInfo.getStrokeRec().isHairlineStyle())) {
770 viewMatrix.preConcat(*prePathMatrix);
771 } else {
772 SkPath* result = pathPtr;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773
bsalomon54443932015-01-29 09:34:18 -0800774 if (!pathIsMutable) {
775 result = tmpPath.init();
776 result->setIsVolatile(true);
777 pathIsMutable = true;
778 }
779 // should I push prePathMatrix on our MV stack temporarily, instead
780 // of applying it here? See SkDraw.cpp
781 pathPtr->transform(*prePathMatrix, result);
782 pathPtr = result;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000783 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000784 }
785 // at this point we're done with prePathMatrix
786 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
787
bsalomon54443932015-01-29 09:34:18 -0800788 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800789 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, viewMatrix, true, &grPaint);
bsalomon54443932015-01-29 09:34:18 -0800790
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000791 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700792 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
793 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000794 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000795 pathPtr = effectPath.get();
796 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700797 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000798 }
799
egdanield58a0ba2014-06-11 10:30:05 -0700800 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000801 if (paint.getMaskFilter()) {
802 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000803 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
804 if (stroke.applyToPath(strokedPath, *pathPtr)) {
805 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000806 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700807 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000808 }
809 }
810
811 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000812 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700813 if (!pathIsMutable) {
814 devPathPtr->setIsVolatile(true);
815 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000816
817 // transform the path into device space
bsalomon54443932015-01-29 09:34:18 -0800818 pathPtr->transform(viewMatrix, devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000819
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000820 SkRect maskRect;
821 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
822 draw.fClip->getBounds(),
bsalomon54443932015-01-29 09:34:18 -0800823 viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000824 &maskRect)) {
825 SkIRect finalIRect;
826 maskRect.roundOut(&finalIRect);
827 if (draw.fClip->quickReject(finalIRect)) {
828 // clipped out
829 return;
830 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000831
joshualitt25d9c152015-02-18 12:29:52 -0800832 if (paint.getMaskFilter()->directFilterMaskGPU(fContext,
833 fRenderTarget,
834 &grPaint,
835 viewMatrix,
836 stroke,
837 *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000838 // the mask filter was able to draw itself directly, so there's nothing
839 // left to do.
840 return;
841 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000842
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000843
joshualitt25d9c152015-02-18 12:29:52 -0800844 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext,
845 fRenderTarget,
846 maskRect,
847 *devPathPtr,
848 strokeInfo,
849 grPaint.isAntiAlias(),
bsalomone3059732014-10-14 11:47:22 -0700850 fRenderTarget->numSamples()));
851 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000852 GrTexture* filtered;
853
bsalomon54443932015-01-29 09:34:18 -0800854 if (paint.getMaskFilter()->filterMaskGPU(mask, viewMatrix, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000855 // filterMaskGPU gives us ownership of a ref to the result
856 SkAutoTUnref<GrTexture> atu(filtered);
joshualitt25d9c152015-02-18 12:29:52 -0800857 if (draw_mask(fContext, fRenderTarget, viewMatrix, maskRect, &grPaint,
858 filtered)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000859 // This path is completely drawn
860 return;
861 }
862 }
863 }
864 }
865
866 // draw the mask on the CPU - this is a fallthrough path in case the
867 // GPU path fails
868 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
869 SkPaint::kFill_Style;
joshualitt25d9c152015-02-18 12:29:52 -0800870 draw_with_mask_filter(fContext, fRenderTarget, viewMatrix, *devPathPtr,
871 paint.getMaskFilter(), *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000872 return;
873 }
874
joshualitt25d9c152015-02-18 12:29:52 -0800875 fContext->drawPath(fRenderTarget, grPaint, viewMatrix, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000876}
877
878static const int kBmpSmallTileSize = 1 << 10;
879
880static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
881 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
882 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
883 return tilesX * tilesY;
884}
885
886static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
887 if (maxTileSize <= kBmpSmallTileSize) {
888 return maxTileSize;
889 }
890
891 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
892 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
893
894 maxTileTotalTileSize *= maxTileSize * maxTileSize;
895 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
896
897 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
898 return kBmpSmallTileSize;
899 } else {
900 return maxTileSize;
901 }
902}
903
904// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
905// pixels from the bitmap are necessary.
906static void determine_clipped_src_rect(const GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800907 const GrRenderTarget* rt,
joshualitt5531d512014-12-17 15:50:11 -0800908 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000909 const SkBitmap& bitmap,
910 const SkRect* srcRectPtr,
911 SkIRect* clippedSrcIRect) {
912 const GrClipData* clip = context->getClip();
joshualitt25d9c152015-02-18 12:29:52 -0800913 clip->getConservativeBounds(rt, clippedSrcIRect, NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000914 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800915 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000916 clippedSrcIRect->setEmpty();
917 return;
918 }
919 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
920 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700921 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000922 // we've setup src space 0,0 to map to the top left of the src rect.
923 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000924 if (!clippedSrcRect.intersect(*srcRectPtr)) {
925 clippedSrcIRect->setEmpty();
926 return;
927 }
928 }
929 clippedSrcRect.roundOut(clippedSrcIRect);
930 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
931 if (!clippedSrcIRect->intersect(bmpBounds)) {
932 clippedSrcIRect->setEmpty();
933 }
934}
935
936bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800937 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000938 const GrTextureParams& params,
939 const SkRect* srcRectPtr,
940 int maxTileSize,
941 int* tileSize,
942 SkIRect* clippedSrcRect) const {
943 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700944 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000945 return false;
946 }
947
948 // if it's larger than the max tile size, then we have no choice but tiling.
949 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt25d9c152015-02-18 12:29:52 -0800950 determine_clipped_src_rect(fContext, fRenderTarget, viewMatrix, bitmap, srcRectPtr,
951 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000952 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
953 return true;
954 }
955
956 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
957 return false;
958 }
959
960 // if the entire texture is already in our cache then no reason to tile it
961 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
962 return false;
963 }
964
965 // At this point we know we could do the draw by uploading the entire bitmap
966 // as a texture. However, if the texture would be large compared to the
967 // cache size and we don't require most of it for this draw then tile to
968 // reduce the amount of upload and cache spill.
969
970 // assumption here is that sw bitmap size is a good proxy for its size as
971 // a texture
972 size_t bmpSize = bitmap.getSize();
973 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000974 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000975 if (bmpSize < cacheSize / 2) {
976 return false;
977 }
978
979 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt25d9c152015-02-18 12:29:52 -0800980 determine_clipped_src_rect(fContext, fRenderTarget, viewMatrix, bitmap, srcRectPtr,
981 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
983 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
984 kBmpSmallTileSize * kBmpSmallTileSize;
985
986 return usedTileBytes < 2 * bmpSize;
987}
988
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000989void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000990 const SkBitmap& bitmap,
991 const SkMatrix& m,
992 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000993 SkMatrix concat;
994 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
995 if (!m.isIdentity()) {
996 concat.setConcat(*draw->fMatrix, m);
997 draw.writable()->fMatrix = &concat;
998 }
999 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001000}
1001
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001002// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001003// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1004// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001005static inline void clamped_outset_with_offset(SkIRect* iRect,
1006 int outset,
1007 SkPoint* offset,
1008 const SkIRect& clamp) {
1009 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001010
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001011 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1012 if (leftClampDelta > 0) {
1013 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001014 iRect->fLeft = clamp.fLeft;
1015 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001016 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001017 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001018
1019 int topClampDelta = clamp.fTop - iRect->fTop;
1020 if (topClampDelta > 0) {
1021 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001022 iRect->fTop = clamp.fTop;
1023 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001024 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001025 }
1026
1027 if (iRect->fRight > clamp.fRight) {
1028 iRect->fRight = clamp.fRight;
1029 }
1030 if (iRect->fBottom > clamp.fBottom) {
1031 iRect->fBottom = clamp.fBottom;
1032 }
1033}
1034
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001035static bool has_aligned_samples(const SkRect& srcRect,
1036 const SkRect& transformedRect) {
1037 // detect pixel disalignment
1038 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1039 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1040 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1041 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1042 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1043 COLOR_BLEED_TOLERANCE &&
1044 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1045 COLOR_BLEED_TOLERANCE) {
1046 return true;
1047 }
1048 return false;
1049}
1050
1051static bool may_color_bleed(const SkRect& srcRect,
1052 const SkRect& transformedRect,
1053 const SkMatrix& m) {
1054 // Only gets called if has_aligned_samples returned false.
1055 // So we can assume that sampling is axis aligned but not texel aligned.
1056 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1057 SkRect innerSrcRect(srcRect), innerTransformedRect,
1058 outerTransformedRect(transformedRect);
1059 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1060 m.mapRect(&innerTransformedRect, innerSrcRect);
1061
1062 // The gap between outerTransformedRect and innerTransformedRect
1063 // represents the projection of the source border area, which is
1064 // problematic for color bleeding. We must check whether any
1065 // destination pixels sample the border area.
1066 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1067 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1068 SkIRect outer, inner;
1069 outerTransformedRect.round(&outer);
1070 innerTransformedRect.round(&inner);
1071 // If the inner and outer rects round to the same result, it means the
1072 // border does not overlap any pixel centers. Yay!
1073 return inner != outer;
1074}
1075
1076static bool needs_texture_domain(const SkBitmap& bitmap,
1077 const SkRect& srcRect,
1078 GrTextureParams &params,
1079 const SkMatrix& contextMatrix,
1080 bool bicubic) {
1081 bool needsTextureDomain = false;
1082
1083 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1084 // Need texture domain if drawing a sub rect
1085 needsTextureDomain = srcRect.width() < bitmap.width() ||
1086 srcRect.height() < bitmap.height();
1087 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1088 // sampling is axis-aligned
1089 SkRect transformedRect;
1090 contextMatrix.mapRect(&transformedRect, srcRect);
1091
1092 if (has_aligned_samples(srcRect, transformedRect)) {
1093 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1094 needsTextureDomain = false;
1095 } else {
1096 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1097 }
1098 }
1099 }
1100 return needsTextureDomain;
1101}
1102
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001103void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1104 const SkBitmap& bitmap,
1105 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001106 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001107 const SkPaint& paint,
1108 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -08001109 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001110
1111 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001112 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001113 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1114 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001115 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001116 SkScalar w = SkIntToScalar(bitmap.width());
1117 SkScalar h = SkIntToScalar(bitmap.height());
1118 dstSize.fWidth = w;
1119 dstSize.fHeight = h;
1120 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001121 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001122 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001123 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001124 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001125 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001126 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1127 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1128 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1129 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001130 }
1131
derekf367e1862014-12-02 11:02:06 -08001132 // If the render target is not msaa and draw is antialiased, we call
1133 // drawRect instead of drawing on the render target directly.
1134 // FIXME: the tiled bitmap code path doesn't currently support
1135 // anti-aliased edges, we work around that for now by drawing directly
1136 // if the image size exceeds maximum texture size.
1137 int maxTextureSize = fContext->getMaxTextureSize();
1138 bool directDraw = fRenderTarget->isMultisampled() ||
1139 !paint.isAntiAlias() ||
1140 bitmap.width() > maxTextureSize ||
1141 bitmap.height() > maxTextureSize;
1142
1143 // we check whether dst rect are pixel aligned
1144 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -08001145 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -08001146
1147 if (staysRect) {
1148 SkRect rect;
1149 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -08001150 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -08001151 const SkScalar *scalars = rect.asScalars();
1152 bool isDstPixelAligned = true;
1153 for (int i = 0; i < 4; i++) {
1154 if (!SkScalarIsInt(scalars[i])) {
1155 isDstPixelAligned = false;
1156 break;
1157 }
1158 }
1159
1160 if (isDstPixelAligned)
1161 directDraw = true;
1162 }
1163 }
1164
1165 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001166 // Convert the bitmap to a shader so that the rect can be drawn
1167 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001168 SkBitmap tmp; // subset of bitmap, if necessary
1169 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001170 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001171 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001172 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1173 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1174 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001175 // In bleed mode we position and trim the bitmap based on the src rect which is
1176 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1177 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1178 // compensate.
1179 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1180 SkIRect iSrc;
1181 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001182
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001183 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1184 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001185
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001186 if (!bitmap.extractSubset(&tmp, iSrc)) {
1187 return; // extraction failed
1188 }
1189 bitmapPtr = &tmp;
1190 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001191
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001192 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001193 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001194 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001195 } else {
1196 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001197 }
1198
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001199 SkPaint paintWithShader(paint);
1200 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001201 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001202 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1203 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001204
1205 return;
1206 }
1207
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001208 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1209 // the view matrix rather than a local matrix.
1210 SkMatrix m;
1211 m.setScale(dstSize.fWidth / srcRect.width(),
1212 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -08001213 SkMatrix viewM = *draw.fMatrix;
1214 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001215
1216 GrTextureParams params;
1217 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1218 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001219
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001220 bool doBicubic = false;
1221
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001222 switch(paintFilterLevel) {
1223 case SkPaint::kNone_FilterLevel:
1224 textureFilterMode = GrTextureParams::kNone_FilterMode;
1225 break;
1226 case SkPaint::kLow_FilterLevel:
1227 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1228 break;
1229 case SkPaint::kMedium_FilterLevel:
joshualitt5531d512014-12-17 15:50:11 -08001230 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001231 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1232 } else {
1233 // Don't trigger MIP level generation unnecessarily.
1234 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1235 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001236 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001237 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001238 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001239 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001240 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001241 break;
1242 default:
1243 SkErrorInternals::SetError( kInvalidPaint_SkError,
1244 "Sorry, I don't understand the filtering "
1245 "mode you asked for. Falling back to "
1246 "MIPMaps.");
1247 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1248 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001249 }
1250
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001251 int tileFilterPad;
1252 if (doBicubic) {
1253 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1254 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1255 tileFilterPad = 0;
1256 } else {
1257 tileFilterPad = 1;
1258 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001259 params.setFilterMode(textureFilterMode);
1260
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001261 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001262 int tileSize;
1263
1264 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001265 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001266 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001267 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1268 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001269 } else {
1270 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001271 bool needsTextureDomain = needs_texture_domain(bitmap,
1272 srcRect,
1273 params,
joshualitt5531d512014-12-17 15:50:11 -08001274 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001275 doBicubic);
1276 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001277 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001278 srcRect,
1279 params,
1280 paint,
1281 flags,
1282 doBicubic,
1283 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001284 }
1285}
1286
1287// Break 'bitmap' into several tiles to draw it since it has already
1288// been determined to be too large to fit in VRAM
1289void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001290 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001291 const SkRect& srcRect,
1292 const SkIRect& clippedSrcIRect,
1293 const GrTextureParams& params,
1294 const SkPaint& paint,
1295 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001296 int tileSize,
1297 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001298 // The following pixel lock is technically redundant, but it is desirable
1299 // to lock outside of the tile loop to prevent redecoding the whole image
1300 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1301 // is larger than the limit of the discardable memory pool.
1302 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001303 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1304
1305 int nx = bitmap.width() / tileSize;
1306 int ny = bitmap.height() / tileSize;
1307 for (int x = 0; x <= nx; x++) {
1308 for (int y = 0; y <= ny; y++) {
1309 SkRect tileR;
1310 tileR.set(SkIntToScalar(x * tileSize),
1311 SkIntToScalar(y * tileSize),
1312 SkIntToScalar((x + 1) * tileSize),
1313 SkIntToScalar((y + 1) * tileSize));
1314
1315 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1316 continue;
1317 }
1318
1319 if (!tileR.intersect(srcRect)) {
1320 continue;
1321 }
1322
1323 SkBitmap tmpB;
1324 SkIRect iTileR;
1325 tileR.roundOut(&iTileR);
1326 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1327 SkIntToScalar(iTileR.fTop));
1328
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001329 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001330 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001331 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001332 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001333 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001334
robertphillipsec8bb942014-11-21 10:16:25 -08001335 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001336 SkIRect iClampRect;
1337
1338 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1339 // In bleed mode we want to always expand the tile on all edges
1340 // but stay within the bitmap bounds
1341 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1342 } else {
1343 // In texture-domain/clamp mode we only want to expand the
1344 // tile on edges interior to "srcRect" (i.e., we want to
1345 // not bleed across the original clamped edges)
1346 srcRect.roundOut(&iClampRect);
1347 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001348 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1349 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001350 }
1351
1352 if (bitmap.extractSubset(&tmpB, iTileR)) {
1353 // now offset it to make it "local" to our tmp bitmap
1354 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001355 GrTextureParams paramsTemp = params;
1356 bool needsTextureDomain = needs_texture_domain(bitmap,
1357 srcRect,
1358 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001359 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001360 bicubic);
1361 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001362 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001363 tileR,
1364 paramsTemp,
1365 paint,
1366 flags,
1367 bicubic,
1368 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001369 }
1370 }
1371 }
1372}
1373
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001374
1375/*
1376 * This is called by drawBitmap(), which has to handle images that may be too
1377 * large to be represented by a single texture.
1378 *
1379 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1380 * and that non-texture portion of the GrPaint has already been setup.
1381 */
1382void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001383 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001384 const SkRect& srcRect,
1385 const GrTextureParams& params,
1386 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001387 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001388 bool bicubic,
1389 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001390 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1391 bitmap.height() <= fContext->getMaxTextureSize());
1392
1393 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001394 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001395 if (NULL == texture) {
1396 return;
1397 }
1398
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001399 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001400 SkRect paintRect;
1401 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1402 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1403 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1404 SkScalarMul(srcRect.fTop, hInv),
1405 SkScalarMul(srcRect.fRight, wInv),
1406 SkScalarMul(srcRect.fBottom, hInv));
1407
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001408 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001409 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001410 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001411 // Use a constrained texture domain to avoid color bleeding
1412 SkScalar left, top, right, bottom;
1413 if (srcRect.width() > SK_Scalar1) {
1414 SkScalar border = SK_ScalarHalf / texture->width();
1415 left = paintRect.left() + border;
1416 right = paintRect.right() - border;
1417 } else {
1418 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1419 }
1420 if (srcRect.height() > SK_Scalar1) {
1421 SkScalar border = SK_ScalarHalf / texture->height();
1422 top = paintRect.top() + border;
1423 bottom = paintRect.bottom() - border;
1424 } else {
1425 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1426 }
1427 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001428 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001429 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001430 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001431 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001432 SkMatrix::I(),
1433 textureDomain,
1434 GrTextureDomain::kClamp_Mode,
1435 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001436 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001437 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001438 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1439 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001440 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001441 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001442 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001443 }
1444
1445 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1446 // the rest from the SkPaint.
1447 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001448 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001449 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001450 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1451 SkColor2GrColor(paint.getColor());
joshualitt25d9c152015-02-18 12:29:52 -08001452 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001453
joshualitt25d9c152015-02-18 12:29:52 -08001454 fContext->drawNonAARectToRect(fRenderTarget, grPaint, viewMatrix, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001455}
1456
fmalita2d97bc12014-11-20 10:44:58 -08001457bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
1458 const SkImageFilter* filter,
1459 const SkImageFilter::Context& ctx,
1460 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001461 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001462
1463 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1464 // (https://code.google.com/p/skia/issues/detail?id=3148).
1465 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001466
1467 if (filter->canFilterImageGPU()) {
joshualitt25d9c152015-02-18 12:29:52 -08001468 // Set the clip wide open and the matrix to identity.
1469 GrContext::AutoWideOpenIdentityDraw awo(context);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001470 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001471 } else {
1472 return false;
1473 }
1474}
1475
1476void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1477 int left, int top, const SkPaint& paint) {
1478 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001479 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001480
1481 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1482 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1483 return;
1484 }
1485
1486 int w = bitmap.width();
1487 int h = bitmap.height();
1488
1489 GrTexture* texture;
1490 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001491 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001492
1493 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001494 // This bitmap will own the filtered result as a texture.
1495 SkBitmap filteredBitmap;
1496
bsalomon49f085d2014-09-05 13:34:00 -07001497 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001498 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001499 SkMatrix matrix(*draw.fMatrix);
1500 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001501 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001502 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001503 // This cache is transient, and is freed (along with all its contained
1504 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001505 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001506 if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap,
1507 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001508 texture = (GrTexture*) filteredBitmap.getTexture();
1509 w = filteredBitmap.width();
1510 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001511 left += offset.x();
1512 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001513 } else {
1514 return;
1515 }
1516 }
1517
1518 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001519 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001520
joshualitt25d9c152015-02-18 12:29:52 -08001521 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1522 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001523
joshualitt25d9c152015-02-18 12:29:52 -08001524 fContext->drawNonAARectToRect(fRenderTarget,
1525 grPaint,
joshualitt16b27892014-12-18 07:47:16 -08001526 SkMatrix::I(),
1527 SkRect::MakeXYWH(SkIntToScalar(left),
1528 SkIntToScalar(top),
1529 SkIntToScalar(w),
1530 SkIntToScalar(h)),
1531 SkRect::MakeXYWH(0,
1532 0,
1533 SK_Scalar1 * w / texture->width(),
1534 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001535}
1536
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001537void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001538 const SkRect* src, const SkRect& dst,
1539 const SkPaint& paint,
1540 SkCanvas::DrawBitmapRectFlags flags) {
1541 SkMatrix matrix;
1542 SkRect bitmapBounds, tmpSrc;
1543
1544 bitmapBounds.set(0, 0,
1545 SkIntToScalar(bitmap.width()),
1546 SkIntToScalar(bitmap.height()));
1547
1548 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001549 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001550 tmpSrc = *src;
1551 } else {
1552 tmpSrc = bitmapBounds;
1553 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001554
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001555 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1556
1557 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001558 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001559 if (!bitmapBounds.contains(tmpSrc)) {
1560 if (!tmpSrc.intersect(bitmapBounds)) {
1561 return; // nothing to draw
1562 }
1563 }
1564 }
1565
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001566 SkRect tmpDst;
1567 matrix.mapRect(&tmpDst, tmpSrc);
1568
1569 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1570 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1571 // Translate so that tempDst's top left is at the origin.
1572 matrix = *origDraw.fMatrix;
1573 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1574 draw.writable()->fMatrix = &matrix;
1575 }
1576 SkSize dstSize;
1577 dstSize.fWidth = tmpDst.width();
1578 dstSize.fHeight = tmpDst.height();
1579
1580 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001581}
1582
1583void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1584 int x, int y, const SkPaint& paint) {
1585 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001586 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001587 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001588
1589 // TODO: If the source device covers the whole of this device, we could
1590 // omit fNeedsClear -related flushing.
1591 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001592
1593 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001594 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001595
1596 GrRenderTarget* devRT = dev->accessRenderTarget();
1597 GrTexture* devTex;
1598 if (NULL == (devTex = devRT->asTexture())) {
1599 return;
1600 }
1601
robertphillips7b9e8a42014-12-11 08:20:31 -08001602 const SkImageInfo ii = dev->imageInfo();
1603 int w = ii.width();
1604 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001605
1606 SkImageFilter* filter = paint.getImageFilter();
1607 // This bitmap will own the filtered result as a texture.
1608 SkBitmap filteredBitmap;
1609
bsalomon49f085d2014-09-05 13:34:00 -07001610 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001611 SkIPoint offset = SkIPoint::Make(0, 0);
1612 SkMatrix matrix(*draw.fMatrix);
1613 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001614 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001615 // This cache is transient, and is freed (along with all its contained
1616 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001617 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001618 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001619 if (this->filterTexture(fContext, devTex, filter, ctx, &filteredBitmap,
1620 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001621 devTex = filteredBitmap.getTexture();
1622 w = filteredBitmap.width();
1623 h = filteredBitmap.height();
1624 x += offset.fX;
1625 y += offset.fY;
1626 } else {
1627 return;
1628 }
1629 }
1630
1631 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001632 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001633
joshualitt25d9c152015-02-18 12:29:52 -08001634 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1635 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001636
1637 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1638 SkIntToScalar(y),
1639 SkIntToScalar(w),
1640 SkIntToScalar(h));
1641
1642 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1643 // scratch texture).
1644 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1645 SK_Scalar1 * h / devTex->height());
1646
joshualitt25d9c152015-02-18 12:29:52 -08001647 fContext->drawNonAARectToRect(fRenderTarget, grPaint, SkMatrix::I(), dstRect, srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001648}
1649
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001650bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001651 return filter->canFilterImageGPU();
1652}
1653
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001654bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001655 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001656 SkBitmap* result, SkIPoint* offset) {
1657 // want explicitly our impl, so guard against a subclass of us overriding it
1658 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1659 return false;
1660 }
1661
1662 SkAutoLockPixels alp(src, !src.getTexture());
1663 if (!src.getTexture() && !src.readyToDraw()) {
1664 return false;
1665 }
1666
1667 GrTexture* texture;
1668 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1669 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001670 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001671
fmalita2d97bc12014-11-20 10:44:58 -08001672 return this->filterTexture(fContext, texture, filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001673}
1674
1675///////////////////////////////////////////////////////////////////////////////
1676
1677// must be in SkCanvas::VertexMode order
1678static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1679 kTriangles_GrPrimitiveType,
1680 kTriangleStrip_GrPrimitiveType,
1681 kTriangleFan_GrPrimitiveType,
1682};
1683
1684void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1685 int vertexCount, const SkPoint vertices[],
1686 const SkPoint texs[], const SkColor colors[],
1687 SkXfermode* xmode,
1688 const uint16_t indices[], int indexCount,
1689 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001690 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001691 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001692
dandov32a311b2014-07-15 19:46:26 -07001693 const uint16_t* outIndices;
1694 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1695 GrPrimitiveType primType;
1696 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001697
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001698 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1699 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001700
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001701 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001702
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001703 SkPaint copy(paint);
1704 copy.setStyle(SkPaint::kStroke_Style);
1705 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001706
dandov32a311b2014-07-15 19:46:26 -07001707 // we ignore the shader if texs is null.
joshualitt25d9c152015-02-18 12:29:52 -08001708 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy,
1709 SkColor2GrColor(copy.getColor()), NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001710
dandov32a311b2014-07-15 19:46:26 -07001711 primType = kLines_GrPrimitiveType;
1712 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001713 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001714 switch (vmode) {
1715 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001716 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001717 break;
1718 case SkCanvas::kTriangleStrip_VertexMode:
1719 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001720 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001721 break;
1722 }
mtklein533eb782014-08-27 10:39:42 -07001723
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001724 VertState state(vertexCount, indices, indexCount);
1725 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001726
dandov32a311b2014-07-15 19:46:26 -07001727 //number of indices for lines per triangle with kLines
1728 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001729
dandov32a311b2014-07-15 19:46:26 -07001730 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1731 outIndices = outAlloc.get();
1732 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001733 int i = 0;
1734 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001735 auxIndices[i] = state.f0;
1736 auxIndices[i + 1] = state.f1;
1737 auxIndices[i + 2] = state.f1;
1738 auxIndices[i + 3] = state.f2;
1739 auxIndices[i + 4] = state.f2;
1740 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001741 i += 6;
1742 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001743 } else {
dandov32a311b2014-07-15 19:46:26 -07001744 outIndices = indices;
1745 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001746
dandov32a311b2014-07-15 19:46:26 -07001747 if (NULL == texs || NULL == paint.getShader()) {
joshualitt25d9c152015-02-18 12:29:52 -08001748 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1749 SkColor2GrColor(paint.getColor()),
dandov32a311b2014-07-15 19:46:26 -07001750 NULL == colors, &grPaint);
1751 } else {
joshualitt25d9c152015-02-18 12:29:52 -08001752 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix,
1753 NULL == colors, &grPaint);
dandov32a311b2014-07-15 19:46:26 -07001754 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001755 }
1756
mtklein2583b622014-06-04 08:20:41 -07001757#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001758 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001759 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1760 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001761 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001762 }
1763 }
mtklein2583b622014-06-04 08:20:41 -07001764#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001765
1766 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001767 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001768 // need to convert byte order and from non-PM to PM
1769 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001770 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001771 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001772 color = colors[i];
1773 if (paint.getAlpha() != 255) {
1774 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1775 }
1776 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001777 }
1778 colors = convertedColors.get();
1779 }
joshualitt25d9c152015-02-18 12:29:52 -08001780 fContext->drawVertices(fRenderTarget,
1781 grPaint,
joshualitt5531d512014-12-17 15:50:11 -08001782 *draw.fMatrix,
dandov32a311b2014-07-15 19:46:26 -07001783 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001784 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001785 vertices,
1786 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001787 colors,
dandov32a311b2014-07-15 19:46:26 -07001788 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001789 indexCount);
1790}
1791
1792///////////////////////////////////////////////////////////////////////////////
1793
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001794void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001795 size_t byteLength, SkScalar x, SkScalar y,
1796 const SkPaint& paint) {
1797 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001798 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001799
jvanverth8c27a182014-10-14 08:45:50 -07001800 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -08001801 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001802
jvanverth8c27a182014-10-14 08:45:50 -07001803 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001804
joshualitt25d9c152015-02-18 12:29:52 -08001805 if (!fTextContext->drawText(fRenderTarget, grPaint, paint, *draw.fMatrix, (const char *)text,
1806 byteLength, x, y)) {
jvanverth8c27a182014-10-14 08:45:50 -07001807 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001808 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001809 }
1810}
1811
fmalita05c4a432014-09-29 06:29:53 -07001812void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1813 const SkScalar pos[], int scalarsPerPos,
1814 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001815 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001816 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001817
jvanverth8c27a182014-10-14 08:45:50 -07001818 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -08001819 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001820
jvanverth8c27a182014-10-14 08:45:50 -07001821 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001822
joshualitt25d9c152015-02-18 12:29:52 -08001823 if (!fTextContext->drawPosText(fRenderTarget, grPaint, paint, *draw.fMatrix, (const char *)text,
1824 byteLength, pos, scalarsPerPos, offset)) {
jvanverth8c27a182014-10-14 08:45:50 -07001825 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001826 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001827 }
1828}
1829
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001830///////////////////////////////////////////////////////////////////////////////
1831
reedb2db8982014-11-13 12:41:02 -08001832bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001833 if (paint.getShader() ||
reedb2db8982014-11-13 12:41:02 -08001834 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001835 paint.getMaskFilter() ||
1836 paint.getRasterizer() ||
1837 paint.getColorFilter() ||
1838 paint.getPathEffect() ||
1839 paint.isFakeBoldText() ||
reedb2db8982014-11-13 12:41:02 -08001840 paint.getStyle() != SkPaint::kFill_Style)
1841 {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001842 return true;
1843 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001844 return false;
1845}
1846
1847void SkGpuDevice::flush() {
1848 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001849 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001850}
1851
1852///////////////////////////////////////////////////////////////////////////////
1853
fmalita6987dca2014-11-13 08:33:37 -08001854SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(const CreateInfo& cinfo) {
bsalomonf2703d82014-10-28 14:33:06 -07001855 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001856 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001857 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001858 desc.fWidth = cinfo.fInfo.width();
1859 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001860 desc.fSampleCnt = fRenderTarget->numSamples();
1861
1862 SkAutoTUnref<GrTexture> texture;
1863 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001864 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001865
hcm4396fa52014-10-27 21:43:30 -07001866 // layers are never draw in repeat modes, so we can request an approx
1867 // match and ignore any padding.
fmalita6987dca2014-11-13 08:33:37 -08001868 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == cinfo.fUsage) ?
hcm4396fa52014-10-27 21:43:30 -07001869 GrContext::kApprox_ScratchTexMatch :
1870 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001871 texture.reset(fContext->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001872
1873 if (texture) {
1874 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry);
1875 return SkGpuDevice::Create(texture->asRenderTarget(), &props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001876 } else {
tfarina38406c82014-10-31 07:11:12 -07001877 SkDebugf("---- failed to create compatible device texture [%d %d]\n",
fmalita6987dca2014-11-13 08:33:37 -08001878 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001879 return NULL;
1880 }
1881}
1882
reed4a8126e2014-09-22 07:29:03 -07001883SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001884 // TODO: Change the signature of newSurface to take a budgeted parameter.
1885 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
1886 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->numSamples(),
1887 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001888}
1889
robertphillips30d2cc62014-09-24 08:52:18 -07001890bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001891 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001892#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001893 // todo: should handle this natively
1894 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001895 return false;
1896 }
1897
robertphillips82365912014-11-12 09:32:34 -08001898 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips81f71b62014-11-11 04:54:49 -08001899
1900 const SkPicture::AccelData* data = mainPicture->EXPERIMENTAL_getAccelData(key);
1901 if (!data) {
1902 return false;
1903 }
1904
robertphillipse5524cd2015-02-20 12:30:26 -08001905 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1906 if (0 == gpuData->numBlocks()) {
1907 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001908 }
1909
robertphillipsfd61ed02014-10-28 07:21:44 -07001910 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001911
robertphillipse5524cd2015-02-20 12:30:26 -08001912 SkIRect iBounds;
1913 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1914 return false;
1915 }
1916
1917 SkRect clipBounds = SkRect::Make(iBounds);
1918
1919 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1920
robertphillipsfd61ed02014-10-28 07:21:44 -07001921 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001922 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001923 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001924 &atlasedNeedRendering, &atlasedRecycled,
1925 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001926
1927 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1928
1929 SkTDArray<GrHoistedLayer> needRendering, recycled;
1930
robertphillipse5524cd2015-02-20 12:30:26 -08001931 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1932
robertphillipsfd61ed02014-10-28 07:21:44 -07001933 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001934 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001935 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001936 &needRendering, &recycled,
1937 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001938
1939 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001940
robertphillips64bf7672014-08-21 13:07:35 -07001941 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001942 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1943 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001944
robertphillipsfd61ed02014-10-28 07:21:44 -07001945 GrLayerHoister::UnlockLayers(fContext, needRendering);
1946 GrLayerHoister::UnlockLayers(fContext, recycled);
1947 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1948 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001949
1950 return true;
robertphillips63242d72014-12-04 08:31:02 -08001951#else
1952 return false;
1953#endif
robertphillips64bf7672014-08-21 13:07:35 -07001954}
1955
senorblancobe129b22014-08-08 07:14:35 -07001956SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001957 // We always return a transient cache, so it is freed after each
1958 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001959 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001960}
reedf037e0b2014-10-30 11:34:15 -07001961
1962#endif