blob: 44120a220f7ab84f3b2e16eca5326fdb61dba851 [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());
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001359 SkPaint2GrPaintNoShader(this->context(), paint, alphaOnly, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001360
1361 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1362}
1363
1364static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001365 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001366 int w, int h, const SkImageFilter::Context& ctx,
1367 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001368 SkASSERT(filter);
1369 SkDeviceImageFilterProxy proxy(device);
1370
1371 if (filter->canFilterImageGPU()) {
1372 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1373 // filter. Also set the clip wide open and the matrix to identity.
1374 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001375 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001376 } else {
1377 return false;
1378 }
1379}
1380
1381void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1382 int left, int top, const SkPaint& paint) {
1383 // drawSprite is defined to be in device coords.
1384 CHECK_SHOULD_DRAW(draw, true);
1385
1386 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1387 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1388 return;
1389 }
1390
1391 int w = bitmap.width();
1392 int h = bitmap.height();
1393
1394 GrTexture* texture;
1395 // draw sprite uses the default texture params
1396 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1397
1398 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001399 // This bitmap will own the filtered result as a texture.
1400 SkBitmap filteredBitmap;
1401
1402 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001403 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001404 SkMatrix matrix(*draw.fMatrix);
1405 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001406 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001407 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1408 SkAutoUnref aur(cache);
1409 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001410 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001411 &offset)) {
1412 texture = (GrTexture*) filteredBitmap.getTexture();
1413 w = filteredBitmap.width();
1414 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001415 left += offset.x();
1416 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001417 } else {
1418 return;
1419 }
1420 }
1421
1422 GrPaint grPaint;
1423 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1424
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001425 SkPaint2GrPaintNoShader(this->context(), paint, true, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001426
1427 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001428 SkRect::MakeXYWH(SkIntToScalar(left),
1429 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001430 SkIntToScalar(w),
1431 SkIntToScalar(h)),
1432 SkRect::MakeXYWH(0,
1433 0,
1434 SK_Scalar1 * w / texture->width(),
1435 SK_Scalar1 * h / texture->height()));
1436}
1437
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001438void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001439 const SkRect* src, const SkRect& dst,
1440 const SkPaint& paint,
1441 SkCanvas::DrawBitmapRectFlags flags) {
1442 SkMatrix matrix;
1443 SkRect bitmapBounds, tmpSrc;
1444
1445 bitmapBounds.set(0, 0,
1446 SkIntToScalar(bitmap.width()),
1447 SkIntToScalar(bitmap.height()));
1448
1449 // Compute matrix from the two rectangles
1450 if (NULL != src) {
1451 tmpSrc = *src;
1452 } else {
1453 tmpSrc = bitmapBounds;
1454 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001455
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001456 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1457
1458 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1459 if (NULL != src) {
1460 if (!bitmapBounds.contains(tmpSrc)) {
1461 if (!tmpSrc.intersect(bitmapBounds)) {
1462 return; // nothing to draw
1463 }
1464 }
1465 }
1466
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001467 SkRect tmpDst;
1468 matrix.mapRect(&tmpDst, tmpSrc);
1469
1470 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1471 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1472 // Translate so that tempDst's top left is at the origin.
1473 matrix = *origDraw.fMatrix;
1474 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1475 draw.writable()->fMatrix = &matrix;
1476 }
1477 SkSize dstSize;
1478 dstSize.fWidth = tmpDst.width();
1479 dstSize.fHeight = tmpDst.height();
1480
1481 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001482}
1483
1484void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1485 int x, int y, const SkPaint& paint) {
1486 // clear of the source device must occur before CHECK_SHOULD_DRAW
1487 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1488 if (dev->fNeedClear) {
1489 // TODO: could check here whether we really need to draw at all
1490 dev->clear(0x0);
1491 }
1492
1493 // drawDevice is defined to be in device coords.
1494 CHECK_SHOULD_DRAW(draw, true);
1495
1496 GrRenderTarget* devRT = dev->accessRenderTarget();
1497 GrTexture* devTex;
1498 if (NULL == (devTex = devRT->asTexture())) {
1499 return;
1500 }
1501
1502 const SkBitmap& bm = dev->accessBitmap(false);
1503 int w = bm.width();
1504 int h = bm.height();
1505
1506 SkImageFilter* filter = paint.getImageFilter();
1507 // This bitmap will own the filtered result as a texture.
1508 SkBitmap filteredBitmap;
1509
1510 if (NULL != filter) {
1511 SkIPoint offset = SkIPoint::Make(0, 0);
1512 SkMatrix matrix(*draw.fMatrix);
1513 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001514 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001515 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1516 SkAutoUnref aur(cache);
1517 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001518 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001519 &offset)) {
1520 devTex = filteredBitmap.getTexture();
1521 w = filteredBitmap.width();
1522 h = filteredBitmap.height();
1523 x += offset.fX;
1524 y += offset.fY;
1525 } else {
1526 return;
1527 }
1528 }
1529
1530 GrPaint grPaint;
1531 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1532
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001533 SkPaint2GrPaintNoShader(this->context(), paint, true, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001534
1535 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1536 SkIntToScalar(y),
1537 SkIntToScalar(w),
1538 SkIntToScalar(h));
1539
1540 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1541 // scratch texture).
1542 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1543 SK_Scalar1 * h / devTex->height());
1544
1545 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1546}
1547
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001548bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001549 return filter->canFilterImageGPU();
1550}
1551
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001552bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001553 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001554 SkBitmap* result, SkIPoint* offset) {
1555 // want explicitly our impl, so guard against a subclass of us overriding it
1556 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1557 return false;
1558 }
1559
1560 SkAutoLockPixels alp(src, !src.getTexture());
1561 if (!src.getTexture() && !src.readyToDraw()) {
1562 return false;
1563 }
1564
1565 GrTexture* texture;
1566 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1567 // must be pushed upstack.
1568 SkAutoCachedTexture act(this, src, NULL, &texture);
1569
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001570 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1571 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001572}
1573
1574///////////////////////////////////////////////////////////////////////////////
1575
1576// must be in SkCanvas::VertexMode order
1577static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1578 kTriangles_GrPrimitiveType,
1579 kTriangleStrip_GrPrimitiveType,
1580 kTriangleFan_GrPrimitiveType,
1581};
1582
1583void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1584 int vertexCount, const SkPoint vertices[],
1585 const SkPoint texs[], const SkColor colors[],
1586 SkXfermode* xmode,
1587 const uint16_t indices[], int indexCount,
1588 const SkPaint& paint) {
1589 CHECK_SHOULD_DRAW(draw, false);
1590
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001591 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1592 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
1593 texs = NULL;
1594 SkPaint copy(paint);
1595 copy.setStyle(SkPaint::kStroke_Style);
1596 copy.setStrokeWidth(0);
1597
1598 VertState state(vertexCount, indices, indexCount);
1599 VertState::Proc vertProc = state.chooseProc(vmode);
1600
1601 SkPoint* pts = new SkPoint[vertexCount * 6];
1602 int i = 0;
1603 while (vertProc(&state)) {
1604 pts[i] = vertices[state.f0];
1605 pts[i + 1] = vertices[state.f1];
1606 pts[i + 2] = vertices[state.f1];
1607 pts[i + 3] = vertices[state.f2];
1608 pts[i + 4] = vertices[state.f2];
1609 pts[i + 5] = vertices[state.f0];
1610 i += 6;
1611 }
1612 draw.drawPoints(SkCanvas::kLines_PointMode, i, pts, copy, true);
1613 return;
1614 }
1615
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001616 GrPaint grPaint;
1617 // we ignore the shader if texs is null.
1618 if (NULL == texs) {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001619 SkPaint2GrPaintNoShader(this->context(), paint, false, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001620 } else {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001621 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001622 }
1623
mtklein2583b622014-06-04 08:20:41 -07001624#if 0
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001625 if (NULL != xmode && NULL != texs && NULL != colors) {
1626 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1627 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001628 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001629 }
1630 }
mtklein2583b622014-06-04 08:20:41 -07001631#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001632
1633 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1634 if (NULL != colors) {
1635 // need to convert byte order and from non-PM to PM
1636 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001637 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001638 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001639 color = colors[i];
1640 if (paint.getAlpha() != 255) {
1641 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1642 }
1643 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001644 }
1645 colors = convertedColors.get();
1646 }
1647 fContext->drawVertices(grPaint,
1648 gVertexMode2PrimitiveType[vmode],
1649 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001650 vertices,
1651 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001652 colors,
1653 indices,
1654 indexCount);
1655}
1656
1657///////////////////////////////////////////////////////////////////////////////
1658
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001659void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1660 size_t byteLength, SkScalar x, SkScalar y,
1661 const SkPaint& paint) {
1662 CHECK_SHOULD_DRAW(draw, false);
1663
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001664 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001665 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001666 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001667
1668 SkDEBUGCODE(this->validate();)
1669
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001670 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1671 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001672 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001673 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001674
1675 SkDEBUGCODE(this->validate();)
1676
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001677 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001678 } else {
1679 // this guy will just call our drawPath()
1680 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001681 }
1682}
1683
1684void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1685 size_t byteLength, const SkScalar pos[],
1686 SkScalar constY, int scalarsPerPos,
1687 const SkPaint& paint) {
1688 CHECK_SHOULD_DRAW(draw, false);
1689
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001690 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001691 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001692 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001693
1694 SkDEBUGCODE(this->validate();)
1695
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001696 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001697 constY, scalarsPerPos);
1698 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001699 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001700 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001701
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001702 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001703
1704 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001705 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001706 } else {
1707 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1708 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001709 }
1710}
1711
1712void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1713 size_t len, const SkPath& path,
1714 const SkMatrix* m, const SkPaint& paint) {
1715 CHECK_SHOULD_DRAW(draw, false);
1716
1717 SkASSERT(draw.fDevice == this);
1718 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1719}
1720
1721///////////////////////////////////////////////////////////////////////////////
1722
1723bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1724 if (!paint.isLCDRenderText()) {
1725 // we're cool with the paint as is
1726 return false;
1727 }
1728
1729 if (paint.getShader() ||
1730 paint.getXfermode() || // unless its srcover
1731 paint.getMaskFilter() ||
1732 paint.getRasterizer() ||
1733 paint.getColorFilter() ||
1734 paint.getPathEffect() ||
1735 paint.isFakeBoldText() ||
1736 paint.getStyle() != SkPaint::kFill_Style) {
1737 // turn off lcd
1738 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1739 flags->fHinting = paint.getHinting();
1740 return true;
1741 }
1742 // we're cool with the paint as is
1743 return false;
1744}
1745
1746void SkGpuDevice::flush() {
1747 DO_DEFERRED_CLEAR();
1748 fContext->resolveRenderTarget(fRenderTarget);
1749}
1750
1751///////////////////////////////////////////////////////////////////////////////
1752
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001753SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001754 GrTextureDesc desc;
1755 desc.fConfig = fRenderTarget->config();
1756 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001757 desc.fWidth = info.width();
1758 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001759 desc.fSampleCnt = fRenderTarget->numSamples();
1760
1761 SkAutoTUnref<GrTexture> texture;
1762 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001763 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001764
1765#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1766 // layers are never draw in repeat modes, so we can request an approx
1767 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001768 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001769 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1770 GrContext::kApprox_ScratchTexMatch :
1771 GrContext::kExact_ScratchTexMatch;
1772 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1773#else
1774 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1775#endif
1776 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001777 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001778 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001779 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1780 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001781 return NULL;
1782 }
1783}
1784
reed@google.com76f10a32014-02-05 15:32:21 +00001785SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1786 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1787}
1788
robertphillips9b14f262014-06-04 05:40:44 -07001789void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001790 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001791
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001792 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1793 if (NULL != existing) {
1794 return;
1795 }
1796
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001797 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001798
1799 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001800
1801 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001802}
1803
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001804static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1805 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001806 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001807 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1808}
1809
robertphillips9b14f262014-06-04 05:40:44 -07001810void SkGpuDevice::EXPERIMENTAL_purge(const SkPicture* picture) {
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001811
1812}
1813
robertphillips9b14f262014-06-04 05:40:44 -07001814bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001815
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001816 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001817
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001818 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001819 if (NULL == data) {
1820 return false;
1821 }
1822
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001823 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001824
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001825 if (0 == gpuData->numSaveLayers()) {
1826 return false;
1827 }
1828
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001829 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1830 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1831 pullForward[i] = false;
1832 }
1833
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001834 SkRect clipBounds;
1835 if (!canvas->getClipBounds(&clipBounds)) {
1836 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001837 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001838 SkIRect query;
1839 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001840
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001841 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001842
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001843 // This code pre-renders the entire layer since it will be cached and potentially
1844 // reused with different clips (e.g., in different tiles). Because of this the
1845 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1846 // is used to limit which clips are pre-rendered.
1847 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001848
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001849 if (ops.valid()) {
1850 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001851 // the BBH to limit the pre-rendering to just the layers needed to cover
1852 // the region being drawn
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001853 for (int i = 0; i < ops.numOps(); ++i) {
1854 uint32_t offset = ops.offset(i);
1855
1856 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001857 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001858 // their parent in the list.
1859 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1860 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1861
1862 if (pullForward[j]) {
1863 continue; // already pulling forward
1864 }
1865
1866 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1867 continue; // the op isn't in this range
1868 }
1869
1870 // TODO: once this code is more stable unsuitable layers can
1871 // just be omitted during the optimization stage
1872 if (!info.fValid ||
1873 kSaveLayerMaxSize < info.fSize.fWidth ||
1874 kSaveLayerMaxSize < info.fSize.fHeight ||
1875 info.fIsNested) {
1876 continue; // this layer is unsuitable
1877 }
1878
1879 pullForward[j] = true;
1880 }
1881 }
1882 } else {
1883 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001884 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001885 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1886 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1887
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001888 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1889 info.fOffset.fY,
1890 info.fSize.fWidth,
1891 info.fSize.fHeight);
1892
1893 if (!SkIRect::Intersects(query, layerRect)) {
1894 continue;
1895 }
1896
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001897 // TODO: once this code is more stable unsuitable layers can
1898 // just be omitted during the optimization stage
1899 if (!info.fValid ||
1900 kSaveLayerMaxSize < info.fSize.fWidth ||
1901 kSaveLayerMaxSize < info.fSize.fHeight ||
1902 info.fIsNested) {
1903 continue;
1904 }
1905
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001906 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001907 }
1908 }
1909
1910 SkPicturePlayback::PlaybackReplacements replacements;
1911
1912 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1913 if (pullForward[i]) {
1914 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1915
1916 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1917
1918 if (NULL != picture->fPlayback) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001919 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001920 replacements.push();
1921 layerInfo->fStart = info.fSaveLayerOpID;
1922 layerInfo->fStop = info.fRestoreOpID;
1923 layerInfo->fPos = info.fOffset;
1924
1925 GrTextureDesc desc;
1926 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1927 desc.fWidth = info.fSize.fWidth;
1928 desc.fHeight = info.fSize.fHeight;
1929 desc.fConfig = kSkia8888_GrPixelConfig;
1930 // TODO: need to deal with sample count
1931
1932 bool bNeedsRendering = true;
1933
1934 // This just uses scratch textures and doesn't cache the texture.
1935 // This can yield a lot of re-rendering
1936 if (NULL == layer->getTexture()) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001937 layer->setTexture(fContext->lockAndRefScratchTexture(desc,
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001938 GrContext::kApprox_ScratchTexMatch));
1939 if (NULL == layer->getTexture()) {
1940 continue;
1941 }
1942 } else {
1943 bNeedsRendering = false;
1944 }
1945
1946 layerInfo->fBM = SkNEW(SkBitmap);
1947 wrap_texture(layer->getTexture(), desc.fWidth, desc.fHeight, layerInfo->fBM);
1948
1949 SkASSERT(info.fPaint);
1950 layerInfo->fPaint = info.fPaint;
1951
1952 if (bNeedsRendering) {
1953 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
1954 layer->getTexture()->asRenderTarget()));
1955
1956 SkCanvas* canvas = surface->getCanvas();
1957
1958 canvas->setMatrix(info.fCTM);
1959 canvas->clear(SK_ColorTRANSPARENT);
1960
1961 picture->fPlayback->setDrawLimits(info.fSaveLayerOpID, info.fRestoreOpID);
1962 picture->fPlayback->draw(*canvas, NULL);
1963 picture->fPlayback->setDrawLimits(0, 0);
1964 canvas->flush();
1965 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001966 }
1967 }
1968 }
1969
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001970 // Playback using new layers
1971 picture->fPlayback->setReplacements(&replacements);
1972 picture->fPlayback->draw(*canvas, NULL);
1973 picture->fPlayback->setReplacements(NULL);
1974
1975 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1976 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1977
1978 if (NULL != layer->getTexture()) {
1979 fContext->unlockScratchTexture(layer->getTexture());
1980 layer->setTexture(NULL);
1981 }
1982 }
1983
1984 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001985}