blob: cddf50a98cbfd501b8f5d79d46933081eff94857 [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
1173 int tileFilterPad;
1174 bool doBicubic = false;
1175
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001176 switch(paintFilterLevel) {
1177 case SkPaint::kNone_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001178 tileFilterPad = 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001179 textureFilterMode = GrTextureParams::kNone_FilterMode;
1180 break;
1181 case SkPaint::kLow_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001182 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001183 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1184 break;
1185 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001186 tileFilterPad = 1;
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001187 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001188 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1189 } else {
1190 // Don't trigger MIP level generation unnecessarily.
1191 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1192 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001193 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001194 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001195 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001196 if (fContext->getMatrix().getMinScale() >= SK_Scalar1) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001197 // We will install an effect that does the filtering in the shader.
1198 textureFilterMode = GrTextureParams::kNone_FilterMode;
1199 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1200 doBicubic = true;
1201 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001202 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1203 tileFilterPad = 1;
1204 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001205 break;
1206 default:
1207 SkErrorInternals::SetError( kInvalidPaint_SkError,
1208 "Sorry, I don't understand the filtering "
1209 "mode you asked for. Falling back to "
1210 "MIPMaps.");
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001211 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001212 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1213 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001214 }
1215
1216 params.setFilterMode(textureFilterMode);
1217
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001218 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001219 int tileSize;
1220
1221 SkIRect clippedSrcRect;
1222 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1223 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001224 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1225 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001226 } else {
1227 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001228 bool needsTextureDomain = needs_texture_domain(bitmap,
1229 srcRect,
1230 params,
1231 fContext->getMatrix(),
1232 doBicubic);
1233 this->internalDrawBitmap(bitmap,
1234 srcRect,
1235 params,
1236 paint,
1237 flags,
1238 doBicubic,
1239 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001240 }
1241}
1242
1243// Break 'bitmap' into several tiles to draw it since it has already
1244// been determined to be too large to fit in VRAM
1245void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1246 const SkRect& srcRect,
1247 const SkIRect& clippedSrcIRect,
1248 const GrTextureParams& params,
1249 const SkPaint& paint,
1250 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001251 int tileSize,
1252 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001253 // The following pixel lock is technically redundant, but it is desirable
1254 // to lock outside of the tile loop to prevent redecoding the whole image
1255 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1256 // is larger than the limit of the discardable memory pool.
1257 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001258 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1259
1260 int nx = bitmap.width() / tileSize;
1261 int ny = bitmap.height() / tileSize;
1262 for (int x = 0; x <= nx; x++) {
1263 for (int y = 0; y <= ny; y++) {
1264 SkRect tileR;
1265 tileR.set(SkIntToScalar(x * tileSize),
1266 SkIntToScalar(y * tileSize),
1267 SkIntToScalar((x + 1) * tileSize),
1268 SkIntToScalar((y + 1) * tileSize));
1269
1270 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1271 continue;
1272 }
1273
1274 if (!tileR.intersect(srcRect)) {
1275 continue;
1276 }
1277
1278 SkBitmap tmpB;
1279 SkIRect iTileR;
1280 tileR.roundOut(&iTileR);
1281 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1282 SkIntToScalar(iTileR.fTop));
1283
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001284 // Adjust the context matrix to draw at the right x,y in device space
1285 SkMatrix tmpM;
1286 GrContext::AutoMatrix am;
1287 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1288 am.setPreConcat(fContext, tmpM);
1289
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001290 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001291 SkIRect iClampRect;
1292
1293 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1294 // In bleed mode we want to always expand the tile on all edges
1295 // but stay within the bitmap bounds
1296 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1297 } else {
1298 // In texture-domain/clamp mode we only want to expand the
1299 // tile on edges interior to "srcRect" (i.e., we want to
1300 // not bleed across the original clamped edges)
1301 srcRect.roundOut(&iClampRect);
1302 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001303 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1304 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001305 }
1306
1307 if (bitmap.extractSubset(&tmpB, iTileR)) {
1308 // now offset it to make it "local" to our tmp bitmap
1309 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001310 GrTextureParams paramsTemp = params;
1311 bool needsTextureDomain = needs_texture_domain(bitmap,
1312 srcRect,
1313 paramsTemp,
1314 fContext->getMatrix(),
1315 bicubic);
1316 this->internalDrawBitmap(tmpB,
1317 tileR,
1318 paramsTemp,
1319 paint,
1320 flags,
1321 bicubic,
1322 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001323 }
1324 }
1325 }
1326}
1327
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001328
1329/*
1330 * This is called by drawBitmap(), which has to handle images that may be too
1331 * large to be represented by a single texture.
1332 *
1333 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1334 * and that non-texture portion of the GrPaint has already been setup.
1335 */
1336void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1337 const SkRect& srcRect,
1338 const GrTextureParams& params,
1339 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001340 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001341 bool bicubic,
1342 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001343 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1344 bitmap.height() <= fContext->getMaxTextureSize());
1345
1346 GrTexture* texture;
1347 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1348 if (NULL == texture) {
1349 return;
1350 }
1351
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001352 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001353 SkRect paintRect;
1354 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1355 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1356 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1357 SkScalarMul(srcRect.fTop, hInv),
1358 SkScalarMul(srcRect.fRight, wInv),
1359 SkScalarMul(srcRect.fBottom, hInv));
1360
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001361 SkRect textureDomain = SkRect::MakeEmpty();
1362 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001363 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001364 // Use a constrained texture domain to avoid color bleeding
1365 SkScalar left, top, right, bottom;
1366 if (srcRect.width() > SK_Scalar1) {
1367 SkScalar border = SK_ScalarHalf / texture->width();
1368 left = paintRect.left() + border;
1369 right = paintRect.right() - border;
1370 } else {
1371 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1372 }
1373 if (srcRect.height() > SK_Scalar1) {
1374 SkScalar border = SK_ScalarHalf / texture->height();
1375 top = paintRect.top() + border;
1376 bottom = paintRect.bottom() - border;
1377 } else {
1378 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1379 }
1380 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001381 if (bicubic) {
1382 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1383 } else {
1384 effect.reset(GrTextureDomainEffect::Create(texture,
1385 SkMatrix::I(),
1386 textureDomain,
1387 GrTextureDomain::kClamp_Mode,
1388 params.filterMode()));
1389 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001390 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001391 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1392 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1393 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001394 } else {
1395 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1396 }
1397
1398 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1399 // the rest from the SkPaint.
1400 GrPaint grPaint;
1401 grPaint.addColorEffect(effect);
1402 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001403 SkPaint2GrPaintNoShader(this->context(), paint, alphaOnly, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001404
1405 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1406}
1407
1408static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001409 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001410 int w, int h, const SkImageFilter::Context& ctx,
1411 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001412 SkASSERT(filter);
1413 SkDeviceImageFilterProxy proxy(device);
1414
1415 if (filter->canFilterImageGPU()) {
1416 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1417 // filter. Also set the clip wide open and the matrix to identity.
1418 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001419 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001420 } else {
1421 return false;
1422 }
1423}
1424
1425void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1426 int left, int top, const SkPaint& paint) {
1427 // drawSprite is defined to be in device coords.
1428 CHECK_SHOULD_DRAW(draw, true);
1429
1430 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1431 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1432 return;
1433 }
1434
1435 int w = bitmap.width();
1436 int h = bitmap.height();
1437
1438 GrTexture* texture;
1439 // draw sprite uses the default texture params
1440 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1441
1442 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001443 // This bitmap will own the filtered result as a texture.
1444 SkBitmap filteredBitmap;
1445
1446 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001447 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001448 SkMatrix matrix(*draw.fMatrix);
1449 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001450 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001451 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1452 SkAutoUnref aur(cache);
1453 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001454 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001455 &offset)) {
1456 texture = (GrTexture*) filteredBitmap.getTexture();
1457 w = filteredBitmap.width();
1458 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001459 left += offset.x();
1460 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001461 } else {
1462 return;
1463 }
1464 }
1465
1466 GrPaint grPaint;
1467 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1468
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001469 SkPaint2GrPaintNoShader(this->context(), paint, true, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001470
1471 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001472 SkRect::MakeXYWH(SkIntToScalar(left),
1473 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001474 SkIntToScalar(w),
1475 SkIntToScalar(h)),
1476 SkRect::MakeXYWH(0,
1477 0,
1478 SK_Scalar1 * w / texture->width(),
1479 SK_Scalar1 * h / texture->height()));
1480}
1481
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001482void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001483 const SkRect* src, const SkRect& dst,
1484 const SkPaint& paint,
1485 SkCanvas::DrawBitmapRectFlags flags) {
1486 SkMatrix matrix;
1487 SkRect bitmapBounds, tmpSrc;
1488
1489 bitmapBounds.set(0, 0,
1490 SkIntToScalar(bitmap.width()),
1491 SkIntToScalar(bitmap.height()));
1492
1493 // Compute matrix from the two rectangles
1494 if (NULL != src) {
1495 tmpSrc = *src;
1496 } else {
1497 tmpSrc = bitmapBounds;
1498 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001499
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001500 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1501
1502 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1503 if (NULL != src) {
1504 if (!bitmapBounds.contains(tmpSrc)) {
1505 if (!tmpSrc.intersect(bitmapBounds)) {
1506 return; // nothing to draw
1507 }
1508 }
1509 }
1510
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001511 SkRect tmpDst;
1512 matrix.mapRect(&tmpDst, tmpSrc);
1513
1514 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1515 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1516 // Translate so that tempDst's top left is at the origin.
1517 matrix = *origDraw.fMatrix;
1518 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1519 draw.writable()->fMatrix = &matrix;
1520 }
1521 SkSize dstSize;
1522 dstSize.fWidth = tmpDst.width();
1523 dstSize.fHeight = tmpDst.height();
1524
1525 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001526}
1527
1528void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1529 int x, int y, const SkPaint& paint) {
1530 // clear of the source device must occur before CHECK_SHOULD_DRAW
1531 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1532 if (dev->fNeedClear) {
1533 // TODO: could check here whether we really need to draw at all
1534 dev->clear(0x0);
1535 }
1536
1537 // drawDevice is defined to be in device coords.
1538 CHECK_SHOULD_DRAW(draw, true);
1539
1540 GrRenderTarget* devRT = dev->accessRenderTarget();
1541 GrTexture* devTex;
1542 if (NULL == (devTex = devRT->asTexture())) {
1543 return;
1544 }
1545
1546 const SkBitmap& bm = dev->accessBitmap(false);
1547 int w = bm.width();
1548 int h = bm.height();
1549
1550 SkImageFilter* filter = paint.getImageFilter();
1551 // This bitmap will own the filtered result as a texture.
1552 SkBitmap filteredBitmap;
1553
1554 if (NULL != filter) {
1555 SkIPoint offset = SkIPoint::Make(0, 0);
1556 SkMatrix matrix(*draw.fMatrix);
1557 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001558 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001559 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1560 SkAutoUnref aur(cache);
1561 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001562 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001563 &offset)) {
1564 devTex = filteredBitmap.getTexture();
1565 w = filteredBitmap.width();
1566 h = filteredBitmap.height();
1567 x += offset.fX;
1568 y += offset.fY;
1569 } else {
1570 return;
1571 }
1572 }
1573
1574 GrPaint grPaint;
1575 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1576
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001577 SkPaint2GrPaintNoShader(this->context(), paint, true, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001578
1579 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1580 SkIntToScalar(y),
1581 SkIntToScalar(w),
1582 SkIntToScalar(h));
1583
1584 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1585 // scratch texture).
1586 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1587 SK_Scalar1 * h / devTex->height());
1588
1589 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1590}
1591
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001592bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001593 return filter->canFilterImageGPU();
1594}
1595
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001596bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001597 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001598 SkBitmap* result, SkIPoint* offset) {
1599 // want explicitly our impl, so guard against a subclass of us overriding it
1600 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1601 return false;
1602 }
1603
1604 SkAutoLockPixels alp(src, !src.getTexture());
1605 if (!src.getTexture() && !src.readyToDraw()) {
1606 return false;
1607 }
1608
1609 GrTexture* texture;
1610 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1611 // must be pushed upstack.
1612 SkAutoCachedTexture act(this, src, NULL, &texture);
1613
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001614 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1615 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001616}
1617
1618///////////////////////////////////////////////////////////////////////////////
1619
1620// must be in SkCanvas::VertexMode order
1621static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1622 kTriangles_GrPrimitiveType,
1623 kTriangleStrip_GrPrimitiveType,
1624 kTriangleFan_GrPrimitiveType,
1625};
1626
1627void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1628 int vertexCount, const SkPoint vertices[],
1629 const SkPoint texs[], const SkColor colors[],
1630 SkXfermode* xmode,
1631 const uint16_t indices[], int indexCount,
1632 const SkPaint& paint) {
1633 CHECK_SHOULD_DRAW(draw, false);
1634
1635 GrPaint grPaint;
1636 // we ignore the shader if texs is null.
1637 if (NULL == texs) {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001638 SkPaint2GrPaintNoShader(this->context(), paint, false, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001639 } else {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001640 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001641 }
1642
1643 if (NULL != xmode && NULL != texs && NULL != colors) {
1644 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1645 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1646#if 0
1647 return
1648#endif
1649 }
1650 }
1651
1652 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1653 if (NULL != colors) {
1654 // need to convert byte order and from non-PM to PM
1655 convertedColors.reset(vertexCount);
1656 for (int i = 0; i < vertexCount; ++i) {
1657 convertedColors[i] = SkColor2GrColor(colors[i]);
1658 }
1659 colors = convertedColors.get();
1660 }
1661 fContext->drawVertices(grPaint,
1662 gVertexMode2PrimitiveType[vmode],
1663 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001664 vertices,
1665 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001666 colors,
1667 indices,
1668 indexCount);
1669}
1670
1671///////////////////////////////////////////////////////////////////////////////
1672
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001673void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1674 size_t byteLength, SkScalar x, SkScalar y,
1675 const SkPaint& paint) {
1676 CHECK_SHOULD_DRAW(draw, false);
1677
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001678 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001679 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001680 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001681
1682 SkDEBUGCODE(this->validate();)
1683
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001684 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1685 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001686 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001687 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001688
1689 SkDEBUGCODE(this->validate();)
1690
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001691 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001692 } else {
1693 // this guy will just call our drawPath()
1694 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001695 }
1696}
1697
1698void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1699 size_t byteLength, const SkScalar pos[],
1700 SkScalar constY, int scalarsPerPos,
1701 const SkPaint& paint) {
1702 CHECK_SHOULD_DRAW(draw, false);
1703
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001704 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001705 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001706 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001707
1708 SkDEBUGCODE(this->validate();)
1709
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001710 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001711 constY, scalarsPerPos);
1712 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001713 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001714 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001715
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001716 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001717
1718 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001719 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001720 } else {
1721 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1722 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001723 }
1724}
1725
1726void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1727 size_t len, const SkPath& path,
1728 const SkMatrix* m, const SkPaint& paint) {
1729 CHECK_SHOULD_DRAW(draw, false);
1730
1731 SkASSERT(draw.fDevice == this);
1732 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1733}
1734
1735///////////////////////////////////////////////////////////////////////////////
1736
1737bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1738 if (!paint.isLCDRenderText()) {
1739 // we're cool with the paint as is
1740 return false;
1741 }
1742
1743 if (paint.getShader() ||
1744 paint.getXfermode() || // unless its srcover
1745 paint.getMaskFilter() ||
1746 paint.getRasterizer() ||
1747 paint.getColorFilter() ||
1748 paint.getPathEffect() ||
1749 paint.isFakeBoldText() ||
1750 paint.getStyle() != SkPaint::kFill_Style) {
1751 // turn off lcd
1752 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1753 flags->fHinting = paint.getHinting();
1754 return true;
1755 }
1756 // we're cool with the paint as is
1757 return false;
1758}
1759
1760void SkGpuDevice::flush() {
1761 DO_DEFERRED_CLEAR();
1762 fContext->resolveRenderTarget(fRenderTarget);
1763}
1764
1765///////////////////////////////////////////////////////////////////////////////
1766
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001767SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001768 GrTextureDesc desc;
1769 desc.fConfig = fRenderTarget->config();
1770 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001771 desc.fWidth = info.width();
1772 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001773 desc.fSampleCnt = fRenderTarget->numSamples();
1774
1775 SkAutoTUnref<GrTexture> texture;
1776 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001777 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001778
1779#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1780 // layers are never draw in repeat modes, so we can request an approx
1781 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001782 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001783 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1784 GrContext::kApprox_ScratchTexMatch :
1785 GrContext::kExact_ScratchTexMatch;
1786 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1787#else
1788 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1789#endif
1790 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001791 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001792 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001793 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1794 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001795 return NULL;
1796 }
1797}
1798
reed@google.com76f10a32014-02-05 15:32:21 +00001799SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1800 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1801}
1802
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001803void SkGpuDevice::EXPERIMENTAL_optimize(SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001804 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001805
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001806 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1807 if (NULL != existing) {
1808 return;
1809 }
1810
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001811 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001812
1813 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001814
1815 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001816}
1817
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001818static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1819 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1820 result->setConfig(info);
1821 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1822}
1823
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001824void SkGpuDevice::EXPERIMENTAL_purge(SkPicture* picture) {
1825
1826}
1827
1828bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001829
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001830 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001831
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001832 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001833 if (NULL == data) {
1834 return false;
1835 }
1836
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001837 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001838
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001839 if (0 == gpuData->numSaveLayers()) {
1840 return false;
1841 }
1842
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001843 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1844 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1845 pullForward[i] = false;
1846 }
1847
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001848 SkRect clipBounds;
1849 if (!canvas->getClipBounds(&clipBounds)) {
1850 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001851 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001852 SkIRect query;
1853 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001854
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001855 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001856
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001857 // This code pre-renders the entire layer since it will be cached and potentially
1858 // reused with different clips (e.g., in different tiles). Because of this the
1859 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1860 // is used to limit which clips are pre-rendered.
1861 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001862
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001863 if (ops.valid()) {
1864 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001865 // the BBH to limit the pre-rendering to just the layers needed to cover
1866 // the region being drawn
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001867 for (int i = 0; i < ops.numOps(); ++i) {
1868 uint32_t offset = ops.offset(i);
1869
1870 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001871 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001872 // their parent in the list.
1873 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1874 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1875
1876 if (pullForward[j]) {
1877 continue; // already pulling forward
1878 }
1879
1880 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1881 continue; // the op isn't in this range
1882 }
1883
1884 // TODO: once this code is more stable unsuitable layers can
1885 // just be omitted during the optimization stage
1886 if (!info.fValid ||
1887 kSaveLayerMaxSize < info.fSize.fWidth ||
1888 kSaveLayerMaxSize < info.fSize.fHeight ||
1889 info.fIsNested) {
1890 continue; // this layer is unsuitable
1891 }
1892
1893 pullForward[j] = true;
1894 }
1895 }
1896 } else {
1897 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001898 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001899 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1900 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1901
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001902 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1903 info.fOffset.fY,
1904 info.fSize.fWidth,
1905 info.fSize.fHeight);
1906
1907 if (!SkIRect::Intersects(query, layerRect)) {
1908 continue;
1909 }
1910
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001911 // TODO: once this code is more stable unsuitable layers can
1912 // just be omitted during the optimization stage
1913 if (!info.fValid ||
1914 kSaveLayerMaxSize < info.fSize.fWidth ||
1915 kSaveLayerMaxSize < info.fSize.fHeight ||
1916 info.fIsNested) {
1917 continue;
1918 }
1919
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001920 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001921 }
1922 }
1923
1924 SkPicturePlayback::PlaybackReplacements replacements;
1925
1926 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1927 if (pullForward[i]) {
1928 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1929
1930 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1931
1932 if (NULL != picture->fPlayback) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001933 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001934 replacements.push();
1935 layerInfo->fStart = info.fSaveLayerOpID;
1936 layerInfo->fStop = info.fRestoreOpID;
1937 layerInfo->fPos = info.fOffset;
1938
1939 GrTextureDesc desc;
1940 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1941 desc.fWidth = info.fSize.fWidth;
1942 desc.fHeight = info.fSize.fHeight;
1943 desc.fConfig = kSkia8888_GrPixelConfig;
1944 // TODO: need to deal with sample count
1945
1946 bool bNeedsRendering = true;
1947
1948 // This just uses scratch textures and doesn't cache the texture.
1949 // This can yield a lot of re-rendering
1950 if (NULL == layer->getTexture()) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001951 layer->setTexture(fContext->lockAndRefScratchTexture(desc,
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001952 GrContext::kApprox_ScratchTexMatch));
1953 if (NULL == layer->getTexture()) {
1954 continue;
1955 }
1956 } else {
1957 bNeedsRendering = false;
1958 }
1959
1960 layerInfo->fBM = SkNEW(SkBitmap);
1961 wrap_texture(layer->getTexture(), desc.fWidth, desc.fHeight, layerInfo->fBM);
1962
1963 SkASSERT(info.fPaint);
1964 layerInfo->fPaint = info.fPaint;
1965
1966 if (bNeedsRendering) {
1967 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
1968 layer->getTexture()->asRenderTarget()));
1969
1970 SkCanvas* canvas = surface->getCanvas();
1971
1972 canvas->setMatrix(info.fCTM);
1973 canvas->clear(SK_ColorTRANSPARENT);
1974
1975 picture->fPlayback->setDrawLimits(info.fSaveLayerOpID, info.fRestoreOpID);
1976 picture->fPlayback->draw(*canvas, NULL);
1977 picture->fPlayback->setDrawLimits(0, 0);
1978 canvas->flush();
1979 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001980 }
1981 }
1982 }
1983
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001984 // Playback using new layers
1985 picture->fPlayback->setReplacements(&replacements);
1986 picture->fPlayback->draw(*canvas, NULL);
1987 picture->fPlayback->setReplacements(NULL);
1988
1989 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1990 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1991
1992 if (NULL != layer->getTexture()) {
1993 fContext->unlockScratchTexture(layer->getTexture());
1994 layer->setTexture(NULL);
1995 }
1996 }
1997
1998 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001999}