blob: ec471ecc681188041ee0fc13d69efaa256020b23 [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"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000020
21#include "SkGrTexturePixelRef.h"
22
commit-bot@chromium.org82139702014-03-10 22:53:20 +000023#include "SkBounder.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000024#include "SkDeviceImageFilterProxy.h"
25#include "SkDrawProcs.h"
26#include "SkGlyphCache.h"
27#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000028#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000029#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000030#include "SkPicture.h"
robertphillips@google.combeb1af22014-05-07 21:31:09 +000031#include "SkPicturePlayback.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000032#include "SkRRect.h"
33#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000034#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000035#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000036#include "SkUtils.h"
37#include "SkErrorInternals.h"
38
39#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
40
41#if 0
42 extern bool (*gShouldDrawProc)();
43 #define CHECK_SHOULD_DRAW(draw, forceI) \
44 do { \
45 if (gShouldDrawProc && !gShouldDrawProc()) return; \
46 this->prepareDraw(draw, forceI); \
47 } while (0)
48#else
49 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
50#endif
51
52// This constant represents the screen alignment criterion in texels for
53// requiring texture domain clamping to prevent color bleeding when drawing
54// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000055#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000056
57#define DO_DEFERRED_CLEAR() \
58 do { \
59 if (fNeedClear) { \
60 this->clear(SK_ColorTRANSPARENT); \
61 } \
62 } while (false) \
63
64///////////////////////////////////////////////////////////////////////////////
65
66#define CHECK_FOR_ANNOTATION(paint) \
67 do { if (paint.getAnnotation()) { return; } } while (0)
68
69///////////////////////////////////////////////////////////////////////////////
70
71
72class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
73public:
74 SkAutoCachedTexture()
75 : fDevice(NULL)
76 , fTexture(NULL) {
77 }
78
79 SkAutoCachedTexture(SkGpuDevice* device,
80 const SkBitmap& bitmap,
81 const GrTextureParams* params,
82 GrTexture** texture)
83 : fDevice(NULL)
84 , fTexture(NULL) {
85 SkASSERT(NULL != texture);
86 *texture = this->set(device, bitmap, params);
87 }
88
89 ~SkAutoCachedTexture() {
90 if (NULL != fTexture) {
91 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
92 }
93 }
94
95 GrTexture* set(SkGpuDevice* device,
96 const SkBitmap& bitmap,
97 const GrTextureParams* params) {
98 if (NULL != fTexture) {
99 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
100 fTexture = NULL;
101 }
102 fDevice = device;
103 GrTexture* result = (GrTexture*)bitmap.getTexture();
104 if (NULL == result) {
105 // Cannot return the native texture so look it up in our cache
106 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
107 result = fTexture;
108 }
109 return result;
110 }
111
112private:
113 SkGpuDevice* fDevice;
114 GrTexture* fTexture;
115};
116
117///////////////////////////////////////////////////////////////////////////////
118
119struct GrSkDrawProcs : public SkDrawProcs {
120public:
121 GrContext* fContext;
122 GrTextContext* fTextContext;
123 GrFontScaler* fFontScaler; // cached in the skia glyphcache
124};
125
126///////////////////////////////////////////////////////////////////////////////
127
128static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
129 switch (config) {
130 case kAlpha_8_GrPixelConfig:
131 *isOpaque = false;
132 return SkBitmap::kA8_Config;
133 case kRGB_565_GrPixelConfig:
134 *isOpaque = true;
135 return SkBitmap::kRGB_565_Config;
136 case kRGBA_4444_GrPixelConfig:
137 *isOpaque = false;
138 return SkBitmap::kARGB_4444_Config;
139 case kSkia8888_GrPixelConfig:
140 // we don't currently have a way of knowing whether
141 // a 8888 is opaque based on the config.
142 *isOpaque = false;
143 return SkBitmap::kARGB_8888_Config;
144 default:
145 *isOpaque = false;
146 return SkBitmap::kNo_Config;
147 }
148}
149
150/*
151 * GrRenderTarget does not know its opaqueness, only its config, so we have
152 * to make conservative guesses when we return an "equivalent" bitmap.
153 */
154static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
155 bool isOpaque;
156 SkBitmap::Config config = grConfig2skConfig(renderTarget->config(), &isOpaque);
157
158 SkBitmap bitmap;
159 bitmap.setConfig(config, renderTarget->width(), renderTarget->height(), 0,
160 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
161 return bitmap;
162}
163
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000164SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000165 SkASSERT(NULL != surface);
166 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
167 return NULL;
168 }
169 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000170 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000171 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000172 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000173 }
174}
175
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000176SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000177 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000178 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000179}
180
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000181SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000182 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000183 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000184}
185
186void SkGpuDevice::initFromRenderTarget(GrContext* context,
187 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000188 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000189 fDrawProcs = NULL;
190
191 fContext = context;
192 fContext->ref();
193
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +0000194 bool useDFFonts = !!(flags & kDFFonts_Flag);
195 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties,
196 useDFFonts));
commit-bot@chromium.org47841822014-03-27 14:19:17 +0000197 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
198
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000199 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000200 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000201
202 SkASSERT(NULL != renderTarget);
203 fRenderTarget = renderTarget;
204 fRenderTarget->ref();
205
206 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
207 // on the RT but not vice-versa.
208 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
209 // busting chrome (for a currently unknown reason).
210 GrSurface* surface = fRenderTarget->asTexture();
211 if (NULL == surface) {
212 surface = fRenderTarget;
213 }
reed@google.combf790232013-12-13 19:45:58 +0000214
215 SkImageInfo info;
216 surface->asImageInfo(&info);
bungeman@google.coma95a0662014-03-19 23:26:50 +0000217 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, SkToBool(flags & kCached_Flag)));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000218
reed@google.com672588b2014-01-08 15:42:01 +0000219 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000220}
221
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000222SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
223 int sampleCount) {
224 if (kUnknown_SkColorType == origInfo.colorType() ||
225 origInfo.width() < 0 || origInfo.height() < 0) {
226 return NULL;
227 }
228
229 SkImageInfo info = origInfo;
230 // TODO: perhas we can loosen this check now that colortype is more detailed
231 // e.g. can we support both RGBA and BGRA here?
232 if (kRGB_565_SkColorType == info.colorType()) {
233 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
234 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000235 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000236 if (kOpaque_SkAlphaType != info.alphaType()) {
237 info.fAlphaType = kPremul_SkAlphaType; // force this setting
238 }
239 }
240
241 GrTextureDesc desc;
242 desc.fFlags = kRenderTarget_GrTextureFlagBit;
243 desc.fWidth = info.width();
244 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000245 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000246 desc.fSampleCnt = sampleCount;
247
248 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
249 if (!texture.get()) {
250 return NULL;
251 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000252
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000253 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
254}
255
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000256SkGpuDevice::~SkGpuDevice() {
257 if (fDrawProcs) {
258 delete fDrawProcs;
259 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000260
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000261 delete fMainTextContext;
262 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000263
264 // The GrContext takes a ref on the target. We don't want to cause the render
265 // target to be unnecessarily kept alive.
266 if (fContext->getRenderTarget() == fRenderTarget) {
267 fContext->setRenderTarget(NULL);
268 }
269
270 if (fContext->getClip() == &fClipData) {
271 fContext->setClip(NULL);
272 }
273
274 SkSafeUnref(fRenderTarget);
275 fContext->unref();
276}
277
278///////////////////////////////////////////////////////////////////////////////
279
280void SkGpuDevice::makeRenderTargetCurrent() {
281 DO_DEFERRED_CLEAR();
282 fContext->setRenderTarget(fRenderTarget);
283}
284
285///////////////////////////////////////////////////////////////////////////////
286
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000287bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
288 int x, int y) {
289 DO_DEFERRED_CLEAR();
290
291 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000292 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000293 if (kUnknown_GrPixelConfig == config) {
294 return false;
295 }
296
297 uint32_t flags = 0;
298 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
299 flags = GrContext::kUnpremul_PixelOpsFlag;
300 }
301 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
302 config, dstPixels, dstRowBytes, flags);
303}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000304
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000305bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
306 int x, int y) {
307 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000308 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000309 if (kUnknown_GrPixelConfig == config) {
310 return false;
311 }
312 uint32_t flags = 0;
313 if (kUnpremul_SkAlphaType == info.alphaType()) {
314 flags = GrContext::kUnpremul_PixelOpsFlag;
315 }
316 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
317
318 // need to bump our genID for compatibility with clients that "know" we have a bitmap
319 this->onAccessBitmap().notifyPixelsChanged();
320
321 return true;
322}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000324const SkBitmap& SkGpuDevice::onAccessBitmap() {
325 DO_DEFERRED_CLEAR();
326 return INHERITED::onAccessBitmap();
327}
328
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000329void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
330 INHERITED::onAttachToCanvas(canvas);
331
332 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
333 fClipData.fClipStack = canvas->getClipStack();
334}
335
336void SkGpuDevice::onDetachFromCanvas() {
337 INHERITED::onDetachFromCanvas();
338 fClipData.fClipStack = NULL;
339}
340
341// call this every draw call, to ensure that the context reflects our state,
342// and not the state from some other canvas/device
343void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
344 SkASSERT(NULL != fClipData.fClipStack);
345
346 fContext->setRenderTarget(fRenderTarget);
347
348 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
349
350 if (forceIdentity) {
351 fContext->setIdentityMatrix();
352 } else {
353 fContext->setMatrix(*draw.fMatrix);
354 }
355 fClipData.fOrigin = this->getOrigin();
356
357 fContext->setClip(&fClipData);
358
359 DO_DEFERRED_CLEAR();
360}
361
362GrRenderTarget* SkGpuDevice::accessRenderTarget() {
363 DO_DEFERRED_CLEAR();
364 return fRenderTarget;
365}
366
367///////////////////////////////////////////////////////////////////////////////
368
369SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
370SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
371SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
372SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
373SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
374 shader_type_mismatch);
375SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
376 shader_type_mismatch);
377SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
378SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
379
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000380///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000381
382SkBitmap::Config SkGpuDevice::config() const {
383 if (NULL == fRenderTarget) {
384 return SkBitmap::kNo_Config;
385 }
386
387 bool isOpaque;
388 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
389}
390
391void SkGpuDevice::clear(SkColor color) {
392 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
393 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
394 fNeedClear = false;
395}
396
397void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
398 CHECK_SHOULD_DRAW(draw, false);
399
400 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000401 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000402
403 fContext->drawPaint(grPaint);
404}
405
406// must be in SkCanvas::PointMode order
407static const GrPrimitiveType gPointMode2PrimtiveType[] = {
408 kPoints_GrPrimitiveType,
409 kLines_GrPrimitiveType,
410 kLineStrip_GrPrimitiveType
411};
412
413void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
414 size_t count, const SkPoint pts[], const SkPaint& paint) {
415 CHECK_FOR_ANNOTATION(paint);
416 CHECK_SHOULD_DRAW(draw, false);
417
418 SkScalar width = paint.getStrokeWidth();
419 if (width < 0) {
420 return;
421 }
422
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000423 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000424 if (GrDashingEffect::DrawDashLine(pts, paint, this->context())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000425 return;
426 }
427 }
428
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000429 // we only handle hairlines and paints without path effects or mask filters,
430 // else we let the SkDraw call our drawPath()
431 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
432 draw.drawPoints(mode, count, pts, paint, true);
433 return;
434 }
435
436 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000437 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000438
439 fContext->drawVertices(grPaint,
440 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000441 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000442 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000443 NULL,
444 NULL,
445 NULL,
446 0);
447}
448
449///////////////////////////////////////////////////////////////////////////////
450
451void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
452 const SkPaint& paint) {
453 CHECK_FOR_ANNOTATION(paint);
454 CHECK_SHOULD_DRAW(draw, false);
455
456 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
457 SkScalar width = paint.getStrokeWidth();
458
459 /*
460 We have special code for hairline strokes, miter-strokes, bevel-stroke
461 and fills. Anything else we just call our path code.
462 */
463 bool usePath = doStroke && width > 0 &&
464 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
465 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
466 // another two reasons we might need to call drawPath...
467 if (paint.getMaskFilter() || paint.getPathEffect()) {
468 usePath = true;
469 }
470 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
471#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
472 if (doStroke) {
473#endif
474 usePath = true;
475#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
476 } else {
477 usePath = !fContext->getMatrix().preservesRightAngles();
478 }
479#endif
480 }
481 // until we can both stroke and fill rectangles
482 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
483 usePath = true;
484 }
485
486 if (usePath) {
487 SkPath path;
488 path.addRect(rect);
489 this->drawPath(draw, path, paint, NULL, true);
490 return;
491 }
492
493 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000494 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000495
496 if (!doStroke) {
497 fContext->drawRect(grPaint, rect);
498 } else {
499 SkStrokeRec stroke(paint);
500 fContext->drawRect(grPaint, rect, &stroke);
501 }
502}
503
504///////////////////////////////////////////////////////////////////////////////
505
506void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
507 const SkPaint& paint) {
508 CHECK_FOR_ANNOTATION(paint);
509 CHECK_SHOULD_DRAW(draw, false);
510
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000511 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000512 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000513
514 SkStrokeRec stroke(paint);
515 if (paint.getMaskFilter()) {
516 // try to hit the fast path for drawing filtered round rects
517
518 SkRRect devRRect;
519 if (rect.transform(fContext->getMatrix(), &devRRect)) {
520 if (devRRect.allCornersCircular()) {
521 SkRect maskRect;
522 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
523 draw.fClip->getBounds(),
524 fContext->getMatrix(),
525 &maskRect)) {
526 SkIRect finalIRect;
527 maskRect.roundOut(&finalIRect);
528 if (draw.fClip->quickReject(finalIRect)) {
529 // clipped out
530 return;
531 }
532 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
533 // nothing to draw
534 return;
535 }
536 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
537 stroke, devRRect)) {
538 return;
539 }
540 }
541
542 }
543 }
544
545 }
546
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000547 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000548 SkPath path;
549 path.addRRect(rect);
550 this->drawPath(draw, path, paint, NULL, true);
551 return;
552 }
553
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000554 fContext->drawRRect(grPaint, rect, stroke);
555}
556
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000557void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
558 const SkRRect& inner, const SkPaint& paint) {
559 SkStrokeRec stroke(paint);
560 if (stroke.isFillStyle()) {
561
562 CHECK_FOR_ANNOTATION(paint);
563 CHECK_SHOULD_DRAW(draw, false);
564
565 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000566 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000567
568 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
569 fContext->drawDRRect(grPaint, outer, inner);
570 return;
571 }
572 }
573
574 SkPath path;
575 path.addRRect(outer);
576 path.addRRect(inner);
577 path.setFillType(SkPath::kEvenOdd_FillType);
578
579 this->drawPath(draw, path, paint, NULL, true);
580}
581
582
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000583/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000584
585void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
586 const SkPaint& paint) {
587 CHECK_FOR_ANNOTATION(paint);
588 CHECK_SHOULD_DRAW(draw, false);
589
590 bool usePath = false;
591 // some basic reasons we might need to call drawPath...
592 if (paint.getMaskFilter() || paint.getPathEffect()) {
593 usePath = true;
594 }
595
596 if (usePath) {
597 SkPath path;
598 path.addOval(oval);
599 this->drawPath(draw, path, paint, NULL, true);
600 return;
601 }
602
603 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000604 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000605 SkStrokeRec stroke(paint);
606
607 fContext->drawOval(grPaint, oval, stroke);
608}
609
610#include "SkMaskFilter.h"
611#include "SkBounder.h"
612
613///////////////////////////////////////////////////////////////////////////////
614
615// helpers for applying mask filters
616namespace {
617
618// Draw a mask using the supplied paint. Since the coverage/geometry
619// is already burnt into the mask this boils down to a rect draw.
620// Return true if the mask was successfully drawn.
621bool draw_mask(GrContext* context, const SkRect& maskRect,
622 GrPaint* grp, GrTexture* mask) {
623 GrContext::AutoMatrix am;
624 if (!am.setIdentity(context, grp)) {
625 return false;
626 }
627
628 SkMatrix matrix;
629 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
630 matrix.postIDiv(mask->width(), mask->height());
631
632 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
633 context->drawRect(*grp, maskRect);
634 return true;
635}
636
637bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
638 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
639 GrPaint* grp, SkPaint::Style style) {
640 SkMask srcM, dstM;
641
642 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
643 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
644 return false;
645 }
646 SkAutoMaskFreeImage autoSrc(srcM.fImage);
647
648 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
649 return false;
650 }
651 // this will free-up dstM when we're done (allocated in filterMask())
652 SkAutoMaskFreeImage autoDst(dstM.fImage);
653
654 if (clip.quickReject(dstM.fBounds)) {
655 return false;
656 }
657 if (bounder && !bounder->doIRect(dstM.fBounds)) {
658 return false;
659 }
660
661 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
662 // the current clip (and identity matrix) and GrPaint settings
663 GrTextureDesc desc;
664 desc.fWidth = dstM.fBounds.width();
665 desc.fHeight = dstM.fBounds.height();
666 desc.fConfig = kAlpha_8_GrPixelConfig;
667
668 GrAutoScratchTexture ast(context, desc);
669 GrTexture* texture = ast.texture();
670
671 if (NULL == texture) {
672 return false;
673 }
674 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
675 dstM.fImage, dstM.fRowBytes);
676
677 SkRect maskRect = SkRect::Make(dstM.fBounds);
678
679 return draw_mask(context, maskRect, grp, texture);
680}
681
682// Create a mask of 'devPath' and place the result in 'mask'. Return true on
683// success; false otherwise.
684bool create_mask_GPU(GrContext* context,
685 const SkRect& maskRect,
686 const SkPath& devPath,
687 const SkStrokeRec& stroke,
688 bool doAA,
689 GrAutoScratchTexture* mask) {
690 GrTextureDesc desc;
691 desc.fFlags = kRenderTarget_GrTextureFlagBit;
692 desc.fWidth = SkScalarCeilToInt(maskRect.width());
693 desc.fHeight = SkScalarCeilToInt(maskRect.height());
694 // We actually only need A8, but it often isn't supported as a
695 // render target so default to RGBA_8888
696 desc.fConfig = kRGBA_8888_GrPixelConfig;
697 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
698 desc.fConfig = kAlpha_8_GrPixelConfig;
699 }
700
701 mask->set(context, desc);
702 if (NULL == mask->texture()) {
703 return false;
704 }
705
706 GrTexture* maskTexture = mask->texture();
707 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
708
709 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
710 GrContext::AutoClip ac(context, clipRect);
711
712 context->clear(NULL, 0x0, true);
713
714 GrPaint tempPaint;
715 if (doAA) {
716 tempPaint.setAntiAlias(true);
717 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
718 // blend coeff of zero requires dual source blending support in order
719 // to properly blend partially covered pixels. This means the AA
720 // code path may not be taken. So we use a dst blend coeff of ISA. We
721 // could special case AA draws to a dst surface with known alpha=0 to
722 // use a zero dst coeff when dual source blending isn't available.
723 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
724 }
725
726 GrContext::AutoMatrix am;
727
728 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
729 SkMatrix translate;
730 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
731 am.set(context, translate);
732 context->drawPath(tempPaint, devPath, stroke);
733 return true;
734}
735
736SkBitmap wrap_texture(GrTexture* texture) {
reed@google.combf790232013-12-13 19:45:58 +0000737 SkImageInfo info;
738 texture->asImageInfo(&info);
739
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000740 SkBitmap result;
reed@google.combf790232013-12-13 19:45:58 +0000741 result.setConfig(info);
742 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000743 return result;
744}
745
746};
747
748void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
749 const SkPaint& paint, const SkMatrix* prePathMatrix,
750 bool pathIsMutable) {
751 CHECK_FOR_ANNOTATION(paint);
752 CHECK_SHOULD_DRAW(draw, false);
753
754 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000755 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000756
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000757 // If we have a prematrix, apply it to the path, optimizing for the case
758 // where the original path can in fact be modified in place (even though
759 // its parameter type is const).
760 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000761 SkTLazy<SkPath> tmpPath;
762 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000763
764 if (prePathMatrix) {
765 SkPath* result = pathPtr;
766
767 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000768 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000769 pathIsMutable = true;
770 }
771 // should I push prePathMatrix on our MV stack temporarily, instead
772 // of applying it here? See SkDraw.cpp
773 pathPtr->transform(*prePathMatrix, result);
774 pathPtr = result;
775 }
776 // at this point we're done with prePathMatrix
777 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
778
779 SkStrokeRec stroke(paint);
780 SkPathEffect* pathEffect = paint.getPathEffect();
781 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000782 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000783 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000784 pathPtr = effectPath.get();
785 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786 }
787
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000788 if (paint.getMaskFilter()) {
789 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000790 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
791 if (stroke.applyToPath(strokedPath, *pathPtr)) {
792 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000793 pathIsMutable = true;
794 stroke.setFillStyle();
795 }
796 }
797
798 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000799 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000800
801 // transform the path into device space
802 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000803
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000804 SkRect maskRect;
805 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
806 draw.fClip->getBounds(),
807 fContext->getMatrix(),
808 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000809 // The context's matrix may change while creating the mask, so save the CTM here to
810 // pass to filterMaskGPU.
811 const SkMatrix ctm = fContext->getMatrix();
812
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000813 SkIRect finalIRect;
814 maskRect.roundOut(&finalIRect);
815 if (draw.fClip->quickReject(finalIRect)) {
816 // clipped out
817 return;
818 }
819 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
820 // nothing to draw
821 return;
822 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000823
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000824 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000825 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000826 // the mask filter was able to draw itself directly, so there's nothing
827 // left to do.
828 return;
829 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000830
831 GrAutoScratchTexture mask;
832
833 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
834 grPaint.isAntiAlias(), &mask)) {
835 GrTexture* filtered;
836
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000837 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000838 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000839 // filterMaskGPU gives us ownership of a ref to the result
840 SkAutoTUnref<GrTexture> atu(filtered);
841
842 // If the scratch texture that we used as the filter src also holds the filter
843 // result then we must detach so that this texture isn't recycled for a later
844 // draw.
845 if (filtered == mask.texture()) {
846 mask.detach();
847 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
848 }
849
850 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
851 // This path is completely drawn
852 return;
853 }
854 }
855 }
856 }
857
858 // draw the mask on the CPU - this is a fallthrough path in case the
859 // GPU path fails
860 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
861 SkPaint::kFill_Style;
862 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
863 *draw.fClip, draw.fBounder, &grPaint, style);
864 return;
865 }
866
867 fContext->drawPath(grPaint, *pathPtr, stroke);
868}
869
870static const int kBmpSmallTileSize = 1 << 10;
871
872static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
873 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
874 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
875 return tilesX * tilesY;
876}
877
878static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
879 if (maxTileSize <= kBmpSmallTileSize) {
880 return maxTileSize;
881 }
882
883 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
884 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
885
886 maxTileTotalTileSize *= maxTileSize * maxTileSize;
887 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
888
889 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
890 return kBmpSmallTileSize;
891 } else {
892 return maxTileSize;
893 }
894}
895
896// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
897// pixels from the bitmap are necessary.
898static void determine_clipped_src_rect(const GrContext* context,
899 const SkBitmap& bitmap,
900 const SkRect* srcRectPtr,
901 SkIRect* clippedSrcIRect) {
902 const GrClipData* clip = context->getClip();
903 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
904 SkMatrix inv;
905 if (!context->getMatrix().invert(&inv)) {
906 clippedSrcIRect->setEmpty();
907 return;
908 }
909 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
910 inv.mapRect(&clippedSrcRect);
911 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000912 // we've setup src space 0,0 to map to the top left of the src rect.
913 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000914 if (!clippedSrcRect.intersect(*srcRectPtr)) {
915 clippedSrcIRect->setEmpty();
916 return;
917 }
918 }
919 clippedSrcRect.roundOut(clippedSrcIRect);
920 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
921 if (!clippedSrcIRect->intersect(bmpBounds)) {
922 clippedSrcIRect->setEmpty();
923 }
924}
925
926bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
927 const GrTextureParams& params,
928 const SkRect* srcRectPtr,
929 int maxTileSize,
930 int* tileSize,
931 SkIRect* clippedSrcRect) const {
932 // if bitmap is explictly texture backed then just use the texture
933 if (NULL != bitmap.getTexture()) {
934 return false;
935 }
936
937 // if it's larger than the max tile size, then we have no choice but tiling.
938 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
939 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
940 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
941 return true;
942 }
943
944 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
945 return false;
946 }
947
948 // if the entire texture is already in our cache then no reason to tile it
949 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
950 return false;
951 }
952
953 // At this point we know we could do the draw by uploading the entire bitmap
954 // as a texture. However, if the texture would be large compared to the
955 // cache size and we don't require most of it for this draw then tile to
956 // reduce the amount of upload and cache spill.
957
958 // assumption here is that sw bitmap size is a good proxy for its size as
959 // a texture
960 size_t bmpSize = bitmap.getSize();
961 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000962 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000963 if (bmpSize < cacheSize / 2) {
964 return false;
965 }
966
967 // Figure out how much of the src we will need based on the src rect and clipping.
968 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
969 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
970 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
971 kBmpSmallTileSize * kBmpSmallTileSize;
972
973 return usedTileBytes < 2 * bmpSize;
974}
975
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000976void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000977 const SkBitmap& bitmap,
978 const SkMatrix& m,
979 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000980 SkMatrix concat;
981 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
982 if (!m.isIdentity()) {
983 concat.setConcat(*draw->fMatrix, m);
984 draw.writable()->fMatrix = &concat;
985 }
986 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000987}
988
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000989// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000990// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
991// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000992static inline void clamped_outset_with_offset(SkIRect* iRect,
993 int outset,
994 SkPoint* offset,
995 const SkIRect& clamp) {
996 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000997
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000998 int leftClampDelta = clamp.fLeft - iRect->fLeft;
999 if (leftClampDelta > 0) {
1000 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001001 iRect->fLeft = clamp.fLeft;
1002 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001003 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001004 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001005
1006 int topClampDelta = clamp.fTop - iRect->fTop;
1007 if (topClampDelta > 0) {
1008 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001009 iRect->fTop = clamp.fTop;
1010 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001011 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001012 }
1013
1014 if (iRect->fRight > clamp.fRight) {
1015 iRect->fRight = clamp.fRight;
1016 }
1017 if (iRect->fBottom > clamp.fBottom) {
1018 iRect->fBottom = clamp.fBottom;
1019 }
1020}
1021
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001022static bool has_aligned_samples(const SkRect& srcRect,
1023 const SkRect& transformedRect) {
1024 // detect pixel disalignment
1025 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1026 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1027 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1028 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1029 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1030 COLOR_BLEED_TOLERANCE &&
1031 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1032 COLOR_BLEED_TOLERANCE) {
1033 return true;
1034 }
1035 return false;
1036}
1037
1038static bool may_color_bleed(const SkRect& srcRect,
1039 const SkRect& transformedRect,
1040 const SkMatrix& m) {
1041 // Only gets called if has_aligned_samples returned false.
1042 // So we can assume that sampling is axis aligned but not texel aligned.
1043 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1044 SkRect innerSrcRect(srcRect), innerTransformedRect,
1045 outerTransformedRect(transformedRect);
1046 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1047 m.mapRect(&innerTransformedRect, innerSrcRect);
1048
1049 // The gap between outerTransformedRect and innerTransformedRect
1050 // represents the projection of the source border area, which is
1051 // problematic for color bleeding. We must check whether any
1052 // destination pixels sample the border area.
1053 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1054 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1055 SkIRect outer, inner;
1056 outerTransformedRect.round(&outer);
1057 innerTransformedRect.round(&inner);
1058 // If the inner and outer rects round to the same result, it means the
1059 // border does not overlap any pixel centers. Yay!
1060 return inner != outer;
1061}
1062
1063static bool needs_texture_domain(const SkBitmap& bitmap,
1064 const SkRect& srcRect,
1065 GrTextureParams &params,
1066 const SkMatrix& contextMatrix,
1067 bool bicubic) {
1068 bool needsTextureDomain = false;
1069
1070 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1071 // Need texture domain if drawing a sub rect
1072 needsTextureDomain = srcRect.width() < bitmap.width() ||
1073 srcRect.height() < bitmap.height();
1074 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1075 // sampling is axis-aligned
1076 SkRect transformedRect;
1077 contextMatrix.mapRect(&transformedRect, srcRect);
1078
1079 if (has_aligned_samples(srcRect, transformedRect)) {
1080 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1081 needsTextureDomain = false;
1082 } else {
1083 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1084 }
1085 }
1086 }
1087 return needsTextureDomain;
1088}
1089
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001090void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1091 const SkBitmap& bitmap,
1092 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001093 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001094 const SkPaint& paint,
1095 SkCanvas::DrawBitmapRectFlags flags) {
1096 CHECK_SHOULD_DRAW(draw, false);
1097
1098 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001099 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001100 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1101 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001102 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001103 SkScalar w = SkIntToScalar(bitmap.width());
1104 SkScalar h = SkIntToScalar(bitmap.height());
1105 dstSize.fWidth = w;
1106 dstSize.fHeight = h;
1107 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001108 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001109 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001110 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001111 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001112 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001113 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1114 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1115 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1116 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001117 }
1118
1119 if (paint.getMaskFilter()){
1120 // Convert the bitmap to a shader so that the rect can be drawn
1121 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001122 SkBitmap tmp; // subset of bitmap, if necessary
1123 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001124 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001125 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001126 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1127 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1128 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001129 // In bleed mode we position and trim the bitmap based on the src rect which is
1130 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1131 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1132 // compensate.
1133 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1134 SkIRect iSrc;
1135 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001136
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001137 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1138 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001139
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001140 if (!bitmap.extractSubset(&tmp, iSrc)) {
1141 return; // extraction failed
1142 }
1143 bitmapPtr = &tmp;
1144 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001145
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001146 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001147 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001148 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001149 } else {
1150 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001151 }
1152
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001153 SkPaint paintWithShader(paint);
1154 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001155 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001156 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1157 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001158
1159 return;
1160 }
1161
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001162 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1163 // the view matrix rather than a local matrix.
1164 SkMatrix m;
1165 m.setScale(dstSize.fWidth / srcRect.width(),
1166 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167 fContext->concatMatrix(m);
1168
1169 GrTextureParams params;
1170 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1171 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001172
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001173 bool doBicubic = false;
1174
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001175 switch(paintFilterLevel) {
1176 case SkPaint::kNone_FilterLevel:
1177 textureFilterMode = GrTextureParams::kNone_FilterMode;
1178 break;
1179 case SkPaint::kLow_FilterLevel:
1180 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1181 break;
1182 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001183 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001184 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1185 } else {
1186 // Don't trigger MIP level generation unnecessarily.
1187 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1188 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001189 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001190 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001191 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001192 doBicubic =
1193 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001194 break;
1195 default:
1196 SkErrorInternals::SetError( kInvalidPaint_SkError,
1197 "Sorry, I don't understand the filtering "
1198 "mode you asked for. Falling back to "
1199 "MIPMaps.");
1200 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1201 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001202 }
1203
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001204 int tileFilterPad;
1205 if (doBicubic) {
1206 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1207 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1208 tileFilterPad = 0;
1209 } else {
1210 tileFilterPad = 1;
1211 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001212 params.setFilterMode(textureFilterMode);
1213
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001214 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001215 int tileSize;
1216
1217 SkIRect clippedSrcRect;
1218 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1219 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001220 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1221 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001222 } else {
1223 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001224 bool needsTextureDomain = needs_texture_domain(bitmap,
1225 srcRect,
1226 params,
1227 fContext->getMatrix(),
1228 doBicubic);
1229 this->internalDrawBitmap(bitmap,
1230 srcRect,
1231 params,
1232 paint,
1233 flags,
1234 doBicubic,
1235 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001236 }
1237}
1238
1239// Break 'bitmap' into several tiles to draw it since it has already
1240// been determined to be too large to fit in VRAM
1241void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1242 const SkRect& srcRect,
1243 const SkIRect& clippedSrcIRect,
1244 const GrTextureParams& params,
1245 const SkPaint& paint,
1246 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001247 int tileSize,
1248 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001249 // The following pixel lock is technically redundant, but it is desirable
1250 // to lock outside of the tile loop to prevent redecoding the whole image
1251 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1252 // is larger than the limit of the discardable memory pool.
1253 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001254 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1255
1256 int nx = bitmap.width() / tileSize;
1257 int ny = bitmap.height() / tileSize;
1258 for (int x = 0; x <= nx; x++) {
1259 for (int y = 0; y <= ny; y++) {
1260 SkRect tileR;
1261 tileR.set(SkIntToScalar(x * tileSize),
1262 SkIntToScalar(y * tileSize),
1263 SkIntToScalar((x + 1) * tileSize),
1264 SkIntToScalar((y + 1) * tileSize));
1265
1266 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1267 continue;
1268 }
1269
1270 if (!tileR.intersect(srcRect)) {
1271 continue;
1272 }
1273
1274 SkBitmap tmpB;
1275 SkIRect iTileR;
1276 tileR.roundOut(&iTileR);
1277 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1278 SkIntToScalar(iTileR.fTop));
1279
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001280 // Adjust the context matrix to draw at the right x,y in device space
1281 SkMatrix tmpM;
1282 GrContext::AutoMatrix am;
1283 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1284 am.setPreConcat(fContext, tmpM);
1285
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001286 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001287 SkIRect iClampRect;
1288
1289 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1290 // In bleed mode we want to always expand the tile on all edges
1291 // but stay within the bitmap bounds
1292 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1293 } else {
1294 // In texture-domain/clamp mode we only want to expand the
1295 // tile on edges interior to "srcRect" (i.e., we want to
1296 // not bleed across the original clamped edges)
1297 srcRect.roundOut(&iClampRect);
1298 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001299 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1300 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001301 }
1302
1303 if (bitmap.extractSubset(&tmpB, iTileR)) {
1304 // now offset it to make it "local" to our tmp bitmap
1305 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001306 GrTextureParams paramsTemp = params;
1307 bool needsTextureDomain = needs_texture_domain(bitmap,
1308 srcRect,
1309 paramsTemp,
1310 fContext->getMatrix(),
1311 bicubic);
1312 this->internalDrawBitmap(tmpB,
1313 tileR,
1314 paramsTemp,
1315 paint,
1316 flags,
1317 bicubic,
1318 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001319 }
1320 }
1321 }
1322}
1323
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001324
1325/*
1326 * This is called by drawBitmap(), which has to handle images that may be too
1327 * large to be represented by a single texture.
1328 *
1329 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1330 * and that non-texture portion of the GrPaint has already been setup.
1331 */
1332void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1333 const SkRect& srcRect,
1334 const GrTextureParams& params,
1335 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001336 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001337 bool bicubic,
1338 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001339 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1340 bitmap.height() <= fContext->getMaxTextureSize());
1341
1342 GrTexture* texture;
1343 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1344 if (NULL == texture) {
1345 return;
1346 }
1347
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001348 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001349 SkRect paintRect;
1350 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1351 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1352 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1353 SkScalarMul(srcRect.fTop, hInv),
1354 SkScalarMul(srcRect.fRight, wInv),
1355 SkScalarMul(srcRect.fBottom, hInv));
1356
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001357 SkRect textureDomain = SkRect::MakeEmpty();
1358 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001359 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001360 // Use a constrained texture domain to avoid color bleeding
1361 SkScalar left, top, right, bottom;
1362 if (srcRect.width() > SK_Scalar1) {
1363 SkScalar border = SK_ScalarHalf / texture->width();
1364 left = paintRect.left() + border;
1365 right = paintRect.right() - border;
1366 } else {
1367 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1368 }
1369 if (srcRect.height() > SK_Scalar1) {
1370 SkScalar border = SK_ScalarHalf / texture->height();
1371 top = paintRect.top() + border;
1372 bottom = paintRect.bottom() - border;
1373 } else {
1374 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1375 }
1376 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001377 if (bicubic) {
1378 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1379 } else {
1380 effect.reset(GrTextureDomainEffect::Create(texture,
1381 SkMatrix::I(),
1382 textureDomain,
1383 GrTextureDomain::kClamp_Mode,
1384 params.filterMode()));
1385 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001386 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001387 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1388 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1389 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001390 } else {
1391 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1392 }
1393
1394 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1395 // the rest from the SkPaint.
1396 GrPaint grPaint;
1397 grPaint.addColorEffect(effect);
1398 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001399 SkPaint2GrPaintNoShader(this->context(), paint, alphaOnly, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001400
1401 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1402}
1403
1404static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001405 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001406 int w, int h, const SkImageFilter::Context& ctx,
1407 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001408 SkASSERT(filter);
1409 SkDeviceImageFilterProxy proxy(device);
1410
1411 if (filter->canFilterImageGPU()) {
1412 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1413 // filter. Also set the clip wide open and the matrix to identity.
1414 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001415 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001416 } else {
1417 return false;
1418 }
1419}
1420
1421void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1422 int left, int top, const SkPaint& paint) {
1423 // drawSprite is defined to be in device coords.
1424 CHECK_SHOULD_DRAW(draw, true);
1425
1426 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1427 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1428 return;
1429 }
1430
1431 int w = bitmap.width();
1432 int h = bitmap.height();
1433
1434 GrTexture* texture;
1435 // draw sprite uses the default texture params
1436 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1437
1438 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001439 // This bitmap will own the filtered result as a texture.
1440 SkBitmap filteredBitmap;
1441
1442 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001443 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001444 SkMatrix matrix(*draw.fMatrix);
1445 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001446 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001447 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1448 SkAutoUnref aur(cache);
1449 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001450 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001451 &offset)) {
1452 texture = (GrTexture*) filteredBitmap.getTexture();
1453 w = filteredBitmap.width();
1454 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001455 left += offset.x();
1456 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001457 } else {
1458 return;
1459 }
1460 }
1461
1462 GrPaint grPaint;
1463 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1464
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001465 SkPaint2GrPaintNoShader(this->context(), paint, true, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001466
1467 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001468 SkRect::MakeXYWH(SkIntToScalar(left),
1469 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001470 SkIntToScalar(w),
1471 SkIntToScalar(h)),
1472 SkRect::MakeXYWH(0,
1473 0,
1474 SK_Scalar1 * w / texture->width(),
1475 SK_Scalar1 * h / texture->height()));
1476}
1477
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001478void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001479 const SkRect* src, const SkRect& dst,
1480 const SkPaint& paint,
1481 SkCanvas::DrawBitmapRectFlags flags) {
1482 SkMatrix matrix;
1483 SkRect bitmapBounds, tmpSrc;
1484
1485 bitmapBounds.set(0, 0,
1486 SkIntToScalar(bitmap.width()),
1487 SkIntToScalar(bitmap.height()));
1488
1489 // Compute matrix from the two rectangles
1490 if (NULL != src) {
1491 tmpSrc = *src;
1492 } else {
1493 tmpSrc = bitmapBounds;
1494 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001495
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001496 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1497
1498 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1499 if (NULL != src) {
1500 if (!bitmapBounds.contains(tmpSrc)) {
1501 if (!tmpSrc.intersect(bitmapBounds)) {
1502 return; // nothing to draw
1503 }
1504 }
1505 }
1506
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001507 SkRect tmpDst;
1508 matrix.mapRect(&tmpDst, tmpSrc);
1509
1510 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1511 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1512 // Translate so that tempDst's top left is at the origin.
1513 matrix = *origDraw.fMatrix;
1514 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1515 draw.writable()->fMatrix = &matrix;
1516 }
1517 SkSize dstSize;
1518 dstSize.fWidth = tmpDst.width();
1519 dstSize.fHeight = tmpDst.height();
1520
1521 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001522}
1523
1524void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1525 int x, int y, const SkPaint& paint) {
1526 // clear of the source device must occur before CHECK_SHOULD_DRAW
1527 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1528 if (dev->fNeedClear) {
1529 // TODO: could check here whether we really need to draw at all
1530 dev->clear(0x0);
1531 }
1532
1533 // drawDevice is defined to be in device coords.
1534 CHECK_SHOULD_DRAW(draw, true);
1535
1536 GrRenderTarget* devRT = dev->accessRenderTarget();
1537 GrTexture* devTex;
1538 if (NULL == (devTex = devRT->asTexture())) {
1539 return;
1540 }
1541
1542 const SkBitmap& bm = dev->accessBitmap(false);
1543 int w = bm.width();
1544 int h = bm.height();
1545
1546 SkImageFilter* filter = paint.getImageFilter();
1547 // This bitmap will own the filtered result as a texture.
1548 SkBitmap filteredBitmap;
1549
1550 if (NULL != filter) {
1551 SkIPoint offset = SkIPoint::Make(0, 0);
1552 SkMatrix matrix(*draw.fMatrix);
1553 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001554 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001555 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1556 SkAutoUnref aur(cache);
1557 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001558 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001559 &offset)) {
1560 devTex = filteredBitmap.getTexture();
1561 w = filteredBitmap.width();
1562 h = filteredBitmap.height();
1563 x += offset.fX;
1564 y += offset.fY;
1565 } else {
1566 return;
1567 }
1568 }
1569
1570 GrPaint grPaint;
1571 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1572
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001573 SkPaint2GrPaintNoShader(this->context(), paint, true, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001574
1575 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1576 SkIntToScalar(y),
1577 SkIntToScalar(w),
1578 SkIntToScalar(h));
1579
1580 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1581 // scratch texture).
1582 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1583 SK_Scalar1 * h / devTex->height());
1584
1585 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1586}
1587
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001588bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001589 return filter->canFilterImageGPU();
1590}
1591
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001592bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001593 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001594 SkBitmap* result, SkIPoint* offset) {
1595 // want explicitly our impl, so guard against a subclass of us overriding it
1596 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1597 return false;
1598 }
1599
1600 SkAutoLockPixels alp(src, !src.getTexture());
1601 if (!src.getTexture() && !src.readyToDraw()) {
1602 return false;
1603 }
1604
1605 GrTexture* texture;
1606 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1607 // must be pushed upstack.
1608 SkAutoCachedTexture act(this, src, NULL, &texture);
1609
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001610 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1611 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001612}
1613
1614///////////////////////////////////////////////////////////////////////////////
1615
1616// must be in SkCanvas::VertexMode order
1617static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1618 kTriangles_GrPrimitiveType,
1619 kTriangleStrip_GrPrimitiveType,
1620 kTriangleFan_GrPrimitiveType,
1621};
1622
1623void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1624 int vertexCount, const SkPoint vertices[],
1625 const SkPoint texs[], const SkColor colors[],
1626 SkXfermode* xmode,
1627 const uint16_t indices[], int indexCount,
1628 const SkPaint& paint) {
1629 CHECK_SHOULD_DRAW(draw, false);
1630
1631 GrPaint grPaint;
1632 // we ignore the shader if texs is null.
1633 if (NULL == texs) {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001634 SkPaint2GrPaintNoShader(this->context(), paint, false, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001635 } else {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001636 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001637 }
1638
1639 if (NULL != xmode && NULL != texs && NULL != colors) {
1640 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1641 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1642#if 0
1643 return
1644#endif
1645 }
1646 }
1647
1648 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1649 if (NULL != colors) {
1650 // need to convert byte order and from non-PM to PM
1651 convertedColors.reset(vertexCount);
1652 for (int i = 0; i < vertexCount; ++i) {
1653 convertedColors[i] = SkColor2GrColor(colors[i]);
1654 }
1655 colors = convertedColors.get();
1656 }
1657 fContext->drawVertices(grPaint,
1658 gVertexMode2PrimitiveType[vmode],
1659 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001660 vertices,
1661 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001662 colors,
1663 indices,
1664 indexCount);
1665}
1666
1667///////////////////////////////////////////////////////////////////////////////
1668
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001669void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1670 size_t byteLength, SkScalar x, SkScalar y,
1671 const SkPaint& paint) {
1672 CHECK_SHOULD_DRAW(draw, false);
1673
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001674 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001675 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001676 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001677
1678 SkDEBUGCODE(this->validate();)
1679
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001680 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1681 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001682 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001683 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001684
1685 SkDEBUGCODE(this->validate();)
1686
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001687 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001688 } else {
1689 // this guy will just call our drawPath()
1690 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001691 }
1692}
1693
1694void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1695 size_t byteLength, const SkScalar pos[],
1696 SkScalar constY, int scalarsPerPos,
1697 const SkPaint& paint) {
1698 CHECK_SHOULD_DRAW(draw, false);
1699
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001700 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001701 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001702 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001703
1704 SkDEBUGCODE(this->validate();)
1705
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001706 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001707 constY, scalarsPerPos);
1708 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001709 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001710 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001711
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001712 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001713
1714 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001715 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001716 } else {
1717 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1718 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001719 }
1720}
1721
1722void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1723 size_t len, const SkPath& path,
1724 const SkMatrix* m, const SkPaint& paint) {
1725 CHECK_SHOULD_DRAW(draw, false);
1726
1727 SkASSERT(draw.fDevice == this);
1728 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1729}
1730
1731///////////////////////////////////////////////////////////////////////////////
1732
1733bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1734 if (!paint.isLCDRenderText()) {
1735 // we're cool with the paint as is
1736 return false;
1737 }
1738
1739 if (paint.getShader() ||
1740 paint.getXfermode() || // unless its srcover
1741 paint.getMaskFilter() ||
1742 paint.getRasterizer() ||
1743 paint.getColorFilter() ||
1744 paint.getPathEffect() ||
1745 paint.isFakeBoldText() ||
1746 paint.getStyle() != SkPaint::kFill_Style) {
1747 // turn off lcd
1748 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1749 flags->fHinting = paint.getHinting();
1750 return true;
1751 }
1752 // we're cool with the paint as is
1753 return false;
1754}
1755
1756void SkGpuDevice::flush() {
1757 DO_DEFERRED_CLEAR();
1758 fContext->resolveRenderTarget(fRenderTarget);
1759}
1760
1761///////////////////////////////////////////////////////////////////////////////
1762
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001763SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001764 GrTextureDesc desc;
1765 desc.fConfig = fRenderTarget->config();
1766 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001767 desc.fWidth = info.width();
1768 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001769 desc.fSampleCnt = fRenderTarget->numSamples();
1770
1771 SkAutoTUnref<GrTexture> texture;
1772 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001773 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001774
1775#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1776 // layers are never draw in repeat modes, so we can request an approx
1777 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001778 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001779 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1780 GrContext::kApprox_ScratchTexMatch :
1781 GrContext::kExact_ScratchTexMatch;
1782 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1783#else
1784 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1785#endif
1786 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001787 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001788 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001789 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1790 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001791 return NULL;
1792 }
1793}
1794
reed@google.com76f10a32014-02-05 15:32:21 +00001795SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1796 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1797}
1798
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001799void SkGpuDevice::EXPERIMENTAL_optimize(SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001800 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001801
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001802 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1803 if (NULL != existing) {
1804 return;
1805 }
1806
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001807 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001808
1809 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001810
1811 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001812}
1813
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001814static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1815 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1816 result->setConfig(info);
1817 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1818}
1819
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001820void SkGpuDevice::EXPERIMENTAL_purge(SkPicture* picture) {
1821
1822}
1823
1824bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001825
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001826 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001827
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001828 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001829 if (NULL == data) {
1830 return false;
1831 }
1832
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001833 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001834
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001835 if (0 == gpuData->numSaveLayers()) {
1836 return false;
1837 }
1838
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001839 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1840 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1841 pullForward[i] = false;
1842 }
1843
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001844 SkRect clipBounds;
1845 if (!canvas->getClipBounds(&clipBounds)) {
1846 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001847 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001848 SkIRect query;
1849 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001850
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001851 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001852
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001853 // This code pre-renders the entire layer since it will be cached and potentially
1854 // reused with different clips (e.g., in different tiles). Because of this the
1855 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1856 // is used to limit which clips are pre-rendered.
1857 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001858
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001859 if (ops.valid()) {
1860 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001861 // the BBH to limit the pre-rendering to just the layers needed to cover
1862 // the region being drawn
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001863 for (int i = 0; i < ops.numOps(); ++i) {
1864 uint32_t offset = ops.offset(i);
1865
1866 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001867 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001868 // their parent in the list.
1869 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1870 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1871
1872 if (pullForward[j]) {
1873 continue; // already pulling forward
1874 }
1875
1876 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1877 continue; // the op isn't in this range
1878 }
1879
1880 // TODO: once this code is more stable unsuitable layers can
1881 // just be omitted during the optimization stage
1882 if (!info.fValid ||
1883 kSaveLayerMaxSize < info.fSize.fWidth ||
1884 kSaveLayerMaxSize < info.fSize.fHeight ||
1885 info.fIsNested) {
1886 continue; // this layer is unsuitable
1887 }
1888
1889 pullForward[j] = true;
1890 }
1891 }
1892 } else {
1893 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001894 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001895 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1896 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1897
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001898 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1899 info.fOffset.fY,
1900 info.fSize.fWidth,
1901 info.fSize.fHeight);
1902
1903 if (!SkIRect::Intersects(query, layerRect)) {
1904 continue;
1905 }
1906
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001907 // TODO: once this code is more stable unsuitable layers can
1908 // just be omitted during the optimization stage
1909 if (!info.fValid ||
1910 kSaveLayerMaxSize < info.fSize.fWidth ||
1911 kSaveLayerMaxSize < info.fSize.fHeight ||
1912 info.fIsNested) {
1913 continue;
1914 }
1915
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001916 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001917 }
1918 }
1919
1920 SkPicturePlayback::PlaybackReplacements replacements;
1921
1922 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1923 if (pullForward[i]) {
1924 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1925
1926 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1927
1928 if (NULL != picture->fPlayback) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001929 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001930 replacements.push();
1931 layerInfo->fStart = info.fSaveLayerOpID;
1932 layerInfo->fStop = info.fRestoreOpID;
1933 layerInfo->fPos = info.fOffset;
1934
1935 GrTextureDesc desc;
1936 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1937 desc.fWidth = info.fSize.fWidth;
1938 desc.fHeight = info.fSize.fHeight;
1939 desc.fConfig = kSkia8888_GrPixelConfig;
1940 // TODO: need to deal with sample count
1941
1942 bool bNeedsRendering = true;
1943
1944 // This just uses scratch textures and doesn't cache the texture.
1945 // This can yield a lot of re-rendering
1946 if (NULL == layer->getTexture()) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001947 layer->setTexture(fContext->lockAndRefScratchTexture(desc,
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001948 GrContext::kApprox_ScratchTexMatch));
1949 if (NULL == layer->getTexture()) {
1950 continue;
1951 }
1952 } else {
1953 bNeedsRendering = false;
1954 }
1955
1956 layerInfo->fBM = SkNEW(SkBitmap);
1957 wrap_texture(layer->getTexture(), desc.fWidth, desc.fHeight, layerInfo->fBM);
1958
1959 SkASSERT(info.fPaint);
1960 layerInfo->fPaint = info.fPaint;
1961
1962 if (bNeedsRendering) {
1963 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
1964 layer->getTexture()->asRenderTarget()));
1965
1966 SkCanvas* canvas = surface->getCanvas();
1967
1968 canvas->setMatrix(info.fCTM);
1969 canvas->clear(SK_ColorTRANSPARENT);
1970
1971 picture->fPlayback->setDrawLimits(info.fSaveLayerOpID, info.fRestoreOpID);
1972 picture->fPlayback->draw(*canvas, NULL);
1973 picture->fPlayback->setDrawLimits(0, 0);
1974 canvas->flush();
1975 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001976 }
1977 }
1978 }
1979
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001980 // Playback using new layers
1981 picture->fPlayback->setReplacements(&replacements);
1982 picture->fPlayback->draw(*canvas, NULL);
1983 picture->fPlayback->setReplacements(NULL);
1984
1985 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1986 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1987
1988 if (NULL != layer->getTexture()) {
1989 fContext->unlockScratchTexture(layer->getTexture());
1990 layer->setTexture(NULL);
1991 }
1992 }
1993
1994 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001995}