blob: 15539a9dcf069620f37a297558e78378f18b27a9 [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
reeda6a8f002014-06-02 05:45:31 -0700355#ifdef SK_SUPPORT_LEGACY_DEVICE_CONFIG
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356SkBitmap::Config SkGpuDevice::config() const {
357 if (NULL == fRenderTarget) {
358 return SkBitmap::kNo_Config;
359 }
360
361 bool isOpaque;
362 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
363}
reeda6a8f002014-06-02 05:45:31 -0700364#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000365
366void SkGpuDevice::clear(SkColor color) {
367 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
368 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
369 fNeedClear = false;
370}
371
372void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
373 CHECK_SHOULD_DRAW(draw, false);
374
375 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000376 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000377
378 fContext->drawPaint(grPaint);
379}
380
381// must be in SkCanvas::PointMode order
382static const GrPrimitiveType gPointMode2PrimtiveType[] = {
383 kPoints_GrPrimitiveType,
384 kLines_GrPrimitiveType,
385 kLineStrip_GrPrimitiveType
386};
387
388void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
389 size_t count, const SkPoint pts[], const SkPaint& paint) {
390 CHECK_FOR_ANNOTATION(paint);
391 CHECK_SHOULD_DRAW(draw, false);
392
393 SkScalar width = paint.getStrokeWidth();
394 if (width < 0) {
395 return;
396 }
397
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000398 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000399 if (GrDashingEffect::DrawDashLine(pts, paint, this->context())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000400 return;
401 }
402 }
403
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000404 // we only handle hairlines and paints without path effects or mask filters,
405 // else we let the SkDraw call our drawPath()
406 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
407 draw.drawPoints(mode, count, pts, paint, true);
408 return;
409 }
410
411 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000412 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000413
414 fContext->drawVertices(grPaint,
415 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000416 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000417 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000418 NULL,
419 NULL,
420 NULL,
421 0);
422}
423
424///////////////////////////////////////////////////////////////////////////////
425
426void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
427 const SkPaint& paint) {
428 CHECK_FOR_ANNOTATION(paint);
429 CHECK_SHOULD_DRAW(draw, false);
430
431 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
432 SkScalar width = paint.getStrokeWidth();
433
434 /*
435 We have special code for hairline strokes, miter-strokes, bevel-stroke
436 and fills. Anything else we just call our path code.
437 */
438 bool usePath = doStroke && width > 0 &&
439 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
440 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
441 // another two reasons we might need to call drawPath...
442 if (paint.getMaskFilter() || paint.getPathEffect()) {
443 usePath = true;
444 }
445 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
446#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
447 if (doStroke) {
448#endif
449 usePath = true;
450#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
451 } else {
452 usePath = !fContext->getMatrix().preservesRightAngles();
453 }
454#endif
455 }
456 // until we can both stroke and fill rectangles
457 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
458 usePath = true;
459 }
460
461 if (usePath) {
462 SkPath path;
463 path.addRect(rect);
464 this->drawPath(draw, path, paint, NULL, true);
465 return;
466 }
467
468 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000469 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000470
471 if (!doStroke) {
472 fContext->drawRect(grPaint, rect);
473 } else {
474 SkStrokeRec stroke(paint);
475 fContext->drawRect(grPaint, rect, &stroke);
476 }
477}
478
479///////////////////////////////////////////////////////////////////////////////
480
481void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
482 const SkPaint& paint) {
483 CHECK_FOR_ANNOTATION(paint);
484 CHECK_SHOULD_DRAW(draw, false);
485
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000486 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000487 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000488
489 SkStrokeRec stroke(paint);
490 if (paint.getMaskFilter()) {
491 // try to hit the fast path for drawing filtered round rects
492
493 SkRRect devRRect;
494 if (rect.transform(fContext->getMatrix(), &devRRect)) {
495 if (devRRect.allCornersCircular()) {
496 SkRect maskRect;
497 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
498 draw.fClip->getBounds(),
499 fContext->getMatrix(),
500 &maskRect)) {
501 SkIRect finalIRect;
502 maskRect.roundOut(&finalIRect);
503 if (draw.fClip->quickReject(finalIRect)) {
504 // clipped out
505 return;
506 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000507 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
508 stroke, devRRect)) {
509 return;
510 }
511 }
512
513 }
514 }
515
516 }
517
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000518 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000519 SkPath path;
520 path.addRRect(rect);
521 this->drawPath(draw, path, paint, NULL, true);
522 return;
523 }
524
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000525 fContext->drawRRect(grPaint, rect, stroke);
526}
527
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000528void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
529 const SkRRect& inner, const SkPaint& paint) {
530 SkStrokeRec stroke(paint);
531 if (stroke.isFillStyle()) {
532
533 CHECK_FOR_ANNOTATION(paint);
534 CHECK_SHOULD_DRAW(draw, false);
535
536 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000537 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000538
539 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
540 fContext->drawDRRect(grPaint, outer, inner);
541 return;
542 }
543 }
544
545 SkPath path;
546 path.addRRect(outer);
547 path.addRRect(inner);
548 path.setFillType(SkPath::kEvenOdd_FillType);
549
550 this->drawPath(draw, path, paint, NULL, true);
551}
552
553
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000554/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000555
556void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
557 const SkPaint& paint) {
558 CHECK_FOR_ANNOTATION(paint);
559 CHECK_SHOULD_DRAW(draw, false);
560
561 bool usePath = false;
562 // some basic reasons we might need to call drawPath...
563 if (paint.getMaskFilter() || paint.getPathEffect()) {
564 usePath = true;
565 }
566
567 if (usePath) {
568 SkPath path;
569 path.addOval(oval);
570 this->drawPath(draw, path, paint, NULL, true);
571 return;
572 }
573
574 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000575 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000576 SkStrokeRec stroke(paint);
577
578 fContext->drawOval(grPaint, oval, stroke);
579}
580
581#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000582
583///////////////////////////////////////////////////////////////////////////////
584
585// helpers for applying mask filters
586namespace {
587
588// Draw a mask using the supplied paint. Since the coverage/geometry
589// is already burnt into the mask this boils down to a rect draw.
590// Return true if the mask was successfully drawn.
591bool draw_mask(GrContext* context, const SkRect& maskRect,
592 GrPaint* grp, GrTexture* mask) {
593 GrContext::AutoMatrix am;
594 if (!am.setIdentity(context, grp)) {
595 return false;
596 }
597
598 SkMatrix matrix;
599 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
600 matrix.postIDiv(mask->width(), mask->height());
601
602 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
603 context->drawRect(*grp, maskRect);
604 return true;
605}
606
607bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700608 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000609 GrPaint* grp, SkPaint::Style style) {
610 SkMask srcM, dstM;
611
612 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
613 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
614 return false;
615 }
616 SkAutoMaskFreeImage autoSrc(srcM.fImage);
617
618 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
619 return false;
620 }
621 // this will free-up dstM when we're done (allocated in filterMask())
622 SkAutoMaskFreeImage autoDst(dstM.fImage);
623
624 if (clip.quickReject(dstM.fBounds)) {
625 return false;
626 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000627
628 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
629 // the current clip (and identity matrix) and GrPaint settings
630 GrTextureDesc desc;
631 desc.fWidth = dstM.fBounds.width();
632 desc.fHeight = dstM.fBounds.height();
633 desc.fConfig = kAlpha_8_GrPixelConfig;
634
635 GrAutoScratchTexture ast(context, desc);
636 GrTexture* texture = ast.texture();
637
638 if (NULL == texture) {
639 return false;
640 }
641 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
642 dstM.fImage, dstM.fRowBytes);
643
644 SkRect maskRect = SkRect::Make(dstM.fBounds);
645
646 return draw_mask(context, maskRect, grp, texture);
647}
648
649// Create a mask of 'devPath' and place the result in 'mask'. Return true on
650// success; false otherwise.
651bool create_mask_GPU(GrContext* context,
652 const SkRect& maskRect,
653 const SkPath& devPath,
654 const SkStrokeRec& stroke,
655 bool doAA,
656 GrAutoScratchTexture* mask) {
657 GrTextureDesc desc;
658 desc.fFlags = kRenderTarget_GrTextureFlagBit;
659 desc.fWidth = SkScalarCeilToInt(maskRect.width());
660 desc.fHeight = SkScalarCeilToInt(maskRect.height());
661 // We actually only need A8, but it often isn't supported as a
662 // render target so default to RGBA_8888
663 desc.fConfig = kRGBA_8888_GrPixelConfig;
664 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
665 desc.fConfig = kAlpha_8_GrPixelConfig;
666 }
667
668 mask->set(context, desc);
669 if (NULL == mask->texture()) {
670 return false;
671 }
672
673 GrTexture* maskTexture = mask->texture();
674 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
675
676 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
677 GrContext::AutoClip ac(context, clipRect);
678
679 context->clear(NULL, 0x0, true);
680
681 GrPaint tempPaint;
682 if (doAA) {
683 tempPaint.setAntiAlias(true);
684 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
685 // blend coeff of zero requires dual source blending support in order
686 // to properly blend partially covered pixels. This means the AA
687 // code path may not be taken. So we use a dst blend coeff of ISA. We
688 // could special case AA draws to a dst surface with known alpha=0 to
689 // use a zero dst coeff when dual source blending isn't available.
690 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
691 }
692
693 GrContext::AutoMatrix am;
694
695 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
696 SkMatrix translate;
697 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
698 am.set(context, translate);
699 context->drawPath(tempPaint, devPath, stroke);
700 return true;
701}
702
703SkBitmap wrap_texture(GrTexture* texture) {
704 SkBitmap result;
reed6c225732014-06-09 19:52:07 -0700705 result.setInfo(texture->info());
706 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000707 return result;
708}
709
710};
711
712void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
713 const SkPaint& paint, const SkMatrix* prePathMatrix,
714 bool pathIsMutable) {
715 CHECK_FOR_ANNOTATION(paint);
716 CHECK_SHOULD_DRAW(draw, false);
717
718 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000719 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000720
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000721 // If we have a prematrix, apply it to the path, optimizing for the case
722 // where the original path can in fact be modified in place (even though
723 // its parameter type is const).
724 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000725 SkTLazy<SkPath> tmpPath;
726 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000727
728 if (prePathMatrix) {
729 SkPath* result = pathPtr;
730
731 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000732 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000733 pathIsMutable = true;
734 }
735 // should I push prePathMatrix on our MV stack temporarily, instead
736 // of applying it here? See SkDraw.cpp
737 pathPtr->transform(*prePathMatrix, result);
738 pathPtr = result;
739 }
740 // at this point we're done with prePathMatrix
741 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
742
743 SkStrokeRec stroke(paint);
744 SkPathEffect* pathEffect = paint.getPathEffect();
745 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000746 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000747 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000748 pathPtr = effectPath.get();
749 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000750 }
751
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000752 if (paint.getMaskFilter()) {
753 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000754 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
755 if (stroke.applyToPath(strokedPath, *pathPtr)) {
756 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000757 pathIsMutable = true;
758 stroke.setFillStyle();
759 }
760 }
761
762 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000763 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764
765 // transform the path into device space
766 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000767
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000768 SkRect maskRect;
769 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
770 draw.fClip->getBounds(),
771 fContext->getMatrix(),
772 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000773 // The context's matrix may change while creating the mask, so save the CTM here to
774 // pass to filterMaskGPU.
775 const SkMatrix ctm = fContext->getMatrix();
776
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000777 SkIRect finalIRect;
778 maskRect.roundOut(&finalIRect);
779 if (draw.fClip->quickReject(finalIRect)) {
780 // clipped out
781 return;
782 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000783
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000784 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000785 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000786 // the mask filter was able to draw itself directly, so there's nothing
787 // left to do.
788 return;
789 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000790
791 GrAutoScratchTexture mask;
792
793 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
794 grPaint.isAntiAlias(), &mask)) {
795 GrTexture* filtered;
796
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000797 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000798 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000799 // filterMaskGPU gives us ownership of a ref to the result
800 SkAutoTUnref<GrTexture> atu(filtered);
801
802 // If the scratch texture that we used as the filter src also holds the filter
803 // result then we must detach so that this texture isn't recycled for a later
804 // draw.
805 if (filtered == mask.texture()) {
806 mask.detach();
807 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
808 }
809
810 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
811 // This path is completely drawn
812 return;
813 }
814 }
815 }
816 }
817
818 // draw the mask on the CPU - this is a fallthrough path in case the
819 // GPU path fails
820 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
821 SkPaint::kFill_Style;
reed868074b2014-06-03 10:53:59 -0700822 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(), *draw.fClip, &grPaint,
823 style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000824 return;
825 }
826
827 fContext->drawPath(grPaint, *pathPtr, stroke);
828}
829
830static const int kBmpSmallTileSize = 1 << 10;
831
832static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
833 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
834 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
835 return tilesX * tilesY;
836}
837
838static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
839 if (maxTileSize <= kBmpSmallTileSize) {
840 return maxTileSize;
841 }
842
843 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
844 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
845
846 maxTileTotalTileSize *= maxTileSize * maxTileSize;
847 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
848
849 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
850 return kBmpSmallTileSize;
851 } else {
852 return maxTileSize;
853 }
854}
855
856// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
857// pixels from the bitmap are necessary.
858static void determine_clipped_src_rect(const GrContext* context,
859 const SkBitmap& bitmap,
860 const SkRect* srcRectPtr,
861 SkIRect* clippedSrcIRect) {
862 const GrClipData* clip = context->getClip();
863 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
864 SkMatrix inv;
865 if (!context->getMatrix().invert(&inv)) {
866 clippedSrcIRect->setEmpty();
867 return;
868 }
869 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
870 inv.mapRect(&clippedSrcRect);
871 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000872 // we've setup src space 0,0 to map to the top left of the src rect.
873 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000874 if (!clippedSrcRect.intersect(*srcRectPtr)) {
875 clippedSrcIRect->setEmpty();
876 return;
877 }
878 }
879 clippedSrcRect.roundOut(clippedSrcIRect);
880 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
881 if (!clippedSrcIRect->intersect(bmpBounds)) {
882 clippedSrcIRect->setEmpty();
883 }
884}
885
886bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
887 const GrTextureParams& params,
888 const SkRect* srcRectPtr,
889 int maxTileSize,
890 int* tileSize,
891 SkIRect* clippedSrcRect) const {
892 // if bitmap is explictly texture backed then just use the texture
893 if (NULL != bitmap.getTexture()) {
894 return false;
895 }
896
897 // if it's larger than the max tile size, then we have no choice but tiling.
898 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
899 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
900 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
901 return true;
902 }
903
904 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
905 return false;
906 }
907
908 // if the entire texture is already in our cache then no reason to tile it
909 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
910 return false;
911 }
912
913 // At this point we know we could do the draw by uploading the entire bitmap
914 // as a texture. However, if the texture would be large compared to the
915 // cache size and we don't require most of it for this draw then tile to
916 // reduce the amount of upload and cache spill.
917
918 // assumption here is that sw bitmap size is a good proxy for its size as
919 // a texture
920 size_t bmpSize = bitmap.getSize();
921 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000922 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000923 if (bmpSize < cacheSize / 2) {
924 return false;
925 }
926
927 // Figure out how much of the src we will need based on the src rect and clipping.
928 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
929 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
930 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
931 kBmpSmallTileSize * kBmpSmallTileSize;
932
933 return usedTileBytes < 2 * bmpSize;
934}
935
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000936void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000937 const SkBitmap& bitmap,
938 const SkMatrix& m,
939 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000940 SkMatrix concat;
941 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
942 if (!m.isIdentity()) {
943 concat.setConcat(*draw->fMatrix, m);
944 draw.writable()->fMatrix = &concat;
945 }
946 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000947}
948
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000949// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000950// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
951// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000952static inline void clamped_outset_with_offset(SkIRect* iRect,
953 int outset,
954 SkPoint* offset,
955 const SkIRect& clamp) {
956 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000957
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000958 int leftClampDelta = clamp.fLeft - iRect->fLeft;
959 if (leftClampDelta > 0) {
960 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000961 iRect->fLeft = clamp.fLeft;
962 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000963 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000964 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000965
966 int topClampDelta = clamp.fTop - iRect->fTop;
967 if (topClampDelta > 0) {
968 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000969 iRect->fTop = clamp.fTop;
970 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000971 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000972 }
973
974 if (iRect->fRight > clamp.fRight) {
975 iRect->fRight = clamp.fRight;
976 }
977 if (iRect->fBottom > clamp.fBottom) {
978 iRect->fBottom = clamp.fBottom;
979 }
980}
981
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000982static bool has_aligned_samples(const SkRect& srcRect,
983 const SkRect& transformedRect) {
984 // detect pixel disalignment
985 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
986 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
987 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
988 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
989 SkScalarAbs(transformedRect.width() - srcRect.width()) <
990 COLOR_BLEED_TOLERANCE &&
991 SkScalarAbs(transformedRect.height() - srcRect.height()) <
992 COLOR_BLEED_TOLERANCE) {
993 return true;
994 }
995 return false;
996}
997
998static bool may_color_bleed(const SkRect& srcRect,
999 const SkRect& transformedRect,
1000 const SkMatrix& m) {
1001 // Only gets called if has_aligned_samples returned false.
1002 // So we can assume that sampling is axis aligned but not texel aligned.
1003 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1004 SkRect innerSrcRect(srcRect), innerTransformedRect,
1005 outerTransformedRect(transformedRect);
1006 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1007 m.mapRect(&innerTransformedRect, innerSrcRect);
1008
1009 // The gap between outerTransformedRect and innerTransformedRect
1010 // represents the projection of the source border area, which is
1011 // problematic for color bleeding. We must check whether any
1012 // destination pixels sample the border area.
1013 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1014 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1015 SkIRect outer, inner;
1016 outerTransformedRect.round(&outer);
1017 innerTransformedRect.round(&inner);
1018 // If the inner and outer rects round to the same result, it means the
1019 // border does not overlap any pixel centers. Yay!
1020 return inner != outer;
1021}
1022
1023static bool needs_texture_domain(const SkBitmap& bitmap,
1024 const SkRect& srcRect,
1025 GrTextureParams &params,
1026 const SkMatrix& contextMatrix,
1027 bool bicubic) {
1028 bool needsTextureDomain = false;
1029
1030 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1031 // Need texture domain if drawing a sub rect
1032 needsTextureDomain = srcRect.width() < bitmap.width() ||
1033 srcRect.height() < bitmap.height();
1034 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1035 // sampling is axis-aligned
1036 SkRect transformedRect;
1037 contextMatrix.mapRect(&transformedRect, srcRect);
1038
1039 if (has_aligned_samples(srcRect, transformedRect)) {
1040 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1041 needsTextureDomain = false;
1042 } else {
1043 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1044 }
1045 }
1046 }
1047 return needsTextureDomain;
1048}
1049
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001050void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1051 const SkBitmap& bitmap,
1052 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001053 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001054 const SkPaint& paint,
1055 SkCanvas::DrawBitmapRectFlags flags) {
1056 CHECK_SHOULD_DRAW(draw, false);
1057
1058 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001059 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001060 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1061 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001062 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001063 SkScalar w = SkIntToScalar(bitmap.width());
1064 SkScalar h = SkIntToScalar(bitmap.height());
1065 dstSize.fWidth = w;
1066 dstSize.fHeight = h;
1067 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001068 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001069 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001070 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001071 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001072 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001073 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1074 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1075 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1076 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001077 }
1078
1079 if (paint.getMaskFilter()){
1080 // Convert the bitmap to a shader so that the rect can be drawn
1081 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001082 SkBitmap tmp; // subset of bitmap, if necessary
1083 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001084 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001085 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001086 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1087 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1088 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001089 // In bleed mode we position and trim the bitmap based on the src rect which is
1090 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1091 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1092 // compensate.
1093 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1094 SkIRect iSrc;
1095 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001096
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001097 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1098 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001099
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001100 if (!bitmap.extractSubset(&tmp, iSrc)) {
1101 return; // extraction failed
1102 }
1103 bitmapPtr = &tmp;
1104 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001105
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001106 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001107 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001108 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001109 } else {
1110 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001111 }
1112
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001113 SkPaint paintWithShader(paint);
1114 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001115 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001116 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1117 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001118
1119 return;
1120 }
1121
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001122 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1123 // the view matrix rather than a local matrix.
1124 SkMatrix m;
1125 m.setScale(dstSize.fWidth / srcRect.width(),
1126 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001127 fContext->concatMatrix(m);
1128
1129 GrTextureParams params;
1130 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1131 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001132
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001133 bool doBicubic = false;
1134
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001135 switch(paintFilterLevel) {
1136 case SkPaint::kNone_FilterLevel:
1137 textureFilterMode = GrTextureParams::kNone_FilterMode;
1138 break;
1139 case SkPaint::kLow_FilterLevel:
1140 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1141 break;
1142 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001143 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001144 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1145 } else {
1146 // Don't trigger MIP level generation unnecessarily.
1147 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1148 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001149 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001150 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001151 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001152 doBicubic =
1153 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001154 break;
1155 default:
1156 SkErrorInternals::SetError( kInvalidPaint_SkError,
1157 "Sorry, I don't understand the filtering "
1158 "mode you asked for. Falling back to "
1159 "MIPMaps.");
1160 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1161 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001162 }
1163
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001164 int tileFilterPad;
1165 if (doBicubic) {
1166 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1167 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1168 tileFilterPad = 0;
1169 } else {
1170 tileFilterPad = 1;
1171 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001172 params.setFilterMode(textureFilterMode);
1173
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001174 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001175 int tileSize;
1176
1177 SkIRect clippedSrcRect;
1178 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1179 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001180 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1181 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001182 } else {
1183 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001184 bool needsTextureDomain = needs_texture_domain(bitmap,
1185 srcRect,
1186 params,
1187 fContext->getMatrix(),
1188 doBicubic);
1189 this->internalDrawBitmap(bitmap,
1190 srcRect,
1191 params,
1192 paint,
1193 flags,
1194 doBicubic,
1195 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001196 }
1197}
1198
1199// Break 'bitmap' into several tiles to draw it since it has already
1200// been determined to be too large to fit in VRAM
1201void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1202 const SkRect& srcRect,
1203 const SkIRect& clippedSrcIRect,
1204 const GrTextureParams& params,
1205 const SkPaint& paint,
1206 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001207 int tileSize,
1208 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001209 // The following pixel lock is technically redundant, but it is desirable
1210 // to lock outside of the tile loop to prevent redecoding the whole image
1211 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1212 // is larger than the limit of the discardable memory pool.
1213 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001214 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1215
1216 int nx = bitmap.width() / tileSize;
1217 int ny = bitmap.height() / tileSize;
1218 for (int x = 0; x <= nx; x++) {
1219 for (int y = 0; y <= ny; y++) {
1220 SkRect tileR;
1221 tileR.set(SkIntToScalar(x * tileSize),
1222 SkIntToScalar(y * tileSize),
1223 SkIntToScalar((x + 1) * tileSize),
1224 SkIntToScalar((y + 1) * tileSize));
1225
1226 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1227 continue;
1228 }
1229
1230 if (!tileR.intersect(srcRect)) {
1231 continue;
1232 }
1233
1234 SkBitmap tmpB;
1235 SkIRect iTileR;
1236 tileR.roundOut(&iTileR);
1237 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1238 SkIntToScalar(iTileR.fTop));
1239
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001240 // Adjust the context matrix to draw at the right x,y in device space
1241 SkMatrix tmpM;
1242 GrContext::AutoMatrix am;
1243 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1244 am.setPreConcat(fContext, tmpM);
1245
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001246 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001247 SkIRect iClampRect;
1248
1249 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1250 // In bleed mode we want to always expand the tile on all edges
1251 // but stay within the bitmap bounds
1252 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1253 } else {
1254 // In texture-domain/clamp mode we only want to expand the
1255 // tile on edges interior to "srcRect" (i.e., we want to
1256 // not bleed across the original clamped edges)
1257 srcRect.roundOut(&iClampRect);
1258 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001259 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1260 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001261 }
1262
1263 if (bitmap.extractSubset(&tmpB, iTileR)) {
1264 // now offset it to make it "local" to our tmp bitmap
1265 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001266 GrTextureParams paramsTemp = params;
1267 bool needsTextureDomain = needs_texture_domain(bitmap,
1268 srcRect,
1269 paramsTemp,
1270 fContext->getMatrix(),
1271 bicubic);
1272 this->internalDrawBitmap(tmpB,
1273 tileR,
1274 paramsTemp,
1275 paint,
1276 flags,
1277 bicubic,
1278 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001279 }
1280 }
1281 }
1282}
1283
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001284
1285/*
1286 * This is called by drawBitmap(), which has to handle images that may be too
1287 * large to be represented by a single texture.
1288 *
1289 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1290 * and that non-texture portion of the GrPaint has already been setup.
1291 */
1292void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1293 const SkRect& srcRect,
1294 const GrTextureParams& params,
1295 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001296 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001297 bool bicubic,
1298 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001299 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1300 bitmap.height() <= fContext->getMaxTextureSize());
1301
1302 GrTexture* texture;
1303 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1304 if (NULL == texture) {
1305 return;
1306 }
1307
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001308 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001309 SkRect paintRect;
1310 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1311 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1312 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1313 SkScalarMul(srcRect.fTop, hInv),
1314 SkScalarMul(srcRect.fRight, wInv),
1315 SkScalarMul(srcRect.fBottom, hInv));
1316
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001317 SkRect textureDomain = SkRect::MakeEmpty();
1318 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001319 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001320 // Use a constrained texture domain to avoid color bleeding
1321 SkScalar left, top, right, bottom;
1322 if (srcRect.width() > SK_Scalar1) {
1323 SkScalar border = SK_ScalarHalf / texture->width();
1324 left = paintRect.left() + border;
1325 right = paintRect.right() - border;
1326 } else {
1327 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1328 }
1329 if (srcRect.height() > SK_Scalar1) {
1330 SkScalar border = SK_ScalarHalf / texture->height();
1331 top = paintRect.top() + border;
1332 bottom = paintRect.bottom() - border;
1333 } else {
1334 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1335 }
1336 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001337 if (bicubic) {
1338 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1339 } else {
1340 effect.reset(GrTextureDomainEffect::Create(texture,
1341 SkMatrix::I(),
1342 textureDomain,
1343 GrTextureDomain::kClamp_Mode,
1344 params.filterMode()));
1345 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001346 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001347 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1348 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1349 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001350 } else {
1351 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1352 }
1353
1354 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1355 // the rest from the SkPaint.
1356 GrPaint grPaint;
1357 grPaint.addColorEffect(effect);
1358 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
dandov9de5b512014-06-10 14:38:28 -07001359 GrColor grColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1360 SkColor2GrColor(paint.getColor());
1361 SkPaint2GrPaintNoShader(this->context(), paint, grColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001362
1363 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1364}
1365
1366static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001367 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001368 int w, int h, const SkImageFilter::Context& ctx,
1369 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001370 SkASSERT(filter);
1371 SkDeviceImageFilterProxy proxy(device);
1372
1373 if (filter->canFilterImageGPU()) {
1374 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1375 // filter. Also set the clip wide open and the matrix to identity.
1376 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001377 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001378 } else {
1379 return false;
1380 }
1381}
1382
1383void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1384 int left, int top, const SkPaint& paint) {
1385 // drawSprite is defined to be in device coords.
1386 CHECK_SHOULD_DRAW(draw, true);
1387
1388 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1389 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1390 return;
1391 }
1392
1393 int w = bitmap.width();
1394 int h = bitmap.height();
1395
1396 GrTexture* texture;
1397 // draw sprite uses the default texture params
1398 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1399
1400 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001401 // This bitmap will own the filtered result as a texture.
1402 SkBitmap filteredBitmap;
1403
1404 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001405 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001406 SkMatrix matrix(*draw.fMatrix);
1407 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001408 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001409 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1410 SkAutoUnref aur(cache);
1411 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001412 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001413 &offset)) {
1414 texture = (GrTexture*) filteredBitmap.getTexture();
1415 w = filteredBitmap.width();
1416 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001417 left += offset.x();
1418 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001419 } else {
1420 return;
1421 }
1422 }
1423
1424 GrPaint grPaint;
1425 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1426
dandov9de5b512014-06-10 14:38:28 -07001427 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1428 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001429
1430 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001431 SkRect::MakeXYWH(SkIntToScalar(left),
1432 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001433 SkIntToScalar(w),
1434 SkIntToScalar(h)),
1435 SkRect::MakeXYWH(0,
1436 0,
1437 SK_Scalar1 * w / texture->width(),
1438 SK_Scalar1 * h / texture->height()));
1439}
1440
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001441void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001442 const SkRect* src, const SkRect& dst,
1443 const SkPaint& paint,
1444 SkCanvas::DrawBitmapRectFlags flags) {
1445 SkMatrix matrix;
1446 SkRect bitmapBounds, tmpSrc;
1447
1448 bitmapBounds.set(0, 0,
1449 SkIntToScalar(bitmap.width()),
1450 SkIntToScalar(bitmap.height()));
1451
1452 // Compute matrix from the two rectangles
1453 if (NULL != src) {
1454 tmpSrc = *src;
1455 } else {
1456 tmpSrc = bitmapBounds;
1457 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001458
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001459 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1460
1461 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1462 if (NULL != src) {
1463 if (!bitmapBounds.contains(tmpSrc)) {
1464 if (!tmpSrc.intersect(bitmapBounds)) {
1465 return; // nothing to draw
1466 }
1467 }
1468 }
1469
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001470 SkRect tmpDst;
1471 matrix.mapRect(&tmpDst, tmpSrc);
1472
1473 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1474 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1475 // Translate so that tempDst's top left is at the origin.
1476 matrix = *origDraw.fMatrix;
1477 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1478 draw.writable()->fMatrix = &matrix;
1479 }
1480 SkSize dstSize;
1481 dstSize.fWidth = tmpDst.width();
1482 dstSize.fHeight = tmpDst.height();
1483
1484 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001485}
1486
1487void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1488 int x, int y, const SkPaint& paint) {
1489 // clear of the source device must occur before CHECK_SHOULD_DRAW
1490 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1491 if (dev->fNeedClear) {
1492 // TODO: could check here whether we really need to draw at all
1493 dev->clear(0x0);
1494 }
1495
1496 // drawDevice is defined to be in device coords.
1497 CHECK_SHOULD_DRAW(draw, true);
1498
1499 GrRenderTarget* devRT = dev->accessRenderTarget();
1500 GrTexture* devTex;
1501 if (NULL == (devTex = devRT->asTexture())) {
1502 return;
1503 }
1504
1505 const SkBitmap& bm = dev->accessBitmap(false);
1506 int w = bm.width();
1507 int h = bm.height();
1508
1509 SkImageFilter* filter = paint.getImageFilter();
1510 // This bitmap will own the filtered result as a texture.
1511 SkBitmap filteredBitmap;
1512
1513 if (NULL != filter) {
1514 SkIPoint offset = SkIPoint::Make(0, 0);
1515 SkMatrix matrix(*draw.fMatrix);
1516 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001517 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001518 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1519 SkAutoUnref aur(cache);
1520 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001521 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001522 &offset)) {
1523 devTex = filteredBitmap.getTexture();
1524 w = filteredBitmap.width();
1525 h = filteredBitmap.height();
1526 x += offset.fX;
1527 y += offset.fY;
1528 } else {
1529 return;
1530 }
1531 }
1532
1533 GrPaint grPaint;
1534 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1535
dandov9de5b512014-06-10 14:38:28 -07001536 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1537 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001538
1539 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1540 SkIntToScalar(y),
1541 SkIntToScalar(w),
1542 SkIntToScalar(h));
1543
1544 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1545 // scratch texture).
1546 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1547 SK_Scalar1 * h / devTex->height());
1548
1549 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1550}
1551
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001552bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001553 return filter->canFilterImageGPU();
1554}
1555
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001556bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001557 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001558 SkBitmap* result, SkIPoint* offset) {
1559 // want explicitly our impl, so guard against a subclass of us overriding it
1560 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1561 return false;
1562 }
1563
1564 SkAutoLockPixels alp(src, !src.getTexture());
1565 if (!src.getTexture() && !src.readyToDraw()) {
1566 return false;
1567 }
1568
1569 GrTexture* texture;
1570 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1571 // must be pushed upstack.
1572 SkAutoCachedTexture act(this, src, NULL, &texture);
1573
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001574 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1575 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001576}
1577
1578///////////////////////////////////////////////////////////////////////////////
1579
1580// must be in SkCanvas::VertexMode order
1581static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1582 kTriangles_GrPrimitiveType,
1583 kTriangleStrip_GrPrimitiveType,
1584 kTriangleFan_GrPrimitiveType,
1585};
1586
1587void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1588 int vertexCount, const SkPoint vertices[],
1589 const SkPoint texs[], const SkColor colors[],
1590 SkXfermode* xmode,
1591 const uint16_t indices[], int indexCount,
1592 const SkPaint& paint) {
1593 CHECK_SHOULD_DRAW(draw, false);
1594
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001595 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1596 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
1597 texs = NULL;
1598 SkPaint copy(paint);
1599 copy.setStyle(SkPaint::kStroke_Style);
1600 copy.setStrokeWidth(0);
1601
1602 VertState state(vertexCount, indices, indexCount);
1603 VertState::Proc vertProc = state.chooseProc(vmode);
1604
1605 SkPoint* pts = new SkPoint[vertexCount * 6];
1606 int i = 0;
1607 while (vertProc(&state)) {
1608 pts[i] = vertices[state.f0];
1609 pts[i + 1] = vertices[state.f1];
1610 pts[i + 2] = vertices[state.f1];
1611 pts[i + 3] = vertices[state.f2];
1612 pts[i + 4] = vertices[state.f2];
1613 pts[i + 5] = vertices[state.f0];
1614 i += 6;
1615 }
1616 draw.drawPoints(SkCanvas::kLines_PointMode, i, pts, copy, true);
1617 return;
1618 }
1619
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001620 GrPaint grPaint;
1621 // we ignore the shader if texs is null.
1622 if (NULL == texs) {
dandov9de5b512014-06-10 14:38:28 -07001623 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1624 NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001625 } else {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001626 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001627 }
1628
mtklein2583b622014-06-04 08:20:41 -07001629#if 0
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001630 if (NULL != xmode && NULL != texs && NULL != colors) {
1631 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1632 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001633 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001634 }
1635 }
mtklein2583b622014-06-04 08:20:41 -07001636#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001637
1638 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1639 if (NULL != colors) {
1640 // need to convert byte order and from non-PM to PM
1641 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001642 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001643 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001644 color = colors[i];
1645 if (paint.getAlpha() != 255) {
1646 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1647 }
1648 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001649 }
1650 colors = convertedColors.get();
1651 }
1652 fContext->drawVertices(grPaint,
1653 gVertexMode2PrimitiveType[vmode],
1654 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001655 vertices,
1656 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001657 colors,
1658 indices,
1659 indexCount);
1660}
1661
1662///////////////////////////////////////////////////////////////////////////////
1663
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001664void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1665 size_t byteLength, SkScalar x, SkScalar y,
1666 const SkPaint& paint) {
1667 CHECK_SHOULD_DRAW(draw, false);
1668
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001669 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001670 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001671 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001672
1673 SkDEBUGCODE(this->validate();)
1674
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001675 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1676 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001677 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001678 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001679
1680 SkDEBUGCODE(this->validate();)
1681
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001682 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001683 } else {
1684 // this guy will just call our drawPath()
1685 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001686 }
1687}
1688
1689void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1690 size_t byteLength, const SkScalar pos[],
1691 SkScalar constY, int scalarsPerPos,
1692 const SkPaint& paint) {
1693 CHECK_SHOULD_DRAW(draw, false);
1694
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001695 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001696 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001697 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001698
1699 SkDEBUGCODE(this->validate();)
1700
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001701 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001702 constY, scalarsPerPos);
1703 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001704 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001705 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001706
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001707 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001708
1709 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001710 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001711 } else {
1712 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1713 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001714 }
1715}
1716
1717void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1718 size_t len, const SkPath& path,
1719 const SkMatrix* m, const SkPaint& paint) {
1720 CHECK_SHOULD_DRAW(draw, false);
1721
1722 SkASSERT(draw.fDevice == this);
1723 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1724}
1725
1726///////////////////////////////////////////////////////////////////////////////
1727
1728bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1729 if (!paint.isLCDRenderText()) {
1730 // we're cool with the paint as is
1731 return false;
1732 }
1733
1734 if (paint.getShader() ||
1735 paint.getXfermode() || // unless its srcover
1736 paint.getMaskFilter() ||
1737 paint.getRasterizer() ||
1738 paint.getColorFilter() ||
1739 paint.getPathEffect() ||
1740 paint.isFakeBoldText() ||
1741 paint.getStyle() != SkPaint::kFill_Style) {
1742 // turn off lcd
1743 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1744 flags->fHinting = paint.getHinting();
1745 return true;
1746 }
1747 // we're cool with the paint as is
1748 return false;
1749}
1750
1751void SkGpuDevice::flush() {
1752 DO_DEFERRED_CLEAR();
1753 fContext->resolveRenderTarget(fRenderTarget);
1754}
1755
1756///////////////////////////////////////////////////////////////////////////////
1757
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001758SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001759 GrTextureDesc desc;
1760 desc.fConfig = fRenderTarget->config();
1761 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001762 desc.fWidth = info.width();
1763 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001764 desc.fSampleCnt = fRenderTarget->numSamples();
1765
1766 SkAutoTUnref<GrTexture> texture;
1767 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001768 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001769
1770#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1771 // layers are never draw in repeat modes, so we can request an approx
1772 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001773 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001774 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1775 GrContext::kApprox_ScratchTexMatch :
1776 GrContext::kExact_ScratchTexMatch;
1777 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1778#else
1779 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1780#endif
1781 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001782 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001783 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001784 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1785 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001786 return NULL;
1787 }
1788}
1789
reed@google.com76f10a32014-02-05 15:32:21 +00001790SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1791 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1792}
1793
robertphillips9b14f262014-06-04 05:40:44 -07001794void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001795 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001796
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001797 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1798 if (NULL != existing) {
1799 return;
1800 }
1801
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001802 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001803
1804 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001805
1806 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001807}
1808
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001809static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1810 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001811 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001812 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1813}
1814
robertphillips9b14f262014-06-04 05:40:44 -07001815void SkGpuDevice::EXPERIMENTAL_purge(const SkPicture* picture) {
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001816
1817}
1818
robertphillips9b14f262014-06-04 05:40:44 -07001819bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001820
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001821 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001822
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001823 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001824 if (NULL == data) {
1825 return false;
1826 }
1827
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001828 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001829
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001830 if (0 == gpuData->numSaveLayers()) {
1831 return false;
1832 }
1833
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001834 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1835 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1836 pullForward[i] = false;
1837 }
1838
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001839 SkRect clipBounds;
1840 if (!canvas->getClipBounds(&clipBounds)) {
1841 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001842 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001843 SkIRect query;
1844 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001845
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001846 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001847
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001848 // This code pre-renders the entire layer since it will be cached and potentially
1849 // reused with different clips (e.g., in different tiles). Because of this the
1850 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1851 // is used to limit which clips are pre-rendered.
1852 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001853
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001854 if (ops.valid()) {
1855 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001856 // the BBH to limit the pre-rendering to just the layers needed to cover
1857 // the region being drawn
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001858 for (int i = 0; i < ops.numOps(); ++i) {
1859 uint32_t offset = ops.offset(i);
1860
1861 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001862 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001863 // their parent in the list.
1864 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1865 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1866
1867 if (pullForward[j]) {
1868 continue; // already pulling forward
1869 }
1870
1871 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1872 continue; // the op isn't in this range
1873 }
1874
1875 // TODO: once this code is more stable unsuitable layers can
1876 // just be omitted during the optimization stage
1877 if (!info.fValid ||
1878 kSaveLayerMaxSize < info.fSize.fWidth ||
1879 kSaveLayerMaxSize < info.fSize.fHeight ||
1880 info.fIsNested) {
1881 continue; // this layer is unsuitable
1882 }
1883
1884 pullForward[j] = true;
1885 }
1886 }
1887 } else {
1888 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001889 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001890 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1891 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1892
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001893 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1894 info.fOffset.fY,
1895 info.fSize.fWidth,
1896 info.fSize.fHeight);
1897
1898 if (!SkIRect::Intersects(query, layerRect)) {
1899 continue;
1900 }
1901
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001902 // TODO: once this code is more stable unsuitable layers can
1903 // just be omitted during the optimization stage
1904 if (!info.fValid ||
1905 kSaveLayerMaxSize < info.fSize.fWidth ||
1906 kSaveLayerMaxSize < info.fSize.fHeight ||
1907 info.fIsNested) {
1908 continue;
1909 }
1910
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001911 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001912 }
1913 }
1914
1915 SkPicturePlayback::PlaybackReplacements replacements;
1916
1917 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1918 if (pullForward[i]) {
1919 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1920
1921 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1922
1923 if (NULL != picture->fPlayback) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001924 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001925 replacements.push();
1926 layerInfo->fStart = info.fSaveLayerOpID;
1927 layerInfo->fStop = info.fRestoreOpID;
1928 layerInfo->fPos = info.fOffset;
1929
1930 GrTextureDesc desc;
1931 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1932 desc.fWidth = info.fSize.fWidth;
1933 desc.fHeight = info.fSize.fHeight;
1934 desc.fConfig = kSkia8888_GrPixelConfig;
1935 // TODO: need to deal with sample count
1936
1937 bool bNeedsRendering = true;
1938
1939 // This just uses scratch textures and doesn't cache the texture.
1940 // This can yield a lot of re-rendering
1941 if (NULL == layer->getTexture()) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001942 layer->setTexture(fContext->lockAndRefScratchTexture(desc,
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001943 GrContext::kApprox_ScratchTexMatch));
1944 if (NULL == layer->getTexture()) {
1945 continue;
1946 }
1947 } else {
1948 bNeedsRendering = false;
1949 }
1950
1951 layerInfo->fBM = SkNEW(SkBitmap);
1952 wrap_texture(layer->getTexture(), desc.fWidth, desc.fHeight, layerInfo->fBM);
1953
1954 SkASSERT(info.fPaint);
1955 layerInfo->fPaint = info.fPaint;
1956
1957 if (bNeedsRendering) {
1958 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
1959 layer->getTexture()->asRenderTarget()));
1960
1961 SkCanvas* canvas = surface->getCanvas();
1962
1963 canvas->setMatrix(info.fCTM);
1964 canvas->clear(SK_ColorTRANSPARENT);
1965
1966 picture->fPlayback->setDrawLimits(info.fSaveLayerOpID, info.fRestoreOpID);
1967 picture->fPlayback->draw(*canvas, NULL);
1968 picture->fPlayback->setDrawLimits(0, 0);
1969 canvas->flush();
1970 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001971 }
1972 }
1973 }
1974
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001975 // Playback using new layers
1976 picture->fPlayback->setReplacements(&replacements);
1977 picture->fPlayback->draw(*canvas, NULL);
1978 picture->fPlayback->setReplacements(NULL);
1979
1980 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1981 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1982
1983 if (NULL != layer->getTexture()) {
1984 fContext->unlockScratchTexture(layer->getTexture());
1985 layer->setTexture(NULL);
1986 }
1987 }
1988
1989 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001990}