blob: 42acc9b81cc905785c7c8f48c21d5bdde36266ae [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"
egdanielbbcb38d2014-06-19 10:19:29 -070021#include "GrTracing.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000022
23#include "SkGrTexturePixelRef.h"
24
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025#include "SkDeviceImageFilterProxy.h"
26#include "SkDrawProcs.h"
27#include "SkGlyphCache.h"
28#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000029#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000030#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000031#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070032#include "SkPictureData.h"
robertphillipsce4dd3d2014-07-07 13:46:35 -070033#include "SkPicturePlayback.h"
robertphillips1ad00e42014-07-08 08:28:08 -070034#include "SkPictureRangePlayback.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkRRect.h"
36#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000037#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000038#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000039#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000040#include "SkVertState.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000041#include "SkErrorInternals.h"
42
43#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
44
45#if 0
46 extern bool (*gShouldDrawProc)();
47 #define CHECK_SHOULD_DRAW(draw, forceI) \
48 do { \
49 if (gShouldDrawProc && !gShouldDrawProc()) return; \
50 this->prepareDraw(draw, forceI); \
51 } while (0)
52#else
53 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
54#endif
55
56// This constant represents the screen alignment criterion in texels for
57// requiring texture domain clamping to prevent color bleeding when drawing
58// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000059#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000060
61#define DO_DEFERRED_CLEAR() \
62 do { \
63 if (fNeedClear) { \
64 this->clear(SK_ColorTRANSPARENT); \
65 } \
66 } while (false) \
67
68///////////////////////////////////////////////////////////////////////////////
69
70#define CHECK_FOR_ANNOTATION(paint) \
71 do { if (paint.getAnnotation()) { return; } } while (0)
72
73///////////////////////////////////////////////////////////////////////////////
74
75
76class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
77public:
78 SkAutoCachedTexture()
79 : fDevice(NULL)
80 , fTexture(NULL) {
81 }
82
83 SkAutoCachedTexture(SkGpuDevice* device,
84 const SkBitmap& bitmap,
85 const GrTextureParams* params,
86 GrTexture** texture)
87 : fDevice(NULL)
88 , fTexture(NULL) {
89 SkASSERT(NULL != texture);
90 *texture = this->set(device, bitmap, params);
91 }
92
93 ~SkAutoCachedTexture() {
94 if (NULL != fTexture) {
95 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
96 }
97 }
98
99 GrTexture* set(SkGpuDevice* device,
100 const SkBitmap& bitmap,
101 const GrTextureParams* params) {
102 if (NULL != fTexture) {
103 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
104 fTexture = NULL;
105 }
106 fDevice = device;
107 GrTexture* result = (GrTexture*)bitmap.getTexture();
108 if (NULL == result) {
109 // Cannot return the native texture so look it up in our cache
110 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
111 result = fTexture;
112 }
113 return result;
114 }
115
116private:
117 SkGpuDevice* fDevice;
118 GrTexture* fTexture;
119};
120
121///////////////////////////////////////////////////////////////////////////////
122
123struct GrSkDrawProcs : public SkDrawProcs {
124public:
125 GrContext* fContext;
126 GrTextContext* fTextContext;
127 GrFontScaler* fFontScaler; // cached in the skia glyphcache
128};
129
130///////////////////////////////////////////////////////////////////////////////
131
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000132SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000133 SkASSERT(NULL != surface);
134 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
135 return NULL;
136 }
137 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000138 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000139 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000140 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000141 }
142}
143
reed89443ab2014-06-27 11:34:19 -0700144SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000145 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000146}
147
reed89443ab2014-06-27 11:34:19 -0700148SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000149 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000150}
151
152void SkGpuDevice::initFromRenderTarget(GrContext* context,
153 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000154 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000155 fDrawProcs = NULL;
156
157 fContext = context;
158 fContext->ref();
159
160 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000161 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000162
163 SkASSERT(NULL != renderTarget);
164 fRenderTarget = renderTarget;
165 fRenderTarget->ref();
166
167 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
168 // on the RT but not vice-versa.
169 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
170 // busting chrome (for a currently unknown reason).
171 GrSurface* surface = fRenderTarget->asTexture();
172 if (NULL == surface) {
173 surface = fRenderTarget;
174 }
reed@google.combf790232013-12-13 19:45:58 +0000175
reed6c225732014-06-09 19:52:07 -0700176 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef,
177 (surface->info(), surface, SkToBool(flags & kCached_Flag)));
reed89443ab2014-06-27 11:34:19 -0700178 fLegacyBitmap.setInfo(surface->info());
179 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700180
181 bool useDFFonts = !!(flags & kDFFonts_Flag);
182 fMainTextContext = fContext->createTextContext(fRenderTarget, fLeakyProperties, useDFFonts);
183 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000184}
185
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000186SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
187 int sampleCount) {
188 if (kUnknown_SkColorType == origInfo.colorType() ||
189 origInfo.width() < 0 || origInfo.height() < 0) {
190 return NULL;
191 }
192
193 SkImageInfo info = origInfo;
194 // TODO: perhas we can loosen this check now that colortype is more detailed
195 // e.g. can we support both RGBA and BGRA here?
196 if (kRGB_565_SkColorType == info.colorType()) {
197 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
198 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000199 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000200 if (kOpaque_SkAlphaType != info.alphaType()) {
201 info.fAlphaType = kPremul_SkAlphaType; // force this setting
202 }
203 }
204
205 GrTextureDesc desc;
206 desc.fFlags = kRenderTarget_GrTextureFlagBit;
207 desc.fWidth = info.width();
208 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000209 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000210 desc.fSampleCnt = sampleCount;
211
212 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
213 if (!texture.get()) {
214 return NULL;
215 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000216
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000217 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
218}
219
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000220SkGpuDevice::~SkGpuDevice() {
221 if (fDrawProcs) {
222 delete fDrawProcs;
223 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000224
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000225 delete fMainTextContext;
226 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000227
228 // The GrContext takes a ref on the target. We don't want to cause the render
229 // target to be unnecessarily kept alive.
230 if (fContext->getRenderTarget() == fRenderTarget) {
231 fContext->setRenderTarget(NULL);
232 }
233
234 if (fContext->getClip() == &fClipData) {
235 fContext->setClip(NULL);
236 }
237
238 SkSafeUnref(fRenderTarget);
239 fContext->unref();
240}
241
242///////////////////////////////////////////////////////////////////////////////
243
244void SkGpuDevice::makeRenderTargetCurrent() {
245 DO_DEFERRED_CLEAR();
246 fContext->setRenderTarget(fRenderTarget);
247}
248
249///////////////////////////////////////////////////////////////////////////////
250
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000251bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
252 int x, int y) {
253 DO_DEFERRED_CLEAR();
254
255 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000256 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000257 if (kUnknown_GrPixelConfig == config) {
258 return false;
259 }
260
261 uint32_t flags = 0;
262 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
263 flags = GrContext::kUnpremul_PixelOpsFlag;
264 }
265 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
266 config, dstPixels, dstRowBytes, flags);
267}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000268
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000269bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
270 int x, int y) {
271 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000272 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000273 if (kUnknown_GrPixelConfig == config) {
274 return false;
275 }
276 uint32_t flags = 0;
277 if (kUnpremul_SkAlphaType == info.alphaType()) {
278 flags = GrContext::kUnpremul_PixelOpsFlag;
279 }
280 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
281
282 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700283 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000284
285 return true;
286}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000287
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000288const SkBitmap& SkGpuDevice::onAccessBitmap() {
289 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700290 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000291}
292
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000293void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
294 INHERITED::onAttachToCanvas(canvas);
295
296 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
297 fClipData.fClipStack = canvas->getClipStack();
298}
299
300void SkGpuDevice::onDetachFromCanvas() {
301 INHERITED::onDetachFromCanvas();
302 fClipData.fClipStack = NULL;
303}
304
305// call this every draw call, to ensure that the context reflects our state,
306// and not the state from some other canvas/device
307void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
308 SkASSERT(NULL != fClipData.fClipStack);
309
310 fContext->setRenderTarget(fRenderTarget);
311
312 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
313
314 if (forceIdentity) {
315 fContext->setIdentityMatrix();
316 } else {
317 fContext->setMatrix(*draw.fMatrix);
318 }
319 fClipData.fOrigin = this->getOrigin();
320
321 fContext->setClip(&fClipData);
322
323 DO_DEFERRED_CLEAR();
324}
325
326GrRenderTarget* SkGpuDevice::accessRenderTarget() {
327 DO_DEFERRED_CLEAR();
328 return fRenderTarget;
329}
330
331///////////////////////////////////////////////////////////////////////////////
332
333SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
334SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
335SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
336SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
337SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
338 shader_type_mismatch);
339SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
340 shader_type_mismatch);
341SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
342SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
343
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000344///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000345
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000346void SkGpuDevice::clear(SkColor color) {
347 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
348 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
349 fNeedClear = false;
350}
351
352void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
353 CHECK_SHOULD_DRAW(draw, false);
354
355 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000356 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000357
358 fContext->drawPaint(grPaint);
359}
360
361// must be in SkCanvas::PointMode order
362static const GrPrimitiveType gPointMode2PrimtiveType[] = {
363 kPoints_GrPrimitiveType,
364 kLines_GrPrimitiveType,
365 kLineStrip_GrPrimitiveType
366};
367
368void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
369 size_t count, const SkPoint pts[], const SkPaint& paint) {
370 CHECK_FOR_ANNOTATION(paint);
371 CHECK_SHOULD_DRAW(draw, false);
372
373 SkScalar width = paint.getStrokeWidth();
374 if (width < 0) {
375 return;
376 }
377
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000378 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700379 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
380 GrPaint grPaint;
381 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
382 SkPath path;
383 path.moveTo(pts[0]);
384 path.lineTo(pts[1]);
385 fContext->drawPath(grPaint, path, strokeInfo);
386 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000387 }
388
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000389 // we only handle hairlines and paints without path effects or mask filters,
390 // else we let the SkDraw call our drawPath()
391 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
392 draw.drawPoints(mode, count, pts, paint, true);
393 return;
394 }
395
396 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000397 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000398
399 fContext->drawVertices(grPaint,
400 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000401 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000402 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000403 NULL,
404 NULL,
405 NULL,
406 0);
407}
408
409///////////////////////////////////////////////////////////////////////////////
410
411void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
412 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700413 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
414
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000415 CHECK_FOR_ANNOTATION(paint);
416 CHECK_SHOULD_DRAW(draw, false);
417
418 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
419 SkScalar width = paint.getStrokeWidth();
420
421 /*
422 We have special code for hairline strokes, miter-strokes, bevel-stroke
423 and fills. Anything else we just call our path code.
424 */
425 bool usePath = doStroke && width > 0 &&
426 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
427 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
428 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700429
430 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000431 usePath = true;
432 }
egdanield58a0ba2014-06-11 10:30:05 -0700433
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000434 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
435#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
436 if (doStroke) {
437#endif
438 usePath = true;
439#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
440 } else {
441 usePath = !fContext->getMatrix().preservesRightAngles();
442 }
443#endif
444 }
445 // until we can both stroke and fill rectangles
446 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
447 usePath = true;
448 }
449
egdanield58a0ba2014-06-11 10:30:05 -0700450 GrStrokeInfo strokeInfo(paint);
451
452 const SkPathEffect* pe = paint.getPathEffect();
453 if (!usePath && NULL != pe && !strokeInfo.isDashed()) {
454 usePath = true;
455 }
456
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000457 if (usePath) {
458 SkPath path;
459 path.addRect(rect);
460 this->drawPath(draw, path, paint, NULL, true);
461 return;
462 }
463
464 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000465 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400466
egdanield58a0ba2014-06-11 10:30:05 -0700467 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000468}
469
470///////////////////////////////////////////////////////////////////////////////
471
472void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
473 const SkPaint& paint) {
474 CHECK_FOR_ANNOTATION(paint);
475 CHECK_SHOULD_DRAW(draw, false);
476
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000477 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000478 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400479
egdanield58a0ba2014-06-11 10:30:05 -0700480 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000481 if (paint.getMaskFilter()) {
482 // try to hit the fast path for drawing filtered round rects
483
484 SkRRect devRRect;
485 if (rect.transform(fContext->getMatrix(), &devRRect)) {
486 if (devRRect.allCornersCircular()) {
487 SkRect maskRect;
488 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
489 draw.fClip->getBounds(),
490 fContext->getMatrix(),
491 &maskRect)) {
492 SkIRect finalIRect;
493 maskRect.roundOut(&finalIRect);
494 if (draw.fClip->quickReject(finalIRect)) {
495 // clipped out
496 return;
497 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000498 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700499 strokeInfo.getStrokeRec(),
500 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000501 return;
502 }
503 }
504
505 }
506 }
507
508 }
509
egdanield58a0ba2014-06-11 10:30:05 -0700510 bool usePath = false;
511
512 if (paint.getMaskFilter()) {
513 usePath = true;
514 } else {
515 const SkPathEffect* pe = paint.getPathEffect();
516 if (NULL != pe && !strokeInfo.isDashed()) {
517 usePath = true;
518 }
519 }
520
521
522 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000523 SkPath path;
524 path.addRRect(rect);
525 this->drawPath(draw, path, paint, NULL, true);
526 return;
527 }
Mike Klein744fb732014-06-23 15:13:26 -0400528
egdanield58a0ba2014-06-11 10:30:05 -0700529 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000530}
531
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000532void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
533 const SkRRect& inner, const SkPaint& paint) {
534 SkStrokeRec stroke(paint);
535 if (stroke.isFillStyle()) {
536
537 CHECK_FOR_ANNOTATION(paint);
538 CHECK_SHOULD_DRAW(draw, false);
539
540 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000541 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000542
543 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
544 fContext->drawDRRect(grPaint, outer, inner);
545 return;
546 }
547 }
548
549 SkPath path;
550 path.addRRect(outer);
551 path.addRRect(inner);
552 path.setFillType(SkPath::kEvenOdd_FillType);
553
554 this->drawPath(draw, path, paint, NULL, true);
555}
556
557
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000558/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000559
560void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
561 const SkPaint& paint) {
562 CHECK_FOR_ANNOTATION(paint);
563 CHECK_SHOULD_DRAW(draw, false);
564
egdanield58a0ba2014-06-11 10:30:05 -0700565 GrStrokeInfo strokeInfo(paint);
566
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000567 bool usePath = false;
568 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700569 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000570 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700571 } else {
572 const SkPathEffect* pe = paint.getPathEffect();
573 if (NULL != pe && !strokeInfo.isDashed()) {
574 usePath = true;
575 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000576 }
577
578 if (usePath) {
579 SkPath path;
580 path.addOval(oval);
581 this->drawPath(draw, path, paint, NULL, true);
582 return;
583 }
584
585 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000586 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000587
egdanield58a0ba2014-06-11 10:30:05 -0700588 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000589}
590
591#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000592
593///////////////////////////////////////////////////////////////////////////////
594
595// helpers for applying mask filters
596namespace {
597
598// Draw a mask using the supplied paint. Since the coverage/geometry
599// is already burnt into the mask this boils down to a rect draw.
600// Return true if the mask was successfully drawn.
601bool draw_mask(GrContext* context, const SkRect& maskRect,
602 GrPaint* grp, GrTexture* mask) {
603 GrContext::AutoMatrix am;
604 if (!am.setIdentity(context, grp)) {
605 return false;
606 }
607
608 SkMatrix matrix;
609 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
610 matrix.postIDiv(mask->width(), mask->height());
611
612 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
613 context->drawRect(*grp, maskRect);
614 return true;
615}
616
617bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700618 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000619 GrPaint* grp, SkPaint::Style style) {
620 SkMask srcM, dstM;
621
622 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
623 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
624 return false;
625 }
626 SkAutoMaskFreeImage autoSrc(srcM.fImage);
627
628 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
629 return false;
630 }
631 // this will free-up dstM when we're done (allocated in filterMask())
632 SkAutoMaskFreeImage autoDst(dstM.fImage);
633
634 if (clip.quickReject(dstM.fBounds)) {
635 return false;
636 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000637
638 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
639 // the current clip (and identity matrix) and GrPaint settings
640 GrTextureDesc desc;
641 desc.fWidth = dstM.fBounds.width();
642 desc.fHeight = dstM.fBounds.height();
643 desc.fConfig = kAlpha_8_GrPixelConfig;
644
645 GrAutoScratchTexture ast(context, desc);
646 GrTexture* texture = ast.texture();
647
648 if (NULL == texture) {
649 return false;
650 }
651 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
652 dstM.fImage, dstM.fRowBytes);
653
654 SkRect maskRect = SkRect::Make(dstM.fBounds);
655
656 return draw_mask(context, maskRect, grp, texture);
657}
658
659// Create a mask of 'devPath' and place the result in 'mask'. Return true on
660// success; false otherwise.
661bool create_mask_GPU(GrContext* context,
662 const SkRect& maskRect,
663 const SkPath& devPath,
egdanield58a0ba2014-06-11 10:30:05 -0700664 const GrStrokeInfo& strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000665 bool doAA,
666 GrAutoScratchTexture* mask) {
667 GrTextureDesc desc;
668 desc.fFlags = kRenderTarget_GrTextureFlagBit;
669 desc.fWidth = SkScalarCeilToInt(maskRect.width());
670 desc.fHeight = SkScalarCeilToInt(maskRect.height());
671 // We actually only need A8, but it often isn't supported as a
672 // render target so default to RGBA_8888
673 desc.fConfig = kRGBA_8888_GrPixelConfig;
674 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
675 desc.fConfig = kAlpha_8_GrPixelConfig;
676 }
677
678 mask->set(context, desc);
679 if (NULL == mask->texture()) {
680 return false;
681 }
682
683 GrTexture* maskTexture = mask->texture();
684 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
685
686 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
687 GrContext::AutoClip ac(context, clipRect);
688
689 context->clear(NULL, 0x0, true);
690
691 GrPaint tempPaint;
692 if (doAA) {
693 tempPaint.setAntiAlias(true);
694 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
695 // blend coeff of zero requires dual source blending support in order
696 // to properly blend partially covered pixels. This means the AA
697 // code path may not be taken. So we use a dst blend coeff of ISA. We
698 // could special case AA draws to a dst surface with known alpha=0 to
699 // use a zero dst coeff when dual source blending isn't available.
700 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
701 }
702
703 GrContext::AutoMatrix am;
704
705 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
706 SkMatrix translate;
707 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
708 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700709 context->drawPath(tempPaint, devPath, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000710 return true;
711}
712
713SkBitmap wrap_texture(GrTexture* texture) {
714 SkBitmap result;
reed6c225732014-06-09 19:52:07 -0700715 result.setInfo(texture->info());
716 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000717 return result;
718}
719
720};
721
722void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
723 const SkPaint& paint, const SkMatrix* prePathMatrix,
724 bool pathIsMutable) {
725 CHECK_FOR_ANNOTATION(paint);
726 CHECK_SHOULD_DRAW(draw, false);
727
728 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000729 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000730
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000731 // If we have a prematrix, apply it to the path, optimizing for the case
732 // where the original path can in fact be modified in place (even though
733 // its parameter type is const).
734 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000735 SkTLazy<SkPath> tmpPath;
736 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000737
738 if (prePathMatrix) {
739 SkPath* result = pathPtr;
740
741 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000742 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000743 pathIsMutable = true;
744 }
745 // should I push prePathMatrix on our MV stack temporarily, instead
746 // of applying it here? See SkDraw.cpp
747 pathPtr->transform(*prePathMatrix, result);
748 pathPtr = result;
749 }
750 // at this point we're done with prePathMatrix
751 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
752
egdanield58a0ba2014-06-11 10:30:05 -0700753 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000754 SkPathEffect* pathEffect = paint.getPathEffect();
755 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700756 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
757 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000758 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000759 pathPtr = effectPath.get();
760 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700761 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000762 }
763
egdanield58a0ba2014-06-11 10:30:05 -0700764 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000765 if (paint.getMaskFilter()) {
766 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000767 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
768 if (stroke.applyToPath(strokedPath, *pathPtr)) {
769 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000770 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700771 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000772 }
773 }
774
775 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000776 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000777
778 // transform the path into device space
779 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000780
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000781 SkRect maskRect;
782 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
783 draw.fClip->getBounds(),
784 fContext->getMatrix(),
785 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000786 // The context's matrix may change while creating the mask, so save the CTM here to
787 // pass to filterMaskGPU.
788 const SkMatrix ctm = fContext->getMatrix();
789
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000790 SkIRect finalIRect;
791 maskRect.roundOut(&finalIRect);
792 if (draw.fClip->quickReject(finalIRect)) {
793 // clipped out
794 return;
795 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000796
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000797 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000798 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000799 // the mask filter was able to draw itself directly, so there's nothing
800 // left to do.
801 return;
802 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000803
804 GrAutoScratchTexture mask;
805
egdanield58a0ba2014-06-11 10:30:05 -0700806 if (create_mask_GPU(fContext, maskRect, *devPathPtr, strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000807 grPaint.isAntiAlias(), &mask)) {
808 GrTexture* filtered;
809
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000810 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000811 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000812 // filterMaskGPU gives us ownership of a ref to the result
813 SkAutoTUnref<GrTexture> atu(filtered);
814
815 // If the scratch texture that we used as the filter src also holds the filter
816 // result then we must detach so that this texture isn't recycled for a later
817 // draw.
818 if (filtered == mask.texture()) {
819 mask.detach();
820 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
821 }
822
823 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
824 // This path is completely drawn
825 return;
826 }
827 }
828 }
829 }
830
831 // draw the mask on the CPU - this is a fallthrough path in case the
832 // GPU path fails
833 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
834 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700835 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
836 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000837 return;
838 }
839
egdanield58a0ba2014-06-11 10:30:05 -0700840 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000841}
842
843static const int kBmpSmallTileSize = 1 << 10;
844
845static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
846 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
847 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
848 return tilesX * tilesY;
849}
850
851static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
852 if (maxTileSize <= kBmpSmallTileSize) {
853 return maxTileSize;
854 }
855
856 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
857 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
858
859 maxTileTotalTileSize *= maxTileSize * maxTileSize;
860 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
861
862 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
863 return kBmpSmallTileSize;
864 } else {
865 return maxTileSize;
866 }
867}
868
869// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
870// pixels from the bitmap are necessary.
871static void determine_clipped_src_rect(const GrContext* context,
872 const SkBitmap& bitmap,
873 const SkRect* srcRectPtr,
874 SkIRect* clippedSrcIRect) {
875 const GrClipData* clip = context->getClip();
876 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
877 SkMatrix inv;
878 if (!context->getMatrix().invert(&inv)) {
879 clippedSrcIRect->setEmpty();
880 return;
881 }
882 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
883 inv.mapRect(&clippedSrcRect);
884 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000885 // we've setup src space 0,0 to map to the top left of the src rect.
886 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000887 if (!clippedSrcRect.intersect(*srcRectPtr)) {
888 clippedSrcIRect->setEmpty();
889 return;
890 }
891 }
892 clippedSrcRect.roundOut(clippedSrcIRect);
893 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
894 if (!clippedSrcIRect->intersect(bmpBounds)) {
895 clippedSrcIRect->setEmpty();
896 }
897}
898
899bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
900 const GrTextureParams& params,
901 const SkRect* srcRectPtr,
902 int maxTileSize,
903 int* tileSize,
904 SkIRect* clippedSrcRect) const {
905 // if bitmap is explictly texture backed then just use the texture
906 if (NULL != bitmap.getTexture()) {
907 return false;
908 }
909
910 // if it's larger than the max tile size, then we have no choice but tiling.
911 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
912 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
913 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
914 return true;
915 }
916
917 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
918 return false;
919 }
920
921 // if the entire texture is already in our cache then no reason to tile it
922 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
923 return false;
924 }
925
926 // At this point we know we could do the draw by uploading the entire bitmap
927 // as a texture. However, if the texture would be large compared to the
928 // cache size and we don't require most of it for this draw then tile to
929 // reduce the amount of upload and cache spill.
930
931 // assumption here is that sw bitmap size is a good proxy for its size as
932 // a texture
933 size_t bmpSize = bitmap.getSize();
934 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000935 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000936 if (bmpSize < cacheSize / 2) {
937 return false;
938 }
939
940 // Figure out how much of the src we will need based on the src rect and clipping.
941 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
942 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
943 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
944 kBmpSmallTileSize * kBmpSmallTileSize;
945
946 return usedTileBytes < 2 * bmpSize;
947}
948
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000949void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000950 const SkBitmap& bitmap,
951 const SkMatrix& m,
952 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000953 SkMatrix concat;
954 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
955 if (!m.isIdentity()) {
956 concat.setConcat(*draw->fMatrix, m);
957 draw.writable()->fMatrix = &concat;
958 }
959 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000960}
961
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000962// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000963// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
964// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000965static inline void clamped_outset_with_offset(SkIRect* iRect,
966 int outset,
967 SkPoint* offset,
968 const SkIRect& clamp) {
969 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000970
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000971 int leftClampDelta = clamp.fLeft - iRect->fLeft;
972 if (leftClampDelta > 0) {
973 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000974 iRect->fLeft = clamp.fLeft;
975 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000976 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000977 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000978
979 int topClampDelta = clamp.fTop - iRect->fTop;
980 if (topClampDelta > 0) {
981 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982 iRect->fTop = clamp.fTop;
983 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000984 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000985 }
986
987 if (iRect->fRight > clamp.fRight) {
988 iRect->fRight = clamp.fRight;
989 }
990 if (iRect->fBottom > clamp.fBottom) {
991 iRect->fBottom = clamp.fBottom;
992 }
993}
994
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000995static bool has_aligned_samples(const SkRect& srcRect,
996 const SkRect& transformedRect) {
997 // detect pixel disalignment
998 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
999 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1000 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1001 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1002 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1003 COLOR_BLEED_TOLERANCE &&
1004 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1005 COLOR_BLEED_TOLERANCE) {
1006 return true;
1007 }
1008 return false;
1009}
1010
1011static bool may_color_bleed(const SkRect& srcRect,
1012 const SkRect& transformedRect,
1013 const SkMatrix& m) {
1014 // Only gets called if has_aligned_samples returned false.
1015 // So we can assume that sampling is axis aligned but not texel aligned.
1016 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1017 SkRect innerSrcRect(srcRect), innerTransformedRect,
1018 outerTransformedRect(transformedRect);
1019 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1020 m.mapRect(&innerTransformedRect, innerSrcRect);
1021
1022 // The gap between outerTransformedRect and innerTransformedRect
1023 // represents the projection of the source border area, which is
1024 // problematic for color bleeding. We must check whether any
1025 // destination pixels sample the border area.
1026 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1027 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1028 SkIRect outer, inner;
1029 outerTransformedRect.round(&outer);
1030 innerTransformedRect.round(&inner);
1031 // If the inner and outer rects round to the same result, it means the
1032 // border does not overlap any pixel centers. Yay!
1033 return inner != outer;
1034}
1035
1036static bool needs_texture_domain(const SkBitmap& bitmap,
1037 const SkRect& srcRect,
1038 GrTextureParams &params,
1039 const SkMatrix& contextMatrix,
1040 bool bicubic) {
1041 bool needsTextureDomain = false;
1042
1043 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1044 // Need texture domain if drawing a sub rect
1045 needsTextureDomain = srcRect.width() < bitmap.width() ||
1046 srcRect.height() < bitmap.height();
1047 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1048 // sampling is axis-aligned
1049 SkRect transformedRect;
1050 contextMatrix.mapRect(&transformedRect, srcRect);
1051
1052 if (has_aligned_samples(srcRect, transformedRect)) {
1053 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1054 needsTextureDomain = false;
1055 } else {
1056 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1057 }
1058 }
1059 }
1060 return needsTextureDomain;
1061}
1062
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001063void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1064 const SkBitmap& bitmap,
1065 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001066 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001067 const SkPaint& paint,
1068 SkCanvas::DrawBitmapRectFlags flags) {
1069 CHECK_SHOULD_DRAW(draw, false);
1070
1071 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001072 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001073 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1074 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001075 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001076 SkScalar w = SkIntToScalar(bitmap.width());
1077 SkScalar h = SkIntToScalar(bitmap.height());
1078 dstSize.fWidth = w;
1079 dstSize.fHeight = h;
1080 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001081 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001082 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001083 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001084 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001085 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001086 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1087 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1088 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1089 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001090 }
1091
1092 if (paint.getMaskFilter()){
1093 // Convert the bitmap to a shader so that the rect can be drawn
1094 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001095 SkBitmap tmp; // subset of bitmap, if necessary
1096 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001097 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001098 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001099 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1100 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1101 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001102 // In bleed mode we position and trim the bitmap based on the src rect which is
1103 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1104 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1105 // compensate.
1106 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1107 SkIRect iSrc;
1108 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001109
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001110 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1111 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001112
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001113 if (!bitmap.extractSubset(&tmp, iSrc)) {
1114 return; // extraction failed
1115 }
1116 bitmapPtr = &tmp;
1117 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001118
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001119 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001120 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001121 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001122 } else {
1123 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001124 }
1125
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001126 SkPaint paintWithShader(paint);
1127 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001128 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001129 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1130 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001131
1132 return;
1133 }
1134
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001135 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1136 // the view matrix rather than a local matrix.
1137 SkMatrix m;
1138 m.setScale(dstSize.fWidth / srcRect.width(),
1139 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001140 fContext->concatMatrix(m);
1141
1142 GrTextureParams params;
1143 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1144 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001145
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001146 bool doBicubic = false;
1147
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001148 switch(paintFilterLevel) {
1149 case SkPaint::kNone_FilterLevel:
1150 textureFilterMode = GrTextureParams::kNone_FilterMode;
1151 break;
1152 case SkPaint::kLow_FilterLevel:
1153 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1154 break;
1155 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001156 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001157 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1158 } else {
1159 // Don't trigger MIP level generation unnecessarily.
1160 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1161 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001162 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001163 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001164 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001165 doBicubic =
1166 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001167 break;
1168 default:
1169 SkErrorInternals::SetError( kInvalidPaint_SkError,
1170 "Sorry, I don't understand the filtering "
1171 "mode you asked for. Falling back to "
1172 "MIPMaps.");
1173 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1174 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001175 }
1176
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001177 int tileFilterPad;
1178 if (doBicubic) {
1179 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1180 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1181 tileFilterPad = 0;
1182 } else {
1183 tileFilterPad = 1;
1184 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001185 params.setFilterMode(textureFilterMode);
1186
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001187 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001188 int tileSize;
1189
1190 SkIRect clippedSrcRect;
1191 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1192 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001193 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1194 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195 } else {
1196 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001197 bool needsTextureDomain = needs_texture_domain(bitmap,
1198 srcRect,
1199 params,
1200 fContext->getMatrix(),
1201 doBicubic);
1202 this->internalDrawBitmap(bitmap,
1203 srcRect,
1204 params,
1205 paint,
1206 flags,
1207 doBicubic,
1208 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001209 }
1210}
1211
1212// Break 'bitmap' into several tiles to draw it since it has already
1213// been determined to be too large to fit in VRAM
1214void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1215 const SkRect& srcRect,
1216 const SkIRect& clippedSrcIRect,
1217 const GrTextureParams& params,
1218 const SkPaint& paint,
1219 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001220 int tileSize,
1221 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001222 // The following pixel lock is technically redundant, but it is desirable
1223 // to lock outside of the tile loop to prevent redecoding the whole image
1224 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1225 // is larger than the limit of the discardable memory pool.
1226 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001227 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1228
1229 int nx = bitmap.width() / tileSize;
1230 int ny = bitmap.height() / tileSize;
1231 for (int x = 0; x <= nx; x++) {
1232 for (int y = 0; y <= ny; y++) {
1233 SkRect tileR;
1234 tileR.set(SkIntToScalar(x * tileSize),
1235 SkIntToScalar(y * tileSize),
1236 SkIntToScalar((x + 1) * tileSize),
1237 SkIntToScalar((y + 1) * tileSize));
1238
1239 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1240 continue;
1241 }
1242
1243 if (!tileR.intersect(srcRect)) {
1244 continue;
1245 }
1246
1247 SkBitmap tmpB;
1248 SkIRect iTileR;
1249 tileR.roundOut(&iTileR);
1250 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1251 SkIntToScalar(iTileR.fTop));
1252
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001253 // Adjust the context matrix to draw at the right x,y in device space
1254 SkMatrix tmpM;
1255 GrContext::AutoMatrix am;
1256 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1257 am.setPreConcat(fContext, tmpM);
1258
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001259 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001260 SkIRect iClampRect;
1261
1262 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1263 // In bleed mode we want to always expand the tile on all edges
1264 // but stay within the bitmap bounds
1265 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1266 } else {
1267 // In texture-domain/clamp mode we only want to expand the
1268 // tile on edges interior to "srcRect" (i.e., we want to
1269 // not bleed across the original clamped edges)
1270 srcRect.roundOut(&iClampRect);
1271 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001272 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1273 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001274 }
1275
1276 if (bitmap.extractSubset(&tmpB, iTileR)) {
1277 // now offset it to make it "local" to our tmp bitmap
1278 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001279 GrTextureParams paramsTemp = params;
1280 bool needsTextureDomain = needs_texture_domain(bitmap,
1281 srcRect,
1282 paramsTemp,
1283 fContext->getMatrix(),
1284 bicubic);
1285 this->internalDrawBitmap(tmpB,
1286 tileR,
1287 paramsTemp,
1288 paint,
1289 flags,
1290 bicubic,
1291 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001292 }
1293 }
1294 }
1295}
1296
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001297
1298/*
1299 * This is called by drawBitmap(), which has to handle images that may be too
1300 * large to be represented by a single texture.
1301 *
1302 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1303 * and that non-texture portion of the GrPaint has already been setup.
1304 */
1305void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1306 const SkRect& srcRect,
1307 const GrTextureParams& params,
1308 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001309 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001310 bool bicubic,
1311 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001312 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1313 bitmap.height() <= fContext->getMaxTextureSize());
1314
1315 GrTexture* texture;
1316 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1317 if (NULL == texture) {
1318 return;
1319 }
1320
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001321 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001322 SkRect paintRect;
1323 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1324 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1325 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1326 SkScalarMul(srcRect.fTop, hInv),
1327 SkScalarMul(srcRect.fRight, wInv),
1328 SkScalarMul(srcRect.fBottom, hInv));
1329
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001330 SkRect textureDomain = SkRect::MakeEmpty();
1331 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001332 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001333 // Use a constrained texture domain to avoid color bleeding
1334 SkScalar left, top, right, bottom;
1335 if (srcRect.width() > SK_Scalar1) {
1336 SkScalar border = SK_ScalarHalf / texture->width();
1337 left = paintRect.left() + border;
1338 right = paintRect.right() - border;
1339 } else {
1340 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1341 }
1342 if (srcRect.height() > SK_Scalar1) {
1343 SkScalar border = SK_ScalarHalf / texture->height();
1344 top = paintRect.top() + border;
1345 bottom = paintRect.bottom() - border;
1346 } else {
1347 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1348 }
1349 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001350 if (bicubic) {
1351 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1352 } else {
1353 effect.reset(GrTextureDomainEffect::Create(texture,
1354 SkMatrix::I(),
1355 textureDomain,
1356 GrTextureDomain::kClamp_Mode,
1357 params.filterMode()));
1358 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001359 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001360 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1361 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1362 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001363 } else {
1364 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1365 }
1366
1367 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1368 // the rest from the SkPaint.
1369 GrPaint grPaint;
1370 grPaint.addColorEffect(effect);
reed0689d7b2014-06-14 05:30:20 -07001371 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
dandov9de5b512014-06-10 14:38:28 -07001372 GrColor grColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1373 SkColor2GrColor(paint.getColor());
1374 SkPaint2GrPaintNoShader(this->context(), paint, grColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001375
1376 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1377}
1378
1379static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001380 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001381 int w, int h, const SkImageFilter::Context& ctx,
1382 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001383 SkASSERT(filter);
1384 SkDeviceImageFilterProxy proxy(device);
1385
1386 if (filter->canFilterImageGPU()) {
1387 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1388 // filter. Also set the clip wide open and the matrix to identity.
1389 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001390 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001391 } else {
1392 return false;
1393 }
1394}
1395
1396void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1397 int left, int top, const SkPaint& paint) {
1398 // drawSprite is defined to be in device coords.
1399 CHECK_SHOULD_DRAW(draw, true);
1400
1401 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1402 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1403 return;
1404 }
1405
1406 int w = bitmap.width();
1407 int h = bitmap.height();
1408
1409 GrTexture* texture;
1410 // draw sprite uses the default texture params
1411 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1412
1413 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001414 // This bitmap will own the filtered result as a texture.
1415 SkBitmap filteredBitmap;
1416
1417 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001418 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001419 SkMatrix matrix(*draw.fMatrix);
1420 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001421 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001422 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1423 SkAutoUnref aur(cache);
1424 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001425 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001426 &offset)) {
1427 texture = (GrTexture*) filteredBitmap.getTexture();
1428 w = filteredBitmap.width();
1429 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001430 left += offset.x();
1431 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001432 } else {
1433 return;
1434 }
1435 }
1436
1437 GrPaint grPaint;
1438 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1439
dandov9de5b512014-06-10 14:38:28 -07001440 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1441 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001442
1443 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001444 SkRect::MakeXYWH(SkIntToScalar(left),
1445 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001446 SkIntToScalar(w),
1447 SkIntToScalar(h)),
1448 SkRect::MakeXYWH(0,
1449 0,
1450 SK_Scalar1 * w / texture->width(),
1451 SK_Scalar1 * h / texture->height()));
1452}
1453
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001454void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001455 const SkRect* src, const SkRect& dst,
1456 const SkPaint& paint,
1457 SkCanvas::DrawBitmapRectFlags flags) {
1458 SkMatrix matrix;
1459 SkRect bitmapBounds, tmpSrc;
1460
1461 bitmapBounds.set(0, 0,
1462 SkIntToScalar(bitmap.width()),
1463 SkIntToScalar(bitmap.height()));
1464
1465 // Compute matrix from the two rectangles
1466 if (NULL != src) {
1467 tmpSrc = *src;
1468 } else {
1469 tmpSrc = bitmapBounds;
1470 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001471
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001472 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1473
1474 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1475 if (NULL != src) {
1476 if (!bitmapBounds.contains(tmpSrc)) {
1477 if (!tmpSrc.intersect(bitmapBounds)) {
1478 return; // nothing to draw
1479 }
1480 }
1481 }
1482
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001483 SkRect tmpDst;
1484 matrix.mapRect(&tmpDst, tmpSrc);
1485
1486 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1487 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1488 // Translate so that tempDst's top left is at the origin.
1489 matrix = *origDraw.fMatrix;
1490 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1491 draw.writable()->fMatrix = &matrix;
1492 }
1493 SkSize dstSize;
1494 dstSize.fWidth = tmpDst.width();
1495 dstSize.fHeight = tmpDst.height();
1496
1497 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001498}
1499
1500void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1501 int x, int y, const SkPaint& paint) {
1502 // clear of the source device must occur before CHECK_SHOULD_DRAW
1503 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1504 if (dev->fNeedClear) {
1505 // TODO: could check here whether we really need to draw at all
1506 dev->clear(0x0);
1507 }
1508
1509 // drawDevice is defined to be in device coords.
1510 CHECK_SHOULD_DRAW(draw, true);
1511
1512 GrRenderTarget* devRT = dev->accessRenderTarget();
1513 GrTexture* devTex;
1514 if (NULL == (devTex = devRT->asTexture())) {
1515 return;
1516 }
1517
1518 const SkBitmap& bm = dev->accessBitmap(false);
1519 int w = bm.width();
1520 int h = bm.height();
1521
1522 SkImageFilter* filter = paint.getImageFilter();
1523 // This bitmap will own the filtered result as a texture.
1524 SkBitmap filteredBitmap;
1525
1526 if (NULL != filter) {
1527 SkIPoint offset = SkIPoint::Make(0, 0);
1528 SkMatrix matrix(*draw.fMatrix);
1529 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001530 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001531 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1532 SkAutoUnref aur(cache);
1533 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001534 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001535 &offset)) {
1536 devTex = filteredBitmap.getTexture();
1537 w = filteredBitmap.width();
1538 h = filteredBitmap.height();
1539 x += offset.fX;
1540 y += offset.fY;
1541 } else {
1542 return;
1543 }
1544 }
1545
1546 GrPaint grPaint;
1547 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1548
dandov9de5b512014-06-10 14:38:28 -07001549 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1550 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001551
1552 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1553 SkIntToScalar(y),
1554 SkIntToScalar(w),
1555 SkIntToScalar(h));
1556
1557 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1558 // scratch texture).
1559 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1560 SK_Scalar1 * h / devTex->height());
1561
1562 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1563}
1564
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001565bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001566 return filter->canFilterImageGPU();
1567}
1568
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001569bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001570 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001571 SkBitmap* result, SkIPoint* offset) {
1572 // want explicitly our impl, so guard against a subclass of us overriding it
1573 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1574 return false;
1575 }
1576
1577 SkAutoLockPixels alp(src, !src.getTexture());
1578 if (!src.getTexture() && !src.readyToDraw()) {
1579 return false;
1580 }
1581
1582 GrTexture* texture;
1583 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1584 // must be pushed upstack.
1585 SkAutoCachedTexture act(this, src, NULL, &texture);
1586
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001587 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1588 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001589}
1590
1591///////////////////////////////////////////////////////////////////////////////
1592
1593// must be in SkCanvas::VertexMode order
1594static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1595 kTriangles_GrPrimitiveType,
1596 kTriangleStrip_GrPrimitiveType,
1597 kTriangleFan_GrPrimitiveType,
1598};
1599
1600void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1601 int vertexCount, const SkPoint vertices[],
1602 const SkPoint texs[], const SkColor colors[],
1603 SkXfermode* xmode,
1604 const uint16_t indices[], int indexCount,
1605 const SkPaint& paint) {
1606 CHECK_SHOULD_DRAW(draw, false);
1607
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001608 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1609 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
1610 texs = NULL;
1611 SkPaint copy(paint);
1612 copy.setStyle(SkPaint::kStroke_Style);
1613 copy.setStrokeWidth(0);
1614
1615 VertState state(vertexCount, indices, indexCount);
1616 VertState::Proc vertProc = state.chooseProc(vmode);
1617
1618 SkPoint* pts = new SkPoint[vertexCount * 6];
1619 int i = 0;
1620 while (vertProc(&state)) {
1621 pts[i] = vertices[state.f0];
1622 pts[i + 1] = vertices[state.f1];
1623 pts[i + 2] = vertices[state.f1];
1624 pts[i + 3] = vertices[state.f2];
1625 pts[i + 4] = vertices[state.f2];
1626 pts[i + 5] = vertices[state.f0];
1627 i += 6;
1628 }
1629 draw.drawPoints(SkCanvas::kLines_PointMode, i, pts, copy, true);
1630 return;
1631 }
1632
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001633 GrPaint grPaint;
1634 // we ignore the shader if texs is null.
1635 if (NULL == texs) {
dandov9de5b512014-06-10 14:38:28 -07001636 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1637 NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001638 } else {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001639 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001640 }
1641
mtklein2583b622014-06-04 08:20:41 -07001642#if 0
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001643 if (NULL != xmode && NULL != texs && NULL != colors) {
1644 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1645 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001646 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001647 }
1648 }
mtklein2583b622014-06-04 08:20:41 -07001649#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001650
1651 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1652 if (NULL != colors) {
1653 // need to convert byte order and from non-PM to PM
1654 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001655 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001656 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001657 color = colors[i];
1658 if (paint.getAlpha() != 255) {
1659 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1660 }
1661 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001662 }
1663 colors = convertedColors.get();
1664 }
1665 fContext->drawVertices(grPaint,
1666 gVertexMode2PrimitiveType[vmode],
1667 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001668 vertices,
1669 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001670 colors,
1671 indices,
1672 indexCount);
1673}
1674
1675///////////////////////////////////////////////////////////////////////////////
1676
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001677void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1678 size_t byteLength, SkScalar x, SkScalar y,
1679 const SkPaint& paint) {
1680 CHECK_SHOULD_DRAW(draw, false);
1681
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001682 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001683 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001684 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001685
1686 SkDEBUGCODE(this->validate();)
1687
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001688 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1689 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001690 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001691 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001692
1693 SkDEBUGCODE(this->validate();)
1694
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001695 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001696 } else {
1697 // this guy will just call our drawPath()
1698 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001699 }
1700}
1701
1702void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1703 size_t byteLength, const SkScalar pos[],
1704 SkScalar constY, int scalarsPerPos,
1705 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001706 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001707 CHECK_SHOULD_DRAW(draw, false);
1708
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001709 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001710 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001711 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001712
1713 SkDEBUGCODE(this->validate();)
1714
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001715 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001716 constY, scalarsPerPos);
1717 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001718 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001719 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001720
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001721 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001722
1723 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001724 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001725 } else {
1726 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1727 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001728 }
1729}
1730
1731void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1732 size_t len, const SkPath& path,
1733 const SkMatrix* m, const SkPaint& paint) {
1734 CHECK_SHOULD_DRAW(draw, false);
1735
1736 SkASSERT(draw.fDevice == this);
1737 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1738}
1739
1740///////////////////////////////////////////////////////////////////////////////
1741
1742bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1743 if (!paint.isLCDRenderText()) {
1744 // we're cool with the paint as is
1745 return false;
1746 }
1747
1748 if (paint.getShader() ||
1749 paint.getXfermode() || // unless its srcover
1750 paint.getMaskFilter() ||
1751 paint.getRasterizer() ||
1752 paint.getColorFilter() ||
1753 paint.getPathEffect() ||
1754 paint.isFakeBoldText() ||
1755 paint.getStyle() != SkPaint::kFill_Style) {
1756 // turn off lcd
1757 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1758 flags->fHinting = paint.getHinting();
1759 return true;
1760 }
1761 // we're cool with the paint as is
1762 return false;
1763}
1764
1765void SkGpuDevice::flush() {
1766 DO_DEFERRED_CLEAR();
1767 fContext->resolveRenderTarget(fRenderTarget);
1768}
1769
1770///////////////////////////////////////////////////////////////////////////////
1771
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001772SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001773 GrTextureDesc desc;
1774 desc.fConfig = fRenderTarget->config();
1775 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001776 desc.fWidth = info.width();
1777 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001778 desc.fSampleCnt = fRenderTarget->numSamples();
1779
1780 SkAutoTUnref<GrTexture> texture;
1781 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001782 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001783
1784#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1785 // layers are never draw in repeat modes, so we can request an approx
1786 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001787 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001788 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1789 GrContext::kApprox_ScratchTexMatch :
1790 GrContext::kExact_ScratchTexMatch;
1791 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1792#else
1793 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1794#endif
1795 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001796 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001797 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001798 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1799 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001800 return NULL;
1801 }
1802}
1803
reed@google.com76f10a32014-02-05 15:32:21 +00001804SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1805 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1806}
1807
robertphillips9b14f262014-06-04 05:40:44 -07001808void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001809 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001810
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001811 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1812 if (NULL != existing) {
1813 return;
1814 }
1815
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001816 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001817
1818 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001819
1820 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001821}
1822
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001823static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1824 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001825 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001826 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1827}
1828
robertphillips9b14f262014-06-04 05:40:44 -07001829void SkGpuDevice::EXPERIMENTAL_purge(const SkPicture* picture) {
robertphillips952841b2014-06-30 08:26:50 -07001830 fContext->getLayerCache()->purge(picture);
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001831}
1832
robertphillips9b14f262014-06-04 05:40:44 -07001833bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001834
robertphillipsdb539902014-07-01 08:47:04 -07001835 if (NULL == picture->fData.get()) {
robertphillips4ec84da2014-06-24 13:10:43 -07001836 return false;
1837 }
1838
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001839 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001840
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001841 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001842 if (NULL == data) {
1843 return false;
1844 }
1845
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001846 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001847
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001848 if (0 == gpuData->numSaveLayers()) {
1849 return false;
1850 }
1851
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001852 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1853 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1854 pullForward[i] = false;
1855 }
1856
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001857 SkRect clipBounds;
1858 if (!canvas->getClipBounds(&clipBounds)) {
1859 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001860 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001861 SkIRect query;
1862 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001863
robertphillipsce4dd3d2014-07-07 13:46:35 -07001864 SkAutoTDelete<const SkPicture::OperationList> ops(picture->EXPERIMENTAL_getActiveOps(query));
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001865
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001866 // This code pre-renders the entire layer since it will be cached and potentially
1867 // reused with different clips (e.g., in different tiles). Because of this the
1868 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1869 // is used to limit which clips are pre-rendered.
1870 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001871
robertphillipsce4dd3d2014-07-07 13:46:35 -07001872 if (NULL != ops.get()) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001873 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001874 // the BBH to limit the pre-rendering to just the layers needed to cover
1875 // the region being drawn
robertphillipsce4dd3d2014-07-07 13:46:35 -07001876 for (int i = 0; i < ops->numOps(); ++i) {
1877 uint32_t offset = ops->offset(i);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001878
1879 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001880 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001881 // their parent in the list.
1882 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1883 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1884
1885 if (pullForward[j]) {
1886 continue; // already pulling forward
1887 }
1888
1889 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1890 continue; // the op isn't in this range
1891 }
1892
1893 // TODO: once this code is more stable unsuitable layers can
1894 // just be omitted during the optimization stage
1895 if (!info.fValid ||
1896 kSaveLayerMaxSize < info.fSize.fWidth ||
1897 kSaveLayerMaxSize < info.fSize.fHeight ||
1898 info.fIsNested) {
1899 continue; // this layer is unsuitable
1900 }
1901
1902 pullForward[j] = true;
1903 }
1904 }
1905 } else {
1906 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001907 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001908 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1909 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1910
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001911 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1912 info.fOffset.fY,
1913 info.fSize.fWidth,
1914 info.fSize.fHeight);
1915
1916 if (!SkIRect::Intersects(query, layerRect)) {
1917 continue;
1918 }
1919
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001920 // TODO: once this code is more stable unsuitable layers can
1921 // just be omitted during the optimization stage
1922 if (!info.fValid ||
1923 kSaveLayerMaxSize < info.fSize.fWidth ||
1924 kSaveLayerMaxSize < info.fSize.fHeight ||
1925 info.fIsNested) {
1926 continue;
1927 }
1928
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001929 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001930 }
1931 }
1932
robertphillipsce4dd3d2014-07-07 13:46:35 -07001933 SkPicturePlayback::PlaybackReplacements replacements;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001934
robertphillips4ec84da2014-06-24 13:10:43 -07001935 // Generate the layer and/or ensure it is locked
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001936 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1937 if (pullForward[i]) {
1938 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1939
1940 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1941
robertphillipsce4dd3d2014-07-07 13:46:35 -07001942 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001943 replacements.push();
robertphillips4ec84da2014-06-24 13:10:43 -07001944 layerInfo->fStart = info.fSaveLayerOpID;
1945 layerInfo->fStop = info.fRestoreOpID;
1946 layerInfo->fPos = info.fOffset;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001947
robertphillips4ec84da2014-06-24 13:10:43 -07001948 GrTextureDesc desc;
1949 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1950 desc.fWidth = info.fSize.fWidth;
1951 desc.fHeight = info.fSize.fHeight;
1952 desc.fConfig = kSkia8888_GrPixelConfig;
1953 // TODO: need to deal with sample count
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001954
robertphillips4ec84da2014-06-24 13:10:43 -07001955 bool needsRendering = !fContext->getLayerCache()->lock(layer, desc);
robertphillips952841b2014-06-30 08:26:50 -07001956 if (NULL == layer->texture()) {
robertphillips4ec84da2014-06-24 13:10:43 -07001957 continue;
1958 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001959
robertphillips4ec84da2014-06-24 13:10:43 -07001960 layerInfo->fBM = SkNEW(SkBitmap); // fBM is allocated so ReplacementInfo can be POD
robertphillips952841b2014-06-30 08:26:50 -07001961 wrap_texture(layer->texture(),
1962 layer->rect().isEmpty() ? desc.fWidth : layer->texture()->width(),
1963 layer->rect().isEmpty() ? desc.fHeight : layer->texture()->height(),
1964 layerInfo->fBM);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001965
robertphillips4ec84da2014-06-24 13:10:43 -07001966 SkASSERT(info.fPaint);
1967 layerInfo->fPaint = info.fPaint;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001968
robertphillips952841b2014-06-30 08:26:50 -07001969 if (layer->rect().isEmpty()) {
1970 layerInfo->fSrcRect = SkIRect::MakeWH(desc.fWidth, desc.fHeight);
1971 } else {
1972 layerInfo->fSrcRect = SkIRect::MakeXYWH(layer->rect().fLeft,
1973 layer->rect().fTop,
1974 layer->rect().width(),
1975 layer->rect().height());
1976 }
1977
robertphillips4ec84da2014-06-24 13:10:43 -07001978 if (needsRendering) {
1979 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
robertphillips952841b2014-06-30 08:26:50 -07001980 layer->texture()->asRenderTarget(),
1981 SkSurface::kStandard_TextRenderMode,
1982 SkSurface::kDontClear_RenderTargetFlag));
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001983
robertphillips4ec84da2014-06-24 13:10:43 -07001984 SkCanvas* canvas = surface->getCanvas();
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001985
robertphillips952841b2014-06-30 08:26:50 -07001986 if (!layer->rect().isEmpty()) {
1987 // Add a rect clip to make sure the rendering doesn't
1988 // extend beyond the boundaries of the atlased sub-rect
1989 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft),
1990 SkIntToScalar(layer->rect().fTop),
1991 SkIntToScalar(layer->rect().width()),
1992 SkIntToScalar(layer->rect().height()));
1993 canvas->clipRect(bound);
1994 // Since 'clear' doesn't respect the clip we need to draw a rect
1995 // TODO: ensure none of the atlased layers contain a clear call!
1996 SkPaint paint;
1997 paint.setColor(SK_ColorTRANSPARENT);
1998 canvas->drawRect(bound, paint);
1999 } else {
2000 canvas->clear(SK_ColorTRANSPARENT);
2001 }
2002
robertphillips4ec84da2014-06-24 13:10:43 -07002003 canvas->setMatrix(info.fCTM);
robertphillips952841b2014-06-30 08:26:50 -07002004
2005 if (!layer->rect().isEmpty()) {
2006 // info.fCTM maps the layer's top/left to the origin.
2007 // Since this layer is atlased the top/left corner needs
2008 // to be offset to some arbitrary location in the backing
2009 // texture.
2010 canvas->translate(SkIntToScalar(layer->rect().fLeft),
2011 SkIntToScalar(layer->rect().fTop));
2012 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002013
robertphillips1ad00e42014-07-08 08:28:08 -07002014 SkPictureRangePlayback rangePlayback(picture,
2015 info.fSaveLayerOpID,
2016 info.fRestoreOpID);
2017 rangePlayback.draw(canvas, NULL);
robertphillips952841b2014-06-30 08:26:50 -07002018
robertphillips4ec84da2014-06-24 13:10:43 -07002019 canvas->flush();
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00002020 }
2021 }
2022 }
2023
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002024 // Playback using new layers
robertphillipsce4dd3d2014-07-07 13:46:35 -07002025 SkPicturePlayback playback(picture);
2026
2027 playback.setReplacements(&replacements);
2028 playback.draw(canvas, NULL);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002029
robertphillips4ec84da2014-06-24 13:10:43 -07002030 // unlock the layers
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002031 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
robertphillips4ec84da2014-06-24 13:10:43 -07002032 GrCachedLayer* layer = fContext->getLayerCache()->findLayer(picture, i);
2033 fContext->getLayerCache()->unlock(layer);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002034 }
2035
2036 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002037}