blob: cf152fa6e51b023027c697250b06fd1fb05725dd [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"
robertphillipsce4dd3d2014-07-07 13:46:35 -070033#include "SkPicturePlayback.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000034#include "SkRRect.h"
35#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000036#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000037#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000038#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000039#include "SkVertState.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000040#include "SkErrorInternals.h"
41
42#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
43
44#if 0
45 extern bool (*gShouldDrawProc)();
46 #define CHECK_SHOULD_DRAW(draw, forceI) \
47 do { \
48 if (gShouldDrawProc && !gShouldDrawProc()) return; \
49 this->prepareDraw(draw, forceI); \
50 } while (0)
51#else
52 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
53#endif
54
55// This constant represents the screen alignment criterion in texels for
56// requiring texture domain clamping to prevent color bleeding when drawing
57// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000058#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000059
60#define DO_DEFERRED_CLEAR() \
61 do { \
62 if (fNeedClear) { \
63 this->clear(SK_ColorTRANSPARENT); \
64 } \
65 } while (false) \
66
67///////////////////////////////////////////////////////////////////////////////
68
69#define CHECK_FOR_ANNOTATION(paint) \
70 do { if (paint.getAnnotation()) { return; } } while (0)
71
72///////////////////////////////////////////////////////////////////////////////
73
74
75class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
76public:
77 SkAutoCachedTexture()
78 : fDevice(NULL)
79 , fTexture(NULL) {
80 }
81
82 SkAutoCachedTexture(SkGpuDevice* device,
83 const SkBitmap& bitmap,
84 const GrTextureParams* params,
85 GrTexture** texture)
86 : fDevice(NULL)
87 , fTexture(NULL) {
88 SkASSERT(NULL != texture);
89 *texture = this->set(device, bitmap, params);
90 }
91
92 ~SkAutoCachedTexture() {
93 if (NULL != fTexture) {
94 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
95 }
96 }
97
98 GrTexture* set(SkGpuDevice* device,
99 const SkBitmap& bitmap,
100 const GrTextureParams* params) {
101 if (NULL != fTexture) {
102 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
103 fTexture = NULL;
104 }
105 fDevice = device;
106 GrTexture* result = (GrTexture*)bitmap.getTexture();
107 if (NULL == result) {
108 // Cannot return the native texture so look it up in our cache
109 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
110 result = fTexture;
111 }
112 return result;
113 }
114
115private:
116 SkGpuDevice* fDevice;
117 GrTexture* fTexture;
118};
119
120///////////////////////////////////////////////////////////////////////////////
121
122struct GrSkDrawProcs : public SkDrawProcs {
123public:
124 GrContext* fContext;
125 GrTextContext* fTextContext;
126 GrFontScaler* fFontScaler; // cached in the skia glyphcache
127};
128
129///////////////////////////////////////////////////////////////////////////////
130
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000131SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000132 SkASSERT(NULL != surface);
133 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
134 return NULL;
135 }
136 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000137 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000138 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000139 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000140 }
141}
142
reed89443ab2014-06-27 11:34:19 -0700143SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000144 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000145}
146
reed89443ab2014-06-27 11:34:19 -0700147SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000148 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000149}
150
151void SkGpuDevice::initFromRenderTarget(GrContext* context,
152 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000153 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000154 fDrawProcs = NULL;
155
156 fContext = context;
157 fContext->ref();
158
159 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000160 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000161
162 SkASSERT(NULL != renderTarget);
163 fRenderTarget = renderTarget;
164 fRenderTarget->ref();
165
166 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
167 // on the RT but not vice-versa.
168 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
169 // busting chrome (for a currently unknown reason).
170 GrSurface* surface = fRenderTarget->asTexture();
171 if (NULL == surface) {
172 surface = fRenderTarget;
173 }
reed@google.combf790232013-12-13 19:45:58 +0000174
reed6c225732014-06-09 19:52:07 -0700175 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef,
176 (surface->info(), surface, SkToBool(flags & kCached_Flag)));
reed89443ab2014-06-27 11:34:19 -0700177 fLegacyBitmap.setInfo(surface->info());
178 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700179
180 bool useDFFonts = !!(flags & kDFFonts_Flag);
181 fMainTextContext = fContext->createTextContext(fRenderTarget, fLeakyProperties, useDFFonts);
182 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000183}
184
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000185SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
186 int sampleCount) {
187 if (kUnknown_SkColorType == origInfo.colorType() ||
188 origInfo.width() < 0 || origInfo.height() < 0) {
189 return NULL;
190 }
191
192 SkImageInfo info = origInfo;
193 // TODO: perhas we can loosen this check now that colortype is more detailed
194 // e.g. can we support both RGBA and BGRA here?
195 if (kRGB_565_SkColorType == info.colorType()) {
196 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
197 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000198 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000199 if (kOpaque_SkAlphaType != info.alphaType()) {
200 info.fAlphaType = kPremul_SkAlphaType; // force this setting
201 }
202 }
203
204 GrTextureDesc desc;
205 desc.fFlags = kRenderTarget_GrTextureFlagBit;
206 desc.fWidth = info.width();
207 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000208 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000209 desc.fSampleCnt = sampleCount;
210
211 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
212 if (!texture.get()) {
213 return NULL;
214 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000215
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000216 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
217}
218
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000219SkGpuDevice::~SkGpuDevice() {
220 if (fDrawProcs) {
221 delete fDrawProcs;
222 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000223
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000224 delete fMainTextContext;
225 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000226
227 // The GrContext takes a ref on the target. We don't want to cause the render
228 // target to be unnecessarily kept alive.
229 if (fContext->getRenderTarget() == fRenderTarget) {
230 fContext->setRenderTarget(NULL);
231 }
232
233 if (fContext->getClip() == &fClipData) {
234 fContext->setClip(NULL);
235 }
236
237 SkSafeUnref(fRenderTarget);
238 fContext->unref();
239}
240
241///////////////////////////////////////////////////////////////////////////////
242
243void SkGpuDevice::makeRenderTargetCurrent() {
244 DO_DEFERRED_CLEAR();
245 fContext->setRenderTarget(fRenderTarget);
246}
247
248///////////////////////////////////////////////////////////////////////////////
249
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000250bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
251 int x, int y) {
252 DO_DEFERRED_CLEAR();
253
254 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000255 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000256 if (kUnknown_GrPixelConfig == config) {
257 return false;
258 }
259
260 uint32_t flags = 0;
261 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
262 flags = GrContext::kUnpremul_PixelOpsFlag;
263 }
264 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
265 config, dstPixels, dstRowBytes, flags);
266}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000267
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000268bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
269 int x, int y) {
270 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000271 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000272 if (kUnknown_GrPixelConfig == config) {
273 return false;
274 }
275 uint32_t flags = 0;
276 if (kUnpremul_SkAlphaType == info.alphaType()) {
277 flags = GrContext::kUnpremul_PixelOpsFlag;
278 }
279 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
280
281 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700282 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000283
284 return true;
285}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000286
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000287const SkBitmap& SkGpuDevice::onAccessBitmap() {
288 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700289 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000290}
291
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000292void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
293 INHERITED::onAttachToCanvas(canvas);
294
295 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
296 fClipData.fClipStack = canvas->getClipStack();
297}
298
299void SkGpuDevice::onDetachFromCanvas() {
300 INHERITED::onDetachFromCanvas();
301 fClipData.fClipStack = NULL;
302}
303
304// call this every draw call, to ensure that the context reflects our state,
305// and not the state from some other canvas/device
306void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
307 SkASSERT(NULL != fClipData.fClipStack);
308
309 fContext->setRenderTarget(fRenderTarget);
310
311 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
312
313 if (forceIdentity) {
314 fContext->setIdentityMatrix();
315 } else {
316 fContext->setMatrix(*draw.fMatrix);
317 }
318 fClipData.fOrigin = this->getOrigin();
319
320 fContext->setClip(&fClipData);
321
322 DO_DEFERRED_CLEAR();
323}
324
325GrRenderTarget* SkGpuDevice::accessRenderTarget() {
326 DO_DEFERRED_CLEAR();
327 return fRenderTarget;
328}
329
330///////////////////////////////////////////////////////////////////////////////
331
332SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
333SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
334SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
335SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
336SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
337 shader_type_mismatch);
338SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
339 shader_type_mismatch);
340SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
341SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
342
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000343///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000344
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000345void SkGpuDevice::clear(SkColor color) {
346 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
347 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
348 fNeedClear = false;
349}
350
351void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
352 CHECK_SHOULD_DRAW(draw, false);
353
354 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000355 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356
357 fContext->drawPaint(grPaint);
358}
359
360// must be in SkCanvas::PointMode order
361static const GrPrimitiveType gPointMode2PrimtiveType[] = {
362 kPoints_GrPrimitiveType,
363 kLines_GrPrimitiveType,
364 kLineStrip_GrPrimitiveType
365};
366
367void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
368 size_t count, const SkPoint pts[], const SkPaint& paint) {
369 CHECK_FOR_ANNOTATION(paint);
370 CHECK_SHOULD_DRAW(draw, false);
371
372 SkScalar width = paint.getStrokeWidth();
373 if (width < 0) {
374 return;
375 }
376
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000377 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700378 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
379 GrPaint grPaint;
380 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
381 SkPath path;
382 path.moveTo(pts[0]);
383 path.lineTo(pts[1]);
384 fContext->drawPath(grPaint, path, strokeInfo);
385 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000386 }
387
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000388 // we only handle hairlines and paints without path effects or mask filters,
389 // else we let the SkDraw call our drawPath()
390 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
391 draw.drawPoints(mode, count, pts, paint, true);
392 return;
393 }
394
395 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000396 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000397
398 fContext->drawVertices(grPaint,
399 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000400 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000401 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000402 NULL,
403 NULL,
404 NULL,
405 0);
406}
407
408///////////////////////////////////////////////////////////////////////////////
409
410void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
411 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700412 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
413
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000414 CHECK_FOR_ANNOTATION(paint);
415 CHECK_SHOULD_DRAW(draw, false);
416
417 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
418 SkScalar width = paint.getStrokeWidth();
419
420 /*
421 We have special code for hairline strokes, miter-strokes, bevel-stroke
422 and fills. Anything else we just call our path code.
423 */
424 bool usePath = doStroke && width > 0 &&
425 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
426 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
427 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700428
429 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000430 usePath = true;
431 }
egdanield58a0ba2014-06-11 10:30:05 -0700432
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000433 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
434#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
435 if (doStroke) {
436#endif
437 usePath = true;
438#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
439 } else {
440 usePath = !fContext->getMatrix().preservesRightAngles();
441 }
442#endif
443 }
444 // until we can both stroke and fill rectangles
445 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
446 usePath = true;
447 }
448
egdanield58a0ba2014-06-11 10:30:05 -0700449 GrStrokeInfo strokeInfo(paint);
450
451 const SkPathEffect* pe = paint.getPathEffect();
452 if (!usePath && NULL != pe && !strokeInfo.isDashed()) {
453 usePath = true;
454 }
455
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000456 if (usePath) {
457 SkPath path;
458 path.addRect(rect);
459 this->drawPath(draw, path, paint, NULL, true);
460 return;
461 }
462
463 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000464 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400465
egdanield58a0ba2014-06-11 10:30:05 -0700466 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000467}
468
469///////////////////////////////////////////////////////////////////////////////
470
471void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
472 const SkPaint& paint) {
473 CHECK_FOR_ANNOTATION(paint);
474 CHECK_SHOULD_DRAW(draw, false);
475
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000476 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000477 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400478
egdanield58a0ba2014-06-11 10:30:05 -0700479 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000480 if (paint.getMaskFilter()) {
481 // try to hit the fast path for drawing filtered round rects
482
483 SkRRect devRRect;
484 if (rect.transform(fContext->getMatrix(), &devRRect)) {
485 if (devRRect.allCornersCircular()) {
486 SkRect maskRect;
487 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
488 draw.fClip->getBounds(),
489 fContext->getMatrix(),
490 &maskRect)) {
491 SkIRect finalIRect;
492 maskRect.roundOut(&finalIRect);
493 if (draw.fClip->quickReject(finalIRect)) {
494 // clipped out
495 return;
496 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000497 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700498 strokeInfo.getStrokeRec(),
499 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000500 return;
501 }
502 }
503
504 }
505 }
506
507 }
508
egdanield58a0ba2014-06-11 10:30:05 -0700509 bool usePath = false;
510
511 if (paint.getMaskFilter()) {
512 usePath = true;
513 } else {
514 const SkPathEffect* pe = paint.getPathEffect();
515 if (NULL != pe && !strokeInfo.isDashed()) {
516 usePath = true;
517 }
518 }
519
520
521 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000522 SkPath path;
523 path.addRRect(rect);
524 this->drawPath(draw, path, paint, NULL, true);
525 return;
526 }
Mike Klein744fb732014-06-23 15:13:26 -0400527
egdanield58a0ba2014-06-11 10:30:05 -0700528 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000529}
530
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000531void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
532 const SkRRect& inner, const SkPaint& paint) {
533 SkStrokeRec stroke(paint);
534 if (stroke.isFillStyle()) {
535
536 CHECK_FOR_ANNOTATION(paint);
537 CHECK_SHOULD_DRAW(draw, false);
538
539 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000540 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000541
542 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
543 fContext->drawDRRect(grPaint, outer, inner);
544 return;
545 }
546 }
547
548 SkPath path;
549 path.addRRect(outer);
550 path.addRRect(inner);
551 path.setFillType(SkPath::kEvenOdd_FillType);
552
553 this->drawPath(draw, path, paint, NULL, true);
554}
555
556
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000557/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000558
559void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
560 const SkPaint& paint) {
561 CHECK_FOR_ANNOTATION(paint);
562 CHECK_SHOULD_DRAW(draw, false);
563
egdanield58a0ba2014-06-11 10:30:05 -0700564 GrStrokeInfo strokeInfo(paint);
565
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000566 bool usePath = false;
567 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700568 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000569 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700570 } else {
571 const SkPathEffect* pe = paint.getPathEffect();
572 if (NULL != pe && !strokeInfo.isDashed()) {
573 usePath = true;
574 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000575 }
576
577 if (usePath) {
578 SkPath path;
579 path.addOval(oval);
580 this->drawPath(draw, path, paint, NULL, true);
581 return;
582 }
583
584 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000585 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000586
egdanield58a0ba2014-06-11 10:30:05 -0700587 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000588}
589
590#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000591
592///////////////////////////////////////////////////////////////////////////////
593
594// helpers for applying mask filters
595namespace {
596
597// Draw a mask using the supplied paint. Since the coverage/geometry
598// is already burnt into the mask this boils down to a rect draw.
599// Return true if the mask was successfully drawn.
600bool draw_mask(GrContext* context, const SkRect& maskRect,
601 GrPaint* grp, GrTexture* mask) {
602 GrContext::AutoMatrix am;
603 if (!am.setIdentity(context, grp)) {
604 return false;
605 }
606
607 SkMatrix matrix;
608 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
609 matrix.postIDiv(mask->width(), mask->height());
610
611 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
612 context->drawRect(*grp, maskRect);
613 return true;
614}
615
616bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700617 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000618 GrPaint* grp, SkPaint::Style style) {
619 SkMask srcM, dstM;
620
621 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
622 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
623 return false;
624 }
625 SkAutoMaskFreeImage autoSrc(srcM.fImage);
626
627 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
628 return false;
629 }
630 // this will free-up dstM when we're done (allocated in filterMask())
631 SkAutoMaskFreeImage autoDst(dstM.fImage);
632
633 if (clip.quickReject(dstM.fBounds)) {
634 return false;
635 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000636
637 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
638 // the current clip (and identity matrix) and GrPaint settings
639 GrTextureDesc desc;
640 desc.fWidth = dstM.fBounds.width();
641 desc.fHeight = dstM.fBounds.height();
642 desc.fConfig = kAlpha_8_GrPixelConfig;
643
644 GrAutoScratchTexture ast(context, desc);
645 GrTexture* texture = ast.texture();
646
647 if (NULL == texture) {
648 return false;
649 }
650 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
651 dstM.fImage, dstM.fRowBytes);
652
653 SkRect maskRect = SkRect::Make(dstM.fBounds);
654
655 return draw_mask(context, maskRect, grp, texture);
656}
657
658// Create a mask of 'devPath' and place the result in 'mask'. Return true on
659// success; false otherwise.
660bool create_mask_GPU(GrContext* context,
661 const SkRect& maskRect,
662 const SkPath& devPath,
egdanield58a0ba2014-06-11 10:30:05 -0700663 const GrStrokeInfo& strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000664 bool doAA,
665 GrAutoScratchTexture* mask) {
666 GrTextureDesc desc;
667 desc.fFlags = kRenderTarget_GrTextureFlagBit;
668 desc.fWidth = SkScalarCeilToInt(maskRect.width());
669 desc.fHeight = SkScalarCeilToInt(maskRect.height());
670 // We actually only need A8, but it often isn't supported as a
671 // render target so default to RGBA_8888
672 desc.fConfig = kRGBA_8888_GrPixelConfig;
673 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
674 desc.fConfig = kAlpha_8_GrPixelConfig;
675 }
676
677 mask->set(context, desc);
678 if (NULL == mask->texture()) {
679 return false;
680 }
681
682 GrTexture* maskTexture = mask->texture();
683 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
684
685 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
686 GrContext::AutoClip ac(context, clipRect);
687
688 context->clear(NULL, 0x0, true);
689
690 GrPaint tempPaint;
691 if (doAA) {
692 tempPaint.setAntiAlias(true);
693 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
694 // blend coeff of zero requires dual source blending support in order
695 // to properly blend partially covered pixels. This means the AA
696 // code path may not be taken. So we use a dst blend coeff of ISA. We
697 // could special case AA draws to a dst surface with known alpha=0 to
698 // use a zero dst coeff when dual source blending isn't available.
699 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
700 }
701
702 GrContext::AutoMatrix am;
703
704 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
705 SkMatrix translate;
706 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
707 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700708 context->drawPath(tempPaint, devPath, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000709 return true;
710}
711
712SkBitmap wrap_texture(GrTexture* texture) {
713 SkBitmap result;
reed6c225732014-06-09 19:52:07 -0700714 result.setInfo(texture->info());
715 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000716 return result;
717}
718
719};
720
721void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
722 const SkPaint& paint, const SkMatrix* prePathMatrix,
723 bool pathIsMutable) {
724 CHECK_FOR_ANNOTATION(paint);
725 CHECK_SHOULD_DRAW(draw, false);
726
727 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000728 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000729
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000730 // If we have a prematrix, apply it to the path, optimizing for the case
731 // where the original path can in fact be modified in place (even though
732 // its parameter type is const).
733 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000734 SkTLazy<SkPath> tmpPath;
735 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000736
737 if (prePathMatrix) {
738 SkPath* result = pathPtr;
739
740 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000741 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742 pathIsMutable = true;
743 }
744 // should I push prePathMatrix on our MV stack temporarily, instead
745 // of applying it here? See SkDraw.cpp
746 pathPtr->transform(*prePathMatrix, result);
747 pathPtr = result;
748 }
749 // at this point we're done with prePathMatrix
750 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
751
egdanield58a0ba2014-06-11 10:30:05 -0700752 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000753 SkPathEffect* pathEffect = paint.getPathEffect();
754 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700755 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
756 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000757 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000758 pathPtr = effectPath.get();
759 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700760 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000761 }
762
egdanield58a0ba2014-06-11 10:30:05 -0700763 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764 if (paint.getMaskFilter()) {
765 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000766 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
767 if (stroke.applyToPath(strokedPath, *pathPtr)) {
768 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000769 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700770 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000771 }
772 }
773
774 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000775 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000776
777 // transform the path into device space
778 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000779
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000780 SkRect maskRect;
781 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
782 draw.fClip->getBounds(),
783 fContext->getMatrix(),
784 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000785 // The context's matrix may change while creating the mask, so save the CTM here to
786 // pass to filterMaskGPU.
787 const SkMatrix ctm = fContext->getMatrix();
788
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000789 SkIRect finalIRect;
790 maskRect.roundOut(&finalIRect);
791 if (draw.fClip->quickReject(finalIRect)) {
792 // clipped out
793 return;
794 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000795
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000796 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000797 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000798 // the mask filter was able to draw itself directly, so there's nothing
799 // left to do.
800 return;
801 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000802
803 GrAutoScratchTexture mask;
804
egdanield58a0ba2014-06-11 10:30:05 -0700805 if (create_mask_GPU(fContext, maskRect, *devPathPtr, strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000806 grPaint.isAntiAlias(), &mask)) {
807 GrTexture* filtered;
808
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000809 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000810 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000811 // filterMaskGPU gives us ownership of a ref to the result
812 SkAutoTUnref<GrTexture> atu(filtered);
813
814 // If the scratch texture that we used as the filter src also holds the filter
815 // result then we must detach so that this texture isn't recycled for a later
816 // draw.
817 if (filtered == mask.texture()) {
818 mask.detach();
819 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
820 }
821
822 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
823 // This path is completely drawn
824 return;
825 }
826 }
827 }
828 }
829
830 // draw the mask on the CPU - this is a fallthrough path in case the
831 // GPU path fails
832 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
833 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700834 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
835 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000836 return;
837 }
838
egdanield58a0ba2014-06-11 10:30:05 -0700839 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000840}
841
842static const int kBmpSmallTileSize = 1 << 10;
843
844static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
845 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
846 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
847 return tilesX * tilesY;
848}
849
850static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
851 if (maxTileSize <= kBmpSmallTileSize) {
852 return maxTileSize;
853 }
854
855 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
856 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
857
858 maxTileTotalTileSize *= maxTileSize * maxTileSize;
859 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
860
861 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
862 return kBmpSmallTileSize;
863 } else {
864 return maxTileSize;
865 }
866}
867
868// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
869// pixels from the bitmap are necessary.
870static void determine_clipped_src_rect(const GrContext* context,
871 const SkBitmap& bitmap,
872 const SkRect* srcRectPtr,
873 SkIRect* clippedSrcIRect) {
874 const GrClipData* clip = context->getClip();
875 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
876 SkMatrix inv;
877 if (!context->getMatrix().invert(&inv)) {
878 clippedSrcIRect->setEmpty();
879 return;
880 }
881 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
882 inv.mapRect(&clippedSrcRect);
883 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000884 // we've setup src space 0,0 to map to the top left of the src rect.
885 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000886 if (!clippedSrcRect.intersect(*srcRectPtr)) {
887 clippedSrcIRect->setEmpty();
888 return;
889 }
890 }
891 clippedSrcRect.roundOut(clippedSrcIRect);
892 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
893 if (!clippedSrcIRect->intersect(bmpBounds)) {
894 clippedSrcIRect->setEmpty();
895 }
896}
897
898bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
899 const GrTextureParams& params,
900 const SkRect* srcRectPtr,
901 int maxTileSize,
902 int* tileSize,
903 SkIRect* clippedSrcRect) const {
904 // if bitmap is explictly texture backed then just use the texture
905 if (NULL != bitmap.getTexture()) {
906 return false;
907 }
908
909 // if it's larger than the max tile size, then we have no choice but tiling.
910 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
911 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
912 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
913 return true;
914 }
915
916 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
917 return false;
918 }
919
920 // if the entire texture is already in our cache then no reason to tile it
921 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
922 return false;
923 }
924
925 // At this point we know we could do the draw by uploading the entire bitmap
926 // as a texture. However, if the texture would be large compared to the
927 // cache size and we don't require most of it for this draw then tile to
928 // reduce the amount of upload and cache spill.
929
930 // assumption here is that sw bitmap size is a good proxy for its size as
931 // a texture
932 size_t bmpSize = bitmap.getSize();
933 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000934 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000935 if (bmpSize < cacheSize / 2) {
936 return false;
937 }
938
939 // Figure out how much of the src we will need based on the src rect and clipping.
940 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
941 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
942 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
943 kBmpSmallTileSize * kBmpSmallTileSize;
944
945 return usedTileBytes < 2 * bmpSize;
946}
947
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000948void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000949 const SkBitmap& bitmap,
950 const SkMatrix& m,
951 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000952 SkMatrix concat;
953 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
954 if (!m.isIdentity()) {
955 concat.setConcat(*draw->fMatrix, m);
956 draw.writable()->fMatrix = &concat;
957 }
958 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000959}
960
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000961// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000962// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
963// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000964static inline void clamped_outset_with_offset(SkIRect* iRect,
965 int outset,
966 SkPoint* offset,
967 const SkIRect& clamp) {
968 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000969
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000970 int leftClampDelta = clamp.fLeft - iRect->fLeft;
971 if (leftClampDelta > 0) {
972 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000973 iRect->fLeft = clamp.fLeft;
974 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000975 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000976 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000977
978 int topClampDelta = clamp.fTop - iRect->fTop;
979 if (topClampDelta > 0) {
980 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000981 iRect->fTop = clamp.fTop;
982 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000983 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000984 }
985
986 if (iRect->fRight > clamp.fRight) {
987 iRect->fRight = clamp.fRight;
988 }
989 if (iRect->fBottom > clamp.fBottom) {
990 iRect->fBottom = clamp.fBottom;
991 }
992}
993
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000994static bool has_aligned_samples(const SkRect& srcRect,
995 const SkRect& transformedRect) {
996 // detect pixel disalignment
997 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
998 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
999 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1000 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1001 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1002 COLOR_BLEED_TOLERANCE &&
1003 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1004 COLOR_BLEED_TOLERANCE) {
1005 return true;
1006 }
1007 return false;
1008}
1009
1010static bool may_color_bleed(const SkRect& srcRect,
1011 const SkRect& transformedRect,
1012 const SkMatrix& m) {
1013 // Only gets called if has_aligned_samples returned false.
1014 // So we can assume that sampling is axis aligned but not texel aligned.
1015 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1016 SkRect innerSrcRect(srcRect), innerTransformedRect,
1017 outerTransformedRect(transformedRect);
1018 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1019 m.mapRect(&innerTransformedRect, innerSrcRect);
1020
1021 // The gap between outerTransformedRect and innerTransformedRect
1022 // represents the projection of the source border area, which is
1023 // problematic for color bleeding. We must check whether any
1024 // destination pixels sample the border area.
1025 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1026 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1027 SkIRect outer, inner;
1028 outerTransformedRect.round(&outer);
1029 innerTransformedRect.round(&inner);
1030 // If the inner and outer rects round to the same result, it means the
1031 // border does not overlap any pixel centers. Yay!
1032 return inner != outer;
1033}
1034
1035static bool needs_texture_domain(const SkBitmap& bitmap,
1036 const SkRect& srcRect,
1037 GrTextureParams &params,
1038 const SkMatrix& contextMatrix,
1039 bool bicubic) {
1040 bool needsTextureDomain = false;
1041
1042 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1043 // Need texture domain if drawing a sub rect
1044 needsTextureDomain = srcRect.width() < bitmap.width() ||
1045 srcRect.height() < bitmap.height();
1046 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1047 // sampling is axis-aligned
1048 SkRect transformedRect;
1049 contextMatrix.mapRect(&transformedRect, srcRect);
1050
1051 if (has_aligned_samples(srcRect, transformedRect)) {
1052 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1053 needsTextureDomain = false;
1054 } else {
1055 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1056 }
1057 }
1058 }
1059 return needsTextureDomain;
1060}
1061
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001062void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1063 const SkBitmap& bitmap,
1064 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001065 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001066 const SkPaint& paint,
1067 SkCanvas::DrawBitmapRectFlags flags) {
1068 CHECK_SHOULD_DRAW(draw, false);
1069
1070 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001071 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001072 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1073 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001074 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001075 SkScalar w = SkIntToScalar(bitmap.width());
1076 SkScalar h = SkIntToScalar(bitmap.height());
1077 dstSize.fWidth = w;
1078 dstSize.fHeight = h;
1079 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001080 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001081 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001082 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001083 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001084 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001085 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1086 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1087 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1088 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001089 }
1090
1091 if (paint.getMaskFilter()){
1092 // Convert the bitmap to a shader so that the rect can be drawn
1093 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001094 SkBitmap tmp; // subset of bitmap, if necessary
1095 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001096 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001097 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001098 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1099 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1100 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001101 // In bleed mode we position and trim the bitmap based on the src rect which is
1102 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1103 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1104 // compensate.
1105 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1106 SkIRect iSrc;
1107 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001108
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001109 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1110 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001111
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001112 if (!bitmap.extractSubset(&tmp, iSrc)) {
1113 return; // extraction failed
1114 }
1115 bitmapPtr = &tmp;
1116 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001117
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001118 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001119 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001120 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001121 } else {
1122 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001123 }
1124
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001125 SkPaint paintWithShader(paint);
1126 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001127 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001128 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1129 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001130
1131 return;
1132 }
1133
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001134 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1135 // the view matrix rather than a local matrix.
1136 SkMatrix m;
1137 m.setScale(dstSize.fWidth / srcRect.width(),
1138 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001139 fContext->concatMatrix(m);
1140
1141 GrTextureParams params;
1142 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1143 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001144
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001145 bool doBicubic = false;
1146
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001147 switch(paintFilterLevel) {
1148 case SkPaint::kNone_FilterLevel:
1149 textureFilterMode = GrTextureParams::kNone_FilterMode;
1150 break;
1151 case SkPaint::kLow_FilterLevel:
1152 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1153 break;
1154 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001155 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001156 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1157 } else {
1158 // Don't trigger MIP level generation unnecessarily.
1159 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1160 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001161 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001162 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001163 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001164 doBicubic =
1165 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001166 break;
1167 default:
1168 SkErrorInternals::SetError( kInvalidPaint_SkError,
1169 "Sorry, I don't understand the filtering "
1170 "mode you asked for. Falling back to "
1171 "MIPMaps.");
1172 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1173 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001174 }
1175
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001176 int tileFilterPad;
1177 if (doBicubic) {
1178 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1179 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1180 tileFilterPad = 0;
1181 } else {
1182 tileFilterPad = 1;
1183 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001184 params.setFilterMode(textureFilterMode);
1185
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001186 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001187 int tileSize;
1188
1189 SkIRect clippedSrcRect;
1190 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1191 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001192 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1193 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001194 } else {
1195 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001196 bool needsTextureDomain = needs_texture_domain(bitmap,
1197 srcRect,
1198 params,
1199 fContext->getMatrix(),
1200 doBicubic);
1201 this->internalDrawBitmap(bitmap,
1202 srcRect,
1203 params,
1204 paint,
1205 flags,
1206 doBicubic,
1207 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001208 }
1209}
1210
1211// Break 'bitmap' into several tiles to draw it since it has already
1212// been determined to be too large to fit in VRAM
1213void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1214 const SkRect& srcRect,
1215 const SkIRect& clippedSrcIRect,
1216 const GrTextureParams& params,
1217 const SkPaint& paint,
1218 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001219 int tileSize,
1220 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001221 // The following pixel lock is technically redundant, but it is desirable
1222 // to lock outside of the tile loop to prevent redecoding the whole image
1223 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1224 // is larger than the limit of the discardable memory pool.
1225 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001226 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1227
1228 int nx = bitmap.width() / tileSize;
1229 int ny = bitmap.height() / tileSize;
1230 for (int x = 0; x <= nx; x++) {
1231 for (int y = 0; y <= ny; y++) {
1232 SkRect tileR;
1233 tileR.set(SkIntToScalar(x * tileSize),
1234 SkIntToScalar(y * tileSize),
1235 SkIntToScalar((x + 1) * tileSize),
1236 SkIntToScalar((y + 1) * tileSize));
1237
1238 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1239 continue;
1240 }
1241
1242 if (!tileR.intersect(srcRect)) {
1243 continue;
1244 }
1245
1246 SkBitmap tmpB;
1247 SkIRect iTileR;
1248 tileR.roundOut(&iTileR);
1249 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1250 SkIntToScalar(iTileR.fTop));
1251
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001252 // Adjust the context matrix to draw at the right x,y in device space
1253 SkMatrix tmpM;
1254 GrContext::AutoMatrix am;
1255 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1256 am.setPreConcat(fContext, tmpM);
1257
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001258 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001259 SkIRect iClampRect;
1260
1261 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1262 // In bleed mode we want to always expand the tile on all edges
1263 // but stay within the bitmap bounds
1264 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1265 } else {
1266 // In texture-domain/clamp mode we only want to expand the
1267 // tile on edges interior to "srcRect" (i.e., we want to
1268 // not bleed across the original clamped edges)
1269 srcRect.roundOut(&iClampRect);
1270 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001271 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1272 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001273 }
1274
1275 if (bitmap.extractSubset(&tmpB, iTileR)) {
1276 // now offset it to make it "local" to our tmp bitmap
1277 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001278 GrTextureParams paramsTemp = params;
1279 bool needsTextureDomain = needs_texture_domain(bitmap,
1280 srcRect,
1281 paramsTemp,
1282 fContext->getMatrix(),
1283 bicubic);
1284 this->internalDrawBitmap(tmpB,
1285 tileR,
1286 paramsTemp,
1287 paint,
1288 flags,
1289 bicubic,
1290 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001291 }
1292 }
1293 }
1294}
1295
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001296
1297/*
1298 * This is called by drawBitmap(), which has to handle images that may be too
1299 * large to be represented by a single texture.
1300 *
1301 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1302 * and that non-texture portion of the GrPaint has already been setup.
1303 */
1304void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1305 const SkRect& srcRect,
1306 const GrTextureParams& params,
1307 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001308 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001309 bool bicubic,
1310 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001311 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1312 bitmap.height() <= fContext->getMaxTextureSize());
1313
1314 GrTexture* texture;
1315 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1316 if (NULL == texture) {
1317 return;
1318 }
1319
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001320 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001321 SkRect paintRect;
1322 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1323 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1324 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1325 SkScalarMul(srcRect.fTop, hInv),
1326 SkScalarMul(srcRect.fRight, wInv),
1327 SkScalarMul(srcRect.fBottom, hInv));
1328
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001329 SkRect textureDomain = SkRect::MakeEmpty();
1330 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001331 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001332 // Use a constrained texture domain to avoid color bleeding
1333 SkScalar left, top, right, bottom;
1334 if (srcRect.width() > SK_Scalar1) {
1335 SkScalar border = SK_ScalarHalf / texture->width();
1336 left = paintRect.left() + border;
1337 right = paintRect.right() - border;
1338 } else {
1339 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1340 }
1341 if (srcRect.height() > SK_Scalar1) {
1342 SkScalar border = SK_ScalarHalf / texture->height();
1343 top = paintRect.top() + border;
1344 bottom = paintRect.bottom() - border;
1345 } else {
1346 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1347 }
1348 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001349 if (bicubic) {
1350 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1351 } else {
1352 effect.reset(GrTextureDomainEffect::Create(texture,
1353 SkMatrix::I(),
1354 textureDomain,
1355 GrTextureDomain::kClamp_Mode,
1356 params.filterMode()));
1357 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001358 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001359 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1360 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1361 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001362 } else {
1363 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1364 }
1365
1366 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1367 // the rest from the SkPaint.
1368 GrPaint grPaint;
1369 grPaint.addColorEffect(effect);
reed0689d7b2014-06-14 05:30:20 -07001370 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
dandov9de5b512014-06-10 14:38:28 -07001371 GrColor grColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1372 SkColor2GrColor(paint.getColor());
1373 SkPaint2GrPaintNoShader(this->context(), paint, grColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001374
1375 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1376}
1377
1378static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001379 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001380 int w, int h, const SkImageFilter::Context& ctx,
1381 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001382 SkASSERT(filter);
1383 SkDeviceImageFilterProxy proxy(device);
1384
1385 if (filter->canFilterImageGPU()) {
1386 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1387 // filter. Also set the clip wide open and the matrix to identity.
1388 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001389 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001390 } else {
1391 return false;
1392 }
1393}
1394
1395void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1396 int left, int top, const SkPaint& paint) {
1397 // drawSprite is defined to be in device coords.
1398 CHECK_SHOULD_DRAW(draw, true);
1399
1400 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1401 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1402 return;
1403 }
1404
1405 int w = bitmap.width();
1406 int h = bitmap.height();
1407
1408 GrTexture* texture;
1409 // draw sprite uses the default texture params
1410 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1411
1412 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001413 // This bitmap will own the filtered result as a texture.
1414 SkBitmap filteredBitmap;
1415
1416 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001417 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001418 SkMatrix matrix(*draw.fMatrix);
1419 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001420 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001421 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1422 SkAutoUnref aur(cache);
1423 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001424 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001425 &offset)) {
1426 texture = (GrTexture*) filteredBitmap.getTexture();
1427 w = filteredBitmap.width();
1428 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001429 left += offset.x();
1430 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001431 } else {
1432 return;
1433 }
1434 }
1435
1436 GrPaint grPaint;
1437 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1438
dandov9de5b512014-06-10 14:38:28 -07001439 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1440 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001441
1442 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001443 SkRect::MakeXYWH(SkIntToScalar(left),
1444 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001445 SkIntToScalar(w),
1446 SkIntToScalar(h)),
1447 SkRect::MakeXYWH(0,
1448 0,
1449 SK_Scalar1 * w / texture->width(),
1450 SK_Scalar1 * h / texture->height()));
1451}
1452
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001453void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001454 const SkRect* src, const SkRect& dst,
1455 const SkPaint& paint,
1456 SkCanvas::DrawBitmapRectFlags flags) {
1457 SkMatrix matrix;
1458 SkRect bitmapBounds, tmpSrc;
1459
1460 bitmapBounds.set(0, 0,
1461 SkIntToScalar(bitmap.width()),
1462 SkIntToScalar(bitmap.height()));
1463
1464 // Compute matrix from the two rectangles
1465 if (NULL != src) {
1466 tmpSrc = *src;
1467 } else {
1468 tmpSrc = bitmapBounds;
1469 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001470
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001471 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1472
1473 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1474 if (NULL != src) {
1475 if (!bitmapBounds.contains(tmpSrc)) {
1476 if (!tmpSrc.intersect(bitmapBounds)) {
1477 return; // nothing to draw
1478 }
1479 }
1480 }
1481
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001482 SkRect tmpDst;
1483 matrix.mapRect(&tmpDst, tmpSrc);
1484
1485 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1486 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1487 // Translate so that tempDst's top left is at the origin.
1488 matrix = *origDraw.fMatrix;
1489 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1490 draw.writable()->fMatrix = &matrix;
1491 }
1492 SkSize dstSize;
1493 dstSize.fWidth = tmpDst.width();
1494 dstSize.fHeight = tmpDst.height();
1495
1496 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001497}
1498
1499void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1500 int x, int y, const SkPaint& paint) {
1501 // clear of the source device must occur before CHECK_SHOULD_DRAW
1502 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1503 if (dev->fNeedClear) {
1504 // TODO: could check here whether we really need to draw at all
1505 dev->clear(0x0);
1506 }
1507
1508 // drawDevice is defined to be in device coords.
1509 CHECK_SHOULD_DRAW(draw, true);
1510
1511 GrRenderTarget* devRT = dev->accessRenderTarget();
1512 GrTexture* devTex;
1513 if (NULL == (devTex = devRT->asTexture())) {
1514 return;
1515 }
1516
1517 const SkBitmap& bm = dev->accessBitmap(false);
1518 int w = bm.width();
1519 int h = bm.height();
1520
1521 SkImageFilter* filter = paint.getImageFilter();
1522 // This bitmap will own the filtered result as a texture.
1523 SkBitmap filteredBitmap;
1524
1525 if (NULL != filter) {
1526 SkIPoint offset = SkIPoint::Make(0, 0);
1527 SkMatrix matrix(*draw.fMatrix);
1528 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001529 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001530 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1531 SkAutoUnref aur(cache);
1532 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001533 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001534 &offset)) {
1535 devTex = filteredBitmap.getTexture();
1536 w = filteredBitmap.width();
1537 h = filteredBitmap.height();
1538 x += offset.fX;
1539 y += offset.fY;
1540 } else {
1541 return;
1542 }
1543 }
1544
1545 GrPaint grPaint;
1546 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1547
dandov9de5b512014-06-10 14:38:28 -07001548 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1549 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001550
1551 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1552 SkIntToScalar(y),
1553 SkIntToScalar(w),
1554 SkIntToScalar(h));
1555
1556 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1557 // scratch texture).
1558 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1559 SK_Scalar1 * h / devTex->height());
1560
1561 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1562}
1563
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001564bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001565 return filter->canFilterImageGPU();
1566}
1567
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001568bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001569 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001570 SkBitmap* result, SkIPoint* offset) {
1571 // want explicitly our impl, so guard against a subclass of us overriding it
1572 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1573 return false;
1574 }
1575
1576 SkAutoLockPixels alp(src, !src.getTexture());
1577 if (!src.getTexture() && !src.readyToDraw()) {
1578 return false;
1579 }
1580
1581 GrTexture* texture;
1582 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1583 // must be pushed upstack.
1584 SkAutoCachedTexture act(this, src, NULL, &texture);
1585
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001586 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1587 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001588}
1589
1590///////////////////////////////////////////////////////////////////////////////
1591
1592// must be in SkCanvas::VertexMode order
1593static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1594 kTriangles_GrPrimitiveType,
1595 kTriangleStrip_GrPrimitiveType,
1596 kTriangleFan_GrPrimitiveType,
1597};
1598
1599void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1600 int vertexCount, const SkPoint vertices[],
1601 const SkPoint texs[], const SkColor colors[],
1602 SkXfermode* xmode,
1603 const uint16_t indices[], int indexCount,
1604 const SkPaint& paint) {
1605 CHECK_SHOULD_DRAW(draw, false);
1606
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001607 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1608 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
1609 texs = NULL;
1610 SkPaint copy(paint);
1611 copy.setStyle(SkPaint::kStroke_Style);
1612 copy.setStrokeWidth(0);
1613
1614 VertState state(vertexCount, indices, indexCount);
1615 VertState::Proc vertProc = state.chooseProc(vmode);
1616
1617 SkPoint* pts = new SkPoint[vertexCount * 6];
1618 int i = 0;
1619 while (vertProc(&state)) {
1620 pts[i] = vertices[state.f0];
1621 pts[i + 1] = vertices[state.f1];
1622 pts[i + 2] = vertices[state.f1];
1623 pts[i + 3] = vertices[state.f2];
1624 pts[i + 4] = vertices[state.f2];
1625 pts[i + 5] = vertices[state.f0];
1626 i += 6;
1627 }
1628 draw.drawPoints(SkCanvas::kLines_PointMode, i, pts, copy, true);
1629 return;
1630 }
1631
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001632 GrPaint grPaint;
1633 // we ignore the shader if texs is null.
1634 if (NULL == texs) {
dandov9de5b512014-06-10 14:38:28 -07001635 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1636 NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001637 } else {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001638 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001639 }
1640
mtklein2583b622014-06-04 08:20:41 -07001641#if 0
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001642 if (NULL != xmode && NULL != texs && NULL != colors) {
1643 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1644 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001645 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001646 }
1647 }
mtklein2583b622014-06-04 08:20:41 -07001648#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001649
1650 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1651 if (NULL != colors) {
1652 // need to convert byte order and from non-PM to PM
1653 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001654 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001655 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001656 color = colors[i];
1657 if (paint.getAlpha() != 255) {
1658 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1659 }
1660 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001661 }
1662 colors = convertedColors.get();
1663 }
1664 fContext->drawVertices(grPaint,
1665 gVertexMode2PrimitiveType[vmode],
1666 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001667 vertices,
1668 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001669 colors,
1670 indices,
1671 indexCount);
1672}
1673
1674///////////////////////////////////////////////////////////////////////////////
1675
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001676void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1677 size_t byteLength, SkScalar x, SkScalar y,
1678 const SkPaint& paint) {
1679 CHECK_SHOULD_DRAW(draw, false);
1680
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001681 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001682 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001683 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001684
1685 SkDEBUGCODE(this->validate();)
1686
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001687 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1688 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001689 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001690 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001691
1692 SkDEBUGCODE(this->validate();)
1693
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001694 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001695 } else {
1696 // this guy will just call our drawPath()
1697 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001698 }
1699}
1700
1701void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1702 size_t byteLength, const SkScalar pos[],
1703 SkScalar constY, int scalarsPerPos,
1704 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001705 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001706 CHECK_SHOULD_DRAW(draw, false);
1707
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001708 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001709 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001710 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001711
1712 SkDEBUGCODE(this->validate();)
1713
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001714 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001715 constY, scalarsPerPos);
1716 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001717 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001718 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001719
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001720 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001721
1722 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001723 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001724 } else {
1725 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1726 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001727 }
1728}
1729
1730void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1731 size_t len, const SkPath& path,
1732 const SkMatrix* m, const SkPaint& paint) {
1733 CHECK_SHOULD_DRAW(draw, false);
1734
1735 SkASSERT(draw.fDevice == this);
1736 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1737}
1738
1739///////////////////////////////////////////////////////////////////////////////
1740
1741bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1742 if (!paint.isLCDRenderText()) {
1743 // we're cool with the paint as is
1744 return false;
1745 }
1746
1747 if (paint.getShader() ||
1748 paint.getXfermode() || // unless its srcover
1749 paint.getMaskFilter() ||
1750 paint.getRasterizer() ||
1751 paint.getColorFilter() ||
1752 paint.getPathEffect() ||
1753 paint.isFakeBoldText() ||
1754 paint.getStyle() != SkPaint::kFill_Style) {
1755 // turn off lcd
1756 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1757 flags->fHinting = paint.getHinting();
1758 return true;
1759 }
1760 // we're cool with the paint as is
1761 return false;
1762}
1763
1764void SkGpuDevice::flush() {
1765 DO_DEFERRED_CLEAR();
1766 fContext->resolveRenderTarget(fRenderTarget);
1767}
1768
1769///////////////////////////////////////////////////////////////////////////////
1770
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001771SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001772 GrTextureDesc desc;
1773 desc.fConfig = fRenderTarget->config();
1774 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001775 desc.fWidth = info.width();
1776 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001777 desc.fSampleCnt = fRenderTarget->numSamples();
1778
1779 SkAutoTUnref<GrTexture> texture;
1780 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001781 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001782
1783#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1784 // layers are never draw in repeat modes, so we can request an approx
1785 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001786 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001787 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1788 GrContext::kApprox_ScratchTexMatch :
1789 GrContext::kExact_ScratchTexMatch;
1790 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1791#else
1792 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1793#endif
1794 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001795 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001796 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001797 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1798 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001799 return NULL;
1800 }
1801}
1802
reed@google.com76f10a32014-02-05 15:32:21 +00001803SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1804 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1805}
1806
robertphillips9b14f262014-06-04 05:40:44 -07001807void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001808 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001809
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001810 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1811 if (NULL != existing) {
1812 return;
1813 }
1814
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001815 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001816
1817 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001818
1819 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001820}
1821
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001822static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1823 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001824 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001825 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1826}
1827
robertphillips9b14f262014-06-04 05:40:44 -07001828void SkGpuDevice::EXPERIMENTAL_purge(const SkPicture* picture) {
robertphillips952841b2014-06-30 08:26:50 -07001829 fContext->getLayerCache()->purge(picture);
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001830}
1831
robertphillips9b14f262014-06-04 05:40:44 -07001832bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001833
robertphillipsdb539902014-07-01 08:47:04 -07001834 if (NULL == picture->fData.get()) {
robertphillips4ec84da2014-06-24 13:10:43 -07001835 return false;
1836 }
1837
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001838 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001839
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001840 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001841 if (NULL == data) {
1842 return false;
1843 }
1844
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001845 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001846
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001847 if (0 == gpuData->numSaveLayers()) {
1848 return false;
1849 }
1850
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001851 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1852 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1853 pullForward[i] = false;
1854 }
1855
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001856 SkRect clipBounds;
1857 if (!canvas->getClipBounds(&clipBounds)) {
1858 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001859 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001860 SkIRect query;
1861 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001862
robertphillipsce4dd3d2014-07-07 13:46:35 -07001863 SkAutoTDelete<const SkPicture::OperationList> ops(picture->EXPERIMENTAL_getActiveOps(query));
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001864
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001865 // This code pre-renders the entire layer since it will be cached and potentially
1866 // reused with different clips (e.g., in different tiles). Because of this the
1867 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1868 // is used to limit which clips are pre-rendered.
1869 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001870
robertphillipsce4dd3d2014-07-07 13:46:35 -07001871 if (NULL != ops.get()) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001872 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001873 // the BBH to limit the pre-rendering to just the layers needed to cover
1874 // the region being drawn
robertphillipsce4dd3d2014-07-07 13:46:35 -07001875 for (int i = 0; i < ops->numOps(); ++i) {
1876 uint32_t offset = ops->offset(i);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001877
1878 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001879 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001880 // their parent in the list.
1881 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1882 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1883
1884 if (pullForward[j]) {
1885 continue; // already pulling forward
1886 }
1887
1888 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1889 continue; // the op isn't in this range
1890 }
1891
1892 // TODO: once this code is more stable unsuitable layers can
1893 // just be omitted during the optimization stage
1894 if (!info.fValid ||
1895 kSaveLayerMaxSize < info.fSize.fWidth ||
1896 kSaveLayerMaxSize < info.fSize.fHeight ||
1897 info.fIsNested) {
1898 continue; // this layer is unsuitable
1899 }
1900
1901 pullForward[j] = true;
1902 }
1903 }
1904 } else {
1905 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001906 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001907 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1908 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1909
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001910 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1911 info.fOffset.fY,
1912 info.fSize.fWidth,
1913 info.fSize.fHeight);
1914
1915 if (!SkIRect::Intersects(query, layerRect)) {
1916 continue;
1917 }
1918
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001919 // TODO: once this code is more stable unsuitable layers can
1920 // just be omitted during the optimization stage
1921 if (!info.fValid ||
1922 kSaveLayerMaxSize < info.fSize.fWidth ||
1923 kSaveLayerMaxSize < info.fSize.fHeight ||
1924 info.fIsNested) {
1925 continue;
1926 }
1927
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001928 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001929 }
1930 }
1931
robertphillipsce4dd3d2014-07-07 13:46:35 -07001932 SkPicturePlayback::PlaybackReplacements replacements;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001933
robertphillips4ec84da2014-06-24 13:10:43 -07001934 // Generate the layer and/or ensure it is locked
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001935 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1936 if (pullForward[i]) {
1937 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1938
1939 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1940
robertphillipsce4dd3d2014-07-07 13:46:35 -07001941 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001942 replacements.push();
robertphillips4ec84da2014-06-24 13:10:43 -07001943 layerInfo->fStart = info.fSaveLayerOpID;
1944 layerInfo->fStop = info.fRestoreOpID;
1945 layerInfo->fPos = info.fOffset;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001946
robertphillips4ec84da2014-06-24 13:10:43 -07001947 GrTextureDesc desc;
1948 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1949 desc.fWidth = info.fSize.fWidth;
1950 desc.fHeight = info.fSize.fHeight;
1951 desc.fConfig = kSkia8888_GrPixelConfig;
1952 // TODO: need to deal with sample count
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001953
robertphillips4ec84da2014-06-24 13:10:43 -07001954 bool needsRendering = !fContext->getLayerCache()->lock(layer, desc);
robertphillips952841b2014-06-30 08:26:50 -07001955 if (NULL == layer->texture()) {
robertphillips4ec84da2014-06-24 13:10:43 -07001956 continue;
1957 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001958
robertphillips4ec84da2014-06-24 13:10:43 -07001959 layerInfo->fBM = SkNEW(SkBitmap); // fBM is allocated so ReplacementInfo can be POD
robertphillips952841b2014-06-30 08:26:50 -07001960 wrap_texture(layer->texture(),
1961 layer->rect().isEmpty() ? desc.fWidth : layer->texture()->width(),
1962 layer->rect().isEmpty() ? desc.fHeight : layer->texture()->height(),
1963 layerInfo->fBM);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001964
robertphillips4ec84da2014-06-24 13:10:43 -07001965 SkASSERT(info.fPaint);
1966 layerInfo->fPaint = info.fPaint;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001967
robertphillips952841b2014-06-30 08:26:50 -07001968 if (layer->rect().isEmpty()) {
1969 layerInfo->fSrcRect = SkIRect::MakeWH(desc.fWidth, desc.fHeight);
1970 } else {
1971 layerInfo->fSrcRect = SkIRect::MakeXYWH(layer->rect().fLeft,
1972 layer->rect().fTop,
1973 layer->rect().width(),
1974 layer->rect().height());
1975 }
1976
robertphillips4ec84da2014-06-24 13:10:43 -07001977 if (needsRendering) {
1978 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
robertphillips952841b2014-06-30 08:26:50 -07001979 layer->texture()->asRenderTarget(),
1980 SkSurface::kStandard_TextRenderMode,
1981 SkSurface::kDontClear_RenderTargetFlag));
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001982
robertphillips4ec84da2014-06-24 13:10:43 -07001983 SkCanvas* canvas = surface->getCanvas();
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001984
robertphillips952841b2014-06-30 08:26:50 -07001985 if (!layer->rect().isEmpty()) {
1986 // Add a rect clip to make sure the rendering doesn't
1987 // extend beyond the boundaries of the atlased sub-rect
1988 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft),
1989 SkIntToScalar(layer->rect().fTop),
1990 SkIntToScalar(layer->rect().width()),
1991 SkIntToScalar(layer->rect().height()));
1992 canvas->clipRect(bound);
1993 // Since 'clear' doesn't respect the clip we need to draw a rect
1994 // TODO: ensure none of the atlased layers contain a clear call!
1995 SkPaint paint;
1996 paint.setColor(SK_ColorTRANSPARENT);
1997 canvas->drawRect(bound, paint);
1998 } else {
1999 canvas->clear(SK_ColorTRANSPARENT);
2000 }
2001
robertphillips4ec84da2014-06-24 13:10:43 -07002002 canvas->setMatrix(info.fCTM);
robertphillips952841b2014-06-30 08:26:50 -07002003
2004 if (!layer->rect().isEmpty()) {
2005 // info.fCTM maps the layer's top/left to the origin.
2006 // Since this layer is atlased the top/left corner needs
2007 // to be offset to some arbitrary location in the backing
2008 // texture.
2009 canvas->translate(SkIntToScalar(layer->rect().fLeft),
2010 SkIntToScalar(layer->rect().fTop));
2011 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002012
robertphillipsce4dd3d2014-07-07 13:46:35 -07002013 SkPicturePlayback playback(picture);
2014 playback.setDrawLimits(info.fSaveLayerOpID, info.fRestoreOpID);
2015 playback.draw(canvas, NULL);
robertphillips952841b2014-06-30 08:26:50 -07002016
robertphillips4ec84da2014-06-24 13:10:43 -07002017 canvas->flush();
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00002018 }
2019 }
2020 }
2021
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002022 // Playback using new layers
robertphillipsce4dd3d2014-07-07 13:46:35 -07002023 SkPicturePlayback playback(picture);
2024
2025 playback.setReplacements(&replacements);
2026 playback.draw(canvas, NULL);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002027
robertphillips4ec84da2014-06-24 13:10:43 -07002028 // unlock the layers
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002029 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
robertphillips4ec84da2014-06-24 13:10:43 -07002030 GrCachedLayer* layer = fContext->getLayerCache()->findLayer(picture, i);
2031 fContext->getLayerCache()->unlock(layer);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002032 }
2033
2034 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002035}