blob: 407f36037da15ba96a2221e1cd0aef6787b567a2 [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) { \
reedaf641a12014-12-02 19:40:18 -080071 this->clear(SK_ColorTRANSPARENT); \
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
297///////////////////////////////////////////////////////////////////////////////
298
299SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
300SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
301SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
302SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
303SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
304 shader_type_mismatch);
305SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
306 shader_type_mismatch);
307SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
308SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
309
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000310///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000311
reedaf641a12014-12-02 19:40:18 -0800312void SkGpuDevice::clear(SkColor color) {
313 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clear", fContext);
314 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
315 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
316 fFlags &= ~kNeedClear_Flag;
317}
318
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000319void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
320 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700321 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000322
323 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000324 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000325
326 fContext->drawPaint(grPaint);
327}
328
329// must be in SkCanvas::PointMode order
330static const GrPrimitiveType gPointMode2PrimtiveType[] = {
331 kPoints_GrPrimitiveType,
332 kLines_GrPrimitiveType,
333 kLineStrip_GrPrimitiveType
334};
335
336void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
337 size_t count, const SkPoint pts[], const SkPaint& paint) {
338 CHECK_FOR_ANNOTATION(paint);
339 CHECK_SHOULD_DRAW(draw, false);
340
341 SkScalar width = paint.getStrokeWidth();
342 if (width < 0) {
343 return;
344 }
345
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000346 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700347 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
348 GrPaint grPaint;
349 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
350 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700351 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700352 path.moveTo(pts[0]);
353 path.lineTo(pts[1]);
354 fContext->drawPath(grPaint, path, strokeInfo);
355 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000356 }
357
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000358 // we only handle hairlines and paints without path effects or mask filters,
359 // else we let the SkDraw call our drawPath()
360 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
361 draw.drawPoints(mode, count, pts, paint, true);
362 return;
363 }
364
365 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000366 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000367
368 fContext->drawVertices(grPaint,
369 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000370 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000371 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000372 NULL,
373 NULL,
374 NULL,
375 0);
376}
377
378///////////////////////////////////////////////////////////////////////////////
379
380void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
381 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700382 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
383
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000384 CHECK_FOR_ANNOTATION(paint);
385 CHECK_SHOULD_DRAW(draw, false);
386
387 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
388 SkScalar width = paint.getStrokeWidth();
389
390 /*
391 We have special code for hairline strokes, miter-strokes, bevel-stroke
392 and fills. Anything else we just call our path code.
393 */
394 bool usePath = doStroke && width > 0 &&
395 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
396 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
397 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700398
399 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000400 usePath = true;
401 }
egdanield58a0ba2014-06-11 10:30:05 -0700402
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000403 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
404#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
405 if (doStroke) {
406#endif
407 usePath = true;
408#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
409 } else {
410 usePath = !fContext->getMatrix().preservesRightAngles();
411 }
412#endif
413 }
414 // until we can both stroke and fill rectangles
415 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
416 usePath = true;
417 }
418
egdanield58a0ba2014-06-11 10:30:05 -0700419 GrStrokeInfo strokeInfo(paint);
420
421 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700422 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700423 usePath = true;
424 }
425
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000426 if (usePath) {
427 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700428 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000429 path.addRect(rect);
430 this->drawPath(draw, path, paint, NULL, true);
431 return;
432 }
433
434 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000435 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400436
egdanield58a0ba2014-06-11 10:30:05 -0700437 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000438}
439
440///////////////////////////////////////////////////////////////////////////////
441
442void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
443 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700444 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000445 CHECK_FOR_ANNOTATION(paint);
446 CHECK_SHOULD_DRAW(draw, false);
447
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000448 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000449 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400450
egdanield58a0ba2014-06-11 10:30:05 -0700451 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000452 if (paint.getMaskFilter()) {
453 // try to hit the fast path for drawing filtered round rects
454
455 SkRRect devRRect;
456 if (rect.transform(fContext->getMatrix(), &devRRect)) {
457 if (devRRect.allCornersCircular()) {
458 SkRect maskRect;
459 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
460 draw.fClip->getBounds(),
461 fContext->getMatrix(),
462 &maskRect)) {
463 SkIRect finalIRect;
464 maskRect.roundOut(&finalIRect);
465 if (draw.fClip->quickReject(finalIRect)) {
466 // clipped out
467 return;
468 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000469 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700470 strokeInfo.getStrokeRec(),
471 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000472 return;
473 }
474 }
475
476 }
477 }
478
479 }
480
egdanield58a0ba2014-06-11 10:30:05 -0700481 bool usePath = false;
482
483 if (paint.getMaskFilter()) {
484 usePath = true;
485 } else {
486 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700487 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700488 usePath = true;
489 }
490 }
491
492
493 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000494 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700495 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000496 path.addRRect(rect);
497 this->drawPath(draw, path, paint, NULL, true);
498 return;
499 }
Mike Klein744fb732014-06-23 15:13:26 -0400500
egdanield58a0ba2014-06-11 10:30:05 -0700501 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000502}
503
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000504void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
505 const SkRRect& inner, const SkPaint& paint) {
506 SkStrokeRec stroke(paint);
507 if (stroke.isFillStyle()) {
508
509 CHECK_FOR_ANNOTATION(paint);
510 CHECK_SHOULD_DRAW(draw, false);
511
512 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000513 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000514
515 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
516 fContext->drawDRRect(grPaint, outer, inner);
517 return;
518 }
519 }
520
521 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700522 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000523 path.addRRect(outer);
524 path.addRRect(inner);
525 path.setFillType(SkPath::kEvenOdd_FillType);
526
527 this->drawPath(draw, path, paint, NULL, true);
528}
529
530
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000531/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000532
533void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
534 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700535 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000536 CHECK_FOR_ANNOTATION(paint);
537 CHECK_SHOULD_DRAW(draw, false);
538
egdanield58a0ba2014-06-11 10:30:05 -0700539 GrStrokeInfo strokeInfo(paint);
540
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541 bool usePath = false;
542 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700543 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000544 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700545 } else {
546 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700547 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700548 usePath = true;
549 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550 }
551
552 if (usePath) {
553 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700554 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000555 path.addOval(oval);
556 this->drawPath(draw, path, paint, NULL, true);
557 return;
558 }
559
560 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000561 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000562
egdanield58a0ba2014-06-11 10:30:05 -0700563 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000564}
565
566#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000567
568///////////////////////////////////////////////////////////////////////////////
569
570// helpers for applying mask filters
571namespace {
572
573// Draw a mask using the supplied paint. Since the coverage/geometry
574// is already burnt into the mask this boils down to a rect draw.
575// Return true if the mask was successfully drawn.
576bool draw_mask(GrContext* context, const SkRect& maskRect,
577 GrPaint* grp, GrTexture* mask) {
578 GrContext::AutoMatrix am;
579 if (!am.setIdentity(context, grp)) {
580 return false;
581 }
582
583 SkMatrix matrix;
584 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
585 matrix.postIDiv(mask->width(), mask->height());
586
joshualittb0a8a372014-09-23 09:50:21 -0700587 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000588 context->drawRect(*grp, maskRect);
589 return true;
590}
591
592bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700593 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000594 GrPaint* grp, SkPaint::Style style) {
595 SkMask srcM, dstM;
596
597 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
598 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
599 return false;
600 }
601 SkAutoMaskFreeImage autoSrc(srcM.fImage);
602
603 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
604 return false;
605 }
606 // this will free-up dstM when we're done (allocated in filterMask())
607 SkAutoMaskFreeImage autoDst(dstM.fImage);
608
609 if (clip.quickReject(dstM.fBounds)) {
610 return false;
611 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000612
613 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
614 // the current clip (and identity matrix) and GrPaint settings
bsalomonf2703d82014-10-28 14:33:06 -0700615 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000616 desc.fWidth = dstM.fBounds.width();
617 desc.fHeight = dstM.fBounds.height();
618 desc.fConfig = kAlpha_8_GrPixelConfig;
619
bsalomone3059732014-10-14 11:47:22 -0700620 SkAutoTUnref<GrTexture> texture(
621 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
622 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000623 return false;
624 }
625 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
626 dstM.fImage, dstM.fRowBytes);
627
628 SkRect maskRect = SkRect::Make(dstM.fBounds);
629
630 return draw_mask(context, maskRect, grp, texture);
631}
632
bsalomone3059732014-10-14 11:47:22 -0700633// Create a mask of 'devPath' and place the result in 'mask'.
634GrTexture* create_mask_GPU(GrContext* context,
635 const SkRect& maskRect,
636 const SkPath& devPath,
637 const GrStrokeInfo& strokeInfo,
638 bool doAA,
639 int sampleCnt) {
bsalomonf2703d82014-10-28 14:33:06 -0700640 GrSurfaceDesc desc;
641 desc.fFlags = kRenderTarget_GrSurfaceFlag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000642 desc.fWidth = SkScalarCeilToInt(maskRect.width());
643 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700644 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000645 // We actually only need A8, but it often isn't supported as a
646 // render target so default to RGBA_8888
647 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700648
649 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
650 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000651 desc.fConfig = kAlpha_8_GrPixelConfig;
652 }
653
bsalomone3059732014-10-14 11:47:22 -0700654 GrTexture* mask = context->refScratchTexture(desc,GrContext::kApprox_ScratchTexMatch);
655 if (NULL == mask) {
656 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000657 }
658
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000659 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
660
bsalomone3059732014-10-14 11:47:22 -0700661 GrContext::AutoRenderTarget art(context, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000662 GrContext::AutoClip ac(context, clipRect);
663
bsalomon89c62982014-11-03 12:08:42 -0800664 context->clear(NULL, 0x0, true, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000665
666 GrPaint tempPaint;
667 if (doAA) {
668 tempPaint.setAntiAlias(true);
669 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
670 // blend coeff of zero requires dual source blending support in order
671 // to properly blend partially covered pixels. This means the AA
672 // code path may not be taken. So we use a dst blend coeff of ISA. We
673 // could special case AA draws to a dst surface with known alpha=0 to
674 // use a zero dst coeff when dual source blending isn't available.
675 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
676 }
677
678 GrContext::AutoMatrix am;
679
680 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
681 SkMatrix translate;
682 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
683 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700684 context->drawPath(tempPaint, devPath, strokeInfo);
bsalomone3059732014-10-14 11:47:22 -0700685 return mask;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000686}
687
688SkBitmap wrap_texture(GrTexture* texture) {
689 SkBitmap result;
bsalomonafbf2d62014-09-30 12:18:44 -0700690 result.setInfo(texture->surfacePriv().info());
reed6c225732014-06-09 19:52:07 -0700691 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000692 return result;
693}
694
695};
696
697void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
698 const SkPaint& paint, const SkMatrix* prePathMatrix,
699 bool pathIsMutable) {
700 CHECK_FOR_ANNOTATION(paint);
701 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700702 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000703
jvanverthb3eb6872014-10-24 07:12:51 -0700704 SkASSERT(!pathIsMutable || origSrcPath.isVolatile());
705
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000706 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000707 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000708
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000709 // If we have a prematrix, apply it to the path, optimizing for the case
710 // where the original path can in fact be modified in place (even though
711 // its parameter type is const).
712 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000713 SkTLazy<SkPath> tmpPath;
714 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000715
716 if (prePathMatrix) {
717 SkPath* result = pathPtr;
718
719 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000720 result = tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700721 result->setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000722 pathIsMutable = true;
723 }
724 // should I push prePathMatrix on our MV stack temporarily, instead
725 // of applying it here? See SkDraw.cpp
726 pathPtr->transform(*prePathMatrix, result);
727 pathPtr = result;
728 }
729 // at this point we're done with prePathMatrix
730 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
731
egdanield58a0ba2014-06-11 10:30:05 -0700732 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000733 SkPathEffect* pathEffect = paint.getPathEffect();
734 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700735 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
736 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000737 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000738 pathPtr = effectPath.get();
739 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700740 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000741 }
742
egdanield58a0ba2014-06-11 10:30:05 -0700743 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000744 if (paint.getMaskFilter()) {
745 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000746 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
747 if (stroke.applyToPath(strokedPath, *pathPtr)) {
748 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000749 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700750 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000751 }
752 }
753
754 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000755 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700756 if (!pathIsMutable) {
757 devPathPtr->setIsVolatile(true);
758 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000759
760 // transform the path into device space
761 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000762
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000763 SkRect maskRect;
764 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
765 draw.fClip->getBounds(),
766 fContext->getMatrix(),
767 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000768 // The context's matrix may change while creating the mask, so save the CTM here to
769 // pass to filterMaskGPU.
770 const SkMatrix ctm = fContext->getMatrix();
771
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000772 SkIRect finalIRect;
773 maskRect.roundOut(&finalIRect);
774 if (draw.fClip->quickReject(finalIRect)) {
775 // clipped out
776 return;
777 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000778
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000779 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000780 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000781 // the mask filter was able to draw itself directly, so there's nothing
782 // left to do.
783 return;
784 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000785
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786
bsalomone3059732014-10-14 11:47:22 -0700787 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext, maskRect, *devPathPtr,
788 strokeInfo, grPaint.isAntiAlias(),
789 fRenderTarget->numSamples()));
790 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000791 GrTexture* filtered;
792
bsalomone3059732014-10-14 11:47:22 -0700793 if (paint.getMaskFilter()->filterMaskGPU(mask, ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000794 // filterMaskGPU gives us ownership of a ref to the result
795 SkAutoTUnref<GrTexture> atu(filtered);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000796 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
797 // This path is completely drawn
798 return;
799 }
800 }
801 }
802 }
803
804 // draw the mask on the CPU - this is a fallthrough path in case the
805 // GPU path fails
806 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
807 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700808 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
809 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000810 return;
811 }
812
egdanield58a0ba2014-06-11 10:30:05 -0700813 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000814}
815
816static const int kBmpSmallTileSize = 1 << 10;
817
818static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
819 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
820 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
821 return tilesX * tilesY;
822}
823
824static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
825 if (maxTileSize <= kBmpSmallTileSize) {
826 return maxTileSize;
827 }
828
829 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
830 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
831
832 maxTileTotalTileSize *= maxTileSize * maxTileSize;
833 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
834
835 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
836 return kBmpSmallTileSize;
837 } else {
838 return maxTileSize;
839 }
840}
841
842// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
843// pixels from the bitmap are necessary.
844static void determine_clipped_src_rect(const GrContext* context,
845 const SkBitmap& bitmap,
846 const SkRect* srcRectPtr,
847 SkIRect* clippedSrcIRect) {
848 const GrClipData* clip = context->getClip();
849 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
850 SkMatrix inv;
851 if (!context->getMatrix().invert(&inv)) {
852 clippedSrcIRect->setEmpty();
853 return;
854 }
855 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
856 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700857 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000858 // we've setup src space 0,0 to map to the top left of the src rect.
859 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000860 if (!clippedSrcRect.intersect(*srcRectPtr)) {
861 clippedSrcIRect->setEmpty();
862 return;
863 }
864 }
865 clippedSrcRect.roundOut(clippedSrcIRect);
866 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
867 if (!clippedSrcIRect->intersect(bmpBounds)) {
868 clippedSrcIRect->setEmpty();
869 }
870}
871
872bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
873 const GrTextureParams& params,
874 const SkRect* srcRectPtr,
875 int maxTileSize,
876 int* tileSize,
877 SkIRect* clippedSrcRect) const {
878 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700879 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000880 return false;
881 }
882
883 // if it's larger than the max tile size, then we have no choice but tiling.
884 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
885 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
886 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
887 return true;
888 }
889
890 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
891 return false;
892 }
893
894 // if the entire texture is already in our cache then no reason to tile it
895 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
896 return false;
897 }
898
899 // At this point we know we could do the draw by uploading the entire bitmap
900 // as a texture. However, if the texture would be large compared to the
901 // cache size and we don't require most of it for this draw then tile to
902 // reduce the amount of upload and cache spill.
903
904 // assumption here is that sw bitmap size is a good proxy for its size as
905 // a texture
906 size_t bmpSize = bitmap.getSize();
907 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000908 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000909 if (bmpSize < cacheSize / 2) {
910 return false;
911 }
912
913 // Figure out how much of the src we will need based on the src rect and clipping.
914 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
915 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
916 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
917 kBmpSmallTileSize * kBmpSmallTileSize;
918
919 return usedTileBytes < 2 * bmpSize;
920}
921
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000922void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000923 const SkBitmap& bitmap,
924 const SkMatrix& m,
925 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000926 SkMatrix concat;
927 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
928 if (!m.isIdentity()) {
929 concat.setConcat(*draw->fMatrix, m);
930 draw.writable()->fMatrix = &concat;
931 }
932 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933}
934
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000935// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000936// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
937// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000938static inline void clamped_outset_with_offset(SkIRect* iRect,
939 int outset,
940 SkPoint* offset,
941 const SkIRect& clamp) {
942 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000943
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000944 int leftClampDelta = clamp.fLeft - iRect->fLeft;
945 if (leftClampDelta > 0) {
946 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000947 iRect->fLeft = clamp.fLeft;
948 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000949 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000950 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000951
952 int topClampDelta = clamp.fTop - iRect->fTop;
953 if (topClampDelta > 0) {
954 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000955 iRect->fTop = clamp.fTop;
956 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000957 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958 }
959
960 if (iRect->fRight > clamp.fRight) {
961 iRect->fRight = clamp.fRight;
962 }
963 if (iRect->fBottom > clamp.fBottom) {
964 iRect->fBottom = clamp.fBottom;
965 }
966}
967
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000968static bool has_aligned_samples(const SkRect& srcRect,
969 const SkRect& transformedRect) {
970 // detect pixel disalignment
971 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
972 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
973 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
974 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
975 SkScalarAbs(transformedRect.width() - srcRect.width()) <
976 COLOR_BLEED_TOLERANCE &&
977 SkScalarAbs(transformedRect.height() - srcRect.height()) <
978 COLOR_BLEED_TOLERANCE) {
979 return true;
980 }
981 return false;
982}
983
984static bool may_color_bleed(const SkRect& srcRect,
985 const SkRect& transformedRect,
986 const SkMatrix& m) {
987 // Only gets called if has_aligned_samples returned false.
988 // So we can assume that sampling is axis aligned but not texel aligned.
989 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
990 SkRect innerSrcRect(srcRect), innerTransformedRect,
991 outerTransformedRect(transformedRect);
992 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
993 m.mapRect(&innerTransformedRect, innerSrcRect);
994
995 // The gap between outerTransformedRect and innerTransformedRect
996 // represents the projection of the source border area, which is
997 // problematic for color bleeding. We must check whether any
998 // destination pixels sample the border area.
999 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1000 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1001 SkIRect outer, inner;
1002 outerTransformedRect.round(&outer);
1003 innerTransformedRect.round(&inner);
1004 // If the inner and outer rects round to the same result, it means the
1005 // border does not overlap any pixel centers. Yay!
1006 return inner != outer;
1007}
1008
1009static bool needs_texture_domain(const SkBitmap& bitmap,
1010 const SkRect& srcRect,
1011 GrTextureParams &params,
1012 const SkMatrix& contextMatrix,
1013 bool bicubic) {
1014 bool needsTextureDomain = false;
1015
1016 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1017 // Need texture domain if drawing a sub rect
1018 needsTextureDomain = srcRect.width() < bitmap.width() ||
1019 srcRect.height() < bitmap.height();
1020 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1021 // sampling is axis-aligned
1022 SkRect transformedRect;
1023 contextMatrix.mapRect(&transformedRect, srcRect);
1024
1025 if (has_aligned_samples(srcRect, transformedRect)) {
1026 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1027 needsTextureDomain = false;
1028 } else {
1029 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1030 }
1031 }
1032 }
1033 return needsTextureDomain;
1034}
1035
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001036void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1037 const SkBitmap& bitmap,
1038 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001039 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001040 const SkPaint& paint,
1041 SkCanvas::DrawBitmapRectFlags flags) {
1042 CHECK_SHOULD_DRAW(draw, false);
1043
1044 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001045 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001046 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1047 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001048 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001049 SkScalar w = SkIntToScalar(bitmap.width());
1050 SkScalar h = SkIntToScalar(bitmap.height());
1051 dstSize.fWidth = w;
1052 dstSize.fHeight = h;
1053 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001054 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001055 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001056 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001057 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001058 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001059 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1060 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1061 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1062 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001063 }
1064
derekf367e1862014-12-02 11:02:06 -08001065 // If the render target is not msaa and draw is antialiased, we call
1066 // drawRect instead of drawing on the render target directly.
1067 // FIXME: the tiled bitmap code path doesn't currently support
1068 // anti-aliased edges, we work around that for now by drawing directly
1069 // if the image size exceeds maximum texture size.
1070 int maxTextureSize = fContext->getMaxTextureSize();
1071 bool directDraw = fRenderTarget->isMultisampled() ||
1072 !paint.isAntiAlias() ||
1073 bitmap.width() > maxTextureSize ||
1074 bitmap.height() > maxTextureSize;
1075
1076 // we check whether dst rect are pixel aligned
1077 if (!directDraw) {
1078 bool staysRect = fContext->getMatrix().rectStaysRect();
1079
1080 if (staysRect) {
1081 SkRect rect;
1082 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
1083 fContext->getMatrix().mapRect(&rect, dstRect);
1084 const SkScalar *scalars = rect.asScalars();
1085 bool isDstPixelAligned = true;
1086 for (int i = 0; i < 4; i++) {
1087 if (!SkScalarIsInt(scalars[i])) {
1088 isDstPixelAligned = false;
1089 break;
1090 }
1091 }
1092
1093 if (isDstPixelAligned)
1094 directDraw = true;
1095 }
1096 }
1097
1098 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001099 // Convert the bitmap to a shader so that the rect can be drawn
1100 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001101 SkBitmap tmp; // subset of bitmap, if necessary
1102 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001103 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001104 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001105 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1106 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1107 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001108 // In bleed mode we position and trim the bitmap based on the src rect which is
1109 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1110 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1111 // compensate.
1112 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1113 SkIRect iSrc;
1114 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001115
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001116 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1117 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001118
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001119 if (!bitmap.extractSubset(&tmp, iSrc)) {
1120 return; // extraction failed
1121 }
1122 bitmapPtr = &tmp;
1123 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001124
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001125 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001126 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001127 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001128 } else {
1129 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001130 }
1131
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001132 SkPaint paintWithShader(paint);
1133 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001134 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001135 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1136 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001137
1138 return;
1139 }
1140
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001141 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1142 // the view matrix rather than a local matrix.
1143 SkMatrix m;
1144 m.setScale(dstSize.fWidth / srcRect.width(),
1145 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001146 fContext->concatMatrix(m);
1147
1148 GrTextureParams params;
1149 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1150 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001151
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001152 bool doBicubic = false;
1153
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001154 switch(paintFilterLevel) {
1155 case SkPaint::kNone_FilterLevel:
1156 textureFilterMode = GrTextureParams::kNone_FilterMode;
1157 break;
1158 case SkPaint::kLow_FilterLevel:
1159 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1160 break;
1161 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001162 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001163 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1164 } else {
1165 // Don't trigger MIP level generation unnecessarily.
1166 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1167 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001168 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001169 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001170 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001171 doBicubic =
1172 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001173 break;
1174 default:
1175 SkErrorInternals::SetError( kInvalidPaint_SkError,
1176 "Sorry, I don't understand the filtering "
1177 "mode you asked for. Falling back to "
1178 "MIPMaps.");
1179 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1180 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001181 }
1182
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001183 int tileFilterPad;
1184 if (doBicubic) {
1185 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1186 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1187 tileFilterPad = 0;
1188 } else {
1189 tileFilterPad = 1;
1190 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001191 params.setFilterMode(textureFilterMode);
1192
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001193 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001194 int tileSize;
1195
1196 SkIRect clippedSrcRect;
1197 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1198 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001199 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1200 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001201 } else {
1202 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001203 bool needsTextureDomain = needs_texture_domain(bitmap,
1204 srcRect,
1205 params,
1206 fContext->getMatrix(),
1207 doBicubic);
1208 this->internalDrawBitmap(bitmap,
1209 srcRect,
1210 params,
1211 paint,
1212 flags,
1213 doBicubic,
1214 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001215 }
1216}
1217
1218// Break 'bitmap' into several tiles to draw it since it has already
1219// been determined to be too large to fit in VRAM
1220void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1221 const SkRect& srcRect,
1222 const SkIRect& clippedSrcIRect,
1223 const GrTextureParams& params,
1224 const SkPaint& paint,
1225 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001226 int tileSize,
1227 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001228 // The following pixel lock is technically redundant, but it is desirable
1229 // to lock outside of the tile loop to prevent redecoding the whole image
1230 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1231 // is larger than the limit of the discardable memory pool.
1232 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001233 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1234
1235 int nx = bitmap.width() / tileSize;
1236 int ny = bitmap.height() / tileSize;
1237 for (int x = 0; x <= nx; x++) {
1238 for (int y = 0; y <= ny; y++) {
1239 SkRect tileR;
1240 tileR.set(SkIntToScalar(x * tileSize),
1241 SkIntToScalar(y * tileSize),
1242 SkIntToScalar((x + 1) * tileSize),
1243 SkIntToScalar((y + 1) * tileSize));
1244
1245 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1246 continue;
1247 }
1248
1249 if (!tileR.intersect(srcRect)) {
1250 continue;
1251 }
1252
1253 SkBitmap tmpB;
1254 SkIRect iTileR;
1255 tileR.roundOut(&iTileR);
1256 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1257 SkIntToScalar(iTileR.fTop));
1258
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001259 // Adjust the context matrix to draw at the right x,y in device space
1260 SkMatrix tmpM;
1261 GrContext::AutoMatrix am;
1262 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1263 am.setPreConcat(fContext, tmpM);
1264
robertphillipsec8bb942014-11-21 10:16:25 -08001265 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001266 SkIRect iClampRect;
1267
1268 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1269 // In bleed mode we want to always expand the tile on all edges
1270 // but stay within the bitmap bounds
1271 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1272 } else {
1273 // In texture-domain/clamp mode we only want to expand the
1274 // tile on edges interior to "srcRect" (i.e., we want to
1275 // not bleed across the original clamped edges)
1276 srcRect.roundOut(&iClampRect);
1277 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001278 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1279 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001280 }
1281
1282 if (bitmap.extractSubset(&tmpB, iTileR)) {
1283 // now offset it to make it "local" to our tmp bitmap
1284 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001285 GrTextureParams paramsTemp = params;
1286 bool needsTextureDomain = needs_texture_domain(bitmap,
1287 srcRect,
1288 paramsTemp,
1289 fContext->getMatrix(),
1290 bicubic);
1291 this->internalDrawBitmap(tmpB,
1292 tileR,
1293 paramsTemp,
1294 paint,
1295 flags,
1296 bicubic,
1297 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001298 }
1299 }
1300 }
1301}
1302
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001303
1304/*
1305 * This is called by drawBitmap(), which has to handle images that may be too
1306 * large to be represented by a single texture.
1307 *
1308 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1309 * and that non-texture portion of the GrPaint has already been setup.
1310 */
1311void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1312 const SkRect& srcRect,
1313 const GrTextureParams& params,
1314 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001315 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001316 bool bicubic,
1317 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001318 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1319 bitmap.height() <= fContext->getMaxTextureSize());
1320
1321 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001322 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001323 if (NULL == texture) {
1324 return;
1325 }
1326
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001327 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001328 SkRect paintRect;
1329 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1330 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1331 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1332 SkScalarMul(srcRect.fTop, hInv),
1333 SkScalarMul(srcRect.fRight, wInv),
1334 SkScalarMul(srcRect.fBottom, hInv));
1335
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001336 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001337 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001338 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001339 // Use a constrained texture domain to avoid color bleeding
1340 SkScalar left, top, right, bottom;
1341 if (srcRect.width() > SK_Scalar1) {
1342 SkScalar border = SK_ScalarHalf / texture->width();
1343 left = paintRect.left() + border;
1344 right = paintRect.right() - border;
1345 } else {
1346 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1347 }
1348 if (srcRect.height() > SK_Scalar1) {
1349 SkScalar border = SK_ScalarHalf / texture->height();
1350 top = paintRect.top() + border;
1351 bottom = paintRect.bottom() - border;
1352 } else {
1353 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1354 }
1355 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001356 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001357 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001358 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001359 fp.reset(GrTextureDomainEffect::Create(texture,
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001360 SkMatrix::I(),
1361 textureDomain,
1362 GrTextureDomain::kClamp_Mode,
1363 params.filterMode()));
1364 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001365 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001366 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1367 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001368 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001369 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001370 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001371 }
1372
1373 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1374 // the rest from the SkPaint.
1375 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001376 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001377 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001378 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1379 SkColor2GrColor(paint.getColor());
1380 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001381
bsalomon01c8da12014-08-04 09:21:30 -07001382 fContext->drawRectToRect(grPaint, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001383}
1384
fmalita2d97bc12014-11-20 10:44:58 -08001385bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
1386 const SkImageFilter* filter,
1387 const SkImageFilter::Context& ctx,
1388 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001389 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001390
1391 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1392 // (https://code.google.com/p/skia/issues/detail?id=3148).
1393 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001394
1395 if (filter->canFilterImageGPU()) {
1396 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1397 // filter. Also set the clip wide open and the matrix to identity.
1398 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001399 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001400 } else {
1401 return false;
1402 }
1403}
1404
1405void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1406 int left, int top, const SkPaint& paint) {
1407 // drawSprite is defined to be in device coords.
1408 CHECK_SHOULD_DRAW(draw, true);
1409
1410 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1411 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1412 return;
1413 }
1414
1415 int w = bitmap.width();
1416 int h = bitmap.height();
1417
1418 GrTexture* texture;
1419 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001420 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001421
1422 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001423 // This bitmap will own the filtered result as a texture.
1424 SkBitmap filteredBitmap;
1425
bsalomon49f085d2014-09-05 13:34:00 -07001426 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001427 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001428 SkMatrix matrix(*draw.fMatrix);
1429 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001430 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001431 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001432 // This cache is transient, and is freed (along with all its contained
1433 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001434 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001435 if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap,
1436 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001437 texture = (GrTexture*) filteredBitmap.getTexture();
1438 w = filteredBitmap.width();
1439 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001440 left += offset.x();
1441 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001442 } else {
1443 return;
1444 }
1445 }
1446
1447 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001448 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001449
dandov9de5b512014-06-10 14:38:28 -07001450 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1451 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001452
1453 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001454 SkRect::MakeXYWH(SkIntToScalar(left),
1455 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001456 SkIntToScalar(w),
1457 SkIntToScalar(h)),
1458 SkRect::MakeXYWH(0,
1459 0,
1460 SK_Scalar1 * w / texture->width(),
1461 SK_Scalar1 * h / texture->height()));
1462}
1463
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001464void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001465 const SkRect* src, const SkRect& dst,
1466 const SkPaint& paint,
1467 SkCanvas::DrawBitmapRectFlags flags) {
1468 SkMatrix matrix;
1469 SkRect bitmapBounds, tmpSrc;
1470
1471 bitmapBounds.set(0, 0,
1472 SkIntToScalar(bitmap.width()),
1473 SkIntToScalar(bitmap.height()));
1474
1475 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001476 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001477 tmpSrc = *src;
1478 } else {
1479 tmpSrc = bitmapBounds;
1480 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001481
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001482 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1483
1484 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001485 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001486 if (!bitmapBounds.contains(tmpSrc)) {
1487 if (!tmpSrc.intersect(bitmapBounds)) {
1488 return; // nothing to draw
1489 }
1490 }
1491 }
1492
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001493 SkRect tmpDst;
1494 matrix.mapRect(&tmpDst, tmpSrc);
1495
1496 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1497 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1498 // Translate so that tempDst's top left is at the origin.
1499 matrix = *origDraw.fMatrix;
1500 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1501 draw.writable()->fMatrix = &matrix;
1502 }
1503 SkSize dstSize;
1504 dstSize.fWidth = tmpDst.width();
1505 dstSize.fHeight = tmpDst.height();
1506
1507 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001508}
1509
1510void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1511 int x, int y, const SkPaint& paint) {
1512 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001513 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001514 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
jvanverth4736e142014-11-07 07:12:46 -08001515 if (dev->fFlags & kNeedClear_Flag) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001516 // TODO: could check here whether we really need to draw at all
reedaf641a12014-12-02 19:40:18 -08001517 dev->clear(0x0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001518 }
1519
1520 // drawDevice is defined to be in device coords.
1521 CHECK_SHOULD_DRAW(draw, true);
1522
1523 GrRenderTarget* devRT = dev->accessRenderTarget();
1524 GrTexture* devTex;
1525 if (NULL == (devTex = devRT->asTexture())) {
1526 return;
1527 }
1528
1529 const SkBitmap& bm = dev->accessBitmap(false);
1530 int w = bm.width();
1531 int h = bm.height();
1532
1533 SkImageFilter* filter = paint.getImageFilter();
1534 // This bitmap will own the filtered result as a texture.
1535 SkBitmap filteredBitmap;
1536
bsalomon49f085d2014-09-05 13:34:00 -07001537 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001538 SkIPoint offset = SkIPoint::Make(0, 0);
1539 SkMatrix matrix(*draw.fMatrix);
1540 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001541 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001542 // This cache is transient, and is freed (along with all its contained
1543 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001544 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001545 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001546 if (this->filterTexture(fContext, devTex, filter, ctx, &filteredBitmap,
1547 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001548 devTex = filteredBitmap.getTexture();
1549 w = filteredBitmap.width();
1550 h = filteredBitmap.height();
1551 x += offset.fX;
1552 y += offset.fY;
1553 } else {
1554 return;
1555 }
1556 }
1557
1558 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001559 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001560
dandov9de5b512014-06-10 14:38:28 -07001561 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1562 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001563
1564 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1565 SkIntToScalar(y),
1566 SkIntToScalar(w),
1567 SkIntToScalar(h));
1568
1569 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1570 // scratch texture).
1571 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1572 SK_Scalar1 * h / devTex->height());
1573
1574 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1575}
1576
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001577bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001578 return filter->canFilterImageGPU();
1579}
1580
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001581bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001582 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001583 SkBitmap* result, SkIPoint* offset) {
1584 // want explicitly our impl, so guard against a subclass of us overriding it
1585 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1586 return false;
1587 }
1588
1589 SkAutoLockPixels alp(src, !src.getTexture());
1590 if (!src.getTexture() && !src.readyToDraw()) {
1591 return false;
1592 }
1593
1594 GrTexture* texture;
1595 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1596 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001597 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001598
fmalita2d97bc12014-11-20 10:44:58 -08001599 return this->filterTexture(fContext, texture, filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001600}
1601
1602///////////////////////////////////////////////////////////////////////////////
1603
1604// must be in SkCanvas::VertexMode order
1605static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1606 kTriangles_GrPrimitiveType,
1607 kTriangleStrip_GrPrimitiveType,
1608 kTriangleFan_GrPrimitiveType,
1609};
1610
1611void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1612 int vertexCount, const SkPoint vertices[],
1613 const SkPoint texs[], const SkColor colors[],
1614 SkXfermode* xmode,
1615 const uint16_t indices[], int indexCount,
1616 const SkPaint& paint) {
1617 CHECK_SHOULD_DRAW(draw, false);
1618
egdanield78a1682014-07-09 10:41:26 -07001619 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001620
dandov32a311b2014-07-15 19:46:26 -07001621 const uint16_t* outIndices;
1622 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1623 GrPrimitiveType primType;
1624 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001625
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001626 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1627 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001628
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001629 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001630
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001631 SkPaint copy(paint);
1632 copy.setStyle(SkPaint::kStroke_Style);
1633 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001634
dandov32a311b2014-07-15 19:46:26 -07001635 // we ignore the shader if texs is null.
1636 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1637 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001638
dandov32a311b2014-07-15 19:46:26 -07001639 primType = kLines_GrPrimitiveType;
1640 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001641 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001642 switch (vmode) {
1643 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001644 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001645 break;
1646 case SkCanvas::kTriangleStrip_VertexMode:
1647 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001648 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001649 break;
1650 }
mtklein533eb782014-08-27 10:39:42 -07001651
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001652 VertState state(vertexCount, indices, indexCount);
1653 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001654
dandov32a311b2014-07-15 19:46:26 -07001655 //number of indices for lines per triangle with kLines
1656 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001657
dandov32a311b2014-07-15 19:46:26 -07001658 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1659 outIndices = outAlloc.get();
1660 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001661 int i = 0;
1662 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001663 auxIndices[i] = state.f0;
1664 auxIndices[i + 1] = state.f1;
1665 auxIndices[i + 2] = state.f1;
1666 auxIndices[i + 3] = state.f2;
1667 auxIndices[i + 4] = state.f2;
1668 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001669 i += 6;
1670 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001671 } else {
dandov32a311b2014-07-15 19:46:26 -07001672 outIndices = indices;
1673 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001674
dandov32a311b2014-07-15 19:46:26 -07001675 if (NULL == texs || NULL == paint.getShader()) {
1676 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1677 NULL == colors, &grPaint);
1678 } else {
1679 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
1680 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001681 }
1682
mtklein2583b622014-06-04 08:20:41 -07001683#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001684 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001685 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1686 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001687 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001688 }
1689 }
mtklein2583b622014-06-04 08:20:41 -07001690#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001691
1692 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001693 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001694 // need to convert byte order and from non-PM to PM
1695 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001696 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001697 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001698 color = colors[i];
1699 if (paint.getAlpha() != 255) {
1700 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1701 }
1702 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001703 }
1704 colors = convertedColors.get();
1705 }
1706 fContext->drawVertices(grPaint,
dandov32a311b2014-07-15 19:46:26 -07001707 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001708 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001709 vertices,
1710 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001711 colors,
dandov32a311b2014-07-15 19:46:26 -07001712 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001713 indexCount);
1714}
1715
1716///////////////////////////////////////////////////////////////////////////////
1717
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001718void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1719 size_t byteLength, SkScalar x, SkScalar y,
1720 const SkPaint& paint) {
1721 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -07001722 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001723
jvanverth8c27a182014-10-14 08:45:50 -07001724 GrPaint grPaint;
1725 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001726
jvanverth8c27a182014-10-14 08:45:50 -07001727 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001728
jvanverth8c27a182014-10-14 08:45:50 -07001729 if (!fTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y)) {
1730 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001731 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001732 }
1733}
1734
fmalita05c4a432014-09-29 06:29:53 -07001735void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1736 const SkScalar pos[], int scalarsPerPos,
1737 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001738 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001739 CHECK_SHOULD_DRAW(draw, false);
1740
jvanverth8c27a182014-10-14 08:45:50 -07001741 GrPaint grPaint;
1742 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001743
jvanverth8c27a182014-10-14 08:45:50 -07001744 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001745
jvanverth8c27a182014-10-14 08:45:50 -07001746 if (!fTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
1747 scalarsPerPos, offset)) {
1748 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001749 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001750 }
1751}
1752
1753void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1754 size_t len, const SkPath& path,
1755 const SkMatrix* m, const SkPaint& paint) {
1756 CHECK_SHOULD_DRAW(draw, false);
1757
1758 SkASSERT(draw.fDevice == this);
1759 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1760}
1761
1762///////////////////////////////////////////////////////////////////////////////
1763
reedb2db8982014-11-13 12:41:02 -08001764bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001765 if (paint.getShader() ||
reedb2db8982014-11-13 12:41:02 -08001766 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001767 paint.getMaskFilter() ||
1768 paint.getRasterizer() ||
1769 paint.getColorFilter() ||
1770 paint.getPathEffect() ||
1771 paint.isFakeBoldText() ||
reedb2db8982014-11-13 12:41:02 -08001772 paint.getStyle() != SkPaint::kFill_Style)
1773 {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001774 return true;
1775 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001776 return false;
1777}
1778
1779void SkGpuDevice::flush() {
1780 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001781 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001782}
1783
1784///////////////////////////////////////////////////////////////////////////////
1785
fmalita6987dca2014-11-13 08:33:37 -08001786SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(const CreateInfo& cinfo) {
bsalomonf2703d82014-10-28 14:33:06 -07001787 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001788 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001789 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001790 desc.fWidth = cinfo.fInfo.width();
1791 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001792 desc.fSampleCnt = fRenderTarget->numSamples();
1793
1794 SkAutoTUnref<GrTexture> texture;
1795 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001796 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
jvanverth4736e142014-11-07 07:12:46 -08001797 // If we're using distance field text, enable in the new device
1798 flags |= (fFlags & kDFText_Flag) ? kDFText_Flag : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001799
1800#if CACHE_COMPATIBLE_DEVICE_TEXTURES
hcm4396fa52014-10-27 21:43:30 -07001801 // layers are never draw in repeat modes, so we can request an approx
1802 // match and ignore any padding.
fmalita6987dca2014-11-13 08:33:37 -08001803 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == cinfo.fUsage) ?
hcm4396fa52014-10-27 21:43:30 -07001804 GrContext::kApprox_ScratchTexMatch :
1805 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001806 texture.reset(fContext->refScratchTexture(desc, match));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001807#else
1808 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1809#endif
bsalomon49f085d2014-09-05 13:34:00 -07001810 if (texture.get()) {
reedb2db8982014-11-13 12:41:02 -08001811 return SkGpuDevice::Create(texture, SkSurfaceProps(0, cinfo.fPixelGeometry), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001812 } else {
tfarina38406c82014-10-31 07:11:12 -07001813 SkDebugf("---- failed to create compatible device texture [%d %d]\n",
fmalita6987dca2014-11-13 08:33:37 -08001814 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001815 return NULL;
1816 }
1817}
1818
reed4a8126e2014-09-22 07:29:03 -07001819SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1820 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001821}
1822
robertphillips30d2cc62014-09-24 08:52:18 -07001823bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001824 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips30d78412014-11-24 09:49:17 -08001825 // todo: should handle this natively
1826 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001827 return false;
1828 }
1829
robertphillips82365912014-11-12 09:32:34 -08001830 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips81f71b62014-11-11 04:54:49 -08001831
1832 const SkPicture::AccelData* data = mainPicture->EXPERIMENTAL_getAccelData(key);
1833 if (!data) {
1834 return false;
1835 }
1836
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001837 SkRect clipBounds;
robertphillips0c423322014-07-31 11:02:38 -07001838 if (!mainCanvas->getClipBounds(&clipBounds)) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001839 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001840 }
1841
robertphillips30d78412014-11-24 09:49:17 -08001842 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1843
1844 const SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1845
robertphillipsfd61ed02014-10-28 07:21:44 -07001846 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001847
robertphillipsfd61ed02014-10-28 07:21:44 -07001848 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001849 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001850 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001851 &atlasedNeedRendering, &atlasedRecycled,
1852 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001853
1854 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1855
1856 SkTDArray<GrHoistedLayer> needRendering, recycled;
1857
1858 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001859 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001860 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001861 &needRendering, &recycled,
1862 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001863
1864 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001865
robertphillips64bf7672014-08-21 13:07:35 -07001866 // Render the entire picture using new layers
robertphillipse99d4992014-12-03 07:33:57 -08001867 GrRecordReplaceDraw(mainPicture, mainCanvas, fContext->getLayerCache(),
1868 initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001869
robertphillipsfd61ed02014-10-28 07:21:44 -07001870 GrLayerHoister::UnlockLayers(fContext, needRendering);
1871 GrLayerHoister::UnlockLayers(fContext, recycled);
1872 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1873 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001874
1875 return true;
1876}
1877
senorblancobe129b22014-08-08 07:14:35 -07001878SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001879 // We always return a transient cache, so it is freed after each
1880 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001881 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001882}
reedf037e0b2014-10-30 11:34:15 -07001883
1884#endif