blob: 7c66fa96fa742d894f8b5b6cd0585ec4a031909c [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
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000023#include "SkDeviceImageFilterProxy.h"
24#include "SkDrawProcs.h"
25#include "SkGlyphCache.h"
26#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000027#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000029#include "SkPicture.h"
robertphillips@google.combeb1af22014-05-07 21:31:09 +000030#include "SkPicturePlayback.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000031#include "SkRRect.h"
32#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000033#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000034#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000036#include "SkVertState.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#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
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000128/*
129 * GrRenderTarget does not know its opaqueness, only its config, so we have
130 * to make conservative guesses when we return an "equivalent" bitmap.
131 */
132static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000133 SkBitmap bitmap;
reed6c225732014-06-09 19:52:07 -0700134 bitmap.setInfo(renderTarget->info());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000135 return bitmap;
136}
137
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000138SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000139 SkASSERT(NULL != surface);
140 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
141 return NULL;
142 }
143 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000144 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000145 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000146 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000147 }
148}
149
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000150SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000151 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000152 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000153}
154
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000155SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000156 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000157 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000158}
159
160void SkGpuDevice::initFromRenderTarget(GrContext* context,
161 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000162 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000163 fDrawProcs = NULL;
164
165 fContext = context;
166 fContext->ref();
167
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +0000168 bool useDFFonts = !!(flags & kDFFonts_Flag);
169 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties,
170 useDFFonts));
commit-bot@chromium.org47841822014-03-27 14:19:17 +0000171 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
172
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000173 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000174 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000175
176 SkASSERT(NULL != renderTarget);
177 fRenderTarget = renderTarget;
178 fRenderTarget->ref();
179
180 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
181 // on the RT but not vice-versa.
182 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
183 // busting chrome (for a currently unknown reason).
184 GrSurface* surface = fRenderTarget->asTexture();
185 if (NULL == surface) {
186 surface = fRenderTarget;
187 }
reed@google.combf790232013-12-13 19:45:58 +0000188
reed6c225732014-06-09 19:52:07 -0700189 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef,
190 (surface->info(), surface, SkToBool(flags & kCached_Flag)));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000191
reed@google.com672588b2014-01-08 15:42:01 +0000192 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000193}
194
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000195SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
196 int sampleCount) {
197 if (kUnknown_SkColorType == origInfo.colorType() ||
198 origInfo.width() < 0 || origInfo.height() < 0) {
199 return NULL;
200 }
201
202 SkImageInfo info = origInfo;
203 // TODO: perhas we can loosen this check now that colortype is more detailed
204 // e.g. can we support both RGBA and BGRA here?
205 if (kRGB_565_SkColorType == info.colorType()) {
206 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
207 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000208 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000209 if (kOpaque_SkAlphaType != info.alphaType()) {
210 info.fAlphaType = kPremul_SkAlphaType; // force this setting
211 }
212 }
213
214 GrTextureDesc desc;
215 desc.fFlags = kRenderTarget_GrTextureFlagBit;
216 desc.fWidth = info.width();
217 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000218 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000219 desc.fSampleCnt = sampleCount;
220
221 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
222 if (!texture.get()) {
223 return NULL;
224 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000225
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000226 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
227}
228
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000229SkGpuDevice::~SkGpuDevice() {
230 if (fDrawProcs) {
231 delete fDrawProcs;
232 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000233
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000234 delete fMainTextContext;
235 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000236
237 // The GrContext takes a ref on the target. We don't want to cause the render
238 // target to be unnecessarily kept alive.
239 if (fContext->getRenderTarget() == fRenderTarget) {
240 fContext->setRenderTarget(NULL);
241 }
242
243 if (fContext->getClip() == &fClipData) {
244 fContext->setClip(NULL);
245 }
246
247 SkSafeUnref(fRenderTarget);
248 fContext->unref();
249}
250
251///////////////////////////////////////////////////////////////////////////////
252
253void SkGpuDevice::makeRenderTargetCurrent() {
254 DO_DEFERRED_CLEAR();
255 fContext->setRenderTarget(fRenderTarget);
256}
257
258///////////////////////////////////////////////////////////////////////////////
259
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000260bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
261 int x, int y) {
262 DO_DEFERRED_CLEAR();
263
264 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000265 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000266 if (kUnknown_GrPixelConfig == config) {
267 return false;
268 }
269
270 uint32_t flags = 0;
271 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
272 flags = GrContext::kUnpremul_PixelOpsFlag;
273 }
274 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
275 config, dstPixels, dstRowBytes, flags);
276}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000277
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000278bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
279 int x, int y) {
280 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000281 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000282 if (kUnknown_GrPixelConfig == config) {
283 return false;
284 }
285 uint32_t flags = 0;
286 if (kUnpremul_SkAlphaType == info.alphaType()) {
287 flags = GrContext::kUnpremul_PixelOpsFlag;
288 }
289 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
290
291 // need to bump our genID for compatibility with clients that "know" we have a bitmap
292 this->onAccessBitmap().notifyPixelsChanged();
293
294 return true;
295}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000296
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000297const SkBitmap& SkGpuDevice::onAccessBitmap() {
298 DO_DEFERRED_CLEAR();
299 return INHERITED::onAccessBitmap();
300}
301
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000302void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
303 INHERITED::onAttachToCanvas(canvas);
304
305 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
306 fClipData.fClipStack = canvas->getClipStack();
307}
308
309void SkGpuDevice::onDetachFromCanvas() {
310 INHERITED::onDetachFromCanvas();
311 fClipData.fClipStack = NULL;
312}
313
314// call this every draw call, to ensure that the context reflects our state,
315// and not the state from some other canvas/device
316void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
317 SkASSERT(NULL != fClipData.fClipStack);
318
319 fContext->setRenderTarget(fRenderTarget);
320
321 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
322
323 if (forceIdentity) {
324 fContext->setIdentityMatrix();
325 } else {
326 fContext->setMatrix(*draw.fMatrix);
327 }
328 fClipData.fOrigin = this->getOrigin();
329
330 fContext->setClip(&fClipData);
331
332 DO_DEFERRED_CLEAR();
333}
334
335GrRenderTarget* SkGpuDevice::accessRenderTarget() {
336 DO_DEFERRED_CLEAR();
337 return fRenderTarget;
338}
339
340///////////////////////////////////////////////////////////////////////////////
341
342SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
343SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
344SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
345SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
346SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
347 shader_type_mismatch);
348SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
349 shader_type_mismatch);
350SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
351SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
352
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000353///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000354
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000355void SkGpuDevice::clear(SkColor color) {
356 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
357 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
358 fNeedClear = false;
359}
360
361void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
362 CHECK_SHOULD_DRAW(draw, false);
363
364 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000365 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000366
367 fContext->drawPaint(grPaint);
368}
369
370// must be in SkCanvas::PointMode order
371static const GrPrimitiveType gPointMode2PrimtiveType[] = {
372 kPoints_GrPrimitiveType,
373 kLines_GrPrimitiveType,
374 kLineStrip_GrPrimitiveType
375};
376
377void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
378 size_t count, const SkPoint pts[], const SkPaint& paint) {
379 CHECK_FOR_ANNOTATION(paint);
380 CHECK_SHOULD_DRAW(draw, false);
381
382 SkScalar width = paint.getStrokeWidth();
383 if (width < 0) {
384 return;
385 }
386
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000387 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000388 if (GrDashingEffect::DrawDashLine(pts, paint, this->context())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000389 return;
390 }
391 }
392
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000393 // we only handle hairlines and paints without path effects or mask filters,
394 // else we let the SkDraw call our drawPath()
395 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
396 draw.drawPoints(mode, count, pts, paint, true);
397 return;
398 }
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->drawVertices(grPaint,
404 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000405 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000406 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000407 NULL,
408 NULL,
409 NULL,
410 0);
411}
412
413///////////////////////////////////////////////////////////////////////////////
414
415void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
416 const SkPaint& paint) {
417 CHECK_FOR_ANNOTATION(paint);
418 CHECK_SHOULD_DRAW(draw, false);
419
420 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
421 SkScalar width = paint.getStrokeWidth();
422
423 /*
424 We have special code for hairline strokes, miter-strokes, bevel-stroke
425 and fills. Anything else we just call our path code.
426 */
427 bool usePath = doStroke && width > 0 &&
428 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
429 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
430 // another two reasons we might need to call drawPath...
431 if (paint.getMaskFilter() || paint.getPathEffect()) {
432 usePath = true;
433 }
434 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
435#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
436 if (doStroke) {
437#endif
438 usePath = true;
439#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
440 } else {
441 usePath = !fContext->getMatrix().preservesRightAngles();
442 }
443#endif
444 }
445 // until we can both stroke and fill rectangles
446 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
447 usePath = true;
448 }
449
450 if (usePath) {
451 SkPath path;
452 path.addRect(rect);
453 this->drawPath(draw, path, paint, NULL, true);
454 return;
455 }
456
457 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000458 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000459
460 if (!doStroke) {
461 fContext->drawRect(grPaint, rect);
462 } else {
463 SkStrokeRec stroke(paint);
464 fContext->drawRect(grPaint, rect, &stroke);
465 }
466}
467
468///////////////////////////////////////////////////////////////////////////////
469
470void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
471 const SkPaint& paint) {
472 CHECK_FOR_ANNOTATION(paint);
473 CHECK_SHOULD_DRAW(draw, false);
474
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000475 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000476 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000477
478 SkStrokeRec stroke(paint);
479 if (paint.getMaskFilter()) {
480 // try to hit the fast path for drawing filtered round rects
481
482 SkRRect devRRect;
483 if (rect.transform(fContext->getMatrix(), &devRRect)) {
484 if (devRRect.allCornersCircular()) {
485 SkRect maskRect;
486 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
487 draw.fClip->getBounds(),
488 fContext->getMatrix(),
489 &maskRect)) {
490 SkIRect finalIRect;
491 maskRect.roundOut(&finalIRect);
492 if (draw.fClip->quickReject(finalIRect)) {
493 // clipped out
494 return;
495 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000496 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
497 stroke, devRRect)) {
498 return;
499 }
500 }
501
502 }
503 }
504
505 }
506
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000507 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000508 SkPath path;
509 path.addRRect(rect);
510 this->drawPath(draw, path, paint, NULL, true);
511 return;
512 }
513
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000514 fContext->drawRRect(grPaint, rect, stroke);
515}
516
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000517void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
518 const SkRRect& inner, const SkPaint& paint) {
519 SkStrokeRec stroke(paint);
520 if (stroke.isFillStyle()) {
521
522 CHECK_FOR_ANNOTATION(paint);
523 CHECK_SHOULD_DRAW(draw, false);
524
525 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000526 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000527
528 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
529 fContext->drawDRRect(grPaint, outer, inner);
530 return;
531 }
532 }
533
534 SkPath path;
535 path.addRRect(outer);
536 path.addRRect(inner);
537 path.setFillType(SkPath::kEvenOdd_FillType);
538
539 this->drawPath(draw, path, paint, NULL, true);
540}
541
542
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000543/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000544
545void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
546 const SkPaint& paint) {
547 CHECK_FOR_ANNOTATION(paint);
548 CHECK_SHOULD_DRAW(draw, false);
549
550 bool usePath = false;
551 // some basic reasons we might need to call drawPath...
552 if (paint.getMaskFilter() || paint.getPathEffect()) {
553 usePath = true;
554 }
555
556 if (usePath) {
557 SkPath path;
558 path.addOval(oval);
559 this->drawPath(draw, path, paint, NULL, true);
560 return;
561 }
562
563 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000564 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000565 SkStrokeRec stroke(paint);
566
567 fContext->drawOval(grPaint, oval, stroke);
568}
569
570#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000571
572///////////////////////////////////////////////////////////////////////////////
573
574// helpers for applying mask filters
575namespace {
576
577// Draw a mask using the supplied paint. Since the coverage/geometry
578// is already burnt into the mask this boils down to a rect draw.
579// Return true if the mask was successfully drawn.
580bool draw_mask(GrContext* context, const SkRect& maskRect,
581 GrPaint* grp, GrTexture* mask) {
582 GrContext::AutoMatrix am;
583 if (!am.setIdentity(context, grp)) {
584 return false;
585 }
586
587 SkMatrix matrix;
588 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
589 matrix.postIDiv(mask->width(), mask->height());
590
591 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
592 context->drawRect(*grp, maskRect);
593 return true;
594}
595
596bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700597 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000598 GrPaint* grp, SkPaint::Style style) {
599 SkMask srcM, dstM;
600
601 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
602 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
603 return false;
604 }
605 SkAutoMaskFreeImage autoSrc(srcM.fImage);
606
607 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
608 return false;
609 }
610 // this will free-up dstM when we're done (allocated in filterMask())
611 SkAutoMaskFreeImage autoDst(dstM.fImage);
612
613 if (clip.quickReject(dstM.fBounds)) {
614 return false;
615 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000616
617 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
618 // the current clip (and identity matrix) and GrPaint settings
619 GrTextureDesc desc;
620 desc.fWidth = dstM.fBounds.width();
621 desc.fHeight = dstM.fBounds.height();
622 desc.fConfig = kAlpha_8_GrPixelConfig;
623
624 GrAutoScratchTexture ast(context, desc);
625 GrTexture* texture = ast.texture();
626
627 if (NULL == texture) {
628 return false;
629 }
630 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
631 dstM.fImage, dstM.fRowBytes);
632
633 SkRect maskRect = SkRect::Make(dstM.fBounds);
634
635 return draw_mask(context, maskRect, grp, texture);
636}
637
638// Create a mask of 'devPath' and place the result in 'mask'. Return true on
639// success; false otherwise.
640bool create_mask_GPU(GrContext* context,
641 const SkRect& maskRect,
642 const SkPath& devPath,
643 const SkStrokeRec& stroke,
644 bool doAA,
645 GrAutoScratchTexture* mask) {
646 GrTextureDesc desc;
647 desc.fFlags = kRenderTarget_GrTextureFlagBit;
648 desc.fWidth = SkScalarCeilToInt(maskRect.width());
649 desc.fHeight = SkScalarCeilToInt(maskRect.height());
650 // We actually only need A8, but it often isn't supported as a
651 // render target so default to RGBA_8888
652 desc.fConfig = kRGBA_8888_GrPixelConfig;
653 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
654 desc.fConfig = kAlpha_8_GrPixelConfig;
655 }
656
657 mask->set(context, desc);
658 if (NULL == mask->texture()) {
659 return false;
660 }
661
662 GrTexture* maskTexture = mask->texture();
663 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
664
665 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
666 GrContext::AutoClip ac(context, clipRect);
667
668 context->clear(NULL, 0x0, true);
669
670 GrPaint tempPaint;
671 if (doAA) {
672 tempPaint.setAntiAlias(true);
673 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
674 // blend coeff of zero requires dual source blending support in order
675 // to properly blend partially covered pixels. This means the AA
676 // code path may not be taken. So we use a dst blend coeff of ISA. We
677 // could special case AA draws to a dst surface with known alpha=0 to
678 // use a zero dst coeff when dual source blending isn't available.
679 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
680 }
681
682 GrContext::AutoMatrix am;
683
684 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
685 SkMatrix translate;
686 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
687 am.set(context, translate);
688 context->drawPath(tempPaint, devPath, stroke);
689 return true;
690}
691
692SkBitmap wrap_texture(GrTexture* texture) {
693 SkBitmap result;
reed6c225732014-06-09 19:52:07 -0700694 result.setInfo(texture->info());
695 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000696 return result;
697}
698
699};
700
701void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
702 const SkPaint& paint, const SkMatrix* prePathMatrix,
703 bool pathIsMutable) {
704 CHECK_FOR_ANNOTATION(paint);
705 CHECK_SHOULD_DRAW(draw, false);
706
707 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000708 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000709
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000710 // If we have a prematrix, apply it to the path, optimizing for the case
711 // where the original path can in fact be modified in place (even though
712 // its parameter type is const).
713 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000714 SkTLazy<SkPath> tmpPath;
715 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000716
717 if (prePathMatrix) {
718 SkPath* result = pathPtr;
719
720 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000721 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000722 pathIsMutable = true;
723 }
724 // should I push prePathMatrix on our MV stack temporarily, instead
725 // of applying it here? See SkDraw.cpp
726 pathPtr->transform(*prePathMatrix, result);
727 pathPtr = result;
728 }
729 // at this point we're done with prePathMatrix
730 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
731
732 SkStrokeRec stroke(paint);
733 SkPathEffect* pathEffect = paint.getPathEffect();
734 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000735 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000736 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000737 pathPtr = effectPath.get();
738 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000739 }
740
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000741 if (paint.getMaskFilter()) {
742 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000743 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
744 if (stroke.applyToPath(strokedPath, *pathPtr)) {
745 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000746 pathIsMutable = true;
747 stroke.setFillStyle();
748 }
749 }
750
751 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000752 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000753
754 // transform the path into device space
755 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000756
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000757 SkRect maskRect;
758 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
759 draw.fClip->getBounds(),
760 fContext->getMatrix(),
761 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000762 // The context's matrix may change while creating the mask, so save the CTM here to
763 // pass to filterMaskGPU.
764 const SkMatrix ctm = fContext->getMatrix();
765
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000766 SkIRect finalIRect;
767 maskRect.roundOut(&finalIRect);
768 if (draw.fClip->quickReject(finalIRect)) {
769 // clipped out
770 return;
771 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000772
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000773 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000774 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000775 // the mask filter was able to draw itself directly, so there's nothing
776 // left to do.
777 return;
778 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000779
780 GrAutoScratchTexture mask;
781
782 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
783 grPaint.isAntiAlias(), &mask)) {
784 GrTexture* filtered;
785
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000786 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000787 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000788 // filterMaskGPU gives us ownership of a ref to the result
789 SkAutoTUnref<GrTexture> atu(filtered);
790
791 // If the scratch texture that we used as the filter src also holds the filter
792 // result then we must detach so that this texture isn't recycled for a later
793 // draw.
794 if (filtered == mask.texture()) {
795 mask.detach();
796 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
797 }
798
799 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
800 // This path is completely drawn
801 return;
802 }
803 }
804 }
805 }
806
807 // draw the mask on the CPU - this is a fallthrough path in case the
808 // GPU path fails
809 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
810 SkPaint::kFill_Style;
reed868074b2014-06-03 10:53:59 -0700811 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(), *draw.fClip, &grPaint,
812 style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000813 return;
814 }
815
816 fContext->drawPath(grPaint, *pathPtr, stroke);
817}
818
819static const int kBmpSmallTileSize = 1 << 10;
820
821static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
822 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
823 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
824 return tilesX * tilesY;
825}
826
827static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
828 if (maxTileSize <= kBmpSmallTileSize) {
829 return maxTileSize;
830 }
831
832 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
833 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
834
835 maxTileTotalTileSize *= maxTileSize * maxTileSize;
836 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
837
838 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
839 return kBmpSmallTileSize;
840 } else {
841 return maxTileSize;
842 }
843}
844
845// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
846// pixels from the bitmap are necessary.
847static void determine_clipped_src_rect(const GrContext* context,
848 const SkBitmap& bitmap,
849 const SkRect* srcRectPtr,
850 SkIRect* clippedSrcIRect) {
851 const GrClipData* clip = context->getClip();
852 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
853 SkMatrix inv;
854 if (!context->getMatrix().invert(&inv)) {
855 clippedSrcIRect->setEmpty();
856 return;
857 }
858 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
859 inv.mapRect(&clippedSrcRect);
860 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000861 // we've setup src space 0,0 to map to the top left of the src rect.
862 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000863 if (!clippedSrcRect.intersect(*srcRectPtr)) {
864 clippedSrcIRect->setEmpty();
865 return;
866 }
867 }
868 clippedSrcRect.roundOut(clippedSrcIRect);
869 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
870 if (!clippedSrcIRect->intersect(bmpBounds)) {
871 clippedSrcIRect->setEmpty();
872 }
873}
874
875bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
876 const GrTextureParams& params,
877 const SkRect* srcRectPtr,
878 int maxTileSize,
879 int* tileSize,
880 SkIRect* clippedSrcRect) const {
881 // if bitmap is explictly texture backed then just use the texture
882 if (NULL != bitmap.getTexture()) {
883 return false;
884 }
885
886 // if it's larger than the max tile size, then we have no choice but tiling.
887 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
888 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
889 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
890 return true;
891 }
892
893 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
894 return false;
895 }
896
897 // if the entire texture is already in our cache then no reason to tile it
898 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
899 return false;
900 }
901
902 // At this point we know we could do the draw by uploading the entire bitmap
903 // as a texture. However, if the texture would be large compared to the
904 // cache size and we don't require most of it for this draw then tile to
905 // reduce the amount of upload and cache spill.
906
907 // assumption here is that sw bitmap size is a good proxy for its size as
908 // a texture
909 size_t bmpSize = bitmap.getSize();
910 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000911 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000912 if (bmpSize < cacheSize / 2) {
913 return false;
914 }
915
916 // Figure out how much of the src we will need based on the src rect and clipping.
917 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
918 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
919 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
920 kBmpSmallTileSize * kBmpSmallTileSize;
921
922 return usedTileBytes < 2 * bmpSize;
923}
924
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000925void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000926 const SkBitmap& bitmap,
927 const SkMatrix& m,
928 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000929 SkMatrix concat;
930 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
931 if (!m.isIdentity()) {
932 concat.setConcat(*draw->fMatrix, m);
933 draw.writable()->fMatrix = &concat;
934 }
935 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000936}
937
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000938// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000939// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
940// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000941static inline void clamped_outset_with_offset(SkIRect* iRect,
942 int outset,
943 SkPoint* offset,
944 const SkIRect& clamp) {
945 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000946
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000947 int leftClampDelta = clamp.fLeft - iRect->fLeft;
948 if (leftClampDelta > 0) {
949 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000950 iRect->fLeft = clamp.fLeft;
951 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000952 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000953 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000954
955 int topClampDelta = clamp.fTop - iRect->fTop;
956 if (topClampDelta > 0) {
957 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958 iRect->fTop = clamp.fTop;
959 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000960 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000961 }
962
963 if (iRect->fRight > clamp.fRight) {
964 iRect->fRight = clamp.fRight;
965 }
966 if (iRect->fBottom > clamp.fBottom) {
967 iRect->fBottom = clamp.fBottom;
968 }
969}
970
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000971static bool has_aligned_samples(const SkRect& srcRect,
972 const SkRect& transformedRect) {
973 // detect pixel disalignment
974 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
975 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
976 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
977 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
978 SkScalarAbs(transformedRect.width() - srcRect.width()) <
979 COLOR_BLEED_TOLERANCE &&
980 SkScalarAbs(transformedRect.height() - srcRect.height()) <
981 COLOR_BLEED_TOLERANCE) {
982 return true;
983 }
984 return false;
985}
986
987static bool may_color_bleed(const SkRect& srcRect,
988 const SkRect& transformedRect,
989 const SkMatrix& m) {
990 // Only gets called if has_aligned_samples returned false.
991 // So we can assume that sampling is axis aligned but not texel aligned.
992 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
993 SkRect innerSrcRect(srcRect), innerTransformedRect,
994 outerTransformedRect(transformedRect);
995 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
996 m.mapRect(&innerTransformedRect, innerSrcRect);
997
998 // The gap between outerTransformedRect and innerTransformedRect
999 // represents the projection of the source border area, which is
1000 // problematic for color bleeding. We must check whether any
1001 // destination pixels sample the border area.
1002 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1003 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1004 SkIRect outer, inner;
1005 outerTransformedRect.round(&outer);
1006 innerTransformedRect.round(&inner);
1007 // If the inner and outer rects round to the same result, it means the
1008 // border does not overlap any pixel centers. Yay!
1009 return inner != outer;
1010}
1011
1012static bool needs_texture_domain(const SkBitmap& bitmap,
1013 const SkRect& srcRect,
1014 GrTextureParams &params,
1015 const SkMatrix& contextMatrix,
1016 bool bicubic) {
1017 bool needsTextureDomain = false;
1018
1019 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1020 // Need texture domain if drawing a sub rect
1021 needsTextureDomain = srcRect.width() < bitmap.width() ||
1022 srcRect.height() < bitmap.height();
1023 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1024 // sampling is axis-aligned
1025 SkRect transformedRect;
1026 contextMatrix.mapRect(&transformedRect, srcRect);
1027
1028 if (has_aligned_samples(srcRect, transformedRect)) {
1029 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1030 needsTextureDomain = false;
1031 } else {
1032 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1033 }
1034 }
1035 }
1036 return needsTextureDomain;
1037}
1038
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001039void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1040 const SkBitmap& bitmap,
1041 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001042 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001043 const SkPaint& paint,
1044 SkCanvas::DrawBitmapRectFlags flags) {
1045 CHECK_SHOULD_DRAW(draw, false);
1046
1047 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001048 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001049 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1050 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001051 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001052 SkScalar w = SkIntToScalar(bitmap.width());
1053 SkScalar h = SkIntToScalar(bitmap.height());
1054 dstSize.fWidth = w;
1055 dstSize.fHeight = h;
1056 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001057 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001058 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001059 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001060 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001061 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001062 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1063 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1064 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1065 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001066 }
1067
1068 if (paint.getMaskFilter()){
1069 // Convert the bitmap to a shader so that the rect can be drawn
1070 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001071 SkBitmap tmp; // subset of bitmap, if necessary
1072 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001073 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001074 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001075 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1076 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1077 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001078 // In bleed mode we position and trim the bitmap based on the src rect which is
1079 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1080 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1081 // compensate.
1082 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1083 SkIRect iSrc;
1084 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001085
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001086 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1087 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001088
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001089 if (!bitmap.extractSubset(&tmp, iSrc)) {
1090 return; // extraction failed
1091 }
1092 bitmapPtr = &tmp;
1093 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001094
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001095 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001096 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001097 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001098 } else {
1099 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001100 }
1101
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001102 SkPaint paintWithShader(paint);
1103 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001104 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001105 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1106 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001107
1108 return;
1109 }
1110
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001111 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1112 // the view matrix rather than a local matrix.
1113 SkMatrix m;
1114 m.setScale(dstSize.fWidth / srcRect.width(),
1115 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001116 fContext->concatMatrix(m);
1117
1118 GrTextureParams params;
1119 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1120 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001121
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001122 bool doBicubic = false;
1123
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001124 switch(paintFilterLevel) {
1125 case SkPaint::kNone_FilterLevel:
1126 textureFilterMode = GrTextureParams::kNone_FilterMode;
1127 break;
1128 case SkPaint::kLow_FilterLevel:
1129 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1130 break;
1131 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001132 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001133 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1134 } else {
1135 // Don't trigger MIP level generation unnecessarily.
1136 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1137 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001138 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001139 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001140 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001141 doBicubic =
1142 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001143 break;
1144 default:
1145 SkErrorInternals::SetError( kInvalidPaint_SkError,
1146 "Sorry, I don't understand the filtering "
1147 "mode you asked for. Falling back to "
1148 "MIPMaps.");
1149 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1150 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001151 }
1152
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001153 int tileFilterPad;
1154 if (doBicubic) {
1155 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1156 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1157 tileFilterPad = 0;
1158 } else {
1159 tileFilterPad = 1;
1160 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001161 params.setFilterMode(textureFilterMode);
1162
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001163 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001164 int tileSize;
1165
1166 SkIRect clippedSrcRect;
1167 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1168 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001169 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1170 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001171 } else {
1172 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001173 bool needsTextureDomain = needs_texture_domain(bitmap,
1174 srcRect,
1175 params,
1176 fContext->getMatrix(),
1177 doBicubic);
1178 this->internalDrawBitmap(bitmap,
1179 srcRect,
1180 params,
1181 paint,
1182 flags,
1183 doBicubic,
1184 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001185 }
1186}
1187
1188// Break 'bitmap' into several tiles to draw it since it has already
1189// been determined to be too large to fit in VRAM
1190void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1191 const SkRect& srcRect,
1192 const SkIRect& clippedSrcIRect,
1193 const GrTextureParams& params,
1194 const SkPaint& paint,
1195 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001196 int tileSize,
1197 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001198 // The following pixel lock is technically redundant, but it is desirable
1199 // to lock outside of the tile loop to prevent redecoding the whole image
1200 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1201 // is larger than the limit of the discardable memory pool.
1202 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001203 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1204
1205 int nx = bitmap.width() / tileSize;
1206 int ny = bitmap.height() / tileSize;
1207 for (int x = 0; x <= nx; x++) {
1208 for (int y = 0; y <= ny; y++) {
1209 SkRect tileR;
1210 tileR.set(SkIntToScalar(x * tileSize),
1211 SkIntToScalar(y * tileSize),
1212 SkIntToScalar((x + 1) * tileSize),
1213 SkIntToScalar((y + 1) * tileSize));
1214
1215 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1216 continue;
1217 }
1218
1219 if (!tileR.intersect(srcRect)) {
1220 continue;
1221 }
1222
1223 SkBitmap tmpB;
1224 SkIRect iTileR;
1225 tileR.roundOut(&iTileR);
1226 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1227 SkIntToScalar(iTileR.fTop));
1228
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001229 // Adjust the context matrix to draw at the right x,y in device space
1230 SkMatrix tmpM;
1231 GrContext::AutoMatrix am;
1232 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1233 am.setPreConcat(fContext, tmpM);
1234
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001235 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001236 SkIRect iClampRect;
1237
1238 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1239 // In bleed mode we want to always expand the tile on all edges
1240 // but stay within the bitmap bounds
1241 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1242 } else {
1243 // In texture-domain/clamp mode we only want to expand the
1244 // tile on edges interior to "srcRect" (i.e., we want to
1245 // not bleed across the original clamped edges)
1246 srcRect.roundOut(&iClampRect);
1247 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001248 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1249 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001250 }
1251
1252 if (bitmap.extractSubset(&tmpB, iTileR)) {
1253 // now offset it to make it "local" to our tmp bitmap
1254 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001255 GrTextureParams paramsTemp = params;
1256 bool needsTextureDomain = needs_texture_domain(bitmap,
1257 srcRect,
1258 paramsTemp,
1259 fContext->getMatrix(),
1260 bicubic);
1261 this->internalDrawBitmap(tmpB,
1262 tileR,
1263 paramsTemp,
1264 paint,
1265 flags,
1266 bicubic,
1267 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001268 }
1269 }
1270 }
1271}
1272
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001273
1274/*
1275 * This is called by drawBitmap(), which has to handle images that may be too
1276 * large to be represented by a single texture.
1277 *
1278 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1279 * and that non-texture portion of the GrPaint has already been setup.
1280 */
1281void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1282 const SkRect& srcRect,
1283 const GrTextureParams& params,
1284 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001285 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001286 bool bicubic,
1287 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001288 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1289 bitmap.height() <= fContext->getMaxTextureSize());
1290
1291 GrTexture* texture;
1292 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1293 if (NULL == texture) {
1294 return;
1295 }
1296
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001297 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001298 SkRect paintRect;
1299 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1300 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1301 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1302 SkScalarMul(srcRect.fTop, hInv),
1303 SkScalarMul(srcRect.fRight, wInv),
1304 SkScalarMul(srcRect.fBottom, hInv));
1305
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001306 SkRect textureDomain = SkRect::MakeEmpty();
1307 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001308 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001309 // Use a constrained texture domain to avoid color bleeding
1310 SkScalar left, top, right, bottom;
1311 if (srcRect.width() > SK_Scalar1) {
1312 SkScalar border = SK_ScalarHalf / texture->width();
1313 left = paintRect.left() + border;
1314 right = paintRect.right() - border;
1315 } else {
1316 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1317 }
1318 if (srcRect.height() > SK_Scalar1) {
1319 SkScalar border = SK_ScalarHalf / texture->height();
1320 top = paintRect.top() + border;
1321 bottom = paintRect.bottom() - border;
1322 } else {
1323 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1324 }
1325 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001326 if (bicubic) {
1327 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1328 } else {
1329 effect.reset(GrTextureDomainEffect::Create(texture,
1330 SkMatrix::I(),
1331 textureDomain,
1332 GrTextureDomain::kClamp_Mode,
1333 params.filterMode()));
1334 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001335 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001336 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1337 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1338 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001339 } else {
1340 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1341 }
1342
1343 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1344 // the rest from the SkPaint.
1345 GrPaint grPaint;
1346 grPaint.addColorEffect(effect);
1347 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
dandov9de5b512014-06-10 14:38:28 -07001348 GrColor grColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1349 SkColor2GrColor(paint.getColor());
1350 SkPaint2GrPaintNoShader(this->context(), paint, grColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001351
1352 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1353}
1354
1355static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001356 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001357 int w, int h, const SkImageFilter::Context& ctx,
1358 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001359 SkASSERT(filter);
1360 SkDeviceImageFilterProxy proxy(device);
1361
1362 if (filter->canFilterImageGPU()) {
1363 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1364 // filter. Also set the clip wide open and the matrix to identity.
1365 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001366 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001367 } else {
1368 return false;
1369 }
1370}
1371
1372void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1373 int left, int top, const SkPaint& paint) {
1374 // drawSprite is defined to be in device coords.
1375 CHECK_SHOULD_DRAW(draw, true);
1376
1377 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1378 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1379 return;
1380 }
1381
1382 int w = bitmap.width();
1383 int h = bitmap.height();
1384
1385 GrTexture* texture;
1386 // draw sprite uses the default texture params
1387 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1388
1389 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001390 // This bitmap will own the filtered result as a texture.
1391 SkBitmap filteredBitmap;
1392
1393 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001394 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001395 SkMatrix matrix(*draw.fMatrix);
1396 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001397 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001398 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1399 SkAutoUnref aur(cache);
1400 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001401 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001402 &offset)) {
1403 texture = (GrTexture*) filteredBitmap.getTexture();
1404 w = filteredBitmap.width();
1405 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001406 left += offset.x();
1407 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001408 } else {
1409 return;
1410 }
1411 }
1412
1413 GrPaint grPaint;
1414 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1415
dandov9de5b512014-06-10 14:38:28 -07001416 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1417 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001418
1419 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001420 SkRect::MakeXYWH(SkIntToScalar(left),
1421 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001422 SkIntToScalar(w),
1423 SkIntToScalar(h)),
1424 SkRect::MakeXYWH(0,
1425 0,
1426 SK_Scalar1 * w / texture->width(),
1427 SK_Scalar1 * h / texture->height()));
1428}
1429
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001430void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001431 const SkRect* src, const SkRect& dst,
1432 const SkPaint& paint,
1433 SkCanvas::DrawBitmapRectFlags flags) {
1434 SkMatrix matrix;
1435 SkRect bitmapBounds, tmpSrc;
1436
1437 bitmapBounds.set(0, 0,
1438 SkIntToScalar(bitmap.width()),
1439 SkIntToScalar(bitmap.height()));
1440
1441 // Compute matrix from the two rectangles
1442 if (NULL != src) {
1443 tmpSrc = *src;
1444 } else {
1445 tmpSrc = bitmapBounds;
1446 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001447
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001448 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1449
1450 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1451 if (NULL != src) {
1452 if (!bitmapBounds.contains(tmpSrc)) {
1453 if (!tmpSrc.intersect(bitmapBounds)) {
1454 return; // nothing to draw
1455 }
1456 }
1457 }
1458
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001459 SkRect tmpDst;
1460 matrix.mapRect(&tmpDst, tmpSrc);
1461
1462 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1463 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1464 // Translate so that tempDst's top left is at the origin.
1465 matrix = *origDraw.fMatrix;
1466 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1467 draw.writable()->fMatrix = &matrix;
1468 }
1469 SkSize dstSize;
1470 dstSize.fWidth = tmpDst.width();
1471 dstSize.fHeight = tmpDst.height();
1472
1473 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001474}
1475
1476void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1477 int x, int y, const SkPaint& paint) {
1478 // clear of the source device must occur before CHECK_SHOULD_DRAW
1479 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1480 if (dev->fNeedClear) {
1481 // TODO: could check here whether we really need to draw at all
1482 dev->clear(0x0);
1483 }
1484
1485 // drawDevice is defined to be in device coords.
1486 CHECK_SHOULD_DRAW(draw, true);
1487
1488 GrRenderTarget* devRT = dev->accessRenderTarget();
1489 GrTexture* devTex;
1490 if (NULL == (devTex = devRT->asTexture())) {
1491 return;
1492 }
1493
1494 const SkBitmap& bm = dev->accessBitmap(false);
1495 int w = bm.width();
1496 int h = bm.height();
1497
1498 SkImageFilter* filter = paint.getImageFilter();
1499 // This bitmap will own the filtered result as a texture.
1500 SkBitmap filteredBitmap;
1501
1502 if (NULL != filter) {
1503 SkIPoint offset = SkIPoint::Make(0, 0);
1504 SkMatrix matrix(*draw.fMatrix);
1505 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001506 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001507 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1508 SkAutoUnref aur(cache);
1509 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001510 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001511 &offset)) {
1512 devTex = filteredBitmap.getTexture();
1513 w = filteredBitmap.width();
1514 h = filteredBitmap.height();
1515 x += offset.fX;
1516 y += offset.fY;
1517 } else {
1518 return;
1519 }
1520 }
1521
1522 GrPaint grPaint;
1523 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1524
dandov9de5b512014-06-10 14:38:28 -07001525 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1526 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001527
1528 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1529 SkIntToScalar(y),
1530 SkIntToScalar(w),
1531 SkIntToScalar(h));
1532
1533 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1534 // scratch texture).
1535 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1536 SK_Scalar1 * h / devTex->height());
1537
1538 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1539}
1540
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001541bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001542 return filter->canFilterImageGPU();
1543}
1544
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001545bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001546 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001547 SkBitmap* result, SkIPoint* offset) {
1548 // want explicitly our impl, so guard against a subclass of us overriding it
1549 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1550 return false;
1551 }
1552
1553 SkAutoLockPixels alp(src, !src.getTexture());
1554 if (!src.getTexture() && !src.readyToDraw()) {
1555 return false;
1556 }
1557
1558 GrTexture* texture;
1559 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1560 // must be pushed upstack.
1561 SkAutoCachedTexture act(this, src, NULL, &texture);
1562
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001563 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1564 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001565}
1566
1567///////////////////////////////////////////////////////////////////////////////
1568
1569// must be in SkCanvas::VertexMode order
1570static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1571 kTriangles_GrPrimitiveType,
1572 kTriangleStrip_GrPrimitiveType,
1573 kTriangleFan_GrPrimitiveType,
1574};
1575
1576void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1577 int vertexCount, const SkPoint vertices[],
1578 const SkPoint texs[], const SkColor colors[],
1579 SkXfermode* xmode,
1580 const uint16_t indices[], int indexCount,
1581 const SkPaint& paint) {
1582 CHECK_SHOULD_DRAW(draw, false);
1583
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001584 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1585 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
1586 texs = NULL;
1587 SkPaint copy(paint);
1588 copy.setStyle(SkPaint::kStroke_Style);
1589 copy.setStrokeWidth(0);
1590
1591 VertState state(vertexCount, indices, indexCount);
1592 VertState::Proc vertProc = state.chooseProc(vmode);
1593
1594 SkPoint* pts = new SkPoint[vertexCount * 6];
1595 int i = 0;
1596 while (vertProc(&state)) {
1597 pts[i] = vertices[state.f0];
1598 pts[i + 1] = vertices[state.f1];
1599 pts[i + 2] = vertices[state.f1];
1600 pts[i + 3] = vertices[state.f2];
1601 pts[i + 4] = vertices[state.f2];
1602 pts[i + 5] = vertices[state.f0];
1603 i += 6;
1604 }
1605 draw.drawPoints(SkCanvas::kLines_PointMode, i, pts, copy, true);
1606 return;
1607 }
1608
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001609 GrPaint grPaint;
1610 // we ignore the shader if texs is null.
1611 if (NULL == texs) {
dandov9de5b512014-06-10 14:38:28 -07001612 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1613 NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001614 } else {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001615 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001616 }
1617
mtklein2583b622014-06-04 08:20:41 -07001618#if 0
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001619 if (NULL != xmode && NULL != texs && NULL != colors) {
1620 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1621 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001622 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001623 }
1624 }
mtklein2583b622014-06-04 08:20:41 -07001625#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001626
1627 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1628 if (NULL != colors) {
1629 // need to convert byte order and from non-PM to PM
1630 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001631 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001632 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001633 color = colors[i];
1634 if (paint.getAlpha() != 255) {
1635 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1636 }
1637 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001638 }
1639 colors = convertedColors.get();
1640 }
1641 fContext->drawVertices(grPaint,
1642 gVertexMode2PrimitiveType[vmode],
1643 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001644 vertices,
1645 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001646 colors,
1647 indices,
1648 indexCount);
1649}
1650
1651///////////////////////////////////////////////////////////////////////////////
1652
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001653void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1654 size_t byteLength, SkScalar x, SkScalar y,
1655 const SkPaint& paint) {
1656 CHECK_SHOULD_DRAW(draw, false);
1657
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001658 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001659 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001660 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001661
1662 SkDEBUGCODE(this->validate();)
1663
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001664 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1665 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001666 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001667 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001668
1669 SkDEBUGCODE(this->validate();)
1670
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001671 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001672 } else {
1673 // this guy will just call our drawPath()
1674 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001675 }
1676}
1677
1678void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1679 size_t byteLength, const SkScalar pos[],
1680 SkScalar constY, int scalarsPerPos,
1681 const SkPaint& paint) {
1682 CHECK_SHOULD_DRAW(draw, false);
1683
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001684 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001685 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001686 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001687
1688 SkDEBUGCODE(this->validate();)
1689
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001690 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001691 constY, scalarsPerPos);
1692 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001693 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001694 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001695
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001696 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001697
1698 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001699 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001700 } else {
1701 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1702 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001703 }
1704}
1705
1706void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1707 size_t len, const SkPath& path,
1708 const SkMatrix* m, const SkPaint& paint) {
1709 CHECK_SHOULD_DRAW(draw, false);
1710
1711 SkASSERT(draw.fDevice == this);
1712 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1713}
1714
1715///////////////////////////////////////////////////////////////////////////////
1716
1717bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1718 if (!paint.isLCDRenderText()) {
1719 // we're cool with the paint as is
1720 return false;
1721 }
1722
1723 if (paint.getShader() ||
1724 paint.getXfermode() || // unless its srcover
1725 paint.getMaskFilter() ||
1726 paint.getRasterizer() ||
1727 paint.getColorFilter() ||
1728 paint.getPathEffect() ||
1729 paint.isFakeBoldText() ||
1730 paint.getStyle() != SkPaint::kFill_Style) {
1731 // turn off lcd
1732 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1733 flags->fHinting = paint.getHinting();
1734 return true;
1735 }
1736 // we're cool with the paint as is
1737 return false;
1738}
1739
1740void SkGpuDevice::flush() {
1741 DO_DEFERRED_CLEAR();
1742 fContext->resolveRenderTarget(fRenderTarget);
1743}
1744
1745///////////////////////////////////////////////////////////////////////////////
1746
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001747SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001748 GrTextureDesc desc;
1749 desc.fConfig = fRenderTarget->config();
1750 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001751 desc.fWidth = info.width();
1752 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001753 desc.fSampleCnt = fRenderTarget->numSamples();
1754
1755 SkAutoTUnref<GrTexture> texture;
1756 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001757 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001758
1759#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1760 // layers are never draw in repeat modes, so we can request an approx
1761 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001762 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001763 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1764 GrContext::kApprox_ScratchTexMatch :
1765 GrContext::kExact_ScratchTexMatch;
1766 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1767#else
1768 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1769#endif
1770 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001771 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001772 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001773 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1774 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001775 return NULL;
1776 }
1777}
1778
reed@google.com76f10a32014-02-05 15:32:21 +00001779SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1780 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1781}
1782
robertphillips9b14f262014-06-04 05:40:44 -07001783void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001784 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001785
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001786 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1787 if (NULL != existing) {
1788 return;
1789 }
1790
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001791 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001792
1793 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001794
1795 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001796}
1797
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001798static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1799 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001800 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001801 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1802}
1803
robertphillips9b14f262014-06-04 05:40:44 -07001804void SkGpuDevice::EXPERIMENTAL_purge(const SkPicture* picture) {
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001805
1806}
1807
robertphillips9b14f262014-06-04 05:40:44 -07001808bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001809
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001810 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001811
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001812 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001813 if (NULL == data) {
1814 return false;
1815 }
1816
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001817 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001818
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001819 if (0 == gpuData->numSaveLayers()) {
1820 return false;
1821 }
1822
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001823 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1824 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1825 pullForward[i] = false;
1826 }
1827
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001828 SkRect clipBounds;
1829 if (!canvas->getClipBounds(&clipBounds)) {
1830 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001831 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001832 SkIRect query;
1833 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001834
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001835 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001836
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001837 // This code pre-renders the entire layer since it will be cached and potentially
1838 // reused with different clips (e.g., in different tiles). Because of this the
1839 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1840 // is used to limit which clips are pre-rendered.
1841 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001842
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001843 if (ops.valid()) {
1844 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001845 // the BBH to limit the pre-rendering to just the layers needed to cover
1846 // the region being drawn
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001847 for (int i = 0; i < ops.numOps(); ++i) {
1848 uint32_t offset = ops.offset(i);
1849
1850 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001851 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001852 // their parent in the list.
1853 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1854 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1855
1856 if (pullForward[j]) {
1857 continue; // already pulling forward
1858 }
1859
1860 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1861 continue; // the op isn't in this range
1862 }
1863
1864 // TODO: once this code is more stable unsuitable layers can
1865 // just be omitted during the optimization stage
1866 if (!info.fValid ||
1867 kSaveLayerMaxSize < info.fSize.fWidth ||
1868 kSaveLayerMaxSize < info.fSize.fHeight ||
1869 info.fIsNested) {
1870 continue; // this layer is unsuitable
1871 }
1872
1873 pullForward[j] = true;
1874 }
1875 }
1876 } else {
1877 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001878 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001879 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1880 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1881
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001882 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1883 info.fOffset.fY,
1884 info.fSize.fWidth,
1885 info.fSize.fHeight);
1886
1887 if (!SkIRect::Intersects(query, layerRect)) {
1888 continue;
1889 }
1890
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001891 // TODO: once this code is more stable unsuitable layers can
1892 // just be omitted during the optimization stage
1893 if (!info.fValid ||
1894 kSaveLayerMaxSize < info.fSize.fWidth ||
1895 kSaveLayerMaxSize < info.fSize.fHeight ||
1896 info.fIsNested) {
1897 continue;
1898 }
1899
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001900 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001901 }
1902 }
1903
1904 SkPicturePlayback::PlaybackReplacements replacements;
1905
1906 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1907 if (pullForward[i]) {
1908 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1909
1910 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1911
1912 if (NULL != picture->fPlayback) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001913 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001914 replacements.push();
1915 layerInfo->fStart = info.fSaveLayerOpID;
1916 layerInfo->fStop = info.fRestoreOpID;
1917 layerInfo->fPos = info.fOffset;
1918
1919 GrTextureDesc desc;
1920 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1921 desc.fWidth = info.fSize.fWidth;
1922 desc.fHeight = info.fSize.fHeight;
1923 desc.fConfig = kSkia8888_GrPixelConfig;
1924 // TODO: need to deal with sample count
1925
1926 bool bNeedsRendering = true;
1927
1928 // This just uses scratch textures and doesn't cache the texture.
1929 // This can yield a lot of re-rendering
1930 if (NULL == layer->getTexture()) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001931 layer->setTexture(fContext->lockAndRefScratchTexture(desc,
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001932 GrContext::kApprox_ScratchTexMatch));
1933 if (NULL == layer->getTexture()) {
1934 continue;
1935 }
1936 } else {
1937 bNeedsRendering = false;
1938 }
1939
1940 layerInfo->fBM = SkNEW(SkBitmap);
1941 wrap_texture(layer->getTexture(), desc.fWidth, desc.fHeight, layerInfo->fBM);
1942
1943 SkASSERT(info.fPaint);
1944 layerInfo->fPaint = info.fPaint;
1945
1946 if (bNeedsRendering) {
1947 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
1948 layer->getTexture()->asRenderTarget()));
1949
1950 SkCanvas* canvas = surface->getCanvas();
1951
1952 canvas->setMatrix(info.fCTM);
1953 canvas->clear(SK_ColorTRANSPARENT);
1954
1955 picture->fPlayback->setDrawLimits(info.fSaveLayerOpID, info.fRestoreOpID);
1956 picture->fPlayback->draw(*canvas, NULL);
1957 picture->fPlayback->setDrawLimits(0, 0);
1958 canvas->flush();
1959 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001960 }
1961 }
1962 }
1963
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001964 // Playback using new layers
1965 picture->fPlayback->setReplacements(&replacements);
1966 picture->fPlayback->draw(*canvas, NULL);
1967 picture->fPlayback->setReplacements(NULL);
1968
1969 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1970 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1971
1972 if (NULL != layer->getTexture()) {
1973 fContext->unlockScratchTexture(layer->getTexture());
1974 layer->setTexture(NULL);
1975 }
1976 }
1977
1978 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001979}