blob: 90e53a0b1020cf3b36df9e6847944dee2c6f16bf [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
robertphillipsccb1b572015-05-27 11:02:55 -070010#include "GrBlurUtils.h"
kkinnunenabcfab42015-02-22 22:53:44 -080011#include "GrContext.h"
robertphillipsea461502015-05-26 11:38:03 -070012#include "GrDrawContext.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"
joshualitt8f94bb22015-04-28 07:04:11 -070018#include "GrTextContext.h"
egdanielbbcb38d2014-06-19 10:19:29 -070019#include "GrTracing.h"
robertphillips30d78412014-11-24 09:49:17 -080020#include "SkCanvasPriv.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000021#include "SkDeviceImageFilterProxy.h"
22#include "SkDrawProcs.h"
kkinnunenabcfab42015-02-22 22:53:44 -080023#include "SkErrorInternals.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000024#include "SkGlyphCache.h"
kkinnunenabcfab42015-02-22 22:53:44 -080025#include "SkGrTexturePixelRef.h"
reeda85d4d02015-05-06 12:56:48 -070026#include "SkImage_Base.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000027#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080028#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000029#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000030#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000031#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070032#include "SkPictureData.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkRRect.h"
kkinnunenabcfab42015-02-22 22:53:44 -080034#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000036#include "SkSurface.h"
kkinnunenabcfab42015-02-22 22:53:44 -080037#include "SkSurface_Gpu.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000038#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000039#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000040#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070041#include "SkXfermode.h"
kkinnunenabcfab42015-02-22 22:53:44 -080042#include "effects/GrBicubicEffect.h"
43#include "effects/GrDashingEffect.h"
44#include "effects/GrSimpleTextureEffect.h"
45#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000046
reedf037e0b2014-10-30 11:34:15 -070047#if SK_SUPPORT_GPU
48
senorblanco55b6d8b2014-07-30 11:26:46 -070049enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
50
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000051#if 0
52 extern bool (*gShouldDrawProc)();
joshualitt5531d512014-12-17 15:50:11 -080053 #define CHECK_SHOULD_DRAW(draw) \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000054 do { \
55 if (gShouldDrawProc && !gShouldDrawProc()) return; \
joshualitt5531d512014-12-17 15:50:11 -080056 this->prepareDraw(draw); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000057 } while (0)
58#else
joshualitt5531d512014-12-17 15:50:11 -080059 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000060#endif
61
62// This constant represents the screen alignment criterion in texels for
63// requiring texture domain clamping to prevent color bleeding when drawing
64// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000065#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000066
67#define DO_DEFERRED_CLEAR() \
68 do { \
bsalomonafe30052015-01-16 07:32:33 -080069 if (fNeedClear) { \
70 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000071 } \
72 } while (false) \
73
74///////////////////////////////////////////////////////////////////////////////
75
76#define CHECK_FOR_ANNOTATION(paint) \
77 do { if (paint.getAnnotation()) { return; } } while (0)
78
79///////////////////////////////////////////////////////////////////////////////
80
bsalomonbcf0a522014-10-08 08:40:09 -070081// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
82// just accesses the backing GrTexture. Otherwise, it creates a cached texture
83// representation and releases it in the destructor.
84class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040085public:
bsalomonbcf0a522014-10-08 08:40:09 -070086 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070087
bsalomonbcf0a522014-10-08 08:40:09 -070088 AutoBitmapTexture(GrContext* context,
89 const SkBitmap& bitmap,
90 const GrTextureParams* params,
91 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040092 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070093 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040094 }
95
bsalomonbcf0a522014-10-08 08:40:09 -070096 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040097 const SkBitmap& bitmap,
98 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -070099 // Either get the texture directly from the bitmap, or else use the cache and
100 // remember to unref it.
101 if (GrTexture* bmpTexture = bitmap.getTexture()) {
102 fTexture.reset(NULL);
103 return bmpTexture;
104 } else {
105 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
106 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400107 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400108 }
109
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000110private:
bsalomonbcf0a522014-10-08 08:40:09 -0700111 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000112};
113
114///////////////////////////////////////////////////////////////////////////////
115
116struct GrSkDrawProcs : public SkDrawProcs {
117public:
118 GrContext* fContext;
119 GrTextContext* fTextContext;
120 GrFontScaler* fFontScaler; // cached in the skia glyphcache
121};
122
123///////////////////////////////////////////////////////////////////////////////
124
bsalomonafe30052015-01-16 07:32:33 -0800125SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags) {
senorblancod0d37ca2015-04-02 04:54:56 -0700126 return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, flags);
127}
128
129SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
130 const SkSurfaceProps* props, unsigned flags) {
bsalomonafe30052015-01-16 07:32:33 -0800131 if (!rt || rt->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000132 return NULL;
133 }
senorblancod0d37ca2015-04-02 04:54:56 -0700134 return SkNEW_ARGS(SkGpuDevice, (rt, width, height, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000135}
136
bsalomonafe30052015-01-16 07:32:33 -0800137static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps* props) {
138 if (props) {
139 return SkDeviceProperties(props->pixelGeometry());
140 } else {
141 return SkDeviceProperties(SkDeviceProperties::kLegacyLCD_InitType);
142 }
reedb2db8982014-11-13 12:41:02 -0800143}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000144
bsalomonafe30052015-01-16 07:32:33 -0800145static SkSurfaceProps copy_or_default_props(const SkSurfaceProps* props) {
146 if (props) {
147 return SkSurfaceProps(*props);
148 } else {
149 return SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType);
150 }
151}
152
senorblancod0d37ca2015-04-02 04:54:56 -0700153SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
154 const SkSurfaceProps* props, unsigned flags)
reedb2db8982014-11-13 12:41:02 -0800155 : INHERITED(surfaceprops_to_deviceprops(props))
bsalomonafe30052015-01-16 07:32:33 -0800156 , fSurfaceProps(copy_or_default_props(props))
reedb2db8982014-11-13 12:41:02 -0800157{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000158 fDrawProcs = NULL;
159
bsalomonafe30052015-01-16 07:32:33 -0800160 fContext = SkRef(rt->getContext());
161 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000162
bsalomonafe30052015-01-16 07:32:33 -0800163 fRenderTarget = SkRef(rt);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000164
senorblancod0d37ca2015-04-02 04:54:56 -0700165 SkImageInfo info = rt->surfacePriv().info().makeWH(width, height);
bsalomonafe30052015-01-16 07:32:33 -0800166 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt));
bsalomonafbf2d62014-09-30 12:18:44 -0700167 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700168 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700169
bsalomonafe30052015-01-16 07:32:33 -0800170 bool useDFT = fSurfaceProps.isUseDistanceFieldFonts();
robertphillipsccb1b572015-05-27 11:02:55 -0700171 fTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFT);
robertphillipsea461502015-05-26 11:38:03 -0700172 fDrawContext.reset(SkRef(fContext->drawContext()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000173}
174
kkinnunenabcfab42015-02-22 22:53:44 -0800175GrRenderTarget* SkGpuDevice::CreateRenderTarget(GrContext* context, SkSurface::Budgeted budgeted,
176 const SkImageInfo& origInfo, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000177 if (kUnknown_SkColorType == origInfo.colorType() ||
178 origInfo.width() < 0 || origInfo.height() < 0) {
179 return NULL;
180 }
181
bsalomonafe30052015-01-16 07:32:33 -0800182 if (!context) {
183 return NULL;
184 }
185
reede5ea5002014-09-03 11:54:58 -0700186 SkColorType ct = origInfo.colorType();
187 SkAlphaType at = origInfo.alphaType();
reede5ea5002014-09-03 11:54:58 -0700188 if (kRGB_565_SkColorType == ct) {
189 at = kOpaque_SkAlphaType; // force this setting
bsalomonafe30052015-01-16 07:32:33 -0800190 } else if (ct != kBGRA_8888_SkColorType && ct != kRGBA_8888_SkColorType) {
191 // Fall back from whatever ct was to default of kRGBA or kBGRA which is aliased as kN32
reede5ea5002014-09-03 11:54:58 -0700192 ct = kN32_SkColorType;
bsalomonafe30052015-01-16 07:32:33 -0800193 }
194 if (kOpaque_SkAlphaType != at) {
195 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000196 }
reede5ea5002014-09-03 11:54:58 -0700197 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000198
bsalomonf2703d82014-10-28 14:33:06 -0700199 GrSurfaceDesc desc;
200 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000201 desc.fWidth = info.width();
202 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000203 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000204 desc.fSampleCnt = sampleCount;
bsalomond309e7a2015-04-30 14:18:54 -0700205 GrTexture* texture = context->textureProvider()->createTexture(
206 desc, SkToBool(budgeted), NULL, 0);
kkinnunenabcfab42015-02-22 22:53:44 -0800207 if (NULL == texture) {
208 return NULL;
209 }
210 SkASSERT(NULL != texture->asRenderTarget());
211 return texture->asRenderTarget();
212}
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000213
kkinnunenabcfab42015-02-22 22:53:44 -0800214SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgeted,
215 const SkImageInfo& info, int sampleCount,
216 const SkSurfaceProps* props, unsigned flags) {
217
218 SkAutoTUnref<GrRenderTarget> rt(CreateRenderTarget(context, budgeted, info, sampleCount));
219 if (NULL == rt) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000220 return NULL;
221 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000222
senorblancod0d37ca2015-04-02 04:54:56 -0700223 return SkNEW_ARGS(SkGpuDevice, (rt, info.width(), info.height(), props, flags));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000224}
225
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000226SkGpuDevice::~SkGpuDevice() {
227 if (fDrawProcs) {
228 delete fDrawProcs;
229 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000230
robertphillipsea461502015-05-26 11:38:03 -0700231 SkDELETE(fTextContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000232
bsalomon32d0b3b2014-08-29 07:50:23 -0700233 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000234 fContext->unref();
235}
236
237///////////////////////////////////////////////////////////////////////////////
238
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000239bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
240 int x, int y) {
241 DO_DEFERRED_CLEAR();
242
243 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000244 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000245 if (kUnknown_GrPixelConfig == config) {
246 return false;
247 }
248
249 uint32_t flags = 0;
250 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
251 flags = GrContext::kUnpremul_PixelOpsFlag;
252 }
253 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
254 config, dstPixels, dstRowBytes, flags);
255}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000256
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000257bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
258 int x, int y) {
259 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000260 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000261 if (kUnknown_GrPixelConfig == config) {
262 return false;
263 }
264 uint32_t flags = 0;
265 if (kUnpremul_SkAlphaType == info.alphaType()) {
266 flags = GrContext::kUnpremul_PixelOpsFlag;
267 }
268 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
269
270 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700271 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000272
273 return true;
274}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000275
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000276const SkBitmap& SkGpuDevice::onAccessBitmap() {
277 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700278 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000279}
280
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000281void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
282 INHERITED::onAttachToCanvas(canvas);
283
284 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
joshualitt44701df2015-02-23 14:44:57 -0800285 fClipStack.reset(SkRef(canvas->getClipStack()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000286}
287
288void SkGpuDevice::onDetachFromCanvas() {
289 INHERITED::onDetachFromCanvas();
joshualitt570d2f82015-02-25 13:19:48 -0800290 fClip.reset();
joshualitt44701df2015-02-23 14:44:57 -0800291 fClipStack.reset(NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000292}
293
294// call this every draw call, to ensure that the context reflects our state,
295// and not the state from some other canvas/device
joshualitt5531d512014-12-17 15:50:11 -0800296void SkGpuDevice::prepareDraw(const SkDraw& draw) {
joshualitt44701df2015-02-23 14:44:57 -0800297 SkASSERT(fClipStack.get());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000298
joshualitt44701df2015-02-23 14:44:57 -0800299 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000300
joshualitt570d2f82015-02-25 13:19:48 -0800301 fClip.setClipStack(fClipStack, &this->getOrigin());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000302
303 DO_DEFERRED_CLEAR();
304}
305
306GrRenderTarget* SkGpuDevice::accessRenderTarget() {
robertphillips7156b092015-05-14 08:54:12 -0700307 DO_DEFERRED_CLEAR();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000308 return fRenderTarget;
309}
310
reed8eddfb52014-12-04 07:50:14 -0800311void SkGpuDevice::clearAll() {
312 GrColor color = 0;
313 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
314 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
robertphillipsea461502015-05-26 11:38:03 -0700315 fDrawContext->clear(fRenderTarget, &rect, color, true);
bsalomonafe30052015-01-16 07:32:33 -0800316 fNeedClear = false;
reed8eddfb52014-12-04 07:50:14 -0800317}
318
kkinnunenabcfab42015-02-22 22:53:44 -0800319void SkGpuDevice::replaceRenderTarget(bool shouldRetainContent) {
320 // Caller must have accessed the render target, because it knows the rt must be replaced.
321 SkASSERT(!fNeedClear);
322
323 SkSurface::Budgeted budgeted =
324 fRenderTarget->resourcePriv().isBudgeted() ? SkSurface::kYes_Budgeted
325 : SkSurface::kNo_Budgeted;
326
327 SkAutoTUnref<GrRenderTarget> newRT(CreateRenderTarget(
328 fRenderTarget->getContext(), budgeted, this->imageInfo(), fRenderTarget->numSamples()));
329
330 if (NULL == newRT) {
331 return;
332 }
333
334 if (shouldRetainContent) {
335 if (fRenderTarget->wasDestroyed()) {
336 return;
337 }
338 this->context()->copySurface(newRT, fRenderTarget);
339 }
340
341 SkASSERT(fRenderTarget != newRT);
342
343 fRenderTarget->unref();
344 fRenderTarget = newRT.detach();
345
346 SkASSERT(fRenderTarget->surfacePriv().info() == fLegacyBitmap.info());
347 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (fRenderTarget->surfacePriv().info(), fRenderTarget));
348 fLegacyBitmap.setPixelRef(pr)->unref();
robertphillipsea461502015-05-26 11:38:03 -0700349
350 fDrawContext.reset(SkRef(fRenderTarget->getContext()->drawContext()));
kkinnunenabcfab42015-02-22 22:53:44 -0800351}
352
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000353///////////////////////////////////////////////////////////////////////////////
354
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000355void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -0800356 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700357 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000358
359 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700360 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
361 return;
362 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000363
robertphillipsea461502015-05-26 11:38:03 -0700364 fDrawContext->drawPaint(fRenderTarget, fClip, grPaint, *draw.fMatrix);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000365}
366
367// must be in SkCanvas::PointMode order
368static const GrPrimitiveType gPointMode2PrimtiveType[] = {
369 kPoints_GrPrimitiveType,
370 kLines_GrPrimitiveType,
371 kLineStrip_GrPrimitiveType
372};
373
374void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
375 size_t count, const SkPoint pts[], const SkPaint& paint) {
376 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800377 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000378
379 SkScalar width = paint.getStrokeWidth();
380 if (width < 0) {
381 return;
382 }
383
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000384 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700385 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
386 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700387 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
388 &grPaint)) {
389 return;
390 }
egdaniele61c4112014-06-12 10:24:21 -0700391 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700392 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700393 path.moveTo(pts[0]);
394 path.lineTo(pts[1]);
robertphillipsea461502015-05-26 11:38:03 -0700395 fDrawContext->drawPath(fRenderTarget, fClip, grPaint, *draw.fMatrix, path, strokeInfo);
egdaniele61c4112014-06-12 10:24:21 -0700396 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000397 }
398
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000399 // we only handle hairlines and paints without path effects or mask filters,
400 // else we let the SkDraw call our drawPath()
401 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
402 draw.drawPoints(mode, count, pts, paint, true);
403 return;
404 }
405
406 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700407 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
408 return;
409 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000410
robertphillipsea461502015-05-26 11:38:03 -0700411 fDrawContext->drawVertices(fRenderTarget,
412 fClip,
413 grPaint,
414 *draw.fMatrix,
415 gPointMode2PrimtiveType[mode],
416 SkToS32(count),
417 (SkPoint*)pts,
418 NULL,
419 NULL,
420 NULL,
421 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000422}
423
424///////////////////////////////////////////////////////////////////////////////
425
426void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
427 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700428 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
429
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000430 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800431 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000432
433 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
434 SkScalar width = paint.getStrokeWidth();
435
436 /*
437 We have special code for hairline strokes, miter-strokes, bevel-stroke
438 and fills. Anything else we just call our path code.
439 */
440 bool usePath = doStroke && width > 0 &&
441 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
442 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
443 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700444
445 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000446 usePath = true;
447 }
egdanield58a0ba2014-06-11 10:30:05 -0700448
joshualitt5531d512014-12-17 15:50:11 -0800449 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000450#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
451 if (doStroke) {
452#endif
453 usePath = true;
454#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
455 } else {
joshualitt5531d512014-12-17 15:50:11 -0800456 usePath = !draw.fMatrix->preservesRightAngles();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000457 }
458#endif
459 }
460 // until we can both stroke and fill rectangles
461 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
462 usePath = true;
463 }
464
egdanield58a0ba2014-06-11 10:30:05 -0700465 GrStrokeInfo strokeInfo(paint);
466
467 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700468 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700469 usePath = true;
470 }
471
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000472 if (usePath) {
473 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700474 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000475 path.addRect(rect);
476 this->drawPath(draw, path, paint, NULL, true);
477 return;
478 }
479
480 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700481 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
482 return;
483 }
Mike Klein744fb732014-06-23 15:13:26 -0400484
robertphillipsea461502015-05-26 11:38:03 -0700485 fDrawContext->drawRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000486}
487
488///////////////////////////////////////////////////////////////////////////////
489
490void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
joshualitt5531d512014-12-17 15:50:11 -0800491 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700492 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000493 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800494 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000495
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000496 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700497 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
498 return;
499 }
Mike Klein744fb732014-06-23 15:13:26 -0400500
egdanield58a0ba2014-06-11 10:30:05 -0700501 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000502 if (paint.getMaskFilter()) {
503 // try to hit the fast path for drawing filtered round rects
504
505 SkRRect devRRect;
joshualitt5531d512014-12-17 15:50:11 -0800506 if (rect.transform(*draw.fMatrix, &devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000507 if (devRRect.allCornersCircular()) {
508 SkRect maskRect;
509 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
joshualitt5531d512014-12-17 15:50:11 -0800510 draw.fClip->getBounds(),
511 *draw.fMatrix,
512 &maskRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000513 SkIRect finalIRect;
514 maskRect.roundOut(&finalIRect);
515 if (draw.fClip->quickReject(finalIRect)) {
516 // clipped out
517 return;
518 }
joshualitt25d9c152015-02-18 12:29:52 -0800519 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext,
520 fRenderTarget,
521 &grPaint,
joshualitt570d2f82015-02-25 13:19:48 -0800522 fClip,
joshualitt5531d512014-12-17 15:50:11 -0800523 *draw.fMatrix,
kkinnunend156d362015-05-18 22:23:54 -0700524 strokeInfo,
egdanield58a0ba2014-06-11 10:30:05 -0700525 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000526 return;
527 }
528 }
529
530 }
531 }
532
533 }
534
egdanield58a0ba2014-06-11 10:30:05 -0700535 bool usePath = false;
536
537 if (paint.getMaskFilter()) {
538 usePath = true;
539 } else {
540 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700541 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700542 usePath = true;
543 }
544 }
545
546
547 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000548 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700549 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550 path.addRRect(rect);
551 this->drawPath(draw, path, paint, NULL, true);
552 return;
553 }
Mike Klein744fb732014-06-23 15:13:26 -0400554
robertphillipsea461502015-05-26 11:38:03 -0700555 fDrawContext->drawRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000556}
557
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000558void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800559 const SkRRect& inner, const SkPaint& paint) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000560 SkStrokeRec stroke(paint);
561 if (stroke.isFillStyle()) {
562
563 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800564 CHECK_SHOULD_DRAW(draw);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000565
566 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700567 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true,
568 &grPaint)) {
569 return;
570 }
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000571
572 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
robertphillipsea461502015-05-26 11:38:03 -0700573 fDrawContext->drawDRRect(fRenderTarget, fClip, grPaint, *draw.fMatrix, outer, inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000574 return;
575 }
576 }
577
578 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700579 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000580 path.addRRect(outer);
581 path.addRRect(inner);
582 path.setFillType(SkPath::kEvenOdd_FillType);
583
584 this->drawPath(draw, path, paint, NULL, true);
585}
586
587
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000588/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000589
590void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
591 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700592 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000593 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800594 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000595
egdanield58a0ba2014-06-11 10:30:05 -0700596 GrStrokeInfo strokeInfo(paint);
597
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000598 bool usePath = false;
599 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700600 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000601 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700602 } else {
603 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700604 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700605 usePath = true;
606 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000607 }
608
609 if (usePath) {
610 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700611 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000612 path.addOval(oval);
613 this->drawPath(draw, path, paint, NULL, true);
614 return;
615 }
616
617 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -0700618 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
619 return;
620 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000621
robertphillipsea461502015-05-26 11:38:03 -0700622 fDrawContext->drawOval(fRenderTarget, fClip, grPaint, *draw.fMatrix, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000623}
624
625#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000626
627///////////////////////////////////////////////////////////////////////////////
628
robertphillipsccb1b572015-05-27 11:02:55 -0700629static SkBitmap wrap_texture(GrTexture* texture, int width, int height) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000630 SkBitmap result;
senorblancod0d37ca2015-04-02 04:54:56 -0700631 result.setInfo(SkImageInfo::MakeN32Premul(width, height));
reed6c225732014-06-09 19:52:07 -0700632 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000633 return result;
634}
635
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000636void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
637 const SkPaint& paint, const SkMatrix* prePathMatrix,
638 bool pathIsMutable) {
639 CHECK_FOR_ANNOTATION(paint);
joshualitt5531d512014-12-17 15:50:11 -0800640 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -0700641 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000642
robertphillipsccb1b572015-05-27 11:02:55 -0700643 GrBlurUtils::drawPathWithMaskFilter(fContext, fDrawContext, fRenderTarget,
644 fClip, origSrcPath, paint,
645 *draw.fMatrix, prePathMatrix,
646 draw.fClip->getBounds(), pathIsMutable);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000647}
648
649static const int kBmpSmallTileSize = 1 << 10;
650
651static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
652 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
653 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
654 return tilesX * tilesY;
655}
656
657static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
658 if (maxTileSize <= kBmpSmallTileSize) {
659 return maxTileSize;
660 }
661
662 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
663 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
664
665 maxTileTotalTileSize *= maxTileSize * maxTileSize;
666 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
667
668 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
669 return kBmpSmallTileSize;
670 } else {
671 return maxTileSize;
672 }
673}
674
675// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
676// pixels from the bitmap are necessary.
677static void determine_clipped_src_rect(const GrContext* context,
joshualitt25d9c152015-02-18 12:29:52 -0800678 const GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800679 const GrClip& clip,
joshualitt5531d512014-12-17 15:50:11 -0800680 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000681 const SkBitmap& bitmap,
682 const SkRect* srcRectPtr,
683 SkIRect* clippedSrcIRect) {
joshualitt570d2f82015-02-25 13:19:48 -0800684 clip.getConservativeBounds(rt, clippedSrcIRect, NULL);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000685 SkMatrix inv;
joshualitt5531d512014-12-17 15:50:11 -0800686 if (!viewMatrix.invert(&inv)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000687 clippedSrcIRect->setEmpty();
688 return;
689 }
690 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
691 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700692 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000693 // we've setup src space 0,0 to map to the top left of the src rect.
694 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000695 if (!clippedSrcRect.intersect(*srcRectPtr)) {
696 clippedSrcIRect->setEmpty();
697 return;
698 }
699 }
700 clippedSrcRect.roundOut(clippedSrcIRect);
701 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
702 if (!clippedSrcIRect->intersect(bmpBounds)) {
703 clippedSrcIRect->setEmpty();
704 }
705}
706
707bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800708 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000709 const GrTextureParams& params,
710 const SkRect* srcRectPtr,
711 int maxTileSize,
712 int* tileSize,
713 SkIRect* clippedSrcRect) const {
714 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700715 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000716 return false;
717 }
718
719 // if it's larger than the max tile size, then we have no choice but tiling.
720 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
joshualitt570d2f82015-02-25 13:19:48 -0800721 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap,
722 srcRectPtr, clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000723 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
724 return true;
725 }
726
727 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
728 return false;
729 }
730
731 // if the entire texture is already in our cache then no reason to tile it
732 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
733 return false;
734 }
735
736 // At this point we know we could do the draw by uploading the entire bitmap
737 // as a texture. However, if the texture would be large compared to the
738 // cache size and we don't require most of it for this draw then tile to
739 // reduce the amount of upload and cache spill.
740
741 // assumption here is that sw bitmap size is a good proxy for its size as
742 // a texture
743 size_t bmpSize = bitmap.getSize();
744 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000745 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000746 if (bmpSize < cacheSize / 2) {
747 return false;
748 }
749
750 // Figure out how much of the src we will need based on the src rect and clipping.
joshualitt570d2f82015-02-25 13:19:48 -0800751 determine_clipped_src_rect(fContext, fRenderTarget, fClip, viewMatrix, bitmap, srcRectPtr,
joshualitt25d9c152015-02-18 12:29:52 -0800752 clippedSrcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000753 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
754 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
755 kBmpSmallTileSize * kBmpSmallTileSize;
756
757 return usedTileBytes < 2 * bmpSize;
758}
759
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000760void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000761 const SkBitmap& bitmap,
762 const SkMatrix& m,
763 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000764 SkMatrix concat;
765 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
766 if (!m.isIdentity()) {
767 concat.setConcat(*draw->fMatrix, m);
768 draw.writable()->fMatrix = &concat;
769 }
770 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000771}
772
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000773// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000774// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
775// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000776static inline void clamped_outset_with_offset(SkIRect* iRect,
777 int outset,
778 SkPoint* offset,
779 const SkIRect& clamp) {
780 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000781
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000782 int leftClampDelta = clamp.fLeft - iRect->fLeft;
783 if (leftClampDelta > 0) {
784 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000785 iRect->fLeft = clamp.fLeft;
786 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000787 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000788 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000789
790 int topClampDelta = clamp.fTop - iRect->fTop;
791 if (topClampDelta > 0) {
792 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000793 iRect->fTop = clamp.fTop;
794 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000795 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000796 }
797
798 if (iRect->fRight > clamp.fRight) {
799 iRect->fRight = clamp.fRight;
800 }
801 if (iRect->fBottom > clamp.fBottom) {
802 iRect->fBottom = clamp.fBottom;
803 }
804}
805
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000806static bool has_aligned_samples(const SkRect& srcRect,
807 const SkRect& transformedRect) {
808 // detect pixel disalignment
809 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
810 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
811 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
812 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
813 SkScalarAbs(transformedRect.width() - srcRect.width()) <
814 COLOR_BLEED_TOLERANCE &&
815 SkScalarAbs(transformedRect.height() - srcRect.height()) <
816 COLOR_BLEED_TOLERANCE) {
817 return true;
818 }
819 return false;
820}
821
822static bool may_color_bleed(const SkRect& srcRect,
823 const SkRect& transformedRect,
824 const SkMatrix& m) {
825 // Only gets called if has_aligned_samples returned false.
826 // So we can assume that sampling is axis aligned but not texel aligned.
827 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
828 SkRect innerSrcRect(srcRect), innerTransformedRect,
829 outerTransformedRect(transformedRect);
830 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
831 m.mapRect(&innerTransformedRect, innerSrcRect);
832
833 // The gap between outerTransformedRect and innerTransformedRect
834 // represents the projection of the source border area, which is
835 // problematic for color bleeding. We must check whether any
836 // destination pixels sample the border area.
837 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
838 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
839 SkIRect outer, inner;
840 outerTransformedRect.round(&outer);
841 innerTransformedRect.round(&inner);
842 // If the inner and outer rects round to the same result, it means the
843 // border does not overlap any pixel centers. Yay!
844 return inner != outer;
845}
846
847static bool needs_texture_domain(const SkBitmap& bitmap,
848 const SkRect& srcRect,
849 GrTextureParams &params,
850 const SkMatrix& contextMatrix,
851 bool bicubic) {
852 bool needsTextureDomain = false;
senorblancod0d37ca2015-04-02 04:54:56 -0700853 GrTexture* tex = bitmap.getTexture();
854 int width = tex ? tex->width() : bitmap.width();
855 int height = tex ? tex->height() : bitmap.height();
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000856
857 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
858 // Need texture domain if drawing a sub rect
senorblancod0d37ca2015-04-02 04:54:56 -0700859 needsTextureDomain = srcRect.width() < width ||
860 srcRect.height() < height;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000861 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
862 // sampling is axis-aligned
863 SkRect transformedRect;
864 contextMatrix.mapRect(&transformedRect, srcRect);
865
866 if (has_aligned_samples(srcRect, transformedRect)) {
867 params.setFilterMode(GrTextureParams::kNone_FilterMode);
868 needsTextureDomain = false;
869 } else {
870 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
871 }
872 }
873 }
874 return needsTextureDomain;
875}
876
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000877void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
878 const SkBitmap& bitmap,
879 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000880 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000881 const SkPaint& paint,
882 SkCanvas::DrawBitmapRectFlags flags) {
joshualitt5531d512014-12-17 15:50:11 -0800883 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000884
885 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000886 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000887 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
888 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000889 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000890 SkScalar w = SkIntToScalar(bitmap.width());
891 SkScalar h = SkIntToScalar(bitmap.height());
892 dstSize.fWidth = w;
893 dstSize.fHeight = h;
894 srcRect.set(0, 0, w, h);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000895 } else {
bsalomon49f085d2014-09-05 13:34:00 -0700896 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000897 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000898 dstSize = *dstSizePtr;
senorblancod0d37ca2015-04-02 04:54:56 -0700899 }
900 GrTexture* tex = bitmap.getTexture();
901 int width = tex ? tex->width() : bitmap.width();
902 int height = tex ? tex->height() : bitmap.height();
903 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
904 srcRect.fRight >= width && srcRect.fBottom >= height) {
905 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000906 }
907
derekf367e1862014-12-02 11:02:06 -0800908 // If the render target is not msaa and draw is antialiased, we call
909 // drawRect instead of drawing on the render target directly.
910 // FIXME: the tiled bitmap code path doesn't currently support
911 // anti-aliased edges, we work around that for now by drawing directly
912 // if the image size exceeds maximum texture size.
913 int maxTextureSize = fContext->getMaxTextureSize();
914 bool directDraw = fRenderTarget->isMultisampled() ||
915 !paint.isAntiAlias() ||
916 bitmap.width() > maxTextureSize ||
917 bitmap.height() > maxTextureSize;
918
919 // we check whether dst rect are pixel aligned
920 if (!directDraw) {
joshualitt5531d512014-12-17 15:50:11 -0800921 bool staysRect = draw.fMatrix->rectStaysRect();
derekf367e1862014-12-02 11:02:06 -0800922
923 if (staysRect) {
924 SkRect rect;
925 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
joshualitt5531d512014-12-17 15:50:11 -0800926 draw.fMatrix->mapRect(&rect, dstRect);
derekf367e1862014-12-02 11:02:06 -0800927 const SkScalar *scalars = rect.asScalars();
928 bool isDstPixelAligned = true;
929 for (int i = 0; i < 4; i++) {
930 if (!SkScalarIsInt(scalars[i])) {
931 isDstPixelAligned = false;
932 break;
933 }
934 }
935
936 if (isDstPixelAligned)
937 directDraw = true;
938 }
939 }
940
941 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000942 // Convert the bitmap to a shader so that the rect can be drawn
943 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000944 SkBitmap tmp; // subset of bitmap, if necessary
945 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000946 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -0700947 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000948 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
949 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
950 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000951 // In bleed mode we position and trim the bitmap based on the src rect which is
952 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
953 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
954 // compensate.
955 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
956 SkIRect iSrc;
957 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000959 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
960 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000961
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000962 if (!bitmap.extractSubset(&tmp, iSrc)) {
963 return; // extraction failed
964 }
965 bitmapPtr = &tmp;
966 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000967
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000968 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000969 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000970 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000971 } else {
972 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000973 }
974
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000975 SkPaint paintWithShader(paint);
976 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000977 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000978 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
979 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000980
981 return;
982 }
983
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000984 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
985 // the view matrix rather than a local matrix.
986 SkMatrix m;
987 m.setScale(dstSize.fWidth / srcRect.width(),
988 dstSize.fHeight / srcRect.height());
joshualitt5531d512014-12-17 15:50:11 -0800989 SkMatrix viewM = *draw.fMatrix;
990 viewM.preConcat(m);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000991
992 GrTextureParams params;
reed93a12152015-03-16 10:08:34 -0700993 SkFilterQuality paintFilterQuality = paint.getFilterQuality();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000994 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000995
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000996 bool doBicubic = false;
997
reed93a12152015-03-16 10:08:34 -0700998 switch(paintFilterQuality) {
999 case kNone_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001000 textureFilterMode = GrTextureParams::kNone_FilterMode;
1001 break;
reed93a12152015-03-16 10:08:34 -07001002 case kLow_SkFilterQuality:
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001003 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1004 break;
reed93a12152015-03-16 10:08:34 -07001005 case kMedium_SkFilterQuality:
joshualitt5531d512014-12-17 15:50:11 -08001006 if (viewM.getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001007 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1008 } else {
1009 // Don't trigger MIP level generation unnecessarily.
1010 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1011 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001012 break;
reed93a12152015-03-16 10:08:34 -07001013 case kHigh_SkFilterQuality:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001014 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001015 doBicubic =
joshualitt5531d512014-12-17 15:50:11 -08001016 GrBicubicEffect::ShouldUseBicubic(viewM, &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001017 break;
1018 default:
1019 SkErrorInternals::SetError( kInvalidPaint_SkError,
1020 "Sorry, I don't understand the filtering "
1021 "mode you asked for. Falling back to "
1022 "MIPMaps.");
1023 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1024 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001025 }
1026
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001027 int tileFilterPad;
1028 if (doBicubic) {
1029 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1030 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1031 tileFilterPad = 0;
1032 } else {
1033 tileFilterPad = 1;
1034 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001035 params.setFilterMode(textureFilterMode);
1036
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001037 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001038 int tileSize;
1039
1040 SkIRect clippedSrcRect;
joshualitt5531d512014-12-17 15:50:11 -08001041 if (this->shouldTileBitmap(bitmap, viewM, params, srcRectPtr, maxTileSize, &tileSize,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001042 &clippedSrcRect)) {
joshualitt5531d512014-12-17 15:50:11 -08001043 this->drawTiledBitmap(bitmap, viewM, srcRect, clippedSrcRect, params, paint, flags,
1044 tileSize, doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045 } else {
1046 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001047 bool needsTextureDomain = needs_texture_domain(bitmap,
1048 srcRect,
1049 params,
joshualitt5531d512014-12-17 15:50:11 -08001050 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001051 doBicubic);
1052 this->internalDrawBitmap(bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001053 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001054 srcRect,
1055 params,
1056 paint,
1057 flags,
1058 doBicubic,
1059 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001060 }
1061}
1062
1063// Break 'bitmap' into several tiles to draw it since it has already
1064// been determined to be too large to fit in VRAM
1065void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001066 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001067 const SkRect& srcRect,
1068 const SkIRect& clippedSrcIRect,
1069 const GrTextureParams& params,
1070 const SkPaint& paint,
1071 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001072 int tileSize,
1073 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001074 // The following pixel lock is technically redundant, but it is desirable
1075 // to lock outside of the tile loop to prevent redecoding the whole image
1076 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1077 // is larger than the limit of the discardable memory pool.
1078 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001079 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1080
1081 int nx = bitmap.width() / tileSize;
1082 int ny = bitmap.height() / tileSize;
1083 for (int x = 0; x <= nx; x++) {
1084 for (int y = 0; y <= ny; y++) {
1085 SkRect tileR;
1086 tileR.set(SkIntToScalar(x * tileSize),
1087 SkIntToScalar(y * tileSize),
1088 SkIntToScalar((x + 1) * tileSize),
1089 SkIntToScalar((y + 1) * tileSize));
1090
1091 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1092 continue;
1093 }
1094
1095 if (!tileR.intersect(srcRect)) {
1096 continue;
1097 }
1098
1099 SkBitmap tmpB;
1100 SkIRect iTileR;
1101 tileR.roundOut(&iTileR);
1102 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1103 SkIntToScalar(iTileR.fTop));
1104
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001105 // Adjust the context matrix to draw at the right x,y in device space
joshualitt5531d512014-12-17 15:50:11 -08001106 SkMatrix viewM = viewMatrix;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001107 SkMatrix tmpM;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001108 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
joshualitt5531d512014-12-17 15:50:11 -08001109 viewM.preConcat(tmpM);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001110
robertphillipsec8bb942014-11-21 10:16:25 -08001111 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001112 SkIRect iClampRect;
1113
1114 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1115 // In bleed mode we want to always expand the tile on all edges
1116 // but stay within the bitmap bounds
1117 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1118 } else {
1119 // In texture-domain/clamp mode we only want to expand the
1120 // tile on edges interior to "srcRect" (i.e., we want to
1121 // not bleed across the original clamped edges)
1122 srcRect.roundOut(&iClampRect);
1123 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001124 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1125 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001126 }
1127
1128 if (bitmap.extractSubset(&tmpB, iTileR)) {
1129 // now offset it to make it "local" to our tmp bitmap
1130 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001131 GrTextureParams paramsTemp = params;
1132 bool needsTextureDomain = needs_texture_domain(bitmap,
1133 srcRect,
1134 paramsTemp,
joshualitt5531d512014-12-17 15:50:11 -08001135 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001136 bicubic);
1137 this->internalDrawBitmap(tmpB,
joshualitt5531d512014-12-17 15:50:11 -08001138 viewM,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001139 tileR,
1140 paramsTemp,
1141 paint,
1142 flags,
1143 bicubic,
1144 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001145 }
1146 }
1147 }
1148}
1149
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001150
1151/*
1152 * This is called by drawBitmap(), which has to handle images that may be too
1153 * large to be represented by a single texture.
1154 *
1155 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1156 * and that non-texture portion of the GrPaint has already been setup.
1157 */
1158void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -08001159 const SkMatrix& viewMatrix,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001160 const SkRect& srcRect,
1161 const GrTextureParams& params,
1162 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001163 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001164 bool bicubic,
1165 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001166 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1167 bitmap.height() <= fContext->getMaxTextureSize());
1168
1169 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001170 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001171 if (NULL == texture) {
1172 return;
1173 }
1174
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001175 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001176 SkRect paintRect;
1177 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1178 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1179 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1180 SkScalarMul(srcRect.fTop, hInv),
1181 SkScalarMul(srcRect.fRight, wInv),
1182 SkScalarMul(srcRect.fBottom, hInv));
1183
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001184 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001185 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001186 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001187 // Use a constrained texture domain to avoid color bleeding
1188 SkScalar left, top, right, bottom;
1189 if (srcRect.width() > SK_Scalar1) {
1190 SkScalar border = SK_ScalarHalf / texture->width();
1191 left = paintRect.left() + border;
1192 right = paintRect.right() - border;
1193 } else {
1194 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1195 }
1196 if (srcRect.height() > SK_Scalar1) {
1197 SkScalar border = SK_ScalarHalf / texture->height();
1198 top = paintRect.top() + border;
1199 bottom = paintRect.bottom() - border;
1200 } else {
1201 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1202 }
1203 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001204 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001205 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001206 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001207 fp.reset(GrTextureDomainEffect::Create(texture,
joshualitt5531d512014-12-17 15:50:11 -08001208 SkMatrix::I(),
1209 textureDomain,
1210 GrTextureDomain::kClamp_Mode,
1211 params.filterMode()));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001212 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001213 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001214 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1215 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001216 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001217 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001218 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001219 }
1220
1221 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1222 // the rest from the SkPaint.
1223 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001224 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001225 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001226 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1227 SkColor2GrColor(paint.getColor());
bsalomonbed83a62015-04-15 14:18:34 -07001228 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint, paintColor, false,
1229 &grPaint)) {
1230 return;
1231 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001232
robertphillipsea461502015-05-26 11:38:03 -07001233 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, viewMatrix, dstRect,
1234 paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001235}
1236
fmalita2d97bc12014-11-20 10:44:58 -08001237bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
senorblancod0d37ca2015-04-02 04:54:56 -07001238 int width, int height,
fmalita2d97bc12014-11-20 10:44:58 -08001239 const SkImageFilter* filter,
1240 const SkImageFilter::Context& ctx,
1241 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001242 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001243
1244 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1245 // (https://code.google.com/p/skia/issues/detail?id=3148).
1246 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001247
1248 if (filter->canFilterImageGPU()) {
senorblancod0d37ca2015-04-02 04:54:56 -07001249 return filter->filterImageGPU(&proxy, wrap_texture(texture, width, height),
1250 ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001251 } else {
1252 return false;
1253 }
1254}
1255
1256void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1257 int left, int top, const SkPaint& paint) {
1258 // drawSprite is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001259 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001260
1261 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1262 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1263 return;
1264 }
1265
1266 int w = bitmap.width();
1267 int h = bitmap.height();
1268
1269 GrTexture* texture;
1270 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001271 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
joshualitt5f5a8d72015-02-25 14:09:45 -08001272 if (!texture) {
1273 return;
1274 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001275
1276 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001277 // This bitmap will own the filtered result as a texture.
1278 SkBitmap filteredBitmap;
1279
bsalomon49f085d2014-09-05 13:34:00 -07001280 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001281 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001282 SkMatrix matrix(*draw.fMatrix);
1283 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001284 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001285 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001286 // This cache is transient, and is freed (along with all its contained
1287 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001288 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001289 if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
fmalita2d97bc12014-11-20 10:44:58 -08001290 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001291 texture = (GrTexture*) filteredBitmap.getTexture();
1292 w = filteredBitmap.width();
1293 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001294 left += offset.x();
1295 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001296 } else {
1297 return;
1298 }
1299 }
1300
1301 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001302 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001303
bsalomonbed83a62015-04-15 14:18:34 -07001304 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1305 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1306 return;
1307 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001308
robertphillipsea461502015-05-26 11:38:03 -07001309 fDrawContext->drawNonAARectToRect(fRenderTarget,
1310 fClip,
1311 grPaint,
1312 SkMatrix::I(),
1313 SkRect::MakeXYWH(SkIntToScalar(left),
1314 SkIntToScalar(top),
1315 SkIntToScalar(w),
1316 SkIntToScalar(h)),
1317 SkRect::MakeXYWH(0,
1318 0,
1319 SK_Scalar1 * w / texture->width(),
1320 SK_Scalar1 * h / texture->height()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001321}
1322
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001323void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001324 const SkRect* src, const SkRect& dst,
1325 const SkPaint& paint,
1326 SkCanvas::DrawBitmapRectFlags flags) {
1327 SkMatrix matrix;
1328 SkRect bitmapBounds, tmpSrc;
1329
1330 bitmapBounds.set(0, 0,
1331 SkIntToScalar(bitmap.width()),
1332 SkIntToScalar(bitmap.height()));
1333
1334 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001335 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001336 tmpSrc = *src;
1337 } else {
1338 tmpSrc = bitmapBounds;
1339 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001340
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001341 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1342
1343 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001344 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001345 if (!bitmapBounds.contains(tmpSrc)) {
1346 if (!tmpSrc.intersect(bitmapBounds)) {
1347 return; // nothing to draw
1348 }
1349 }
1350 }
1351
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001352 SkRect tmpDst;
1353 matrix.mapRect(&tmpDst, tmpSrc);
1354
1355 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1356 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1357 // Translate so that tempDst's top left is at the origin.
1358 matrix = *origDraw.fMatrix;
1359 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1360 draw.writable()->fMatrix = &matrix;
1361 }
1362 SkSize dstSize;
1363 dstSize.fWidth = tmpDst.width();
1364 dstSize.fHeight = tmpDst.height();
1365
1366 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001367}
1368
1369void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1370 int x, int y, const SkPaint& paint) {
1371 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001372 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001373 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
kkinnunen2e4414e2015-02-19 07:20:40 -08001374
1375 // TODO: If the source device covers the whole of this device, we could
1376 // omit fNeedsClear -related flushing.
1377 // TODO: if source needs clear, we could maybe omit the draw fully.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001378
1379 // drawDevice is defined to be in device coords.
joshualitt5531d512014-12-17 15:50:11 -08001380 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001381
1382 GrRenderTarget* devRT = dev->accessRenderTarget();
1383 GrTexture* devTex;
1384 if (NULL == (devTex = devRT->asTexture())) {
1385 return;
1386 }
1387
robertphillips7b9e8a42014-12-11 08:20:31 -08001388 const SkImageInfo ii = dev->imageInfo();
1389 int w = ii.width();
1390 int h = ii.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001391
1392 SkImageFilter* filter = paint.getImageFilter();
1393 // This bitmap will own the filtered result as a texture.
1394 SkBitmap filteredBitmap;
1395
bsalomon49f085d2014-09-05 13:34:00 -07001396 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001397 SkIPoint offset = SkIPoint::Make(0, 0);
1398 SkMatrix matrix(*draw.fMatrix);
1399 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001400 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001401 // This cache is transient, and is freed (along with all its contained
1402 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001403 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001404 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblancod0d37ca2015-04-02 04:54:56 -07001405 if (this->filterTexture(fContext, devTex, device->width(), device->height(),
1406 filter, ctx, &filteredBitmap, &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001407 devTex = filteredBitmap.getTexture();
1408 w = filteredBitmap.width();
1409 h = filteredBitmap.height();
1410 x += offset.fX;
1411 y += offset.fY;
1412 } else {
1413 return;
1414 }
1415 }
1416
1417 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001418 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001419
bsalomonbed83a62015-04-15 14:18:34 -07001420 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1421 SkColor2GrColorJustAlpha(paint.getColor()), false, &grPaint)) {
1422 return;
1423 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001424
1425 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1426 SkIntToScalar(y),
1427 SkIntToScalar(w),
1428 SkIntToScalar(h));
1429
1430 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1431 // scratch texture).
1432 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1433 SK_Scalar1 * h / devTex->height());
1434
robertphillipsea461502015-05-26 11:38:03 -07001435 fDrawContext->drawNonAARectToRect(fRenderTarget, fClip, grPaint, SkMatrix::I(), dstRect,
1436 srcRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001437}
1438
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001439bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001440 return filter->canFilterImageGPU();
1441}
1442
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001443bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001444 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001445 SkBitmap* result, SkIPoint* offset) {
1446 // want explicitly our impl, so guard against a subclass of us overriding it
1447 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1448 return false;
1449 }
1450
1451 SkAutoLockPixels alp(src, !src.getTexture());
1452 if (!src.getTexture() && !src.readyToDraw()) {
1453 return false;
1454 }
1455
1456 GrTexture* texture;
1457 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1458 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001459 AutoBitmapTexture abt(fContext, src, NULL, &texture);
robertphillipsf83be822015-04-30 08:55:06 -07001460 if (!texture) {
1461 return false;
1462 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001463
senorblancod0d37ca2015-04-02 04:54:56 -07001464 return this->filterTexture(fContext, texture, src.width(), src.height(),
1465 filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001466}
1467
reeda85d4d02015-05-06 12:56:48 -07001468static bool wrap_as_bm(const SkImage* image, SkBitmap* bm) {
1469 GrTexture* tex = image->getTexture();
1470 if (tex) {
reed8b26b992015-05-07 15:36:17 -07001471 GrWrapTextureInBitmap(tex, image->width(), image->height(), image->isOpaque(), bm);
1472 return true;
reeda85d4d02015-05-06 12:56:48 -07001473 } else {
reed8b26b992015-05-07 15:36:17 -07001474 return as_IB(image)->getROPixels(bm);
reeda85d4d02015-05-06 12:56:48 -07001475 }
reeda85d4d02015-05-06 12:56:48 -07001476}
1477
1478void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
1479 const SkPaint& paint) {
1480 SkBitmap bm;
1481 if (wrap_as_bm(image, &bm)) {
1482 this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
1483 }
1484}
1485
1486void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
1487 const SkRect& dst, const SkPaint& paint) {
1488 SkBitmap bm;
1489 if (wrap_as_bm(image, &bm)) {
1490 this->drawBitmapRect(draw, bm, src, dst, paint, SkCanvas::kNone_DrawBitmapRectFlag);
1491 }
1492}
1493
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001494///////////////////////////////////////////////////////////////////////////////
1495
1496// must be in SkCanvas::VertexMode order
1497static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1498 kTriangles_GrPrimitiveType,
1499 kTriangleStrip_GrPrimitiveType,
1500 kTriangleFan_GrPrimitiveType,
1501};
1502
1503void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1504 int vertexCount, const SkPoint vertices[],
1505 const SkPoint texs[], const SkColor colors[],
1506 SkXfermode* xmode,
1507 const uint16_t indices[], int indexCount,
1508 const SkPaint& paint) {
joshualitt5531d512014-12-17 15:50:11 -08001509 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001510 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001511
dandov32a311b2014-07-15 19:46:26 -07001512 const uint16_t* outIndices;
1513 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1514 GrPrimitiveType primType;
1515 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001516
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001517 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1518 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001519
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001520 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001521
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001522 SkPaint copy(paint);
1523 copy.setStyle(SkPaint::kStroke_Style);
1524 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001525
dandov32a311b2014-07-15 19:46:26 -07001526 // we ignore the shader if texs is null.
bsalomonbed83a62015-04-15 14:18:34 -07001527 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, copy,
1528 SkColor2GrColor(copy.getColor()), NULL == colors, &grPaint)) {
1529 return;
1530 }
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001531
dandov32a311b2014-07-15 19:46:26 -07001532 primType = kLines_GrPrimitiveType;
1533 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001534 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001535 switch (vmode) {
1536 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001537 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001538 break;
1539 case SkCanvas::kTriangleStrip_VertexMode:
1540 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001541 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001542 break;
1543 }
mtklein533eb782014-08-27 10:39:42 -07001544
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001545 VertState state(vertexCount, indices, indexCount);
1546 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001547
dandov32a311b2014-07-15 19:46:26 -07001548 //number of indices for lines per triangle with kLines
1549 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001550
dandov32a311b2014-07-15 19:46:26 -07001551 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1552 outIndices = outAlloc.get();
1553 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001554 int i = 0;
1555 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001556 auxIndices[i] = state.f0;
1557 auxIndices[i + 1] = state.f1;
1558 auxIndices[i + 2] = state.f1;
1559 auxIndices[i + 3] = state.f2;
1560 auxIndices[i + 4] = state.f2;
1561 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001562 i += 6;
1563 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001564 } else {
dandov32a311b2014-07-15 19:46:26 -07001565 outIndices = indices;
1566 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001567
dandov32a311b2014-07-15 19:46:26 -07001568 if (NULL == texs || NULL == paint.getShader()) {
bsalomonbed83a62015-04-15 14:18:34 -07001569 if (!SkPaint2GrPaintNoShader(this->context(), fRenderTarget, paint,
1570 SkColor2GrColor(paint.getColor()),
1571 NULL == colors, &grPaint)) {
1572 return;
1573 }
dandov32a311b2014-07-15 19:46:26 -07001574 } else {
bsalomonbed83a62015-04-15 14:18:34 -07001575 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix,
1576 NULL == colors, &grPaint)) {
1577 return;
1578 }
dandov32a311b2014-07-15 19:46:26 -07001579 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001580 }
1581
mtklein2583b622014-06-04 08:20:41 -07001582#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001583 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001584 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1585 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001586 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001587 }
1588 }
mtklein2583b622014-06-04 08:20:41 -07001589#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001590
1591 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001592 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001593 // need to convert byte order and from non-PM to PM
1594 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001595 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001596 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001597 color = colors[i];
1598 if (paint.getAlpha() != 255) {
1599 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1600 }
1601 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001602 }
1603 colors = convertedColors.get();
1604 }
robertphillipsea461502015-05-26 11:38:03 -07001605 fDrawContext->drawVertices(fRenderTarget,
1606 fClip,
1607 grPaint,
1608 *draw.fMatrix,
1609 primType,
1610 vertexCount,
1611 vertices,
1612 texs,
1613 colors,
1614 outIndices,
1615 indexCount);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001616}
1617
1618///////////////////////////////////////////////////////////////////////////////
1619
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001620void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
joshualitt5531d512014-12-17 15:50:11 -08001621 size_t byteLength, SkScalar x, SkScalar y,
1622 const SkPaint& paint) {
1623 CHECK_SHOULD_DRAW(draw);
egdanield78a1682014-07-09 10:41:26 -07001624 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001625
jvanverth8c27a182014-10-14 08:45:50 -07001626 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001627 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1628 return;
1629 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001630
jvanverth8c27a182014-10-14 08:45:50 -07001631 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001632
joshualitt6e8cd962015-03-20 10:30:14 -07001633 fTextContext->drawText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
1634 (const char *)text, byteLength, x, y, draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001635}
1636
fmalita05c4a432014-09-29 06:29:53 -07001637void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1638 const SkScalar pos[], int scalarsPerPos,
1639 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001640 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
joshualitt5531d512014-12-17 15:50:11 -08001641 CHECK_SHOULD_DRAW(draw);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001642
jvanverth8c27a182014-10-14 08:45:50 -07001643 GrPaint grPaint;
bsalomonbed83a62015-04-15 14:18:34 -07001644 if (!SkPaint2GrPaint(this->context(), fRenderTarget, paint, *draw.fMatrix, true, &grPaint)) {
1645 return;
1646 }
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001647
jvanverth8c27a182014-10-14 08:45:50 -07001648 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001649
joshualitt6e8cd962015-03-20 10:30:14 -07001650 fTextContext->drawPosText(fRenderTarget, fClip, grPaint, paint, *draw.fMatrix,
1651 (const char *)text, byteLength, pos, scalarsPerPos, offset,
1652 draw.fClip->getBounds());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001653}
1654
joshualitt9c328182015-03-23 08:13:04 -07001655void SkGpuDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y,
1656 const SkPaint& paint, SkDrawFilter* drawFilter) {
1657 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawTextBlob", fContext);
1658 CHECK_SHOULD_DRAW(draw);
1659
1660 SkDEBUGCODE(this->validate();)
1661
robertphillips9c240a12015-05-28 07:45:59 -07001662 fTextContext->drawTextBlob(fRenderTarget, fClip, paint, *draw.fMatrix,
robertphillipsccb1b572015-05-27 11:02:55 -07001663 blob, x, y, drawFilter, draw.fClip->getBounds());
joshualitt9c328182015-03-23 08:13:04 -07001664}
1665
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001666///////////////////////////////////////////////////////////////////////////////
1667
reedb2db8982014-11-13 12:41:02 -08001668bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
robertphillips9c240a12015-05-28 07:45:59 -07001669 return GrTextContext::ShouldDisableLCD(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001670}
1671
1672void SkGpuDevice::flush() {
1673 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001674 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001675}
1676
1677///////////////////////////////////////////////////////////////////////////////
1678
reed76033be2015-03-14 10:54:31 -07001679SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
bsalomonf2703d82014-10-28 14:33:06 -07001680 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001681 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001682 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001683 desc.fWidth = cinfo.fInfo.width();
1684 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001685 desc.fSampleCnt = fRenderTarget->numSamples();
1686
1687 SkAutoTUnref<GrTexture> texture;
1688 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001689 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001690
hcm4396fa52014-10-27 21:43:30 -07001691 // layers are never draw in repeat modes, so we can request an approx
1692 // match and ignore any padding.
bsalomond309e7a2015-04-30 14:18:54 -07001693 const GrTextureProvider::ScratchTexMatch match = (kNever_TileUsage == cinfo.fTileUsage) ?
1694 GrTextureProvider::kApprox_ScratchTexMatch :
1695 GrTextureProvider::kExact_ScratchTexMatch;
1696 texture.reset(fContext->textureProvider()->refScratchTexture(desc, match));
bsalomonafe30052015-01-16 07:32:33 -08001697
1698 if (texture) {
1699 SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry);
senorblancod0d37ca2015-04-02 04:54:56 -07001700 return SkGpuDevice::Create(
1701 texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001702 } else {
joshualitt5f5a8d72015-02-25 14:09:45 -08001703 SkErrorInternals::SetError( kInternalError_SkError,
reed61f501f2015-04-29 08:34:00 -07001704 "---- failed to create gpu device texture [%d %d]\n",
joshualitt5f5a8d72015-02-25 14:09:45 -08001705 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001706 return NULL;
1707 }
1708}
1709
reed4a8126e2014-09-22 07:29:03 -07001710SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
bsalomonafe30052015-01-16 07:32:33 -08001711 // TODO: Change the signature of newSurface to take a budgeted parameter.
1712 static const SkSurface::Budgeted kBudgeted = SkSurface::kNo_Budgeted;
1713 return SkSurface::NewRenderTarget(fContext, kBudgeted, info, fRenderTarget->numSamples(),
1714 &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001715}
1716
robertphillips30d2cc62014-09-24 08:52:18 -07001717bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001718 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips63242d72014-12-04 08:31:02 -08001719#ifndef SK_IGNORE_GPU_LAYER_HOISTING
robertphillips30d78412014-11-24 09:49:17 -08001720 // todo: should handle this natively
1721 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001722 return false;
1723 }
1724
mtklein9db912c2015-05-19 11:11:26 -07001725 const SkBigPicture::AccelData* data = NULL;
1726 if (const SkBigPicture* bp = mainPicture->asSkBigPicture()) {
1727 data = bp->accelData();
1728 }
robertphillips81f71b62014-11-11 04:54:49 -08001729 if (!data) {
1730 return false;
1731 }
1732
robertphillipse5524cd2015-02-20 12:30:26 -08001733 const SkLayerInfo *gpuData = static_cast<const SkLayerInfo*>(data);
1734 if (0 == gpuData->numBlocks()) {
1735 return false;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001736 }
1737
robertphillipsfd61ed02014-10-28 07:21:44 -07001738 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001739
robertphillipse5524cd2015-02-20 12:30:26 -08001740 SkIRect iBounds;
1741 if (!mainCanvas->getClipDeviceBounds(&iBounds)) {
1742 return false;
1743 }
1744
1745 SkRect clipBounds = SkRect::Make(iBounds);
1746
1747 SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1748
robertphillipsfd61ed02014-10-28 07:21:44 -07001749 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001750 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001751 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001752 &atlasedNeedRendering, &atlasedRecycled,
1753 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001754
1755 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1756
1757 SkTDArray<GrHoistedLayer> needRendering, recycled;
1758
robertphillipse5524cd2015-02-20 12:30:26 -08001759 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1760
robertphillipsfd61ed02014-10-28 07:21:44 -07001761 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001762 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001763 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001764 &needRendering, &recycled,
1765 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001766
1767 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001768
robertphillips64bf7672014-08-21 13:07:35 -07001769 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001770 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1771 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001772
robertphillipsfd61ed02014-10-28 07:21:44 -07001773 GrLayerHoister::UnlockLayers(fContext, needRendering);
1774 GrLayerHoister::UnlockLayers(fContext, recycled);
1775 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1776 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001777
1778 return true;
robertphillips63242d72014-12-04 08:31:02 -08001779#else
1780 return false;
1781#endif
robertphillips64bf7672014-08-21 13:07:35 -07001782}
1783
senorblancobe129b22014-08-08 07:14:35 -07001784SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001785 // We always return a transient cache, so it is freed after each
1786 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001787 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001788}
reedf037e0b2014-10-30 11:34:15 -07001789
1790#endif