blob: da81c696d2aa5857ee5d6e5b3b878fa9bebc7ed5 [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"
robertphillips@google.come930a072014-04-03 00:34:27 +000018#include "GrLayerCache.h"
robertphillips98d709b2014-09-02 10:20:50 -070019#include "GrLayerHoister.h"
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000020#include "GrPictureUtils.h"
robertphillips274b4ba2014-09-04 07:24:18 -070021#include "GrRecordReplaceDraw.h"
egdanield58a0ba2014-06-11 10:30:05 -070022#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070023#include "GrTracing.h"
derekff4555aa2014-10-06 12:19:12 -070024#include "GrGpu.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025
26#include "SkGrTexturePixelRef.h"
27
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkDeviceImageFilterProxy.h"
29#include "SkDrawProcs.h"
30#include "SkGlyphCache.h"
31#include "SkImageFilter.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 { \
70 if (fNeedClear) { \
71 this->clear(SK_ColorTRANSPARENT); \
72 } \
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
reed4a8126e2014-09-22 07:29:03 -0700134SkGpuDevice::SkGpuDevice(GrSurface* surface, const SkSurfaceProps& props, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000135
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000136 fDrawProcs = NULL;
137
bsalomon32d0b3b2014-08-29 07:50:23 -0700138 fContext = SkRef(surface->getContext());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000139
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000140 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000141
bsalomon32d0b3b2014-08-29 07:50:23 -0700142 fRenderTarget = SkRef(surface->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000143
bsalomonafbf2d62014-09-30 12:18:44 -0700144 SkImageInfo info = surface->surfacePriv().info();
bsalomonbcf0a522014-10-08 08:40:09 -0700145 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface));
bsalomonafbf2d62014-09-30 12:18:44 -0700146 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700147 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700148
reed4a8126e2014-09-22 07:29:03 -0700149 this->setPixelGeometry(props.pixelGeometry());
150
kkinnunenc6cb56f2014-06-24 00:12:27 -0700151 bool useDFFonts = !!(flags & kDFFonts_Flag);
jvanverth8c27a182014-10-14 08:45:50 -0700152 fTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFFonts);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000153}
154
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000155SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
reed4a8126e2014-09-22 07:29:03 -0700156 const SkSurfaceProps& props, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000157 if (kUnknown_SkColorType == origInfo.colorType() ||
158 origInfo.width() < 0 || origInfo.height() < 0) {
159 return NULL;
160 }
161
reede5ea5002014-09-03 11:54:58 -0700162 SkColorType ct = origInfo.colorType();
163 SkAlphaType at = origInfo.alphaType();
robertphillipsa0537de2014-09-18 08:01:23 -0700164 // TODO: perhaps we can loosen this check now that colortype is more detailed
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000165 // e.g. can we support both RGBA and BGRA here?
reede5ea5002014-09-03 11:54:58 -0700166 if (kRGB_565_SkColorType == ct) {
167 at = kOpaque_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000168 } else {
reede5ea5002014-09-03 11:54:58 -0700169 ct = kN32_SkColorType;
170 if (kOpaque_SkAlphaType != at) {
171 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000172 }
173 }
reede5ea5002014-09-03 11:54:58 -0700174 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000175
bsalomonf2703d82014-10-28 14:33:06 -0700176 GrSurfaceDesc desc;
177 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000178 desc.fWidth = info.width();
179 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000180 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000181 desc.fSampleCnt = sampleCount;
182
183 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
184 if (!texture.get()) {
185 return NULL;
186 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000187
reed4a8126e2014-09-22 07:29:03 -0700188 return SkNEW_ARGS(SkGpuDevice, (texture.get(), props));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000189}
190
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000191SkGpuDevice::~SkGpuDevice() {
192 if (fDrawProcs) {
193 delete fDrawProcs;
194 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000195
jvanverth8c27a182014-10-14 08:45:50 -0700196 delete fTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000197
198 // The GrContext takes a ref on the target. We don't want to cause the render
199 // target to be unnecessarily kept alive.
200 if (fContext->getRenderTarget() == fRenderTarget) {
201 fContext->setRenderTarget(NULL);
202 }
203
204 if (fContext->getClip() == &fClipData) {
205 fContext->setClip(NULL);
206 }
207
bsalomon32d0b3b2014-08-29 07:50:23 -0700208 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000209 fContext->unref();
210}
211
212///////////////////////////////////////////////////////////////////////////////
213
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000214bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
215 int x, int y) {
216 DO_DEFERRED_CLEAR();
217
218 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000219 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000220 if (kUnknown_GrPixelConfig == config) {
221 return false;
222 }
223
224 uint32_t flags = 0;
225 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
226 flags = GrContext::kUnpremul_PixelOpsFlag;
227 }
228 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
229 config, dstPixels, dstRowBytes, flags);
230}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000231
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000232bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
233 int x, int y) {
234 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000235 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000236 if (kUnknown_GrPixelConfig == config) {
237 return false;
238 }
239 uint32_t flags = 0;
240 if (kUnpremul_SkAlphaType == info.alphaType()) {
241 flags = GrContext::kUnpremul_PixelOpsFlag;
242 }
243 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
244
245 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700246 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000247
248 return true;
249}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000250
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000251const SkBitmap& SkGpuDevice::onAccessBitmap() {
252 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700253 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000254}
255
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000256void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
257 INHERITED::onAttachToCanvas(canvas);
258
259 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
260 fClipData.fClipStack = canvas->getClipStack();
261}
262
263void SkGpuDevice::onDetachFromCanvas() {
264 INHERITED::onDetachFromCanvas();
265 fClipData.fClipStack = NULL;
266}
267
268// call this every draw call, to ensure that the context reflects our state,
269// and not the state from some other canvas/device
270void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
bsalomon49f085d2014-09-05 13:34:00 -0700271 SkASSERT(fClipData.fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000272
273 fContext->setRenderTarget(fRenderTarget);
274
275 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
276
277 if (forceIdentity) {
278 fContext->setIdentityMatrix();
279 } else {
280 fContext->setMatrix(*draw.fMatrix);
281 }
282 fClipData.fOrigin = this->getOrigin();
283
284 fContext->setClip(&fClipData);
285
286 DO_DEFERRED_CLEAR();
287}
288
289GrRenderTarget* SkGpuDevice::accessRenderTarget() {
290 DO_DEFERRED_CLEAR();
291 return fRenderTarget;
292}
293
294///////////////////////////////////////////////////////////////////////////////
295
296SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
297SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
298SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
299SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
300SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
301 shader_type_mismatch);
302SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
303 shader_type_mismatch);
304SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
305SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
306
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000307///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000308
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000309void SkGpuDevice::clear(SkColor color) {
egdanield78a1682014-07-09 10:41:26 -0700310 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clear", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000311 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
312 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
313 fNeedClear = false;
314}
315
316void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
317 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700318 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000319
320 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000321 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000322
323 fContext->drawPaint(grPaint);
324}
325
326// must be in SkCanvas::PointMode order
327static const GrPrimitiveType gPointMode2PrimtiveType[] = {
328 kPoints_GrPrimitiveType,
329 kLines_GrPrimitiveType,
330 kLineStrip_GrPrimitiveType
331};
332
333void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
334 size_t count, const SkPoint pts[], const SkPaint& paint) {
335 CHECK_FOR_ANNOTATION(paint);
336 CHECK_SHOULD_DRAW(draw, false);
337
338 SkScalar width = paint.getStrokeWidth();
339 if (width < 0) {
340 return;
341 }
342
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000343 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700344 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
345 GrPaint grPaint;
346 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
347 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700348 path.setIsVolatile(true);
egdaniele61c4112014-06-12 10:24:21 -0700349 path.moveTo(pts[0]);
350 path.lineTo(pts[1]);
351 fContext->drawPath(grPaint, path, strokeInfo);
352 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000353 }
354
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000355 // we only handle hairlines and paints without path effects or mask filters,
356 // else we let the SkDraw call our drawPath()
357 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
358 draw.drawPoints(mode, count, pts, paint, true);
359 return;
360 }
361
362 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000363 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000364
365 fContext->drawVertices(grPaint,
366 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000367 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000368 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000369 NULL,
370 NULL,
371 NULL,
372 0);
373}
374
375///////////////////////////////////////////////////////////////////////////////
376
377void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
378 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700379 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
380
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000381 CHECK_FOR_ANNOTATION(paint);
382 CHECK_SHOULD_DRAW(draw, false);
383
384 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
385 SkScalar width = paint.getStrokeWidth();
386
387 /*
388 We have special code for hairline strokes, miter-strokes, bevel-stroke
389 and fills. Anything else we just call our path code.
390 */
391 bool usePath = doStroke && width > 0 &&
392 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
393 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
394 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700395
396 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000397 usePath = true;
398 }
egdanield58a0ba2014-06-11 10:30:05 -0700399
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000400 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
401#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
402 if (doStroke) {
403#endif
404 usePath = true;
405#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
406 } else {
407 usePath = !fContext->getMatrix().preservesRightAngles();
408 }
409#endif
410 }
411 // until we can both stroke and fill rectangles
412 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
413 usePath = true;
414 }
415
egdanield58a0ba2014-06-11 10:30:05 -0700416 GrStrokeInfo strokeInfo(paint);
417
418 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700419 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700420 usePath = true;
421 }
422
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000423 if (usePath) {
424 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700425 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000426 path.addRect(rect);
427 this->drawPath(draw, path, paint, NULL, true);
428 return;
429 }
430
431 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000432 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400433
egdanield58a0ba2014-06-11 10:30:05 -0700434 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000435}
436
437///////////////////////////////////////////////////////////////////////////////
438
439void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
440 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700441 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000442 CHECK_FOR_ANNOTATION(paint);
443 CHECK_SHOULD_DRAW(draw, false);
444
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000445 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000446 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400447
egdanield58a0ba2014-06-11 10:30:05 -0700448 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000449 if (paint.getMaskFilter()) {
450 // try to hit the fast path for drawing filtered round rects
451
452 SkRRect devRRect;
453 if (rect.transform(fContext->getMatrix(), &devRRect)) {
454 if (devRRect.allCornersCircular()) {
455 SkRect maskRect;
456 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
457 draw.fClip->getBounds(),
458 fContext->getMatrix(),
459 &maskRect)) {
460 SkIRect finalIRect;
461 maskRect.roundOut(&finalIRect);
462 if (draw.fClip->quickReject(finalIRect)) {
463 // clipped out
464 return;
465 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000466 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700467 strokeInfo.getStrokeRec(),
468 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000469 return;
470 }
471 }
472
473 }
474 }
475
476 }
477
egdanield58a0ba2014-06-11 10:30:05 -0700478 bool usePath = false;
479
480 if (paint.getMaskFilter()) {
481 usePath = true;
482 } else {
483 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700484 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700485 usePath = true;
486 }
487 }
488
489
490 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000491 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700492 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000493 path.addRRect(rect);
494 this->drawPath(draw, path, paint, NULL, true);
495 return;
496 }
Mike Klein744fb732014-06-23 15:13:26 -0400497
egdanield58a0ba2014-06-11 10:30:05 -0700498 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000499}
500
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000501void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
502 const SkRRect& inner, const SkPaint& paint) {
503 SkStrokeRec stroke(paint);
504 if (stroke.isFillStyle()) {
505
506 CHECK_FOR_ANNOTATION(paint);
507 CHECK_SHOULD_DRAW(draw, false);
508
509 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000510 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000511
512 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
513 fContext->drawDRRect(grPaint, outer, inner);
514 return;
515 }
516 }
517
518 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700519 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000520 path.addRRect(outer);
521 path.addRRect(inner);
522 path.setFillType(SkPath::kEvenOdd_FillType);
523
524 this->drawPath(draw, path, paint, NULL, true);
525}
526
527
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000528/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000529
530void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
531 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700532 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000533 CHECK_FOR_ANNOTATION(paint);
534 CHECK_SHOULD_DRAW(draw, false);
535
egdanield58a0ba2014-06-11 10:30:05 -0700536 GrStrokeInfo strokeInfo(paint);
537
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000538 bool usePath = false;
539 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700540 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700542 } else {
543 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700544 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700545 usePath = true;
546 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000547 }
548
549 if (usePath) {
550 SkPath path;
jvanverthb3eb6872014-10-24 07:12:51 -0700551 path.setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000552 path.addOval(oval);
553 this->drawPath(draw, path, paint, NULL, true);
554 return;
555 }
556
557 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000558 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000559
egdanield58a0ba2014-06-11 10:30:05 -0700560 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000561}
562
563#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000564
565///////////////////////////////////////////////////////////////////////////////
566
567// helpers for applying mask filters
568namespace {
569
570// Draw a mask using the supplied paint. Since the coverage/geometry
571// is already burnt into the mask this boils down to a rect draw.
572// Return true if the mask was successfully drawn.
573bool draw_mask(GrContext* context, const SkRect& maskRect,
574 GrPaint* grp, GrTexture* mask) {
575 GrContext::AutoMatrix am;
576 if (!am.setIdentity(context, grp)) {
577 return false;
578 }
579
580 SkMatrix matrix;
581 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
582 matrix.postIDiv(mask->width(), mask->height());
583
joshualittb0a8a372014-09-23 09:50:21 -0700584 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000585 context->drawRect(*grp, maskRect);
586 return true;
587}
588
589bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700590 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000591 GrPaint* grp, SkPaint::Style style) {
592 SkMask srcM, dstM;
593
594 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
595 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
596 return false;
597 }
598 SkAutoMaskFreeImage autoSrc(srcM.fImage);
599
600 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
601 return false;
602 }
603 // this will free-up dstM when we're done (allocated in filterMask())
604 SkAutoMaskFreeImage autoDst(dstM.fImage);
605
606 if (clip.quickReject(dstM.fBounds)) {
607 return false;
608 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000609
610 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
611 // the current clip (and identity matrix) and GrPaint settings
bsalomonf2703d82014-10-28 14:33:06 -0700612 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000613 desc.fWidth = dstM.fBounds.width();
614 desc.fHeight = dstM.fBounds.height();
615 desc.fConfig = kAlpha_8_GrPixelConfig;
616
bsalomone3059732014-10-14 11:47:22 -0700617 SkAutoTUnref<GrTexture> texture(
618 context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
619 if (!texture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000620 return false;
621 }
622 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
623 dstM.fImage, dstM.fRowBytes);
624
625 SkRect maskRect = SkRect::Make(dstM.fBounds);
626
627 return draw_mask(context, maskRect, grp, texture);
628}
629
bsalomone3059732014-10-14 11:47:22 -0700630// Create a mask of 'devPath' and place the result in 'mask'.
631GrTexture* create_mask_GPU(GrContext* context,
632 const SkRect& maskRect,
633 const SkPath& devPath,
634 const GrStrokeInfo& strokeInfo,
635 bool doAA,
636 int sampleCnt) {
bsalomonf2703d82014-10-28 14:33:06 -0700637 GrSurfaceDesc desc;
638 desc.fFlags = kRenderTarget_GrSurfaceFlag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000639 desc.fWidth = SkScalarCeilToInt(maskRect.width());
640 desc.fHeight = SkScalarCeilToInt(maskRect.height());
bsalomone3059732014-10-14 11:47:22 -0700641 desc.fSampleCnt = doAA ? sampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000642 // We actually only need A8, but it often isn't supported as a
643 // render target so default to RGBA_8888
644 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700645
646 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
647 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000648 desc.fConfig = kAlpha_8_GrPixelConfig;
649 }
650
bsalomone3059732014-10-14 11:47:22 -0700651 GrTexture* mask = context->refScratchTexture(desc,GrContext::kApprox_ScratchTexMatch);
652 if (NULL == mask) {
653 return NULL;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000654 }
655
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000656 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
657
bsalomone3059732014-10-14 11:47:22 -0700658 GrContext::AutoRenderTarget art(context, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000659 GrContext::AutoClip ac(context, clipRect);
660
bsalomon89c62982014-11-03 12:08:42 -0800661 context->clear(NULL, 0x0, true, mask->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000662
663 GrPaint tempPaint;
664 if (doAA) {
665 tempPaint.setAntiAlias(true);
666 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
667 // blend coeff of zero requires dual source blending support in order
668 // to properly blend partially covered pixels. This means the AA
669 // code path may not be taken. So we use a dst blend coeff of ISA. We
670 // could special case AA draws to a dst surface with known alpha=0 to
671 // use a zero dst coeff when dual source blending isn't available.
672 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
673 }
674
675 GrContext::AutoMatrix am;
676
677 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
678 SkMatrix translate;
679 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
680 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700681 context->drawPath(tempPaint, devPath, strokeInfo);
bsalomone3059732014-10-14 11:47:22 -0700682 return mask;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000683}
684
685SkBitmap wrap_texture(GrTexture* texture) {
686 SkBitmap result;
bsalomonafbf2d62014-09-30 12:18:44 -0700687 result.setInfo(texture->surfacePriv().info());
reed6c225732014-06-09 19:52:07 -0700688 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000689 return result;
690}
691
692};
693
694void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
695 const SkPaint& paint, const SkMatrix* prePathMatrix,
696 bool pathIsMutable) {
697 CHECK_FOR_ANNOTATION(paint);
698 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700699 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000700
jvanverthb3eb6872014-10-24 07:12:51 -0700701 SkASSERT(!pathIsMutable || origSrcPath.isVolatile());
702
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000703 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000704 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000705
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000706 // If we have a prematrix, apply it to the path, optimizing for the case
707 // where the original path can in fact be modified in place (even though
708 // its parameter type is const).
709 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000710 SkTLazy<SkPath> tmpPath;
711 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000712
713 if (prePathMatrix) {
714 SkPath* result = pathPtr;
715
716 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000717 result = tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700718 result->setIsVolatile(true);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000719 pathIsMutable = true;
720 }
721 // should I push prePathMatrix on our MV stack temporarily, instead
722 // of applying it here? See SkDraw.cpp
723 pathPtr->transform(*prePathMatrix, result);
724 pathPtr = result;
725 }
726 // at this point we're done with prePathMatrix
727 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
728
egdanield58a0ba2014-06-11 10:30:05 -0700729 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000730 SkPathEffect* pathEffect = paint.getPathEffect();
731 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700732 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
733 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000734 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000735 pathPtr = effectPath.get();
736 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700737 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000738 }
739
egdanield58a0ba2014-06-11 10:30:05 -0700740 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000741 if (paint.getMaskFilter()) {
742 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000743 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
744 if (stroke.applyToPath(strokedPath, *pathPtr)) {
745 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000746 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700747 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000748 }
749 }
750
751 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000752 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
jvanverthb3eb6872014-10-24 07:12:51 -0700753 if (!pathIsMutable) {
754 devPathPtr->setIsVolatile(true);
755 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000756
757 // transform the path into device space
758 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000759
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000760 SkRect maskRect;
761 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
762 draw.fClip->getBounds(),
763 fContext->getMatrix(),
764 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000765 // The context's matrix may change while creating the mask, so save the CTM here to
766 // pass to filterMaskGPU.
767 const SkMatrix ctm = fContext->getMatrix();
768
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000769 SkIRect finalIRect;
770 maskRect.roundOut(&finalIRect);
771 if (draw.fClip->quickReject(finalIRect)) {
772 // clipped out
773 return;
774 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000775
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000776 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000777 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000778 // the mask filter was able to draw itself directly, so there's nothing
779 // left to do.
780 return;
781 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000782
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000783
bsalomone3059732014-10-14 11:47:22 -0700784 SkAutoTUnref<GrTexture> mask(create_mask_GPU(fContext, maskRect, *devPathPtr,
785 strokeInfo, grPaint.isAntiAlias(),
786 fRenderTarget->numSamples()));
787 if (mask) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000788 GrTexture* filtered;
789
bsalomone3059732014-10-14 11:47:22 -0700790 if (paint.getMaskFilter()->filterMaskGPU(mask, ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000791 // filterMaskGPU gives us ownership of a ref to the result
792 SkAutoTUnref<GrTexture> atu(filtered);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000793 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
794 // This path is completely drawn
795 return;
796 }
797 }
798 }
799 }
800
801 // draw the mask on the CPU - this is a fallthrough path in case the
802 // GPU path fails
803 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
804 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700805 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
806 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000807 return;
808 }
809
egdanield58a0ba2014-06-11 10:30:05 -0700810 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000811}
812
813static const int kBmpSmallTileSize = 1 << 10;
814
815static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
816 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
817 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
818 return tilesX * tilesY;
819}
820
821static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
822 if (maxTileSize <= kBmpSmallTileSize) {
823 return maxTileSize;
824 }
825
826 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
827 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
828
829 maxTileTotalTileSize *= maxTileSize * maxTileSize;
830 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
831
832 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
833 return kBmpSmallTileSize;
834 } else {
835 return maxTileSize;
836 }
837}
838
839// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
840// pixels from the bitmap are necessary.
841static void determine_clipped_src_rect(const GrContext* context,
842 const SkBitmap& bitmap,
843 const SkRect* srcRectPtr,
844 SkIRect* clippedSrcIRect) {
845 const GrClipData* clip = context->getClip();
846 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
847 SkMatrix inv;
848 if (!context->getMatrix().invert(&inv)) {
849 clippedSrcIRect->setEmpty();
850 return;
851 }
852 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
853 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700854 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000855 // we've setup src space 0,0 to map to the top left of the src rect.
856 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000857 if (!clippedSrcRect.intersect(*srcRectPtr)) {
858 clippedSrcIRect->setEmpty();
859 return;
860 }
861 }
862 clippedSrcRect.roundOut(clippedSrcIRect);
863 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
864 if (!clippedSrcIRect->intersect(bmpBounds)) {
865 clippedSrcIRect->setEmpty();
866 }
867}
868
869bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
870 const GrTextureParams& params,
871 const SkRect* srcRectPtr,
872 int maxTileSize,
873 int* tileSize,
874 SkIRect* clippedSrcRect) const {
875 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700876 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000877 return false;
878 }
879
880 // if it's larger than the max tile size, then we have no choice but tiling.
881 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
882 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
883 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
884 return true;
885 }
886
887 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
888 return false;
889 }
890
891 // if the entire texture is already in our cache then no reason to tile it
892 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
893 return false;
894 }
895
896 // At this point we know we could do the draw by uploading the entire bitmap
897 // as a texture. However, if the texture would be large compared to the
898 // cache size and we don't require most of it for this draw then tile to
899 // reduce the amount of upload and cache spill.
900
901 // assumption here is that sw bitmap size is a good proxy for its size as
902 // a texture
903 size_t bmpSize = bitmap.getSize();
904 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000905 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000906 if (bmpSize < cacheSize / 2) {
907 return false;
908 }
909
910 // Figure out how much of the src we will need based on the src rect and clipping.
911 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
912 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
913 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
914 kBmpSmallTileSize * kBmpSmallTileSize;
915
916 return usedTileBytes < 2 * bmpSize;
917}
918
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000919void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000920 const SkBitmap& bitmap,
921 const SkMatrix& m,
922 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000923 SkMatrix concat;
924 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
925 if (!m.isIdentity()) {
926 concat.setConcat(*draw->fMatrix, m);
927 draw.writable()->fMatrix = &concat;
928 }
929 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000930}
931
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000932// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000933// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
934// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000935static inline void clamped_outset_with_offset(SkIRect* iRect,
936 int outset,
937 SkPoint* offset,
938 const SkIRect& clamp) {
939 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000940
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000941 int leftClampDelta = clamp.fLeft - iRect->fLeft;
942 if (leftClampDelta > 0) {
943 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000944 iRect->fLeft = clamp.fLeft;
945 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000946 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000947 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000948
949 int topClampDelta = clamp.fTop - iRect->fTop;
950 if (topClampDelta > 0) {
951 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000952 iRect->fTop = clamp.fTop;
953 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000954 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000955 }
956
957 if (iRect->fRight > clamp.fRight) {
958 iRect->fRight = clamp.fRight;
959 }
960 if (iRect->fBottom > clamp.fBottom) {
961 iRect->fBottom = clamp.fBottom;
962 }
963}
964
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000965static bool has_aligned_samples(const SkRect& srcRect,
966 const SkRect& transformedRect) {
967 // detect pixel disalignment
968 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
969 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
970 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
971 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
972 SkScalarAbs(transformedRect.width() - srcRect.width()) <
973 COLOR_BLEED_TOLERANCE &&
974 SkScalarAbs(transformedRect.height() - srcRect.height()) <
975 COLOR_BLEED_TOLERANCE) {
976 return true;
977 }
978 return false;
979}
980
981static bool may_color_bleed(const SkRect& srcRect,
982 const SkRect& transformedRect,
983 const SkMatrix& m) {
984 // Only gets called if has_aligned_samples returned false.
985 // So we can assume that sampling is axis aligned but not texel aligned.
986 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
987 SkRect innerSrcRect(srcRect), innerTransformedRect,
988 outerTransformedRect(transformedRect);
989 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
990 m.mapRect(&innerTransformedRect, innerSrcRect);
991
992 // The gap between outerTransformedRect and innerTransformedRect
993 // represents the projection of the source border area, which is
994 // problematic for color bleeding. We must check whether any
995 // destination pixels sample the border area.
996 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
997 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
998 SkIRect outer, inner;
999 outerTransformedRect.round(&outer);
1000 innerTransformedRect.round(&inner);
1001 // If the inner and outer rects round to the same result, it means the
1002 // border does not overlap any pixel centers. Yay!
1003 return inner != outer;
1004}
1005
1006static bool needs_texture_domain(const SkBitmap& bitmap,
1007 const SkRect& srcRect,
1008 GrTextureParams &params,
1009 const SkMatrix& contextMatrix,
1010 bool bicubic) {
1011 bool needsTextureDomain = false;
1012
1013 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1014 // Need texture domain if drawing a sub rect
1015 needsTextureDomain = srcRect.width() < bitmap.width() ||
1016 srcRect.height() < bitmap.height();
1017 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1018 // sampling is axis-aligned
1019 SkRect transformedRect;
1020 contextMatrix.mapRect(&transformedRect, srcRect);
1021
1022 if (has_aligned_samples(srcRect, transformedRect)) {
1023 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1024 needsTextureDomain = false;
1025 } else {
1026 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1027 }
1028 }
1029 }
1030 return needsTextureDomain;
1031}
1032
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001033void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1034 const SkBitmap& bitmap,
1035 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001036 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001037 const SkPaint& paint,
1038 SkCanvas::DrawBitmapRectFlags flags) {
1039 CHECK_SHOULD_DRAW(draw, false);
1040
1041 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001042 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001043 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1044 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001046 SkScalar w = SkIntToScalar(bitmap.width());
1047 SkScalar h = SkIntToScalar(bitmap.height());
1048 dstSize.fWidth = w;
1049 dstSize.fHeight = h;
1050 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001051 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001052 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001053 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001054 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001055 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001056 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1057 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1058 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1059 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001060 }
1061
1062 if (paint.getMaskFilter()){
1063 // Convert the bitmap to a shader so that the rect can be drawn
1064 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001065 SkBitmap tmp; // subset of bitmap, if necessary
1066 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001067 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001068 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001069 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1070 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1071 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001072 // In bleed mode we position and trim the bitmap based on the src rect which is
1073 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1074 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1075 // compensate.
1076 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1077 SkIRect iSrc;
1078 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001079
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001080 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1081 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001082
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001083 if (!bitmap.extractSubset(&tmp, iSrc)) {
1084 return; // extraction failed
1085 }
1086 bitmapPtr = &tmp;
1087 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001088
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001089 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001090 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001091 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001092 } else {
1093 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001094 }
1095
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001096 SkPaint paintWithShader(paint);
1097 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001098 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001099 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1100 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001101
1102 return;
1103 }
1104
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001105 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1106 // the view matrix rather than a local matrix.
1107 SkMatrix m;
1108 m.setScale(dstSize.fWidth / srcRect.width(),
1109 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001110 fContext->concatMatrix(m);
1111
1112 GrTextureParams params;
1113 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1114 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001115
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001116 bool doBicubic = false;
1117
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001118 switch(paintFilterLevel) {
1119 case SkPaint::kNone_FilterLevel:
1120 textureFilterMode = GrTextureParams::kNone_FilterMode;
1121 break;
1122 case SkPaint::kLow_FilterLevel:
1123 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1124 break;
1125 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001126 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001127 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1128 } else {
1129 // Don't trigger MIP level generation unnecessarily.
1130 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1131 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001132 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001133 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001134 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001135 doBicubic =
1136 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001137 break;
1138 default:
1139 SkErrorInternals::SetError( kInvalidPaint_SkError,
1140 "Sorry, I don't understand the filtering "
1141 "mode you asked for. Falling back to "
1142 "MIPMaps.");
1143 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1144 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001145 }
1146
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001147 int tileFilterPad;
1148 if (doBicubic) {
1149 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1150 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1151 tileFilterPad = 0;
1152 } else {
1153 tileFilterPad = 1;
1154 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001155 params.setFilterMode(textureFilterMode);
1156
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001157 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001158 int tileSize;
1159
1160 SkIRect clippedSrcRect;
1161 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1162 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001163 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1164 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001165 } else {
1166 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001167 bool needsTextureDomain = needs_texture_domain(bitmap,
1168 srcRect,
1169 params,
1170 fContext->getMatrix(),
1171 doBicubic);
1172 this->internalDrawBitmap(bitmap,
1173 srcRect,
1174 params,
1175 paint,
1176 flags,
1177 doBicubic,
1178 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001179 }
1180}
1181
1182// Break 'bitmap' into several tiles to draw it since it has already
1183// been determined to be too large to fit in VRAM
1184void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1185 const SkRect& srcRect,
1186 const SkIRect& clippedSrcIRect,
1187 const GrTextureParams& params,
1188 const SkPaint& paint,
1189 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001190 int tileSize,
1191 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001192 // The following pixel lock is technically redundant, but it is desirable
1193 // to lock outside of the tile loop to prevent redecoding the whole image
1194 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1195 // is larger than the limit of the discardable memory pool.
1196 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001197 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1198
1199 int nx = bitmap.width() / tileSize;
1200 int ny = bitmap.height() / tileSize;
1201 for (int x = 0; x <= nx; x++) {
1202 for (int y = 0; y <= ny; y++) {
1203 SkRect tileR;
1204 tileR.set(SkIntToScalar(x * tileSize),
1205 SkIntToScalar(y * tileSize),
1206 SkIntToScalar((x + 1) * tileSize),
1207 SkIntToScalar((y + 1) * tileSize));
1208
1209 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1210 continue;
1211 }
1212
1213 if (!tileR.intersect(srcRect)) {
1214 continue;
1215 }
1216
1217 SkBitmap tmpB;
1218 SkIRect iTileR;
1219 tileR.roundOut(&iTileR);
1220 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1221 SkIntToScalar(iTileR.fTop));
1222
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001223 // Adjust the context matrix to draw at the right x,y in device space
1224 SkMatrix tmpM;
1225 GrContext::AutoMatrix am;
1226 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1227 am.setPreConcat(fContext, tmpM);
1228
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001229 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001230 SkIRect iClampRect;
1231
1232 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1233 // In bleed mode we want to always expand the tile on all edges
1234 // but stay within the bitmap bounds
1235 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1236 } else {
1237 // In texture-domain/clamp mode we only want to expand the
1238 // tile on edges interior to "srcRect" (i.e., we want to
1239 // not bleed across the original clamped edges)
1240 srcRect.roundOut(&iClampRect);
1241 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001242 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1243 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001244 }
1245
1246 if (bitmap.extractSubset(&tmpB, iTileR)) {
1247 // now offset it to make it "local" to our tmp bitmap
1248 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001249 GrTextureParams paramsTemp = params;
1250 bool needsTextureDomain = needs_texture_domain(bitmap,
1251 srcRect,
1252 paramsTemp,
1253 fContext->getMatrix(),
1254 bicubic);
1255 this->internalDrawBitmap(tmpB,
1256 tileR,
1257 paramsTemp,
1258 paint,
1259 flags,
1260 bicubic,
1261 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001262 }
1263 }
1264 }
1265}
1266
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001267
1268/*
1269 * This is called by drawBitmap(), which has to handle images that may be too
1270 * large to be represented by a single texture.
1271 *
1272 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1273 * and that non-texture portion of the GrPaint has already been setup.
1274 */
1275void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1276 const SkRect& srcRect,
1277 const GrTextureParams& params,
1278 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001279 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001280 bool bicubic,
1281 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001282 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1283 bitmap.height() <= fContext->getMaxTextureSize());
1284
1285 GrTexture* texture;
bsalomonbcf0a522014-10-08 08:40:09 -07001286 AutoBitmapTexture abt(fContext, bitmap, &params, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001287 if (NULL == texture) {
1288 return;
1289 }
1290
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001291 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001292 SkRect paintRect;
1293 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1294 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1295 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1296 SkScalarMul(srcRect.fTop, hInv),
1297 SkScalarMul(srcRect.fRight, wInv),
1298 SkScalarMul(srcRect.fBottom, hInv));
1299
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001300 SkRect textureDomain = SkRect::MakeEmpty();
joshualittb0a8a372014-09-23 09:50:21 -07001301 SkAutoTUnref<GrFragmentProcessor> fp;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001302 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001303 // Use a constrained texture domain to avoid color bleeding
1304 SkScalar left, top, right, bottom;
1305 if (srcRect.width() > SK_Scalar1) {
1306 SkScalar border = SK_ScalarHalf / texture->width();
1307 left = paintRect.left() + border;
1308 right = paintRect.right() - border;
1309 } else {
1310 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1311 }
1312 if (srcRect.height() > SK_Scalar1) {
1313 SkScalar border = SK_ScalarHalf / texture->height();
1314 top = paintRect.top() + border;
1315 bottom = paintRect.bottom() - border;
1316 } else {
1317 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1318 }
1319 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001320 if (bicubic) {
joshualittb0a8a372014-09-23 09:50:21 -07001321 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001322 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001323 fp.reset(GrTextureDomainEffect::Create(texture,
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001324 SkMatrix::I(),
1325 textureDomain,
1326 GrTextureDomain::kClamp_Mode,
1327 params.filterMode()));
1328 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001329 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001330 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1331 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
joshualittb0a8a372014-09-23 09:50:21 -07001332 fp.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001333 } else {
joshualittb0a8a372014-09-23 09:50:21 -07001334 fp.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001335 }
1336
1337 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1338 // the rest from the SkPaint.
1339 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001340 grPaint.addColorProcessor(fp);
reed0689d7b2014-06-14 05:30:20 -07001341 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001342 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1343 SkColor2GrColor(paint.getColor());
1344 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001345
bsalomon01c8da12014-08-04 09:21:30 -07001346 fContext->drawRectToRect(grPaint, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001347}
1348
1349static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001350 GrTexture* texture, const SkImageFilter* filter,
robertphillips6219e1f2014-10-20 08:12:04 -07001351 const SkImageFilter::Context& ctx,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001352 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001353 SkASSERT(filter);
1354 SkDeviceImageFilterProxy proxy(device);
1355
1356 if (filter->canFilterImageGPU()) {
1357 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1358 // filter. Also set the clip wide open and the matrix to identity.
1359 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001360 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001361 } else {
1362 return false;
1363 }
1364}
1365
1366void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1367 int left, int top, const SkPaint& paint) {
1368 // drawSprite is defined to be in device coords.
1369 CHECK_SHOULD_DRAW(draw, true);
1370
1371 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1372 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1373 return;
1374 }
1375
1376 int w = bitmap.width();
1377 int h = bitmap.height();
1378
1379 GrTexture* texture;
1380 // draw sprite uses the default texture params
bsalomonbcf0a522014-10-08 08:40:09 -07001381 AutoBitmapTexture abt(fContext, bitmap, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001382
1383 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001384 // This bitmap will own the filtered result as a texture.
1385 SkBitmap filteredBitmap;
1386
bsalomon49f085d2014-09-05 13:34:00 -07001387 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001388 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001389 SkMatrix matrix(*draw.fMatrix);
1390 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001391 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001392 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001393 // This cache is transient, and is freed (along with all its contained
1394 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001395 SkImageFilter::Context ctx(matrix, clipBounds, cache);
robertphillips6219e1f2014-10-20 08:12:04 -07001396 if (filter_texture(this, fContext, texture, filter, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001397 &offset)) {
1398 texture = (GrTexture*) filteredBitmap.getTexture();
1399 w = filteredBitmap.width();
1400 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001401 left += offset.x();
1402 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001403 } else {
1404 return;
1405 }
1406 }
1407
1408 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001409 grPaint.addColorTextureProcessor(texture, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001410
dandov9de5b512014-06-10 14:38:28 -07001411 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1412 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001413
1414 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001415 SkRect::MakeXYWH(SkIntToScalar(left),
1416 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001417 SkIntToScalar(w),
1418 SkIntToScalar(h)),
1419 SkRect::MakeXYWH(0,
1420 0,
1421 SK_Scalar1 * w / texture->width(),
1422 SK_Scalar1 * h / texture->height()));
1423}
1424
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001425void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001426 const SkRect* src, const SkRect& dst,
1427 const SkPaint& paint,
1428 SkCanvas::DrawBitmapRectFlags flags) {
1429 SkMatrix matrix;
1430 SkRect bitmapBounds, tmpSrc;
1431
1432 bitmapBounds.set(0, 0,
1433 SkIntToScalar(bitmap.width()),
1434 SkIntToScalar(bitmap.height()));
1435
1436 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001437 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001438 tmpSrc = *src;
1439 } else {
1440 tmpSrc = bitmapBounds;
1441 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001442
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001443 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1444
1445 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001446 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001447 if (!bitmapBounds.contains(tmpSrc)) {
1448 if (!tmpSrc.intersect(bitmapBounds)) {
1449 return; // nothing to draw
1450 }
1451 }
1452 }
1453
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001454 SkRect tmpDst;
1455 matrix.mapRect(&tmpDst, tmpSrc);
1456
1457 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1458 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1459 // Translate so that tempDst's top left is at the origin.
1460 matrix = *origDraw.fMatrix;
1461 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1462 draw.writable()->fMatrix = &matrix;
1463 }
1464 SkSize dstSize;
1465 dstSize.fWidth = tmpDst.width();
1466 dstSize.fHeight = tmpDst.height();
1467
1468 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001469}
1470
1471void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1472 int x, int y, const SkPaint& paint) {
1473 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001474 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001475 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1476 if (dev->fNeedClear) {
1477 // TODO: could check here whether we really need to draw at all
1478 dev->clear(0x0);
1479 }
1480
1481 // drawDevice is defined to be in device coords.
1482 CHECK_SHOULD_DRAW(draw, true);
1483
1484 GrRenderTarget* devRT = dev->accessRenderTarget();
1485 GrTexture* devTex;
1486 if (NULL == (devTex = devRT->asTexture())) {
1487 return;
1488 }
1489
1490 const SkBitmap& bm = dev->accessBitmap(false);
1491 int w = bm.width();
1492 int h = bm.height();
1493
1494 SkImageFilter* filter = paint.getImageFilter();
1495 // This bitmap will own the filtered result as a texture.
1496 SkBitmap filteredBitmap;
1497
bsalomon49f085d2014-09-05 13:34:00 -07001498 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001499 SkIPoint offset = SkIPoint::Make(0, 0);
1500 SkMatrix matrix(*draw.fMatrix);
1501 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001502 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001503 // This cache is transient, and is freed (along with all its contained
1504 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001505 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001506 SkImageFilter::Context ctx(matrix, clipBounds, cache);
robertphillips6219e1f2014-10-20 08:12:04 -07001507 if (filter_texture(this, fContext, devTex, filter, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001508 &offset)) {
1509 devTex = filteredBitmap.getTexture();
1510 w = filteredBitmap.width();
1511 h = filteredBitmap.height();
1512 x += offset.fX;
1513 y += offset.fY;
1514 } else {
1515 return;
1516 }
1517 }
1518
1519 GrPaint grPaint;
joshualittb0a8a372014-09-23 09:50:21 -07001520 grPaint.addColorTextureProcessor(devTex, SkMatrix::I());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001521
dandov9de5b512014-06-10 14:38:28 -07001522 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1523 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001524
1525 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1526 SkIntToScalar(y),
1527 SkIntToScalar(w),
1528 SkIntToScalar(h));
1529
1530 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1531 // scratch texture).
1532 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1533 SK_Scalar1 * h / devTex->height());
1534
1535 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1536}
1537
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001538bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001539 return filter->canFilterImageGPU();
1540}
1541
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001542bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001543 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001544 SkBitmap* result, SkIPoint* offset) {
1545 // want explicitly our impl, so guard against a subclass of us overriding it
1546 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1547 return false;
1548 }
1549
1550 SkAutoLockPixels alp(src, !src.getTexture());
1551 if (!src.getTexture() && !src.readyToDraw()) {
1552 return false;
1553 }
1554
1555 GrTexture* texture;
1556 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1557 // must be pushed upstack.
bsalomonbcf0a522014-10-08 08:40:09 -07001558 AutoBitmapTexture abt(fContext, src, NULL, &texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001559
robertphillips6219e1f2014-10-20 08:12:04 -07001560 return filter_texture(this, fContext, texture, filter, ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001561}
1562
1563///////////////////////////////////////////////////////////////////////////////
1564
1565// must be in SkCanvas::VertexMode order
1566static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1567 kTriangles_GrPrimitiveType,
1568 kTriangleStrip_GrPrimitiveType,
1569 kTriangleFan_GrPrimitiveType,
1570};
1571
1572void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1573 int vertexCount, const SkPoint vertices[],
1574 const SkPoint texs[], const SkColor colors[],
1575 SkXfermode* xmode,
1576 const uint16_t indices[], int indexCount,
1577 const SkPaint& paint) {
1578 CHECK_SHOULD_DRAW(draw, false);
1579
egdanield78a1682014-07-09 10:41:26 -07001580 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001581
dandov32a311b2014-07-15 19:46:26 -07001582 const uint16_t* outIndices;
1583 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1584 GrPrimitiveType primType;
1585 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001586
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001587 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1588 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001589
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001590 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001591
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001592 SkPaint copy(paint);
1593 copy.setStyle(SkPaint::kStroke_Style);
1594 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001595
dandov32a311b2014-07-15 19:46:26 -07001596 // we ignore the shader if texs is null.
1597 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1598 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001599
dandov32a311b2014-07-15 19:46:26 -07001600 primType = kLines_GrPrimitiveType;
1601 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001602 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001603 switch (vmode) {
1604 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001605 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001606 break;
1607 case SkCanvas::kTriangleStrip_VertexMode:
1608 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001609 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001610 break;
1611 }
mtklein533eb782014-08-27 10:39:42 -07001612
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001613 VertState state(vertexCount, indices, indexCount);
1614 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001615
dandov32a311b2014-07-15 19:46:26 -07001616 //number of indices for lines per triangle with kLines
1617 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001618
dandov32a311b2014-07-15 19:46:26 -07001619 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1620 outIndices = outAlloc.get();
1621 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001622 int i = 0;
1623 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001624 auxIndices[i] = state.f0;
1625 auxIndices[i + 1] = state.f1;
1626 auxIndices[i + 2] = state.f1;
1627 auxIndices[i + 3] = state.f2;
1628 auxIndices[i + 4] = state.f2;
1629 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001630 i += 6;
1631 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001632 } else {
dandov32a311b2014-07-15 19:46:26 -07001633 outIndices = indices;
1634 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001635
dandov32a311b2014-07-15 19:46:26 -07001636 if (NULL == texs || NULL == paint.getShader()) {
1637 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1638 NULL == colors, &grPaint);
1639 } else {
1640 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
1641 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001642 }
1643
mtklein2583b622014-06-04 08:20:41 -07001644#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001645 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001646 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1647 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001648 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001649 }
1650 }
mtklein2583b622014-06-04 08:20:41 -07001651#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001652
1653 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001654 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001655 // need to convert byte order and from non-PM to PM
1656 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001657 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001658 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001659 color = colors[i];
1660 if (paint.getAlpha() != 255) {
1661 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1662 }
1663 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001664 }
1665 colors = convertedColors.get();
1666 }
1667 fContext->drawVertices(grPaint,
dandov32a311b2014-07-15 19:46:26 -07001668 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001669 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001670 vertices,
1671 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001672 colors,
dandov32a311b2014-07-15 19:46:26 -07001673 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001674 indexCount);
1675}
1676
1677///////////////////////////////////////////////////////////////////////////////
1678
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001679void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1680 size_t byteLength, SkScalar x, SkScalar y,
1681 const SkPaint& paint) {
1682 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -07001683 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001684
jvanverth8c27a182014-10-14 08:45:50 -07001685 GrPaint grPaint;
1686 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001687
jvanverth8c27a182014-10-14 08:45:50 -07001688 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001689
jvanverth8c27a182014-10-14 08:45:50 -07001690 if (!fTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y)) {
1691 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001692 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001693 }
1694}
1695
fmalita05c4a432014-09-29 06:29:53 -07001696void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1697 const SkScalar pos[], int scalarsPerPos,
1698 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001699 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001700 CHECK_SHOULD_DRAW(draw, false);
1701
jvanverth8c27a182014-10-14 08:45:50 -07001702 GrPaint grPaint;
1703 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001704
jvanverth8c27a182014-10-14 08:45:50 -07001705 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001706
jvanverth8c27a182014-10-14 08:45:50 -07001707 if (!fTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
1708 scalarsPerPos, offset)) {
1709 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001710 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001711 }
1712}
1713
1714void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1715 size_t len, const SkPath& path,
1716 const SkMatrix* m, const SkPaint& paint) {
1717 CHECK_SHOULD_DRAW(draw, false);
1718
1719 SkASSERT(draw.fDevice == this);
1720 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1721}
1722
1723///////////////////////////////////////////////////////////////////////////////
1724
1725bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1726 if (!paint.isLCDRenderText()) {
1727 // we're cool with the paint as is
1728 return false;
1729 }
1730
1731 if (paint.getShader() ||
1732 paint.getXfermode() || // unless its srcover
1733 paint.getMaskFilter() ||
1734 paint.getRasterizer() ||
1735 paint.getColorFilter() ||
1736 paint.getPathEffect() ||
1737 paint.isFakeBoldText() ||
1738 paint.getStyle() != SkPaint::kFill_Style) {
reed3375c802014-09-16 12:27:55 -07001739 // turn off lcd, but turn on kGenA8
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001740 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
reed3375c802014-09-16 12:27:55 -07001741 flags->fFlags |= SkPaint::kGenA8FromLCD_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001742 return true;
1743 }
1744 // we're cool with the paint as is
1745 return false;
1746}
1747
1748void SkGpuDevice::flush() {
1749 DO_DEFERRED_CLEAR();
bsalomon87a94eb2014-11-03 14:28:32 -08001750 fRenderTarget->prepareForExternalRead();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001751}
1752
1753///////////////////////////////////////////////////////////////////////////////
1754
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001755SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
bsalomonf2703d82014-10-28 14:33:06 -07001756 GrSurfaceDesc desc;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001757 desc.fConfig = fRenderTarget->config();
bsalomonf2703d82014-10-28 14:33:06 -07001758 desc.fFlags = kRenderTarget_GrSurfaceFlag;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001759 desc.fWidth = info.width();
1760 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001761 desc.fSampleCnt = fRenderTarget->numSamples();
1762
1763 SkAutoTUnref<GrTexture> texture;
1764 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001765 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001766
1767#if CACHE_COMPATIBLE_DEVICE_TEXTURES
hcm4396fa52014-10-27 21:43:30 -07001768 // layers are never draw in repeat modes, so we can request an approx
1769 // match and ignore any padding.
1770 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1771 GrContext::kApprox_ScratchTexMatch :
1772 GrContext::kExact_ScratchTexMatch;
bsalomone3059732014-10-14 11:47:22 -07001773 texture.reset(fContext->refScratchTexture(desc, match));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001774#else
1775 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1776#endif
bsalomon49f085d2014-09-05 13:34:00 -07001777 if (texture.get()) {
reed4a8126e2014-09-22 07:29:03 -07001778 return SkGpuDevice::Create(texture, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001779 } else {
tfarina38406c82014-10-31 07:11:12 -07001780 SkDebugf("---- failed to create compatible device texture [%d %d]\n",
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001781 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001782 return NULL;
1783 }
1784}
1785
reed4a8126e2014-09-22 07:29:03 -07001786SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1787 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001788}
1789
robertphillips9b14f262014-06-04 05:40:44 -07001790void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
robertphillipsd771f6b2014-07-22 10:18:06 -07001791 fContext->getLayerCache()->processDeletedPictures();
1792
bsalomon49f085d2014-09-05 13:34:00 -07001793 if (picture->fData.get() && !picture->fData->suitableForLayerOptimization()) {
robertphillipsc019ec42014-08-12 05:35:58 -07001794 return;
1795 }
1796
robertphillips6617d502014-08-18 09:39:34 -07001797 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001798
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001799 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
bsalomon49f085d2014-09-05 13:34:00 -07001800 if (existing) {
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001801 return;
1802 }
1803
robertphillipsd6283302014-08-27 11:53:28 -07001804 GPUOptimize(picture);
robertphillipsd771f6b2014-07-22 10:18:06 -07001805
1806 fContext->getLayerCache()->trackPicture(picture);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001807}
1808
robertphillips30d2cc62014-09-24 08:52:18 -07001809bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001810 const SkMatrix* matrix, const SkPaint* paint) {
1811 // todo: should handle these natively
1812 if (matrix || paint) {
1813 return false;
1814 }
1815
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001816 SkRect clipBounds;
robertphillips0c423322014-07-31 11:02:38 -07001817 if (!mainCanvas->getClipBounds(&clipBounds)) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001818 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001819 }
1820
robertphillipsfd61ed02014-10-28 07:21:44 -07001821 SkTDArray<GrHoistedLayer> atlasedNeedRendering, atlasedRecycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001822
robertphillipsfd61ed02014-10-28 07:21:44 -07001823 GrLayerHoister::FindLayersToAtlas(fContext, mainPicture,
1824 clipBounds,
1825 &atlasedNeedRendering, &atlasedRecycled);
1826
1827 GrLayerHoister::DrawLayersToAtlas(fContext, atlasedNeedRendering);
1828
1829 SkTDArray<GrHoistedLayer> needRendering, recycled;
1830
1831 GrLayerHoister::FindLayersToHoist(fContext, mainPicture,
1832 clipBounds,
1833 &needRendering, &recycled);
1834
1835 GrLayerHoister::DrawLayers(fContext, needRendering);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001836
robertphillips4aa6dfc2014-09-17 07:50:47 -07001837 GrReplacements replacements;
1838
robertphillipsfd61ed02014-10-28 07:21:44 -07001839 GrLayerHoister::ConvertLayersToReplacements(needRendering, &replacements);
1840 GrLayerHoister::ConvertLayersToReplacements(recycled, &replacements);
robertphillips4aa6dfc2014-09-17 07:50:47 -07001841
robertphillips64bf7672014-08-21 13:07:35 -07001842 // Render the entire picture using new layers
robertphillipsee6631e2014-09-29 05:32:49 -07001843 const SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1844
1845 GrRecordReplaceDraw(mainPicture, mainCanvas, &replacements, initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001846
robertphillipsfd61ed02014-10-28 07:21:44 -07001847 GrLayerHoister::UnlockLayers(fContext, needRendering);
1848 GrLayerHoister::UnlockLayers(fContext, recycled);
1849 GrLayerHoister::UnlockLayers(fContext, atlasedNeedRendering);
1850 GrLayerHoister::UnlockLayers(fContext, atlasedRecycled);
robertphillips64bf7672014-08-21 13:07:35 -07001851
1852 return true;
1853}
1854
senorblancobe129b22014-08-08 07:14:35 -07001855SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001856 // We always return a transient cache, so it is freed after each
1857 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001858 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001859}
reedf037e0b2014-10-30 11:34:15 -07001860
1861#endif