blob: 288ea5ae6d13f61e3cffc54f0ea54856b66dbca7 [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"
egdanield58a0ba2014-06-11 10:30:05 -070020#include "GrStrokeInfo.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000021
22#include "SkGrTexturePixelRef.h"
23
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000024#include "SkDeviceImageFilterProxy.h"
25#include "SkDrawProcs.h"
26#include "SkGlyphCache.h"
27#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000028#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000029#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000030#include "SkPicture.h"
robertphillips@google.combeb1af22014-05-07 21:31:09 +000031#include "SkPicturePlayback.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000032#include "SkRRect.h"
33#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000034#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000035#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000036#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000037#include "SkVertState.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000038#include "SkErrorInternals.h"
39
40#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
41
42#if 0
43 extern bool (*gShouldDrawProc)();
44 #define CHECK_SHOULD_DRAW(draw, forceI) \
45 do { \
46 if (gShouldDrawProc && !gShouldDrawProc()) return; \
47 this->prepareDraw(draw, forceI); \
48 } while (0)
49#else
50 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
51#endif
52
53// This constant represents the screen alignment criterion in texels for
54// requiring texture domain clamping to prevent color bleeding when drawing
55// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000056#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000057
58#define DO_DEFERRED_CLEAR() \
59 do { \
60 if (fNeedClear) { \
61 this->clear(SK_ColorTRANSPARENT); \
62 } \
63 } while (false) \
64
65///////////////////////////////////////////////////////////////////////////////
66
67#define CHECK_FOR_ANNOTATION(paint) \
68 do { if (paint.getAnnotation()) { return; } } while (0)
69
70///////////////////////////////////////////////////////////////////////////////
71
72
73class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
74public:
75 SkAutoCachedTexture()
76 : fDevice(NULL)
77 , fTexture(NULL) {
78 }
79
80 SkAutoCachedTexture(SkGpuDevice* device,
81 const SkBitmap& bitmap,
82 const GrTextureParams* params,
83 GrTexture** texture)
84 : fDevice(NULL)
85 , fTexture(NULL) {
86 SkASSERT(NULL != texture);
87 *texture = this->set(device, bitmap, params);
88 }
89
90 ~SkAutoCachedTexture() {
91 if (NULL != fTexture) {
92 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
93 }
94 }
95
96 GrTexture* set(SkGpuDevice* device,
97 const SkBitmap& bitmap,
98 const GrTextureParams* params) {
99 if (NULL != fTexture) {
100 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
101 fTexture = NULL;
102 }
103 fDevice = device;
104 GrTexture* result = (GrTexture*)bitmap.getTexture();
105 if (NULL == result) {
106 // Cannot return the native texture so look it up in our cache
107 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
108 result = fTexture;
109 }
110 return result;
111 }
112
113private:
114 SkGpuDevice* fDevice;
115 GrTexture* fTexture;
116};
117
118///////////////////////////////////////////////////////////////////////////////
119
120struct GrSkDrawProcs : public SkDrawProcs {
121public:
122 GrContext* fContext;
123 GrTextContext* fTextContext;
124 GrFontScaler* fFontScaler; // cached in the skia glyphcache
125};
126
127///////////////////////////////////////////////////////////////////////////////
128
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000129/*
130 * GrRenderTarget does not know its opaqueness, only its config, so we have
131 * to make conservative guesses when we return an "equivalent" bitmap.
132 */
133static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000134 SkBitmap bitmap;
reed6c225732014-06-09 19:52:07 -0700135 bitmap.setInfo(renderTarget->info());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000136 return bitmap;
137}
138
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000139SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000140 SkASSERT(NULL != surface);
141 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
142 return NULL;
143 }
144 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000145 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000146 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000147 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000148 }
149}
150
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000151SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000152 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000153 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000154}
155
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000156SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000157 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000158 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000159}
160
161void SkGpuDevice::initFromRenderTarget(GrContext* context,
162 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000163 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000164 fDrawProcs = NULL;
165
166 fContext = context;
167 fContext->ref();
168
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +0000169 bool useDFFonts = !!(flags & kDFFonts_Flag);
170 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties,
171 useDFFonts));
commit-bot@chromium.org47841822014-03-27 14:19:17 +0000172 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
173
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000174 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000175 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000176
177 SkASSERT(NULL != renderTarget);
178 fRenderTarget = renderTarget;
179 fRenderTarget->ref();
180
181 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
182 // on the RT but not vice-versa.
183 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
184 // busting chrome (for a currently unknown reason).
185 GrSurface* surface = fRenderTarget->asTexture();
186 if (NULL == surface) {
187 surface = fRenderTarget;
188 }
reed@google.combf790232013-12-13 19:45:58 +0000189
reed6c225732014-06-09 19:52:07 -0700190 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef,
191 (surface->info(), surface, SkToBool(flags & kCached_Flag)));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000192
reed@google.com672588b2014-01-08 15:42:01 +0000193 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000194}
195
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000196SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
197 int sampleCount) {
198 if (kUnknown_SkColorType == origInfo.colorType() ||
199 origInfo.width() < 0 || origInfo.height() < 0) {
200 return NULL;
201 }
202
203 SkImageInfo info = origInfo;
204 // TODO: perhas we can loosen this check now that colortype is more detailed
205 // e.g. can we support both RGBA and BGRA here?
206 if (kRGB_565_SkColorType == info.colorType()) {
207 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
208 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000209 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000210 if (kOpaque_SkAlphaType != info.alphaType()) {
211 info.fAlphaType = kPremul_SkAlphaType; // force this setting
212 }
213 }
214
215 GrTextureDesc desc;
216 desc.fFlags = kRenderTarget_GrTextureFlagBit;
217 desc.fWidth = info.width();
218 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000219 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000220 desc.fSampleCnt = sampleCount;
221
222 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
223 if (!texture.get()) {
224 return NULL;
225 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000226
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000227 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
228}
229
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000230SkGpuDevice::~SkGpuDevice() {
231 if (fDrawProcs) {
232 delete fDrawProcs;
233 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000234
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000235 delete fMainTextContext;
236 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000237
238 // The GrContext takes a ref on the target. We don't want to cause the render
239 // target to be unnecessarily kept alive.
240 if (fContext->getRenderTarget() == fRenderTarget) {
241 fContext->setRenderTarget(NULL);
242 }
243
244 if (fContext->getClip() == &fClipData) {
245 fContext->setClip(NULL);
246 }
247
248 SkSafeUnref(fRenderTarget);
249 fContext->unref();
250}
251
252///////////////////////////////////////////////////////////////////////////////
253
254void SkGpuDevice::makeRenderTargetCurrent() {
255 DO_DEFERRED_CLEAR();
256 fContext->setRenderTarget(fRenderTarget);
257}
258
259///////////////////////////////////////////////////////////////////////////////
260
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000261bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
262 int x, int y) {
263 DO_DEFERRED_CLEAR();
264
265 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000266 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000267 if (kUnknown_GrPixelConfig == config) {
268 return false;
269 }
270
271 uint32_t flags = 0;
272 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
273 flags = GrContext::kUnpremul_PixelOpsFlag;
274 }
275 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
276 config, dstPixels, dstRowBytes, flags);
277}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000278
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000279bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
280 int x, int y) {
281 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000282 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000283 if (kUnknown_GrPixelConfig == config) {
284 return false;
285 }
286 uint32_t flags = 0;
287 if (kUnpremul_SkAlphaType == info.alphaType()) {
288 flags = GrContext::kUnpremul_PixelOpsFlag;
289 }
290 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
291
292 // need to bump our genID for compatibility with clients that "know" we have a bitmap
293 this->onAccessBitmap().notifyPixelsChanged();
294
295 return true;
296}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000297
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000298const SkBitmap& SkGpuDevice::onAccessBitmap() {
299 DO_DEFERRED_CLEAR();
300 return INHERITED::onAccessBitmap();
301}
302
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000303void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
304 INHERITED::onAttachToCanvas(canvas);
305
306 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
307 fClipData.fClipStack = canvas->getClipStack();
308}
309
310void SkGpuDevice::onDetachFromCanvas() {
311 INHERITED::onDetachFromCanvas();
312 fClipData.fClipStack = NULL;
313}
314
315// call this every draw call, to ensure that the context reflects our state,
316// and not the state from some other canvas/device
317void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
318 SkASSERT(NULL != fClipData.fClipStack);
319
320 fContext->setRenderTarget(fRenderTarget);
321
322 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
323
324 if (forceIdentity) {
325 fContext->setIdentityMatrix();
326 } else {
327 fContext->setMatrix(*draw.fMatrix);
328 }
329 fClipData.fOrigin = this->getOrigin();
330
331 fContext->setClip(&fClipData);
332
333 DO_DEFERRED_CLEAR();
334}
335
336GrRenderTarget* SkGpuDevice::accessRenderTarget() {
337 DO_DEFERRED_CLEAR();
338 return fRenderTarget;
339}
340
341///////////////////////////////////////////////////////////////////////////////
342
343SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
344SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
345SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
346SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
347SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
348 shader_type_mismatch);
349SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
350 shader_type_mismatch);
351SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
352SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
353
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000354///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000355
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356void SkGpuDevice::clear(SkColor color) {
357 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
358 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
359 fNeedClear = false;
360}
361
362void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
363 CHECK_SHOULD_DRAW(draw, false);
364
365 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000366 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000367
368 fContext->drawPaint(grPaint);
369}
370
371// must be in SkCanvas::PointMode order
372static const GrPrimitiveType gPointMode2PrimtiveType[] = {
373 kPoints_GrPrimitiveType,
374 kLines_GrPrimitiveType,
375 kLineStrip_GrPrimitiveType
376};
377
378void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
379 size_t count, const SkPoint pts[], const SkPaint& paint) {
380 CHECK_FOR_ANNOTATION(paint);
381 CHECK_SHOULD_DRAW(draw, false);
382
383 SkScalar width = paint.getStrokeWidth();
384 if (width < 0) {
385 return;
386 }
387
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000388 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000389 if (GrDashingEffect::DrawDashLine(pts, paint, this->context())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000390 return;
391 }
392 }
393
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000394 // we only handle hairlines and paints without path effects or mask filters,
395 // else we let the SkDraw call our drawPath()
396 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
397 draw.drawPoints(mode, count, pts, paint, true);
398 return;
399 }
400
401 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000402 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000403
404 fContext->drawVertices(grPaint,
405 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000406 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000407 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000408 NULL,
409 NULL,
410 NULL,
411 0);
412}
413
414///////////////////////////////////////////////////////////////////////////////
415
416void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
417 const SkPaint& paint) {
418 CHECK_FOR_ANNOTATION(paint);
419 CHECK_SHOULD_DRAW(draw, false);
420
421 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
422 SkScalar width = paint.getStrokeWidth();
423
424 /*
425 We have special code for hairline strokes, miter-strokes, bevel-stroke
426 and fills. Anything else we just call our path code.
427 */
428 bool usePath = doStroke && width > 0 &&
429 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
430 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
431 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700432
433 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000434 usePath = true;
435 }
egdanield58a0ba2014-06-11 10:30:05 -0700436
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000437 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
438#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
439 if (doStroke) {
440#endif
441 usePath = true;
442#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
443 } else {
444 usePath = !fContext->getMatrix().preservesRightAngles();
445 }
446#endif
447 }
448 // until we can both stroke and fill rectangles
449 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
450 usePath = true;
451 }
452
egdanield58a0ba2014-06-11 10:30:05 -0700453 GrStrokeInfo strokeInfo(paint);
454
455 const SkPathEffect* pe = paint.getPathEffect();
456 if (!usePath && NULL != pe && !strokeInfo.isDashed()) {
457 usePath = true;
458 }
459
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000460 if (usePath) {
461 SkPath path;
462 path.addRect(rect);
463 this->drawPath(draw, path, paint, NULL, true);
464 return;
465 }
466
467 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000468 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
egdanield58a0ba2014-06-11 10:30:05 -0700469
470 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000471}
472
473///////////////////////////////////////////////////////////////////////////////
474
475void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
476 const SkPaint& paint) {
477 CHECK_FOR_ANNOTATION(paint);
478 CHECK_SHOULD_DRAW(draw, false);
479
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000480 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000481 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
egdanield58a0ba2014-06-11 10:30:05 -0700482
483 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000484 if (paint.getMaskFilter()) {
485 // try to hit the fast path for drawing filtered round rects
486
487 SkRRect devRRect;
488 if (rect.transform(fContext->getMatrix(), &devRRect)) {
489 if (devRRect.allCornersCircular()) {
490 SkRect maskRect;
491 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
492 draw.fClip->getBounds(),
493 fContext->getMatrix(),
494 &maskRect)) {
495 SkIRect finalIRect;
496 maskRect.roundOut(&finalIRect);
497 if (draw.fClip->quickReject(finalIRect)) {
498 // clipped out
499 return;
500 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000501 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700502 strokeInfo.getStrokeRec(),
503 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000504 return;
505 }
506 }
507
508 }
509 }
510
511 }
512
egdanield58a0ba2014-06-11 10:30:05 -0700513 bool usePath = false;
514
515 if (paint.getMaskFilter()) {
516 usePath = true;
517 } else {
518 const SkPathEffect* pe = paint.getPathEffect();
519 if (NULL != pe && !strokeInfo.isDashed()) {
520 usePath = true;
521 }
522 }
523
524
525 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000526 SkPath path;
527 path.addRRect(rect);
528 this->drawPath(draw, path, paint, NULL, true);
529 return;
530 }
egdanield58a0ba2014-06-11 10:30:05 -0700531
532 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000533}
534
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000535void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
536 const SkRRect& inner, const SkPaint& paint) {
537 SkStrokeRec stroke(paint);
538 if (stroke.isFillStyle()) {
539
540 CHECK_FOR_ANNOTATION(paint);
541 CHECK_SHOULD_DRAW(draw, false);
542
543 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000544 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000545
546 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
547 fContext->drawDRRect(grPaint, outer, inner);
548 return;
549 }
550 }
551
552 SkPath path;
553 path.addRRect(outer);
554 path.addRRect(inner);
555 path.setFillType(SkPath::kEvenOdd_FillType);
556
557 this->drawPath(draw, path, paint, NULL, true);
558}
559
560
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000561/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000562
563void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
564 const SkPaint& paint) {
565 CHECK_FOR_ANNOTATION(paint);
566 CHECK_SHOULD_DRAW(draw, false);
567
egdanield58a0ba2014-06-11 10:30:05 -0700568 GrStrokeInfo strokeInfo(paint);
569
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000570 bool usePath = false;
571 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700572 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000573 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700574 } else {
575 const SkPathEffect* pe = paint.getPathEffect();
576 if (NULL != pe && !strokeInfo.isDashed()) {
577 usePath = true;
578 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000579 }
580
581 if (usePath) {
582 SkPath path;
583 path.addOval(oval);
584 this->drawPath(draw, path, paint, NULL, true);
585 return;
586 }
587
588 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000589 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000590
egdanield58a0ba2014-06-11 10:30:05 -0700591 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000592}
593
594#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000595
596///////////////////////////////////////////////////////////////////////////////
597
598// helpers for applying mask filters
599namespace {
600
601// Draw a mask using the supplied paint. Since the coverage/geometry
602// is already burnt into the mask this boils down to a rect draw.
603// Return true if the mask was successfully drawn.
604bool draw_mask(GrContext* context, const SkRect& maskRect,
605 GrPaint* grp, GrTexture* mask) {
606 GrContext::AutoMatrix am;
607 if (!am.setIdentity(context, grp)) {
608 return false;
609 }
610
611 SkMatrix matrix;
612 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
613 matrix.postIDiv(mask->width(), mask->height());
614
615 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
616 context->drawRect(*grp, maskRect);
617 return true;
618}
619
620bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700621 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000622 GrPaint* grp, SkPaint::Style style) {
623 SkMask srcM, dstM;
624
625 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
626 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
627 return false;
628 }
629 SkAutoMaskFreeImage autoSrc(srcM.fImage);
630
631 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
632 return false;
633 }
634 // this will free-up dstM when we're done (allocated in filterMask())
635 SkAutoMaskFreeImage autoDst(dstM.fImage);
636
637 if (clip.quickReject(dstM.fBounds)) {
638 return false;
639 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000640
641 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
642 // the current clip (and identity matrix) and GrPaint settings
643 GrTextureDesc desc;
644 desc.fWidth = dstM.fBounds.width();
645 desc.fHeight = dstM.fBounds.height();
646 desc.fConfig = kAlpha_8_GrPixelConfig;
647
648 GrAutoScratchTexture ast(context, desc);
649 GrTexture* texture = ast.texture();
650
651 if (NULL == texture) {
652 return false;
653 }
654 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
655 dstM.fImage, dstM.fRowBytes);
656
657 SkRect maskRect = SkRect::Make(dstM.fBounds);
658
659 return draw_mask(context, maskRect, grp, texture);
660}
661
662// Create a mask of 'devPath' and place the result in 'mask'. Return true on
663// success; false otherwise.
664bool create_mask_GPU(GrContext* context,
665 const SkRect& maskRect,
666 const SkPath& devPath,
egdanield58a0ba2014-06-11 10:30:05 -0700667 const GrStrokeInfo& strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000668 bool doAA,
669 GrAutoScratchTexture* mask) {
670 GrTextureDesc desc;
671 desc.fFlags = kRenderTarget_GrTextureFlagBit;
672 desc.fWidth = SkScalarCeilToInt(maskRect.width());
673 desc.fHeight = SkScalarCeilToInt(maskRect.height());
674 // We actually only need A8, but it often isn't supported as a
675 // render target so default to RGBA_8888
676 desc.fConfig = kRGBA_8888_GrPixelConfig;
677 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
678 desc.fConfig = kAlpha_8_GrPixelConfig;
679 }
680
681 mask->set(context, desc);
682 if (NULL == mask->texture()) {
683 return false;
684 }
685
686 GrTexture* maskTexture = mask->texture();
687 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
688
689 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
690 GrContext::AutoClip ac(context, clipRect);
691
692 context->clear(NULL, 0x0, true);
693
694 GrPaint tempPaint;
695 if (doAA) {
696 tempPaint.setAntiAlias(true);
697 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
698 // blend coeff of zero requires dual source blending support in order
699 // to properly blend partially covered pixels. This means the AA
700 // code path may not be taken. So we use a dst blend coeff of ISA. We
701 // could special case AA draws to a dst surface with known alpha=0 to
702 // use a zero dst coeff when dual source blending isn't available.
703 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
704 }
705
706 GrContext::AutoMatrix am;
707
708 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
709 SkMatrix translate;
710 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
711 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700712 context->drawPath(tempPaint, devPath, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000713 return true;
714}
715
716SkBitmap wrap_texture(GrTexture* texture) {
717 SkBitmap result;
reed6c225732014-06-09 19:52:07 -0700718 result.setInfo(texture->info());
719 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000720 return result;
721}
722
723};
724
725void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
726 const SkPaint& paint, const SkMatrix* prePathMatrix,
727 bool pathIsMutable) {
728 CHECK_FOR_ANNOTATION(paint);
729 CHECK_SHOULD_DRAW(draw, false);
730
731 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000732 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000733
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000734 // If we have a prematrix, apply it to the path, optimizing for the case
735 // where the original path can in fact be modified in place (even though
736 // its parameter type is const).
737 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000738 SkTLazy<SkPath> tmpPath;
739 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000740
741 if (prePathMatrix) {
742 SkPath* result = pathPtr;
743
744 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000745 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000746 pathIsMutable = true;
747 }
748 // should I push prePathMatrix on our MV stack temporarily, instead
749 // of applying it here? See SkDraw.cpp
750 pathPtr->transform(*prePathMatrix, result);
751 pathPtr = result;
752 }
753 // at this point we're done with prePathMatrix
754 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
755
egdanield58a0ba2014-06-11 10:30:05 -0700756 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000757 SkPathEffect* pathEffect = paint.getPathEffect();
758 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700759 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
760 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000761 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000762 pathPtr = effectPath.get();
763 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700764 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000765 }
766
egdanield58a0ba2014-06-11 10:30:05 -0700767 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000768 if (paint.getMaskFilter()) {
769 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000770 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
771 if (stroke.applyToPath(strokedPath, *pathPtr)) {
772 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700774 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000775 }
776 }
777
778 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000779 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000780
781 // transform the path into device space
782 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000783
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000784 SkRect maskRect;
785 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
786 draw.fClip->getBounds(),
787 fContext->getMatrix(),
788 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000789 // The context's matrix may change while creating the mask, so save the CTM here to
790 // pass to filterMaskGPU.
791 const SkMatrix ctm = fContext->getMatrix();
792
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000793 SkIRect finalIRect;
794 maskRect.roundOut(&finalIRect);
795 if (draw.fClip->quickReject(finalIRect)) {
796 // clipped out
797 return;
798 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000799
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000800 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000801 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000802 // the mask filter was able to draw itself directly, so there's nothing
803 // left to do.
804 return;
805 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000806
807 GrAutoScratchTexture mask;
808
egdanield58a0ba2014-06-11 10:30:05 -0700809 if (create_mask_GPU(fContext, maskRect, *devPathPtr, strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000810 grPaint.isAntiAlias(), &mask)) {
811 GrTexture* filtered;
812
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000813 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000814 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000815 // filterMaskGPU gives us ownership of a ref to the result
816 SkAutoTUnref<GrTexture> atu(filtered);
817
818 // If the scratch texture that we used as the filter src also holds the filter
819 // result then we must detach so that this texture isn't recycled for a later
820 // draw.
821 if (filtered == mask.texture()) {
822 mask.detach();
823 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
824 }
825
826 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
827 // This path is completely drawn
828 return;
829 }
830 }
831 }
832 }
833
834 // draw the mask on the CPU - this is a fallthrough path in case the
835 // GPU path fails
836 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
837 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700838 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
839 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000840 return;
841 }
842
egdanield58a0ba2014-06-11 10:30:05 -0700843 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000844}
845
846static const int kBmpSmallTileSize = 1 << 10;
847
848static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
849 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
850 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
851 return tilesX * tilesY;
852}
853
854static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
855 if (maxTileSize <= kBmpSmallTileSize) {
856 return maxTileSize;
857 }
858
859 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
860 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
861
862 maxTileTotalTileSize *= maxTileSize * maxTileSize;
863 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
864
865 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
866 return kBmpSmallTileSize;
867 } else {
868 return maxTileSize;
869 }
870}
871
872// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
873// pixels from the bitmap are necessary.
874static void determine_clipped_src_rect(const GrContext* context,
875 const SkBitmap& bitmap,
876 const SkRect* srcRectPtr,
877 SkIRect* clippedSrcIRect) {
878 const GrClipData* clip = context->getClip();
879 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
880 SkMatrix inv;
881 if (!context->getMatrix().invert(&inv)) {
882 clippedSrcIRect->setEmpty();
883 return;
884 }
885 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
886 inv.mapRect(&clippedSrcRect);
887 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000888 // we've setup src space 0,0 to map to the top left of the src rect.
889 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000890 if (!clippedSrcRect.intersect(*srcRectPtr)) {
891 clippedSrcIRect->setEmpty();
892 return;
893 }
894 }
895 clippedSrcRect.roundOut(clippedSrcIRect);
896 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
897 if (!clippedSrcIRect->intersect(bmpBounds)) {
898 clippedSrcIRect->setEmpty();
899 }
900}
901
902bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
903 const GrTextureParams& params,
904 const SkRect* srcRectPtr,
905 int maxTileSize,
906 int* tileSize,
907 SkIRect* clippedSrcRect) const {
908 // if bitmap is explictly texture backed then just use the texture
909 if (NULL != bitmap.getTexture()) {
910 return false;
911 }
912
913 // if it's larger than the max tile size, then we have no choice but tiling.
914 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
915 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
916 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
917 return true;
918 }
919
920 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
921 return false;
922 }
923
924 // if the entire texture is already in our cache then no reason to tile it
925 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
926 return false;
927 }
928
929 // At this point we know we could do the draw by uploading the entire bitmap
930 // as a texture. However, if the texture would be large compared to the
931 // cache size and we don't require most of it for this draw then tile to
932 // reduce the amount of upload and cache spill.
933
934 // assumption here is that sw bitmap size is a good proxy for its size as
935 // a texture
936 size_t bmpSize = bitmap.getSize();
937 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000938 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000939 if (bmpSize < cacheSize / 2) {
940 return false;
941 }
942
943 // Figure out how much of the src we will need based on the src rect and clipping.
944 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
945 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
946 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
947 kBmpSmallTileSize * kBmpSmallTileSize;
948
949 return usedTileBytes < 2 * bmpSize;
950}
951
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000952void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000953 const SkBitmap& bitmap,
954 const SkMatrix& m,
955 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000956 SkMatrix concat;
957 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
958 if (!m.isIdentity()) {
959 concat.setConcat(*draw->fMatrix, m);
960 draw.writable()->fMatrix = &concat;
961 }
962 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000963}
964
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000965// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000966// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
967// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000968static inline void clamped_outset_with_offset(SkIRect* iRect,
969 int outset,
970 SkPoint* offset,
971 const SkIRect& clamp) {
972 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000973
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000974 int leftClampDelta = clamp.fLeft - iRect->fLeft;
975 if (leftClampDelta > 0) {
976 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000977 iRect->fLeft = clamp.fLeft;
978 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000979 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000980 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000981
982 int topClampDelta = clamp.fTop - iRect->fTop;
983 if (topClampDelta > 0) {
984 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000985 iRect->fTop = clamp.fTop;
986 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000987 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000988 }
989
990 if (iRect->fRight > clamp.fRight) {
991 iRect->fRight = clamp.fRight;
992 }
993 if (iRect->fBottom > clamp.fBottom) {
994 iRect->fBottom = clamp.fBottom;
995 }
996}
997
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000998static bool has_aligned_samples(const SkRect& srcRect,
999 const SkRect& transformedRect) {
1000 // detect pixel disalignment
1001 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1002 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1003 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1004 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1005 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1006 COLOR_BLEED_TOLERANCE &&
1007 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1008 COLOR_BLEED_TOLERANCE) {
1009 return true;
1010 }
1011 return false;
1012}
1013
1014static bool may_color_bleed(const SkRect& srcRect,
1015 const SkRect& transformedRect,
1016 const SkMatrix& m) {
1017 // Only gets called if has_aligned_samples returned false.
1018 // So we can assume that sampling is axis aligned but not texel aligned.
1019 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1020 SkRect innerSrcRect(srcRect), innerTransformedRect,
1021 outerTransformedRect(transformedRect);
1022 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1023 m.mapRect(&innerTransformedRect, innerSrcRect);
1024
1025 // The gap between outerTransformedRect and innerTransformedRect
1026 // represents the projection of the source border area, which is
1027 // problematic for color bleeding. We must check whether any
1028 // destination pixels sample the border area.
1029 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1030 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1031 SkIRect outer, inner;
1032 outerTransformedRect.round(&outer);
1033 innerTransformedRect.round(&inner);
1034 // If the inner and outer rects round to the same result, it means the
1035 // border does not overlap any pixel centers. Yay!
1036 return inner != outer;
1037}
1038
1039static bool needs_texture_domain(const SkBitmap& bitmap,
1040 const SkRect& srcRect,
1041 GrTextureParams &params,
1042 const SkMatrix& contextMatrix,
1043 bool bicubic) {
1044 bool needsTextureDomain = false;
1045
1046 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1047 // Need texture domain if drawing a sub rect
1048 needsTextureDomain = srcRect.width() < bitmap.width() ||
1049 srcRect.height() < bitmap.height();
1050 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1051 // sampling is axis-aligned
1052 SkRect transformedRect;
1053 contextMatrix.mapRect(&transformedRect, srcRect);
1054
1055 if (has_aligned_samples(srcRect, transformedRect)) {
1056 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1057 needsTextureDomain = false;
1058 } else {
1059 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1060 }
1061 }
1062 }
1063 return needsTextureDomain;
1064}
1065
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001066void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1067 const SkBitmap& bitmap,
1068 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001069 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001070 const SkPaint& paint,
1071 SkCanvas::DrawBitmapRectFlags flags) {
1072 CHECK_SHOULD_DRAW(draw, false);
1073
1074 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001075 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001076 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1077 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001078 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001079 SkScalar w = SkIntToScalar(bitmap.width());
1080 SkScalar h = SkIntToScalar(bitmap.height());
1081 dstSize.fWidth = w;
1082 dstSize.fHeight = h;
1083 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001084 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001085 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001086 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001087 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001088 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001089 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1090 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1091 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1092 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001093 }
1094
1095 if (paint.getMaskFilter()){
1096 // Convert the bitmap to a shader so that the rect can be drawn
1097 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001098 SkBitmap tmp; // subset of bitmap, if necessary
1099 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001100 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001101 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001102 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1103 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1104 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001105 // In bleed mode we position and trim the bitmap based on the src rect which is
1106 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1107 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1108 // compensate.
1109 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1110 SkIRect iSrc;
1111 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001112
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001113 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1114 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001115
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001116 if (!bitmap.extractSubset(&tmp, iSrc)) {
1117 return; // extraction failed
1118 }
1119 bitmapPtr = &tmp;
1120 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001121
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001122 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001123 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001124 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001125 } else {
1126 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001127 }
1128
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001129 SkPaint paintWithShader(paint);
1130 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001131 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001132 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1133 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001134
1135 return;
1136 }
1137
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001138 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1139 // the view matrix rather than a local matrix.
1140 SkMatrix m;
1141 m.setScale(dstSize.fWidth / srcRect.width(),
1142 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001143 fContext->concatMatrix(m);
1144
1145 GrTextureParams params;
1146 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1147 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001148
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001149 bool doBicubic = false;
1150
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001151 switch(paintFilterLevel) {
1152 case SkPaint::kNone_FilterLevel:
1153 textureFilterMode = GrTextureParams::kNone_FilterMode;
1154 break;
1155 case SkPaint::kLow_FilterLevel:
1156 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1157 break;
1158 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001159 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001160 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1161 } else {
1162 // Don't trigger MIP level generation unnecessarily.
1163 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1164 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001165 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001166 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001167 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001168 doBicubic =
1169 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001170 break;
1171 default:
1172 SkErrorInternals::SetError( kInvalidPaint_SkError,
1173 "Sorry, I don't understand the filtering "
1174 "mode you asked for. Falling back to "
1175 "MIPMaps.");
1176 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1177 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001178 }
1179
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001180 int tileFilterPad;
1181 if (doBicubic) {
1182 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1183 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1184 tileFilterPad = 0;
1185 } else {
1186 tileFilterPad = 1;
1187 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001188 params.setFilterMode(textureFilterMode);
1189
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001190 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001191 int tileSize;
1192
1193 SkIRect clippedSrcRect;
1194 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1195 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001196 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1197 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001198 } else {
1199 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001200 bool needsTextureDomain = needs_texture_domain(bitmap,
1201 srcRect,
1202 params,
1203 fContext->getMatrix(),
1204 doBicubic);
1205 this->internalDrawBitmap(bitmap,
1206 srcRect,
1207 params,
1208 paint,
1209 flags,
1210 doBicubic,
1211 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001212 }
1213}
1214
1215// Break 'bitmap' into several tiles to draw it since it has already
1216// been determined to be too large to fit in VRAM
1217void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1218 const SkRect& srcRect,
1219 const SkIRect& clippedSrcIRect,
1220 const GrTextureParams& params,
1221 const SkPaint& paint,
1222 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001223 int tileSize,
1224 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001225 // The following pixel lock is technically redundant, but it is desirable
1226 // to lock outside of the tile loop to prevent redecoding the whole image
1227 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1228 // is larger than the limit of the discardable memory pool.
1229 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001230 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1231
1232 int nx = bitmap.width() / tileSize;
1233 int ny = bitmap.height() / tileSize;
1234 for (int x = 0; x <= nx; x++) {
1235 for (int y = 0; y <= ny; y++) {
1236 SkRect tileR;
1237 tileR.set(SkIntToScalar(x * tileSize),
1238 SkIntToScalar(y * tileSize),
1239 SkIntToScalar((x + 1) * tileSize),
1240 SkIntToScalar((y + 1) * tileSize));
1241
1242 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1243 continue;
1244 }
1245
1246 if (!tileR.intersect(srcRect)) {
1247 continue;
1248 }
1249
1250 SkBitmap tmpB;
1251 SkIRect iTileR;
1252 tileR.roundOut(&iTileR);
1253 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1254 SkIntToScalar(iTileR.fTop));
1255
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001256 // Adjust the context matrix to draw at the right x,y in device space
1257 SkMatrix tmpM;
1258 GrContext::AutoMatrix am;
1259 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1260 am.setPreConcat(fContext, tmpM);
1261
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001262 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001263 SkIRect iClampRect;
1264
1265 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1266 // In bleed mode we want to always expand the tile on all edges
1267 // but stay within the bitmap bounds
1268 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1269 } else {
1270 // In texture-domain/clamp mode we only want to expand the
1271 // tile on edges interior to "srcRect" (i.e., we want to
1272 // not bleed across the original clamped edges)
1273 srcRect.roundOut(&iClampRect);
1274 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001275 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1276 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001277 }
1278
1279 if (bitmap.extractSubset(&tmpB, iTileR)) {
1280 // now offset it to make it "local" to our tmp bitmap
1281 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001282 GrTextureParams paramsTemp = params;
1283 bool needsTextureDomain = needs_texture_domain(bitmap,
1284 srcRect,
1285 paramsTemp,
1286 fContext->getMatrix(),
1287 bicubic);
1288 this->internalDrawBitmap(tmpB,
1289 tileR,
1290 paramsTemp,
1291 paint,
1292 flags,
1293 bicubic,
1294 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001295 }
1296 }
1297 }
1298}
1299
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001300
1301/*
1302 * This is called by drawBitmap(), which has to handle images that may be too
1303 * large to be represented by a single texture.
1304 *
1305 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1306 * and that non-texture portion of the GrPaint has already been setup.
1307 */
1308void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1309 const SkRect& srcRect,
1310 const GrTextureParams& params,
1311 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001312 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001313 bool bicubic,
1314 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001315 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1316 bitmap.height() <= fContext->getMaxTextureSize());
1317
1318 GrTexture* texture;
1319 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1320 if (NULL == texture) {
1321 return;
1322 }
1323
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001324 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001325 SkRect paintRect;
1326 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1327 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1328 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1329 SkScalarMul(srcRect.fTop, hInv),
1330 SkScalarMul(srcRect.fRight, wInv),
1331 SkScalarMul(srcRect.fBottom, hInv));
1332
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001333 SkRect textureDomain = SkRect::MakeEmpty();
1334 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001335 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001336 // Use a constrained texture domain to avoid color bleeding
1337 SkScalar left, top, right, bottom;
1338 if (srcRect.width() > SK_Scalar1) {
1339 SkScalar border = SK_ScalarHalf / texture->width();
1340 left = paintRect.left() + border;
1341 right = paintRect.right() - border;
1342 } else {
1343 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1344 }
1345 if (srcRect.height() > SK_Scalar1) {
1346 SkScalar border = SK_ScalarHalf / texture->height();
1347 top = paintRect.top() + border;
1348 bottom = paintRect.bottom() - border;
1349 } else {
1350 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1351 }
1352 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001353 if (bicubic) {
1354 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1355 } else {
1356 effect.reset(GrTextureDomainEffect::Create(texture,
1357 SkMatrix::I(),
1358 textureDomain,
1359 GrTextureDomain::kClamp_Mode,
1360 params.filterMode()));
1361 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001362 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001363 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1364 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1365 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001366 } else {
1367 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1368 }
1369
1370 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1371 // the rest from the SkPaint.
1372 GrPaint grPaint;
1373 grPaint.addColorEffect(effect);
1374 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
dandov9de5b512014-06-10 14:38:28 -07001375 GrColor grColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1376 SkColor2GrColor(paint.getColor());
1377 SkPaint2GrPaintNoShader(this->context(), paint, grColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001378
1379 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1380}
1381
1382static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001383 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001384 int w, int h, const SkImageFilter::Context& ctx,
1385 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001386 SkASSERT(filter);
1387 SkDeviceImageFilterProxy proxy(device);
1388
1389 if (filter->canFilterImageGPU()) {
1390 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1391 // filter. Also set the clip wide open and the matrix to identity.
1392 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001393 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001394 } else {
1395 return false;
1396 }
1397}
1398
1399void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1400 int left, int top, const SkPaint& paint) {
1401 // drawSprite is defined to be in device coords.
1402 CHECK_SHOULD_DRAW(draw, true);
1403
1404 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1405 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1406 return;
1407 }
1408
1409 int w = bitmap.width();
1410 int h = bitmap.height();
1411
1412 GrTexture* texture;
1413 // draw sprite uses the default texture params
1414 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1415
1416 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001417 // This bitmap will own the filtered result as a texture.
1418 SkBitmap filteredBitmap;
1419
1420 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001421 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001422 SkMatrix matrix(*draw.fMatrix);
1423 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001424 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001425 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1426 SkAutoUnref aur(cache);
1427 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001428 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001429 &offset)) {
1430 texture = (GrTexture*) filteredBitmap.getTexture();
1431 w = filteredBitmap.width();
1432 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001433 left += offset.x();
1434 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001435 } else {
1436 return;
1437 }
1438 }
1439
1440 GrPaint grPaint;
1441 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1442
dandov9de5b512014-06-10 14:38:28 -07001443 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1444 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001445
1446 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001447 SkRect::MakeXYWH(SkIntToScalar(left),
1448 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001449 SkIntToScalar(w),
1450 SkIntToScalar(h)),
1451 SkRect::MakeXYWH(0,
1452 0,
1453 SK_Scalar1 * w / texture->width(),
1454 SK_Scalar1 * h / texture->height()));
1455}
1456
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001457void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001458 const SkRect* src, const SkRect& dst,
1459 const SkPaint& paint,
1460 SkCanvas::DrawBitmapRectFlags flags) {
1461 SkMatrix matrix;
1462 SkRect bitmapBounds, tmpSrc;
1463
1464 bitmapBounds.set(0, 0,
1465 SkIntToScalar(bitmap.width()),
1466 SkIntToScalar(bitmap.height()));
1467
1468 // Compute matrix from the two rectangles
1469 if (NULL != src) {
1470 tmpSrc = *src;
1471 } else {
1472 tmpSrc = bitmapBounds;
1473 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001474
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001475 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1476
1477 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1478 if (NULL != src) {
1479 if (!bitmapBounds.contains(tmpSrc)) {
1480 if (!tmpSrc.intersect(bitmapBounds)) {
1481 return; // nothing to draw
1482 }
1483 }
1484 }
1485
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001486 SkRect tmpDst;
1487 matrix.mapRect(&tmpDst, tmpSrc);
1488
1489 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1490 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1491 // Translate so that tempDst's top left is at the origin.
1492 matrix = *origDraw.fMatrix;
1493 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1494 draw.writable()->fMatrix = &matrix;
1495 }
1496 SkSize dstSize;
1497 dstSize.fWidth = tmpDst.width();
1498 dstSize.fHeight = tmpDst.height();
1499
1500 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001501}
1502
1503void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1504 int x, int y, const SkPaint& paint) {
1505 // clear of the source device must occur before CHECK_SHOULD_DRAW
1506 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1507 if (dev->fNeedClear) {
1508 // TODO: could check here whether we really need to draw at all
1509 dev->clear(0x0);
1510 }
1511
1512 // drawDevice is defined to be in device coords.
1513 CHECK_SHOULD_DRAW(draw, true);
1514
1515 GrRenderTarget* devRT = dev->accessRenderTarget();
1516 GrTexture* devTex;
1517 if (NULL == (devTex = devRT->asTexture())) {
1518 return;
1519 }
1520
1521 const SkBitmap& bm = dev->accessBitmap(false);
1522 int w = bm.width();
1523 int h = bm.height();
1524
1525 SkImageFilter* filter = paint.getImageFilter();
1526 // This bitmap will own the filtered result as a texture.
1527 SkBitmap filteredBitmap;
1528
1529 if (NULL != filter) {
1530 SkIPoint offset = SkIPoint::Make(0, 0);
1531 SkMatrix matrix(*draw.fMatrix);
1532 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001533 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001534 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1535 SkAutoUnref aur(cache);
1536 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001537 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001538 &offset)) {
1539 devTex = filteredBitmap.getTexture();
1540 w = filteredBitmap.width();
1541 h = filteredBitmap.height();
1542 x += offset.fX;
1543 y += offset.fY;
1544 } else {
1545 return;
1546 }
1547 }
1548
1549 GrPaint grPaint;
1550 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1551
dandov9de5b512014-06-10 14:38:28 -07001552 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1553 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001554
1555 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1556 SkIntToScalar(y),
1557 SkIntToScalar(w),
1558 SkIntToScalar(h));
1559
1560 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1561 // scratch texture).
1562 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1563 SK_Scalar1 * h / devTex->height());
1564
1565 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1566}
1567
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001568bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001569 return filter->canFilterImageGPU();
1570}
1571
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001572bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001573 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001574 SkBitmap* result, SkIPoint* offset) {
1575 // want explicitly our impl, so guard against a subclass of us overriding it
1576 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1577 return false;
1578 }
1579
1580 SkAutoLockPixels alp(src, !src.getTexture());
1581 if (!src.getTexture() && !src.readyToDraw()) {
1582 return false;
1583 }
1584
1585 GrTexture* texture;
1586 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1587 // must be pushed upstack.
1588 SkAutoCachedTexture act(this, src, NULL, &texture);
1589
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001590 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1591 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001592}
1593
1594///////////////////////////////////////////////////////////////////////////////
1595
1596// must be in SkCanvas::VertexMode order
1597static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1598 kTriangles_GrPrimitiveType,
1599 kTriangleStrip_GrPrimitiveType,
1600 kTriangleFan_GrPrimitiveType,
1601};
1602
1603void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1604 int vertexCount, const SkPoint vertices[],
1605 const SkPoint texs[], const SkColor colors[],
1606 SkXfermode* xmode,
1607 const uint16_t indices[], int indexCount,
1608 const SkPaint& paint) {
1609 CHECK_SHOULD_DRAW(draw, false);
1610
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001611 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1612 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
1613 texs = NULL;
1614 SkPaint copy(paint);
1615 copy.setStyle(SkPaint::kStroke_Style);
1616 copy.setStrokeWidth(0);
1617
1618 VertState state(vertexCount, indices, indexCount);
1619 VertState::Proc vertProc = state.chooseProc(vmode);
1620
1621 SkPoint* pts = new SkPoint[vertexCount * 6];
1622 int i = 0;
1623 while (vertProc(&state)) {
1624 pts[i] = vertices[state.f0];
1625 pts[i + 1] = vertices[state.f1];
1626 pts[i + 2] = vertices[state.f1];
1627 pts[i + 3] = vertices[state.f2];
1628 pts[i + 4] = vertices[state.f2];
1629 pts[i + 5] = vertices[state.f0];
1630 i += 6;
1631 }
1632 draw.drawPoints(SkCanvas::kLines_PointMode, i, pts, copy, true);
1633 return;
1634 }
1635
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001636 GrPaint grPaint;
1637 // we ignore the shader if texs is null.
1638 if (NULL == texs) {
dandov9de5b512014-06-10 14:38:28 -07001639 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1640 NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001641 } else {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001642 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001643 }
1644
mtklein2583b622014-06-04 08:20:41 -07001645#if 0
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001646 if (NULL != xmode && NULL != texs && NULL != colors) {
1647 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1648 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001649 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001650 }
1651 }
mtklein2583b622014-06-04 08:20:41 -07001652#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001653
1654 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1655 if (NULL != colors) {
1656 // need to convert byte order and from non-PM to PM
1657 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001658 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001659 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001660 color = colors[i];
1661 if (paint.getAlpha() != 255) {
1662 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1663 }
1664 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001665 }
1666 colors = convertedColors.get();
1667 }
1668 fContext->drawVertices(grPaint,
1669 gVertexMode2PrimitiveType[vmode],
1670 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001671 vertices,
1672 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001673 colors,
1674 indices,
1675 indexCount);
1676}
1677
1678///////////////////////////////////////////////////////////////////////////////
1679
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001680void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1681 size_t byteLength, SkScalar x, SkScalar y,
1682 const SkPaint& paint) {
1683 CHECK_SHOULD_DRAW(draw, false);
1684
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001685 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001686 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001687 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001688
1689 SkDEBUGCODE(this->validate();)
1690
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001691 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1692 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001693 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001694 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001695
1696 SkDEBUGCODE(this->validate();)
1697
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001698 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001699 } else {
1700 // this guy will just call our drawPath()
1701 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001702 }
1703}
1704
1705void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1706 size_t byteLength, const SkScalar pos[],
1707 SkScalar constY, int scalarsPerPos,
1708 const SkPaint& paint) {
1709 CHECK_SHOULD_DRAW(draw, false);
1710
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001711 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001712 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001713 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001714
1715 SkDEBUGCODE(this->validate();)
1716
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001717 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001718 constY, scalarsPerPos);
1719 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001720 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001721 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001722
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001723 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001724
1725 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001726 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001727 } else {
1728 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1729 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001730 }
1731}
1732
1733void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1734 size_t len, const SkPath& path,
1735 const SkMatrix* m, const SkPaint& paint) {
1736 CHECK_SHOULD_DRAW(draw, false);
1737
1738 SkASSERT(draw.fDevice == this);
1739 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1740}
1741
1742///////////////////////////////////////////////////////////////////////////////
1743
1744bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1745 if (!paint.isLCDRenderText()) {
1746 // we're cool with the paint as is
1747 return false;
1748 }
1749
1750 if (paint.getShader() ||
1751 paint.getXfermode() || // unless its srcover
1752 paint.getMaskFilter() ||
1753 paint.getRasterizer() ||
1754 paint.getColorFilter() ||
1755 paint.getPathEffect() ||
1756 paint.isFakeBoldText() ||
1757 paint.getStyle() != SkPaint::kFill_Style) {
1758 // turn off lcd
1759 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1760 flags->fHinting = paint.getHinting();
1761 return true;
1762 }
1763 // we're cool with the paint as is
1764 return false;
1765}
1766
1767void SkGpuDevice::flush() {
1768 DO_DEFERRED_CLEAR();
1769 fContext->resolveRenderTarget(fRenderTarget);
1770}
1771
1772///////////////////////////////////////////////////////////////////////////////
1773
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001774SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001775 GrTextureDesc desc;
1776 desc.fConfig = fRenderTarget->config();
1777 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001778 desc.fWidth = info.width();
1779 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001780 desc.fSampleCnt = fRenderTarget->numSamples();
1781
1782 SkAutoTUnref<GrTexture> texture;
1783 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001784 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001785
1786#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1787 // layers are never draw in repeat modes, so we can request an approx
1788 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001789 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001790 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1791 GrContext::kApprox_ScratchTexMatch :
1792 GrContext::kExact_ScratchTexMatch;
1793 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1794#else
1795 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1796#endif
1797 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001798 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001799 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001800 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1801 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001802 return NULL;
1803 }
1804}
1805
reed@google.com76f10a32014-02-05 15:32:21 +00001806SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1807 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1808}
1809
robertphillips9b14f262014-06-04 05:40:44 -07001810void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001811 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001812
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001813 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1814 if (NULL != existing) {
1815 return;
1816 }
1817
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001818 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001819
1820 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001821
1822 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001823}
1824
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001825static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1826 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001827 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001828 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1829}
1830
robertphillips9b14f262014-06-04 05:40:44 -07001831void SkGpuDevice::EXPERIMENTAL_purge(const SkPicture* picture) {
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001832
1833}
1834
robertphillips9b14f262014-06-04 05:40:44 -07001835bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001836
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001837 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001838
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001839 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001840 if (NULL == data) {
1841 return false;
1842 }
1843
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001844 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001845
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001846 if (0 == gpuData->numSaveLayers()) {
1847 return false;
1848 }
1849
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001850 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1851 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1852 pullForward[i] = false;
1853 }
1854
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001855 SkRect clipBounds;
1856 if (!canvas->getClipBounds(&clipBounds)) {
1857 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001858 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001859 SkIRect query;
1860 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001861
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001862 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001863
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001864 // This code pre-renders the entire layer since it will be cached and potentially
1865 // reused with different clips (e.g., in different tiles). Because of this the
1866 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1867 // is used to limit which clips are pre-rendered.
1868 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001869
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001870 if (ops.valid()) {
1871 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001872 // the BBH to limit the pre-rendering to just the layers needed to cover
1873 // the region being drawn
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001874 for (int i = 0; i < ops.numOps(); ++i) {
1875 uint32_t offset = ops.offset(i);
1876
1877 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001878 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001879 // their parent in the list.
1880 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1881 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1882
1883 if (pullForward[j]) {
1884 continue; // already pulling forward
1885 }
1886
1887 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1888 continue; // the op isn't in this range
1889 }
1890
1891 // TODO: once this code is more stable unsuitable layers can
1892 // just be omitted during the optimization stage
1893 if (!info.fValid ||
1894 kSaveLayerMaxSize < info.fSize.fWidth ||
1895 kSaveLayerMaxSize < info.fSize.fHeight ||
1896 info.fIsNested) {
1897 continue; // this layer is unsuitable
1898 }
1899
1900 pullForward[j] = true;
1901 }
1902 }
1903 } else {
1904 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001905 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001906 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1907 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1908
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001909 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1910 info.fOffset.fY,
1911 info.fSize.fWidth,
1912 info.fSize.fHeight);
1913
1914 if (!SkIRect::Intersects(query, layerRect)) {
1915 continue;
1916 }
1917
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001918 // TODO: once this code is more stable unsuitable layers can
1919 // just be omitted during the optimization stage
1920 if (!info.fValid ||
1921 kSaveLayerMaxSize < info.fSize.fWidth ||
1922 kSaveLayerMaxSize < info.fSize.fHeight ||
1923 info.fIsNested) {
1924 continue;
1925 }
1926
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001927 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001928 }
1929 }
1930
1931 SkPicturePlayback::PlaybackReplacements replacements;
1932
1933 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1934 if (pullForward[i]) {
1935 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1936
1937 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1938
1939 if (NULL != picture->fPlayback) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001940 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001941 replacements.push();
1942 layerInfo->fStart = info.fSaveLayerOpID;
1943 layerInfo->fStop = info.fRestoreOpID;
1944 layerInfo->fPos = info.fOffset;
1945
1946 GrTextureDesc desc;
1947 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1948 desc.fWidth = info.fSize.fWidth;
1949 desc.fHeight = info.fSize.fHeight;
1950 desc.fConfig = kSkia8888_GrPixelConfig;
1951 // TODO: need to deal with sample count
1952
1953 bool bNeedsRendering = true;
1954
1955 // This just uses scratch textures and doesn't cache the texture.
1956 // This can yield a lot of re-rendering
1957 if (NULL == layer->getTexture()) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001958 layer->setTexture(fContext->lockAndRefScratchTexture(desc,
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001959 GrContext::kApprox_ScratchTexMatch));
1960 if (NULL == layer->getTexture()) {
1961 continue;
1962 }
1963 } else {
1964 bNeedsRendering = false;
1965 }
1966
1967 layerInfo->fBM = SkNEW(SkBitmap);
1968 wrap_texture(layer->getTexture(), desc.fWidth, desc.fHeight, layerInfo->fBM);
1969
1970 SkASSERT(info.fPaint);
1971 layerInfo->fPaint = info.fPaint;
1972
1973 if (bNeedsRendering) {
1974 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
1975 layer->getTexture()->asRenderTarget()));
1976
1977 SkCanvas* canvas = surface->getCanvas();
1978
1979 canvas->setMatrix(info.fCTM);
1980 canvas->clear(SK_ColorTRANSPARENT);
1981
1982 picture->fPlayback->setDrawLimits(info.fSaveLayerOpID, info.fRestoreOpID);
1983 picture->fPlayback->draw(*canvas, NULL);
1984 picture->fPlayback->setDrawLimits(0, 0);
1985 canvas->flush();
1986 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001987 }
1988 }
1989 }
1990
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001991 // Playback using new layers
1992 picture->fPlayback->setReplacements(&replacements);
1993 picture->fPlayback->draw(*canvas, NULL);
1994 picture->fPlayback->setReplacements(NULL);
1995
1996 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1997 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1998
1999 if (NULL != layer->getTexture()) {
2000 fContext->unlockScratchTexture(layer->getTexture());
2001 layer->setTexture(NULL);
2002 }
2003 }
2004
2005 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002006}