blob: fb5fa7cc7a5f25004fbed7c69815a16ab300edff [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
9
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000010#include "GrBitmapTextContext.h"
kkinnunenabcfab42015-02-22 22:53:44 -080011#include "GrContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000012#include "GrDistanceFieldTextContext.h"
kkinnunenabcfab42015-02-22 22:53:44 -080013#include "GrGpu.h"
14#include "GrGpuResourcePriv.h"
robertphillips98d709b2014-09-02 10:20:50 -070015#include "GrLayerHoister.h"
robertphillips274b4ba2014-09-04 07:24:18 -070016#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070017#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070018#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080019#include "SkCanvasPriv.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000020#include "SkDeviceImageFilterProxy.h"
21#include "SkDrawProcs.h"
kkinnunenabcfab42015-02-22 22:53:44 -080022#include "SkErrorInternals.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000023#include "SkGlyphCache.h"
kkinnunenabcfab42015-02-22 22:53:44 -080024#include "SkGrTexturePixelRef.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080026#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000027#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000029#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070030#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000031#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080032#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000034#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080035#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000036#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000038#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070039#include "SkXfermode.h"
kkinnunenabcfab42015-02-22 22:53:44 -080040#include "effects/GrBicubicEffect.h"
41#include "effects/GrDashingEffect.h"
42#include "effects/GrSimpleTextureEffect.h"
43#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000044
reedf037e0b2014-10-30 11:34:15 -070045#if SK_SUPPORT_GPU
46
senorblanco55b6d8b2014-07-30 11:26:46 -070047enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
48
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000049#if 0
50 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080051 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000052 do { \
53 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080054 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000055 } while (0)
56#else
joshualitt5531d512014-12-17 15:50:11 -080057 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058#endif
59
60// This constant represents the screen alignment criterion in texels for
61// requiring texture domain clamping to prevent color bleeding when drawing
62// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000063#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000064
65#define DO_DEFERRED_CLEAR() \
66 do { \
bsalomonafe30052015-01-16 07:32:33 -080067 if (fNeedClear) { \
68 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000069 } \
70 } while (false) \
71
72///////////////////////////////////////////////////////////////////////////////
73
74#define CHECK_FOR_ANNOTATION(paint) \
75 do { if (paint.getAnnotation()) { return; } } while (0)
76
77///////////////////////////////////////////////////////////////////////////////
78
bsalomonbcf0a522014-10-08 08:40:09 -070079// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
80// just accesses the backing GrTexture. Otherwise, it creates a cached texture
81// representation and releases it in the destructor.
82class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040083public:
bsalomonbcf0a522014-10-08 08:40:09 -070084 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070085
bsalomonbcf0a522014-10-08 08:40:09 -070086 AutoBitmapTexture(GrContext* context,
87 const SkBitmap& bitmap,
88 const GrTextureParams* params,
89 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040090 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070091 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040092 }
93
bsalomonbcf0a522014-10-08 08:40:09 -070094 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040095 const SkBitmap& bitmap,
96 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -070097 // Either get the texture directly from the bitmap, or else use the cache and
98 // remember to unref it.
99 if (GrTexture* bmpTexture = bitmap.getTexture()) {
100 fTexture.reset(NULL);
101 return bmpTexture;
102 } else {
103 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
104 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400105 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400106 }
107
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000108private:
bsalomonbcf0a522014-10-08 08:40:09 -0700109 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000110};
111
112///////////////////////////////////////////////////////////////////////////////
113
114struct GrSkDrawProcs : public SkDrawProcs {
115public:
116 GrContext* fContext;
117 GrTextContext* fTextContext;
118 GrFontScaler* fFontScaler; // cached in the skia glyphcache
119};
120
121///////////////////////////////////////////////////////////////////////////////
122
bsalomonafe30052015-01-16 07:32:33 -0800123SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags) {
124 if (!rt || rt->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000125 return NULL;
126 }
bsalomonafe30052015-01-16 07:32:33 -0800127 return SkNEW_ARGS(SkGpuDevice, (rt, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000128}
129
bsalomonafe30052015-01-16 07:32:33 -0800130static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps* props) {
131 if (props) {
132 return SkDeviceProperties(props->pixelGeometry());
133 } else {
134 return SkDeviceProperties(SkDeviceProperties::kLegacyLCD_InitType);
135 }
reedb2db8982014-11-13 12:41:02 -0800136}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000137
bsalomonafe30052015-01-16 07:32:33 -0800138static SkSurfaceProps copy_or_default_props(const SkSurfaceProps* props) {
139 if (props) {
140 return SkSurfaceProps(*props);
141 } else {
142 return SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
143 }
144}
145
146SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags)
reedb2db8982014-11-13 12:41:02 -0800147 : INHERITED(surfaceprops_to_deviceprops(props))
bsalomonafe30052015-01-16 07:32:33 -0800148 , fSurfaceProps(copy_or_default_props(props))
reedb2db8982014-11-13 12:41:02 -0800149{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000150 fDrawProcs = NULL;
151
bsalomonafe30052015-01-16 07:32:33 -0800152 fContext = SkRef(rt->getContext());
153 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000154
bsalomonafe30052015-01-16 07:32:33 -0800155 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000156
bsalomonafe30052015-01-16 07:32:33 -0800157 SkImageInfo info = rt->surfacePriv().info();
158 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt));
bsalomonafbf2d62014-09-30 12:18:44 -0700159 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700160 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700161
bsalomonafe30052015-01-16 07:32:33 -0800162 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts();
jvanverth4736e142014-11-07 07:12:46 -0800163 fTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFT);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000164}
165
kkinnunenabcfab42015-02-22 22:53:44 -0800166GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
167 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000168 if (kUnknown_SkColorType == origInfo.colorType() ||
169 origInfo.width() < 0 || origInfo.height() < 0) {
170 return NULL;
171 }
172
bsalomonafe30052015-01-16 07:32:33 -0800173 if (!context) {
174 return NULL;
175 }
176
reede5ea5002014-09-03 11:54:58 -0700177 SkColorType ct = origInfo.colorType();
178 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700179 if (kRGB_565_SkColorType == ct) {
180 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800181 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
182 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700183 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800184 }
185 if (kOpaque_SkAlphaType != at) {
186 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000187 }
reede5ea5002014-09-03 11:54:58 -0700188 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000189
bsalomonf2703d82014-10-28 14:33:06 -0700190 GrSurfaceDesc desc;
191 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000192 desc.fWidth = info.width();
193 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000194 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000195 desc.fSampleCnt = sampleCount;
kkinnunenabcfab42015-02-22 22:53:44 -0800196 GrTexture* texture = context->createTexture(desc, SkToBool(budgeted), NULL, 0);
197 if (NULL == texture) {
198 return NULL;
199 }
200 SkASSERT(NULL != texture->asRenderTarget());
201 return texture->asRenderTarget();
202}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000203
kkinnunenabcfab42015-02-22 22:53:44 -0800204SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
205 const SkImageInfo& info, int sampleCount,
206 const SkSurfaceProps* props, unsigned flags) {
207
208 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
209 if (NULL == rt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000210 return NULL;
211 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000212
kkinnunenabcfab42015-02-22 22:53:44 -0800213 return SkNEW_ARGS(SkGpuDevice, (rt, props, flags));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000214}
215
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000216SkGpuDevice::~SkGpuDevice() {
217 if (fDrawProcs) {
218 delete fDrawProcs;
219 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000220
jvanverth8c27a182014-10-14 08:45:50 -0700221 delete fTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000222
bsalomon32d0b3b2014-08-29 07:50:23 -0700223 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000224 fContext->unref();
225}
226
227///////////////////////////////////////////////////////////////////////////////
228
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000229bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
230 int x, int y) {
231 DO_DEFERRED_CLEAR();
232
233 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000234 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000235 if (kUnknown_GrPixelConfig == config) {
236 return false;
237 }
238
239 uint32_t flags = 0;
240 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
241 flags = GrContext::kUnpremul_PixelOpsFlag;
242 }
243 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
244 config, dstPixels, dstRowBytes, flags);
245}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000246
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000247bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
248 int x, int y) {
249 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000250 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000251 if (kUnknown_GrPixelConfig == config) {
252 return false;
253 }
254 uint32_t flags = 0;
255 if (kUnpremul_SkAlphaType == info.alphaType()) {
256 flags = GrContext::kUnpremul_PixelOpsFlag;
257 }
258 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
259
260 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700261 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000262
263 return true;
264}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000265
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000266const SkBitmap& SkGpuDevice::onAccessBitmap() {
267 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700268 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000269}
270
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000271void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
272 INHERITED::onAttachToCanvas(canvas);
273
274 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800275 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000276}
277
278void SkGpuDevice::onDetachFromCanvas() {
279 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800280 fClip.reset();
joshualitt44701df2015-02-23 14:44:57 -0800281 fClipStack.reset(NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000282}
283
284// call this every draw call, to ensure that the context reflects our state,
285// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800286void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800287 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000288
joshualitt44701df2015-02-23 14:44:57 -0800289 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000290
joshualitt570d2f82015-02-25 13:19:48 -0800291 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000292
293 DO_DEFERRED_CLEAR();
294}
295
296GrRenderTarget* SkGpuDevice::accessRenderTarget() {
297 DO_DEFERRED_CLEAR();
298 return fRenderTarget;
299}
300
reed8eddfb52014-12-04 07:50:14 -0800301void SkGpuDevice::clearAll() {
302 GrColor color = 0;
303 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
304 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
305 fContext->clear(&rect, color, true, fRenderTarget);
bsalomonafe30052015-01-16 07:32:33 -0800306 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800307}
308
kkinnunenabcfab42015-02-22 22:53:44 -0800309void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
310 // Caller must have accessed the render target, because it knows the rt must be replaced.
311 SkASSERT(!fNeedClear);
312
313 SkSurface::Budgeted budgeted =
314 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
315 : SkSurface::kNo_Budgeted;
316
317 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
318 fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->numSamples()));
319
320 if (NULL == newRT) {
321 return;
322 }
323
324 if (shouldRetainContent) {
325 if (fRenderTarget->wasDestroyed()) {
326 return;
327 }
328 this->context()->copySurface(newRT, fRenderTarget);
329 }
330
331 SkASSERT(fRenderTarget != newRT);
332
333 fRenderTarget->unref();
334 fRenderTarget = newRT.detach();
335
336 SkASSERT(fRenderTarget->surfacePriv().info() == fLegacyBitmap.info());
337 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (fRenderTarget->surfacePriv().info(), fRenderTarget));
338 fLegacyBitmap.setPixelRef(pr)->unref();
339}
340
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000341///////////////////////////////////////////////////////////////////////////////
342
343SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
344SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
345SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
346SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
347SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
348 shader_type_mismatch);
349SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
350 shader_type_mismatch);
351SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
352SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
353
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000354///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000355
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800357 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700358 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000359
360 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800361 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000362
joshualitt570d2f82015-02-25 13:19:48 -0800363 fContext->drawPaint(fRenderTarget, fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000364}
365
366// must be in SkCanvas::PointMode order
367static const GrPrimitiveType gPointMode2PrimtiveType[] = {
368 kPoints_GrPrimitiveType,
369 kLines_GrPrimitiveType,
370 kLineStrip_GrPrimitiveType
371};
372
373void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
374 size_t count, const SkPoint pts[], const SkPaint& paint) {
375 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800376 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000377
378 SkScalar width = paint.getStrokeWidth();
379 if (width < 0) {
380 return;
381 }
382
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000383 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700384 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
385 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800386 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
egdaniele61c4112014-06-12 10:24:21 -0700387 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700388 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700389 path.moveTo(pts[0]);
390 path.lineTo(pts[1]);
joshualitt570d2f82015-02-25 13:19:48 -0800391 fContext->drawPath(fRenderTarget, fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700392 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000393 }
394
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000395 // we only handle hairlines and paints without path effects or mask filters,
396 // else we let the SkDraw call our drawPath()
397 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
398 draw.drawPoints(mode, count, pts, paint, true);
399 return;
400 }
401
402 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800403 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000404
joshualitt25d9c152015-02-18 12:29:52 -0800405 fContext->drawVertices(fRenderTarget,
joshualitt570d2f82015-02-25 13:19:48 -0800406 fClip,
joshualitt25d9c152015-02-18 12:29:52 -0800407 grPaint,
joshualitt5531d512014-12-17 15:50:11 -0800408 *draw.fMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000409 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000410 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000411 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000412 NULL,
413 NULL,
414 NULL,
415 0);
416}
417
418///////////////////////////////////////////////////////////////////////////////
419
420void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
421 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700422 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
423
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000424 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800425 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000426
427 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
428 SkScalar width = paint.getStrokeWidth();
429
430 /*
431 We have special code for hairline strokes, miter-strokes, bevel-stroke
432 and fills. Anything else we just call our path code.
433 */
434 bool usePath = doStroke && width > 0 &&
435 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
436 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
437 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700438
439 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000440 usePath = true;
441 }
egdanield58a0ba2014-06-11 10:30:05 -0700442
joshualitt5531d512014-12-17 15:50:11 -0800443 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000444#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
445 if (doStroke) {
446#endif
447 usePath = true;
448#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
449 } else {
joshualitt5531d512014-12-17 15:50:11 -0800450 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000451 }
452#endif
453 }
454 // until we can both stroke and fill rectangles
455 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
456 usePath = true;
457 }
458
egdanield58a0ba2014-06-11 10:30:05 -0700459 GrStrokeInfo strokeInfo(paint);
460
461 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700462 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700463 usePath = true;
464 }
465
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000466 if (usePath) {
467 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700468 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000469 path.addRect(rect);
470 this->drawPath(draw, path, paint, NULL, true);
471 return;
472 }
473
474 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800475 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400476
joshualitt570d2f82015-02-25 13:19:48 -0800477 fContext->drawRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000478}
479
480///////////////////////////////////////////////////////////////////////////////
481
482void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800483 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700484 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000485 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800486 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000487
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000488 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800489 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400490
egdanield58a0ba2014-06-11 10:30:05 -0700491 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000492 if (paint.getMaskFilter()) {
493 // try to hit the fast path for drawing filtered round rects
494
495 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800496 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000497 if (devRRect.allCornersCircular()) {
498 SkRect maskRect;
499 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800500 draw.fClip->getBounds(),
501 *draw.fMatrix,
502 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000503 SkIRect finalIRect;
504 maskRect.roundOut(&finalIRect);
505 if (draw.fClip->quickReject(finalIRect)) {
506 // clipped out
507 return;
508 }
joshualitt25d9c152015-02-18 12:29:52 -0800509 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
510 fRenderTarget,
511 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800512 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800513 *draw.fMatrix,
egdanield58a0ba2014-06-11 10:30:05 -0700514 strokeInfo.getStrokeRec(),
515 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000516 return;
517 }
518 }
519
520 }
521 }
522
523 }
524
egdanield58a0ba2014-06-11 10:30:05 -0700525 bool usePath = false;
526
527 if (paint.getMaskFilter()) {
528 usePath = true;
529 } else {
530 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700531 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700532 usePath = true;
533 }
534 }
535
536
537 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000538 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700539 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000540 path.addRRect(rect);
541 this->drawPath(draw, path, paint, NULL, true);
542 return;
543 }
Mike Klein744fb732014-06-23 15:13:26 -0400544
joshualitt570d2f82015-02-25 13:19:48 -0800545 fContext->drawRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000546}
547
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000548void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800549 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000550 SkStrokeRec stroke(paint);
551 if (stroke.isFillStyle()) {
552
553 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800554 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000555
556 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800557 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000558
559 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
joshualitt570d2f82015-02-25 13:19:48 -0800560 fContext->drawDRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000561 return;
562 }
563 }
564
565 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700566 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000567 path.addRRect(outer);
568 path.addRRect(inner);
569 path.setFillType(SkPath::kEvenOdd_FillType);
570
571 this->drawPath(draw, path, paint, NULL, true);
572}
573
574
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000575/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000576
577void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
578 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700579 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000580 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800581 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000582
egdanield58a0ba2014-06-11 10:30:05 -0700583 GrStrokeInfo strokeInfo(paint);
584
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000585 bool usePath = false;
586 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700587 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000588 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700589 } else {
590 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700591 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700592 usePath = true;
593 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000594 }
595
596 if (usePath) {
597 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700598 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000599 path.addOval(oval);
600 this->drawPath(draw, path, paint, NULL, true);
601 return;
602 }
603
604 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -0800605 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000606
joshualitt570d2f82015-02-25 13:19:48 -0800607 fContext->drawOval(fRenderTarget, fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000608}
609
610#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000611
612///////////////////////////////////////////////////////////////////////////////
613
614// helpers for applying mask filters
615namespace {
616
617// Draw a mask using the supplied paint. Since the coverage/geometry
618// is already burnt into the mask this boils down to a rect draw.
619// Return true if the mask was successfully drawn.
joshualitt25d9c152015-02-18 12:29:52 -0800620bool draw_mask(GrContext* context,
621 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800622 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800623 const SkMatrix& viewMatrix,
624 const SkRect& maskRect,
625 GrPaint* grp,
626 GrTexture* mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000627 SkMatrix matrix;
628 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
629 matrix.postIDiv(mask->width(), mask->height());
630
joshualitt16b27892014-12-18 07:47:16 -0800631 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix,
632 kDevice_GrCoordSet))->unref();
633
634 SkMatrix inverse;
635 if (!viewMatrix.invert(&inverse)) {
636 return false;
637 }
joshualitt570d2f82015-02-25 13:19:48 -0800638 context->drawNonAARectWithLocalMatrix(rt, clip, *grp, SkMatrix::I(), maskRect, inverse);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000639 return true;
640}
641
joshualitt25d9c152015-02-18 12:29:52 -0800642bool draw_with_mask_filter(GrContext* context,
643 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800644 const GrClip& clipData,
joshualitt25d9c152015-02-18 12:29:52 -0800645 const SkMatrix& viewMatrix,
646 const SkPath& devPath,
647 SkMaskFilter* filter,
648 const SkRegion& clip,
649 GrPaint* grp,
650 SkPaint::Style style) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000651 SkMask srcM, dstM;
652
joshualitt5531d512014-12-17 15:50:11 -0800653 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &viewMatrix, &srcM,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000654 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
655 return false;
656 }
657 SkAutoMaskFreeImage autoSrc(srcM.fImage);
658
joshualitt5531d512014-12-17 15:50:11 -0800659 if (!filter->filterMask(&dstM, srcM, viewMatrix, NULL)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660 return false;
661 }
662 // this will free-up dstM when we're done (allocated in filterMask())
663 SkAutoMaskFreeImage autoDst(dstM.fImage);
664
665 if (clip.quickReject(dstM.fBounds)) {
666 return false;
667 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000668
669 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
670 // the current clip (and identity matrix) and GrPaint settings
bsalomonf2703d82014-10-28 14:33:06 -0700671 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000672 desc.fWidth = dstM.fBounds.width();
673 desc.fHeight = dstM.fBounds.height();
674 desc.fConfig = kAlpha_8_GrPixelConfig;
675
bsalomone3059732014-10-14 11:47:22 -0700676 SkAutoTUnref<GrTexture> texture(
677 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
678 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000679 return false;
680 }
681 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
682 dstM.fImage, dstM.fRowBytes);
683
684 SkRect maskRect = SkRect::Make(dstM.fBounds);
685
joshualitt570d2f82015-02-25 13:19:48 -0800686 return draw_mask(context, rt, clipData, viewMatrix, maskRect, grp, texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000687}
688
bsalomone3059732014-10-14 11:47:22 -0700689// Create a mask of 'devPath' and place the result in 'mask'.
690GrTexture* create_mask_GPU(GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800691 GrRenderTarget* rt,
bsalomone3059732014-10-14 11:47:22 -0700692 const SkRect& maskRect,
693 const SkPath& devPath,
694 const GrStrokeInfo& strokeInfo,
695 bool doAA,
696 int sampleCnt) {
bsalomonf2703d82014-10-28 14:33:06 -0700697 GrSurfaceDesc desc;
698 desc.fFlags = kRenderTarget_GrSurfaceFlag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000699 desc.fWidth = SkScalarCeilToInt(maskRect.width());
700 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700701 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000702 // We actually only need A8, but it often isn't supported as a
703 // render target so default to RGBA_8888
704 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700705
706 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
707 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000708 desc.fConfig = kAlpha_8_GrPixelConfig;
709 }
710
bsalomone3059732014-10-14 11:47:22 -0700711 GrTexture* mask = context->refScratchTexture(desc,GrContext::kApprox_ScratchTexMatch);
712 if (NULL == mask) {
713 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000714 }
715
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000716 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
717
bsalomon89c62982014-11-03 12:08:42 -0800718 context->clear(NULL, 0x0, true, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000719
720 GrPaint tempPaint;
egdanielb197b8f2015-02-17 07:34:43 -0800721 tempPaint.setAntiAlias(doAA);
722 tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000723
joshualitt570d2f82015-02-25 13:19:48 -0800724 // setup new clip
725 GrClip clip(clipRect);
726
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);
joshualitt570d2f82015-02-25 13:19:48 -0800730 context->drawPath(mask->asRenderTarget(), clip, 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,
joshualitt570d2f82015-02-25 13:19:48 -0800835 fClip,
joshualitt25d9c152015-02-18 12:29:52 -0800836 viewMatrix,
837 stroke,
838 *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000839 // the mask filter was able to draw itself directly, so there's nothing
840 // left to do.
841 return;
842 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000843
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000844
joshualitt25d9c152015-02-18 12:29:52 -0800845 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext,
846 fRenderTarget,
847 maskRect,
848 *devPathPtr,
849 strokeInfo,
850 grPaint.isAntiAlias(),
bsalomone3059732014-10-14 11:47:22 -0700851 fRenderTarget->numSamples()));
852 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000853 GrTexture* filtered;
854
bsalomon54443932015-01-29 09:34:18 -0800855 if (paint.getMaskFilter()->filterMaskGPU(mask, viewMatrix, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000856 // filterMaskGPU gives us ownership of a ref to the result
857 SkAutoTUnref<GrTexture> atu(filtered);
joshualitt570d2f82015-02-25 13:19:48 -0800858 if (draw_mask(fContext,
859 fRenderTarget,
860 fClip,
861 viewMatrix,
862 maskRect,
863 &grPaint,
joshualitt25d9c152015-02-18 12:29:52 -0800864 filtered)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000865 // This path is completely drawn
866 return;
867 }
868 }
869 }
870 }
871
872 // draw the mask on the CPU - this is a fallthrough path in case the
873 // GPU path fails
874 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
875 SkPaint::kFill_Style;
joshualitt570d2f82015-02-25 13:19:48 -0800876 draw_with_mask_filter(fContext, fRenderTarget, fClip, viewMatrix, *devPathPtr,
joshualitt25d9c152015-02-18 12:29:52 -0800877 paint.getMaskFilter(), *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000878 return;
879 }
880
joshualitt570d2f82015-02-25 13:19:48 -0800881 fContext->drawPath(fRenderTarget, fClip, grPaint, viewMatrix, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000882}
883
884static const int kBmpSmallTileSize = 1 << 10;
885
886static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
887 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
888 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
889 return tilesX * tilesY;
890}
891
892static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
893 if (maxTileSize <= kBmpSmallTileSize) {
894 return maxTileSize;
895 }
896
897 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
898 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
899
900 maxTileTotalTileSize *= maxTileSize * maxTileSize;
901 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
902
903 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
904 return kBmpSmallTileSize;
905 } else {
906 return maxTileSize;
907 }
908}
909
910// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
911// pixels from the bitmap are necessary.
912static void determine_clipped_src_rect(const GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800913 const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800914 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800915 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000916 const SkBitmap& bitmap,
917 const SkRect* srcRectPtr,
918 SkIRect* clippedSrcIRect) {
joshualitt570d2f82015-02-25 13:19:48 -0800919 clip.getConservativeBounds(rt, clippedSrcIRect, NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000920 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800921 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000922 clippedSrcIRect->setEmpty();
923 return;
924 }
925 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
926 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700927 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000928 // we've setup src space 0,0 to map to the top left of the src rect.
929 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000930 if (!clippedSrcRect.intersect(*srcRectPtr)) {
931 clippedSrcIRect->setEmpty();
932 return;
933 }
934 }
935 clippedSrcRect.roundOut(clippedSrcIRect);
936 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
937 if (!clippedSrcIRect->intersect(bmpBounds)) {
938 clippedSrcIRect->setEmpty();
939 }
940}
941
942bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800943 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000944 const GrTextureParams& params,
945 const SkRect* srcRectPtr,
946 int maxTileSize,
947 int* tileSize,
948 SkIRect* clippedSrcRect) const {
949 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700950 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000951 return false;
952 }
953
954 // if it's larger than the max tile size, then we have no choice but tiling.
955 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt570d2f82015-02-25 13:19:48 -0800956 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap,
957 srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
959 return true;
960 }
961
962 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
963 return false;
964 }
965
966 // if the entire texture is already in our cache then no reason to tile it
967 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
968 return false;
969 }
970
971 // At this point we know we could do the draw by uploading the entire bitmap
972 // as a texture. However, if the texture would be large compared to the
973 // cache size and we don't require most of it for this draw then tile to
974 // reduce the amount of upload and cache spill.
975
976 // assumption here is that sw bitmap size is a good proxy for its size as
977 // a texture
978 size_t bmpSize = bitmap.getSize();
979 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000980 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000981 if (bmpSize < cacheSize / 2) {
982 return false;
983 }
984
985 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt570d2f82015-02-25 13:19:48 -0800986 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap, srcRectPtr,
joshualitt25d9c152015-02-18 12:29:52 -0800987 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000988 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
989 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
990 kBmpSmallTileSize * kBmpSmallTileSize;
991
992 return usedTileBytes < 2 * bmpSize;
993}
994
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000995void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000996 const SkBitmap& bitmap,
997 const SkMatrix& m,
998 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000999 SkMatrix concat;
1000 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1001 if (!m.isIdentity()) {
1002 concat.setConcat(*draw->fMatrix, m);
1003 draw.writable()->fMatrix = &concat;
1004 }
1005 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001006}
1007
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001008// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001009// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
1010// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001011static inline void clamped_outset_with_offset(SkIRect* iRect,
1012 int outset,
1013 SkPoint* offset,
1014 const SkIRect& clamp) {
1015 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001016
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001017 int leftClampDelta = clamp.fLeft - iRect->fLeft;
1018 if (leftClampDelta > 0) {
1019 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001020 iRect->fLeft = clamp.fLeft;
1021 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001022 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001023 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001024
1025 int topClampDelta = clamp.fTop - iRect->fTop;
1026 if (topClampDelta > 0) {
1027 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001028 iRect->fTop = clamp.fTop;
1029 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001030 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001031 }
1032
1033 if (iRect->fRight > clamp.fRight) {
1034 iRect->fRight = clamp.fRight;
1035 }
1036 if (iRect->fBottom > clamp.fBottom) {
1037 iRect->fBottom = clamp.fBottom;
1038 }
1039}
1040
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001041static bool has_aligned_samples(const SkRect& srcRect,
1042 const SkRect& transformedRect) {
1043 // detect pixel disalignment
1044 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1045 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1046 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1047 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1048 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1049 COLOR_BLEED_TOLERANCE &&
1050 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1051 COLOR_BLEED_TOLERANCE) {
1052 return true;
1053 }
1054 return false;
1055}
1056
1057static bool may_color_bleed(const SkRect& srcRect,
1058 const SkRect& transformedRect,
1059 const SkMatrix& m) {
1060 // Only gets called if has_aligned_samples returned false.
1061 // So we can assume that sampling is axis aligned but not texel aligned.
1062 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1063 SkRect innerSrcRect(srcRect), innerTransformedRect,
1064 outerTransformedRect(transformedRect);
1065 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1066 m.mapRect(&innerTransformedRect, innerSrcRect);
1067
1068 // The gap between outerTransformedRect and innerTransformedRect
1069 // represents the projection of the source border area, which is
1070 // problematic for color bleeding. We must check whether any
1071 // destination pixels sample the border area.
1072 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1073 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1074 SkIRect outer, inner;
1075 outerTransformedRect.round(&outer);
1076 innerTransformedRect.round(&inner);
1077 // If the inner and outer rects round to the same result, it means the
1078 // border does not overlap any pixel centers. Yay!
1079 return inner != outer;
1080}
1081
1082static bool needs_texture_domain(const SkBitmap& bitmap,
1083 const SkRect& srcRect,
1084 GrTextureParams &params,
1085 const SkMatrix& contextMatrix,
1086 bool bicubic) {
1087 bool needsTextureDomain = false;
1088
1089 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1090 // Need texture domain if drawing a sub rect
1091 needsTextureDomain = srcRect.width() < bitmap.width() ||
1092 srcRect.height() < bitmap.height();
1093 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1094 // sampling is axis-aligned
1095 SkRect transformedRect;
1096 contextMatrix.mapRect(&transformedRect, srcRect);
1097
1098 if (has_aligned_samples(srcRect, transformedRect)) {
1099 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1100 needsTextureDomain = false;
1101 } else {
1102 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1103 }
1104 }
1105 }
1106 return needsTextureDomain;
1107}
1108
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001109void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1110 const SkBitmap& bitmap,
1111 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001112 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001113 const SkPaint& paint,
1114 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -08001115 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001116
1117 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001118 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001119 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1120 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001121 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001122 SkScalar w = SkIntToScalar(bitmap.width());
1123 SkScalar h = SkIntToScalar(bitmap.height());
1124 dstSize.fWidth = w;
1125 dstSize.fHeight = h;
1126 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001127 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001128 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001129 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001130 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001131 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001132 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1133 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1134 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1135 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001136 }
1137
derekf367e1862014-12-02 11:02:06 -08001138 // If the render target is not msaa and draw is antialiased, we call
1139 // drawRect instead of drawing on the render target directly.
1140 // FIXME: the tiled bitmap code path doesn't currently support
1141 // anti-aliased edges, we work around that for now by drawing directly
1142 // if the image size exceeds maximum texture size.
1143 int maxTextureSize = fContext->getMaxTextureSize();
1144 bool directDraw = fRenderTarget->isMultisampled() ||
1145 !paint.isAntiAlias() ||
1146 bitmap.width() > maxTextureSize ||
1147 bitmap.height() > maxTextureSize;
1148
1149 // we check whether dst rect are pixel aligned
1150 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -08001151 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -08001152
1153 if (staysRect) {
1154 SkRect rect;
1155 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -08001156 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -08001157 const SkScalar *scalars = rect.asScalars();
1158 bool isDstPixelAligned = true;
1159 for (int i = 0; i < 4; i++) {
1160 if (!SkScalarIsInt(scalars[i])) {
1161 isDstPixelAligned = false;
1162 break;
1163 }
1164 }
1165
1166 if (isDstPixelAligned)
1167 directDraw = true;
1168 }
1169 }
1170
1171 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001172 // Convert the bitmap to a shader so that the rect can be drawn
1173 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001174 SkBitmap tmp; // subset of bitmap, if necessary
1175 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001176 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001177 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001178 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1179 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1180 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001181 // In bleed mode we position and trim the bitmap based on the src rect which is
1182 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1183 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1184 // compensate.
1185 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1186 SkIRect iSrc;
1187 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001188
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001189 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1190 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001191
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001192 if (!bitmap.extractSubset(&tmp, iSrc)) {
1193 return; // extraction failed
1194 }
1195 bitmapPtr = &tmp;
1196 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001197
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001198 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001199 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001200 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001201 } else {
1202 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001203 }
1204
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001205 SkPaint paintWithShader(paint);
1206 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001207 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001208 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1209 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001210
1211 return;
1212 }
1213
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001214 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1215 // the view matrix rather than a local matrix.
1216 SkMatrix m;
1217 m.setScale(dstSize.fWidth / srcRect.width(),
1218 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -08001219 SkMatrix viewM = *draw.fMatrix;
1220 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001221
1222 GrTextureParams params;
1223 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1224 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001225
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001226 bool doBicubic = false;
1227
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001228 switch(paintFilterLevel) {
1229 case SkPaint::kNone_FilterLevel:
1230 textureFilterMode = GrTextureParams::kNone_FilterMode;
1231 break;
1232 case SkPaint::kLow_FilterLevel:
1233 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1234 break;
1235 case SkPaint::kMedium_FilterLevel:
joshualitt5531d512014-12-17 15:50:11 -08001236 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001237 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1238 } else {
1239 // Don't trigger MIP level generation unnecessarily.
1240 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1241 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001242 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001243 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001244 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001245 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001246 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001247 break;
1248 default:
1249 SkErrorInternals::SetError( kInvalidPaint_SkError,
1250 "Sorry, I don't understand the filtering "
1251 "mode you asked for. Falling back to "
1252 "MIPMaps.");
1253 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1254 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001255 }
1256
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001257 int tileFilterPad;
1258 if (doBicubic) {
1259 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1260 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1261 tileFilterPad = 0;
1262 } else {
1263 tileFilterPad = 1;
1264 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001265 params.setFilterMode(textureFilterMode);
1266
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001267 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001268 int tileSize;
1269
1270 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001271 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001272 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001273 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1274 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001275 } else {
1276 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001277 bool needsTextureDomain = needs_texture_domain(bitmap,
1278 srcRect,
1279 params,
joshualitt5531d512014-12-17 15:50:11 -08001280 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001281 doBicubic);
1282 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001283 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001284 srcRect,
1285 params,
1286 paint,
1287 flags,
1288 doBicubic,
1289 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001290 }
1291}
1292
1293// Break 'bitmap' into several tiles to draw it since it has already
1294// been determined to be too large to fit in VRAM
1295void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001296 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001297 const SkRect& srcRect,
1298 const SkIRect& clippedSrcIRect,
1299 const GrTextureParams& params,
1300 const SkPaint& paint,
1301 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001302 int tileSize,
1303 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001304 // The following pixel lock is technically redundant, but it is desirable
1305 // to lock outside of the tile loop to prevent redecoding the whole image
1306 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1307 // is larger than the limit of the discardable memory pool.
1308 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001309 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1310
1311 int nx = bitmap.width() / tileSize;
1312 int ny = bitmap.height() / tileSize;
1313 for (int x = 0; x <= nx; x++) {
1314 for (int y = 0; y <= ny; y++) {
1315 SkRect tileR;
1316 tileR.set(SkIntToScalar(x * tileSize),
1317 SkIntToScalar(y * tileSize),
1318 SkIntToScalar((x + 1) * tileSize),
1319 SkIntToScalar((y + 1) * tileSize));
1320
1321 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1322 continue;
1323 }
1324
1325 if (!tileR.intersect(srcRect)) {
1326 continue;
1327 }
1328
1329 SkBitmap tmpB;
1330 SkIRect iTileR;
1331 tileR.roundOut(&iTileR);
1332 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1333 SkIntToScalar(iTileR.fTop));
1334
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001335 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001336 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001337 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001338 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001339 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001340
robertphillipsec8bb942014-11-21 10:16:25 -08001341 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001342 SkIRect iClampRect;
1343
1344 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1345 // In bleed mode we want to always expand the tile on all edges
1346 // but stay within the bitmap bounds
1347 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1348 } else {
1349 // In texture-domain/clamp mode we only want to expand the
1350 // tile on edges interior to "srcRect" (i.e., we want to
1351 // not bleed across the original clamped edges)
1352 srcRect.roundOut(&iClampRect);
1353 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001354 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1355 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001356 }
1357
1358 if (bitmap.extractSubset(&tmpB, iTileR)) {
1359 // now offset it to make it "local" to our tmp bitmap
1360 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001361 GrTextureParams paramsTemp = params;
1362 bool needsTextureDomain = needs_texture_domain(bitmap,
1363 srcRect,
1364 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001365 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001366 bicubic);
1367 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001368 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001369 tileR,
1370 paramsTemp,
1371 paint,
1372 flags,
1373 bicubic,
1374 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001375 }
1376 }
1377 }
1378}
1379
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001380
1381/*
1382 * This is called by drawBitmap(), which has to handle images that may be too
1383 * large to be represented by a single texture.
1384 *
1385 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1386 * and that non-texture portion of the GrPaint has already been setup.
1387 */
1388void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001389 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001390 const SkRect& srcRect,
1391 const GrTextureParams& params,
1392 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001393 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001394 bool bicubic,
1395 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001396 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1397 bitmap.height() <= fContext->getMaxTextureSize());
1398
1399 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001400 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001401 if (NULL == texture) {
1402 return;
1403 }
1404
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001405 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001406 SkRect paintRect;
1407 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1408 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1409 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1410 SkScalarMul(srcRect.fTop, hInv),
1411 SkScalarMul(srcRect.fRight, wInv),
1412 SkScalarMul(srcRect.fBottom, hInv));
1413
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001414 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001415 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001416 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001417 // Use a constrained texture domain to avoid color bleeding
1418 SkScalar left, top, right, bottom;
1419 if (srcRect.width() > SK_Scalar1) {
1420 SkScalar border = SK_ScalarHalf / texture->width();
1421 left = paintRect.left() + border;
1422 right = paintRect.right() - border;
1423 } else {
1424 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1425 }
1426 if (srcRect.height() > SK_Scalar1) {
1427 SkScalar border = SK_ScalarHalf / texture->height();
1428 top = paintRect.top() + border;
1429 bottom = paintRect.bottom() - border;
1430 } else {
1431 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1432 }
1433 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001434 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001435 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001436 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001437 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001438 SkMatrix::I(),
1439 textureDomain,
1440 GrTextureDomain::kClamp_Mode,
1441 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001442 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001443 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001444 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1445 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001446 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001447 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001448 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001449 }
1450
1451 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1452 // the rest from the SkPaint.
1453 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001454 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001455 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001456 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1457 SkColor2GrColor(paint.getColor());
joshualitt25d9c152015-02-18 12:29:52 -08001458 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001459
joshualitt570d2f82015-02-25 13:19:48 -08001460 fContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dstRect,
1461 paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001462}
1463
fmalita2d97bc12014-11-20 10:44:58 -08001464bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
1465 const SkImageFilter* filter,
1466 const SkImageFilter::Context& ctx,
1467 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001468 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001469
1470 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1471 // (https://code.google.com/p/skia/issues/detail?id=3148).
1472 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001473
1474 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001475 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001476 } else {
1477 return false;
1478 }
1479}
1480
1481void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1482 int left, int top, const SkPaint& paint) {
1483 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001484 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001485
1486 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1487 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1488 return;
1489 }
1490
1491 int w = bitmap.width();
1492 int h = bitmap.height();
1493
1494 GrTexture* texture;
1495 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001496 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001497 if (!texture) {
1498 return;
1499 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001500
1501 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001502 // This bitmap will own the filtered result as a texture.
1503 SkBitmap filteredBitmap;
1504
bsalomon49f085d2014-09-05 13:34:00 -07001505 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001506 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001507 SkMatrix matrix(*draw.fMatrix);
1508 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001509 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001510 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001511 // This cache is transient, and is freed (along with all its contained
1512 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001513 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001514 if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap,
1515 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001516 texture = (GrTexture*) filteredBitmap.getTexture();
1517 w = filteredBitmap.width();
1518 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001519 left += offset.x();
1520 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001521 } else {
1522 return;
1523 }
1524 }
1525
1526 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001527 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001528
joshualitt25d9c152015-02-18 12:29:52 -08001529 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1530 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001531
joshualitt25d9c152015-02-18 12:29:52 -08001532 fContext->drawNonAARectToRect(fRenderTarget,
joshualitt570d2f82015-02-25 13:19:48 -08001533 fClip,
joshualitt25d9c152015-02-18 12:29:52 -08001534 grPaint,
joshualitt16b27892014-12-18 07:47:16 -08001535 SkMatrix::I(),
1536 SkRect::MakeXYWH(SkIntToScalar(left),
1537 SkIntToScalar(top),
1538 SkIntToScalar(w),
1539 SkIntToScalar(h)),
1540 SkRect::MakeXYWH(0,
1541 0,
1542 SK_Scalar1 * w / texture->width(),
1543 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001544}
1545
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001546void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001547 const SkRect* src, const SkRect& dst,
1548 const SkPaint& paint,
1549 SkCanvas::DrawBitmapRectFlags flags) {
1550 SkMatrix matrix;
1551 SkRect bitmapBounds, tmpSrc;
1552
1553 bitmapBounds.set(0, 0,
1554 SkIntToScalar(bitmap.width()),
1555 SkIntToScalar(bitmap.height()));
1556
1557 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001558 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001559 tmpSrc = *src;
1560 } else {
1561 tmpSrc = bitmapBounds;
1562 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001563
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001564 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1565
1566 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001567 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001568 if (!bitmapBounds.contains(tmpSrc)) {
1569 if (!tmpSrc.intersect(bitmapBounds)) {
1570 return; // nothing to draw
1571 }
1572 }
1573 }
1574
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001575 SkRect tmpDst;
1576 matrix.mapRect(&tmpDst, tmpSrc);
1577
1578 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1579 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1580 // Translate so that tempDst's top left is at the origin.
1581 matrix = *origDraw.fMatrix;
1582 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1583 draw.writable()->fMatrix = &matrix;
1584 }
1585 SkSize dstSize;
1586 dstSize.fWidth = tmpDst.width();
1587 dstSize.fHeight = tmpDst.height();
1588
1589 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001590}
1591
1592void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1593 int x, int y, const SkPaint& paint) {
1594 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001595 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001596 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001597
1598 // TODO: If the source device covers the whole of this device, we could
1599 // omit fNeedsClear -related flushing.
1600 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001601
1602 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001603 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001604
1605 GrRenderTarget* devRT = dev->accessRenderTarget();
1606 GrTexture* devTex;
1607 if (NULL == (devTex = devRT->asTexture())) {
1608 return;
1609 }
1610
robertphillips7b9e8a42014-12-11 08:20:31 -08001611 const SkImageInfo ii = dev->imageInfo();
1612 int w = ii.width();
1613 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001614
1615 SkImageFilter* filter = paint.getImageFilter();
1616 // This bitmap will own the filtered result as a texture.
1617 SkBitmap filteredBitmap;
1618
bsalomon49f085d2014-09-05 13:34:00 -07001619 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001620 SkIPoint offset = SkIPoint::Make(0, 0);
1621 SkMatrix matrix(*draw.fMatrix);
1622 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001623 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001624 // This cache is transient, and is freed (along with all its contained
1625 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001626 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001627 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001628 if (this->filterTexture(fContext, devTex, filter, ctx, &filteredBitmap,
1629 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001630 devTex = filteredBitmap.getTexture();
1631 w = filteredBitmap.width();
1632 h = filteredBitmap.height();
1633 x += offset.fX;
1634 y += offset.fY;
1635 } else {
1636 return;
1637 }
1638 }
1639
1640 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001641 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001642
joshualitt25d9c152015-02-18 12:29:52 -08001643 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1644 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001645
1646 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1647 SkIntToScalar(y),
1648 SkIntToScalar(w),
1649 SkIntToScalar(h));
1650
1651 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1652 // scratch texture).
1653 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1654 SK_Scalar1 * h / devTex->height());
1655
joshualitt570d2f82015-02-25 13:19:48 -08001656 fContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, SkMatrix::I(), dstRect,
1657 srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001658}
1659
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001660bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001661 return filter->canFilterImageGPU();
1662}
1663
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001664bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001665 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001666 SkBitmap* result, SkIPoint* offset) {
1667 // want explicitly our impl, so guard against a subclass of us overriding it
1668 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1669 return false;
1670 }
1671
1672 SkAutoLockPixels alp(src, !src.getTexture());
1673 if (!src.getTexture() && !src.readyToDraw()) {
1674 return false;
1675 }
1676
1677 GrTexture* texture;
1678 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1679 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001680 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001681
fmalita2d97bc12014-11-20 10:44:58 -08001682 return this->filterTexture(fContext, texture, filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001683}
1684
1685///////////////////////////////////////////////////////////////////////////////
1686
1687// must be in SkCanvas::VertexMode order
1688static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1689 kTriangles_GrPrimitiveType,
1690 kTriangleStrip_GrPrimitiveType,
1691 kTriangleFan_GrPrimitiveType,
1692};
1693
1694void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1695 int vertexCount, const SkPoint vertices[],
1696 const SkPoint texs[], const SkColor colors[],
1697 SkXfermode* xmode,
1698 const uint16_t indices[], int indexCount,
1699 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001700 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001701 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001702
dandov32a311b2014-07-15 19:46:26 -07001703 const uint16_t* outIndices;
1704 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1705 GrPrimitiveType primType;
1706 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001707
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001708 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1709 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001710
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001711 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001712
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001713 SkPaint copy(paint);
1714 copy.setStyle(SkPaint::kStroke_Style);
1715 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001716
dandov32a311b2014-07-15 19:46:26 -07001717 // we ignore the shader if texs is null.
joshualitt25d9c152015-02-18 12:29:52 -08001718 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy,
1719 SkColor2GrColor(copy.getColor()), NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001720
dandov32a311b2014-07-15 19:46:26 -07001721 primType = kLines_GrPrimitiveType;
1722 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001723 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001724 switch (vmode) {
1725 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001726 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001727 break;
1728 case SkCanvas::kTriangleStrip_VertexMode:
1729 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001730 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001731 break;
1732 }
mtklein533eb782014-08-27 10:39:42 -07001733
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001734 VertState state(vertexCount, indices, indexCount);
1735 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001736
dandov32a311b2014-07-15 19:46:26 -07001737 //number of indices for lines per triangle with kLines
1738 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001739
dandov32a311b2014-07-15 19:46:26 -07001740 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1741 outIndices = outAlloc.get();
1742 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001743 int i = 0;
1744 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001745 auxIndices[i] = state.f0;
1746 auxIndices[i + 1] = state.f1;
1747 auxIndices[i + 2] = state.f1;
1748 auxIndices[i + 3] = state.f2;
1749 auxIndices[i + 4] = state.f2;
1750 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001751 i += 6;
1752 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001753 } else {
dandov32a311b2014-07-15 19:46:26 -07001754 outIndices = indices;
1755 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001756
dandov32a311b2014-07-15 19:46:26 -07001757 if (NULL == texs || NULL == paint.getShader()) {
joshualitt25d9c152015-02-18 12:29:52 -08001758 SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1759 SkColor2GrColor(paint.getColor()),
dandov32a311b2014-07-15 19:46:26 -07001760 NULL == colors, &grPaint);
1761 } else {
joshualitt25d9c152015-02-18 12:29:52 -08001762 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix,
1763 NULL == colors, &grPaint);
dandov32a311b2014-07-15 19:46:26 -07001764 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001765 }
1766
mtklein2583b622014-06-04 08:20:41 -07001767#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001768 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001769 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1770 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001771 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001772 }
1773 }
mtklein2583b622014-06-04 08:20:41 -07001774#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001775
1776 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001777 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001778 // need to convert byte order and from non-PM to PM
1779 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001780 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001781 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001782 color = colors[i];
1783 if (paint.getAlpha() != 255) {
1784 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1785 }
1786 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001787 }
1788 colors = convertedColors.get();
1789 }
joshualitt25d9c152015-02-18 12:29:52 -08001790 fContext->drawVertices(fRenderTarget,
joshualitt570d2f82015-02-25 13:19:48 -08001791 fClip,
joshualitt25d9c152015-02-18 12:29:52 -08001792 grPaint,
joshualitt5531d512014-12-17 15:50:11 -08001793 *draw.fMatrix,
dandov32a311b2014-07-15 19:46:26 -07001794 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001795 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001796 vertices,
1797 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001798 colors,
dandov32a311b2014-07-15 19:46:26 -07001799 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001800 indexCount);
1801}
1802
1803///////////////////////////////////////////////////////////////////////////////
1804
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001805void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001806 size_t byteLength, SkScalar x, SkScalar y,
1807 const SkPaint& paint) {
1808 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001809 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001810
jvanverth8c27a182014-10-14 08:45:50 -07001811 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -08001812 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001813
jvanverth8c27a182014-10-14 08:45:50 -07001814 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001815
joshualitt570d2f82015-02-25 13:19:48 -08001816 if (!fTextContext->drawText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
1817 (const char *)text, byteLength, x, y)) {
jvanverth8c27a182014-10-14 08:45:50 -07001818 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001819 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001820 }
1821}
1822
fmalita05c4a432014-09-29 06:29:53 -07001823void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1824 const SkScalar pos[], int scalarsPerPos,
1825 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001826 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001827 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001828
jvanverth8c27a182014-10-14 08:45:50 -07001829 GrPaint grPaint;
joshualitt25d9c152015-02-18 12:29:52 -08001830 SkPaint2GrPaintShader(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001831
jvanverth8c27a182014-10-14 08:45:50 -07001832 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001833
joshualitt570d2f82015-02-25 13:19:48 -08001834 if (!fTextContext->drawPosText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
1835 (const char *)text, byteLength, pos, scalarsPerPos, offset)) {
jvanverth8c27a182014-10-14 08:45:50 -07001836 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001837 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001838 }
1839}
1840
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001841///////////////////////////////////////////////////////////////////////////////
1842
reedb2db8982014-11-13 12:41:02 -08001843bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001844 if (paint.getShader() ||
reedb2db8982014-11-13 12:41:02 -08001845 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001846 paint.getMaskFilter() ||
1847 paint.getRasterizer() ||
1848 paint.getColorFilter() ||
1849 paint.getPathEffect() ||
1850 paint.isFakeBoldText() ||
reedb2db8982014-11-13 12:41:02 -08001851 paint.getStyle() != SkPaint::kFill_Style)
1852 {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001853 return true;
1854 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001855 return false;
1856}
1857
1858void SkGpuDevice::flush() {
1859 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001860 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001861}
1862
1863///////////////////////////////////////////////////////////////////////////////
1864
reed173e5fe2015-03-13 20:05:18 -07001865SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(const CreateInfo& cinfo) {
bsalomonf2703d82014-10-28 14:33:06 -07001866 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001867 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001868 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001869 desc.fWidth = cinfo.fInfo.width();
1870 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001871 desc.fSampleCnt = fRenderTarget->numSamples();
1872
1873 SkAutoTUnref<GrTexture> texture;
1874 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001875 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001876
hcm4396fa52014-10-27 21:43:30 -07001877 // layers are never draw in repeat modes, so we can request an approx
1878 // match and ignore any padding.
reed173e5fe2015-03-13 20:05:18 -07001879 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == cinfo.fUsage) ?
hcm4396fa52014-10-27 21:43:30 -07001880 GrContext::kApprox_ScratchTexMatch :
1881 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001882 texture.reset(fContext->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001883
1884 if (texture) {
1885 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry);
1886 return SkGpuDevice::Create(texture->asRenderTarget(), &props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001887 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001888 SkErrorInternals::SetError( kInternalError_SkError,
1889 "---- failed to create compatible device texture [%d %d]\n",
1890 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001891 return NULL;
1892 }
1893}
1894
reed4a8126e2014-09-22 07:29:03 -07001895SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001896 // TODO: Change the signature of newSurface to take a budgeted parameter.
1897 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
1898 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->numSamples(),
1899 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001900}
1901
robertphillips30d2cc62014-09-24 08:52:18 -07001902bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001903 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001904#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001905 // todo: should handle this natively
1906 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001907 return false;
1908 }
1909
robertphillips82365912014-11-12 09:32:34 -08001910 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips81f71b62014-11-11 04:54:49 -08001911
1912 const SkPicture::AccelData* data = mainPicture->EXPERIMENTAL_getAccelData(key);
1913 if (!data) {
1914 return false;
1915 }
1916
robertphillipse5524cd2015-02-20 12:30:26 -08001917 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1918 if (0 == gpuData->numBlocks()) {
1919 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001920 }
1921
robertphillipsfd61ed02014-10-28 07:21:44 -07001922 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001923
robertphillipse5524cd2015-02-20 12:30:26 -08001924 SkIRect iBounds;
1925 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1926 return false;
1927 }
1928
1929 SkRect clipBounds = SkRect::Make(iBounds);
1930
1931 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1932
robertphillipsfd61ed02014-10-28 07:21:44 -07001933 GrLayerHoister::FindLayersToAtlas(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 &atlasedNeedRendering, &atlasedRecycled,
1937 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001938
1939 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1940
1941 SkTDArray<GrHoistedLayer> needRendering, recycled;
1942
robertphillipse5524cd2015-02-20 12:30:26 -08001943 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1944
robertphillipsfd61ed02014-10-28 07:21:44 -07001945 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001946 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001947 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001948 &needRendering, &recycled,
1949 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001950
1951 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001952
robertphillips64bf7672014-08-21 13:07:35 -07001953 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001954 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1955 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001956
robertphillipsfd61ed02014-10-28 07:21:44 -07001957 GrLayerHoister::UnlockLayers(fContext, needRendering);
1958 GrLayerHoister::UnlockLayers(fContext, recycled);
1959 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1960 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001961
1962 return true;
robertphillips63242d72014-12-04 08:31:02 -08001963#else
1964 return false;
1965#endif
robertphillips64bf7672014-08-21 13:07:35 -07001966}
1967
senorblancobe129b22014-08-08 07:14:35 -07001968SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001969 // We always return a transient cache, so it is freed after each
1970 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001971 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001972}
reedf037e0b2014-10-30 11:34:15 -07001973
1974#endif