blob: 3a0d6f4e3a3fcd9f5c006c5483b6cd4474308111 [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkGpuDevice.h"
9
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000010#include "effects/GrBicubicEffect.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000011#include "effects/GrDashingEffect.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000012#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000013#include "effects/GrSimpleTextureEffect.h"
14
15#include "GrContext.h"
16#include "GrBitmapTextContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000017#include "GrDistanceFieldTextContext.h"
robertphillips98d709b2014-09-02 10:20:50 -070018#include "GrLayerHoister.h"
robertphillips274b4ba2014-09-04 07:24:18 -070019#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070020#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070021#include "GrTracing.h"
derekff4555aa2014-10-06 12:19:12 -070022#include "GrGpu.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000023
24#include "SkGrTexturePixelRef.h"
25
robertphillips30d78412014-11-24 09:49:17 -080026#include "SkCanvasPriv.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000027#include "SkDeviceImageFilterProxy.h"
28#include "SkDrawProcs.h"
29#include "SkGlyphCache.h"
30#include "SkImageFilter.h"
robertphillips82365912014-11-12 09:32:34 -080031#include "SkLayerInfo.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000032#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000034#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070035#include "SkPictureData.h"
robertphillips274b4ba2014-09-04 07:24:18 -070036#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkRRect.h"
38#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000039#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000040#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000041#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000042#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070043#include "SkXfermode.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000044#include "SkErrorInternals.h"
45
reedf037e0b2014-10-30 11:34:15 -070046#if SK_SUPPORT_GPU
47
senorblanco55b6d8b2014-07-30 11:26:46 -070048enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
49
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000050#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
51
52#if 0
53 extern bool (*gShouldDrawProc)();
54 #define CHECK_SHOULD_DRAW(draw, forceI) \
55 do { \
56 if (gShouldDrawProc && !gShouldDrawProc()) return; \
57 this->prepareDraw(draw, forceI); \
58 } while (0)
59#else
60 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
61#endif
62
63// This constant represents the screen alignment criterion in texels for
64// requiring texture domain clamping to prevent color bleeding when drawing
65// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000066#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000067
68#define DO_DEFERRED_CLEAR() \
69 do { \
jvanverth4736e142014-11-07 07:12:46 -080070 if (fFlags & kNeedClear_Flag) { \
reed37294692014-12-02 10:08:11 -080071 this->clearAll(); \
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000072 } \
73 } while (false) \
74
75///////////////////////////////////////////////////////////////////////////////
76
77#define CHECK_FOR_ANNOTATION(paint) \
78 do { if (paint.getAnnotation()) { return; } } while (0)
79
80///////////////////////////////////////////////////////////////////////////////
81
bsalomonbcf0a522014-10-08 08:40:09 -070082// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
83// just accesses the backing GrTexture. Otherwise, it creates a cached texture
84// representation and releases it in the destructor.
85class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040086public:
bsalomonbcf0a522014-10-08 08:40:09 -070087 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070088
bsalomonbcf0a522014-10-08 08:40:09 -070089 AutoBitmapTexture(GrContext* context,
90 const SkBitmap& bitmap,
91 const GrTextureParams* params,
92 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040093 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070094 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040095 }
96
bsalomonbcf0a522014-10-08 08:40:09 -070097 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040098 const SkBitmap& bitmap,
99 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -0700100 // Either get the texture directly from the bitmap, or else use the cache and
101 // remember to unref it.
102 if (GrTexture* bmpTexture = bitmap.getTexture()) {
103 fTexture.reset(NULL);
104 return bmpTexture;
105 } else {
106 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
107 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400108 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400109 }
110
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000111private:
bsalomonbcf0a522014-10-08 08:40:09 -0700112 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000113};
114
115///////////////////////////////////////////////////////////////////////////////
116
117struct GrSkDrawProcs : public SkDrawProcs {
118public:
119 GrContext* fContext;
120 GrTextContext* fTextContext;
121 GrFontScaler* fFontScaler; // cached in the skia glyphcache
122};
123
124///////////////////////////////////////////////////////////////////////////////
125
reed4a8126e2014-09-22 07:29:03 -0700126SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, const SkSurfaceProps& props, unsigned flags) {
bsalomon49f085d2014-09-05 13:34:00 -0700127 SkASSERT(surface);
bsalomon32d0b3b2014-08-29 07:50:23 -0700128 if (NULL == surface->asRenderTarget() || surface->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000129 return NULL;
130 }
reed4a8126e2014-09-22 07:29:03 -0700131 return SkNEW_ARGS(SkGpuDevice, (surface, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000132}
133
reedb2db8982014-11-13 12:41:02 -0800134static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps& props) {
135 return SkDeviceProperties(props.pixelGeometry());
136}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000137
reedb2db8982014-11-13 12:41:02 -0800138SkGpuDevice::SkGpuDevice(GrSurface* surface, const SkSurfaceProps& props, unsigned flags)
139 : INHERITED(surfaceprops_to_deviceprops(props))
140{
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000141 fDrawProcs = NULL;
142
bsalomon32d0b3b2014-08-29 07:50:23 -0700143 fContext = SkRef(surface->getContext());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000144
jvanverth4736e142014-11-07 07:12:46 -0800145 fFlags = flags;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000146
bsalomon32d0b3b2014-08-29 07:50:23 -0700147 fRenderTarget = SkRef(surface->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000148
bsalomonafbf2d62014-09-30 12:18:44 -0700149 SkImageInfo info = surface->surfacePriv().info();
bsalomonbcf0a522014-10-08 08:40:09 -0700150 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface));
bsalomonafbf2d62014-09-30 12:18:44 -0700151 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700152 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700153
jvanverth4736e142014-11-07 07:12:46 -0800154 bool useDFT = SkToBool(flags & kDFText_Flag);
155 fTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFT);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000156}
157
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000158SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
reed4a8126e2014-09-22 07:29:03 -0700159 const SkSurfaceProps& props, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000160 if (kUnknown_SkColorType == origInfo.colorType() ||
161 origInfo.width() < 0 || origInfo.height() < 0) {
162 return NULL;
163 }
164
reede5ea5002014-09-03 11:54:58 -0700165 SkColorType ct = origInfo.colorType();
166 SkAlphaType at = origInfo.alphaType();
robertphillipsa0537de2014-09-18 08:01:23 -0700167 // TODO: perhaps we can loosen this check now that colortype is more detailed
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000168 // e.g. can we support both RGBA and BGRA here?
reede5ea5002014-09-03 11:54:58 -0700169 if (kRGB_565_SkColorType == ct) {
170 at = kOpaque_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000171 } else {
reede5ea5002014-09-03 11:54:58 -0700172 ct = kN32_SkColorType;
173 if (kOpaque_SkAlphaType != at) {
174 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000175 }
176 }
reede5ea5002014-09-03 11:54:58 -0700177 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000178
bsalomonf2703d82014-10-28 14:33:06 -0700179 GrSurfaceDesc desc;
180 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000181 desc.fWidth = info.width();
182 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000183 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000184 desc.fSampleCnt = sampleCount;
185
186 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
187 if (!texture.get()) {
188 return NULL;
189 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000190
reed4a8126e2014-09-22 07:29:03 -0700191 return SkNEW_ARGS(SkGpuDevice, (texture.get(), props));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000192}
193
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000194SkGpuDevice::~SkGpuDevice() {
195 if (fDrawProcs) {
196 delete fDrawProcs;
197 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000198
jvanverth8c27a182014-10-14 08:45:50 -0700199 delete fTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000200
201 // The GrContext takes a ref on the target. We don't want to cause the render
202 // target to be unnecessarily kept alive.
203 if (fContext->getRenderTarget() == fRenderTarget) {
204 fContext->setRenderTarget(NULL);
205 }
206
207 if (fContext->getClip() == &fClipData) {
208 fContext->setClip(NULL);
209 }
210
bsalomon32d0b3b2014-08-29 07:50:23 -0700211 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000212 fContext->unref();
213}
214
215///////////////////////////////////////////////////////////////////////////////
216
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000217bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
218 int x, int y) {
219 DO_DEFERRED_CLEAR();
220
221 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000222 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000223 if (kUnknown_GrPixelConfig == config) {
224 return false;
225 }
226
227 uint32_t flags = 0;
228 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
229 flags = GrContext::kUnpremul_PixelOpsFlag;
230 }
231 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
232 config, dstPixels, dstRowBytes, flags);
233}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000234
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000235bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
236 int x, int y) {
237 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000238 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000239 if (kUnknown_GrPixelConfig == config) {
240 return false;
241 }
242 uint32_t flags = 0;
243 if (kUnpremul_SkAlphaType == info.alphaType()) {
244 flags = GrContext::kUnpremul_PixelOpsFlag;
245 }
246 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
247
248 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700249 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000250
251 return true;
252}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000253
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000254const SkBitmap& SkGpuDevice::onAccessBitmap() {
255 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700256 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000257}
258
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000259void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
260 INHERITED::onAttachToCanvas(canvas);
261
262 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
263 fClipData.fClipStack = canvas->getClipStack();
264}
265
266void SkGpuDevice::onDetachFromCanvas() {
267 INHERITED::onDetachFromCanvas();
268 fClipData.fClipStack = NULL;
269}
270
271// call this every draw call, to ensure that the context reflects our state,
272// and not the state from some other canvas/device
273void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
bsalomon49f085d2014-09-05 13:34:00 -0700274 SkASSERT(fClipData.fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000275
276 fContext->setRenderTarget(fRenderTarget);
277
278 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
279
280 if (forceIdentity) {
281 fContext->setIdentityMatrix();
282 } else {
283 fContext->setMatrix(*draw.fMatrix);
284 }
285 fClipData.fOrigin = this->getOrigin();
286
287 fContext->setClip(&fClipData);
288
289 DO_DEFERRED_CLEAR();
290}
291
292GrRenderTarget* SkGpuDevice::accessRenderTarget() {
293 DO_DEFERRED_CLEAR();
294 return fRenderTarget;
295}
296
reed37294692014-12-02 10:08:11 -0800297void SkGpuDevice::clearAll() {
298 GrColor color = 0;
299 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clearAll", fContext);
300 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
301 fContext->clear(&rect, color, true, fRenderTarget);
302 fFlags &= ~kNeedClear_Flag;
303}
304
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000305///////////////////////////////////////////////////////////////////////////////
306
307SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
308SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
309SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
310SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
311SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
312 shader_type_mismatch);
313SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
314 shader_type_mismatch);
315SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
316SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
317
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000318///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000319
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000320void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
321 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700322 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323
324 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000325 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000326
327 fContext->drawPaint(grPaint);
328}
329
330// must be in SkCanvas::PointMode order
331static const GrPrimitiveType gPointMode2PrimtiveType[] = {
332 kPoints_GrPrimitiveType,
333 kLines_GrPrimitiveType,
334 kLineStrip_GrPrimitiveType
335};
336
337void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
338 size_t count, const SkPoint pts[], const SkPaint& paint) {
339 CHECK_FOR_ANNOTATION(paint);
340 CHECK_SHOULD_DRAW(draw, false);
341
342 SkScalar width = paint.getStrokeWidth();
343 if (width < 0) {
344 return;
345 }
346
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000347 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700348 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
349 GrPaint grPaint;
350 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
351 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700352 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700353 path.moveTo(pts[0]);
354 path.lineTo(pts[1]);
355 fContext->drawPath(grPaint, path, strokeInfo);
356 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000357 }
358
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000359 // we only handle hairlines and paints without path effects or mask filters,
360 // else we let the SkDraw call our drawPath()
361 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
362 draw.drawPoints(mode, count, pts, paint, true);
363 return;
364 }
365
366 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000367 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000368
369 fContext->drawVertices(grPaint,
370 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000371 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000372 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000373 NULL,
374 NULL,
375 NULL,
376 0);
377}
378
379///////////////////////////////////////////////////////////////////////////////
380
381void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
382 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700383 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
384
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000385 CHECK_FOR_ANNOTATION(paint);
386 CHECK_SHOULD_DRAW(draw, false);
387
388 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
389 SkScalar width = paint.getStrokeWidth();
390
391 /*
392 We have special code for hairline strokes, miter-strokes, bevel-stroke
393 and fills. Anything else we just call our path code.
394 */
395 bool usePath = doStroke && width > 0 &&
396 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
397 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
398 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700399
400 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000401 usePath = true;
402 }
egdanield58a0ba2014-06-11 10:30:05 -0700403
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000404 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
405#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
406 if (doStroke) {
407#endif
408 usePath = true;
409#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
410 } else {
411 usePath = !fContext->getMatrix().preservesRightAngles();
412 }
413#endif
414 }
415 // until we can both stroke and fill rectangles
416 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
417 usePath = true;
418 }
419
egdanield58a0ba2014-06-11 10:30:05 -0700420 GrStrokeInfo strokeInfo(paint);
421
422 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700423 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700424 usePath = true;
425 }
426
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000427 if (usePath) {
428 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700429 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000430 path.addRect(rect);
431 this->drawPath(draw, path, paint, NULL, true);
432 return;
433 }
434
435 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000436 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400437
egdanield58a0ba2014-06-11 10:30:05 -0700438 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000439}
440
441///////////////////////////////////////////////////////////////////////////////
442
443void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
444 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700445 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000446 CHECK_FOR_ANNOTATION(paint);
447 CHECK_SHOULD_DRAW(draw, false);
448
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000449 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000450 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400451
egdanield58a0ba2014-06-11 10:30:05 -0700452 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000453 if (paint.getMaskFilter()) {
454 // try to hit the fast path for drawing filtered round rects
455
456 SkRRect devRRect;
457 if (rect.transform(fContext->getMatrix(), &devRRect)) {
458 if (devRRect.allCornersCircular()) {
459 SkRect maskRect;
460 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
461 draw.fClip->getBounds(),
462 fContext->getMatrix(),
463 &maskRect)) {
464 SkIRect finalIRect;
465 maskRect.roundOut(&finalIRect);
466 if (draw.fClip->quickReject(finalIRect)) {
467 // clipped out
468 return;
469 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000470 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700471 strokeInfo.getStrokeRec(),
472 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000473 return;
474 }
475 }
476
477 }
478 }
479
480 }
481
egdanield58a0ba2014-06-11 10:30:05 -0700482 bool usePath = false;
483
484 if (paint.getMaskFilter()) {
485 usePath = true;
486 } else {
487 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700488 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700489 usePath = true;
490 }
491 }
492
493
494 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000495 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700496 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000497 path.addRRect(rect);
498 this->drawPath(draw, path, paint, NULL, true);
499 return;
500 }
Mike Klein744fb732014-06-23 15:13:26 -0400501
egdanield58a0ba2014-06-11 10:30:05 -0700502 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000503}
504
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000505void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
506 const SkRRect& inner, const SkPaint& paint) {
507 SkStrokeRec stroke(paint);
508 if (stroke.isFillStyle()) {
509
510 CHECK_FOR_ANNOTATION(paint);
511 CHECK_SHOULD_DRAW(draw, false);
512
513 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000514 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000515
516 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
517 fContext->drawDRRect(grPaint, outer, inner);
518 return;
519 }
520 }
521
522 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700523 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000524 path.addRRect(outer);
525 path.addRRect(inner);
526 path.setFillType(SkPath::kEvenOdd_FillType);
527
528 this->drawPath(draw, path, paint, NULL, true);
529}
530
531
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000532/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000533
534void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
535 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700536 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000537 CHECK_FOR_ANNOTATION(paint);
538 CHECK_SHOULD_DRAW(draw, false);
539
egdanield58a0ba2014-06-11 10:30:05 -0700540 GrStrokeInfo strokeInfo(paint);
541
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000542 bool usePath = false;
543 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700544 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000545 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700546 } else {
547 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700548 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700549 usePath = true;
550 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000551 }
552
553 if (usePath) {
554 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700555 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000556 path.addOval(oval);
557 this->drawPath(draw, path, paint, NULL, true);
558 return;
559 }
560
561 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000562 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000563
egdanield58a0ba2014-06-11 10:30:05 -0700564 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000565}
566
567#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000568
569///////////////////////////////////////////////////////////////////////////////
570
571// helpers for applying mask filters
572namespace {
573
574// Draw a mask using the supplied paint. Since the coverage/geometry
575// is already burnt into the mask this boils down to a rect draw.
576// Return true if the mask was successfully drawn.
577bool draw_mask(GrContext* context, const SkRect& maskRect,
578 GrPaint* grp, GrTexture* mask) {
579 GrContext::AutoMatrix am;
580 if (!am.setIdentity(context, grp)) {
581 return false;
582 }
583
584 SkMatrix matrix;
585 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
586 matrix.postIDiv(mask->width(), mask->height());
587
joshualittb0a8a372014-09-23 09:50:21 -0700588 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000589 context->drawRect(*grp, maskRect);
590 return true;
591}
592
593bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700594 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000595 GrPaint* grp, SkPaint::Style style) {
596 SkMask srcM, dstM;
597
598 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
599 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
600 return false;
601 }
602 SkAutoMaskFreeImage autoSrc(srcM.fImage);
603
604 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
605 return false;
606 }
607 // this will free-up dstM when we're done (allocated in filterMask())
608 SkAutoMaskFreeImage autoDst(dstM.fImage);
609
610 if (clip.quickReject(dstM.fBounds)) {
611 return false;
612 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000613
614 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
615 // the current clip (and identity matrix) and GrPaint settings
bsalomonf2703d82014-10-28 14:33:06 -0700616 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000617 desc.fWidth = dstM.fBounds.width();
618 desc.fHeight = dstM.fBounds.height();
619 desc.fConfig = kAlpha_8_GrPixelConfig;
620
bsalomone3059732014-10-14 11:47:22 -0700621 SkAutoTUnref<GrTexture> texture(
622 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
623 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000624 return false;
625 }
626 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
627 dstM.fImage, dstM.fRowBytes);
628
629 SkRect maskRect = SkRect::Make(dstM.fBounds);
630
631 return draw_mask(context, maskRect, grp, texture);
632}
633
bsalomone3059732014-10-14 11:47:22 -0700634// Create a mask of 'devPath' and place the result in 'mask'.
635GrTexture* create_mask_GPU(GrContext* context,
636 const SkRect& maskRect,
637 const SkPath& devPath,
638 const GrStrokeInfo& strokeInfo,
639 bool doAA,
640 int sampleCnt) {
bsalomonf2703d82014-10-28 14:33:06 -0700641 GrSurfaceDesc desc;
642 desc.fFlags = kRenderTarget_GrSurfaceFlag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000643 desc.fWidth = SkScalarCeilToInt(maskRect.width());
644 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700645 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000646 // We actually only need A8, but it often isn't supported as a
647 // render target so default to RGBA_8888
648 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700649
650 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
651 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000652 desc.fConfig = kAlpha_8_GrPixelConfig;
653 }
654
bsalomone3059732014-10-14 11:47:22 -0700655 GrTexture* mask = context->refScratchTexture(desc,GrContext::kApprox_ScratchTexMatch);
656 if (NULL == mask) {
657 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000658 }
659
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000660 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
661
bsalomone3059732014-10-14 11:47:22 -0700662 GrContext::AutoRenderTarget art(context, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000663 GrContext::AutoClip ac(context, clipRect);
664
bsalomon89c62982014-11-03 12:08:42 -0800665 context->clear(NULL, 0x0, true, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000666
667 GrPaint tempPaint;
668 if (doAA) {
669 tempPaint.setAntiAlias(true);
670 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
671 // blend coeff of zero requires dual source blending support in order
672 // to properly blend partially covered pixels. This means the AA
673 // code path may not be taken. So we use a dst blend coeff of ISA. We
674 // could special case AA draws to a dst surface with known alpha=0 to
675 // use a zero dst coeff when dual source blending isn't available.
676 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
677 }
678
679 GrContext::AutoMatrix am;
680
681 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
682 SkMatrix translate;
683 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
684 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700685 context->drawPath(tempPaint, devPath, strokeInfo);
bsalomone3059732014-10-14 11:47:22 -0700686 return mask;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000687}
688
689SkBitmap wrap_texture(GrTexture* texture) {
690 SkBitmap result;
bsalomonafbf2d62014-09-30 12:18:44 -0700691 result.setInfo(texture->surfacePriv().info());
reed6c225732014-06-09 19:52:07 -0700692 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000693 return result;
694}
695
696};
697
698void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
699 const SkPaint& paint, const SkMatrix* prePathMatrix,
700 bool pathIsMutable) {
701 CHECK_FOR_ANNOTATION(paint);
702 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700703 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000704
jvanverthb3eb6872014-10-24 07:12:51 -0700705 SkASSERT(!pathIsMutable || origSrcPath.isVolatile());
706
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000707 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000708 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000709
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000710 // If we have a prematrix, apply it to the path, optimizing for the case
711 // where the original path can in fact be modified in place (even though
712 // its parameter type is const).
713 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000714 SkTLazy<SkPath> tmpPath;
715 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000716
717 if (prePathMatrix) {
718 SkPath* result = pathPtr;
719
720 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000721 result = tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700722 result->setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000723 pathIsMutable = true;
724 }
725 // should I push prePathMatrix on our MV stack temporarily, instead
726 // of applying it here? See SkDraw.cpp
727 pathPtr->transform(*prePathMatrix, result);
728 pathPtr = result;
729 }
730 // at this point we're done with prePathMatrix
731 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
732
egdanield58a0ba2014-06-11 10:30:05 -0700733 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000734 SkPathEffect* pathEffect = paint.getPathEffect();
735 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700736 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
737 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000738 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000739 pathPtr = effectPath.get();
740 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700741 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742 }
743
egdanield58a0ba2014-06-11 10:30:05 -0700744 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000745 if (paint.getMaskFilter()) {
746 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000747 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
748 if (stroke.applyToPath(strokedPath, *pathPtr)) {
749 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000750 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700751 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000752 }
753 }
754
755 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000756 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700757 if (!pathIsMutable) {
758 devPathPtr->setIsVolatile(true);
759 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000760
761 // transform the path into device space
762 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000763
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764 SkRect maskRect;
765 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
766 draw.fClip->getBounds(),
767 fContext->getMatrix(),
768 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000769 // The context's matrix may change while creating the mask, so save the CTM here to
770 // pass to filterMaskGPU.
771 const SkMatrix ctm = fContext->getMatrix();
772
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773 SkIRect finalIRect;
774 maskRect.roundOut(&finalIRect);
775 if (draw.fClip->quickReject(finalIRect)) {
776 // clipped out
777 return;
778 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000779
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000780 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000781 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000782 // the mask filter was able to draw itself directly, so there's nothing
783 // left to do.
784 return;
785 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000787
bsalomone3059732014-10-14 11:47:22 -0700788 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext, maskRect, *devPathPtr,
789 strokeInfo, grPaint.isAntiAlias(),
790 fRenderTarget->numSamples()));
791 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000792 GrTexture* filtered;
793
bsalomone3059732014-10-14 11:47:22 -0700794 if (paint.getMaskFilter()->filterMaskGPU(mask, ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000795 // filterMaskGPU gives us ownership of a ref to the result
796 SkAutoTUnref<GrTexture> atu(filtered);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000797 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
798 // This path is completely drawn
799 return;
800 }
801 }
802 }
803 }
804
805 // draw the mask on the CPU - this is a fallthrough path in case the
806 // GPU path fails
807 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
808 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700809 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
810 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000811 return;
812 }
813
egdanield58a0ba2014-06-11 10:30:05 -0700814 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000815}
816
817static const int kBmpSmallTileSize = 1 << 10;
818
819static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
820 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
821 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
822 return tilesX * tilesY;
823}
824
825static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
826 if (maxTileSize <= kBmpSmallTileSize) {
827 return maxTileSize;
828 }
829
830 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
831 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
832
833 maxTileTotalTileSize *= maxTileSize * maxTileSize;
834 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
835
836 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
837 return kBmpSmallTileSize;
838 } else {
839 return maxTileSize;
840 }
841}
842
843// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
844// pixels from the bitmap are necessary.
845static void determine_clipped_src_rect(const GrContext* context,
846 const SkBitmap& bitmap,
847 const SkRect* srcRectPtr,
848 SkIRect* clippedSrcIRect) {
849 const GrClipData* clip = context->getClip();
850 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
851 SkMatrix inv;
852 if (!context->getMatrix().invert(&inv)) {
853 clippedSrcIRect->setEmpty();
854 return;
855 }
856 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
857 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700858 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000859 // we've setup src space 0,0 to map to the top left of the src rect.
860 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000861 if (!clippedSrcRect.intersect(*srcRectPtr)) {
862 clippedSrcIRect->setEmpty();
863 return;
864 }
865 }
866 clippedSrcRect.roundOut(clippedSrcIRect);
867 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
868 if (!clippedSrcIRect->intersect(bmpBounds)) {
869 clippedSrcIRect->setEmpty();
870 }
871}
872
873bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
874 const GrTextureParams& params,
875 const SkRect* srcRectPtr,
876 int maxTileSize,
877 int* tileSize,
878 SkIRect* clippedSrcRect) const {
879 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700880 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000881 return false;
882 }
883
884 // if it's larger than the max tile size, then we have no choice but tiling.
885 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
886 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
887 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
888 return true;
889 }
890
891 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
892 return false;
893 }
894
895 // if the entire texture is already in our cache then no reason to tile it
896 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
897 return false;
898 }
899
900 // At this point we know we could do the draw by uploading the entire bitmap
901 // as a texture. However, if the texture would be large compared to the
902 // cache size and we don't require most of it for this draw then tile to
903 // reduce the amount of upload and cache spill.
904
905 // assumption here is that sw bitmap size is a good proxy for its size as
906 // a texture
907 size_t bmpSize = bitmap.getSize();
908 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000909 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000910 if (bmpSize < cacheSize / 2) {
911 return false;
912 }
913
914 // Figure out how much of the src we will need based on the src rect and clipping.
915 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
916 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
917 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
918 kBmpSmallTileSize * kBmpSmallTileSize;
919
920 return usedTileBytes < 2 * bmpSize;
921}
922
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000923void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000924 const SkBitmap& bitmap,
925 const SkMatrix& m,
926 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000927 SkMatrix concat;
928 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
929 if (!m.isIdentity()) {
930 concat.setConcat(*draw->fMatrix, m);
931 draw.writable()->fMatrix = &concat;
932 }
933 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000934}
935
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000936// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000937// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
938// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000939static inline void clamped_outset_with_offset(SkIRect* iRect,
940 int outset,
941 SkPoint* offset,
942 const SkIRect& clamp) {
943 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000944
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000945 int leftClampDelta = clamp.fLeft - iRect->fLeft;
946 if (leftClampDelta > 0) {
947 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000948 iRect->fLeft = clamp.fLeft;
949 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000950 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000951 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000952
953 int topClampDelta = clamp.fTop - iRect->fTop;
954 if (topClampDelta > 0) {
955 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000956 iRect->fTop = clamp.fTop;
957 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000958 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000959 }
960
961 if (iRect->fRight > clamp.fRight) {
962 iRect->fRight = clamp.fRight;
963 }
964 if (iRect->fBottom > clamp.fBottom) {
965 iRect->fBottom = clamp.fBottom;
966 }
967}
968
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000969static bool has_aligned_samples(const SkRect& srcRect,
970 const SkRect& transformedRect) {
971 // detect pixel disalignment
972 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
973 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
974 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
975 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
976 SkScalarAbs(transformedRect.width() - srcRect.width()) <
977 COLOR_BLEED_TOLERANCE &&
978 SkScalarAbs(transformedRect.height() - srcRect.height()) <
979 COLOR_BLEED_TOLERANCE) {
980 return true;
981 }
982 return false;
983}
984
985static bool may_color_bleed(const SkRect& srcRect,
986 const SkRect& transformedRect,
987 const SkMatrix& m) {
988 // Only gets called if has_aligned_samples returned false.
989 // So we can assume that sampling is axis aligned but not texel aligned.
990 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
991 SkRect innerSrcRect(srcRect), innerTransformedRect,
992 outerTransformedRect(transformedRect);
993 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
994 m.mapRect(&innerTransformedRect, innerSrcRect);
995
996 // The gap between outerTransformedRect and innerTransformedRect
997 // represents the projection of the source border area, which is
998 // problematic for color bleeding. We must check whether any
999 // destination pixels sample the border area.
1000 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1001 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1002 SkIRect outer, inner;
1003 outerTransformedRect.round(&outer);
1004 innerTransformedRect.round(&inner);
1005 // If the inner and outer rects round to the same result, it means the
1006 // border does not overlap any pixel centers. Yay!
1007 return inner != outer;
1008}
1009
1010static bool needs_texture_domain(const SkBitmap& bitmap,
1011 const SkRect& srcRect,
1012 GrTextureParams &params,
1013 const SkMatrix& contextMatrix,
1014 bool bicubic) {
1015 bool needsTextureDomain = false;
1016
1017 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1018 // Need texture domain if drawing a sub rect
1019 needsTextureDomain = srcRect.width() < bitmap.width() ||
1020 srcRect.height() < bitmap.height();
1021 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1022 // sampling is axis-aligned
1023 SkRect transformedRect;
1024 contextMatrix.mapRect(&transformedRect, srcRect);
1025
1026 if (has_aligned_samples(srcRect, transformedRect)) {
1027 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1028 needsTextureDomain = false;
1029 } else {
1030 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1031 }
1032 }
1033 }
1034 return needsTextureDomain;
1035}
1036
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001037void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1038 const SkBitmap& bitmap,
1039 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001040 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001041 const SkPaint& paint,
1042 SkCanvas::DrawBitmapRectFlags flags) {
1043 CHECK_SHOULD_DRAW(draw, false);
1044
1045 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001046 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001047 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1048 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001049 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001050 SkScalar w = SkIntToScalar(bitmap.width());
1051 SkScalar h = SkIntToScalar(bitmap.height());
1052 dstSize.fWidth = w;
1053 dstSize.fHeight = h;
1054 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001055 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001056 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001057 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001058 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001059 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001060 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1061 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1062 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1063 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001064 }
1065
derekf367e1862014-12-02 11:02:06 -08001066 // If the render target is not msaa and draw is antialiased, we call
1067 // drawRect instead of drawing on the render target directly.
1068 // FIXME: the tiled bitmap code path doesn't currently support
1069 // anti-aliased edges, we work around that for now by drawing directly
1070 // if the image size exceeds maximum texture size.
1071 int maxTextureSize = fContext->getMaxTextureSize();
1072 bool directDraw = fRenderTarget->isMultisampled() ||
1073 !paint.isAntiAlias() ||
1074 bitmap.width() > maxTextureSize ||
1075 bitmap.height() > maxTextureSize;
1076
1077 // we check whether dst rect are pixel aligned
1078 if (!directDraw) {
1079 bool staysRect = fContext->getMatrix().rectStaysRect();
1080
1081 if (staysRect) {
1082 SkRect rect;
1083 SkRect dstRect = SkRect::MakeXYWH(0, 0, dstSize.fWidth, dstSize.fHeight);
1084 fContext->getMatrix().mapRect(&rect, dstRect);
1085 const SkScalar *scalars = rect.asScalars();
1086 bool isDstPixelAligned = true;
1087 for (int i = 0; i < 4; i++) {
1088 if (!SkScalarIsInt(scalars[i])) {
1089 isDstPixelAligned = false;
1090 break;
1091 }
1092 }
1093
1094 if (isDstPixelAligned)
1095 directDraw = true;
1096 }
1097 }
1098
1099 if (paint.getMaskFilter() || !directDraw) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001100 // Convert the bitmap to a shader so that the rect can be drawn
1101 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001102 SkBitmap tmp; // subset of bitmap, if necessary
1103 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001104 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001105 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001106 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1107 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1108 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001109 // In bleed mode we position and trim the bitmap based on the src rect which is
1110 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1111 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1112 // compensate.
1113 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1114 SkIRect iSrc;
1115 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001116
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001117 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1118 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001119
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001120 if (!bitmap.extractSubset(&tmp, iSrc)) {
1121 return; // extraction failed
1122 }
1123 bitmapPtr = &tmp;
1124 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001125
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001126 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001127 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001128 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001129 } else {
1130 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001131 }
1132
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001133 SkPaint paintWithShader(paint);
1134 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001135 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001136 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1137 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001138
1139 return;
1140 }
1141
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001142 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1143 // the view matrix rather than a local matrix.
1144 SkMatrix m;
1145 m.setScale(dstSize.fWidth / srcRect.width(),
1146 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001147 fContext->concatMatrix(m);
1148
1149 GrTextureParams params;
1150 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1151 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001152
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001153 bool doBicubic = false;
1154
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001155 switch(paintFilterLevel) {
1156 case SkPaint::kNone_FilterLevel:
1157 textureFilterMode = GrTextureParams::kNone_FilterMode;
1158 break;
1159 case SkPaint::kLow_FilterLevel:
1160 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1161 break;
1162 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001163 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001164 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1165 } else {
1166 // Don't trigger MIP level generation unnecessarily.
1167 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1168 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001169 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001170 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001171 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001172 doBicubic =
1173 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001174 break;
1175 default:
1176 SkErrorInternals::SetError( kInvalidPaint_SkError,
1177 "Sorry, I don't understand the filtering "
1178 "mode you asked for. Falling back to "
1179 "MIPMaps.");
1180 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1181 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001182 }
1183
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001184 int tileFilterPad;
1185 if (doBicubic) {
1186 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1187 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1188 tileFilterPad = 0;
1189 } else {
1190 tileFilterPad = 1;
1191 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001192 params.setFilterMode(textureFilterMode);
1193
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001194 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195 int tileSize;
1196
1197 SkIRect clippedSrcRect;
1198 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1199 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001200 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1201 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001202 } else {
1203 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001204 bool needsTextureDomain = needs_texture_domain(bitmap,
1205 srcRect,
1206 params,
1207 fContext->getMatrix(),
1208 doBicubic);
1209 this->internalDrawBitmap(bitmap,
1210 srcRect,
1211 params,
1212 paint,
1213 flags,
1214 doBicubic,
1215 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001216 }
1217}
1218
1219// Break 'bitmap' into several tiles to draw it since it has already
1220// been determined to be too large to fit in VRAM
1221void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1222 const SkRect& srcRect,
1223 const SkIRect& clippedSrcIRect,
1224 const GrTextureParams& params,
1225 const SkPaint& paint,
1226 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001227 int tileSize,
1228 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001229 // The following pixel lock is technically redundant, but it is desirable
1230 // to lock outside of the tile loop to prevent redecoding the whole image
1231 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1232 // is larger than the limit of the discardable memory pool.
1233 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001234 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1235
1236 int nx = bitmap.width() / tileSize;
1237 int ny = bitmap.height() / tileSize;
1238 for (int x = 0; x <= nx; x++) {
1239 for (int y = 0; y <= ny; y++) {
1240 SkRect tileR;
1241 tileR.set(SkIntToScalar(x * tileSize),
1242 SkIntToScalar(y * tileSize),
1243 SkIntToScalar((x + 1) * tileSize),
1244 SkIntToScalar((y + 1) * tileSize));
1245
1246 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1247 continue;
1248 }
1249
1250 if (!tileR.intersect(srcRect)) {
1251 continue;
1252 }
1253
1254 SkBitmap tmpB;
1255 SkIRect iTileR;
1256 tileR.roundOut(&iTileR);
1257 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1258 SkIntToScalar(iTileR.fTop));
1259
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001260 // Adjust the context matrix to draw at the right x,y in device space
1261 SkMatrix tmpM;
1262 GrContext::AutoMatrix am;
1263 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1264 am.setPreConcat(fContext, tmpM);
1265
robertphillipsec8bb942014-11-21 10:16:25 -08001266 if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001267 SkIRect iClampRect;
1268
1269 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1270 // In bleed mode we want to always expand the tile on all edges
1271 // but stay within the bitmap bounds
1272 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1273 } else {
1274 // In texture-domain/clamp mode we only want to expand the
1275 // tile on edges interior to "srcRect" (i.e., we want to
1276 // not bleed across the original clamped edges)
1277 srcRect.roundOut(&iClampRect);
1278 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001279 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1280 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001281 }
1282
1283 if (bitmap.extractSubset(&tmpB, iTileR)) {
1284 // now offset it to make it "local" to our tmp bitmap
1285 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001286 GrTextureParams paramsTemp = params;
1287 bool needsTextureDomain = needs_texture_domain(bitmap,
1288 srcRect,
1289 paramsTemp,
1290 fContext->getMatrix(),
1291 bicubic);
1292 this->internalDrawBitmap(tmpB,
1293 tileR,
1294 paramsTemp,
1295 paint,
1296 flags,
1297 bicubic,
1298 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001299 }
1300 }
1301 }
1302}
1303
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001304
1305/*
1306 * This is called by drawBitmap(), which has to handle images that may be too
1307 * large to be represented by a single texture.
1308 *
1309 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1310 * and that non-texture portion of the GrPaint has already been setup.
1311 */
1312void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1313 const SkRect& srcRect,
1314 const GrTextureParams& params,
1315 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001316 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001317 bool bicubic,
1318 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001319 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1320 bitmap.height() <= fContext->getMaxTextureSize());
1321
1322 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001323 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001324 if (NULL == texture) {
1325 return;
1326 }
1327
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001328 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001329 SkRect paintRect;
1330 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1331 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1332 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1333 SkScalarMul(srcRect.fTop, hInv),
1334 SkScalarMul(srcRect.fRight, wInv),
1335 SkScalarMul(srcRect.fBottom, hInv));
1336
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001337 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001338 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001339 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001340 // Use a constrained texture domain to avoid color bleeding
1341 SkScalar left, top, right, bottom;
1342 if (srcRect.width() > SK_Scalar1) {
1343 SkScalar border = SK_ScalarHalf / texture->width();
1344 left = paintRect.left() + border;
1345 right = paintRect.right() - border;
1346 } else {
1347 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1348 }
1349 if (srcRect.height() > SK_Scalar1) {
1350 SkScalar border = SK_ScalarHalf / texture->height();
1351 top = paintRect.top() + border;
1352 bottom = paintRect.bottom() - border;
1353 } else {
1354 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1355 }
1356 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001357 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001358 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001359 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001360 fp.reset(GrTextureDomainEffect::Create(texture,
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001361 SkMatrix::I(),
1362 textureDomain,
1363 GrTextureDomain::kClamp_Mode,
1364 params.filterMode()));
1365 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001366 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001367 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1368 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001369 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001370 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001371 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001372 }
1373
1374 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1375 // the rest from the SkPaint.
1376 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001377 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001378 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001379 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1380 SkColor2GrColor(paint.getColor());
1381 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001382
bsalomon01c8da12014-08-04 09:21:30 -07001383 fContext->drawRectToRect(grPaint, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001384}
1385
fmalita2d97bc12014-11-20 10:44:58 -08001386bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
1387 const SkImageFilter* filter,
1388 const SkImageFilter::Context& ctx,
1389 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001390 SkASSERT(filter);
fmalita2d97bc12014-11-20 10:44:58 -08001391
1392 // FIXME: plumb actual surface props such that we don't have to lie about the flags here
1393 // (https://code.google.com/p/skia/issues/detail?id=3148).
1394 SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001395
1396 if (filter->canFilterImageGPU()) {
1397 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1398 // filter. Also set the clip wide open and the matrix to identity.
1399 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001400 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001401 } else {
1402 return false;
1403 }
1404}
1405
1406void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1407 int left, int top, const SkPaint& paint) {
1408 // drawSprite is defined to be in device coords.
1409 CHECK_SHOULD_DRAW(draw, true);
1410
1411 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1412 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1413 return;
1414 }
1415
1416 int w = bitmap.width();
1417 int h = bitmap.height();
1418
1419 GrTexture* texture;
1420 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001421 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001422
1423 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001424 // This bitmap will own the filtered result as a texture.
1425 SkBitmap filteredBitmap;
1426
bsalomon49f085d2014-09-05 13:34:00 -07001427 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001428 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001429 SkMatrix matrix(*draw.fMatrix);
1430 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001431 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001432 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001433 // This cache is transient, and is freed (along with all its contained
1434 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001435 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001436 if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap,
1437 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001438 texture = (GrTexture*) filteredBitmap.getTexture();
1439 w = filteredBitmap.width();
1440 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001441 left += offset.x();
1442 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001443 } else {
1444 return;
1445 }
1446 }
1447
1448 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001449 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001450
dandov9de5b512014-06-10 14:38:28 -07001451 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1452 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001453
1454 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001455 SkRect::MakeXYWH(SkIntToScalar(left),
1456 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001457 SkIntToScalar(w),
1458 SkIntToScalar(h)),
1459 SkRect::MakeXYWH(0,
1460 0,
1461 SK_Scalar1 * w / texture->width(),
1462 SK_Scalar1 * h / texture->height()));
1463}
1464
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001465void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001466 const SkRect* src, const SkRect& dst,
1467 const SkPaint& paint,
1468 SkCanvas::DrawBitmapRectFlags flags) {
1469 SkMatrix matrix;
1470 SkRect bitmapBounds, tmpSrc;
1471
1472 bitmapBounds.set(0, 0,
1473 SkIntToScalar(bitmap.width()),
1474 SkIntToScalar(bitmap.height()));
1475
1476 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001477 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001478 tmpSrc = *src;
1479 } else {
1480 tmpSrc = bitmapBounds;
1481 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001482
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001483 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1484
1485 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001486 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001487 if (!bitmapBounds.contains(tmpSrc)) {
1488 if (!tmpSrc.intersect(bitmapBounds)) {
1489 return; // nothing to draw
1490 }
1491 }
1492 }
1493
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001494 SkRect tmpDst;
1495 matrix.mapRect(&tmpDst, tmpSrc);
1496
1497 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1498 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1499 // Translate so that tempDst's top left is at the origin.
1500 matrix = *origDraw.fMatrix;
1501 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1502 draw.writable()->fMatrix = &matrix;
1503 }
1504 SkSize dstSize;
1505 dstSize.fWidth = tmpDst.width();
1506 dstSize.fHeight = tmpDst.height();
1507
1508 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001509}
1510
1511void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1512 int x, int y, const SkPaint& paint) {
1513 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001514 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001515 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
jvanverth4736e142014-11-07 07:12:46 -08001516 if (dev->fFlags & kNeedClear_Flag) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001517 // TODO: could check here whether we really need to draw at all
reed37294692014-12-02 10:08:11 -08001518 dev->clearAll();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001519 }
1520
1521 // drawDevice is defined to be in device coords.
1522 CHECK_SHOULD_DRAW(draw, true);
1523
1524 GrRenderTarget* devRT = dev->accessRenderTarget();
1525 GrTexture* devTex;
1526 if (NULL == (devTex = devRT->asTexture())) {
1527 return;
1528 }
1529
1530 const SkBitmap& bm = dev->accessBitmap(false);
1531 int w = bm.width();
1532 int h = bm.height();
1533
1534 SkImageFilter* filter = paint.getImageFilter();
1535 // This bitmap will own the filtered result as a texture.
1536 SkBitmap filteredBitmap;
1537
bsalomon49f085d2014-09-05 13:34:00 -07001538 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001539 SkIPoint offset = SkIPoint::Make(0, 0);
1540 SkMatrix matrix(*draw.fMatrix);
1541 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001542 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001543 // This cache is transient, and is freed (along with all its contained
1544 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001545 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001546 SkImageFilter::Context ctx(matrix, clipBounds, cache);
fmalita2d97bc12014-11-20 10:44:58 -08001547 if (this->filterTexture(fContext, devTex, filter, ctx, &filteredBitmap,
1548 &offset)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001549 devTex = filteredBitmap.getTexture();
1550 w = filteredBitmap.width();
1551 h = filteredBitmap.height();
1552 x += offset.fX;
1553 y += offset.fY;
1554 } else {
1555 return;
1556 }
1557 }
1558
1559 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001560 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001561
dandov9de5b512014-06-10 14:38:28 -07001562 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1563 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001564
1565 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1566 SkIntToScalar(y),
1567 SkIntToScalar(w),
1568 SkIntToScalar(h));
1569
1570 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1571 // scratch texture).
1572 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1573 SK_Scalar1 * h / devTex->height());
1574
1575 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1576}
1577
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001578bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001579 return filter->canFilterImageGPU();
1580}
1581
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001582bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001583 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001584 SkBitmap* result, SkIPoint* offset) {
1585 // want explicitly our impl, so guard against a subclass of us overriding it
1586 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1587 return false;
1588 }
1589
1590 SkAutoLockPixels alp(src, !src.getTexture());
1591 if (!src.getTexture() && !src.readyToDraw()) {
1592 return false;
1593 }
1594
1595 GrTexture* texture;
1596 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1597 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001598 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001599
fmalita2d97bc12014-11-20 10:44:58 -08001600 return this->filterTexture(fContext, texture, filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001601}
1602
1603///////////////////////////////////////////////////////////////////////////////
1604
1605// must be in SkCanvas::VertexMode order
1606static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1607 kTriangles_GrPrimitiveType,
1608 kTriangleStrip_GrPrimitiveType,
1609 kTriangleFan_GrPrimitiveType,
1610};
1611
1612void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1613 int vertexCount, const SkPoint vertices[],
1614 const SkPoint texs[], const SkColor colors[],
1615 SkXfermode* xmode,
1616 const uint16_t indices[], int indexCount,
1617 const SkPaint& paint) {
1618 CHECK_SHOULD_DRAW(draw, false);
1619
egdanield78a1682014-07-09 10:41:26 -07001620 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001621
dandov32a311b2014-07-15 19:46:26 -07001622 const uint16_t* outIndices;
1623 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1624 GrPrimitiveType primType;
1625 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001626
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001627 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1628 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001629
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001630 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001631
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001632 SkPaint copy(paint);
1633 copy.setStyle(SkPaint::kStroke_Style);
1634 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001635
dandov32a311b2014-07-15 19:46:26 -07001636 // we ignore the shader if texs is null.
1637 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1638 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001639
dandov32a311b2014-07-15 19:46:26 -07001640 primType = kLines_GrPrimitiveType;
1641 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001642 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001643 switch (vmode) {
1644 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001645 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001646 break;
1647 case SkCanvas::kTriangleStrip_VertexMode:
1648 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001649 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001650 break;
1651 }
mtklein533eb782014-08-27 10:39:42 -07001652
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001653 VertState state(vertexCount, indices, indexCount);
1654 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001655
dandov32a311b2014-07-15 19:46:26 -07001656 //number of indices for lines per triangle with kLines
1657 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001658
dandov32a311b2014-07-15 19:46:26 -07001659 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1660 outIndices = outAlloc.get();
1661 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001662 int i = 0;
1663 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001664 auxIndices[i] = state.f0;
1665 auxIndices[i + 1] = state.f1;
1666 auxIndices[i + 2] = state.f1;
1667 auxIndices[i + 3] = state.f2;
1668 auxIndices[i + 4] = state.f2;
1669 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001670 i += 6;
1671 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001672 } else {
dandov32a311b2014-07-15 19:46:26 -07001673 outIndices = indices;
1674 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001675
dandov32a311b2014-07-15 19:46:26 -07001676 if (NULL == texs || NULL == paint.getShader()) {
1677 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1678 NULL == colors, &grPaint);
1679 } else {
1680 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
1681 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001682 }
1683
mtklein2583b622014-06-04 08:20:41 -07001684#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001685 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001686 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1687 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001688 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001689 }
1690 }
mtklein2583b622014-06-04 08:20:41 -07001691#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001692
1693 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001694 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001695 // need to convert byte order and from non-PM to PM
1696 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001697 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001698 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001699 color = colors[i];
1700 if (paint.getAlpha() != 255) {
1701 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1702 }
1703 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001704 }
1705 colors = convertedColors.get();
1706 }
1707 fContext->drawVertices(grPaint,
dandov32a311b2014-07-15 19:46:26 -07001708 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001709 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001710 vertices,
1711 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001712 colors,
dandov32a311b2014-07-15 19:46:26 -07001713 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001714 indexCount);
1715}
1716
1717///////////////////////////////////////////////////////////////////////////////
1718
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001719void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1720 size_t byteLength, SkScalar x, SkScalar y,
1721 const SkPaint& paint) {
1722 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -07001723 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001724
jvanverth8c27a182014-10-14 08:45:50 -07001725 GrPaint grPaint;
1726 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001727
jvanverth8c27a182014-10-14 08:45:50 -07001728 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001729
jvanverth8c27a182014-10-14 08:45:50 -07001730 if (!fTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y)) {
1731 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001732 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001733 }
1734}
1735
fmalita05c4a432014-09-29 06:29:53 -07001736void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1737 const SkScalar pos[], int scalarsPerPos,
1738 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001739 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001740 CHECK_SHOULD_DRAW(draw, false);
1741
jvanverth8c27a182014-10-14 08:45:50 -07001742 GrPaint grPaint;
1743 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001744
jvanverth8c27a182014-10-14 08:45:50 -07001745 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001746
jvanverth8c27a182014-10-14 08:45:50 -07001747 if (!fTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
1748 scalarsPerPos, offset)) {
1749 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001750 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001751 }
1752}
1753
1754void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1755 size_t len, const SkPath& path,
1756 const SkMatrix* m, const SkPaint& paint) {
1757 CHECK_SHOULD_DRAW(draw, false);
1758
1759 SkASSERT(draw.fDevice == this);
1760 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1761}
1762
1763///////////////////////////////////////////////////////////////////////////////
1764
reedb2db8982014-11-13 12:41:02 -08001765bool SkGpuDevice::onShouldDisableLCD(const SkPaint& paint) const {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001766 if (paint.getShader() ||
reedb2db8982014-11-13 12:41:02 -08001767 !SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode) ||
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001768 paint.getMaskFilter() ||
1769 paint.getRasterizer() ||
1770 paint.getColorFilter() ||
1771 paint.getPathEffect() ||
1772 paint.isFakeBoldText() ||
reedb2db8982014-11-13 12:41:02 -08001773 paint.getStyle() != SkPaint::kFill_Style)
1774 {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001775 return true;
1776 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001777 return false;
1778}
1779
1780void SkGpuDevice::flush() {
1781 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001782 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001783}
1784
1785///////////////////////////////////////////////////////////////////////////////
1786
fmalita6987dca2014-11-13 08:33:37 -08001787SkBaseDevice* SkGpuDevice::onCreateCompatibleDevice(const CreateInfo& cinfo) {
bsalomonf2703d82014-10-28 14:33:06 -07001788 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001789 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001790 desc.fFlags = kRenderTarget_GrSurfaceFlag;
fmalita6987dca2014-11-13 08:33:37 -08001791 desc.fWidth = cinfo.fInfo.width();
1792 desc.fHeight = cinfo.fInfo.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001793 desc.fSampleCnt = fRenderTarget->numSamples();
1794
1795 SkAutoTUnref<GrTexture> texture;
1796 // Skia's convention is to only clear a device if it is non-opaque.
fmalita6987dca2014-11-13 08:33:37 -08001797 unsigned flags = cinfo.fInfo.isOpaque() ? 0 : kNeedClear_Flag;
jvanverth4736e142014-11-07 07:12:46 -08001798 // If we're using distance field text, enable in the new device
1799 flags |= (fFlags & kDFText_Flag) ? kDFText_Flag : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001800
1801#if CACHE_COMPATIBLE_DEVICE_TEXTURES
hcm4396fa52014-10-27 21:43:30 -07001802 // layers are never draw in repeat modes, so we can request an approx
1803 // match and ignore any padding.
fmalita6987dca2014-11-13 08:33:37 -08001804 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == cinfo.fUsage) ?
hcm4396fa52014-10-27 21:43:30 -07001805 GrContext::kApprox_ScratchTexMatch :
1806 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001807 texture.reset(fContext->refScratchTexture(desc, match));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001808#else
1809 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1810#endif
bsalomon49f085d2014-09-05 13:34:00 -07001811 if (texture.get()) {
reedb2db8982014-11-13 12:41:02 -08001812 return SkGpuDevice::Create(texture, SkSurfaceProps(0, cinfo.fPixelGeometry), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001813 } else {
tfarina38406c82014-10-31 07:11:12 -07001814 SkDebugf("---- failed to create compatible device texture [%d %d]\n",
fmalita6987dca2014-11-13 08:33:37 -08001815 cinfo.fInfo.width(), cinfo.fInfo.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001816 return NULL;
1817 }
1818}
1819
reed4a8126e2014-09-22 07:29:03 -07001820SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1821 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001822}
1823
robertphillips30d2cc62014-09-24 08:52:18 -07001824bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001825 const SkMatrix* matrix, const SkPaint* paint) {
robertphillips30d78412014-11-24 09:49:17 -08001826 // todo: should handle this natively
1827 if (paint) {
reedd5fa1a42014-08-09 11:08:05 -07001828 return false;
1829 }
1830
robertphillips82365912014-11-12 09:32:34 -08001831 SkPicture::AccelData::Key key = SkLayerInfo::ComputeKey();
robertphillips81f71b62014-11-11 04:54:49 -08001832
1833 const SkPicture::AccelData* data = mainPicture->EXPERIMENTAL_getAccelData(key);
1834 if (!data) {
1835 return false;
1836 }
1837
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001838 SkRect clipBounds;
robertphillips0c423322014-07-31 11:02:38 -07001839 if (!mainCanvas->getClipBounds(&clipBounds)) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001840 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001841 }
1842
robertphillips30d78412014-11-24 09:49:17 -08001843 SkAutoCanvasMatrixPaint acmp(mainCanvas, matrix, paint, mainPicture->cullRect());
1844
1845 const SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1846
robertphillipsfd61ed02014-10-28 07:21:44 -07001847 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001848
robertphillipsfd61ed02014-10-28 07:21:44 -07001849 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001850 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001851 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001852 &atlasedNeedRendering, &atlasedRecycled,
1853 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001854
1855 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1856
1857 SkTDArray<GrHoistedLayer> needRendering, recycled;
1858
1859 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
robertphillips30d78412014-11-24 09:49:17 -08001860 initialMatrix,
robertphillipsfd61ed02014-10-28 07:21:44 -07001861 clipBounds,
robertphillipsa63f32e2014-11-10 08:10:42 -08001862 &needRendering, &recycled,
1863 fRenderTarget->numSamples());
robertphillipsfd61ed02014-10-28 07:21:44 -07001864
1865 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001866
robertphillips4aa6dfc2014-09-17 07:50:47 -07001867 GrReplacements replacements;
1868
robertphillips01d6e5f2014-12-01 09:09:27 -08001869 GrLayerHoister::ConvertLayersToReplacements(mainPicture, needRendering, &replacements);
1870 GrLayerHoister::ConvertLayersToReplacements(mainPicture, recycled, &replacements);
robertphillips4aa6dfc2014-09-17 07:50:47 -07001871
robertphillips64bf7672014-08-21 13:07:35 -07001872 // Render the entire picture using new layers
robertphillipsee6631e2014-09-29 05:32:49 -07001873 GrRecordReplaceDraw(mainPicture, mainCanvas, &replacements, initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001874
robertphillipsfd61ed02014-10-28 07:21:44 -07001875 GrLayerHoister::UnlockLayers(fContext, needRendering);
1876 GrLayerHoister::UnlockLayers(fContext, recycled);
1877 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1878 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001879
1880 return true;
1881}
1882
senorblancobe129b22014-08-08 07:14:35 -07001883SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001884 // We always return a transient cache, so it is freed after each
1885 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001886 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001887}
reedf037e0b2014-10-30 11:34:15 -07001888
1889#endif