blob: e6c834394311c56e7d424d1e6022c9d5dac33634 [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
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000010#include "effects/GrBicubicEffect.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000011#include "effects/GrDashingEffect.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000012#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000013#include "effects/GrSimpleTextureEffect.h"
14
15#include "GrContext.h"
16#include "GrBitmapTextContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000017#include "GrDistanceFieldTextContext.h"
robertphillips98d709b2014-09-02 10:20:50 -070018#include "GrLayerHoister.h"
robertphillips274b4ba2014-09-04 07:24:18 -070019#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070020#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070021#include "GrTracing.h"
derekff4555aa2014-10-06 12:19:12 -070022#include "GrGpu.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000023
24#include "SkGrTexturePixelRef.h"
25
robertphillips30d78412014-11-24 09:49:17 -080026#include "SkCanvasPriv.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000027#include "SkDeviceImageFilterProxy.h"
28#include "SkDrawProcs.h"
29#include "SkGlyphCache.h"
30#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080031#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000032#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000034#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070035#include "SkPictureData.h"
robertphillips274b4ba2014-09-04 07:24:18 -070036#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkRRect.h"
38#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000039#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000040#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000041#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000042#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070043#include "SkXfermode.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000044#include "SkErrorInternals.h"
45
reedf037e0b2014-10-30 11:34:15 -070046#if SK_SUPPORT_GPU
47
senorblanco55b6d8b2014-07-30 11:26:46 -070048enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
49
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000050#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
51
52#if 0
53 extern bool (*gShouldDrawProc)();
54 #define CHECK_SHOULD_DRAW(draw, forceI) \
55 do { \
56 if (gShouldDrawProc && !gShouldDrawProc()) return; \
57 this->prepareDraw(draw, forceI); \
58 } while (0)
59#else
60 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
61#endif
62
63// This constant represents the screen alignment criterion in texels for
64// requiring texture domain clamping to prevent color bleeding when drawing
65// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000066#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000067
68#define DO_DEFERRED_CLEAR() \
69 do { \
jvanverth4736e142014-11-07 07:12:46 -080070 if (fFlags & kNeedClear_Flag) { \
reed37294692014-12-02 10:08:11 -080071 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000072 } \
73 } while (false) \
74
75///////////////////////////////////////////////////////////////////////////////
76
77#define CHECK_FOR_ANNOTATION(paint) \
78 do { if (paint.getAnnotation()) { return; } } while (0)
79
80///////////////////////////////////////////////////////////////////////////////
81
bsalomonbcf0a522014-10-08 08:40:09 -070082// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
83// just accesses the backing GrTexture. Otherwise, it creates a cached texture
84// representation and releases it in the destructor.
85class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040086public:
bsalomonbcf0a522014-10-08 08:40:09 -070087 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070088
bsalomonbcf0a522014-10-08 08:40:09 -070089 AutoBitmapTexture(GrContext* context,
90 const SkBitmap& bitmap,
91 const GrTextureParams* params,
92 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040093 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070094 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040095 }
96
bsalomonbcf0a522014-10-08 08:40:09 -070097 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040098 const SkBitmap& bitmap,
99 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -0700100 // Either get the texture directly from the bitmap, or else use the cache and
101 // remember to unref it.
102 if (GrTexture* bmpTexture = bitmap.getTexture()) {
103 fTexture.reset(NULL);
104 return bmpTexture;
105 } else {
106 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
107 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400108 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400109 }
110
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000111private:
bsalomonbcf0a522014-10-08 08:40:09 -0700112 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000113};
114
115///////////////////////////////////////////////////////////////////////////////
116
117struct GrSkDrawProcs : public SkDrawProcs {
118public:
119 GrContext* fContext;
120 GrTextContext* fTextContext;
121 GrFontScaler* fFontScaler; // cached in the skia glyphcache
122};
123
124///////////////////////////////////////////////////////////////////////////////
125
reed4a8126e2014-09-22 07:29:03 -0700126SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, const SkSurfaceProps& props, unsigned flags) {
bsalomon49f085d2014-09-05 13:34:00 -0700127 SkASSERT(surface);
bsalomon32d0b3b2014-08-29 07:50:23 -0700128 if (NULL == surface->asRenderTarget() || surface->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000129 return NULL;
130 }
reed4a8126e2014-09-22 07:29:03 -0700131 return SkNEW_ARGS(SkGpuDevice, (surface, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000132}
133
reedb2db8982014-11-13 12:41:02 -0800134static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps& props) {
135 return SkDeviceProperties(props.pixelGeometry());
136}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000137
reedb2db8982014-11-13 12:41:02 -0800138SkGpuDevice::SkGpuDevice(GrSurface* surface, const SkSurfaceProps& props, unsigned flags)
139 : INHERITED(surfaceprops_to_deviceprops(props))
140{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000141 fDrawProcs = NULL;
142
bsalomon32d0b3b2014-08-29 07:50:23 -0700143 fContext = SkRef(surface->getContext());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000144
jvanverth4736e142014-11-07 07:12:46 -0800145 fFlags = flags;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000146
bsalomon32d0b3b2014-08-29 07:50:23 -0700147 fRenderTarget = SkRef(surface->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000148
bsalomonafbf2d62014-09-30 12:18:44 -0700149 SkImageInfo info = surface->surfacePriv().info();
bsalomonbcf0a522014-10-08 08:40:09 -0700150 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface));
bsalomonafbf2d62014-09-30 12:18:44 -0700151 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700152 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700153
jvanverth4736e142014-11-07 07:12:46 -0800154 bool useDFT = SkToBool(flags & kDFText_Flag);
155 fTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFT);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000156}
157
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000158SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
reed4a8126e2014-09-22 07:29:03 -0700159 const SkSurfaceProps& props, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000160 if (kUnknown_SkColorType == origInfo.colorType() ||
161 origInfo.width() < 0 || origInfo.height() < 0) {
162 return NULL;
163 }
164
reede5ea5002014-09-03 11:54:58 -0700165 SkColorType ct = origInfo.colorType();
166 SkAlphaType at = origInfo.alphaType();
robertphillipsa0537de2014-09-18 08:01:23 -0700167 // TODO: perhaps we can loosen this check now that colortype is more detailed
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000168 // e.g. can we support both RGBA and BGRA here?
reede5ea5002014-09-03 11:54:58 -0700169 if (kRGB_565_SkColorType == ct) {
170 at = kOpaque_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000171 } else {
reede5ea5002014-09-03 11:54:58 -0700172 ct = kN32_SkColorType;
173 if (kOpaque_SkAlphaType != at) {
174 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000175 }
176 }
reede5ea5002014-09-03 11:54:58 -0700177 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000178
bsalomonf2703d82014-10-28 14:33:06 -0700179 GrSurfaceDesc desc;
180 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000181 desc.fWidth = info.width();
182 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000183 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000184 desc.fSampleCnt = sampleCount;
185
186 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
187 if (!texture.get()) {
188 return NULL;
189 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000190
reed4a8126e2014-09-22 07:29:03 -0700191 return SkNEW_ARGS(SkGpuDevice, (texture.get(), props));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000192}
193
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000194SkGpuDevice::~SkGpuDevice() {
195 if (fDrawProcs) {
196 delete fDrawProcs;
197 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000198
jvanverth8c27a182014-10-14 08:45:50 -0700199 delete fTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000200
201 // The GrContext takes a ref on the target. We don't want to cause the render
202 // target to be unnecessarily kept alive.
203 if (fContext->getRenderTarget() == fRenderTarget) {
204 fContext->setRenderTarget(NULL);
205 }
206
207 if (fContext->getClip() == &fClipData) {
208 fContext->setClip(NULL);
209 }
210
bsalomon32d0b3b2014-08-29 07:50:23 -0700211 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000212 fContext->unref();
213}
214
215///////////////////////////////////////////////////////////////////////////////
216
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000217bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
218 int x, int y) {
219 DO_DEFERRED_CLEAR();
220
221 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000222 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000223 if (kUnknown_GrPixelConfig == config) {
224 return false;
225 }
226
227 uint32_t flags = 0;
228 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
229 flags = GrContext::kUnpremul_PixelOpsFlag;
230 }
231 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
232 config, dstPixels, dstRowBytes, flags);
233}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000234
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000235bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
236 int x, int y) {
237 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000238 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000239 if (kUnknown_GrPixelConfig == config) {
240 return false;
241 }
242 uint32_t flags = 0;
243 if (kUnpremul_SkAlphaType == info.alphaType()) {
244 flags = GrContext::kUnpremul_PixelOpsFlag;
245 }
246 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
247
248 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700249 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000250
251 return true;
252}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000253
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000254const SkBitmap& SkGpuDevice::onAccessBitmap() {
255 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700256 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000257}
258
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000259void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
260 INHERITED::onAttachToCanvas(canvas);
261
262 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
263 fClipData.fClipStack = canvas->getClipStack();
264}
265
266void SkGpuDevice::onDetachFromCanvas() {
267 INHERITED::onDetachFromCanvas();
268 fClipData.fClipStack = NULL;
269}
270
271// call this every draw call, to ensure that the context reflects our state,
272// and not the state from some other canvas/device
273void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
bsalomon49f085d2014-09-05 13:34:00 -0700274 SkASSERT(fClipData.fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000275
276 fContext->setRenderTarget(fRenderTarget);
277
278 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
279
280 if (forceIdentity) {
281 fContext->setIdentityMatrix();
282 } else {
283 fContext->setMatrix(*draw.fMatrix);
284 }
285 fClipData.fOrigin = this->getOrigin();
286
287 fContext->setClip(&fClipData);
288
289 DO_DEFERRED_CLEAR();
290}
291
292GrRenderTarget* SkGpuDevice::accessRenderTarget() {
293 DO_DEFERRED_CLEAR();
294 return fRenderTarget;
295}
296
reed37294692014-12-02 10:08:11 -0800297void SkGpuDevice::clearAll() {
298 GrColor color = 0;
299 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
300 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
301 fContext->clear(&rect, color, true, fRenderTarget);
302 fFlags &= ~kNeedClear_Flag;
303}
304
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000305///////////////////////////////////////////////////////////////////////////////
306
307SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
308SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
309SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
310SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
311SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
312 shader_type_mismatch);
313SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
314 shader_type_mismatch);
315SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
316SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
317
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000318///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000319
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000320void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
321 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700322 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323
324 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000325 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000326
327 fContext->drawPaint(grPaint);
328}
329
330// must be in SkCanvas::PointMode order
331static const GrPrimitiveType gPointMode2PrimtiveType[] = {
332 kPoints_GrPrimitiveType,
333 kLines_GrPrimitiveType,
334 kLineStrip_GrPrimitiveType
335};
336
337void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
338 size_t count, const SkPoint pts[], const SkPaint& paint) {
339 CHECK_FOR_ANNOTATION(paint);
340 CHECK_SHOULD_DRAW(draw, false);
341
342 SkScalar width = paint.getStrokeWidth();
343 if (width < 0) {
344 return;
345 }
346
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000347 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700348 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
349 GrPaint grPaint;
350 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
351 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700352 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700353 path.moveTo(pts[0]);
354 path.lineTo(pts[1]);
355 fContext->drawPath(grPaint, path, strokeInfo);
356 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000357 }
358
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000359 // we only handle hairlines and paints without path effects or mask filters,
360 // else we let the SkDraw call our drawPath()
361 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
362 draw.drawPoints(mode, count, pts, paint, true);
363 return;
364 }
365
366 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000367 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000368
369 fContext->drawVertices(grPaint,
370 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000371 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000372 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000373 NULL,
374 NULL,
375 NULL,
376 0);
377}
378
379///////////////////////////////////////////////////////////////////////////////
380
381void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
382 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700383 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
384
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000385 CHECK_FOR_ANNOTATION(paint);
386 CHECK_SHOULD_DRAW(draw, false);
387
388 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
389 SkScalar width = paint.getStrokeWidth();
390
391 /*
392 We have special code for hairline strokes, miter-strokes, bevel-stroke
393 and fills. Anything else we just call our path code.
394 */
395 bool usePath = doStroke && width > 0 &&
396 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
397 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
398 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700399
400 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000401 usePath = true;
402 }
egdanield58a0ba2014-06-11 10:30:05 -0700403
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000404 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
405#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
406 if (doStroke) {
407#endif
408 usePath = true;
409#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
410 } else {
411 usePath = !fContext->getMatrix().preservesRightAngles();
412 }
413#endif
414 }
415 // until we can both stroke and fill rectangles
416 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
417 usePath = true;
418 }
419
egdanield58a0ba2014-06-11 10:30:05 -0700420 GrStrokeInfo strokeInfo(paint);
421
422 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700423 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700424 usePath = true;
425 }
426
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000427 if (usePath) {
428 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700429 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000430 path.addRect(rect);
431 this->drawPath(draw, path, paint, NULL, true);
432 return;
433 }
434
435 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000436 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400437
egdanield58a0ba2014-06-11 10:30:05 -0700438 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000439}
440
441///////////////////////////////////////////////////////////////////////////////
442
443void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
444 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700445 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000446 CHECK_FOR_ANNOTATION(paint);
447 CHECK_SHOULD_DRAW(draw, false);
448
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000449 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000450 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400451
egdanield58a0ba2014-06-11 10:30:05 -0700452 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000453 if (paint.getMaskFilter()) {
454 // try to hit the fast path for drawing filtered round rects
455
456 SkRRect devRRect;
457 if (rect.transform(fContext->getMatrix(), &devRRect)) {
458 if (devRRect.allCornersCircular()) {
459 SkRect maskRect;
460 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
461 draw.fClip->getBounds(),
462 fContext->getMatrix(),
463 &maskRect)) {
464 SkIRect finalIRect;
465 maskRect.roundOut(&finalIRect);
466 if (draw.fClip->quickReject(finalIRect)) {
467 // clipped out
468 return;
469 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000470 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700471 strokeInfo.getStrokeRec(),
472 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000473 return;
474 }
475 }
476
477 }
478 }
479
480 }
481
egdanield58a0ba2014-06-11 10:30:05 -0700482 bool usePath = false;
483
484 if (paint.getMaskFilter()) {
485 usePath = true;
486 } else {
487 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700488 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700489 usePath = true;
490 }
491 }
492
493
494 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000495 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700496 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000497 path.addRRect(rect);
498 this->drawPath(draw, path, paint, NULL, true);
499 return;
500 }
Mike Klein744fb732014-06-23 15:13:26 -0400501
egdanield58a0ba2014-06-11 10:30:05 -0700502 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000503}
504
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000505void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
506 const SkRRect& inner, const SkPaint& paint) {
507 SkStrokeRec stroke(paint);
508 if (stroke.isFillStyle()) {
509
510 CHECK_FOR_ANNOTATION(paint);
511 CHECK_SHOULD_DRAW(draw, false);
512
513 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000514 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000515
516 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
517 fContext->drawDRRect(grPaint, outer, inner);
518 return;
519 }
520 }
521
522 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700523 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000524 path.addRRect(outer);
525 path.addRRect(inner);
526 path.setFillType(SkPath::kEvenOdd_FillType);
527
528 this->drawPath(draw, path, paint, NULL, true);
529}
530
531
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000532/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000533
534void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
535 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700536 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000537 CHECK_FOR_ANNOTATION(paint);
538 CHECK_SHOULD_DRAW(draw, false);
539
egdanield58a0ba2014-06-11 10:30:05 -0700540 GrStrokeInfo strokeInfo(paint);
541
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000542 bool usePath = false;
543 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700544 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000545 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700546 } else {
547 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700548 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700549 usePath = true;
550 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000551 }
552
553 if (usePath) {
554 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700555 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000556 path.addOval(oval);
557 this->drawPath(draw, path, paint, NULL, true);
558 return;
559 }
560
561 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000562 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000563
egdanield58a0ba2014-06-11 10:30:05 -0700564 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000565}
566
567#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000568
569///////////////////////////////////////////////////////////////////////////////
570
571// helpers for applying mask filters
572namespace {
573
574// Draw a mask using the supplied paint. Since the coverage/geometry
575// is already burnt into the mask this boils down to a rect draw.
576// Return true if the mask was successfully drawn.
577bool draw_mask(GrContext* context, const SkRect& maskRect,
578 GrPaint* grp, GrTexture* mask) {
579 GrContext::AutoMatrix am;
580 if (!am.setIdentity(context, grp)) {
581 return false;
582 }
583
584 SkMatrix matrix;
585 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
586 matrix.postIDiv(mask->width(), mask->height());
587
joshualittb0a8a372014-09-23 09:50:21 -0700588 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000589 context->drawRect(*grp, maskRect);
590 return true;
591}
592
593bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700594 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000595 GrPaint* grp, SkPaint::Style style) {
596 SkMask srcM, dstM;
597
598 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
599 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
600 return false;
601 }
602 SkAutoMaskFreeImage autoSrc(srcM.fImage);
603
604 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
605 return false;
606 }
607 // this will free-up dstM when we're done (allocated in filterMask())
608 SkAutoMaskFreeImage autoDst(dstM.fImage);
609
610 if (clip.quickReject(dstM.fBounds)) {
611 return false;
612 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000613
614 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
615 // the current clip (and identity matrix) and GrPaint settings
bsalomonf2703d82014-10-28 14:33:06 -0700616 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000617 desc.fWidth = dstM.fBounds.width();
618 desc.fHeight = dstM.fBounds.height();
619 desc.fConfig = kAlpha_8_GrPixelConfig;
620
bsalomone3059732014-10-14 11:47:22 -0700621 SkAutoTUnref<GrTexture> texture(
622 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
623 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000624 return false;
625 }
626 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
627 dstM.fImage, dstM.fRowBytes);
628
629 SkRect maskRect = SkRect::Make(dstM.fBounds);
630
631 return draw_mask(context, maskRect, grp, texture);
632}
633
bsalomone3059732014-10-14 11:47:22 -0700634// Create a mask of 'devPath' and place the result in 'mask'.
635GrTexture* create_mask_GPU(GrContext* context,
636 const SkRect& maskRect,
637 const SkPath& devPath,
638 const GrStrokeInfo& strokeInfo,
639 bool doAA,
640 int sampleCnt) {
bsalomonf2703d82014-10-28 14:33:06 -0700641 GrSurfaceDesc desc;
642 desc.fFlags = kRenderTarget_GrSurfaceFlag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000643 desc.fWidth = SkScalarCeilToInt(maskRect.width());
644 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700645 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000646 // We actually only need A8, but it often isn't supported as a
647 // render target so default to RGBA_8888
648 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700649
650 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
651 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000652 desc.fConfig = kAlpha_8_GrPixelConfig;
653 }
654
bsalomone3059732014-10-14 11:47:22 -0700655 GrTexture* mask = context->refScratchTexture(desc,GrContext::kApprox_ScratchTexMatch);
656 if (NULL == mask) {
657 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000658 }
659
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
661
bsalomone3059732014-10-14 11:47:22 -0700662 GrContext::AutoRenderTarget art(context, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000663 GrContext::AutoClip ac(context, clipRect);
664
bsalomon89c62982014-11-03 12:08:42 -0800665 context->clear(NULL, 0x0, true, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000666
667 GrPaint tempPaint;
668 if (doAA) {
669 tempPaint.setAntiAlias(true);
670 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
671 // blend coeff of zero requires dual source blending support in order
672 // to properly blend partially covered pixels. This means the AA
673 // code path may not be taken. So we use a dst blend coeff of ISA. We
674 // could special case AA draws to a dst surface with known alpha=0 to
675 // use a zero dst coeff when dual source blending isn't available.
676 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
677 }
678
679 GrContext::AutoMatrix am;
680
681 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
682 SkMatrix translate;
683 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
684 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700685 context->drawPath(tempPaint, devPath, strokeInfo);
bsalomone3059732014-10-14 11:47:22 -0700686 return mask;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000687}
688
689SkBitmap wrap_texture(GrTexture* texture) {
690 SkBitmap result;
bsalomonafbf2d62014-09-30 12:18:44 -0700691 result.setInfo(texture->surfacePriv().info());
reed6c225732014-06-09 19:52:07 -0700692 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000693 return result;
694}
695
696};
697
698void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
699 const SkPaint& paint, const SkMatrix* prePathMatrix,
700 bool pathIsMutable) {
701 CHECK_FOR_ANNOTATION(paint);
702 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700703 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000704
jvanverthb3eb6872014-10-24 07:12:51 -0700705 SkASSERT(!pathIsMutable || origSrcPath.isVolatile());
706
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000707 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000708 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000709
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000710 // If we have a prematrix, apply it to the path, optimizing for the case
711 // where the original path can in fact be modified in place (even though
712 // its parameter type is const).
713 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000714 SkTLazy<SkPath> tmpPath;
715 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000716
717 if (prePathMatrix) {
718 SkPath* result = pathPtr;
719
720 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000721 result = tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700722 result->setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000723 pathIsMutable = true;
724 }
725 // should I push prePathMatrix on our MV stack temporarily, instead
726 // of applying it here? See SkDraw.cpp
727 pathPtr->transform(*prePathMatrix, result);
728 pathPtr = result;
729 }
730 // at this point we're done with prePathMatrix
731 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
732
egdanield58a0ba2014-06-11 10:30:05 -0700733 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000734 SkPathEffect* pathEffect = paint.getPathEffect();
735 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700736 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
737 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000738 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000739 pathPtr = effectPath.get();
740 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700741 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742 }
743
egdanield58a0ba2014-06-11 10:30:05 -0700744 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000745 if (paint.getMaskFilter()) {
746 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000747 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
748 if (stroke.applyToPath(strokedPath, *pathPtr)) {
749 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000750 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700751 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000752 }
753 }
754
755 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000756 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700757 if (!pathIsMutable) {
758 devPathPtr->setIsVolatile(true);
759 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000760
761 // transform the path into device space
762 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000763
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764 SkRect maskRect;
765 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
766 draw.fClip->getBounds(),
767 fContext->getMatrix(),
768 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000769 // The context's matrix may change while creating the mask, so save the CTM here to
770 // pass to filterMaskGPU.
771 const SkMatrix ctm = fContext->getMatrix();
772
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773 SkIRect finalIRect;
774 maskRect.roundOut(&finalIRect);
775 if (draw.fClip->quickReject(finalIRect)) {
776 // clipped out
777 return;
778 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000779
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000780 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000781 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000782 // the mask filter was able to draw itself directly, so there's nothing
783 // left to do.
784 return;
785 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000787
bsalomone3059732014-10-14 11:47:22 -0700788 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext, maskRect, *devPathPtr,
789 strokeInfo, grPaint.isAntiAlias(),
790 fRenderTarget->numSamples()));
791 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000792 GrTexture* filtered;
793
bsalomone3059732014-10-14 11:47:22 -0700794 if (paint.getMaskFilter()->filterMaskGPU(mask, ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000795 // filterMaskGPU gives us ownership of a ref to the result
796 SkAutoTUnref<GrTexture> atu(filtered);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000797 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
798 // This path is completely drawn
799 return;
800 }
801 }
802 }
803 }
804
805 // draw the mask on the CPU - this is a fallthrough path in case the
806 // GPU path fails
807 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
808 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700809 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
810 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000811 return;
812 }
813
egdanield58a0ba2014-06-11 10:30:05 -0700814 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000815}
816
817static const int kBmpSmallTileSize = 1 << 10;
818
819static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
820 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
821 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
822 return tilesX * tilesY;
823}
824
825static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
826 if (maxTileSize <= kBmpSmallTileSize) {
827 return maxTileSize;
828 }
829
830 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
831 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
832
833 maxTileTotalTileSize *= maxTileSize * maxTileSize;
834 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
835
836 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
837 return kBmpSmallTileSize;
838 } else {
839 return maxTileSize;
840 }
841}
842
843// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
844// pixels from the bitmap are necessary.
845static void determine_clipped_src_rect(const GrContext* context,
846 const SkBitmap& bitmap,
847 const SkRect* srcRectPtr,
848 SkIRect* clippedSrcIRect) {
849 const GrClipData* clip = context->getClip();
850 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
851 SkMatrix inv;
852 if (!context->getMatrix().invert(&inv)) {
853 clippedSrcIRect->setEmpty();
854 return;
855 }
856 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
857 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700858 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000859 // we've setup src space 0,0 to map to the top left of the src rect.
860 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000861 if (!clippedSrcRect.intersect(*srcRectPtr)) {
862 clippedSrcIRect->setEmpty();
863 return;
864 }
865 }
866 clippedSrcRect.roundOut(clippedSrcIRect);
867 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
868 if (!clippedSrcIRect->intersect(bmpBounds)) {
869 clippedSrcIRect->setEmpty();
870 }
871}
872
873bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
874 const GrTextureParams& params,
875 const SkRect* srcRectPtr,
876 int maxTileSize,
877 int* tileSize,
878 SkIRect* clippedSrcRect) const {
879 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700880 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000881 return false;
882 }
883
884 // if it's larger than the max tile size, then we have no choice but tiling.
885 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
886 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
887 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
888 return true;
889 }
890
891 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
892 return false;
893 }
894
895 // if the entire texture is already in our cache then no reason to tile it
896 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
897 return false;
898 }
899
900 // At this point we know we could do the draw by uploading the entire bitmap
901 // as a texture. However, if the texture would be large compared to the
902 // cache size and we don't require most of it for this draw then tile to
903 // reduce the amount of upload and cache spill.
904
905 // assumption here is that sw bitmap size is a good proxy for its size as
906 // a texture
907 size_t bmpSize = bitmap.getSize();
908 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000909 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000910 if (bmpSize < cacheSize / 2) {
911 return false;
912 }
913
914 // Figure out how much of the src we will need based on the src rect and clipping.
915 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
916 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
917 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
918 kBmpSmallTileSize * kBmpSmallTileSize;
919
920 return usedTileBytes < 2 * bmpSize;
921}
922
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000923void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000924 const SkBitmap& bitmap,
925 const SkMatrix& m,
926 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000927 SkMatrix concat;
928 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
929 if (!m.isIdentity()) {
930 concat.setConcat(*draw->fMatrix, m);
931 draw.writable()->fMatrix = &concat;
932 }
933 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000934}
935
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000936// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000937// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
938// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000939static inline void clamped_outset_with_offset(SkIRect* iRect,
940 int outset,
941 SkPoint* offset,
942 const SkIRect& clamp) {
943 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000944
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000945 int leftClampDelta = clamp.fLeft - iRect->fLeft;
946 if (leftClampDelta > 0) {
947 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000948 iRect->fLeft = clamp.fLeft;
949 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000950 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000951 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000952
953 int topClampDelta = clamp.fTop - iRect->fTop;
954 if (topClampDelta > 0) {
955 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000956 iRect->fTop = clamp.fTop;
957 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000958 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000959 }
960
961 if (iRect->fRight > clamp.fRight) {
962 iRect->fRight = clamp.fRight;
963 }
964 if (iRect->fBottom > clamp.fBottom) {
965 iRect->fBottom = clamp.fBottom;
966 }
967}
968
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000969static bool has_aligned_samples(const SkRect& srcRect,
970 const SkRect& transformedRect) {
971 // detect pixel disalignment
972 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
973 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
974 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
975 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
976 SkScalarAbs(transformedRect.width() - srcRect.width()) <
977 COLOR_BLEED_TOLERANCE &&
978 SkScalarAbs(transformedRect.height() - srcRect.height()) <
979 COLOR_BLEED_TOLERANCE) {
980 return true;
981 }
982 return false;
983}
984
985static bool may_color_bleed(const SkRect& srcRect,
986 const SkRect& transformedRect,
987 const SkMatrix& m) {
988 // Only gets called if has_aligned_samples returned false.
989 // So we can assume that sampling is axis aligned but not texel aligned.
990 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
991 SkRect innerSrcRect(srcRect), innerTransformedRect,
992 outerTransformedRect(transformedRect);
993 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
994 m.mapRect(&innerTransformedRect, innerSrcRect);
995
996 // The gap between outerTransformedRect and innerTransformedRect
997 // represents the projection of the source border area, which is
998 // problematic for color bleeding. We must check whether any
999 // destination pixels sample the border area.
1000 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1001 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1002 SkIRect outer, inner;
1003 outerTransformedRect.round(&outer);
1004 innerTransformedRect.round(&inner);
1005 // If the inner and outer rects round to the same result, it means the
1006 // border does not overlap any pixel centers. Yay!
1007 return inner != outer;
1008}
1009
1010static bool needs_texture_domain(const SkBitmap& bitmap,
1011 const SkRect& srcRect,
1012 GrTextureParams &params,
1013 const SkMatrix& contextMatrix,
1014 bool bicubic) {
1015 bool needsTextureDomain = false;
1016
1017 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1018 // Need texture domain if drawing a sub rect
1019 needsTextureDomain = srcRect.width() < bitmap.width() ||
1020 srcRect.height() < bitmap.height();
1021 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1022 // sampling is axis-aligned
1023 SkRect transformedRect;
1024 contextMatrix.mapRect(&transformedRect, srcRect);
1025
1026 if (has_aligned_samples(srcRect, transformedRect)) {
1027 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1028 needsTextureDomain = false;
1029 } else {
1030 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1031 }
1032 }
1033 }
1034 return needsTextureDomain;
1035}
1036
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001037void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1038 const SkBitmap& bitmap,
1039 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001040 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001041 const SkPaint& paint,
1042 SkCanvas::DrawBitmapRectFlags flags) {
1043 CHECK_SHOULD_DRAW(draw, false);
1044
1045 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001046 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001047 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1048 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001049 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001050 SkScalar w = SkIntToScalar(bitmap.width());
1051 SkScalar h = SkIntToScalar(bitmap.height());
1052 dstSize.fWidth = w;
1053 dstSize.fHeight = h;
1054 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001055 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001056 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001057 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001058 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001059 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001060 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1061 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1062 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1063 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001064 }
1065
1066 if (paint.getMaskFilter()){
1067 // Convert the bitmap to a shader so that the rect can be drawn
1068 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001069 SkBitmap tmp; // subset of bitmap, if necessary
1070 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001071 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001072 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001073 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1074 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1075 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001076 // In bleed mode we position and trim the bitmap based on the src rect which is
1077 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1078 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1079 // compensate.
1080 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1081 SkIRect iSrc;
1082 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001083
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001084 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1085 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001086
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001087 if (!bitmap.extractSubset(&tmp, iSrc)) {
1088 return; // extraction failed
1089 }
1090 bitmapPtr = &tmp;
1091 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001092
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001093 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001094 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001095 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001096 } else {
1097 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001098 }
1099
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001100 SkPaint paintWithShader(paint);
1101 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001102 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001103 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1104 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001105
1106 return;
1107 }
1108
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001109 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1110 // the view matrix rather than a local matrix.
1111 SkMatrix m;
1112 m.setScale(dstSize.fWidth / srcRect.width(),
1113 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001114 fContext->concatMatrix(m);
1115
1116 GrTextureParams params;
1117 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1118 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001119
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001120 bool doBicubic = false;
1121
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001122 switch(paintFilterLevel) {
1123 case SkPaint::kNone_FilterLevel:
1124 textureFilterMode = GrTextureParams::kNone_FilterMode;
1125 break;
1126 case SkPaint::kLow_FilterLevel:
1127 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1128 break;
1129 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001130 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001131 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1132 } else {
1133 // Don't trigger MIP level generation unnecessarily.
1134 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1135 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001136 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001137 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001138 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001139 doBicubic =
1140 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001141 break;
1142 default:
1143 SkErrorInternals::SetError( kInvalidPaint_SkError,
1144 "Sorry, I don't understand the filtering "
1145 "mode you asked for. Falling back to "
1146 "MIPMaps.");
1147 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1148 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001149 }
1150
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001151 int tileFilterPad;
1152 if (doBicubic) {
1153 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1154 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1155 tileFilterPad = 0;
1156 } else {
1157 tileFilterPad = 1;
1158 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001159 params.setFilterMode(textureFilterMode);
1160
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001161 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001162 int tileSize;
1163
1164 SkIRect clippedSrcRect;
1165 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1166 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001167 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1168 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001169 } else {
1170 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001171 bool needsTextureDomain = needs_texture_domain(bitmap,
1172 srcRect,
1173 params,
1174 fContext->getMatrix(),
1175 doBicubic);
1176 this->internalDrawBitmap(bitmap,
1177 srcRect,
1178 params,
1179 paint,
1180 flags,
1181 doBicubic,
1182 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001183 }
1184}
1185
1186// Break 'bitmap' into several tiles to draw it since it has already
1187// been determined to be too large to fit in VRAM
1188void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1189 const SkRect& srcRect,
1190 const SkIRect& clippedSrcIRect,
1191 const GrTextureParams& params,
1192 const SkPaint& paint,
1193 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001194 int tileSize,
1195 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001196 // The following pixel lock is technically redundant, but it is desirable
1197 // to lock outside of the tile loop to prevent redecoding the whole image
1198 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1199 // is larger than the limit of the discardable memory pool.
1200 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001201 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1202
1203 int nx = bitmap.width() / tileSize;
1204 int ny = bitmap.height() / tileSize;
1205 for (int x = 0; x <= nx; x++) {
1206 for (int y = 0; y <= ny; y++) {
1207 SkRect tileR;
1208 tileR.set(SkIntToScalar(x * tileSize),
1209 SkIntToScalar(y * tileSize),
1210 SkIntToScalar((x + 1) * tileSize),
1211 SkIntToScalar((y + 1) * tileSize));
1212
1213 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1214 continue;
1215 }
1216
1217 if (!tileR.intersect(srcRect)) {
1218 continue;
1219 }
1220
1221 SkBitmap tmpB;
1222 SkIRect iTileR;
1223 tileR.roundOut(&iTileR);
1224 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1225 SkIntToScalar(iTileR.fTop));
1226
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001227 // Adjust the context matrix to draw at the right x,y in device space
1228 SkMatrix tmpM;
1229 GrContext::AutoMatrix am;
1230 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1231 am.setPreConcat(fContext, tmpM);
1232
robertphillipsec8bb942014-11-21 10:16:25 -08001233 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001234 SkIRect iClampRect;
1235
1236 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1237 // In bleed mode we want to always expand the tile on all edges
1238 // but stay within the bitmap bounds
1239 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1240 } else {
1241 // In texture-domain/clamp mode we only want to expand the
1242 // tile on edges interior to "srcRect" (i.e., we want to
1243 // not bleed across the original clamped edges)
1244 srcRect.roundOut(&iClampRect);
1245 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001246 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1247 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001248 }
1249
1250 if (bitmap.extractSubset(&tmpB, iTileR)) {
1251 // now offset it to make it "local" to our tmp bitmap
1252 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001253 GrTextureParams paramsTemp = params;
1254 bool needsTextureDomain = needs_texture_domain(bitmap,
1255 srcRect,
1256 paramsTemp,
1257 fContext->getMatrix(),
1258 bicubic);
1259 this->internalDrawBitmap(tmpB,
1260 tileR,
1261 paramsTemp,
1262 paint,
1263 flags,
1264 bicubic,
1265 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001266 }
1267 }
1268 }
1269}
1270
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001271
1272/*
1273 * This is called by drawBitmap(), which has to handle images that may be too
1274 * large to be represented by a single texture.
1275 *
1276 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1277 * and that non-texture portion of the GrPaint has already been setup.
1278 */
1279void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1280 const SkRect& srcRect,
1281 const GrTextureParams& params,
1282 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001283 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001284 bool bicubic,
1285 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001286 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1287 bitmap.height() <= fContext->getMaxTextureSize());
1288
1289 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001290 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001291 if (NULL == texture) {
1292 return;
1293 }
1294
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001295 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001296 SkRect paintRect;
1297 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1298 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1299 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1300 SkScalarMul(srcRect.fTop, hInv),
1301 SkScalarMul(srcRect.fRight, wInv),
1302 SkScalarMul(srcRect.fBottom, hInv));
1303
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001304 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001305 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001306 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001307 // Use a constrained texture domain to avoid color bleeding
1308 SkScalar left, top, right, bottom;
1309 if (srcRect.width() > SK_Scalar1) {
1310 SkScalar border = SK_ScalarHalf / texture->width();
1311 left = paintRect.left() + border;
1312 right = paintRect.right() - border;
1313 } else {
1314 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1315 }
1316 if (srcRect.height() > SK_Scalar1) {
1317 SkScalar border = SK_ScalarHalf / texture->height();
1318 top = paintRect.top() + border;
1319 bottom = paintRect.bottom() - border;
1320 } else {
1321 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1322 }
1323 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001324 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001325 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001326 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001327 fp.reset(GrTextureDomainEffect::Create(texture,
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001328 SkMatrix::I(),
1329 textureDomain,
1330 GrTextureDomain::kClamp_Mode,
1331 params.filterMode()));
1332 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001333 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001334 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1335 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001336 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001337 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001338 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001339 }
1340
1341 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1342 // the rest from the SkPaint.
1343 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001344 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001345 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001346 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1347 SkColor2GrColor(paint.getColor());
1348 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001349
bsalomon01c8da12014-08-04 09:21:30 -07001350 fContext->drawRectToRect(grPaint, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001351}
1352
fmalita2d97bc12014-11-20 10:44:58 -08001353bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
1354 const SkImageFilter* filter,
1355 const SkImageFilter::Context& ctx,
1356 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001357 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001358
1359 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1360 // (https://code.google.com/p/skia/issues/detail?id=3148).
1361 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001362
1363 if (filter->canFilterImageGPU()) {
1364 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1365 // filter. Also set the clip wide open and the matrix to identity.
1366 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001367 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001368 } else {
1369 return false;
1370 }
1371}
1372
1373void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1374 int left, int top, const SkPaint& paint) {
1375 // drawSprite is defined to be in device coords.
1376 CHECK_SHOULD_DRAW(draw, true);
1377
1378 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1379 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1380 return;
1381 }
1382
1383 int w = bitmap.width();
1384 int h = bitmap.height();
1385
1386 GrTexture* texture;
1387 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001388 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001389
1390 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001391 // This bitmap will own the filtered result as a texture.
1392 SkBitmap filteredBitmap;
1393
bsalomon49f085d2014-09-05 13:34:00 -07001394 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001395 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001396 SkMatrix matrix(*draw.fMatrix);
1397 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001398 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001399 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001400 // This cache is transient, and is freed (along with all its contained
1401 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001402 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001403 if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap,
1404 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001405 texture = (GrTexture*) filteredBitmap.getTexture();
1406 w = filteredBitmap.width();
1407 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001408 left += offset.x();
1409 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001410 } else {
1411 return;
1412 }
1413 }
1414
1415 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001416 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001417
dandov9de5b512014-06-10 14:38:28 -07001418 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1419 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001420
1421 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001422 SkRect::MakeXYWH(SkIntToScalar(left),
1423 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001424 SkIntToScalar(w),
1425 SkIntToScalar(h)),
1426 SkRect::MakeXYWH(0,
1427 0,
1428 SK_Scalar1 * w / texture->width(),
1429 SK_Scalar1 * h / texture->height()));
1430}
1431
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001432void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001433 const SkRect* src, const SkRect& dst,
1434 const SkPaint& paint,
1435 SkCanvas::DrawBitmapRectFlags flags) {
1436 SkMatrix matrix;
1437 SkRect bitmapBounds, tmpSrc;
1438
1439 bitmapBounds.set(0, 0,
1440 SkIntToScalar(bitmap.width()),
1441 SkIntToScalar(bitmap.height()));
1442
1443 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001444 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001445 tmpSrc = *src;
1446 } else {
1447 tmpSrc = bitmapBounds;
1448 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001449
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001450 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1451
1452 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001453 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001454 if (!bitmapBounds.contains(tmpSrc)) {
1455 if (!tmpSrc.intersect(bitmapBounds)) {
1456 return; // nothing to draw
1457 }
1458 }
1459 }
1460
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001461 SkRect tmpDst;
1462 matrix.mapRect(&tmpDst, tmpSrc);
1463
1464 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1465 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1466 // Translate so that tempDst's top left is at the origin.
1467 matrix = *origDraw.fMatrix;
1468 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1469 draw.writable()->fMatrix = &matrix;
1470 }
1471 SkSize dstSize;
1472 dstSize.fWidth = tmpDst.width();
1473 dstSize.fHeight = tmpDst.height();
1474
1475 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001476}
1477
1478void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1479 int x, int y, const SkPaint& paint) {
1480 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001481 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001482 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
jvanverth4736e142014-11-07 07:12:46 -08001483 if (dev->fFlags & kNeedClear_Flag) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001484 // TODO: could check here whether we really need to draw at all
reed37294692014-12-02 10:08:11 -08001485 dev->clearAll();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001486 }
1487
1488 // drawDevice is defined to be in device coords.
1489 CHECK_SHOULD_DRAW(draw, true);
1490
1491 GrRenderTarget* devRT = dev->accessRenderTarget();
1492 GrTexture* devTex;
1493 if (NULL == (devTex = devRT->asTexture())) {
1494 return;
1495 }
1496
1497 const SkBitmap& bm = dev->accessBitmap(false);
1498 int w = bm.width();
1499 int h = bm.height();
1500
1501 SkImageFilter* filter = paint.getImageFilter();
1502 // This bitmap will own the filtered result as a texture.
1503 SkBitmap filteredBitmap;
1504
bsalomon49f085d2014-09-05 13:34:00 -07001505 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001506 SkIPoint offset = SkIPoint::Make(0, 0);
1507 SkMatrix matrix(*draw.fMatrix);
1508 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001509 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001510 // This cache is transient, and is freed (along with all its contained
1511 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001512 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
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, devTex, filter, ctx, &filteredBitmap,
1515 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001516 devTex = filteredBitmap.getTexture();
1517 w = filteredBitmap.width();
1518 h = filteredBitmap.height();
1519 x += offset.fX;
1520 y += offset.fY;
1521 } else {
1522 return;
1523 }
1524 }
1525
1526 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001527 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001528
dandov9de5b512014-06-10 14:38:28 -07001529 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1530 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001531
1532 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1533 SkIntToScalar(y),
1534 SkIntToScalar(w),
1535 SkIntToScalar(h));
1536
1537 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1538 // scratch texture).
1539 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1540 SK_Scalar1 * h / devTex->height());
1541
1542 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1543}
1544
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001545bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001546 return filter->canFilterImageGPU();
1547}
1548
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001549bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001550 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001551 SkBitmap* result, SkIPoint* offset) {
1552 // want explicitly our impl, so guard against a subclass of us overriding it
1553 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1554 return false;
1555 }
1556
1557 SkAutoLockPixels alp(src, !src.getTexture());
1558 if (!src.getTexture() && !src.readyToDraw()) {
1559 return false;
1560 }
1561
1562 GrTexture* texture;
1563 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1564 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001565 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001566
fmalita2d97bc12014-11-20 10:44:58 -08001567 return this->filterTexture(fContext, texture, filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001568}
1569
1570///////////////////////////////////////////////////////////////////////////////
1571
1572// must be in SkCanvas::VertexMode order
1573static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1574 kTriangles_GrPrimitiveType,
1575 kTriangleStrip_GrPrimitiveType,
1576 kTriangleFan_GrPrimitiveType,
1577};
1578
1579void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1580 int vertexCount, const SkPoint vertices[],
1581 const SkPoint texs[], const SkColor colors[],
1582 SkXfermode* xmode,
1583 const uint16_t indices[], int indexCount,
1584 const SkPaint& paint) {
1585 CHECK_SHOULD_DRAW(draw, false);
1586
egdanield78a1682014-07-09 10:41:26 -07001587 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001588
dandov32a311b2014-07-15 19:46:26 -07001589 const uint16_t* outIndices;
1590 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1591 GrPrimitiveType primType;
1592 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001593
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001594 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1595 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001596
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001597 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001598
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001599 SkPaint copy(paint);
1600 copy.setStyle(SkPaint::kStroke_Style);
1601 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001602
dandov32a311b2014-07-15 19:46:26 -07001603 // we ignore the shader if texs is null.
1604 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1605 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001606
dandov32a311b2014-07-15 19:46:26 -07001607 primType = kLines_GrPrimitiveType;
1608 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001609 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001610 switch (vmode) {
1611 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001612 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001613 break;
1614 case SkCanvas::kTriangleStrip_VertexMode:
1615 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001616 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001617 break;
1618 }
mtklein533eb782014-08-27 10:39:42 -07001619
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001620 VertState state(vertexCount, indices, indexCount);
1621 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001622
dandov32a311b2014-07-15 19:46:26 -07001623 //number of indices for lines per triangle with kLines
1624 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001625
dandov32a311b2014-07-15 19:46:26 -07001626 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1627 outIndices = outAlloc.get();
1628 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001629 int i = 0;
1630 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001631 auxIndices[i] = state.f0;
1632 auxIndices[i + 1] = state.f1;
1633 auxIndices[i + 2] = state.f1;
1634 auxIndices[i + 3] = state.f2;
1635 auxIndices[i + 4] = state.f2;
1636 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001637 i += 6;
1638 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001639 } else {
dandov32a311b2014-07-15 19:46:26 -07001640 outIndices = indices;
1641 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001642
dandov32a311b2014-07-15 19:46:26 -07001643 if (NULL == texs || NULL == paint.getShader()) {
1644 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1645 NULL == colors, &grPaint);
1646 } else {
1647 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
1648 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001649 }
1650
mtklein2583b622014-06-04 08:20:41 -07001651#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001652 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001653 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1654 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001655 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001656 }
1657 }
mtklein2583b622014-06-04 08:20:41 -07001658#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001659
1660 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001661 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001662 // need to convert byte order and from non-PM to PM
1663 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001664 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001665 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001666 color = colors[i];
1667 if (paint.getAlpha() != 255) {
1668 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1669 }
1670 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001671 }
1672 colors = convertedColors.get();
1673 }
1674 fContext->drawVertices(grPaint,
dandov32a311b2014-07-15 19:46:26 -07001675 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001676 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001677 vertices,
1678 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001679 colors,
dandov32a311b2014-07-15 19:46:26 -07001680 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001681 indexCount);
1682}
1683
1684///////////////////////////////////////////////////////////////////////////////
1685
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001686void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1687 size_t byteLength, SkScalar x, SkScalar y,
1688 const SkPaint& paint) {
1689 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -07001690 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001691
jvanverth8c27a182014-10-14 08:45:50 -07001692 GrPaint grPaint;
1693 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001694
jvanverth8c27a182014-10-14 08:45:50 -07001695 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001696
jvanverth8c27a182014-10-14 08:45:50 -07001697 if (!fTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y)) {
1698 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001699 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001700 }
1701}
1702
fmalita05c4a432014-09-29 06:29:53 -07001703void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1704 const SkScalar pos[], int scalarsPerPos,
1705 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001706 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001707 CHECK_SHOULD_DRAW(draw, false);
1708
jvanverth8c27a182014-10-14 08:45:50 -07001709 GrPaint grPaint;
1710 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001711
jvanverth8c27a182014-10-14 08:45:50 -07001712 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001713
jvanverth8c27a182014-10-14 08:45:50 -07001714 if (!fTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
1715 scalarsPerPos, offset)) {
1716 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001717 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001718 }
1719}
1720
1721void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1722 size_t len, const SkPath& path,
1723 const SkMatrix* m, const SkPaint& paint) {
1724 CHECK_SHOULD_DRAW(draw, false);
1725
1726 SkASSERT(draw.fDevice == this);
1727 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1728}
1729
1730///////////////////////////////////////////////////////////////////////////////
1731
reedb2db8982014-11-13 12:41:02 -08001732bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001733 if (paint.getShader() ||
reedb2db8982014-11-13 12:41:02 -08001734 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001735 paint.getMaskFilter() ||
1736 paint.getRasterizer() ||
1737 paint.getColorFilter() ||
1738 paint.getPathEffect() ||
1739 paint.isFakeBoldText() ||
reedb2db8982014-11-13 12:41:02 -08001740 paint.getStyle() != SkPaint::kFill_Style)
1741 {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001742 return true;
1743 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001744 return false;
1745}
1746
1747void SkGpuDevice::flush() {
1748 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001749 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001750}
1751
1752///////////////////////////////////////////////////////////////////////////////
1753
fmalita6987dca2014-11-13 08:33:37 -08001754SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(const CreateInfo& cinfo) {
bsalomonf2703d82014-10-28 14:33:06 -07001755 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001756 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001757 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001758 desc.fWidth = cinfo.fInfo.width();
1759 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001760 desc.fSampleCnt = fRenderTarget->numSamples();
1761
1762 SkAutoTUnref<GrTexture> texture;
1763 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001764 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
jvanverth4736e142014-11-07 07:12:46 -08001765 // If we're using distance field text, enable in the new device
1766 flags |= (fFlags & kDFText_Flag) ? kDFText_Flag : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001767
1768#if CACHE_COMPATIBLE_DEVICE_TEXTURES
hcm4396fa52014-10-27 21:43:30 -07001769 // layers are never draw in repeat modes, so we can request an approx
1770 // match and ignore any padding.
fmalita6987dca2014-11-13 08:33:37 -08001771 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == cinfo.fUsage) ?
hcm4396fa52014-10-27 21:43:30 -07001772 GrContext::kApprox_ScratchTexMatch :
1773 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001774 texture.reset(fContext->refScratchTexture(desc, match));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001775#else
1776 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1777#endif
bsalomon49f085d2014-09-05 13:34:00 -07001778 if (texture.get()) {
reedb2db8982014-11-13 12:41:02 -08001779 return SkGpuDevice::Create(texture, SkSurfaceProps(0, cinfo.fPixelGeometry), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001780 } else {
tfarina38406c82014-10-31 07:11:12 -07001781 SkDebugf("---- failed to create compatible device texture [%d %d]\n",
fmalita6987dca2014-11-13 08:33:37 -08001782 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001783 return NULL;
1784 }
1785}
1786
reed4a8126e2014-09-22 07:29:03 -07001787SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1788 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001789}
1790
robertphillips30d2cc62014-09-24 08:52:18 -07001791bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001792 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips30d78412014-11-24 09:49:17 -08001793 // todo: should handle this natively
1794 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001795 return false;
1796 }
1797
robertphillips82365912014-11-12 09:32:34 -08001798 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips81f71b62014-11-11 04:54:49 -08001799
1800 const SkPicture::AccelData* data = mainPicture->EXPERIMENTAL_getAccelData(key);
1801 if (!data) {
1802 return false;
1803 }
1804
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001805 SkRect clipBounds;
robertphillips0c423322014-07-31 11:02:38 -07001806 if (!mainCanvas->getClipBounds(&clipBounds)) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001807 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001808 }
1809
robertphillips30d78412014-11-24 09:49:17 -08001810 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1811
1812 const SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1813
robertphillipsfd61ed02014-10-28 07:21:44 -07001814 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001815
robertphillipsfd61ed02014-10-28 07:21:44 -07001816 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001817 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001818 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001819 &atlasedNeedRendering, &atlasedRecycled,
1820 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001821
1822 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1823
1824 SkTDArray<GrHoistedLayer> needRendering, recycled;
1825
1826 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001827 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001828 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001829 &needRendering, &recycled,
1830 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001831
1832 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001833
robertphillips4aa6dfc2014-09-17 07:50:47 -07001834 GrReplacements replacements;
1835
robertphillips01d6e5f2014-12-01 09:09:27 -08001836 GrLayerHoister::ConvertLayersToReplacements(mainPicture, needRendering, &replacements);
1837 GrLayerHoister::ConvertLayersToReplacements(mainPicture, recycled, &replacements);
robertphillips4aa6dfc2014-09-17 07:50:47 -07001838
robertphillips64bf7672014-08-21 13:07:35 -07001839 // Render the entire picture using new layers
robertphillipsee6631e2014-09-29 05:32:49 -07001840 GrRecordReplaceDraw(mainPicture, mainCanvas, &replacements, initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001841
robertphillipsfd61ed02014-10-28 07:21:44 -07001842 GrLayerHoister::UnlockLayers(fContext, needRendering);
1843 GrLayerHoister::UnlockLayers(fContext, recycled);
1844 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1845 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001846
1847 return true;
1848}
1849
senorblancobe129b22014-08-08 07:14:35 -07001850SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001851 // We always return a transient cache, so it is freed after each
1852 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001853 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001854}
reedf037e0b2014-10-30 11:34:15 -07001855
1856#endif