blob: 0abf9d84b5588bc00597abd67e41dac949e01262 [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"
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000019#include "GrPictureUtils.h"
egdanield58a0ba2014-06-11 10:30:05 -070020#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070021#include "GrTracing.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000022
23#include "SkGrTexturePixelRef.h"
24
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025#include "SkDeviceImageFilterProxy.h"
26#include "SkDrawProcs.h"
27#include "SkGlyphCache.h"
28#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000029#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000030#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000031#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070032#include "SkPictureData.h"
robertphillips1ad00e42014-07-08 08:28:08 -070033#include "SkPictureRangePlayback.h"
robertphillipsc26d9912014-07-10 07:21:27 -070034#include "SkPictureReplacementPlayback.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkRRect.h"
36#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000037#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000038#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000039#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000040#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070041#include "SkXfermode.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000042#include "SkErrorInternals.h"
43
44#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
45
46#if 0
47 extern bool (*gShouldDrawProc)();
48 #define CHECK_SHOULD_DRAW(draw, forceI) \
49 do { \
50 if (gShouldDrawProc && !gShouldDrawProc()) return; \
51 this->prepareDraw(draw, forceI); \
52 } while (0)
53#else
54 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
55#endif
56
57// This constant represents the screen alignment criterion in texels for
58// requiring texture domain clamping to prevent color bleeding when drawing
59// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000060#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000061
62#define DO_DEFERRED_CLEAR() \
63 do { \
64 if (fNeedClear) { \
65 this->clear(SK_ColorTRANSPARENT); \
66 } \
67 } while (false) \
68
69///////////////////////////////////////////////////////////////////////////////
70
71#define CHECK_FOR_ANNOTATION(paint) \
72 do { if (paint.getAnnotation()) { return; } } while (0)
73
74///////////////////////////////////////////////////////////////////////////////
75
76
77class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
78public:
79 SkAutoCachedTexture()
80 : fDevice(NULL)
81 , fTexture(NULL) {
82 }
83
84 SkAutoCachedTexture(SkGpuDevice* device,
85 const SkBitmap& bitmap,
86 const GrTextureParams* params,
87 GrTexture** texture)
88 : fDevice(NULL)
89 , fTexture(NULL) {
90 SkASSERT(NULL != texture);
91 *texture = this->set(device, bitmap, params);
92 }
93
94 ~SkAutoCachedTexture() {
95 if (NULL != fTexture) {
96 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
97 }
98 }
99
100 GrTexture* set(SkGpuDevice* device,
101 const SkBitmap& bitmap,
102 const GrTextureParams* params) {
103 if (NULL != fTexture) {
104 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
105 fTexture = NULL;
106 }
107 fDevice = device;
108 GrTexture* result = (GrTexture*)bitmap.getTexture();
109 if (NULL == result) {
110 // Cannot return the native texture so look it up in our cache
111 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
112 result = fTexture;
113 }
114 return result;
115 }
116
117private:
118 SkGpuDevice* fDevice;
119 GrTexture* fTexture;
120};
121
122///////////////////////////////////////////////////////////////////////////////
123
124struct GrSkDrawProcs : public SkDrawProcs {
125public:
126 GrContext* fContext;
127 GrTextContext* fTextContext;
128 GrFontScaler* fFontScaler; // cached in the skia glyphcache
129};
130
131///////////////////////////////////////////////////////////////////////////////
132
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000133SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000134 SkASSERT(NULL != surface);
135 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
136 return NULL;
137 }
138 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000139 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000140 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000141 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000142 }
143}
144
reed89443ab2014-06-27 11:34:19 -0700145SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000146 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000147}
148
reed89443ab2014-06-27 11:34:19 -0700149SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000150 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000151}
152
153void SkGpuDevice::initFromRenderTarget(GrContext* context,
154 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000155 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000156 fDrawProcs = NULL;
157
158 fContext = context;
159 fContext->ref();
160
161 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000162 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000163
164 SkASSERT(NULL != renderTarget);
165 fRenderTarget = renderTarget;
166 fRenderTarget->ref();
167
168 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
169 // on the RT but not vice-versa.
170 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
171 // busting chrome (for a currently unknown reason).
172 GrSurface* surface = fRenderTarget->asTexture();
173 if (NULL == surface) {
174 surface = fRenderTarget;
175 }
reed@google.combf790232013-12-13 19:45:58 +0000176
reed6c225732014-06-09 19:52:07 -0700177 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef,
178 (surface->info(), surface, SkToBool(flags & kCached_Flag)));
reed89443ab2014-06-27 11:34:19 -0700179 fLegacyBitmap.setInfo(surface->info());
180 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700181
182 bool useDFFonts = !!(flags & kDFFonts_Flag);
183 fMainTextContext = fContext->createTextContext(fRenderTarget, fLeakyProperties, useDFFonts);
184 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000185}
186
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000187SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
188 int sampleCount) {
189 if (kUnknown_SkColorType == origInfo.colorType() ||
190 origInfo.width() < 0 || origInfo.height() < 0) {
191 return NULL;
192 }
193
194 SkImageInfo info = origInfo;
195 // TODO: perhas we can loosen this check now that colortype is more detailed
196 // e.g. can we support both RGBA and BGRA here?
197 if (kRGB_565_SkColorType == info.colorType()) {
198 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
199 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000200 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000201 if (kOpaque_SkAlphaType != info.alphaType()) {
202 info.fAlphaType = kPremul_SkAlphaType; // force this setting
203 }
204 }
205
206 GrTextureDesc desc;
207 desc.fFlags = kRenderTarget_GrTextureFlagBit;
208 desc.fWidth = info.width();
209 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000210 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000211 desc.fSampleCnt = sampleCount;
212
213 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
214 if (!texture.get()) {
215 return NULL;
216 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000217
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000218 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
219}
220
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000221SkGpuDevice::~SkGpuDevice() {
222 if (fDrawProcs) {
223 delete fDrawProcs;
224 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000225
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000226 delete fMainTextContext;
227 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000228
229 // The GrContext takes a ref on the target. We don't want to cause the render
230 // target to be unnecessarily kept alive.
231 if (fContext->getRenderTarget() == fRenderTarget) {
232 fContext->setRenderTarget(NULL);
233 }
234
235 if (fContext->getClip() == &fClipData) {
236 fContext->setClip(NULL);
237 }
238
239 SkSafeUnref(fRenderTarget);
240 fContext->unref();
241}
242
243///////////////////////////////////////////////////////////////////////////////
244
245void SkGpuDevice::makeRenderTargetCurrent() {
246 DO_DEFERRED_CLEAR();
247 fContext->setRenderTarget(fRenderTarget);
248}
249
250///////////////////////////////////////////////////////////////////////////////
251
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000252bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
253 int x, int y) {
254 DO_DEFERRED_CLEAR();
255
256 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000257 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000258 if (kUnknown_GrPixelConfig == config) {
259 return false;
260 }
261
262 uint32_t flags = 0;
263 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
264 flags = GrContext::kUnpremul_PixelOpsFlag;
265 }
266 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
267 config, dstPixels, dstRowBytes, flags);
268}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000269
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000270bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
271 int x, int y) {
272 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000273 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000274 if (kUnknown_GrPixelConfig == config) {
275 return false;
276 }
277 uint32_t flags = 0;
278 if (kUnpremul_SkAlphaType == info.alphaType()) {
279 flags = GrContext::kUnpremul_PixelOpsFlag;
280 }
281 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
282
283 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700284 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000285
286 return true;
287}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000288
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000289const SkBitmap& SkGpuDevice::onAccessBitmap() {
290 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700291 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000292}
293
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000294void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
295 INHERITED::onAttachToCanvas(canvas);
296
297 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
298 fClipData.fClipStack = canvas->getClipStack();
299}
300
301void SkGpuDevice::onDetachFromCanvas() {
302 INHERITED::onDetachFromCanvas();
303 fClipData.fClipStack = NULL;
304}
305
306// call this every draw call, to ensure that the context reflects our state,
307// and not the state from some other canvas/device
308void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
309 SkASSERT(NULL != fClipData.fClipStack);
310
311 fContext->setRenderTarget(fRenderTarget);
312
313 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
314
315 if (forceIdentity) {
316 fContext->setIdentityMatrix();
317 } else {
318 fContext->setMatrix(*draw.fMatrix);
319 }
320 fClipData.fOrigin = this->getOrigin();
321
322 fContext->setClip(&fClipData);
323
324 DO_DEFERRED_CLEAR();
325}
326
327GrRenderTarget* SkGpuDevice::accessRenderTarget() {
328 DO_DEFERRED_CLEAR();
329 return fRenderTarget;
330}
331
332///////////////////////////////////////////////////////////////////////////////
333
334SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
335SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
336SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
337SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
338SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
339 shader_type_mismatch);
340SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
341 shader_type_mismatch);
342SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
343SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
344
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000345///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000346
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000347void SkGpuDevice::clear(SkColor color) {
egdanield78a1682014-07-09 10:41:26 -0700348 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clear", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000349 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
350 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
351 fNeedClear = false;
352}
353
354void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
355 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700356 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000357
358 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000359 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000360
361 fContext->drawPaint(grPaint);
362}
363
364// must be in SkCanvas::PointMode order
365static const GrPrimitiveType gPointMode2PrimtiveType[] = {
366 kPoints_GrPrimitiveType,
367 kLines_GrPrimitiveType,
368 kLineStrip_GrPrimitiveType
369};
370
371void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
372 size_t count, const SkPoint pts[], const SkPaint& paint) {
373 CHECK_FOR_ANNOTATION(paint);
374 CHECK_SHOULD_DRAW(draw, false);
375
376 SkScalar width = paint.getStrokeWidth();
377 if (width < 0) {
378 return;
379 }
380
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000381 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700382 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
383 GrPaint grPaint;
384 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
385 SkPath path;
386 path.moveTo(pts[0]);
387 path.lineTo(pts[1]);
388 fContext->drawPath(grPaint, path, strokeInfo);
389 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000390 }
391
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000392 // we only handle hairlines and paints without path effects or mask filters,
393 // else we let the SkDraw call our drawPath()
394 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
395 draw.drawPoints(mode, count, pts, paint, true);
396 return;
397 }
398
399 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000400 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000401
402 fContext->drawVertices(grPaint,
403 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000404 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000405 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000406 NULL,
407 NULL,
408 NULL,
409 0);
410}
411
412///////////////////////////////////////////////////////////////////////////////
413
414void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
415 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700416 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
417
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000418 CHECK_FOR_ANNOTATION(paint);
419 CHECK_SHOULD_DRAW(draw, false);
420
421 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
422 SkScalar width = paint.getStrokeWidth();
423
424 /*
425 We have special code for hairline strokes, miter-strokes, bevel-stroke
426 and fills. Anything else we just call our path code.
427 */
428 bool usePath = doStroke && width > 0 &&
429 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
430 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
431 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700432
433 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000434 usePath = true;
435 }
egdanield58a0ba2014-06-11 10:30:05 -0700436
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000437 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
438#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
439 if (doStroke) {
440#endif
441 usePath = true;
442#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
443 } else {
444 usePath = !fContext->getMatrix().preservesRightAngles();
445 }
446#endif
447 }
448 // until we can both stroke and fill rectangles
449 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
450 usePath = true;
451 }
452
egdanield58a0ba2014-06-11 10:30:05 -0700453 GrStrokeInfo strokeInfo(paint);
454
455 const SkPathEffect* pe = paint.getPathEffect();
456 if (!usePath && NULL != pe && !strokeInfo.isDashed()) {
457 usePath = true;
458 }
459
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000460 if (usePath) {
461 SkPath path;
462 path.addRect(rect);
463 this->drawPath(draw, path, paint, NULL, true);
464 return;
465 }
466
467 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000468 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400469
egdanield58a0ba2014-06-11 10:30:05 -0700470 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000471}
472
473///////////////////////////////////////////////////////////////////////////////
474
475void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
476 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700477 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000478 CHECK_FOR_ANNOTATION(paint);
479 CHECK_SHOULD_DRAW(draw, false);
480
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000481 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000482 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400483
egdanield58a0ba2014-06-11 10:30:05 -0700484 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000485 if (paint.getMaskFilter()) {
486 // try to hit the fast path for drawing filtered round rects
487
488 SkRRect devRRect;
489 if (rect.transform(fContext->getMatrix(), &devRRect)) {
490 if (devRRect.allCornersCircular()) {
491 SkRect maskRect;
492 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
493 draw.fClip->getBounds(),
494 fContext->getMatrix(),
495 &maskRect)) {
496 SkIRect finalIRect;
497 maskRect.roundOut(&finalIRect);
498 if (draw.fClip->quickReject(finalIRect)) {
499 // clipped out
500 return;
501 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000502 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700503 strokeInfo.getStrokeRec(),
504 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000505 return;
506 }
507 }
508
509 }
510 }
511
512 }
513
egdanield58a0ba2014-06-11 10:30:05 -0700514 bool usePath = false;
515
516 if (paint.getMaskFilter()) {
517 usePath = true;
518 } else {
519 const SkPathEffect* pe = paint.getPathEffect();
520 if (NULL != pe && !strokeInfo.isDashed()) {
521 usePath = true;
522 }
523 }
524
525
526 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000527 SkPath path;
528 path.addRRect(rect);
529 this->drawPath(draw, path, paint, NULL, true);
530 return;
531 }
Mike Klein744fb732014-06-23 15:13:26 -0400532
egdanield58a0ba2014-06-11 10:30:05 -0700533 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000534}
535
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000536void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
537 const SkRRect& inner, const SkPaint& paint) {
538 SkStrokeRec stroke(paint);
539 if (stroke.isFillStyle()) {
540
541 CHECK_FOR_ANNOTATION(paint);
542 CHECK_SHOULD_DRAW(draw, false);
543
544 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000545 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000546
547 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
548 fContext->drawDRRect(grPaint, outer, inner);
549 return;
550 }
551 }
552
553 SkPath path;
554 path.addRRect(outer);
555 path.addRRect(inner);
556 path.setFillType(SkPath::kEvenOdd_FillType);
557
558 this->drawPath(draw, path, paint, NULL, true);
559}
560
561
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000562/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000563
564void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
565 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700566 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000567 CHECK_FOR_ANNOTATION(paint);
568 CHECK_SHOULD_DRAW(draw, false);
569
egdanield58a0ba2014-06-11 10:30:05 -0700570 GrStrokeInfo strokeInfo(paint);
571
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000572 bool usePath = false;
573 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700574 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000575 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700576 } else {
577 const SkPathEffect* pe = paint.getPathEffect();
578 if (NULL != pe && !strokeInfo.isDashed()) {
579 usePath = true;
580 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000581 }
582
583 if (usePath) {
584 SkPath path;
585 path.addOval(oval);
586 this->drawPath(draw, path, paint, NULL, true);
587 return;
588 }
589
590 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000591 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000592
egdanield58a0ba2014-06-11 10:30:05 -0700593 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000594}
595
596#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000597
598///////////////////////////////////////////////////////////////////////////////
599
600// helpers for applying mask filters
601namespace {
602
603// Draw a mask using the supplied paint. Since the coverage/geometry
604// is already burnt into the mask this boils down to a rect draw.
605// Return true if the mask was successfully drawn.
606bool draw_mask(GrContext* context, const SkRect& maskRect,
607 GrPaint* grp, GrTexture* mask) {
608 GrContext::AutoMatrix am;
609 if (!am.setIdentity(context, grp)) {
610 return false;
611 }
612
613 SkMatrix matrix;
614 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
615 matrix.postIDiv(mask->width(), mask->height());
616
617 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
618 context->drawRect(*grp, maskRect);
619 return true;
620}
621
622bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700623 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000624 GrPaint* grp, SkPaint::Style style) {
625 SkMask srcM, dstM;
626
627 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
628 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
629 return false;
630 }
631 SkAutoMaskFreeImage autoSrc(srcM.fImage);
632
633 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
634 return false;
635 }
636 // this will free-up dstM when we're done (allocated in filterMask())
637 SkAutoMaskFreeImage autoDst(dstM.fImage);
638
639 if (clip.quickReject(dstM.fBounds)) {
640 return false;
641 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000642
643 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
644 // the current clip (and identity matrix) and GrPaint settings
645 GrTextureDesc desc;
646 desc.fWidth = dstM.fBounds.width();
647 desc.fHeight = dstM.fBounds.height();
648 desc.fConfig = kAlpha_8_GrPixelConfig;
649
650 GrAutoScratchTexture ast(context, desc);
651 GrTexture* texture = ast.texture();
652
653 if (NULL == texture) {
654 return false;
655 }
656 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
657 dstM.fImage, dstM.fRowBytes);
658
659 SkRect maskRect = SkRect::Make(dstM.fBounds);
660
661 return draw_mask(context, maskRect, grp, texture);
662}
663
664// Create a mask of 'devPath' and place the result in 'mask'. Return true on
665// success; false otherwise.
666bool create_mask_GPU(GrContext* context,
667 const SkRect& maskRect,
668 const SkPath& devPath,
egdanield58a0ba2014-06-11 10:30:05 -0700669 const GrStrokeInfo& strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000670 bool doAA,
671 GrAutoScratchTexture* mask) {
672 GrTextureDesc desc;
673 desc.fFlags = kRenderTarget_GrTextureFlagBit;
674 desc.fWidth = SkScalarCeilToInt(maskRect.width());
675 desc.fHeight = SkScalarCeilToInt(maskRect.height());
676 // We actually only need A8, but it often isn't supported as a
677 // render target so default to RGBA_8888
678 desc.fConfig = kRGBA_8888_GrPixelConfig;
679 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
680 desc.fConfig = kAlpha_8_GrPixelConfig;
681 }
682
683 mask->set(context, desc);
684 if (NULL == mask->texture()) {
685 return false;
686 }
687
688 GrTexture* maskTexture = mask->texture();
689 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
690
691 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
692 GrContext::AutoClip ac(context, clipRect);
693
694 context->clear(NULL, 0x0, true);
695
696 GrPaint tempPaint;
697 if (doAA) {
698 tempPaint.setAntiAlias(true);
699 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
700 // blend coeff of zero requires dual source blending support in order
701 // to properly blend partially covered pixels. This means the AA
702 // code path may not be taken. So we use a dst blend coeff of ISA. We
703 // could special case AA draws to a dst surface with known alpha=0 to
704 // use a zero dst coeff when dual source blending isn't available.
705 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
706 }
707
708 GrContext::AutoMatrix am;
709
710 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
711 SkMatrix translate;
712 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
713 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700714 context->drawPath(tempPaint, devPath, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000715 return true;
716}
717
718SkBitmap wrap_texture(GrTexture* texture) {
719 SkBitmap result;
reed6c225732014-06-09 19:52:07 -0700720 result.setInfo(texture->info());
721 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000722 return result;
723}
724
725};
726
727void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
728 const SkPaint& paint, const SkMatrix* prePathMatrix,
729 bool pathIsMutable) {
730 CHECK_FOR_ANNOTATION(paint);
731 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700732 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000733
734 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000735 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000736
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000737 // If we have a prematrix, apply it to the path, optimizing for the case
738 // where the original path can in fact be modified in place (even though
739 // its parameter type is const).
740 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000741 SkTLazy<SkPath> tmpPath;
742 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000743
744 if (prePathMatrix) {
745 SkPath* result = pathPtr;
746
747 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000748 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000749 pathIsMutable = true;
750 }
751 // should I push prePathMatrix on our MV stack temporarily, instead
752 // of applying it here? See SkDraw.cpp
753 pathPtr->transform(*prePathMatrix, result);
754 pathPtr = result;
755 }
756 // at this point we're done with prePathMatrix
757 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
758
egdanield58a0ba2014-06-11 10:30:05 -0700759 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000760 SkPathEffect* pathEffect = paint.getPathEffect();
761 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700762 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
763 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000765 pathPtr = effectPath.get();
766 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700767 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000768 }
769
egdanield58a0ba2014-06-11 10:30:05 -0700770 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000771 if (paint.getMaskFilter()) {
772 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000773 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
774 if (stroke.applyToPath(strokedPath, *pathPtr)) {
775 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000776 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700777 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000778 }
779 }
780
781 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000782 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000783
784 // transform the path into device space
785 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000786
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000787 SkRect maskRect;
788 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
789 draw.fClip->getBounds(),
790 fContext->getMatrix(),
791 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000792 // The context's matrix may change while creating the mask, so save the CTM here to
793 // pass to filterMaskGPU.
794 const SkMatrix ctm = fContext->getMatrix();
795
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000796 SkIRect finalIRect;
797 maskRect.roundOut(&finalIRect);
798 if (draw.fClip->quickReject(finalIRect)) {
799 // clipped out
800 return;
801 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000802
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000803 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000804 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000805 // the mask filter was able to draw itself directly, so there's nothing
806 // left to do.
807 return;
808 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000809
810 GrAutoScratchTexture mask;
811
egdanield58a0ba2014-06-11 10:30:05 -0700812 if (create_mask_GPU(fContext, maskRect, *devPathPtr, strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000813 grPaint.isAntiAlias(), &mask)) {
814 GrTexture* filtered;
815
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000816 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000817 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000818 // filterMaskGPU gives us ownership of a ref to the result
819 SkAutoTUnref<GrTexture> atu(filtered);
820
821 // If the scratch texture that we used as the filter src also holds the filter
822 // result then we must detach so that this texture isn't recycled for a later
823 // draw.
824 if (filtered == mask.texture()) {
825 mask.detach();
826 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
827 }
828
829 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
830 // This path is completely drawn
831 return;
832 }
833 }
834 }
835 }
836
837 // draw the mask on the CPU - this is a fallthrough path in case the
838 // GPU path fails
839 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
840 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700841 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
842 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000843 return;
844 }
845
egdanield58a0ba2014-06-11 10:30:05 -0700846 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000847}
848
849static const int kBmpSmallTileSize = 1 << 10;
850
851static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
852 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
853 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
854 return tilesX * tilesY;
855}
856
857static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
858 if (maxTileSize <= kBmpSmallTileSize) {
859 return maxTileSize;
860 }
861
862 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
863 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
864
865 maxTileTotalTileSize *= maxTileSize * maxTileSize;
866 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
867
868 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
869 return kBmpSmallTileSize;
870 } else {
871 return maxTileSize;
872 }
873}
874
875// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
876// pixels from the bitmap are necessary.
877static void determine_clipped_src_rect(const GrContext* context,
878 const SkBitmap& bitmap,
879 const SkRect* srcRectPtr,
880 SkIRect* clippedSrcIRect) {
881 const GrClipData* clip = context->getClip();
882 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
883 SkMatrix inv;
884 if (!context->getMatrix().invert(&inv)) {
885 clippedSrcIRect->setEmpty();
886 return;
887 }
888 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
889 inv.mapRect(&clippedSrcRect);
890 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000891 // we've setup src space 0,0 to map to the top left of the src rect.
892 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000893 if (!clippedSrcRect.intersect(*srcRectPtr)) {
894 clippedSrcIRect->setEmpty();
895 return;
896 }
897 }
898 clippedSrcRect.roundOut(clippedSrcIRect);
899 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
900 if (!clippedSrcIRect->intersect(bmpBounds)) {
901 clippedSrcIRect->setEmpty();
902 }
903}
904
905bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
906 const GrTextureParams& params,
907 const SkRect* srcRectPtr,
908 int maxTileSize,
909 int* tileSize,
910 SkIRect* clippedSrcRect) const {
911 // if bitmap is explictly texture backed then just use the texture
912 if (NULL != bitmap.getTexture()) {
913 return false;
914 }
915
916 // if it's larger than the max tile size, then we have no choice but tiling.
917 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
918 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
919 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
920 return true;
921 }
922
923 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
924 return false;
925 }
926
927 // if the entire texture is already in our cache then no reason to tile it
928 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
929 return false;
930 }
931
932 // At this point we know we could do the draw by uploading the entire bitmap
933 // as a texture. However, if the texture would be large compared to the
934 // cache size and we don't require most of it for this draw then tile to
935 // reduce the amount of upload and cache spill.
936
937 // assumption here is that sw bitmap size is a good proxy for its size as
938 // a texture
939 size_t bmpSize = bitmap.getSize();
940 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000941 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000942 if (bmpSize < cacheSize / 2) {
943 return false;
944 }
945
946 // Figure out how much of the src we will need based on the src rect and clipping.
947 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
948 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
949 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
950 kBmpSmallTileSize * kBmpSmallTileSize;
951
952 return usedTileBytes < 2 * bmpSize;
953}
954
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000955void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000956 const SkBitmap& bitmap,
957 const SkMatrix& m,
958 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000959 SkMatrix concat;
960 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
961 if (!m.isIdentity()) {
962 concat.setConcat(*draw->fMatrix, m);
963 draw.writable()->fMatrix = &concat;
964 }
965 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000966}
967
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000968// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000969// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
970// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000971static inline void clamped_outset_with_offset(SkIRect* iRect,
972 int outset,
973 SkPoint* offset,
974 const SkIRect& clamp) {
975 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000976
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000977 int leftClampDelta = clamp.fLeft - iRect->fLeft;
978 if (leftClampDelta > 0) {
979 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000980 iRect->fLeft = clamp.fLeft;
981 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000982 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000983 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000984
985 int topClampDelta = clamp.fTop - iRect->fTop;
986 if (topClampDelta > 0) {
987 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000988 iRect->fTop = clamp.fTop;
989 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000990 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000991 }
992
993 if (iRect->fRight > clamp.fRight) {
994 iRect->fRight = clamp.fRight;
995 }
996 if (iRect->fBottom > clamp.fBottom) {
997 iRect->fBottom = clamp.fBottom;
998 }
999}
1000
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001001static bool has_aligned_samples(const SkRect& srcRect,
1002 const SkRect& transformedRect) {
1003 // detect pixel disalignment
1004 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1005 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1006 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1007 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1008 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1009 COLOR_BLEED_TOLERANCE &&
1010 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1011 COLOR_BLEED_TOLERANCE) {
1012 return true;
1013 }
1014 return false;
1015}
1016
1017static bool may_color_bleed(const SkRect& srcRect,
1018 const SkRect& transformedRect,
1019 const SkMatrix& m) {
1020 // Only gets called if has_aligned_samples returned false.
1021 // So we can assume that sampling is axis aligned but not texel aligned.
1022 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1023 SkRect innerSrcRect(srcRect), innerTransformedRect,
1024 outerTransformedRect(transformedRect);
1025 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1026 m.mapRect(&innerTransformedRect, innerSrcRect);
1027
1028 // The gap between outerTransformedRect and innerTransformedRect
1029 // represents the projection of the source border area, which is
1030 // problematic for color bleeding. We must check whether any
1031 // destination pixels sample the border area.
1032 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1033 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1034 SkIRect outer, inner;
1035 outerTransformedRect.round(&outer);
1036 innerTransformedRect.round(&inner);
1037 // If the inner and outer rects round to the same result, it means the
1038 // border does not overlap any pixel centers. Yay!
1039 return inner != outer;
1040}
1041
1042static bool needs_texture_domain(const SkBitmap& bitmap,
1043 const SkRect& srcRect,
1044 GrTextureParams &params,
1045 const SkMatrix& contextMatrix,
1046 bool bicubic) {
1047 bool needsTextureDomain = false;
1048
1049 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1050 // Need texture domain if drawing a sub rect
1051 needsTextureDomain = srcRect.width() < bitmap.width() ||
1052 srcRect.height() < bitmap.height();
1053 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1054 // sampling is axis-aligned
1055 SkRect transformedRect;
1056 contextMatrix.mapRect(&transformedRect, srcRect);
1057
1058 if (has_aligned_samples(srcRect, transformedRect)) {
1059 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1060 needsTextureDomain = false;
1061 } else {
1062 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1063 }
1064 }
1065 }
1066 return needsTextureDomain;
1067}
1068
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001069void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1070 const SkBitmap& bitmap,
1071 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001072 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001073 const SkPaint& paint,
1074 SkCanvas::DrawBitmapRectFlags flags) {
1075 CHECK_SHOULD_DRAW(draw, false);
1076
1077 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001078 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001079 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1080 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001081 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001082 SkScalar w = SkIntToScalar(bitmap.width());
1083 SkScalar h = SkIntToScalar(bitmap.height());
1084 dstSize.fWidth = w;
1085 dstSize.fHeight = h;
1086 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001087 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001088 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001089 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001090 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001091 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001092 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1093 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1094 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1095 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001096 }
1097
1098 if (paint.getMaskFilter()){
1099 // Convert the bitmap to a shader so that the rect can be drawn
1100 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001101 SkBitmap tmp; // subset of bitmap, if necessary
1102 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001103 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001104 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001105 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1106 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1107 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001108 // In bleed mode we position and trim the bitmap based on the src rect which is
1109 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1110 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1111 // compensate.
1112 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1113 SkIRect iSrc;
1114 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001115
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001116 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1117 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001118
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001119 if (!bitmap.extractSubset(&tmp, iSrc)) {
1120 return; // extraction failed
1121 }
1122 bitmapPtr = &tmp;
1123 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001124
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001125 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001126 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001127 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001128 } else {
1129 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001130 }
1131
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001132 SkPaint paintWithShader(paint);
1133 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001134 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001135 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1136 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001137
1138 return;
1139 }
1140
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001141 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1142 // the view matrix rather than a local matrix.
1143 SkMatrix m;
1144 m.setScale(dstSize.fWidth / srcRect.width(),
1145 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001146 fContext->concatMatrix(m);
1147
1148 GrTextureParams params;
1149 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1150 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001151
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001152 bool doBicubic = false;
1153
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001154 switch(paintFilterLevel) {
1155 case SkPaint::kNone_FilterLevel:
1156 textureFilterMode = GrTextureParams::kNone_FilterMode;
1157 break;
1158 case SkPaint::kLow_FilterLevel:
1159 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1160 break;
1161 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001162 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001163 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1164 } else {
1165 // Don't trigger MIP level generation unnecessarily.
1166 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1167 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001168 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001169 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001170 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001171 doBicubic =
1172 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001173 break;
1174 default:
1175 SkErrorInternals::SetError( kInvalidPaint_SkError,
1176 "Sorry, I don't understand the filtering "
1177 "mode you asked for. Falling back to "
1178 "MIPMaps.");
1179 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1180 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001181 }
1182
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001183 int tileFilterPad;
1184 if (doBicubic) {
1185 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1186 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1187 tileFilterPad = 0;
1188 } else {
1189 tileFilterPad = 1;
1190 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001191 params.setFilterMode(textureFilterMode);
1192
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001193 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001194 int tileSize;
1195
1196 SkIRect clippedSrcRect;
1197 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1198 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001199 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1200 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001201 } else {
1202 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001203 bool needsTextureDomain = needs_texture_domain(bitmap,
1204 srcRect,
1205 params,
1206 fContext->getMatrix(),
1207 doBicubic);
1208 this->internalDrawBitmap(bitmap,
1209 srcRect,
1210 params,
1211 paint,
1212 flags,
1213 doBicubic,
1214 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001215 }
1216}
1217
1218// Break 'bitmap' into several tiles to draw it since it has already
1219// been determined to be too large to fit in VRAM
1220void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1221 const SkRect& srcRect,
1222 const SkIRect& clippedSrcIRect,
1223 const GrTextureParams& params,
1224 const SkPaint& paint,
1225 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001226 int tileSize,
1227 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001228 // The following pixel lock is technically redundant, but it is desirable
1229 // to lock outside of the tile loop to prevent redecoding the whole image
1230 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1231 // is larger than the limit of the discardable memory pool.
1232 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001233 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1234
1235 int nx = bitmap.width() / tileSize;
1236 int ny = bitmap.height() / tileSize;
1237 for (int x = 0; x <= nx; x++) {
1238 for (int y = 0; y <= ny; y++) {
1239 SkRect tileR;
1240 tileR.set(SkIntToScalar(x * tileSize),
1241 SkIntToScalar(y * tileSize),
1242 SkIntToScalar((x + 1) * tileSize),
1243 SkIntToScalar((y + 1) * tileSize));
1244
1245 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1246 continue;
1247 }
1248
1249 if (!tileR.intersect(srcRect)) {
1250 continue;
1251 }
1252
1253 SkBitmap tmpB;
1254 SkIRect iTileR;
1255 tileR.roundOut(&iTileR);
1256 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1257 SkIntToScalar(iTileR.fTop));
1258
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001259 // Adjust the context matrix to draw at the right x,y in device space
1260 SkMatrix tmpM;
1261 GrContext::AutoMatrix am;
1262 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1263 am.setPreConcat(fContext, tmpM);
1264
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001265 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001266 SkIRect iClampRect;
1267
1268 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1269 // In bleed mode we want to always expand the tile on all edges
1270 // but stay within the bitmap bounds
1271 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1272 } else {
1273 // In texture-domain/clamp mode we only want to expand the
1274 // tile on edges interior to "srcRect" (i.e., we want to
1275 // not bleed across the original clamped edges)
1276 srcRect.roundOut(&iClampRect);
1277 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001278 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1279 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001280 }
1281
1282 if (bitmap.extractSubset(&tmpB, iTileR)) {
1283 // now offset it to make it "local" to our tmp bitmap
1284 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001285 GrTextureParams paramsTemp = params;
1286 bool needsTextureDomain = needs_texture_domain(bitmap,
1287 srcRect,
1288 paramsTemp,
1289 fContext->getMatrix(),
1290 bicubic);
1291 this->internalDrawBitmap(tmpB,
1292 tileR,
1293 paramsTemp,
1294 paint,
1295 flags,
1296 bicubic,
1297 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001298 }
1299 }
1300 }
1301}
1302
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001303
1304/*
1305 * This is called by drawBitmap(), which has to handle images that may be too
1306 * large to be represented by a single texture.
1307 *
1308 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1309 * and that non-texture portion of the GrPaint has already been setup.
1310 */
1311void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1312 const SkRect& srcRect,
1313 const GrTextureParams& params,
1314 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001315 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001316 bool bicubic,
1317 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001318 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1319 bitmap.height() <= fContext->getMaxTextureSize());
1320
1321 GrTexture* texture;
1322 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1323 if (NULL == texture) {
1324 return;
1325 }
1326
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001327 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001328 SkRect paintRect;
1329 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1330 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1331 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1332 SkScalarMul(srcRect.fTop, hInv),
1333 SkScalarMul(srcRect.fRight, wInv),
1334 SkScalarMul(srcRect.fBottom, hInv));
1335
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001336 SkRect textureDomain = SkRect::MakeEmpty();
bsalomon83d081a2014-07-08 09:56:10 -07001337 SkAutoTUnref<GrEffect> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001338 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001339 // Use a constrained texture domain to avoid color bleeding
1340 SkScalar left, top, right, bottom;
1341 if (srcRect.width() > SK_Scalar1) {
1342 SkScalar border = SK_ScalarHalf / texture->width();
1343 left = paintRect.left() + border;
1344 right = paintRect.right() - border;
1345 } else {
1346 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1347 }
1348 if (srcRect.height() > SK_Scalar1) {
1349 SkScalar border = SK_ScalarHalf / texture->height();
1350 top = paintRect.top() + border;
1351 bottom = paintRect.bottom() - border;
1352 } else {
1353 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1354 }
1355 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001356 if (bicubic) {
1357 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1358 } else {
1359 effect.reset(GrTextureDomainEffect::Create(texture,
1360 SkMatrix::I(),
1361 textureDomain,
1362 GrTextureDomain::kClamp_Mode,
1363 params.filterMode()));
1364 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001365 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001366 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1367 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1368 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001369 } else {
1370 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1371 }
1372
1373 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1374 // the rest from the SkPaint.
1375 GrPaint grPaint;
1376 grPaint.addColorEffect(effect);
reed0689d7b2014-06-14 05:30:20 -07001377 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001378 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1379 SkColor2GrColor(paint.getColor());
1380 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001381
1382 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1383}
1384
1385static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001386 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001387 int w, int h, const SkImageFilter::Context& ctx,
1388 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001389 SkASSERT(filter);
1390 SkDeviceImageFilterProxy proxy(device);
1391
1392 if (filter->canFilterImageGPU()) {
1393 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1394 // filter. Also set the clip wide open and the matrix to identity.
1395 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001396 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001397 } else {
1398 return false;
1399 }
1400}
1401
1402void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1403 int left, int top, const SkPaint& paint) {
1404 // drawSprite is defined to be in device coords.
1405 CHECK_SHOULD_DRAW(draw, true);
1406
1407 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1408 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1409 return;
1410 }
1411
1412 int w = bitmap.width();
1413 int h = bitmap.height();
1414
1415 GrTexture* texture;
1416 // draw sprite uses the default texture params
1417 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1418
1419 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001420 // This bitmap will own the filtered result as a texture.
1421 SkBitmap filteredBitmap;
1422
1423 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001424 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001425 SkMatrix matrix(*draw.fMatrix);
1426 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001427 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001428 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1429 SkAutoUnref aur(cache);
1430 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001431 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001432 &offset)) {
1433 texture = (GrTexture*) filteredBitmap.getTexture();
1434 w = filteredBitmap.width();
1435 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001436 left += offset.x();
1437 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001438 } else {
1439 return;
1440 }
1441 }
1442
1443 GrPaint grPaint;
1444 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1445
dandov9de5b512014-06-10 14:38:28 -07001446 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1447 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001448
1449 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001450 SkRect::MakeXYWH(SkIntToScalar(left),
1451 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001452 SkIntToScalar(w),
1453 SkIntToScalar(h)),
1454 SkRect::MakeXYWH(0,
1455 0,
1456 SK_Scalar1 * w / texture->width(),
1457 SK_Scalar1 * h / texture->height()));
1458}
1459
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001460void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001461 const SkRect* src, const SkRect& dst,
1462 const SkPaint& paint,
1463 SkCanvas::DrawBitmapRectFlags flags) {
1464 SkMatrix matrix;
1465 SkRect bitmapBounds, tmpSrc;
1466
1467 bitmapBounds.set(0, 0,
1468 SkIntToScalar(bitmap.width()),
1469 SkIntToScalar(bitmap.height()));
1470
1471 // Compute matrix from the two rectangles
1472 if (NULL != src) {
1473 tmpSrc = *src;
1474 } else {
1475 tmpSrc = bitmapBounds;
1476 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001477
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001478 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1479
1480 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1481 if (NULL != src) {
1482 if (!bitmapBounds.contains(tmpSrc)) {
1483 if (!tmpSrc.intersect(bitmapBounds)) {
1484 return; // nothing to draw
1485 }
1486 }
1487 }
1488
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001489 SkRect tmpDst;
1490 matrix.mapRect(&tmpDst, tmpSrc);
1491
1492 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1493 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1494 // Translate so that tempDst's top left is at the origin.
1495 matrix = *origDraw.fMatrix;
1496 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1497 draw.writable()->fMatrix = &matrix;
1498 }
1499 SkSize dstSize;
1500 dstSize.fWidth = tmpDst.width();
1501 dstSize.fHeight = tmpDst.height();
1502
1503 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001504}
1505
1506void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1507 int x, int y, const SkPaint& paint) {
1508 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001509 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001510 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1511 if (dev->fNeedClear) {
1512 // TODO: could check here whether we really need to draw at all
1513 dev->clear(0x0);
1514 }
1515
1516 // drawDevice is defined to be in device coords.
1517 CHECK_SHOULD_DRAW(draw, true);
1518
1519 GrRenderTarget* devRT = dev->accessRenderTarget();
1520 GrTexture* devTex;
1521 if (NULL == (devTex = devRT->asTexture())) {
1522 return;
1523 }
1524
1525 const SkBitmap& bm = dev->accessBitmap(false);
1526 int w = bm.width();
1527 int h = bm.height();
1528
1529 SkImageFilter* filter = paint.getImageFilter();
1530 // This bitmap will own the filtered result as a texture.
1531 SkBitmap filteredBitmap;
1532
1533 if (NULL != filter) {
1534 SkIPoint offset = SkIPoint::Make(0, 0);
1535 SkMatrix matrix(*draw.fMatrix);
1536 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001537 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001538 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1539 SkAutoUnref aur(cache);
1540 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001541 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001542 &offset)) {
1543 devTex = filteredBitmap.getTexture();
1544 w = filteredBitmap.width();
1545 h = filteredBitmap.height();
1546 x += offset.fX;
1547 y += offset.fY;
1548 } else {
1549 return;
1550 }
1551 }
1552
1553 GrPaint grPaint;
1554 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1555
dandov9de5b512014-06-10 14:38:28 -07001556 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1557 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001558
1559 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1560 SkIntToScalar(y),
1561 SkIntToScalar(w),
1562 SkIntToScalar(h));
1563
1564 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1565 // scratch texture).
1566 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1567 SK_Scalar1 * h / devTex->height());
1568
1569 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1570}
1571
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001572bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001573 return filter->canFilterImageGPU();
1574}
1575
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001576bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001577 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001578 SkBitmap* result, SkIPoint* offset) {
1579 // want explicitly our impl, so guard against a subclass of us overriding it
1580 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1581 return false;
1582 }
1583
1584 SkAutoLockPixels alp(src, !src.getTexture());
1585 if (!src.getTexture() && !src.readyToDraw()) {
1586 return false;
1587 }
1588
1589 GrTexture* texture;
1590 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1591 // must be pushed upstack.
1592 SkAutoCachedTexture act(this, src, NULL, &texture);
1593
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001594 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1595 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001596}
1597
1598///////////////////////////////////////////////////////////////////////////////
1599
1600// must be in SkCanvas::VertexMode order
1601static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1602 kTriangles_GrPrimitiveType,
1603 kTriangleStrip_GrPrimitiveType,
1604 kTriangleFan_GrPrimitiveType,
1605};
1606
1607void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1608 int vertexCount, const SkPoint vertices[],
1609 const SkPoint texs[], const SkColor colors[],
1610 SkXfermode* xmode,
1611 const uint16_t indices[], int indexCount,
1612 const SkPaint& paint) {
1613 CHECK_SHOULD_DRAW(draw, false);
1614
egdanield78a1682014-07-09 10:41:26 -07001615 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
dandov32a311b2014-07-15 19:46:26 -07001616
1617 const uint16_t* outIndices;
1618 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1619 GrPrimitiveType primType;
1620 GrPaint grPaint;
1621
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001622 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1623 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
dandov32a311b2014-07-15 19:46:26 -07001624
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001625 texs = NULL;
dandov32a311b2014-07-15 19:46:26 -07001626
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001627 SkPaint copy(paint);
1628 copy.setStyle(SkPaint::kStroke_Style);
1629 copy.setStrokeWidth(0);
dandov32a311b2014-07-15 19:46:26 -07001630
1631 // we ignore the shader if texs is null.
1632 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1633 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001634
dandov32a311b2014-07-15 19:46:26 -07001635 primType = kLines_GrPrimitiveType;
1636 int triangleCount = 0;
1637 switch (vmode) {
1638 case SkCanvas::kTriangles_VertexMode:
1639 triangleCount = indexCount / 3;
1640 break;
1641 case SkCanvas::kTriangleStrip_VertexMode:
1642 case SkCanvas::kTriangleFan_VertexMode:
1643 triangleCount = indexCount - 2;
1644 break;
1645 }
1646
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001647 VertState state(vertexCount, indices, indexCount);
1648 VertState::Proc vertProc = state.chooseProc(vmode);
dandov32a311b2014-07-15 19:46:26 -07001649
1650 //number of indices for lines per triangle with kLines
1651 indexCount = triangleCount * 6;
1652
1653 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1654 outIndices = outAlloc.get();
1655 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001656 int i = 0;
1657 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001658 auxIndices[i] = state.f0;
1659 auxIndices[i + 1] = state.f1;
1660 auxIndices[i + 2] = state.f1;
1661 auxIndices[i + 3] = state.f2;
1662 auxIndices[i + 4] = state.f2;
1663 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001664 i += 6;
1665 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001666 } else {
dandov32a311b2014-07-15 19:46:26 -07001667 outIndices = indices;
1668 primType = gVertexMode2PrimitiveType[vmode];
1669
1670 if (NULL == texs || NULL == paint.getShader()) {
1671 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1672 NULL == colors, &grPaint);
1673 } else {
1674 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
1675 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001676 }
1677
mtklein2583b622014-06-04 08:20:41 -07001678#if 0
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001679 if (NULL != xmode && NULL != texs && NULL != colors) {
1680 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1681 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001682 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001683 }
1684 }
mtklein2583b622014-06-04 08:20:41 -07001685#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001686
1687 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1688 if (NULL != colors) {
1689 // need to convert byte order and from non-PM to PM
1690 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001691 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001692 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001693 color = colors[i];
1694 if (paint.getAlpha() != 255) {
1695 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1696 }
1697 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001698 }
1699 colors = convertedColors.get();
1700 }
1701 fContext->drawVertices(grPaint,
dandov32a311b2014-07-15 19:46:26 -07001702 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001703 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001704 vertices,
1705 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001706 colors,
dandov32a311b2014-07-15 19:46:26 -07001707 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001708 indexCount);
1709}
1710
1711///////////////////////////////////////////////////////////////////////////////
1712
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001713void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1714 size_t byteLength, SkScalar x, SkScalar y,
1715 const SkPaint& paint) {
1716 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -07001717 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001718
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001719 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001720 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001721 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001722
1723 SkDEBUGCODE(this->validate();)
1724
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001725 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1726 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001727 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001728 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001729
1730 SkDEBUGCODE(this->validate();)
1731
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001732 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001733 } else {
1734 // this guy will just call our drawPath()
1735 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001736 }
1737}
1738
1739void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1740 size_t byteLength, const SkScalar pos[],
1741 SkScalar constY, int scalarsPerPos,
1742 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001743 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001744 CHECK_SHOULD_DRAW(draw, false);
1745
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001746 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001747 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001748 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001749
1750 SkDEBUGCODE(this->validate();)
1751
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001752 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001753 constY, scalarsPerPos);
1754 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001755 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001756 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001757
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001758 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001759
1760 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001761 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001762 } else {
1763 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1764 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001765 }
1766}
1767
1768void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1769 size_t len, const SkPath& path,
1770 const SkMatrix* m, const SkPaint& paint) {
1771 CHECK_SHOULD_DRAW(draw, false);
1772
1773 SkASSERT(draw.fDevice == this);
1774 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1775}
1776
1777///////////////////////////////////////////////////////////////////////////////
1778
1779bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1780 if (!paint.isLCDRenderText()) {
1781 // we're cool with the paint as is
1782 return false;
1783 }
1784
1785 if (paint.getShader() ||
1786 paint.getXfermode() || // unless its srcover
1787 paint.getMaskFilter() ||
1788 paint.getRasterizer() ||
1789 paint.getColorFilter() ||
1790 paint.getPathEffect() ||
1791 paint.isFakeBoldText() ||
1792 paint.getStyle() != SkPaint::kFill_Style) {
1793 // turn off lcd
1794 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1795 flags->fHinting = paint.getHinting();
1796 return true;
1797 }
1798 // we're cool with the paint as is
1799 return false;
1800}
1801
1802void SkGpuDevice::flush() {
1803 DO_DEFERRED_CLEAR();
1804 fContext->resolveRenderTarget(fRenderTarget);
1805}
1806
1807///////////////////////////////////////////////////////////////////////////////
1808
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001809SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001810 GrTextureDesc desc;
1811 desc.fConfig = fRenderTarget->config();
1812 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001813 desc.fWidth = info.width();
1814 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001815 desc.fSampleCnt = fRenderTarget->numSamples();
1816
1817 SkAutoTUnref<GrTexture> texture;
1818 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001819 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001820
1821#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1822 // layers are never draw in repeat modes, so we can request an approx
1823 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001824 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001825 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1826 GrContext::kApprox_ScratchTexMatch :
1827 GrContext::kExact_ScratchTexMatch;
1828 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1829#else
1830 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1831#endif
1832 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001833 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001834 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001835 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1836 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001837 return NULL;
1838 }
1839}
1840
reed@google.com76f10a32014-02-05 15:32:21 +00001841SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1842 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1843}
1844
robertphillips9b14f262014-06-04 05:40:44 -07001845void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
robertphillipsd771f6b2014-07-22 10:18:06 -07001846 fContext->getLayerCache()->processDeletedPictures();
1847
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001848 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001849
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001850 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1851 if (NULL != existing) {
1852 return;
1853 }
1854
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001855 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001856
1857 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001858
1859 GatherGPUInfo(picture, data);
robertphillipsd771f6b2014-07-22 10:18:06 -07001860
1861 fContext->getLayerCache()->trackPicture(picture);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001862}
1863
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001864static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1865 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001866 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001867 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1868}
1869
robertphillips9b14f262014-06-04 05:40:44 -07001870bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture) {
robertphillipsd771f6b2014-07-22 10:18:06 -07001871 fContext->getLayerCache()->processDeletedPictures();
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001872
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001873 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001874
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001875 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001876 if (NULL == data) {
1877 return false;
1878 }
1879
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001880 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001881
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001882 if (0 == gpuData->numSaveLayers()) {
1883 return false;
1884 }
1885
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001886 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1887 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1888 pullForward[i] = false;
1889 }
1890
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001891 SkRect clipBounds;
1892 if (!canvas->getClipBounds(&clipBounds)) {
1893 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001894 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001895 SkIRect query;
1896 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001897
robertphillipsce4dd3d2014-07-07 13:46:35 -07001898 SkAutoTDelete<const SkPicture::OperationList> ops(picture->EXPERIMENTAL_getActiveOps(query));
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001899
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001900 // This code pre-renders the entire layer since it will be cached and potentially
1901 // reused with different clips (e.g., in different tiles). Because of this the
1902 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1903 // is used to limit which clips are pre-rendered.
1904 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001905
robertphillipsce4dd3d2014-07-07 13:46:35 -07001906 if (NULL != ops.get()) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001907 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001908 // the BBH to limit the pre-rendering to just the layers needed to cover
1909 // the region being drawn
robertphillipsce4dd3d2014-07-07 13:46:35 -07001910 for (int i = 0; i < ops->numOps(); ++i) {
1911 uint32_t offset = ops->offset(i);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001912
1913 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001914 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001915 // their parent in the list.
1916 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1917 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1918
1919 if (pullForward[j]) {
1920 continue; // already pulling forward
1921 }
1922
1923 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1924 continue; // the op isn't in this range
1925 }
1926
1927 // TODO: once this code is more stable unsuitable layers can
1928 // just be omitted during the optimization stage
1929 if (!info.fValid ||
1930 kSaveLayerMaxSize < info.fSize.fWidth ||
1931 kSaveLayerMaxSize < info.fSize.fHeight ||
1932 info.fIsNested) {
1933 continue; // this layer is unsuitable
1934 }
1935
1936 pullForward[j] = true;
1937 }
1938 }
1939 } else {
1940 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001941 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001942 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1943 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1944
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001945 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1946 info.fOffset.fY,
1947 info.fSize.fWidth,
1948 info.fSize.fHeight);
1949
1950 if (!SkIRect::Intersects(query, layerRect)) {
1951 continue;
1952 }
1953
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001954 // TODO: once this code is more stable unsuitable layers can
1955 // just be omitted during the optimization stage
1956 if (!info.fValid ||
1957 kSaveLayerMaxSize < info.fSize.fWidth ||
1958 kSaveLayerMaxSize < info.fSize.fHeight ||
1959 info.fIsNested) {
1960 continue;
1961 }
1962
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001963 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001964 }
1965 }
1966
robertphillipsc26d9912014-07-10 07:21:27 -07001967 SkPictureReplacementPlayback::PlaybackReplacements replacements;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001968
robertphillips4ec84da2014-06-24 13:10:43 -07001969 // Generate the layer and/or ensure it is locked
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001970 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1971 if (pullForward[i]) {
1972 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1973
1974 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1975
robertphillipsc26d9912014-07-10 07:21:27 -07001976 SkPictureReplacementPlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001977 replacements.push();
robertphillips4ec84da2014-06-24 13:10:43 -07001978 layerInfo->fStart = info.fSaveLayerOpID;
1979 layerInfo->fStop = info.fRestoreOpID;
1980 layerInfo->fPos = info.fOffset;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001981
robertphillips4ec84da2014-06-24 13:10:43 -07001982 GrTextureDesc desc;
1983 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1984 desc.fWidth = info.fSize.fWidth;
1985 desc.fHeight = info.fSize.fHeight;
1986 desc.fConfig = kSkia8888_GrPixelConfig;
1987 // TODO: need to deal with sample count
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001988
robertphillips4ec84da2014-06-24 13:10:43 -07001989 bool needsRendering = !fContext->getLayerCache()->lock(layer, desc);
robertphillips952841b2014-06-30 08:26:50 -07001990 if (NULL == layer->texture()) {
robertphillips4ec84da2014-06-24 13:10:43 -07001991 continue;
1992 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001993
robertphillips4ec84da2014-06-24 13:10:43 -07001994 layerInfo->fBM = SkNEW(SkBitmap); // fBM is allocated so ReplacementInfo can be POD
robertphillips952841b2014-06-30 08:26:50 -07001995 wrap_texture(layer->texture(),
robertphillips21048b52014-07-15 19:46:35 -07001996 !layer->isAtlased() ? desc.fWidth : layer->texture()->width(),
1997 !layer->isAtlased() ? desc.fHeight : layer->texture()->height(),
robertphillips952841b2014-06-30 08:26:50 -07001998 layerInfo->fBM);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001999
robertphillips4ec84da2014-06-24 13:10:43 -07002000 SkASSERT(info.fPaint);
2001 layerInfo->fPaint = info.fPaint;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002002
robertphillips21048b52014-07-15 19:46:35 -07002003 layerInfo->fSrcRect = SkIRect::MakeXYWH(layer->rect().fLeft,
2004 layer->rect().fTop,
2005 layer->rect().width(),
2006 layer->rect().height());
2007
robertphillips952841b2014-06-30 08:26:50 -07002008
robertphillips4ec84da2014-06-24 13:10:43 -07002009 if (needsRendering) {
2010 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
robertphillips952841b2014-06-30 08:26:50 -07002011 layer->texture()->asRenderTarget(),
2012 SkSurface::kStandard_TextRenderMode,
2013 SkSurface::kDontClear_RenderTargetFlag));
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002014
robertphillips4ec84da2014-06-24 13:10:43 -07002015 SkCanvas* canvas = surface->getCanvas();
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002016
robertphillips21048b52014-07-15 19:46:35 -07002017 // Add a rect clip to make sure the rendering doesn't
2018 // extend beyond the boundaries of the atlased sub-rect
2019 SkRect bound = SkRect::Make(layerInfo->fSrcRect);
2020 canvas->clipRect(bound);
2021
2022 if (layer->isAtlased()) {
robertphillips952841b2014-06-30 08:26:50 -07002023 // Since 'clear' doesn't respect the clip we need to draw a rect
2024 // TODO: ensure none of the atlased layers contain a clear call!
2025 SkPaint paint;
2026 paint.setColor(SK_ColorTRANSPARENT);
robertphillips320c9232014-07-29 06:07:19 -07002027 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrc_Mode))->unref();
robertphillips952841b2014-06-30 08:26:50 -07002028 canvas->drawRect(bound, paint);
2029 } else {
2030 canvas->clear(SK_ColorTRANSPARENT);
2031 }
2032
robertphillips21048b52014-07-15 19:46:35 -07002033 // info.fCTM maps the layer's top/left to the origin.
2034 // If this layer is atlased the top/left corner needs
2035 // to be offset to some arbitrary location in the backing
2036 // texture.
2037 canvas->translate(bound.fLeft, bound.fTop);
2038 canvas->concat(info.fCTM);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002039
robertphillips1ad00e42014-07-08 08:28:08 -07002040 SkPictureRangePlayback rangePlayback(picture,
2041 info.fSaveLayerOpID,
2042 info.fRestoreOpID);
2043 rangePlayback.draw(canvas, NULL);
robertphillips952841b2014-06-30 08:26:50 -07002044
robertphillips4ec84da2014-06-24 13:10:43 -07002045 canvas->flush();
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00002046 }
2047 }
2048 }
2049
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002050 // Playback using new layers
robertphillipsc26d9912014-07-10 07:21:27 -07002051 SkPictureReplacementPlayback playback(picture, &replacements, ops.get());
robertphillipsce4dd3d2014-07-07 13:46:35 -07002052
robertphillipsce4dd3d2014-07-07 13:46:35 -07002053 playback.draw(canvas, NULL);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002054
robertphillips4ec84da2014-06-24 13:10:43 -07002055 // unlock the layers
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002056 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
robertphillips4ec84da2014-06-24 13:10:43 -07002057 GrCachedLayer* layer = fContext->getLayerCache()->findLayer(picture, i);
2058 fContext->getLayerCache()->unlock(layer);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002059 }
2060
2061 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002062}