blob: b719f84278d35dca18398d756aef313531c9527e [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"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000024
25#include "SkGrTexturePixelRef.h"
26
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000027#include "SkDeviceImageFilterProxy.h"
28#include "SkDrawProcs.h"
29#include "SkGlyphCache.h"
30#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000031#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000032#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000033#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070034#include "SkPictureData.h"
robertphillips274b4ba2014-09-04 07:24:18 -070035#include "SkRecord.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000036#include "SkRRect.h"
37#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000038#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000039#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000040#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000041#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070042#include "SkXfermode.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000043#include "SkErrorInternals.h"
44
senorblanco55b6d8b2014-07-30 11:26:46 -070045enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
46
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000047#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
48
49#if 0
50 extern bool (*gShouldDrawProc)();
51 #define CHECK_SHOULD_DRAW(draw, forceI) \
52 do { \
53 if (gShouldDrawProc && !gShouldDrawProc()) return; \
54 this->prepareDraw(draw, forceI); \
55 } while (0)
56#else
57 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
58#endif
59
60// This constant represents the screen alignment criterion in texels for
61// requiring texture domain clamping to prevent color bleeding when drawing
62// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000063#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000064
65#define DO_DEFERRED_CLEAR() \
66 do { \
67 if (fNeedClear) { \
68 this->clear(SK_ColorTRANSPARENT); \
69 } \
70 } while (false) \
71
72///////////////////////////////////////////////////////////////////////////////
73
74#define CHECK_FOR_ANNOTATION(paint) \
75 do { if (paint.getAnnotation()) { return; } } while (0)
76
77///////////////////////////////////////////////////////////////////////////////
78
79
80class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
81public:
82 SkAutoCachedTexture()
83 : fDevice(NULL)
84 , fTexture(NULL) {
85 }
86
87 SkAutoCachedTexture(SkGpuDevice* device,
88 const SkBitmap& bitmap,
89 const GrTextureParams* params,
90 GrTexture** texture)
91 : fDevice(NULL)
92 , fTexture(NULL) {
bsalomon49f085d2014-09-05 13:34:00 -070093 SkASSERT(texture);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000094 *texture = this->set(device, bitmap, params);
95 }
96
97 ~SkAutoCachedTexture() {
bsalomon49f085d2014-09-05 13:34:00 -070098 if (fTexture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000099 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
100 }
101 }
102
103 GrTexture* set(SkGpuDevice* device,
104 const SkBitmap& bitmap,
105 const GrTextureParams* params) {
bsalomon49f085d2014-09-05 13:34:00 -0700106 if (fTexture) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000107 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
108 fTexture = NULL;
109 }
110 fDevice = device;
111 GrTexture* result = (GrTexture*)bitmap.getTexture();
112 if (NULL == result) {
113 // Cannot return the native texture so look it up in our cache
114 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
115 result = fTexture;
116 }
117 return result;
118 }
119
120private:
121 SkGpuDevice* fDevice;
122 GrTexture* fTexture;
123};
124
125///////////////////////////////////////////////////////////////////////////////
126
127struct GrSkDrawProcs : public SkDrawProcs {
128public:
129 GrContext* fContext;
130 GrTextContext* fTextContext;
131 GrFontScaler* fFontScaler; // cached in the skia glyphcache
132};
133
134///////////////////////////////////////////////////////////////////////////////
135
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000136SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
bsalomon49f085d2014-09-05 13:34:00 -0700137 SkASSERT(surface);
bsalomon32d0b3b2014-08-29 07:50:23 -0700138 if (NULL == surface->asRenderTarget() || surface->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000139 return NULL;
140 }
bsalomon32d0b3b2014-08-29 07:50:23 -0700141 return SkNEW_ARGS(SkGpuDevice, (surface, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000142}
143
bsalomon32d0b3b2014-08-29 07:50:23 -0700144SkGpuDevice::SkGpuDevice(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000145
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000146 fDrawProcs = NULL;
147
bsalomon32d0b3b2014-08-29 07:50:23 -0700148 fContext = SkRef(surface->getContext());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000149
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000150 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000151
bsalomon32d0b3b2014-08-29 07:50:23 -0700152 fRenderTarget = SkRef(surface->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000153
reed6c225732014-06-09 19:52:07 -0700154 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef,
155 (surface->info(), surface, SkToBool(flags & kCached_Flag)));
reed89443ab2014-06-27 11:34:19 -0700156 fLegacyBitmap.setInfo(surface->info());
157 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700158
159 bool useDFFonts = !!(flags & kDFFonts_Flag);
160 fMainTextContext = fContext->createTextContext(fRenderTarget, fLeakyProperties, useDFFonts);
161 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000162}
163
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000164SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
165 int sampleCount) {
166 if (kUnknown_SkColorType == origInfo.colorType() ||
167 origInfo.width() < 0 || origInfo.height() < 0) {
168 return NULL;
169 }
170
reede5ea5002014-09-03 11:54:58 -0700171 SkColorType ct = origInfo.colorType();
172 SkAlphaType at = origInfo.alphaType();
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000173 // TODO: perhas we can loosen this check now that colortype is more detailed
174 // e.g. can we support both RGBA and BGRA here?
reede5ea5002014-09-03 11:54:58 -0700175 if (kRGB_565_SkColorType == ct) {
176 at = kOpaque_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000177 } else {
reede5ea5002014-09-03 11:54:58 -0700178 ct = kN32_SkColorType;
179 if (kOpaque_SkAlphaType != at) {
180 at = kPremul_SkAlphaType; // force this setting
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000181 }
182 }
reede5ea5002014-09-03 11:54:58 -0700183 const SkImageInfo info = SkImageInfo::Make(origInfo.width(), origInfo.height(), ct, at);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000184
185 GrTextureDesc desc;
186 desc.fFlags = kRenderTarget_GrTextureFlagBit;
187 desc.fWidth = info.width();
188 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000189 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000190 desc.fSampleCnt = sampleCount;
191
192 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
193 if (!texture.get()) {
194 return NULL;
195 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000196
bsalomon32d0b3b2014-08-29 07:50:23 -0700197 return SkNEW_ARGS(SkGpuDevice, (texture.get()));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000198}
199
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000200SkGpuDevice::~SkGpuDevice() {
201 if (fDrawProcs) {
202 delete fDrawProcs;
203 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000204
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000205 delete fMainTextContext;
206 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000207
208 // The GrContext takes a ref on the target. We don't want to cause the render
209 // target to be unnecessarily kept alive.
210 if (fContext->getRenderTarget() == fRenderTarget) {
211 fContext->setRenderTarget(NULL);
212 }
213
214 if (fContext->getClip() == &fClipData) {
215 fContext->setClip(NULL);
216 }
217
bsalomon32d0b3b2014-08-29 07:50:23 -0700218 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000219 fContext->unref();
220}
221
222///////////////////////////////////////////////////////////////////////////////
223
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000224bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
225 int x, int y) {
226 DO_DEFERRED_CLEAR();
227
228 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000229 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000230 if (kUnknown_GrPixelConfig == config) {
231 return false;
232 }
233
234 uint32_t flags = 0;
235 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
236 flags = GrContext::kUnpremul_PixelOpsFlag;
237 }
238 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
239 config, dstPixels, dstRowBytes, flags);
240}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000241
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000242bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
243 int x, int y) {
244 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000245 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000246 if (kUnknown_GrPixelConfig == config) {
247 return false;
248 }
249 uint32_t flags = 0;
250 if (kUnpremul_SkAlphaType == info.alphaType()) {
251 flags = GrContext::kUnpremul_PixelOpsFlag;
252 }
253 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
254
255 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700256 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000257
258 return true;
259}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000260
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000261const SkBitmap& SkGpuDevice::onAccessBitmap() {
262 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700263 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000264}
265
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000266void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
267 INHERITED::onAttachToCanvas(canvas);
268
269 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
270 fClipData.fClipStack = canvas->getClipStack();
271}
272
273void SkGpuDevice::onDetachFromCanvas() {
274 INHERITED::onDetachFromCanvas();
275 fClipData.fClipStack = NULL;
276}
277
278// call this every draw call, to ensure that the context reflects our state,
279// and not the state from some other canvas/device
280void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
bsalomon49f085d2014-09-05 13:34:00 -0700281 SkASSERT(fClipData.fClipStack);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000282
283 fContext->setRenderTarget(fRenderTarget);
284
285 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
286
287 if (forceIdentity) {
288 fContext->setIdentityMatrix();
289 } else {
290 fContext->setMatrix(*draw.fMatrix);
291 }
292 fClipData.fOrigin = this->getOrigin();
293
294 fContext->setClip(&fClipData);
295
296 DO_DEFERRED_CLEAR();
297}
298
299GrRenderTarget* SkGpuDevice::accessRenderTarget() {
300 DO_DEFERRED_CLEAR();
301 return fRenderTarget;
302}
303
304///////////////////////////////////////////////////////////////////////////////
305
306SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
307SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
308SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
309SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
310SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
311 shader_type_mismatch);
312SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
313 shader_type_mismatch);
314SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
315SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
316
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000317///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000318
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000319void SkGpuDevice::clear(SkColor color) {
egdanield78a1682014-07-09 10:41:26 -0700320 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clear", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000321 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
322 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
323 fNeedClear = false;
324}
325
326void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
327 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700328 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000329
330 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000331 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000332
333 fContext->drawPaint(grPaint);
334}
335
336// must be in SkCanvas::PointMode order
337static const GrPrimitiveType gPointMode2PrimtiveType[] = {
338 kPoints_GrPrimitiveType,
339 kLines_GrPrimitiveType,
340 kLineStrip_GrPrimitiveType
341};
342
343void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
344 size_t count, const SkPoint pts[], const SkPaint& paint) {
345 CHECK_FOR_ANNOTATION(paint);
346 CHECK_SHOULD_DRAW(draw, false);
347
348 SkScalar width = paint.getStrokeWidth();
349 if (width < 0) {
350 return;
351 }
352
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000353 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700354 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
355 GrPaint grPaint;
356 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
357 SkPath path;
358 path.moveTo(pts[0]);
359 path.lineTo(pts[1]);
360 fContext->drawPath(grPaint, path, strokeInfo);
361 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000362 }
363
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000364 // we only handle hairlines and paints without path effects or mask filters,
365 // else we let the SkDraw call our drawPath()
366 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
367 draw.drawPoints(mode, count, pts, paint, true);
368 return;
369 }
370
371 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000372 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000373
374 fContext->drawVertices(grPaint,
375 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000376 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000377 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000378 NULL,
379 NULL,
380 NULL,
381 0);
382}
383
384///////////////////////////////////////////////////////////////////////////////
385
386void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
387 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700388 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
389
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000390 CHECK_FOR_ANNOTATION(paint);
391 CHECK_SHOULD_DRAW(draw, false);
392
393 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
394 SkScalar width = paint.getStrokeWidth();
395
396 /*
397 We have special code for hairline strokes, miter-strokes, bevel-stroke
398 and fills. Anything else we just call our path code.
399 */
400 bool usePath = doStroke && width > 0 &&
401 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
402 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
403 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700404
405 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000406 usePath = true;
407 }
egdanield58a0ba2014-06-11 10:30:05 -0700408
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000409 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
410#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
411 if (doStroke) {
412#endif
413 usePath = true;
414#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
415 } else {
416 usePath = !fContext->getMatrix().preservesRightAngles();
417 }
418#endif
419 }
420 // until we can both stroke and fill rectangles
421 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
422 usePath = true;
423 }
424
egdanield58a0ba2014-06-11 10:30:05 -0700425 GrStrokeInfo strokeInfo(paint);
426
427 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700428 if (!usePath && pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700429 usePath = true;
430 }
431
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000432 if (usePath) {
433 SkPath path;
434 path.addRect(rect);
435 this->drawPath(draw, path, paint, NULL, true);
436 return;
437 }
438
439 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000440 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400441
egdanield58a0ba2014-06-11 10:30:05 -0700442 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000443}
444
445///////////////////////////////////////////////////////////////////////////////
446
447void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
448 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700449 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000450 CHECK_FOR_ANNOTATION(paint);
451 CHECK_SHOULD_DRAW(draw, false);
452
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000453 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000454 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400455
egdanield58a0ba2014-06-11 10:30:05 -0700456 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000457 if (paint.getMaskFilter()) {
458 // try to hit the fast path for drawing filtered round rects
459
460 SkRRect devRRect;
461 if (rect.transform(fContext->getMatrix(), &devRRect)) {
462 if (devRRect.allCornersCircular()) {
463 SkRect maskRect;
464 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
465 draw.fClip->getBounds(),
466 fContext->getMatrix(),
467 &maskRect)) {
468 SkIRect finalIRect;
469 maskRect.roundOut(&finalIRect);
470 if (draw.fClip->quickReject(finalIRect)) {
471 // clipped out
472 return;
473 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000474 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700475 strokeInfo.getStrokeRec(),
476 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000477 return;
478 }
479 }
480
481 }
482 }
483
484 }
485
egdanield58a0ba2014-06-11 10:30:05 -0700486 bool usePath = false;
487
488 if (paint.getMaskFilter()) {
489 usePath = true;
490 } else {
491 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700492 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700493 usePath = true;
494 }
495 }
496
497
498 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000499 SkPath path;
500 path.addRRect(rect);
501 this->drawPath(draw, path, paint, NULL, true);
502 return;
503 }
Mike Klein744fb732014-06-23 15:13:26 -0400504
egdanield58a0ba2014-06-11 10:30:05 -0700505 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000506}
507
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000508void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
509 const SkRRect& inner, const SkPaint& paint) {
510 SkStrokeRec stroke(paint);
511 if (stroke.isFillStyle()) {
512
513 CHECK_FOR_ANNOTATION(paint);
514 CHECK_SHOULD_DRAW(draw, false);
515
516 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000517 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000518
519 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
520 fContext->drawDRRect(grPaint, outer, inner);
521 return;
522 }
523 }
524
525 SkPath path;
526 path.addRRect(outer);
527 path.addRRect(inner);
528 path.setFillType(SkPath::kEvenOdd_FillType);
529
530 this->drawPath(draw, path, paint, NULL, true);
531}
532
533
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000534/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000535
536void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
537 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700538 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000539 CHECK_FOR_ANNOTATION(paint);
540 CHECK_SHOULD_DRAW(draw, false);
541
egdanield58a0ba2014-06-11 10:30:05 -0700542 GrStrokeInfo strokeInfo(paint);
543
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000544 bool usePath = false;
545 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700546 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000547 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700548 } else {
549 const SkPathEffect* pe = paint.getPathEffect();
bsalomon49f085d2014-09-05 13:34:00 -0700550 if (pe && !strokeInfo.isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700551 usePath = true;
552 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000553 }
554
555 if (usePath) {
556 SkPath path;
557 path.addOval(oval);
558 this->drawPath(draw, path, paint, NULL, true);
559 return;
560 }
561
562 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000563 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000564
egdanield58a0ba2014-06-11 10:30:05 -0700565 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000566}
567
568#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000569
570///////////////////////////////////////////////////////////////////////////////
571
572// helpers for applying mask filters
573namespace {
574
575// Draw a mask using the supplied paint. Since the coverage/geometry
576// is already burnt into the mask this boils down to a rect draw.
577// Return true if the mask was successfully drawn.
578bool draw_mask(GrContext* context, const SkRect& maskRect,
579 GrPaint* grp, GrTexture* mask) {
580 GrContext::AutoMatrix am;
581 if (!am.setIdentity(context, grp)) {
582 return false;
583 }
584
585 SkMatrix matrix;
586 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
587 matrix.postIDiv(mask->width(), mask->height());
588
589 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
590 context->drawRect(*grp, maskRect);
591 return true;
592}
593
594bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700595 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000596 GrPaint* grp, SkPaint::Style style) {
597 SkMask srcM, dstM;
598
599 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
600 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
601 return false;
602 }
603 SkAutoMaskFreeImage autoSrc(srcM.fImage);
604
605 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
606 return false;
607 }
608 // this will free-up dstM when we're done (allocated in filterMask())
609 SkAutoMaskFreeImage autoDst(dstM.fImage);
610
611 if (clip.quickReject(dstM.fBounds)) {
612 return false;
613 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000614
615 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
616 // the current clip (and identity matrix) and GrPaint settings
617 GrTextureDesc desc;
618 desc.fWidth = dstM.fBounds.width();
619 desc.fHeight = dstM.fBounds.height();
620 desc.fConfig = kAlpha_8_GrPixelConfig;
621
622 GrAutoScratchTexture ast(context, desc);
623 GrTexture* texture = ast.texture();
624
625 if (NULL == texture) {
626 return false;
627 }
628 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
629 dstM.fImage, dstM.fRowBytes);
630
631 SkRect maskRect = SkRect::Make(dstM.fBounds);
632
633 return draw_mask(context, maskRect, grp, texture);
634}
635
636// Create a mask of 'devPath' and place the result in 'mask'. Return true on
637// success; false otherwise.
638bool create_mask_GPU(GrContext* context,
639 const SkRect& maskRect,
640 const SkPath& devPath,
egdanield58a0ba2014-06-11 10:30:05 -0700641 const GrStrokeInfo& strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000642 bool doAA,
643 GrAutoScratchTexture* mask) {
644 GrTextureDesc desc;
645 desc.fFlags = kRenderTarget_GrTextureFlagBit;
646 desc.fWidth = SkScalarCeilToInt(maskRect.width());
647 desc.fHeight = SkScalarCeilToInt(maskRect.height());
648 // We actually only need A8, but it often isn't supported as a
649 // render target so default to RGBA_8888
650 desc.fConfig = kRGBA_8888_GrPixelConfig;
651 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
652 desc.fConfig = kAlpha_8_GrPixelConfig;
653 }
654
655 mask->set(context, desc);
656 if (NULL == mask->texture()) {
657 return false;
658 }
659
660 GrTexture* maskTexture = mask->texture();
661 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
662
663 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
664 GrContext::AutoClip ac(context, clipRect);
665
666 context->clear(NULL, 0x0, true);
667
668 GrPaint tempPaint;
669 if (doAA) {
670 tempPaint.setAntiAlias(true);
671 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
672 // blend coeff of zero requires dual source blending support in order
673 // to properly blend partially covered pixels. This means the AA
674 // code path may not be taken. So we use a dst blend coeff of ISA. We
675 // could special case AA draws to a dst surface with known alpha=0 to
676 // use a zero dst coeff when dual source blending isn't available.
677 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
678 }
679
680 GrContext::AutoMatrix am;
681
682 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
683 SkMatrix translate;
684 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
685 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700686 context->drawPath(tempPaint, devPath, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000687 return true;
688}
689
690SkBitmap wrap_texture(GrTexture* texture) {
691 SkBitmap result;
reed6c225732014-06-09 19:52:07 -0700692 result.setInfo(texture->info());
693 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000694 return result;
695}
696
697};
698
699void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
700 const SkPaint& paint, const SkMatrix* prePathMatrix,
701 bool pathIsMutable) {
702 CHECK_FOR_ANNOTATION(paint);
703 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700704 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000705
706 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000707 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000708
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000709 // If we have a prematrix, apply it to the path, optimizing for the case
710 // where the original path can in fact be modified in place (even though
711 // its parameter type is const).
712 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000713 SkTLazy<SkPath> tmpPath;
714 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000715
716 if (prePathMatrix) {
717 SkPath* result = pathPtr;
718
719 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000720 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000721 pathIsMutable = true;
722 }
723 // should I push prePathMatrix on our MV stack temporarily, instead
724 // of applying it here? See SkDraw.cpp
725 pathPtr->transform(*prePathMatrix, result);
726 pathPtr = result;
727 }
728 // at this point we're done with prePathMatrix
729 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
730
egdanield58a0ba2014-06-11 10:30:05 -0700731 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000732 SkPathEffect* pathEffect = paint.getPathEffect();
733 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700734 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
735 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000736 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000737 pathPtr = effectPath.get();
738 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700739 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000740 }
741
egdanield58a0ba2014-06-11 10:30:05 -0700742 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000743 if (paint.getMaskFilter()) {
744 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000745 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
746 if (stroke.applyToPath(strokedPath, *pathPtr)) {
747 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000748 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700749 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000750 }
751 }
752
753 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000754 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000755
756 // transform the path into device space
757 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000758
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000759 SkRect maskRect;
760 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
761 draw.fClip->getBounds(),
762 fContext->getMatrix(),
763 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000764 // The context's matrix may change while creating the mask, so save the CTM here to
765 // pass to filterMaskGPU.
766 const SkMatrix ctm = fContext->getMatrix();
767
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000768 SkIRect finalIRect;
769 maskRect.roundOut(&finalIRect);
770 if (draw.fClip->quickReject(finalIRect)) {
771 // clipped out
772 return;
773 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000774
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000775 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000776 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000777 // the mask filter was able to draw itself directly, so there's nothing
778 // left to do.
779 return;
780 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000781
782 GrAutoScratchTexture mask;
783
egdanield58a0ba2014-06-11 10:30:05 -0700784 if (create_mask_GPU(fContext, maskRect, *devPathPtr, strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000785 grPaint.isAntiAlias(), &mask)) {
786 GrTexture* filtered;
787
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000788 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000789 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000790 // filterMaskGPU gives us ownership of a ref to the result
791 SkAutoTUnref<GrTexture> atu(filtered);
792
793 // If the scratch texture that we used as the filter src also holds the filter
794 // result then we must detach so that this texture isn't recycled for a later
795 // draw.
796 if (filtered == mask.texture()) {
797 mask.detach();
798 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
799 }
800
801 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
802 // This path is completely drawn
803 return;
804 }
805 }
806 }
807 }
808
809 // draw the mask on the CPU - this is a fallthrough path in case the
810 // GPU path fails
811 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
812 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700813 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
814 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000815 return;
816 }
817
egdanield58a0ba2014-06-11 10:30:05 -0700818 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000819}
820
821static const int kBmpSmallTileSize = 1 << 10;
822
823static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
824 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
825 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
826 return tilesX * tilesY;
827}
828
829static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
830 if (maxTileSize <= kBmpSmallTileSize) {
831 return maxTileSize;
832 }
833
834 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
835 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
836
837 maxTileTotalTileSize *= maxTileSize * maxTileSize;
838 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
839
840 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
841 return kBmpSmallTileSize;
842 } else {
843 return maxTileSize;
844 }
845}
846
847// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
848// pixels from the bitmap are necessary.
849static void determine_clipped_src_rect(const GrContext* context,
850 const SkBitmap& bitmap,
851 const SkRect* srcRectPtr,
852 SkIRect* clippedSrcIRect) {
853 const GrClipData* clip = context->getClip();
854 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
855 SkMatrix inv;
856 if (!context->getMatrix().invert(&inv)) {
857 clippedSrcIRect->setEmpty();
858 return;
859 }
860 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
861 inv.mapRect(&clippedSrcRect);
bsalomon49f085d2014-09-05 13:34:00 -0700862 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000863 // we've setup src space 0,0 to map to the top left of the src rect.
864 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000865 if (!clippedSrcRect.intersect(*srcRectPtr)) {
866 clippedSrcIRect->setEmpty();
867 return;
868 }
869 }
870 clippedSrcRect.roundOut(clippedSrcIRect);
871 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
872 if (!clippedSrcIRect->intersect(bmpBounds)) {
873 clippedSrcIRect->setEmpty();
874 }
875}
876
877bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
878 const GrTextureParams& params,
879 const SkRect* srcRectPtr,
880 int maxTileSize,
881 int* tileSize,
882 SkIRect* clippedSrcRect) const {
883 // if bitmap is explictly texture backed then just use the texture
bsalomon49f085d2014-09-05 13:34:00 -0700884 if (bitmap.getTexture()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000885 return false;
886 }
887
888 // if it's larger than the max tile size, then we have no choice but tiling.
889 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
890 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
891 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
892 return true;
893 }
894
895 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
896 return false;
897 }
898
899 // if the entire texture is already in our cache then no reason to tile it
900 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
901 return false;
902 }
903
904 // At this point we know we could do the draw by uploading the entire bitmap
905 // as a texture. However, if the texture would be large compared to the
906 // cache size and we don't require most of it for this draw then tile to
907 // reduce the amount of upload and cache spill.
908
909 // assumption here is that sw bitmap size is a good proxy for its size as
910 // a texture
911 size_t bmpSize = bitmap.getSize();
912 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000913 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000914 if (bmpSize < cacheSize / 2) {
915 return false;
916 }
917
918 // Figure out how much of the src we will need based on the src rect and clipping.
919 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
920 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
921 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
922 kBmpSmallTileSize * kBmpSmallTileSize;
923
924 return usedTileBytes < 2 * bmpSize;
925}
926
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000927void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000928 const SkBitmap& bitmap,
929 const SkMatrix& m,
930 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000931 SkMatrix concat;
932 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
933 if (!m.isIdentity()) {
934 concat.setConcat(*draw->fMatrix, m);
935 draw.writable()->fMatrix = &concat;
936 }
937 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000938}
939
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000940// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000941// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
942// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000943static inline void clamped_outset_with_offset(SkIRect* iRect,
944 int outset,
945 SkPoint* offset,
946 const SkIRect& clamp) {
947 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000948
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000949 int leftClampDelta = clamp.fLeft - iRect->fLeft;
950 if (leftClampDelta > 0) {
951 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000952 iRect->fLeft = clamp.fLeft;
953 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000954 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000955 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000956
957 int topClampDelta = clamp.fTop - iRect->fTop;
958 if (topClampDelta > 0) {
959 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000960 iRect->fTop = clamp.fTop;
961 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000962 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000963 }
964
965 if (iRect->fRight > clamp.fRight) {
966 iRect->fRight = clamp.fRight;
967 }
968 if (iRect->fBottom > clamp.fBottom) {
969 iRect->fBottom = clamp.fBottom;
970 }
971}
972
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000973static bool has_aligned_samples(const SkRect& srcRect,
974 const SkRect& transformedRect) {
975 // detect pixel disalignment
976 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
977 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
978 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
979 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
980 SkScalarAbs(transformedRect.width() - srcRect.width()) <
981 COLOR_BLEED_TOLERANCE &&
982 SkScalarAbs(transformedRect.height() - srcRect.height()) <
983 COLOR_BLEED_TOLERANCE) {
984 return true;
985 }
986 return false;
987}
988
989static bool may_color_bleed(const SkRect& srcRect,
990 const SkRect& transformedRect,
991 const SkMatrix& m) {
992 // Only gets called if has_aligned_samples returned false.
993 // So we can assume that sampling is axis aligned but not texel aligned.
994 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
995 SkRect innerSrcRect(srcRect), innerTransformedRect,
996 outerTransformedRect(transformedRect);
997 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
998 m.mapRect(&innerTransformedRect, innerSrcRect);
999
1000 // The gap between outerTransformedRect and innerTransformedRect
1001 // represents the projection of the source border area, which is
1002 // problematic for color bleeding. We must check whether any
1003 // destination pixels sample the border area.
1004 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1005 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1006 SkIRect outer, inner;
1007 outerTransformedRect.round(&outer);
1008 innerTransformedRect.round(&inner);
1009 // If the inner and outer rects round to the same result, it means the
1010 // border does not overlap any pixel centers. Yay!
1011 return inner != outer;
1012}
1013
1014static bool needs_texture_domain(const SkBitmap& bitmap,
1015 const SkRect& srcRect,
1016 GrTextureParams &params,
1017 const SkMatrix& contextMatrix,
1018 bool bicubic) {
1019 bool needsTextureDomain = false;
1020
1021 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1022 // Need texture domain if drawing a sub rect
1023 needsTextureDomain = srcRect.width() < bitmap.width() ||
1024 srcRect.height() < bitmap.height();
1025 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1026 // sampling is axis-aligned
1027 SkRect transformedRect;
1028 contextMatrix.mapRect(&transformedRect, srcRect);
1029
1030 if (has_aligned_samples(srcRect, transformedRect)) {
1031 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1032 needsTextureDomain = false;
1033 } else {
1034 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1035 }
1036 }
1037 }
1038 return needsTextureDomain;
1039}
1040
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001041void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1042 const SkBitmap& bitmap,
1043 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001044 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001045 const SkPaint& paint,
1046 SkCanvas::DrawBitmapRectFlags flags) {
1047 CHECK_SHOULD_DRAW(draw, false);
1048
1049 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001050 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001051 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1052 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001053 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001054 SkScalar w = SkIntToScalar(bitmap.width());
1055 SkScalar h = SkIntToScalar(bitmap.height());
1056 dstSize.fWidth = w;
1057 dstSize.fHeight = h;
1058 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001059 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001060 } else {
bsalomon49f085d2014-09-05 13:34:00 -07001061 SkASSERT(dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001062 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001063 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001064 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1065 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1066 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1067 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001068 }
1069
1070 if (paint.getMaskFilter()){
1071 // Convert the bitmap to a shader so that the rect can be drawn
1072 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001073 SkBitmap tmp; // subset of bitmap, if necessary
1074 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001075 SkMatrix localM;
bsalomon49f085d2014-09-05 13:34:00 -07001076 if (srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001077 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1078 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1079 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001080 // In bleed mode we position and trim the bitmap based on the src rect which is
1081 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1082 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1083 // compensate.
1084 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1085 SkIRect iSrc;
1086 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001087
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001088 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1089 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001090
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001091 if (!bitmap.extractSubset(&tmp, iSrc)) {
1092 return; // extraction failed
1093 }
1094 bitmapPtr = &tmp;
1095 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001096
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001097 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001098 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001099 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001100 } else {
1101 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001102 }
1103
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001104 SkPaint paintWithShader(paint);
1105 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001106 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001107 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1108 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001109
1110 return;
1111 }
1112
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001113 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1114 // the view matrix rather than a local matrix.
1115 SkMatrix m;
1116 m.setScale(dstSize.fWidth / srcRect.width(),
1117 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001118 fContext->concatMatrix(m);
1119
1120 GrTextureParams params;
1121 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1122 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001123
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001124 bool doBicubic = false;
1125
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001126 switch(paintFilterLevel) {
1127 case SkPaint::kNone_FilterLevel:
1128 textureFilterMode = GrTextureParams::kNone_FilterMode;
1129 break;
1130 case SkPaint::kLow_FilterLevel:
1131 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1132 break;
1133 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001134 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001135 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1136 } else {
1137 // Don't trigger MIP level generation unnecessarily.
1138 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1139 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001140 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001141 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001142 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001143 doBicubic =
1144 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001145 break;
1146 default:
1147 SkErrorInternals::SetError( kInvalidPaint_SkError,
1148 "Sorry, I don't understand the filtering "
1149 "mode you asked for. Falling back to "
1150 "MIPMaps.");
1151 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1152 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001153 }
1154
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001155 int tileFilterPad;
1156 if (doBicubic) {
1157 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1158 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1159 tileFilterPad = 0;
1160 } else {
1161 tileFilterPad = 1;
1162 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001163 params.setFilterMode(textureFilterMode);
1164
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001165 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001166 int tileSize;
1167
1168 SkIRect clippedSrcRect;
1169 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1170 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001171 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1172 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001173 } else {
1174 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001175 bool needsTextureDomain = needs_texture_domain(bitmap,
1176 srcRect,
1177 params,
1178 fContext->getMatrix(),
1179 doBicubic);
1180 this->internalDrawBitmap(bitmap,
1181 srcRect,
1182 params,
1183 paint,
1184 flags,
1185 doBicubic,
1186 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001187 }
1188}
1189
1190// Break 'bitmap' into several tiles to draw it since it has already
1191// been determined to be too large to fit in VRAM
1192void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1193 const SkRect& srcRect,
1194 const SkIRect& clippedSrcIRect,
1195 const GrTextureParams& params,
1196 const SkPaint& paint,
1197 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001198 int tileSize,
1199 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001200 // The following pixel lock is technically redundant, but it is desirable
1201 // to lock outside of the tile loop to prevent redecoding the whole image
1202 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1203 // is larger than the limit of the discardable memory pool.
1204 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001205 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1206
1207 int nx = bitmap.width() / tileSize;
1208 int ny = bitmap.height() / tileSize;
1209 for (int x = 0; x <= nx; x++) {
1210 for (int y = 0; y <= ny; y++) {
1211 SkRect tileR;
1212 tileR.set(SkIntToScalar(x * tileSize),
1213 SkIntToScalar(y * tileSize),
1214 SkIntToScalar((x + 1) * tileSize),
1215 SkIntToScalar((y + 1) * tileSize));
1216
1217 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1218 continue;
1219 }
1220
1221 if (!tileR.intersect(srcRect)) {
1222 continue;
1223 }
1224
1225 SkBitmap tmpB;
1226 SkIRect iTileR;
1227 tileR.roundOut(&iTileR);
1228 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1229 SkIntToScalar(iTileR.fTop));
1230
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001231 // Adjust the context matrix to draw at the right x,y in device space
1232 SkMatrix tmpM;
1233 GrContext::AutoMatrix am;
1234 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1235 am.setPreConcat(fContext, tmpM);
1236
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001237 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001238 SkIRect iClampRect;
1239
1240 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1241 // In bleed mode we want to always expand the tile on all edges
1242 // but stay within the bitmap bounds
1243 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1244 } else {
1245 // In texture-domain/clamp mode we only want to expand the
1246 // tile on edges interior to "srcRect" (i.e., we want to
1247 // not bleed across the original clamped edges)
1248 srcRect.roundOut(&iClampRect);
1249 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001250 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1251 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001252 }
1253
1254 if (bitmap.extractSubset(&tmpB, iTileR)) {
1255 // now offset it to make it "local" to our tmp bitmap
1256 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001257 GrTextureParams paramsTemp = params;
1258 bool needsTextureDomain = needs_texture_domain(bitmap,
1259 srcRect,
1260 paramsTemp,
1261 fContext->getMatrix(),
1262 bicubic);
1263 this->internalDrawBitmap(tmpB,
1264 tileR,
1265 paramsTemp,
1266 paint,
1267 flags,
1268 bicubic,
1269 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001270 }
1271 }
1272 }
1273}
1274
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001275
1276/*
1277 * This is called by drawBitmap(), which has to handle images that may be too
1278 * large to be represented by a single texture.
1279 *
1280 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1281 * and that non-texture portion of the GrPaint has already been setup.
1282 */
1283void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1284 const SkRect& srcRect,
1285 const GrTextureParams& params,
1286 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001287 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001288 bool bicubic,
1289 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001290 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1291 bitmap.height() <= fContext->getMaxTextureSize());
1292
1293 GrTexture* texture;
1294 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1295 if (NULL == texture) {
1296 return;
1297 }
1298
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001299 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001300 SkRect paintRect;
1301 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1302 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1303 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1304 SkScalarMul(srcRect.fTop, hInv),
1305 SkScalarMul(srcRect.fRight, wInv),
1306 SkScalarMul(srcRect.fBottom, hInv));
1307
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001308 SkRect textureDomain = SkRect::MakeEmpty();
bsalomon83d081a2014-07-08 09:56:10 -07001309 SkAutoTUnref<GrEffect> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001310 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001311 // Use a constrained texture domain to avoid color bleeding
1312 SkScalar left, top, right, bottom;
1313 if (srcRect.width() > SK_Scalar1) {
1314 SkScalar border = SK_ScalarHalf / texture->width();
1315 left = paintRect.left() + border;
1316 right = paintRect.right() - border;
1317 } else {
1318 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1319 }
1320 if (srcRect.height() > SK_Scalar1) {
1321 SkScalar border = SK_ScalarHalf / texture->height();
1322 top = paintRect.top() + border;
1323 bottom = paintRect.bottom() - border;
1324 } else {
1325 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1326 }
1327 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001328 if (bicubic) {
1329 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1330 } else {
1331 effect.reset(GrTextureDomainEffect::Create(texture,
1332 SkMatrix::I(),
1333 textureDomain,
1334 GrTextureDomain::kClamp_Mode,
1335 params.filterMode()));
1336 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001337 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001338 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1339 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1340 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001341 } else {
1342 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1343 }
1344
1345 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1346 // the rest from the SkPaint.
1347 GrPaint grPaint;
1348 grPaint.addColorEffect(effect);
reed0689d7b2014-06-14 05:30:20 -07001349 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001350 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1351 SkColor2GrColor(paint.getColor());
1352 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001353
bsalomon01c8da12014-08-04 09:21:30 -07001354 fContext->drawRectToRect(grPaint, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001355}
1356
1357static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001358 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001359 int w, int h, const SkImageFilter::Context& ctx,
1360 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001361 SkASSERT(filter);
1362 SkDeviceImageFilterProxy proxy(device);
1363
1364 if (filter->canFilterImageGPU()) {
1365 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1366 // filter. Also set the clip wide open and the matrix to identity.
1367 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001368 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001369 } else {
1370 return false;
1371 }
1372}
1373
1374void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1375 int left, int top, const SkPaint& paint) {
1376 // drawSprite is defined to be in device coords.
1377 CHECK_SHOULD_DRAW(draw, true);
1378
1379 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1380 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1381 return;
1382 }
1383
1384 int w = bitmap.width();
1385 int h = bitmap.height();
1386
1387 GrTexture* texture;
1388 // draw sprite uses the default texture params
1389 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1390
1391 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001392 // This bitmap will own the filtered result as a texture.
1393 SkBitmap filteredBitmap;
1394
bsalomon49f085d2014-09-05 13:34:00 -07001395 if (filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001396 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001397 SkMatrix matrix(*draw.fMatrix);
1398 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001399 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001400 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001401 // This cache is transient, and is freed (along with all its contained
1402 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001403 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001404 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001405 &offset)) {
1406 texture = (GrTexture*) filteredBitmap.getTexture();
1407 w = filteredBitmap.width();
1408 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001409 left += offset.x();
1410 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001411 } else {
1412 return;
1413 }
1414 }
1415
1416 GrPaint grPaint;
1417 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1418
dandov9de5b512014-06-10 14:38:28 -07001419 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1420 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001421
1422 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001423 SkRect::MakeXYWH(SkIntToScalar(left),
1424 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001425 SkIntToScalar(w),
1426 SkIntToScalar(h)),
1427 SkRect::MakeXYWH(0,
1428 0,
1429 SK_Scalar1 * w / texture->width(),
1430 SK_Scalar1 * h / texture->height()));
1431}
1432
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001433void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001434 const SkRect* src, const SkRect& dst,
1435 const SkPaint& paint,
1436 SkCanvas::DrawBitmapRectFlags flags) {
1437 SkMatrix matrix;
1438 SkRect bitmapBounds, tmpSrc;
1439
1440 bitmapBounds.set(0, 0,
1441 SkIntToScalar(bitmap.width()),
1442 SkIntToScalar(bitmap.height()));
1443
1444 // Compute matrix from the two rectangles
bsalomon49f085d2014-09-05 13:34:00 -07001445 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001446 tmpSrc = *src;
1447 } else {
1448 tmpSrc = bitmapBounds;
1449 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001450
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001451 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1452
1453 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
bsalomon49f085d2014-09-05 13:34:00 -07001454 if (src) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001455 if (!bitmapBounds.contains(tmpSrc)) {
1456 if (!tmpSrc.intersect(bitmapBounds)) {
1457 return; // nothing to draw
1458 }
1459 }
1460 }
1461
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001462 SkRect tmpDst;
1463 matrix.mapRect(&tmpDst, tmpSrc);
1464
1465 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1466 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1467 // Translate so that tempDst's top left is at the origin.
1468 matrix = *origDraw.fMatrix;
1469 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1470 draw.writable()->fMatrix = &matrix;
1471 }
1472 SkSize dstSize;
1473 dstSize.fWidth = tmpDst.width();
1474 dstSize.fHeight = tmpDst.height();
1475
1476 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001477}
1478
1479void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1480 int x, int y, const SkPaint& paint) {
1481 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001482 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001483 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1484 if (dev->fNeedClear) {
1485 // TODO: could check here whether we really need to draw at all
1486 dev->clear(0x0);
1487 }
1488
1489 // drawDevice is defined to be in device coords.
1490 CHECK_SHOULD_DRAW(draw, true);
1491
1492 GrRenderTarget* devRT = dev->accessRenderTarget();
1493 GrTexture* devTex;
1494 if (NULL == (devTex = devRT->asTexture())) {
1495 return;
1496 }
1497
1498 const SkBitmap& bm = dev->accessBitmap(false);
1499 int w = bm.width();
1500 int h = bm.height();
1501
1502 SkImageFilter* filter = paint.getImageFilter();
1503 // This bitmap will own the filtered result as a texture.
1504 SkBitmap filteredBitmap;
1505
bsalomon49f085d2014-09-05 13:34:00 -07001506 if (filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001507 SkIPoint offset = SkIPoint::Make(0, 0);
1508 SkMatrix matrix(*draw.fMatrix);
1509 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001510 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001511 // This cache is transient, and is freed (along with all its contained
1512 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001513 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001514 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001515 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001516 &offset)) {
1517 devTex = filteredBitmap.getTexture();
1518 w = filteredBitmap.width();
1519 h = filteredBitmap.height();
1520 x += offset.fX;
1521 y += offset.fY;
1522 } else {
1523 return;
1524 }
1525 }
1526
1527 GrPaint grPaint;
1528 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1529
dandov9de5b512014-06-10 14:38:28 -07001530 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1531 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001532
1533 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1534 SkIntToScalar(y),
1535 SkIntToScalar(w),
1536 SkIntToScalar(h));
1537
1538 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1539 // scratch texture).
1540 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1541 SK_Scalar1 * h / devTex->height());
1542
1543 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1544}
1545
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001546bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001547 return filter->canFilterImageGPU();
1548}
1549
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001550bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001551 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001552 SkBitmap* result, SkIPoint* offset) {
1553 // want explicitly our impl, so guard against a subclass of us overriding it
1554 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1555 return false;
1556 }
1557
1558 SkAutoLockPixels alp(src, !src.getTexture());
1559 if (!src.getTexture() && !src.readyToDraw()) {
1560 return false;
1561 }
1562
1563 GrTexture* texture;
1564 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1565 // must be pushed upstack.
1566 SkAutoCachedTexture act(this, src, NULL, &texture);
1567
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001568 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1569 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001570}
1571
1572///////////////////////////////////////////////////////////////////////////////
1573
1574// must be in SkCanvas::VertexMode order
1575static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1576 kTriangles_GrPrimitiveType,
1577 kTriangleStrip_GrPrimitiveType,
1578 kTriangleFan_GrPrimitiveType,
1579};
1580
1581void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1582 int vertexCount, const SkPoint vertices[],
1583 const SkPoint texs[], const SkColor colors[],
1584 SkXfermode* xmode,
1585 const uint16_t indices[], int indexCount,
1586 const SkPaint& paint) {
1587 CHECK_SHOULD_DRAW(draw, false);
1588
egdanield78a1682014-07-09 10:41:26 -07001589 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001590
dandov32a311b2014-07-15 19:46:26 -07001591 const uint16_t* outIndices;
1592 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1593 GrPrimitiveType primType;
1594 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001595
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001596 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1597 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001598
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001599 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001600
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001601 SkPaint copy(paint);
1602 copy.setStyle(SkPaint::kStroke_Style);
1603 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001604
dandov32a311b2014-07-15 19:46:26 -07001605 // we ignore the shader if texs is null.
1606 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1607 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001608
dandov32a311b2014-07-15 19:46:26 -07001609 primType = kLines_GrPrimitiveType;
1610 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001611 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001612 switch (vmode) {
1613 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001614 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001615 break;
1616 case SkCanvas::kTriangleStrip_VertexMode:
1617 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001618 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001619 break;
1620 }
mtklein533eb782014-08-27 10:39:42 -07001621
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001622 VertState state(vertexCount, indices, indexCount);
1623 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001624
dandov32a311b2014-07-15 19:46:26 -07001625 //number of indices for lines per triangle with kLines
1626 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001627
dandov32a311b2014-07-15 19:46:26 -07001628 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1629 outIndices = outAlloc.get();
1630 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001631 int i = 0;
1632 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001633 auxIndices[i] = state.f0;
1634 auxIndices[i + 1] = state.f1;
1635 auxIndices[i + 2] = state.f1;
1636 auxIndices[i + 3] = state.f2;
1637 auxIndices[i + 4] = state.f2;
1638 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001639 i += 6;
1640 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001641 } else {
dandov32a311b2014-07-15 19:46:26 -07001642 outIndices = indices;
1643 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001644
dandov32a311b2014-07-15 19:46:26 -07001645 if (NULL == texs || NULL == paint.getShader()) {
1646 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1647 NULL == colors, &grPaint);
1648 } else {
1649 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
1650 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001651 }
1652
mtklein2583b622014-06-04 08:20:41 -07001653#if 0
bsalomon49f085d2014-09-05 13:34:00 -07001654 if (xmode && texs && colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001655 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1656 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001657 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001658 }
1659 }
mtklein2583b622014-06-04 08:20:41 -07001660#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001661
1662 SkAutoSTMalloc<128, GrColor> convertedColors(0);
bsalomon49f085d2014-09-05 13:34:00 -07001663 if (colors) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001664 // need to convert byte order and from non-PM to PM
1665 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001666 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001667 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001668 color = colors[i];
1669 if (paint.getAlpha() != 255) {
1670 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1671 }
1672 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001673 }
1674 colors = convertedColors.get();
1675 }
1676 fContext->drawVertices(grPaint,
dandov32a311b2014-07-15 19:46:26 -07001677 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001678 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001679 vertices,
1680 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001681 colors,
dandov32a311b2014-07-15 19:46:26 -07001682 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001683 indexCount);
1684}
1685
1686///////////////////////////////////////////////////////////////////////////////
1687
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001688void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1689 size_t byteLength, SkScalar x, SkScalar y,
1690 const SkPaint& paint) {
1691 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -07001692 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001693
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001694 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001695 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001696 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001697
1698 SkDEBUGCODE(this->validate();)
1699
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001700 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1701 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001702 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001703 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001704
1705 SkDEBUGCODE(this->validate();)
1706
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001707 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001708 } else {
1709 // this guy will just call our drawPath()
1710 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001711 }
1712}
1713
1714void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1715 size_t byteLength, const SkScalar pos[],
1716 SkScalar constY, int scalarsPerPos,
1717 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001718 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001719 CHECK_SHOULD_DRAW(draw, false);
1720
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001721 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001722 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001723 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001724
1725 SkDEBUGCODE(this->validate();)
1726
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001727 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001728 constY, scalarsPerPos);
1729 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001730 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001731 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001732
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001733 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001734
1735 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001736 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001737 } else {
1738 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1739 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001740 }
1741}
1742
1743void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1744 size_t len, const SkPath& path,
1745 const SkMatrix* m, const SkPaint& paint) {
1746 CHECK_SHOULD_DRAW(draw, false);
1747
1748 SkASSERT(draw.fDevice == this);
1749 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1750}
1751
1752///////////////////////////////////////////////////////////////////////////////
1753
1754bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1755 if (!paint.isLCDRenderText()) {
1756 // we're cool with the paint as is
1757 return false;
1758 }
1759
1760 if (paint.getShader() ||
1761 paint.getXfermode() || // unless its srcover
1762 paint.getMaskFilter() ||
1763 paint.getRasterizer() ||
1764 paint.getColorFilter() ||
1765 paint.getPathEffect() ||
1766 paint.isFakeBoldText() ||
1767 paint.getStyle() != SkPaint::kFill_Style) {
1768 // turn off lcd
1769 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1770 flags->fHinting = paint.getHinting();
1771 return true;
1772 }
1773 // we're cool with the paint as is
1774 return false;
1775}
1776
1777void SkGpuDevice::flush() {
1778 DO_DEFERRED_CLEAR();
1779 fContext->resolveRenderTarget(fRenderTarget);
1780}
1781
1782///////////////////////////////////////////////////////////////////////////////
1783
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001784SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001785 GrTextureDesc desc;
1786 desc.fConfig = fRenderTarget->config();
1787 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001788 desc.fWidth = info.width();
1789 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001790 desc.fSampleCnt = fRenderTarget->numSamples();
1791
1792 SkAutoTUnref<GrTexture> texture;
1793 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001794 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001795
1796#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1797 // layers are never draw in repeat modes, so we can request an approx
1798 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001799 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001800 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1801 GrContext::kApprox_ScratchTexMatch :
1802 GrContext::kExact_ScratchTexMatch;
1803 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1804#else
1805 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1806#endif
bsalomon49f085d2014-09-05 13:34:00 -07001807 if (texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001808 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001809 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001810 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1811 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001812 return NULL;
1813 }
1814}
1815
reed@google.com76f10a32014-02-05 15:32:21 +00001816SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1817 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1818}
1819
robertphillips9b14f262014-06-04 05:40:44 -07001820void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
robertphillipsd771f6b2014-07-22 10:18:06 -07001821 fContext->getLayerCache()->processDeletedPictures();
1822
bsalomon49f085d2014-09-05 13:34:00 -07001823 if (picture->fData.get() && !picture->fData->suitableForLayerOptimization()) {
robertphillipsc019ec42014-08-12 05:35:58 -07001824 return;
1825 }
1826
robertphillips6617d502014-08-18 09:39:34 -07001827 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001828
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001829 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
bsalomon49f085d2014-09-05 13:34:00 -07001830 if (existing) {
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001831 return;
1832 }
1833
robertphillipsd6283302014-08-27 11:53:28 -07001834 GPUOptimize(picture);
robertphillipsd771f6b2014-07-22 10:18:06 -07001835
1836 fContext->getLayerCache()->trackPicture(picture);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001837}
1838
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001839static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1840 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001841 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001842 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1843}
1844
reedd5fa1a42014-08-09 11:08:05 -07001845bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* picture,
1846 const SkMatrix* matrix, const SkPaint* paint) {
1847 // todo: should handle these natively
1848 if (matrix || paint) {
1849 return false;
1850 }
1851
robertphillipsd771f6b2014-07-22 10:18:06 -07001852 fContext->getLayerCache()->processDeletedPictures();
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001853
robertphillips6617d502014-08-18 09:39:34 -07001854 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001855
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001856 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001857 if (NULL == data) {
1858 return false;
1859 }
1860
robertphillips6617d502014-08-18 09:39:34 -07001861 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001862
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001863 if (0 == gpuData->numSaveLayers()) {
1864 return false;
1865 }
1866
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001867 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001868
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001869 SkRect clipBounds;
robertphillips0c423322014-07-31 11:02:38 -07001870 if (!mainCanvas->getClipBounds(&clipBounds)) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001871 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001872 }
1873
robertphillips2ed8a752014-09-03 13:46:02 -07001874 if (!GrLayerHoister::FindLayersToHoist(gpuData, clipBounds, pullForward.get())) {
robertphillips64bf7672014-08-21 13:07:35 -07001875 return false;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001876 }
1877
robertphillips274b4ba2014-09-04 07:24:18 -07001878 GrReplacements replacements;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001879
robertphillips0c423322014-07-31 11:02:38 -07001880 SkTDArray<GrCachedLayer*> atlased, nonAtlased;
1881 atlased.setReserve(gpuData->numSaveLayers());
1882
robertphillips4ec84da2014-06-24 13:10:43 -07001883 // Generate the layer and/or ensure it is locked
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001884 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1885 if (pullForward[i]) {
robertphillips6617d502014-08-18 09:39:34 -07001886 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001887
robertphillipsd6283302014-08-27 11:53:28 -07001888 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture->uniqueID(),
1889 info.fSaveLayerOpID,
robertphillips4815fe52014-09-16 10:32:43 -07001890 info.fRestoreOpID,
1891 info.fOffset,
robertphillipsd6283302014-08-27 11:53:28 -07001892 info.fOriginXform);
robertphillips0c423322014-07-31 11:02:38 -07001893
robertphillips274b4ba2014-09-04 07:24:18 -07001894 GrReplacements::ReplacementInfo* layerInfo = replacements.push();
robertphillips4ec84da2014-06-24 13:10:43 -07001895 layerInfo->fStart = info.fSaveLayerOpID;
1896 layerInfo->fStop = info.fRestoreOpID;
1897 layerInfo->fPos = info.fOffset;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001898
robertphillips4ec84da2014-06-24 13:10:43 -07001899 GrTextureDesc desc;
1900 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1901 desc.fWidth = info.fSize.fWidth;
1902 desc.fHeight = info.fSize.fHeight;
1903 desc.fConfig = kSkia8888_GrPixelConfig;
1904 // TODO: need to deal with sample count
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001905
robertphillips6f294af2014-08-18 08:50:03 -07001906 bool needsRendering = fContext->getLayerCache()->lock(layer, desc,
1907 info.fHasNestedLayers || info.fIsNested);
robertphillips952841b2014-06-30 08:26:50 -07001908 if (NULL == layer->texture()) {
robertphillips4ec84da2014-06-24 13:10:43 -07001909 continue;
1910 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001911
robertphillips274b4ba2014-09-04 07:24:18 -07001912 SkBitmap bm;
mtklein533eb782014-08-27 10:39:42 -07001913 wrap_texture(layer->texture(),
robertphillips21048b52014-07-15 19:46:35 -07001914 !layer->isAtlased() ? desc.fWidth : layer->texture()->width(),
1915 !layer->isAtlased() ? desc.fHeight : layer->texture()->height(),
robertphillips274b4ba2014-09-04 07:24:18 -07001916 &bm);
1917 layerInfo->fImage = SkImage::NewTexture(bm);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001918
robertphillips4ec84da2014-06-24 13:10:43 -07001919 SkASSERT(info.fPaint);
1920 layerInfo->fPaint = info.fPaint;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001921
robertphillips21048b52014-07-15 19:46:35 -07001922 layerInfo->fSrcRect = SkIRect::MakeXYWH(layer->rect().fLeft,
1923 layer->rect().fTop,
1924 layer->rect().width(),
1925 layer->rect().height());
1926
robertphillips4ec84da2014-06-24 13:10:43 -07001927 if (needsRendering) {
robertphillips21048b52014-07-15 19:46:35 -07001928 if (layer->isAtlased()) {
robertphillips0c423322014-07-31 11:02:38 -07001929 *atlased.append() = layer;
robertphillips952841b2014-06-30 08:26:50 -07001930 } else {
robertphillips0c423322014-07-31 11:02:38 -07001931 *nonAtlased.append() = layer;
robertphillips952841b2014-06-30 08:26:50 -07001932 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001933 }
1934 }
1935 }
1936
robertphillips98d709b2014-09-02 10:20:50 -07001937 GrLayerHoister::DrawLayers(picture, atlased, nonAtlased);
robertphillips64bf7672014-08-21 13:07:35 -07001938
1939 // Render the entire picture using new layers
robertphillips274b4ba2014-09-04 07:24:18 -07001940 GrRecordReplaceDraw(*picture->fRecord, mainCanvas, picture->fBBH.get(), &replacements, NULL);
robertphillips64bf7672014-08-21 13:07:35 -07001941
robertphillips98d709b2014-09-02 10:20:50 -07001942 GrLayerHoister::UnlockLayers(fContext->getLayerCache(), picture);
robertphillips64bf7672014-08-21 13:07:35 -07001943
1944 return true;
1945}
1946
senorblancobe129b22014-08-08 07:14:35 -07001947SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07001948 // We always return a transient cache, so it is freed after each
1949 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07001950 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07001951}