blob: 0912b3d28e3f4475568ff3bc4f9901dd3a549fb0 [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
senorblanco55b6d8b2014-07-30 11:26:46 -070046enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
47
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000048#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
49
50#if 0
51 extern bool (*gShouldDrawProc)();
52 #define CHECK_SHOULD_DRAW(draw, forceI) \
53 do { \
54 if (gShouldDrawProc && !gShouldDrawProc()) return; \
55 this->prepareDraw(draw, forceI); \
56 } while (0)
57#else
58 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
59#endif
60
61// This constant represents the screen alignment criterion in texels for
62// requiring texture domain clamping to prevent color bleeding when drawing
63// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000064#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000065
66#define DO_DEFERRED_CLEAR() \
67 do { \
68 if (fNeedClear) { \
69 this->clear(SK_ColorTRANSPARENT); \
70 } \
71 } while (false) \
72
73///////////////////////////////////////////////////////////////////////////////
74
75#define CHECK_FOR_ANNOTATION(paint) \
76 do { if (paint.getAnnotation()) { return; } } while (0)
77
78///////////////////////////////////////////////////////////////////////////////
79
bsalomonbcf0a522014-10-08 08:40:09 -070080// Helper for turning a bitmap into a texture. If the bitmap is GrTexture backed this
81// just accesses the backing GrTexture. Otherwise, it creates a cached texture
82// representation and releases it in the destructor.
83class AutoBitmapTexture : public SkNoncopyable {
Brian Salomon9323b8b2014-10-07 15:07:38 -040084public:
bsalomonbcf0a522014-10-08 08:40:09 -070085 AutoBitmapTexture() {}
robertphillipsdbe60742014-09-30 06:54:17 -070086
bsalomonbcf0a522014-10-08 08:40:09 -070087 AutoBitmapTexture(GrContext* context,
88 const SkBitmap& bitmap,
89 const GrTextureParams* params,
90 GrTexture** texture) {
Brian Salomon9323b8b2014-10-07 15:07:38 -040091 SkASSERT(texture);
bsalomonbcf0a522014-10-08 08:40:09 -070092 *texture = this->set(context, bitmap, params);
Brian Salomon9323b8b2014-10-07 15:07:38 -040093 }
94
bsalomonbcf0a522014-10-08 08:40:09 -070095 GrTexture* set(GrContext* context,
Brian Salomon9323b8b2014-10-07 15:07:38 -040096 const SkBitmap& bitmap,
97 const GrTextureParams* params) {
bsalomonbcf0a522014-10-08 08:40:09 -070098 // Either get the texture directly from the bitmap, or else use the cache and
99 // remember to unref it.
100 if (GrTexture* bmpTexture = bitmap.getTexture()) {
101 fTexture.reset(NULL);
102 return bmpTexture;
103 } else {
104 fTexture.reset(GrRefCachedBitmapTexture(context, bitmap, params));
105 return fTexture.get();
Brian Salomon9323b8b2014-10-07 15:07:38 -0400106 }
Brian Salomon9323b8b2014-10-07 15:07:38 -0400107 }
108
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000109private:
bsalomonbcf0a522014-10-08 08:40:09 -0700110 SkAutoTUnref<GrTexture> fTexture;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000111};
112
113///////////////////////////////////////////////////////////////////////////////
114
115struct GrSkDrawProcs : public SkDrawProcs {
116public:
117 GrContext* fContext;
118 GrTextContext* fTextContext;
119 GrFontScaler* fFontScaler; // cached in the skia glyphcache
120};
121
122///////////////////////////////////////////////////////////////////////////////
123
reed4a8126e2014-09-22 07:29:03 -0700124SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, const SkSurfaceProps& props, unsigned flags) {
bsalomon49f085d2014-09-05 13:34:00 -0700125 SkASSERT(surface);
bsalomon32d0b3b2014-08-29 07:50:23 -0700126 if (NULL == surface->asRenderTarget() || surface->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000127 return NULL;
128 }
reed4a8126e2014-09-22 07:29:03 -0700129 return SkNEW_ARGS(SkGpuDevice, (surface, props, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000130}
131
reed4a8126e2014-09-22 07:29:03 -0700132SkGpuDevice::SkGpuDevice(GrSurface* surface, const SkSurfaceProps& props, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000133
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000134 fDrawProcs = NULL;
135
bsalomon32d0b3b2014-08-29 07:50:23 -0700136 fContext = SkRef(surface->getContext());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000137
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000138 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000139
bsalomon32d0b3b2014-08-29 07:50:23 -0700140 fRenderTarget = SkRef(surface->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000141
bsalomonafbf2d62014-09-30 12:18:44 -0700142 SkImageInfo info = surface->surfacePriv().info();
bsalomonbcf0a522014-10-08 08:40:09 -0700143 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface));
bsalomonafbf2d62014-09-30 12:18:44 -0700144 fLegacyBitmap.setInfo(info);
reed89443ab2014-06-27 11:34:19 -0700145 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700146
reed4a8126e2014-09-22 07:29:03 -0700147 this->setPixelGeometry(props.pixelGeometry());
148
kkinnunenc6cb56f2014-06-24 00:12:27 -0700149 bool useDFFonts = !!(flags & kDFFonts_Flag);
jvanverth8c27a182014-10-14 08:45:50 -0700150 fTextContext = fContext->createTextContext(fRenderTarget, this->getLeakyProperties(), useDFFonts);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000151}
152
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000153SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
reed4a8126e2014-09-22 07:29:03 -0700154 const SkSurfaceProps& props, int sampleCount) {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000155 if (kUnknown_SkColorType == origInfo.colorType() ||
156 origInfo.width() < 0 || origInfo.height() < 0) {
157 return NULL;
158 }
159
reede5ea5002014-09-03 11:54:58 -0700160 SkColorType ct = origInfo.colorType();
161 SkAlphaType at = origInfo.alphaType();
robertphillipsa0537de2014-09-18 08:01:23 -0700162 // TODO: perhaps we can loosen this check now that colortype is more detailed
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000163 // e.g. can we support both RGBA and BGRA here?
reede5ea5002014-09-03 11:54:58 -0700164 if (kRGB_565_SkColorType == ct) {
165 at = kOpaque_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000166 } else {
reede5ea5002014-09-03 11:54:58 -0700167 ct = kN32_SkColorType;
168 if (kOpaque_SkAlphaType != at) {
169 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000170 }
171 }
reede5ea5002014-09-03 11:54:58 -0700172 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000173
174 GrTextureDesc desc;
175 desc.fFlags = kRenderTarget_GrTextureFlagBit;
176 desc.fWidth = info.width();
177 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000178 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000179 desc.fSampleCnt = sampleCount;
180
181 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
182 if (!texture.get()) {
183 return NULL;
184 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000185
reed4a8126e2014-09-22 07:29:03 -0700186 return SkNEW_ARGS(SkGpuDevice, (texture.get(), props));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000187}
188
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000189SkGpuDevice::~SkGpuDevice() {
190 if (fDrawProcs) {
191 delete fDrawProcs;
192 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000193
jvanverth8c27a182014-10-14 08:45:50 -0700194 delete fTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000195
196 // The GrContext takes a ref on the target. We don't want to cause the render
197 // target to be unnecessarily kept alive.
198 if (fContext->getRenderTarget() == fRenderTarget) {
199 fContext->setRenderTarget(NULL);
200 }
201
202 if (fContext->getClip() == &fClipData) {
203 fContext->setClip(NULL);
204 }
205
bsalomon32d0b3b2014-08-29 07:50:23 -0700206 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000207 fContext->unref();
208}
209
210///////////////////////////////////////////////////////////////////////////////
211
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000212bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
213 int x, int y) {
214 DO_DEFERRED_CLEAR();
215
216 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000217 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000218 if (kUnknown_GrPixelConfig == config) {
219 return false;
220 }
221
222 uint32_t flags = 0;
223 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
224 flags = GrContext::kUnpremul_PixelOpsFlag;
225 }
226 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
227 config, dstPixels, dstRowBytes, flags);
228}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000229
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000230bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
231 int x, int y) {
232 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000233 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000234 if (kUnknown_GrPixelConfig == config) {
235 return false;
236 }
237 uint32_t flags = 0;
238 if (kUnpremul_SkAlphaType == info.alphaType()) {
239 flags = GrContext::kUnpremul_PixelOpsFlag;
240 }
241 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
242
243 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700244 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000245
246 return true;
247}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000248
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000249const SkBitmap& SkGpuDevice::onAccessBitmap() {
250 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700251 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000252}
253
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000254void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
255 INHERITED::onAttachToCanvas(canvas);
256
257 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
258 fClipData.fClipStack = canvas->getClipStack();
259}
260
261void SkGpuDevice::onDetachFromCanvas() {
262 INHERITED::onDetachFromCanvas();
263 fClipData.fClipStack = NULL;
264}
265
266// call this every draw call, to ensure that the context reflects our state,
267// and not the state from some other canvas/device
268void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
bsalomon49f085d2014-09-05 13:34:00 -0700269 SkASSERT(fClipData.fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000270
271 fContext->setRenderTarget(fRenderTarget);
272
273 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
274
275 if (forceIdentity) {
276 fContext->setIdentityMatrix();
277 } else {
278 fContext->setMatrix(*draw.fMatrix);
279 }
280 fClipData.fOrigin = this->getOrigin();
281
282 fContext->setClip(&fClipData);
283
284 DO_DEFERRED_CLEAR();
285}
286
287GrRenderTarget* SkGpuDevice::accessRenderTarget() {
288 DO_DEFERRED_CLEAR();
289 return fRenderTarget;
290}
291
292///////////////////////////////////////////////////////////////////////////////
293
294SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
295SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
296SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
297SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
298SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
299 shader_type_mismatch);
300SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
301 shader_type_mismatch);
302SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
303SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
304
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000305///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000306
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000307void SkGpuDevice::clear(SkColor color) {
egdanield78a1682014-07-09 10:41:26 -0700308 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clear", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000309 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
310 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
311 fNeedClear = false;
312}
313
314void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
315 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700316 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000317
318 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000319 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000320
321 fContext->drawPaint(grPaint);
322}
323
324// must be in SkCanvas::PointMode order
325static const GrPrimitiveType gPointMode2PrimtiveType[] = {
326 kPoints_GrPrimitiveType,
327 kLines_GrPrimitiveType,
328 kLineStrip_GrPrimitiveType
329};
330
331void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
332 size_t count, const SkPoint pts[], const SkPaint& paint) {
333 CHECK_FOR_ANNOTATION(paint);
334 CHECK_SHOULD_DRAW(draw, false);
335
336 SkScalar width = paint.getStrokeWidth();
337 if (width < 0) {
338 return;
339 }
340
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000341 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700342 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
343 GrPaint grPaint;
344 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
345 SkPath path;
346 path.moveTo(pts[0]);
347 path.lineTo(pts[1]);
348 fContext->drawPath(grPaint, path, strokeInfo);
349 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000350 }
351
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000352 // we only handle hairlines and paints without path effects or mask filters,
353 // else we let the SkDraw call our drawPath()
354 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
355 draw.drawPoints(mode, count, pts, paint, true);
356 return;
357 }
358
359 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000360 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000361
362 fContext->drawVertices(grPaint,
363 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000364 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000365 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000366 NULL,
367 NULL,
368 NULL,
369 0);
370}
371
372///////////////////////////////////////////////////////////////////////////////
373
374void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
375 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700376 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
377
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000378 CHECK_FOR_ANNOTATION(paint);
379 CHECK_SHOULD_DRAW(draw, false);
380
381 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
382 SkScalar width = paint.getStrokeWidth();
383
384 /*
385 We have special code for hairline strokes, miter-strokes, bevel-stroke
386 and fills. Anything else we just call our path code.
387 */
388 bool usePath = doStroke && width > 0 &&
389 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
390 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
391 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700392
393 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000394 usePath = true;
395 }
egdanield58a0ba2014-06-11 10:30:05 -0700396
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000397 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
398#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
399 if (doStroke) {
400#endif
401 usePath = true;
402#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
403 } else {
404 usePath = !fContext->getMatrix().preservesRightAngles();
405 }
406#endif
407 }
408 // until we can both stroke and fill rectangles
409 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
410 usePath = true;
411 }
412
egdanield58a0ba2014-06-11 10:30:05 -0700413 GrStrokeInfo strokeInfo(paint);
414
415 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700416 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700417 usePath = true;
418 }
419
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000420 if (usePath) {
421 SkPath path;
422 path.addRect(rect);
423 this->drawPath(draw, path, paint, NULL, true);
424 return;
425 }
426
427 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000428 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400429
egdanield58a0ba2014-06-11 10:30:05 -0700430 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000431}
432
433///////////////////////////////////////////////////////////////////////////////
434
435void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
436 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700437 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000438 CHECK_FOR_ANNOTATION(paint);
439 CHECK_SHOULD_DRAW(draw, false);
440
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000441 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000442 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400443
egdanield58a0ba2014-06-11 10:30:05 -0700444 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000445 if (paint.getMaskFilter()) {
446 // try to hit the fast path for drawing filtered round rects
447
448 SkRRect devRRect;
449 if (rect.transform(fContext->getMatrix(), &devRRect)) {
450 if (devRRect.allCornersCircular()) {
451 SkRect maskRect;
452 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
453 draw.fClip->getBounds(),
454 fContext->getMatrix(),
455 &maskRect)) {
456 SkIRect finalIRect;
457 maskRect.roundOut(&finalIRect);
458 if (draw.fClip->quickReject(finalIRect)) {
459 // clipped out
460 return;
461 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000462 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700463 strokeInfo.getStrokeRec(),
464 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000465 return;
466 }
467 }
468
469 }
470 }
471
472 }
473
egdanield58a0ba2014-06-11 10:30:05 -0700474 bool usePath = false;
475
476 if (paint.getMaskFilter()) {
477 usePath = true;
478 } else {
479 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700480 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700481 usePath = true;
482 }
483 }
484
485
486 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000487 SkPath path;
488 path.addRRect(rect);
489 this->drawPath(draw, path, paint, NULL, true);
490 return;
491 }
Mike Klein744fb732014-06-23 15:13:26 -0400492
egdanield58a0ba2014-06-11 10:30:05 -0700493 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000494}
495
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000496void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
497 const SkRRect& inner, const SkPaint& paint) {
498 SkStrokeRec stroke(paint);
499 if (stroke.isFillStyle()) {
500
501 CHECK_FOR_ANNOTATION(paint);
502 CHECK_SHOULD_DRAW(draw, false);
503
504 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000505 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000506
507 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
508 fContext->drawDRRect(grPaint, outer, inner);
509 return;
510 }
511 }
512
513 SkPath path;
514 path.addRRect(outer);
515 path.addRRect(inner);
516 path.setFillType(SkPath::kEvenOdd_FillType);
517
518 this->drawPath(draw, path, paint, NULL, true);
519}
520
521
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000522/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000523
524void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
525 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700526 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000527 CHECK_FOR_ANNOTATION(paint);
528 CHECK_SHOULD_DRAW(draw, false);
529
egdanield58a0ba2014-06-11 10:30:05 -0700530 GrStrokeInfo strokeInfo(paint);
531
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000532 bool usePath = false;
533 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700534 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000535 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700536 } else {
537 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700538 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700539 usePath = true;
540 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541 }
542
543 if (usePath) {
544 SkPath path;
545 path.addOval(oval);
546 this->drawPath(draw, path, paint, NULL, true);
547 return;
548 }
549
550 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000551 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000552
egdanield58a0ba2014-06-11 10:30:05 -0700553 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000554}
555
556#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000557
558///////////////////////////////////////////////////////////////////////////////
559
560// helpers for applying mask filters
561namespace {
562
563// Draw a mask using the supplied paint. Since the coverage/geometry
564// is already burnt into the mask this boils down to a rect draw.
565// Return true if the mask was successfully drawn.
566bool draw_mask(GrContext* context, const SkRect& maskRect,
567 GrPaint* grp, GrTexture* mask) {
568 GrContext::AutoMatrix am;
569 if (!am.setIdentity(context, grp)) {
570 return false;
571 }
572
573 SkMatrix matrix;
574 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
575 matrix.postIDiv(mask->width(), mask->height());
576
joshualittb0a8a372014-09-23 09:50:21 -0700577 grp->addCoverageProcessor(GrSimpleTextureEffect::Create(mask, matrix))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000578 context->drawRect(*grp, maskRect);
579 return true;
580}
581
582bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700583 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000584 GrPaint* grp, SkPaint::Style style) {
585 SkMask srcM, dstM;
586
587 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
588 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
589 return false;
590 }
591 SkAutoMaskFreeImage autoSrc(srcM.fImage);
592
593 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
594 return false;
595 }
596 // this will free-up dstM when we're done (allocated in filterMask())
597 SkAutoMaskFreeImage autoDst(dstM.fImage);
598
599 if (clip.quickReject(dstM.fBounds)) {
600 return false;
601 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000602
603 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
604 // the current clip (and identity matrix) and GrPaint settings
605 GrTextureDesc desc;
606 desc.fWidth = dstM.fBounds.width();
607 desc.fHeight = dstM.fBounds.height();
608 desc.fConfig = kAlpha_8_GrPixelConfig;
609
610 GrAutoScratchTexture ast(context, desc);
611 GrTexture* texture = ast.texture();
612
613 if (NULL == texture) {
614 return false;
615 }
616 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
617 dstM.fImage, dstM.fRowBytes);
618
619 SkRect maskRect = SkRect::Make(dstM.fBounds);
620
621 return draw_mask(context, maskRect, grp, texture);
622}
623
624// Create a mask of 'devPath' and place the result in 'mask'. Return true on
625// success; false otherwise.
626bool create_mask_GPU(GrContext* context,
627 const SkRect& maskRect,
628 const SkPath& devPath,
egdanield58a0ba2014-06-11 10:30:05 -0700629 const GrStrokeInfo& strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000630 bool doAA,
derekff4555aa2014-10-06 12:19:12 -0700631 GrAutoScratchTexture* mask,
632 int SampleCnt) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000633 GrTextureDesc desc;
634 desc.fFlags = kRenderTarget_GrTextureFlagBit;
635 desc.fWidth = SkScalarCeilToInt(maskRect.width());
636 desc.fHeight = SkScalarCeilToInt(maskRect.height());
derekfe6efd392014-10-07 14:44:34 -0700637 desc.fSampleCnt = doAA ? SampleCnt : 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000638 // We actually only need A8, but it often isn't supported as a
639 // render target so default to RGBA_8888
640 desc.fConfig = kRGBA_8888_GrPixelConfig;
derekff4555aa2014-10-06 12:19:12 -0700641
642 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig,
643 desc.fSampleCnt > 0)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000644 desc.fConfig = kAlpha_8_GrPixelConfig;
645 }
646
647 mask->set(context, desc);
648 if (NULL == mask->texture()) {
649 return false;
650 }
651
652 GrTexture* maskTexture = mask->texture();
653 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
654
655 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
656 GrContext::AutoClip ac(context, clipRect);
657
658 context->clear(NULL, 0x0, true);
659
660 GrPaint tempPaint;
661 if (doAA) {
662 tempPaint.setAntiAlias(true);
663 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
664 // blend coeff of zero requires dual source blending support in order
665 // to properly blend partially covered pixels. This means the AA
666 // code path may not be taken. So we use a dst blend coeff of ISA. We
667 // could special case AA draws to a dst surface with known alpha=0 to
668 // use a zero dst coeff when dual source blending isn't available.
669 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
670 }
671
672 GrContext::AutoMatrix am;
673
674 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
675 SkMatrix translate;
676 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
677 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700678 context->drawPath(tempPaint, devPath, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000679 return true;
680}
681
682SkBitmap wrap_texture(GrTexture* texture) {
683 SkBitmap result;
bsalomonafbf2d62014-09-30 12:18:44 -0700684 result.setInfo(texture->surfacePriv().info());
reed6c225732014-06-09 19:52:07 -0700685 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000686 return result;
687}
688
689};
690
691void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
692 const SkPaint& paint, const SkMatrix* prePathMatrix,
693 bool pathIsMutable) {
694 CHECK_FOR_ANNOTATION(paint);
695 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700696 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000697
698 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000699 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000700
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000701 // If we have a prematrix, apply it to the path, optimizing for the case
702 // where the original path can in fact be modified in place (even though
703 // its parameter type is const).
704 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000705 SkTLazy<SkPath> tmpPath;
706 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000707
708 if (prePathMatrix) {
709 SkPath* result = pathPtr;
710
711 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000712 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000713 pathIsMutable = true;
714 }
715 // should I push prePathMatrix on our MV stack temporarily, instead
716 // of applying it here? See SkDraw.cpp
717 pathPtr->transform(*prePathMatrix, result);
718 pathPtr = result;
719 }
720 // at this point we're done with prePathMatrix
721 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
722
egdanield58a0ba2014-06-11 10:30:05 -0700723 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000724 SkPathEffect* pathEffect = paint.getPathEffect();
725 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700726 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
727 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000728 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000729 pathPtr = effectPath.get();
730 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700731 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000732 }
733
egdanield58a0ba2014-06-11 10:30:05 -0700734 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000735 if (paint.getMaskFilter()) {
736 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000737 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
738 if (stroke.applyToPath(strokedPath, *pathPtr)) {
739 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000740 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700741 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742 }
743 }
744
745 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000746 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000747
748 // transform the path into device space
749 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000750
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000751 SkRect maskRect;
752 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
753 draw.fClip->getBounds(),
754 fContext->getMatrix(),
755 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000756 // The context's matrix may change while creating the mask, so save the CTM here to
757 // pass to filterMaskGPU.
758 const SkMatrix ctm = fContext->getMatrix();
759
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000760 SkIRect finalIRect;
761 maskRect.roundOut(&finalIRect);
762 if (draw.fClip->quickReject(finalIRect)) {
763 // clipped out
764 return;
765 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000766
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000767 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000768 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000769 // the mask filter was able to draw itself directly, so there's nothing
770 // left to do.
771 return;
772 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773
774 GrAutoScratchTexture mask;
775
egdanield58a0ba2014-06-11 10:30:05 -0700776 if (create_mask_GPU(fContext, maskRect, *devPathPtr, strokeInfo,
derekff4555aa2014-10-06 12:19:12 -0700777 grPaint.isAntiAlias(), &mask, fRenderTarget->numSamples())) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000778 GrTexture* filtered;
779
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000780 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000781 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000782 // filterMaskGPU gives us ownership of a ref to the result
783 SkAutoTUnref<GrTexture> atu(filtered);
784
785 // If the scratch texture that we used as the filter src also holds the filter
786 // result then we must detach so that this texture isn't recycled for a later
787 // draw.
788 if (filtered == mask.texture()) {
789 mask.detach();
790 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
791 }
792
793 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,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001351 int w, int h, const SkImageFilter::Context& ctx,
1352 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);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001396 if (filter_texture(this, fContext, texture, filter, w, h, 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);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001507 if (filter_texture(this, fContext, devTex, filter, w, h, 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
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001560 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1561 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001562}
1563
1564///////////////////////////////////////////////////////////////////////////////
1565
1566// must be in SkCanvas::VertexMode order
1567static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1568 kTriangles_GrPrimitiveType,
1569 kTriangleStrip_GrPrimitiveType,
1570 kTriangleFan_GrPrimitiveType,
1571};
1572
1573void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1574 int vertexCount, const SkPoint vertices[],
1575 const SkPoint texs[], const SkColor colors[],
1576 SkXfermode* xmode,
1577 const uint16_t indices[], int indexCount,
1578 const SkPaint& paint) {
1579 CHECK_SHOULD_DRAW(draw, false);
1580
egdanield78a1682014-07-09 10:41:26 -07001581 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001582
dandov32a311b2014-07-15 19:46:26 -07001583 const uint16_t* outIndices;
1584 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1585 GrPrimitiveType primType;
1586 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001587
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001588 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1589 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001590
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001591 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001592
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001593 SkPaint copy(paint);
1594 copy.setStyle(SkPaint::kStroke_Style);
1595 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001596
dandov32a311b2014-07-15 19:46:26 -07001597 // we ignore the shader if texs is null.
1598 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1599 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001600
dandov32a311b2014-07-15 19:46:26 -07001601 primType = kLines_GrPrimitiveType;
1602 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001603 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001604 switch (vmode) {
1605 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001606 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001607 break;
1608 case SkCanvas::kTriangleStrip_VertexMode:
1609 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001610 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001611 break;
1612 }
mtklein533eb782014-08-27 10:39:42 -07001613
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001614 VertState state(vertexCount, indices, indexCount);
1615 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001616
dandov32a311b2014-07-15 19:46:26 -07001617 //number of indices for lines per triangle with kLines
1618 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001619
dandov32a311b2014-07-15 19:46:26 -07001620 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1621 outIndices = outAlloc.get();
1622 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001623 int i = 0;
1624 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001625 auxIndices[i] = state.f0;
1626 auxIndices[i + 1] = state.f1;
1627 auxIndices[i + 2] = state.f1;
1628 auxIndices[i + 3] = state.f2;
1629 auxIndices[i + 4] = state.f2;
1630 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001631 i += 6;
1632 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001633 } else {
dandov32a311b2014-07-15 19:46:26 -07001634 outIndices = indices;
1635 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001636
dandov32a311b2014-07-15 19:46:26 -07001637 if (NULL == texs || NULL == paint.getShader()) {
1638 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1639 NULL == colors, &grPaint);
1640 } else {
1641 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
1642 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001643 }
1644
mtklein2583b622014-06-04 08:20:41 -07001645#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001646 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001647 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1648 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001649 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001650 }
1651 }
mtklein2583b622014-06-04 08:20:41 -07001652#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001653
1654 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001655 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001656 // need to convert byte order and from non-PM to PM
1657 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001658 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001659 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001660 color = colors[i];
1661 if (paint.getAlpha() != 255) {
1662 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1663 }
1664 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001665 }
1666 colors = convertedColors.get();
1667 }
1668 fContext->drawVertices(grPaint,
dandov32a311b2014-07-15 19:46:26 -07001669 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001670 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001671 vertices,
1672 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001673 colors,
dandov32a311b2014-07-15 19:46:26 -07001674 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001675 indexCount);
1676}
1677
1678///////////////////////////////////////////////////////////////////////////////
1679
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001680void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1681 size_t byteLength, SkScalar x, SkScalar y,
1682 const SkPaint& paint) {
1683 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -07001684 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001685
jvanverth8c27a182014-10-14 08:45:50 -07001686 GrPaint grPaint;
1687 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001688
jvanverth8c27a182014-10-14 08:45:50 -07001689 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001690
jvanverth8c27a182014-10-14 08:45:50 -07001691 if (!fTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y)) {
1692 // this will just call our drawPath()
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001693 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001694 }
1695}
1696
fmalita05c4a432014-09-29 06:29:53 -07001697void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, size_t byteLength,
1698 const SkScalar pos[], int scalarsPerPos,
1699 const SkPoint& offset, const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001700 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001701 CHECK_SHOULD_DRAW(draw, false);
1702
jvanverth8c27a182014-10-14 08:45:50 -07001703 GrPaint grPaint;
1704 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001705
jvanverth8c27a182014-10-14 08:45:50 -07001706 SkDEBUGCODE(this->validate();)
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001707
jvanverth8c27a182014-10-14 08:45:50 -07001708 if (!fTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
1709 scalarsPerPos, offset)) {
1710 // this will just call our drawPath()
fmalita05c4a432014-09-29 06:29:53 -07001711 draw.drawPosText_asPaths((const char*)text, byteLength, pos, scalarsPerPos, offset, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001712 }
1713}
1714
1715void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1716 size_t len, const SkPath& path,
1717 const SkMatrix* m, const SkPaint& paint) {
1718 CHECK_SHOULD_DRAW(draw, false);
1719
1720 SkASSERT(draw.fDevice == this);
1721 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1722}
1723
1724///////////////////////////////////////////////////////////////////////////////
1725
1726bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1727 if (!paint.isLCDRenderText()) {
1728 // we're cool with the paint as is
1729 return false;
1730 }
1731
1732 if (paint.getShader() ||
1733 paint.getXfermode() || // unless its srcover
1734 paint.getMaskFilter() ||
1735 paint.getRasterizer() ||
1736 paint.getColorFilter() ||
1737 paint.getPathEffect() ||
1738 paint.isFakeBoldText() ||
1739 paint.getStyle() != SkPaint::kFill_Style) {
reed3375c802014-09-16 12:27:55 -07001740 // turn off lcd, but turn on kGenA8
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001741 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
reed3375c802014-09-16 12:27:55 -07001742 flags->fFlags |= SkPaint::kGenA8FromLCD_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001743 return true;
1744 }
1745 // we're cool with the paint as is
1746 return false;
1747}
1748
1749void SkGpuDevice::flush() {
1750 DO_DEFERRED_CLEAR();
1751 fContext->resolveRenderTarget(fRenderTarget);
1752}
1753
1754///////////////////////////////////////////////////////////////////////////////
1755
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001756SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001757 GrTextureDesc desc;
1758 desc.fConfig = fRenderTarget->config();
1759 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001760 desc.fWidth = info.width();
1761 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001762 desc.fSampleCnt = fRenderTarget->numSamples();
1763
1764 SkAutoTUnref<GrTexture> texture;
1765 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001766 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001767
1768#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1769 // layers are never draw in repeat modes, so we can request an approx
1770 // match and ignore any padding.
1771 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1772 GrContext::kApprox_ScratchTexMatch :
1773 GrContext::kExact_ScratchTexMatch;
1774 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1775#else
1776 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1777#endif
bsalomon49f085d2014-09-05 13:34:00 -07001778 if (texture.get()) {
reed4a8126e2014-09-22 07:29:03 -07001779 return SkGpuDevice::Create(texture, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001780 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001781 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1782 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001783 return NULL;
1784 }
1785}
1786
reed4a8126e2014-09-22 07:29:03 -07001787SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1788 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples(), &props);
reed@google.com76f10a32014-02-05 15:32:21 +00001789}
1790
robertphillips9b14f262014-06-04 05:40:44 -07001791void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
robertphillipsd771f6b2014-07-22 10:18:06 -07001792 fContext->getLayerCache()->processDeletedPictures();
1793
bsalomon49f085d2014-09-05 13:34:00 -07001794 if (picture->fData.get() && !picture->fData->suitableForLayerOptimization()) {
robertphillipsc019ec42014-08-12 05:35:58 -07001795 return;
1796 }
1797
robertphillips6617d502014-08-18 09:39:34 -07001798 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001799
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001800 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
bsalomon49f085d2014-09-05 13:34:00 -07001801 if (existing) {
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001802 return;
1803 }
1804
robertphillipsd6283302014-08-27 11:53:28 -07001805 GPUOptimize(picture);
robertphillipsd771f6b2014-07-22 10:18:06 -07001806
1807 fContext->getLayerCache()->trackPicture(picture);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001808}
1809
robertphillips30d2cc62014-09-24 08:52:18 -07001810bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* mainPicture,
reedd5fa1a42014-08-09 11:08:05 -07001811 const SkMatrix* matrix, const SkPaint* paint) {
1812 // todo: should handle these natively
1813 if (matrix || paint) {
1814 return false;
1815 }
1816
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001817 SkRect clipBounds;
robertphillips0c423322014-07-31 11:02:38 -07001818 if (!mainCanvas->getClipBounds(&clipBounds)) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001819 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001820 }
1821
robertphillipsd61ef012014-10-08 05:17:02 -07001822 SkTDArray<GrHoistedLayer> atlased, nonAtlased, recycled;
robertphillips1c4c5282014-09-18 12:03:15 -07001823
robertphillipsd61ef012014-10-08 05:17:02 -07001824 if (!GrLayerHoister::FindLayersToHoist(fContext, mainPicture, clipBounds,
1825 &atlased, &nonAtlased, &recycled)) {
robertphillips64bf7672014-08-21 13:07:35 -07001826 return false;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001827 }
1828
robertphillips4aa6dfc2014-09-17 07:50:47 -07001829 GrReplacements replacements;
1830
robertphillipsb5a97152014-09-30 11:33:02 -07001831 GrLayerHoister::DrawLayers(atlased, nonAtlased, recycled, &replacements);
robertphillips4aa6dfc2014-09-17 07:50:47 -07001832
robertphillips64bf7672014-08-21 13:07:35 -07001833 // Render the entire picture using new layers
robertphillipsee6631e2014-09-29 05:32:49 -07001834 const SkMatrix initialMatrix = mainCanvas->getTotalMatrix();
1835
1836 GrRecordReplaceDraw(mainPicture, mainCanvas, &replacements, initialMatrix, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001837
robertphillipsd61ef012014-10-08 05:17:02 -07001838 GrLayerHoister::UnlockLayers(fContext, atlased, nonAtlased, recycled);
robertphillips64bf7672014-08-21 13:07:35 -07001839
1840 return true;
1841}
1842
senorblancobe129b22014-08-08 07:14:35 -07001843SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001844 // We always return a transient cache, so it is freed after each
1845 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001846 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001847}